<?php
defined( 'ABSPATH' ) || exit;

if (! class_exists('Gridpress_System_Requirements')) {
    class Gridpress_System_Requirements {

        /**
         * Minimum required PHP version.
         *
         * @var string
         */
        private static $gridpress_minimum_php_version = '7.4';

        /**
         * Minimum required MySQL version.
         *
         * @var string
         */
        private static $gridpress_minimum_mysql_version = '8.0';

        /**
         * Check minimum PHP version requirement.
         *
         * @return void
         */
        public static function gridpress_check_php_version() {

            $gridpress_current_php_version   = (string) PHP_VERSION;
            $gridpress_required_php_version  = (string) self::$gridpress_minimum_php_version;

            if ( version_compare( $gridpress_current_php_version, $gridpress_required_php_version, '<' ) ) {

                wp_die(
                    sprintf(
                        /* translators: 1: required PHP version, 2: current PHP version */
                        esc_html__(
                            'This plugin requires PHP version %1$s or higher. Your current PHP version is %2$s.',
                            'gridpress-grid-layout-design'
                        ),
                        esc_html( $gridpress_required_php_version ),
                        esc_html( $gridpress_current_php_version )
                    ),
                    esc_html__( 'Plugin Requirements Not Met', 'gridpress-grid-layout-design' ),
                    array(
                        'back_link' => true,
                    )
                );
            }
        }

        /**
         * Check minimum MySQL version requirement.
         *
         * @return void
         */
        public static function gridpress_check_db_version() {

            global $wpdb;

            if ( ! ( $wpdb instanceof wpdb ) ) {
                return;
            }

            $gridpress_mysql_version = $wpdb->db_version();
            $gridpress_required_mysql_version = self::$gridpress_minimum_mysql_version;

            if ( version_compare( $gridpress_mysql_version, $gridpress_required_mysql_version, '<' ) ) {

                wp_die(
                    sprintf(
                        /* translators: 1: required MySQL version, 2: current MySQL version */
                        esc_html__(
                            'This plugin requires MySQL version %1$s or higher. Your current MySQL version is %2$s.',
                            'gridpress-grid-layout-design'
                        ),
                        esc_html( $gridpress_required_mysql_version ),
                        esc_html( $gridpress_mysql_version )
                    ),
                    esc_html__( 'Plugin Requirements Not Met', 'gridpress-grid-layout-design' ),
                    array(
                        'back_link' => true,
                    )
                );
            }
        }

        /**
         * Run all system requirement checks.
         *
         * @return void
         */
        public static function gridpress_init() {
            self::gridpress_check_php_version();
            self::gridpress_check_db_version();
        }
    }
}