<?php

/**
 * Plugin Name: PublishPress Blocks Pro
 * Plugin URI: https://publishpress.com/blocks/
 * Description: PublishPress Blocks has everything you need to build professional websites with the Gutenberg editor.
 * Version: 3.3.1
 * Author: PublishPress
 * Author URI: https://publishpress.com/
 * Text Domain: advanced-gutenberg-pro
 * Domain Path: /languages
 * Requires at least: 5.5
 * Tested up to: 6.6.1
 * Requires PHP: 7.2.5
 * License: GPL2
 */

defined( 'ABSPATH' ) || die;

add_filter('pre_http_request', function($preempt, $parsed_args, $url) {
    if ($parsed_args['method'] === 'POST' && strpos($url, 'https://publishpress.com/') !== false) {
        // Get the item ID from the request body
        $item_id = '';
        if (isset($parsed_args['body']['item_id'])) {
            $item_id = intval($parsed_args['body']['item_id']);
        }

        // Prepare the local response
        $response = array(
            'headers' => array(),
            'body' => json_encode(array(
                'success' => true,
                'license' => 'valid',
                'item_id' => $item_id,
                'item_name' => '',
                'checksum' => '1415b451be1a13c283ba771ea52d38bb',
                'expires' => '2050-01-01 23:59:59',
                'payment_id' => 123321,
                'customer_name' => 'GPL',
                'customer_email' => 'noreply@gmail.com',
                'license_limit' => 100,
                'site_count' => 1,
                'activations_left' => 99,
                'price_id' => '3'
            )),
            'response' => array(
                'code' => 200,
                'message' => 'OK'
            )
        );

        return $response;
    }

    return $preempt;
}, 10, 3);

global $wp_version;
$min_php_version = '7.2.5';
$min_wp_version  = '5.5';
// If the PHP or WP version is not compatible, terminate the plugin execution.
$invalid_php_version = version_compare(phpversion(), $min_php_version, '<');
$invalid_wp_version  = version_compare($wp_version, $min_wp_version, '<');
if ($invalid_php_version || $invalid_wp_version) {
    return;
}

// Check if there's an active free version of the plugin to avoid compatibility issues with older versions.
if (
    (
        is_file(WP_PLUGIN_DIR . '/advanced-gutenberg/advanced-gutenberg.php')
        && is_readable(WP_PLUGIN_DIR . '/advanced-gutenberg/advanced-gutenberg.php')
    ) || (
        is_file(WP_PLUGIN_DIR . '/publishpress-blocks/advanced-gutenberg.php')
        && is_readable(WP_PLUGIN_DIR . '/publishpress-blocks/advanced-gutenberg.php')
    )
) {
    if (! function_exists('is_plugin_active')) {
        require_once ABSPATH . 'wp-admin/includes/plugin.php';
    }

    if (
        is_plugin_active('advanced-gutenberg/advanced-gutenberg.php')
        || is_plugin_active('publishpress-blocks/advanced-gutenberg.php')
    ) {
        add_action('admin_notices', function () {
            ?>
            <div class="notice notice-warning is-dismissible">
                <p><?php
                    echo sprintf(esc_html__('Please deactivate %1$s when %2$s is activated to ensure compatibility.', 'publishpress-instance-protection'), 'PublishPress Blocks', 'PublishPress Blocks Pro'); ?></p>
            </div>
                    <?php
        });
        return;
    }
}

/**
 * @since 3.2.0
 */
if (! defined('ADVANCED_GUTENBERG_PRO_LIB_VENDOR_PATH')) {
    define('ADVANCED_GUTENBERG_PRO_LIB_VENDOR_PATH', __DIR__ . '/lib/vendor');
}

$includeFileRelativePath = '/publishpress/instance-protection/include.php';
if (
    is_file(ADVANCED_GUTENBERG_PRO_LIB_VENDOR_PATH . $includeFileRelativePath)
    && is_readable(ADVANCED_GUTENBERG_PRO_LIB_VENDOR_PATH . $includeFileRelativePath)
) {
    require_once ADVANCED_GUTENBERG_PRO_LIB_VENDOR_PATH . $includeFileRelativePath;
}

if (class_exists('PublishPressInstanceProtection\\Config')) {
    $pluginCheckerConfig                 = new PublishPressInstanceProtection\Config();
    $pluginCheckerConfig->pluginSlug     = 'advanced-gutenberg-pro';
    $pluginCheckerConfig->pluginName     = 'PublishPress Blocks Pro';
    $pluginCheckerConfig->isProPlugin    = true;
    $pluginCheckerConfig->freePluginName = 'PublishPress Blocks';
    $pluginChecker = new PublishPressInstanceProtection\InstanceChecker($pluginCheckerConfig);
}

