<?php
/**
 * Plugin Name:       GDPR Consent Lite
 * Plugin URI:        https://www.wpupgrader.com/gdpr-consent-wordpress-plugin/
 * Description:       Allow visitors to grant explicit consent for use with your site, which helps you to comply with GDPR standards. Works with custom Themes, Multisites and WPML enabled sites.
 * Version:           3.0.10
 * Tested up to:      5.5
 * Requires at least: 4.5.1
 * Requires PHP:      5.6.2
 * Author:            WP Upgrader
 * Author URI:        https://www.wpupgrader.com/
 * License:           GPLv2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Domain Path:       /languages
 * Text Domain:       gdprc_consent
 */

namespace Wpupgrader\WordPress\Plugins\GdprConsentPlugin;

if (!class_exists('Wpupgrader\WordPress\Plugins\GdprConsentPlugin\GdprConsentPlugin')) {
    require_once __DIR__.'/vendor/autoload.php';
    require_once __DIR__.'/Library/utilities.php';

    /**
     * Class GdprConsentPlugin implements the Singleton pattern
     * You may use GdprConsentPlugin::getInstance() anywhere to access this class.
     *
     * @package Wpupgrader\WordPress\Plugins
     */
    final class GdprConsentPlugin extends \Wpupgrader\Libraries\WordPress\Plugins\Core\Core
    {
        const NAME    = 'GDPR Consent Lite';
        const VERSION = '3.0.10';

        public $pluginfile = __FILE__;
        public $settings;
        public $license_page;
        public $license_settings;
        public $updater;
        public $import_export_page;
    
        static $settingsOnPage = false;

        public function __construct()
        {
            if (\gdprConsentPluginIsProEdition()) {
                $config = new \Wpupgrader\WordPress\Plugins\GdprConsentPro\Config($this);
                \add_filter('admin_body_class', function($classes) {
                    return $classes.' gdprc-is-pro';
                });
            }
            else {
                $config = new \Wpupgrader\WordPress\Plugins\GdprConsentLite\Config($this);
                \add_filter('admin_body_class', function($classes) {
                    return $classes.' gdprc-is-lite';
                });
            }

            parent::__construct($config);

            if (\gdprConsentPluginIsProEdition()) {
                \Wpupgrader\WordPress\Plugins\GdprConsentPro\Hooks\Autoloader::Factory($this, $this->getConfig()->get('source_path').'/Hooks');
                $this->settings = new \Wpupgrader\WordPress\Plugins\GdprConsentPro\Settings\Page($this->getConfig());
                $this->settings->addSettingsPage();

                $this->license_settings = new \Wpupgrader\WordPress\Plugins\GdprConsentPro\Settings\LicenseSettings($this->getConfig());
                $this->license_page = new \Wpupgrader\WordPress\Plugins\GdprConsentPro\Settings\LicensePage($this->getConfig(), $this->license_settings);
                $this->license_page->addSettingsPage();

                \add_action(\is_multisite() ? "network_admin_notices" : "admin_notices", function() {
                    if ($this->license_page->isViewing()) {
                        return;
                    }
                    $error = null;
                    try {
                        $license = $this->license_settings->getLicense();
                        if ($license == null) {
                            $error = \__("You have no active or valid license?", "gdpr_consent");
                        }
                        else if (!$license->isValid()) {
                            $error = $license->getStateDescription();
                        }
                    }
                    catch (\Wpupgrader\WordPress\Plugins\GdprConsentPro\WPUpgrader\LicenseError $e) {
                        $error = $e->getMessage();
                    }
                    catch (\Wpupgrader\WordPress\Plugins\GdprConsentPro\WPUpgrader\APIException $e) {
                        // We ignore this error.
                    }

                    if (!$error) {
                        return;
                    }

                    \printf('<div class="notice notice-error"><p><strong><!--span class="gdpri gdpri-warning"></span--> GDPR Consent Pro</strong></p><p>%s <a href="%s">%s</a> (Some features may be disabled!)</p></div>', $error, $this->license_page->getUrl(), \__("Please update your license key.", "gdpr_constent"), \esc_html($error));
                });

                $this->import_export_page = new \Wpupgrader\WordPress\Plugins\GdprConsentPro\Settings\ImportExportPage($this->getConfig());
                $this->import_export_page->addSettingsPage();
            }
            else {
                \Wpupgrader\WordPress\Plugins\GdprConsentLite\Hooks\Autoloader::Factory($this, $this->getConfig()->get('source_path').'/Hooks');
                $this->settings = new \Wpupgrader\WordPress\Plugins\GdprConsentLite\Settings\Page($this->getConfig());
                $this->settings->addSettingsPage();

                \add_filter('plugin_action_links', function($links, $file) {
                    if (\plugin_basename(\dirname(__FILE__).'/GdprConsentPlugin.php') == $file) {
                        $links["upgrade"] = "<a href='https://www.wpupgrader.com/wordpress-plugins/gdpr-consent/#pricing' target='_blank'><b>".__('Upgrade to Pro!', 'gdprc_consent').'</b></a>';
                    }
                    return $links;
                }, 10, 2);

                global $pagenow;
                if ($pagenow === "admin.php" || $pagenow === "plugins.php") {
                    $this->upgrade_notice();
                }
            }
        }

        public function upgrade_notice()
        {
            if (\is_multisite() && \gdprc_wpml_plugin_install()) {
                \add_action('admin_notices', function() {
                    ?>
					<div class="notice notice-info is-dismissible">
						<p><b>Hey!</b> You are using Multisite with WPML. Did you consider upgrading to
							<a href='https://www.wpupgrader.com/wordpress-plugins/gdpr-consent/#pricing' target='_blank'>GDPR Consent Pro</a>for Multisite and WPML Support?</p>
					</div>
                    <?php
                });
            }
            else if (\is_multisite()) {
                \add_action('admin_notices', function() {
                    ?>
					<div class="notice notice-info is-dismissible">
						<p><b>Hey!</b> You are using Multisite. Did you consider upgrading to
							<a href='https://www.wpupgrader.com/wordpress-plugins/gdpr-consent/#pricing' target='_blank'>GDPR Consent Pro</a> for Multisite Support?</p>
					</div>
                    <?php
                });
            }
            else if (\gdprc_wpml_plugin_install()) {
                \add_action('admin_notices', function() {
                    ?>
					<div class="notice notice-info is-dismissible">
						<p><b>Hey!</b> You are using WPML. Did you consider upgrading to <a href='https://www.wpupgrader.com/wordpress-plugins/gdpr-consent/#pricing' target='_blank'>GDPR Consent Pro</a> for WPML Support?
						</p>
					</div>
                    <?php
                });
            }
        }

        public function dependent_check()
        {
            $notice = null;
            $error = null;

            // Make sure mu-plugin is in place
            if (!\array_key_exists('gdprc-mu-plugin.php', \get_mu_plugins())) {
                if (!isset($_GET['action']) || \sanitize_key($_GET['action']) != 'activate') {
                    if (!\is_dir(\trailingslashit(WP_CONTENT_DIR).'mu-plugins/')) {
                        \mkdir(\trailingslashit(WP_CONTENT_DIR).'mu-plugins/');
                    }

                    \copy(\trailingslashit(__DIR__).'mu/gdprc-mu-plugin.php', \trailingslashit(WP_CONTENT_DIR).'mu-plugins/gdprc-mu-plugin.php');

                    if (!\file_exists(\trailingslashit(WP_CONTENT_DIR).'mu-plugins/gdprc-mu-plugin.php')) {
                        $error = \__('Error: This needs writing permission to /wp-content/mu-plugins. Please consult your hosting provider. Deactivating plugin.', 'gdprc_consent');
                    }
                    else {
                        if (isset($_GET['activate'])) {
                            $notice = \__('GDPR Consent Plugin is activated succesfully, and the must-use script is installed too!', 'gdprc_consent');
                        }
                        else {
                            $notice = \__('GDPR Consent Plugin noticed the must-use script was missing. It has just been reinstalled succesfully.', 'gdprc_consent');
                        }
                    }
                }
            }

            if (\get_transient('gdprc_plugin_activated')) {
                $notice = \__('GDPR Consent Plugin is activated succesfully, and the must-use script is installed too!', 'gdprc_consent');

                /* Delete transient, only display this notice once. */
                \delete_transient('gdprc_plugin_activated');
            }

            if (isset($error)) {
                \Wpupgrader\Libraries\WordPress\Plugins\Core\Notices::get_instance()->register_error($error);
                \deactivate_plugins(__FILE__); // Deactivate ourself

                if (isset($_GET['activate'])) {
                    unset($_GET['activate']);
                }
            }
            else {
                if (isset($notice)) {
                    \Wpupgrader\Libraries\WordPress\Plugins\Core\Notices::get_instance()->register_notice($notice);
                }
            }
        }

        static public function deactivation_hook()
        {
            @\unlink(\trailingslashit(WP_CONTENT_DIR).'mu-plugins/gdprc-mu-plugin.php');
        }

        static public function activation_hook()
        {
            \set_transient('gdprc_plugin_activated', true, 5); // This sets your welcome message for 5 sec
        }
    }

    $GdprConsentPlugin = GdprConsentPlugin::getInstance();

    \register_deactivation_hook(__FILE__, [$GdprConsentPlugin, 'deactivation_hook']);
    \register_activation_hook(__FILE__, [$GdprConsentPlugin, 'activation_hook']);
}
else {
    \add_action('admin_notices', function() {
        global $pagenow;
        if ($pagenow !== "plugins.php") {
            echo '<div class="notice notice-warning is-dismissible"><p><strong>Whoops!</strong> It seems there already is a version ('.GdprConsentPlugin::VERSION.') of '.GdprConsentPlugin::NAME.' installed? Please <a href="/wp-admin/plugins.php?s=GDPR+Consent&plugin_status=active">disable</a> it if you want to use <b>GDPR Consent Lite</b> version 3.0.10!</p></div>';
        }
    });
}