<?php

/**
 * Plugin Name:         Extra Product Options for WooCommerce
 * Description:         The most powerful WooCommerce product options plugin with nested conditional logic. Add 22+ field types with advanced AND/OR groups, custom pricing, and powerful display rules to boost sales and offer personalized products effortlessly.
 * Version:             4.4.7
 * Author:              actpro
 * Author URI:          https://profiles.wordpress.org/actpro/
 * License:             GPL-2.0+
 * License URI:         http://www.gnu.org/licenses/gpl-2.0.txt
 * Text Domain:         extra-product-options-for-woocommerce
 * Domain Path:         /languages
 * Requires Plugins:    woocommerce
 * WC requires at least: 8.0
 * WC tested up to:      10.7
 * Requires at least:    6.1
 * Tested up to:         6.9
 * Requires PHP:         7.4
 *
 * @package Extra_Product_Options_For_WooCommerce
 *
 */
// If this file is called directly, abort.
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
// Include WordPress admin functions for plugin management.
if ( !function_exists( 'is_plugin_active' ) ) {
    require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
// Check for plugin conflicts and handle accordingly.
$epofw_current_plugin_folder = basename( __DIR__ );
// Check if current plugin is the PRO version.
$epofw_is_current_pro = 'extra-product-options-for-woocommerce-pro' === $epofw_current_plugin_folder;
// Always check for dynamic product options plugin.
$epofw_should_deactivate = is_plugin_active( 'dynamic-product-options-for-woocommerce/dynamic-product-options-for-woocommerce.php' );
// Conditionally check based on current plugin version.
if ( !$epofw_should_deactivate ) {
    if ( $epofw_is_current_pro ) {
        // Current is PRO, deactivate if FREE is active.
        $epofw_should_deactivate = is_plugin_active( 'extra-product-options-for-woocommerce/extra-product-options-for-woocommerce.php' );
    } else {
        // Current is FREE, deactivate if PRO is active.
        $epofw_should_deactivate = is_plugin_active( 'extra-product-options-for-woocommerce-pro/extra-product-options-for-woocommerce.php' );
    }
}
// Check if the plugin should be deactivated.
if ( $epofw_should_deactivate ) {
    // Safely deactivate this plugin.
    // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Variable scoped to conditional block.
    $plugin_basename = plugin_basename( __FILE__ );
    // Ensure deactivate_plugins function is available.
    if ( function_exists( 'deactivate_plugins' ) ) {
        deactivate_plugins( $plugin_basename );
    }
    // Don't continue loading the plugin.
    return;
}
if ( !defined( 'EPOFW_PLUGIN_URL' ) ) {
    define( 'EPOFW_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
}
if ( !defined( 'EPOFW_PLUGIN_DIR' ) ) {
    define( 'EPOFW_PLUGIN_DIR', __DIR__ );
}
if ( !defined( 'EPOFW_PLUGIN_FILE' ) ) {
    define( 'EPOFW_PLUGIN_FILE', __FILE__ );
}
if ( !defined( 'EPOFW_PLUGIN_DIR_PATH' ) ) {
    define( 'EPOFW_PLUGIN_DIR_PATH', plugin_dir_path( __FILE__ ) );
}
if ( !defined( 'EPOFW_PLUGIN_BASENAME' ) ) {
    define( 'EPOFW_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
}
if ( !defined( 'EPOFW_UPLOAD_DIR' ) ) {
    define( 'EPOFW_UPLOAD_DIR', 'epofw_files' );
}
require_once EPOFW_PLUGIN_DIR_PATH . 'settings/epofw-constant.php';
/**
 * Global variable for plugin runtime state.
 *
 * Used for storing runtime flags and data that need to be shared across functions.
 * Keys:
 * - 'settings_cache_cleared': bool - Flag to reset static settings cache after save.
 *
 * @global array $epofw_globals
 */
global $epofw_globals;
$epofw_globals = array();
// Declare HPOS (High-Performance Order Storage) and Cart/Checkout Blocks compatibility.
add_action( 'before_woocommerce_init', function () {
    if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
        // Declare HPOS compatibility.
        \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
        // Declare Cart/Checkout Blocks compatibility.
        \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', __FILE__, true );
    }
} );
if ( !function_exists( 'epofw_fs' ) ) {
    /**
     * Returns the Freemius SDK instance (lazy-loaded).
     *
     * @global \Freemius $epofw_fs
     * @return \Freemius
     */
    function epofw_fs() {
        global $epofw_fs;
        if ( !isset( $epofw_fs ) ) {
            // Include Freemius SDK.
            require_once __DIR__ . '/freemius/start.php';
            $epofw_fs = fs_dynamic_init( array(
                'id'               => '8709',
                'slug'             => 'extra-product-options-for-woocommerce',
                'premium_slug'     => 'extra-product-options-for-woocommerce-pro',
                'type'             => 'plugin',
                'public_key'       => 'pk_ad1a3c9efcc20666a39697d82499b',
                'is_premium'       => false,
                'premium_suffix'   => '( Premium )',
                'has_addons'       => false,
                'has_paid_plans'   => true,
                'trial'            => array(
                    'days'               => 3,
                    'is_require_payment' => false,
                ),
                'has_affiliation'  => 'selected',
                'menu'             => array(
                    'slug'    => 'epofw-main',
                    'support' => false,
                    'parent'  => array(
                        'slug' => 'woocommerce',
                    ),
                ),
                'is_live'          => true,
                'is_org_compliant' => true,
            ) );
        }
        return $epofw_fs;
    }

    // Init Freemius.
    epofw_fs();
    /**
     * Freemius SDK.
     *
     * @since 4.0.0
     */
    do_action( 'epofw_fs_loaded' );
}
// Not like register_uninstall_hook(), you do NOT have to use a static function.
epofw_fs()->add_action( 'after_uninstall', 'epofw_fs_uninstall_cleanup' );
// Textdomain is loaded in EPOFW_Init class on 'init' hook to avoid early loading notice.
if ( !function_exists( 'epofw_activate_fn' ) ) {
    /**
     * The code that runs during plugin activation.
     */
    function epofw_activate_fn() {
        // phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WordPress core filter, must use as-is.
        /**
         * Filters the array of active plugin paths (WordPress core).
         *
         * @since 1.0.0
         *
         * @param string[] $active_plugins List of active plugin basenames.
         */
        $epofw_active_plugins = apply_filters( 'active_plugins', get_option( 'active_plugins' ) );
        // phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
        if ( !in_array( 'woocommerce/woocommerce.php', $epofw_active_plugins, true ) ) {
            wp_die( '<strong>' . esc_html( EPOFW_PLUGIN_NAME ) . '</strong> plugin requires <strong>WooCommerce</strong>. Return to <a href=' . esc_url( get_admin_url( null, 'plugins.php' ) ) . '>Plugins page</a>.' );
        } else {
            update_option( 'epofw_enable_logging', 'on' );
        }
        // Check if this is a fresh installation (no existing data).
        // Existing customers will have install_date or custom_tables_version already set.
        $is_fresh_install = !get_option( 'epofw_install_date' ) && !get_option( 'epofw_custom_tables_version' );
        // Set install date for new installations.
        if ( !get_option( 'epofw_install_date' ) ) {
            add_option( 'epofw_install_date', time() );
        }
        // Schedule demo addon import ONLY for fresh installations.
        // Import happens later in EPOFW_Import::init() when custom tables are ready.
        // Existing customers who deactivate/reactivate won't get demos auto-imported.
        if ( $is_fresh_install && !get_option( 'epofw_demo_addons_imported' ) ) {
            update_option( 'epofw_pending_demo_import', 'yes' );
        }
    }

}
register_activation_hook( __FILE__, 'epofw_activate_fn' );
// Hook plugin action links after init to ensure textdomain is loaded (WordPress 6.7+ requirement).
add_action( 'init', function () {
    $prefix = ( is_network_admin() ? 'network_admin_' : '' );
    add_filter( "{$prefix}plugin_action_links_" . EPOFW_PLUGIN_BASENAME, 'epofw_plugin_action_links', 10 );
}, 20 );
if ( !function_exists( 'epofw_plugin_action_links' ) ) {
    /**
     * Add helpful link in plugins section.
     *
     * @param array $actions associative array of action names to anchor tags.
     *
     * @return array associative array of plugin action links
     *
     * @since 1.0.0
     */
    function epofw_plugin_action_links(  $actions  ) {
        $custom_actions = array(
            'configure' => sprintf( '<a href="%s">%s</a>', esc_url( add_query_arg( array(
                'page' => 'epofw-main',
            ), admin_url( 'admin.php' ) ) ), esc_html__( 'Settings', 'extra-product-options-for-woocommerce' ) ),
        );
        $custom_actions['pro_configure'] = sprintf( '<a href="%s" target="_blank">%s</a>', esc_url( 'https://wpactpro.com' ), esc_html__( 'Premium Features', 'extra-product-options-for-woocommerce' ) );
        return array_merge( $custom_actions, $actions );
    }

}
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WordPress core filter, must use as-is.
/**
 * Filters the array of active plugin paths (WordPress core).
 *
 * @since 1.0.0
 *
 * @param string[] $active_plugins List of active plugin basenames.
 */
$epofw_active_plugins_boot = apply_filters( 'active_plugins', get_option( 'active_plugins' ) );
// phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
if ( in_array( 'woocommerce/woocommerce.php', $epofw_active_plugins_boot, true ) ) {
    require_once plugin_dir_path( __FILE__ ) . 'includes/class-epofw-init.php';
}
if ( !function_exists( 'epofw_migration_callback' ) ) {
    /**
     * Function will run migration process for lower version 2.0.
     */
    function epofw_migration_callback() {
        if ( version_compare( EPOFW_PLUGIN_VERSION, '2.0', '<' ) ) {
            $product_option_args = array(
                'post_type'      => EPOFW_DFT_POST_TYPE,
                'post_status'    => array('draft', 'publish'),
                'posts_per_page' => -1,
                'orderby'        => 'ID',
                'order'          => 'DESC',
                'fields'         => 'ids',
            );
            $prd_wp_query = new WP_Query($product_option_args);
            $old_data = array();
            if ( $prd_wp_query->have_posts() ) {
                foreach ( $prd_wp_query->posts as $f_id ) {
                    if ( !get_post_meta( $f_id, 'epofw_mgr_status', true ) ) {
                        $old_data[$f_id] = $f_id;
                    }
                }
            }
            $total_count = count( $old_data );
            $get_epofw_migration_count = get_option( 'epofw_migration_count' );
            if ( (int) $get_epofw_migration_count !== (int) $total_count ) {
                include plugin_dir_path( __FILE__ ) . 'includes/class-epofw-bg-process.php';
                $background_process = new EPOFW_Bg_Process();
                if ( !empty( $old_data ) ) {
                    $background_process->push_to_queue( $old_data );
                    $background_process->save()->dispatch();
                }
            }
        }
        if ( version_compare( EPOFW_PLUGIN_VERSION, '3.0.3', '>' ) ) {
            $get_data = get_option( 'epofw_general_data' );
            $check_migration = get_option( 'epofw_migration_304' );
            if ( !$check_migration && is_serialized( $get_data ) ) {
                $get_data = maybe_unserialize( $get_data );
                $epofw_general_data = epofw_check_array_key_exists( 'epofw_general_data', $get_data );
                update_option( 'epofw_general_data', epofw_sanitize_array( $epofw_general_data ) );
                update_option( 'epofw_migration_304', true );
                // Clear the general settings cache after migration.
                if ( function_exists( 'epofw_clear_general_settings_cache' ) ) {
                    epofw_clear_general_settings_cache();
                }
            }
        }
    }

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