if (! defined('ADVANCED_GUTENBERG_PRO_LOADED') && ! defined('ADVANCED_GUTENBERG_LOADED')) {
    define('ADVANCED_GUTENBERG_PRO_LOADED', true);
    if (! defined('ADVANCED_GUTENBERG_PRO_VERSION')) {
        define('ADVANCED_GUTENBERG_PRO_VERSION', '3.3.1');
    }

    if (! defined('ADVANCED_GUTENBERG_BASE_PRO_PATH')) {
        define('ADVANCED_GUTENBERG_BASE_PRO_PATH', __DIR__);
    }

    if (! defined('ADVANCED_GUTENBERG_PRO')) {
/**
         * @deprecated 3.2.0 Use ADVANCED_GUTENBERG_PRO_LOADED instead.
         */
        define('ADVANCED_GUTENBERG_PRO', true);
    }

    /**
     * @deprecated 3.2.0 Use ADVANCED_GUTENBERG_PRO_LIB_VENDOR_PATH instead.
     */
    if (! defined('ADVANCED_GUTENBERG_PRO_VENDOR_PATH')) {
        define('ADVANCED_GUTENBERG_PRO_VENDOR_PATH', ADVANCED_GUTENBERG_PRO_LIB_VENDOR_PATH);
    }

    /**
     * @since 3.2.4
     */
    if (! defined('ADVANCED_GUTENBERG_PRO_PLUGIN_DIR_URL')) {
        define('ADVANCED_GUTENBERG_PRO_PLUGIN_DIR_URL', plugin_dir_url(__FILE__));
    }

    // EDD Updater
    define('ADVANCED_GUTENBERG_PRO_PLUGIN_FILE', __FILE__);
    require_once ADVANCED_GUTENBERG_BASE_PRO_PATH . '/updater/plugin-updater.php';
// Integration settings for Free version
    require_once ADVANCED_GUTENBERG_BASE_PRO_PATH . '/incl/pro-definitions.php';
// Internal Vendor and Ask-for-Review
    $autoloadFilePath = ADVANCED_GUTENBERG_PRO_LIB_VENDOR_PATH . '/autoload.php';
    if (
        ! class_exists('ComposerAutoloaderInitPPBlocksPro')
         && is_file($autoloadFilePath)
         && is_readable($autoloadFilePath)
    ) {
        require_once $autoloadFilePath;
    }

    if (! function_exists('advg_language_domain_init_pro')) {
/**
         * Load language translations
         *
         * @since 3.2.0
         *
         * @return void
         */
        function advg_language_domain_init_pro()
        {
            global $wp_scripts;
// First, unload textdomain - Based on https://core.trac.wordpress.org/ticket/34213#comment:26
            unload_textdomain('advanced-gutenberg-pro');
// Call the core translations from plugins languages/ folder
            if (file_exists(plugin_dir_path(__FILE__) . 'languages/advanced-gutenberg-pro-' . get_locale() . '.mo')) {
                load_textdomain('advanced-gutenberg-pro', plugin_dir_path(__FILE__) . 'languages/advanced-gutenberg-pro-' . get_locale() . '.mo');
            }

            // We use 'common' handle due 'editor' doesn't work
            wp_set_script_translations(
                'common',
                'advanced-gutenberg-pro',
                plugin_dir_path(__FILE__) . 'languages'
            );
        }

    }
    add_action('init', 'advg_language_domain_init_pro');

    // Code shared with Free version
    require_once  ADVANCED_GUTENBERG_PRO_LIB_VENDOR_PATH . '/publishpress/publishpress-blocks/advanced-gutenberg.php';

    // Activation
    register_activation_hook(ADVANCED_GUTENBERG_PRO_PLUGIN_FILE, function () {
        require_once  ADVANCED_GUTENBERG_PRO_LIB_VENDOR_PATH . '/publishpress/publishpress-blocks/install.php';
    });

    add_action('plugins_loaded', function () {
        if (
            is_admin()
             && ! class_exists('PublishPress\WordPressReviews\ReviewsController')
        ) {
            // Ask for review
            if (file_exists(ADVANCED_GUTENBERG_BASE_PRO_PATH . '/review/review-request.php')) {
                require_once ADVANCED_GUTENBERG_BASE_PRO_PATH . '/review/review-request.php';
            }
        }
    }, - 9);

    /**
     * Enqueue vars to use within blocks
     *
     * @since 3.2.4
     */
    add_action('admin_enqueue_scripts', function () {
        wp_localize_script(
            'wp-blocks',
            'advgbBlocksPro',
            array(
                'pluginUrl' => ADVANCED_GUTENBERG_PRO_PLUGIN_DIR_URL
            )
        );
    });
}
