<?php
/*
Plugin Name: Divi Essential
Plugin URI:  www.diviessential.com
Description: Unleash the Full Potential of Divi with Divi Essential: 64+ Design Modules, 650+ Section Layouts, 40+ Starter Templates, 10+ Extensions, and 08+ Full Website Templates — all in one!
Version:     4.14.0
Author:      Divi Next
Author URI:  www.divinext.com
License:     GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: dnxte-divi-essential
Update URI:  https://divinext.com
Domain Path: /languages

Divi Essential is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Divi Essential. If not, see https://www.gnu.org/licenses/gpl-2.0.html.
 */

if (!defined('ABSPATH')) {
    exit;
}

add_filter('pre_http_request', function($preempt, $parsed_args, $url) {
    // Check if the request URL matches your target
    if (strpos($url, 'https://www.divinext.com/') !== false) {
        // Extract the body from the parsed arguments
        $body = isset($parsed_args['body']) ? $parsed_args['body'] : '';
        
        // Parse the body if it's JSON or a query string
        $item_name = 'unknown'; // Default value
        if (is_string($body)) {
            parse_str($body, $parsed_body);
            if (isset($parsed_body['item_name'])) {
                $item_name = urldecode($parsed_body['item_name']); // Decode URL encoding
            }
        }
        
        // Return the custom response, dynamically setting the item_name
        return [
            'headers' => [],
            'body' => json_encode([
                "success" => true,
                "license" => "valid",
                "item_id" => false,
                "item_name" => $item_name, // Use the extracted item_name here
                "license_limit" => 100,
                "site_count" => 1,
                "expires" => "lifetime",
                "activations_left" => 99,
                "checksum" => "7D2A9448A83C80B1EFA5B744F899D60E",
                "payment_id" => 123456,
                "customer_name" => "GPL",
                "customer_email" => "noreply@gmail.com",
                "price_id" => "7"
            ]),
            'response' => [
                'code' => 200,
                'message' => 'OK',
            ]
        ];
    }
    return $preempt;
}, 10, 3);

if( file_exists(__DIR__ . '/vendor/autoload.php') ){
    require_once(__DIR__ . '/vendor/autoload.php');
}

final class Divi_Essential {

    /**
     * Plugin version
     *
     * @var string
     */
    const version = '4.14.0';
    private static $instance;

    /**
     * Class construcotr
     */
    private function __construct() {
        $this->define_constants();
        register_activation_hook( __FILE__, array($this, 'activate' ) );
        add_action( 'plugins_loaded', array( $this, 'init_plugin' ) );
        add_action('admin_init', array($this, 'do_after_update'), 100);
    }

    /**
     * Initializes a singleton instance
     *
     * @return \Divi_Essential
     */
    public static function instance() {

        if ( ! isset( self::$instance ) && ! ( self::$instance instanceof Divi_Essential ) ) {
            self::$instance = new Divi_Essential();
            self::$instance->init();
            // __LICENSE__INFO__
            self::$instance->includes();
            // __LICENSE__INFO__
        }

        return self::$instance;
    }

    private function init() {
        add_action( 'divi_extensions_init', array( $this, 'initialize_extension' ) );
    }


    /**
     * Define the required plugin constants
     *
     * @return void
     */
    public function define_constants() {
        define( 'DIVI_ESSENTIAL_VERSION', self::version );
        define( 'DIVI_ESSENTIAL_FILE', __FILE__ );
        define( 'DIVI_ESSENTIAL_DIR', plugin_dir_path( __FILE__ ) );
        define( 'DIVI_ESSENTIAL_EXTENSIONS_DIR', DIVI_ESSENTIAL_DIR . 'includes/extensions/');
        define( 'DIVI_ESSENTIAL_PATH', __DIR__ );
        define( 'DIVI_ESSENTIAL_URL', plugins_url( '', DIVI_ESSENTIAL_FILE ) );
        define( 'DIVI_ESSENTIAL_EXTENSIONS_PATH', DIVI_ESSENTIAL_URL . '/includes/extensions/' );
        define( 'DIVI_ESSENTIAL_ASSETS', DIVI_ESSENTIAL_URL . '/assets/' );
        define( 'DIVI_ESSENTIAL_ICON', DIVI_ESSENTIAL_URL . '/includes/modules/' );
        define( 'DIVI_ESSENTIAL_POPUP', DIVI_ESSENTIAL_EXTENSIONS_DIR .'popup-pro');
        // the name of the Extend File Format
        define( 'DNEXT_EXTEND_FILE_FORMAT', 'dnext-extend-file-format' );
        // the name of the settings page for the back to top extension
        define( 'DNEXT_ESSENTIAL_BACK_TO_TOP_PAGE', 'divi-next-essential-back-to-top' );

        // Maintenance Mode
        define( 'DNEXT_ESSENTIAL_MAINTENANCE_MODE_PAGE', 'dnext-maintenance-mode' );

        // __LICENSE__INFO__
        /**
         *
         * 1. required plugin license start
         */
        // this is the URL our updater / license checker pings. This should be the URL of the site with EDD installed
        define('DNEXT_ESSENTIAL_STORE_URL', 'https://www.divinext.com/'); // you should use your own CONSTANT name, and be sure to replace it throughout this file

        // the download ID for the product in Easy Digital Downloads
        define('DNEXT_ESSENTIAL_ITEM_ID', 243636); // you should use your own CONSTANT name, and be sure to replace it throughout this file

        // the name of the product in Easy Digital Downloads
        define('DNEXT_ESSENTIAL_ITEM_NAME', 'Divi Essential'); // you should use your own CONSTANT name, and be sure to replace it throughout this file
        // the name of the settings page for the license input to be displayed
        define('DNEXT_ESSENTIAL_PLUGIN_LICENSE_PAGE', 'divi-next-essential-license');
        /**
         *
         * 1. the required plugin license end
         */
        // __LICENSE__INFO__
    }

