<?php

/**
 * Plugin Name:       Privex Password Protect – Lock Pages, Posts & Categories
 * Description:       A professional-grade password protection solution to keep your WordPress content truly private. Combine premium aesthetics with hardened security like Brute Force protection and CAPTCHA.
 * Version:           1.0.1
 * Author:            shoiswp
 * Author URI:        https://profiles.wordpress.org/shoiswp/
 * License:           GPL-2.0+
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Text Domain:       privex-password-protect
 * Domain Path:       /languages
 * Requires at least: 5.8
 * Requires PHP:      7.4
 */
// Direct access guard.
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
// --- Freemius Auto-Deactivation Support ---
// When the PRO version is activated, it deactivates the free version automatically.
if ( function_exists( 'ppp_fs' ) ) {
    ppp_fs()->set_basename( false, __FILE__ );
} else {
    /**
     * Freemius SDK Integration.
     * DO NOT REMOVE the function_exists check — it is required for the
     * auto-deactivation mechanism to work correctly.
     */
    if ( !function_exists( 'ppp_fs' ) ) {
        /**
         * Helper function to access the Freemius SDK instance.
         *
         * @return Freemius
         */
        function ppp_fs() {
            global $ppp_fs;
            if ( !isset( $ppp_fs ) ) {
                // Include Freemius SDK.
                require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';
                $ppp_fs = fs_dynamic_init( array(
                    'id'               => '29385',
                    'slug'             => 'privex-password-protect',
                    'type'             => 'plugin',
                    'public_key'       => 'pk_31b16b3f9101e28a349916bb61a1d',
                    'is_premium'       => false,
                    'premium_suffix'   => 'Premium',
                    'has_addons'       => false,
                    'has_paid_plans'   => true,
                    'is_org_compliant' => true,
                    'menu'             => array(
                        'slug'    => 'ppmstr-dashboard',
                        'account' => true,
                        'contact' => true,
                        'support' => false,
                    ),
                    'is_live'          => true,
                ) );
            }
            return $ppp_fs;
        }

        // Init Freemius.
        ppp_fs();
        // Signal that SDK was initiated.
        do_action( 'ppp_fs_loaded' );
        /**
         * Freemius uninstall cleanup.
         * Using add_action('after_uninstall') instead of register_uninstall_hook()
         * so that Freemius can collect uninstall feedback first.
         */
        function ppp_fs_uninstall_cleanup() {
            $ppmstr_s = get_option( 'ppmstr_settings', array() );
            // Only delete data if the setting is enabled.
            if ( !empty( $ppmstr_s['delete_on_uninstall'] ) ) {
                // Remove plugin options.
                delete_option( 'ppmstr_settings' );
                delete_option( 'ppmstr_version' );
                delete_option( 'ppmstr_active_sessions' );
                delete_option( 'ppmstr_active_template' );
                delete_option( 'ppmstr_templates' );
                delete_option( 'ppmstr_custom_templates' );
                // Remove all post meta created by this plugin.
                global $wpdb;
                // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
                $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE %s", $wpdb->esc_like( '_ppmstr_' ) . '%' ) );
                // Clear transients.
                delete_transient( 'ppmstr_locked_ids' );
            }
        }

        ppp_fs()->add_action( 'after_uninstall', 'ppp_fs_uninstall_cleanup' );
    }
    // =========================================================================
    // Plugin Constants
    // =========================================================================
    define( 'PPMSTR_VERSION', '1.0.1' );
    define( 'PPMSTR_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
    define( 'PPMSTR_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
    define( 'PPMSTR_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
    define( 'PPMSTR_SLUG', 'privex-password-protect' );
    // =========================================================================
    // Activation & Deactivation Hooks
    // =========================================================================
    /**
     * Plugin activation callback.
     */
    function ppmstr_activate() {
        require_once PPMSTR_PLUGIN_DIR . 'includes/class-ppmstr-activator.php';
        PPMSTR_Activator::activate();
    }

    register_activation_hook( __FILE__, 'ppmstr_activate' );
    /**
     * Plugin deactivation callback.
     */
    function ppmstr_deactivate() {
        require_once PPMSTR_PLUGIN_DIR . 'includes/class-ppmstr-deactivator.php';
        PPMSTR_Deactivator::deactivate();
    }

    register_deactivation_hook( __FILE__, 'ppmstr_deactivate' );
    // =========================================================================
    // Core Includes — always loaded.
    // =========================================================================
    require_once PPMSTR_PLUGIN_DIR . 'includes/class-ppmstr-i18n.php';
    require_once PPMSTR_PLUGIN_DIR . 'includes/core/class-ppmstr-password-validator.php';
    require_once PPMSTR_PLUGIN_DIR . 'includes/core/class-ppmstr-cookie-manager.php';
    require_once PPMSTR_PLUGIN_DIR . 'includes/core/class-ppmstr-access-checker.php';
    require_once PPMSTR_PLUGIN_DIR . 'includes/core/class-ppmstr-protection-engine.php';
    require_once PPMSTR_PLUGIN_DIR . 'includes/core/class-ppmstr-visibility-handler.php';
    require_once PPMSTR_PLUGIN_DIR . 'includes/security/class-ppmstr-security-handler.php';
    // =========================================================================
    // Context-Specific Includes.
    // =========================================================================
    if ( is_admin() ) {
        require_once PPMSTR_PLUGIN_DIR . 'admin/class-ppmstr-admin.php';
        require_once PPMSTR_PLUGIN_DIR . 'admin/class-ppmstr-settings.php';
        require_once PPMSTR_PLUGIN_DIR . 'admin/class-ppmstr-metabox.php';
        require_once PPMSTR_PLUGIN_DIR . 'admin/class-ppmstr-password-admin.php';
        require_once PPMSTR_PLUGIN_DIR . 'admin/class-ppmstr-template-builder.php';
    }
    if ( !is_admin() || wp_doing_ajax() ) {
        require_once PPMSTR_PLUGIN_DIR . 'public/class-ppmstr-public.php';
        require_once PPMSTR_PLUGIN_DIR . 'public/class-ppmstr-form-renderer.php';
        require_once PPMSTR_PLUGIN_DIR . 'public/class-ppmstr-shortcodes.php';
        require_once PPMSTR_PLUGIN_DIR . 'public/class-ppmstr-ajax-handler.php';
    }
    // =========================================================================
    // Bootstrap — fires on plugins_loaded.
    // =========================================================================
    /**
     * Initialize all plugin components.
     */
    function ppmstr_init() {
        // Internationalization.
        $i18n = new PPMSTR_I18n();
        $i18n->load();
        // Core services.
        $validator = new PPMSTR_Password_Validator();
        $cookies = new PPMSTR_Cookie_Manager();
        $access = new PPMSTR_Access_Checker($validator, $cookies);
        $engine = new PPMSTR_Protection_Engine($access, $cookies);
        $engine->init();
        $visibility = new PPMSTR_Visibility_Handler($access);
        $visibility->init();
        if ( is_admin() ) {
            $admin = new PPMSTR_Admin();
            $admin->init();
            $settings = new PPMSTR_Settings();
            $settings->init();
            $metabox = new PPMSTR_Metabox();
            $metabox->init();
            $pass_admin = new PPMSTR_Password_Admin();
            $pass_admin->init();
            $tpl_builder = new PPMSTR_Template_Builder();
            $tpl_builder->init();
        }
        if ( !is_admin() || wp_doing_ajax() ) {
            $renderer = new PPMSTR_Form_Renderer();
            $public = new PPMSTR_Public($engine, $renderer);
            $public->init();
            $shortcodes = new PPMSTR_Shortcodes();
            $shortcodes->init();
            $ajax = new PPMSTR_Public_AJAX_Handler();
            $ajax->init();
        }
    }

    add_action( 'plugins_loaded', 'ppmstr_init' );
}
// End Freemius wrapper.