<?php

/**
 * Plugin Name: CatalogPilot AI Studio for WooCommerce (Premium)
 * Description:       AI content writer for WooCommerce. Text, SEO, FAQ schema, review summaries, image generation, AI background removal, and a "missing content" scanner — across 9 text and 4 image providers.
 * Version:           1.0.13
 * Requires at least: 6.2
 * Requires PHP:      7.4
 * Author:            cryptoexchange
 * Author URI:        https://wpcrypto.net/
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       catalogpilot-ai-studio-for-woocommerce
 * Domain Path:       /languages
 * WC requires at least: 7.0
 * WC tested up to:      10.7
 * Requires Plugins:      woocommerce
 *
 * @package CatalogPilot\AiStudio
 */
defined( 'ABSPATH' ) || exit;
define( 'CPAIS_VERSION', '1.0.13' );
define( 'CPAIS_FILE', __FILE__ );
define( 'CPAIS_PATH', plugin_dir_path( __FILE__ ) );
define( 'CPAIS_URL', plugin_dir_url( __FILE__ ) );
define( 'CPAIS_BASENAME', plugin_basename( __FILE__ ) );
if ( !function_exists( 'cpais_fs' ) ) {
    function cpais_fs() {
        // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
        global $cpais_fs;
        // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
        if ( !isset( $cpais_fs ) ) {
            require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';
            $cpais_fs = fs_dynamic_init( array(
                'id'               => '33262',
                'slug'             => 'catalogpilot-ai-studio-for-woocommerce',
                'type'             => 'plugin',
                'public_key'       => 'pk_fa3de6c8823c69bb83c7b9f5909f5',
                'is_premium'       => false,
                'has_addons'       => false,
                'has_paid_plans'   => true,
                'is_org_compliant' => true,
                'anonymous_mode'   => true,
                'menu'             => array(
                    'slug'    => 'catalogpilot-ai-studio-for-woocommerce',
                    'account' => true,
                    'support' => true,
                ),
                'is_live'          => true,
            ) );
        }
        return $cpais_fs;
    }

    cpais_fs();
    do_action( 'cpais_fs_loaded' );
    // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
}
require_once CPAIS_PATH . 'includes/class-plugin.php';
register_activation_hook( __FILE__, array('CatalogPilot\\AiStudio\\Plugin', 'activate') );
register_deactivation_hook( __FILE__, array('CatalogPilot\\AiStudio\\Plugin', 'deactivate') );
add_action( 'plugins_loaded', array('CatalogPilot\\AiStudio\\Plugin', 'instance') );
add_action( 'before_woocommerce_init', function () {
    if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
        \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
        \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'product_block_editor', __FILE__, true );
        \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', __FILE__, true );
    }
} );
/**
 * Uninstall cleanup, honours the "Delete all data on uninstall" preference.
 */
function cpais_uninstall_cleanup() {
    $stored = get_option( 'cpais_settings', array() );
    if ( !is_array( $stored ) || empty( $stored['delete_data_on_uninstall'] ) ) {
        return;
    }
    $options = array(
        'cpais_settings',
        'cpais_version',
        'cpais_usage_stats',
        'cpais_bulk_job',
        'cpais_cache_index'
    );
    foreach ( $options as $opt ) {
        delete_option( $opt );
        delete_site_option( $opt );
    }
    global $wpdb;
    // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Uninstall cleanup: post meta prefixed by plugin. Bulk delete; cache invalidation not applicable during uninstall.
    $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE %s", $wpdb->esc_like( '_cpais_' ) . '%' ) );
    // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Uninstall cleanup: expired transients. Bulk delete; cache invalidation not applicable during uninstall.
    $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s", '_transient_' . $wpdb->esc_like( 'cpais_cache_' ) . '%', '_transient_timeout_' . $wpdb->esc_like( 'cpais_cache_' ) . '%' ) );
}

if ( function_exists( 'cpais_fs' ) ) {
    cpais_fs()->add_action( 'after_uninstall', 'cpais_uninstall_cleanup' );
}