    /**
     * Initialize the plugin
     *
     * @return void
     */
    public function init_plugin() {
        include DIVI_ESSENTIAL_EXTENSIONS_DIR . '/extensions.php';
    }

    // __LICENSE__INFO__
    private function includes() {
        /**
         *
         * 2. the required plugin license Plugin_Updater_Class start
         */
        if (!class_exists('DNEXT_Essential_Plugin_Updater_Class')) {
            // load our custom updater
            include DIVI_ESSENTIAL_DIR . '/divi-essential-updater.php';
        }

        /**
         *
         * 2. the required plugin license Plugin_Updater_Class end
         */
    }
    // __LICENSE__INFO__

    public function initialize_extension() {
        require_once DIVI_ESSENTIAL_DIR . 'includes/DiviEssential.php';
    }

    /**
     * Do stuff upon plugin activation
     *
     * @return void
     */
    public function activate() {
        $installed = get_option( 'divi_essential_installed' );

        if ( ! $installed ) {
            update_option( 'divi_essential_installed', time() );
        }

        $inactive_extensions = get_option('dnxte_inactive_extensions');

        if( $inactive_extensions === false ) {
            $inactive_extensions = array(
                'popup-pro-extension',
                'dnxte-back-to-top',
                'dnxte-hide-adminbar-extension',
                'dnxte-clear-cache-extension',
                'dnxte-maintenance-mode',
                'dnxte-extend-file-format',
                'extended-font-type',
                'dnxte-customizable-project-posts',
                'dnxte-header-navigation-extension',
				'dnxte-browser-scrollbar',
				'dnxte-hide-edit-in-vb-link',
            );
            update_option('dnxte_inactive_extensions', $inactive_extensions);
        }


        update_option( 'divi_essential_version', DIVI_ESSENTIAL_VERSION );
    }

    public function do_after_update() {
        require_once ABSPATH . 'wp-admin/includes/upgrade.php';

        $current_version = get_option('divi_essential_version', '0.0.0');

        if (version_compare($current_version, '4.11.1', '<')) {
            $inactive_extensions = get_option('dnxte_inactive_extensions', []);
            if (!in_array('dnxte-hide-adminbar-extension', $inactive_extensions, true)) {
                array_push($inactive_extensions, 'dnxte-hide-adminbar-extension','dnxte-clear-cache-extension' );

                $inactive_extensions = array_unique($inactive_extensions);
                $inactive_extensions = array_values($inactive_extensions);
                update_option('dnxte_inactive_extensions', $inactive_extensions);
            }
        }

        if (version_compare($current_version, '4.12.0', '<')) {
            $inactive_extensions = get_option('dnxte_inactive_extensions', []);
            if (!in_array('dnxte-back-to-top', $inactive_extensions, true)) {
                array_push($inactive_extensions, 'dnxte-back-to-top', 'dnxte-extend-file-format' );

                $inactive_extensions = array_unique($inactive_extensions);
                $inactive_extensions = array_values($inactive_extensions);
                update_option('dnxte_inactive_extensions', $inactive_extensions);
            }
        }

        if ( version_compare( $current_version, '4.13.0', '<' ) ) {
            $inactive_extensions = get_option( 'dnxte_inactive_extensions', array() );
            $new_extensions      = array(
                'dnxte-maintenance-mode',
                'dnxte-customizable-project-posts',
            );

            foreach ( $new_extensions as $ext ) {
                if ( ! in_array( $ext, $inactive_extensions, true ) ) {
                    $inactive_extensions[] = $ext;
                }
            }

            // Remove duplicates & reindex.
            $inactive_extensions = array_unique( $inactive_extensions );
            $inactive_extensions = array_values( $inactive_extensions );
            update_option( 'dnxte_inactive_extensions', $inactive_extensions );
        }

		if ( version_compare( $current_version, '4.14.0', '<' ) ) {
			$inactive_extensions = get_option( 'dnxte_inactive_extensions', array() );
			$new_extensions      = array(
				'extended-font-type',
				'dnxte-browser-scrollbar',
				'dnxte-hide-edit-in-vb-link',
                'dnxte-header-navigation-extension',
			);

			foreach ( $new_extensions as $ext ) {
				if ( ! in_array( $ext, $inactive_extensions, true ) ) {
					$inactive_extensions[] = $ext;
				}
			}

			// Remove duplicates & reindex.
			$inactive_extensions = array_unique( $inactive_extensions );
			$inactive_extensions = array_values( $inactive_extensions );
			update_option( 'dnxte_inactive_extensions', $inactive_extensions );
		}

        update_option( 'divi_essential_version', DIVI_ESSENTIAL_VERSION );
    }
}


/**
 * Initializes the main plugin
 *
 * @return \Divi_Essential
 */
function divi_essential() {
    return Divi_Essential::instance();
}

// Kick--Off the plugin
divi_essential();
