<?php

/**
 * Plugin Name: Easy Search Replace
 * Description: Find & replace text, HTML, or URLs across your WordPress site in real time — no database changes.
 * Version:     1.2.0
 * Author:      FluxPress
 * Author URI:  https://fluxpress.io
 * Text Domain: easy-search-replace
 * Domain Path: /languages
 * License:     GNU General Public License v2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Requires at least: 5.0
 * Requires PHP: 7.2
 *
 * @package EasySearchReplace
 */
// Exit if accessed directly.
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
/* ---------------------------------------------------------------------------
 * Plugin constants
 * ------------------------------------------------------------------------- */
if ( !defined( 'ESRN_VERSION' ) ) {
    define( 'ESRN_VERSION', '1.2.0' );
}
if ( !defined( 'ESRN_PLUGIN_FILE' ) ) {
    define( 'ESRN_PLUGIN_FILE', __FILE__ );
}
if ( !defined( 'ESRN_PLUGIN_DIR' ) ) {
    define( 'ESRN_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
}
if ( !defined( 'ESRN_PLUGIN_URL' ) ) {
    define( 'ESRN_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
}
if ( !defined( 'ESRN_PLUGIN_BASENAME' ) ) {
    define( 'ESRN_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
}
if ( !defined( 'ESRN_OPTION_KEY' ) ) {
    define( 'ESRN_OPTION_KEY', 'esrn_search_replace_options' );
}
/* ---------------------------------------------------------------------------
 * Composer autoload (loads Freemius SDK and any future Composer packages)
 * ------------------------------------------------------------------------- */
require_once __DIR__ . '/vendor/autoload.php';
/* ---------------------------------------------------------------------------
 * Freemius SDK initialization
 * ------------------------------------------------------------------------- */
if ( !function_exists( 'esrn_fs' ) ) {
    // Create a helper function for easy SDK access.
    function esrn_fs() {
        global $esrn_fs;
        if ( !isset( $esrn_fs ) ) {
            // Include Freemius SDK.
            // SDK is auto-loaded through Composer
            $esrn_fs = fs_dynamic_init( array(
                'id'               => '28354',
                'slug'             => 'easy-search-replace',
                'premium_slug'     => 'easy-search-replace-pro',
                'type'             => 'plugin',
                'public_key'       => 'pk_9b2e8b8a3228f372ccc9548cfb273',
                'is_premium'       => false,
                'premium_suffix'   => '(pro)',
                'has_addons'       => false,
                'has_paid_plans'   => true,
                'is_org_compliant' => true,
                'menu'             => array(
                    'slug'   => 'easy_search_replace',
                    'parent' => array(
                        'slug' => 'options-general.php',
                    ),
                ),
                'is_live'          => true,
            ) );
        }
        return $esrn_fs;
    }

    // Init Freemius.
    esrn_fs();
    // Signal that SDK was initiated.
    do_action( 'esrn_fs_loaded' );
}
/* ---------------------------------------------------------------------------
 * Load plugin classes and modules
 *
 * Order matters: schema + gate first (pure data/helper), then engine (needs them),
 * then the legacy procedural files (options UI, DOM lib).
 * ------------------------------------------------------------------------- */
require_once ESRN_PLUGIN_DIR . 'inc/class-rule-schema.php';
require_once ESRN_PLUGIN_DIR . 'inc/class-freemius-gate.php';
require_once ESRN_PLUGIN_DIR . 'inc/class-rule-engine.php';
require_once ESRN_PLUGIN_DIR . 'inc/options.php';
require_once ESRN_PLUGIN_DIR . 'lib/dom.php';
/* ---------------------------------------------------------------------------
 * Pro feature classes - loaded conditionally on active license.
 *
 * Each class hooks into `esrn_should_apply_rule` (a filter run by the engine
 * when deciding whether to apply a rule to the current request) to add its
 * veto check. This keeps the engine free of premium branching.
 * ------------------------------------------------------------------------- */
function esrn_load_pro_features() {
    if ( !ESRN_Freemius_Gate::is_pro() ) {
        return;
    }
    require_once ESRN_PLUGIN_DIR . 'inc/pro/class-scheduler.php';
    require_once ESRN_PLUGIN_DIR . 'inc/pro/class-roles.php';
    require_once ESRN_PLUGIN_DIR . 'inc/pro/class-device.php';
    require_once ESRN_PLUGIN_DIR . 'inc/pro/class-exclusions.php';
    ESRN_Pro_Scheduler::register();
    ESRN_Pro_Roles::register();
    ESRN_Pro_Device::register();
    ESRN_Pro_Exclusions::register();
}

add_action( 'plugins_loaded', 'esrn_load_pro_features', 8 );
/* ---------------------------------------------------------------------------
 * Bootstrap
 * ------------------------------------------------------------------------- */
/**
 * Run one-time data migration on load. Cheap - no-op after first successful run.
 */
add_action( 'plugins_loaded', array('ESRN_Rule_Schema', 'maybe_migrate'), 5 );
/**
 * Boot the rule engine.
 */
function esrn_boot_engine() {
    static $engine = null;
    if ( null === $engine ) {
        $engine = new ESRN_Rule_Engine();
        $engine->register();
    }
    return $engine;
}

add_action( 'plugins_loaded', 'esrn_boot_engine', 10 );
/* ---------------------------------------------------------------------------
 * Internationalization
 * ------------------------------------------------------------------------- */
/**
 * Load the plugin translation files.
 */
function esrn_load_textdomain() {
    load_plugin_textdomain( 'easy-search-replace', false, dirname( ESRN_PLUGIN_BASENAME ) . '/languages' );
}

add_action( 'plugins_loaded', 'esrn_load_textdomain' );
/* ---------------------------------------------------------------------------
 * Admin assets & settings link
 * ------------------------------------------------------------------------- */
/**
 * Enqueue admin assets only on the plugin's settings screen.
 *
 * @param string $hook_suffix Current admin page hook suffix.
 */
function esrn_enqueue_admin_scripts(  $hook_suffix  ) {
    // get_current_screen() is the safest way to identify our settings page.
    if ( 'settings_page_easy_search_replace' !== $hook_suffix ) {
        return;
    }
    wp_enqueue_script(
        'esrn-search-replace',
        ESRN_PLUGIN_URL . 'assets/admin.js',
        array('jquery'),
        ESRN_VERSION,
        true
    );
    wp_localize_script( 'esrn-search-replace', 'esrnAdminL10n', array(
        'ruleLabel'          => __( 'Rule %d', 'easy-search-replace' ),
        'select2Placeholder' => __( 'Select post types', 'easy-search-replace' ),
    ) );
    wp_enqueue_style(
        'esrn-search-replace',
        ESRN_PLUGIN_URL . 'assets/admin.css',
        array(),
        ESRN_VERSION
    );
    // Prefer core's registered Select2 if available; otherwise fall back to bundled copy.
    if ( wp_script_is( 'select2', 'registered' ) ) {
        wp_enqueue_style( 'select2' );
        wp_enqueue_script( 'select2' );
    } else {
        wp_enqueue_style(
            'esrn-select2',
            ESRN_PLUGIN_URL . 'assets/select2.css',
            array(),
            '4.0.13'
        );
        wp_enqueue_script(
            'esrn-select2',
            ESRN_PLUGIN_URL . 'assets/select2.js',
            array('jquery'),
            '4.0.13',
            true
        );
    }
}

add_action( 'admin_enqueue_scripts', 'esrn_enqueue_admin_scripts' );
/**
 * Suppress third-party admin notices on our settings page.
 */
function esrn_suppress_admin_notices() {
    $screen = ( function_exists( 'get_current_screen' ) ? get_current_screen() : null );
    if ( !$screen || 'settings_page_easy_search_replace' !== $screen->id ) {
        return;
    }
    remove_all_actions( 'admin_notices' );
    remove_all_actions( 'all_admin_notices' );
    remove_all_actions( 'user_admin_notices' );
    remove_all_actions( 'network_admin_notices' );
}

add_action( 'in_admin_header', 'esrn_suppress_admin_notices', 1000 );
/**
 * Add a "Settings" link to the plugins list row.
 *
 * @param array $links Existing action links.
 * @return array
 */
function esrn_add_settings_link(  $links  ) {
    $settings_url = admin_url( 'options-general.php?page=easy_search_replace' );
    $settings_link = sprintf( '<a href="%s">%s</a>', esc_url( $settings_url ), esc_html__( 'Settings', 'easy-search-replace' ) );
    array_unshift( $links, $settings_link );
    return $links;
}

add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'esrn_add_settings_link' );