set_basename( false, __FILE__ ); } else { // Freemius SDK is bundled with the plugin. The Composer autoload below stays only // as a fallback for development checkouts that still have it installed. $aifi_fs_sdk = dirname( __FILE__ ) . '/freemius/start.php'; if ( file_exists( $aifi_fs_sdk ) ) { require_once $aifi_fs_sdk; } $autoload = dirname( __FILE__ ) . '/vendor/autoload.php'; if ( !function_exists( 'fs_dynamic_init' ) && file_exists( $autoload ) ) { require_once $autoload; } // Create a helper function for easy SDK access. function afig_fs() { global $afig_fs; if ( !isset( $afig_fs ) ) { // Include Freemius SDK. // SDK is loaded from the bundled freemius/ directory above. $afig_fs = fs_dynamic_init( array( 'id' => '22299', 'slug' => 'ai-featured-image-generator', 'type' => 'plugin', 'public_key' => 'pk_8ee5817c3f9bac01d4aa681869233', 'is_premium' => false, 'premium_suffix' => 'Pro', 'has_addons' => false, 'has_paid_plans' => true, 'menu' => array( 'slug' => 'ai-featured-image-generator', 'parent' => array( 'slug' => 'ai-featured-image-generator', ), 'support' => false, ), 'is_live' => true, 'is_org_compliant' => true, ) ); } return $afig_fs; } // Init Freemius. afig_fs(); // Hide Account page for free users afig_fs()->add_filter( 'is_submenu_visible', function ( $is_visible, $submenu_id ) { if ( 'account' === $submenu_id && !afig_fs()->can_use_premium_code() ) { return false; } return $is_visible; }, 10, 2 ); // The in-admin pricing page only exists for administrators, and only after the // opt-in screen is done (not on a network-activated multisite either). Everyone // else gets the hosted checkout so "Upgrade to Pro" always opens something. afig_fs()->add_filter( 'pricing_url', function ( $url ) { $fs = afig_fs(); $in_admin_page = current_user_can( 'manage_options' ) && !$fs->is_activation_mode() && (!$fs->is_network_active() || $fs->is_delegated_connection()); return ( $in_admin_page ? $url : 'https://checkout.freemius.com/plugin/22299/plan/37317/licenses/1/' ); } ); // Signal that SDK was initiated. do_action( 'afig_fs_loaded' ); // Filter plugin name to ensure correct display: remove "Pro" for free users, add for premium add_filter( 'all_plugins', function ( $plugins ) { $plugin_file = plugin_basename( __FILE__ ); if ( isset( $plugins[$plugin_file] ) ) { $original_name = $plugins[$plugin_file]['Name'] ?? ''; $can_use_premium = class_exists( '\\AIFI\\Providers' ) && \AIFI\Providers::can_use_premium(); // Ensure correct name based on license: remove "Pro" for free users, add for premium $base_name = str_replace( ' Pro', '', $original_name ); if ( $can_use_premium ) { // Premium users should see "Pro" suffix if ( strpos( $base_name, ' Pro' ) === false ) { $plugins[$plugin_file]['Name'] = $base_name . ' Pro'; } } else { // Free users should NOT see "Pro" suffix $plugins[$plugin_file]['Name'] = $base_name; } } return $plugins; }, 999 ); // Plugin version define( 'AIFI_VERSION', '2.2.1' ); define( 'AIFI_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); define( 'AIFI_PLUGIN_URL', plugin_dir_url( __FILE__ ) ); // Autoloader spl_autoload_register( function ( $class ) { $prefix = 'AIFI\\'; $base_dir = AIFI_PLUGIN_DIR . 'inc/'; $len = strlen( $prefix ); if ( strncmp( $prefix, $class, $len ) !== 0 ) { return; } $relative_class = substr( $class, $len ); $file = $base_dir . 'class-' . str_replace( '\\', '/', strtolower( str_replace( '_', '-', $relative_class ) ) ) . '.php'; if ( file_exists( $file ) ) { require $file; } } ); // WooCommerce: the plugin never touches orders, so it works with High-Performance Order Storage. 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 ); } } ); // Initialize the plugin function aifi_init() { $plugin = new AIFI\Plugin(); $plugin->init(); } add_action( 'plugins_loaded', 'aifi_init' ); /** * Options the plugin stores, on each site and, on multisite, for the network. * * @return string[] */ function aifi_uninstall_option_names() { return array( 'aifi_settings', 'aifi_first_image_hint_shown', 'aifi_brand_kit_updated', 'aifi_mcp_usage_count', 'aifi_mcp_connected_at', 'aifi_mcp_long_sessions', 'aifi_podcast_feed_flushed', 'aifi_install_date', 'aifi_generation_count', 'aifi_review_state', 'aifi_version', 'aifi_whats_new', 'aifi_auto_generate_next_slot' ); } /** * Uninstall: one site's options, transients, scheduled events and unfinished recordings. * Posts, images, recordings and their post meta are kept. */ function aifi_uninstall_site_data() { global $wpdb; foreach ( aifi_uninstall_option_names() as $name ) { delete_option( $name ); } // Transients with a fixed name, so an object cache forgets them too. delete_transient( 'aifi_podcast_feed' ); // Locks, kept checks, voice lists and recordings in progress all have a changing part // in their name. Single sites keep site transients in the options table as well. $prefixes = array( '_transient_aifi_', '_transient_timeout_aifi_', '_site_transient_aifi_', '_site_transient_timeout_aifi_', 'aifi_lock_' ); foreach ( $prefixes as $prefix ) { $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s", $wpdb->esc_like( $prefix ) . '%' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching } wp_cache_delete( 'alloptions', 'options' ); wp_cache_delete( 'notoptions', 'options' ); // Every event of each hook, whatever its arguments (one automatic image event per post). wp_unschedule_hook( 'aifi_auto_generate' ); wp_unschedule_hook( 'aifi_audio_tmp_cleanup' ); // The uploads/aifi-audio-tmp folder. if ( class_exists( '\\AIFI\\Audio' ) ) { \AIFI\Audio::cleanup_all(); } } /** * Uninstall on multisite: the network options and site transients of every network. */ function aifi_uninstall_network_data() { global $wpdb; $network_ids = ( function_exists( 'get_networks' ) ? get_networks( array( 'fields' => 'ids', ) ) : array(get_current_network_id()) ); foreach ( $network_ids as $network_id ) { foreach ( aifi_uninstall_option_names() as $name ) { delete_network_option( (int) $network_id, $name ); } } foreach ( array('_site_transient_aifi_', '_site_transient_timeout_aifi_') as $prefix ) { $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE meta_key LIKE %s", $wpdb->esc_like( $prefix ) . '%' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching } } /** * Removes what the plugin stored when it is deleted from the Plugins screen: API keys * and settings, counters, notices, transients, scheduled events and unfinished * recordings. Freemius calls it only when no other copy (Free or Pro) is still active. * Posts, images, recordings and their post meta are kept. */ function aifi_fs_uninstall_cleanup() { global $wpdb; if ( is_multisite() ) { $batch = 100; $offset = 0; do { $site_ids = get_sites( array( 'fields' => 'ids', 'number' => $batch, 'offset' => $offset, ) ); foreach ( $site_ids as $site_id ) { switch_to_blog( (int) $site_id ); aifi_uninstall_site_data(); restore_current_blog(); } $offset += $batch; } while ( count( $site_ids ) === $batch ); aifi_uninstall_network_data(); } else { aifi_uninstall_site_data(); } // Dismissed notices, per user (one table for every site). $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->usermeta} WHERE meta_key LIKE %s", $wpdb->esc_like( 'aifi_' ) . '%' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching // The AI assistant keys the plugin created (application passwords named // "AI Images (MCP)"), so a copied key stops working once the plugin is gone. if ( class_exists( 'WP_Application_Passwords' ) ) { $user_ids = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s", '_application_passwords' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching foreach ( array_map( 'intval', $user_ids ) as $user_id ) { foreach ( WP_Application_Passwords::get_user_application_passwords( $user_id ) as $item ) { if ( isset( $item['name'], $item['uuid'] ) && 0 === strpos( (string) $item['name'], 'AI Images (MCP)' ) ) { WP_Application_Passwords::delete_application_password( $user_id, $item['uuid'] ); } } } } } afig_fs()->add_action( 'after_uninstall', 'aifi_fs_uninstall_cleanup' ); // Activation hook register_activation_hook( __FILE__, function () { // Get existing options to preserve settings during upgrade $existing = ( is_multisite() ? get_network_option( null, 'aifi_settings' ) : get_option( 'aifi_settings' ) ); // Check if user can use premium features (Freemius may not be fully initialized during activation) $can_use_premium = \AIFI\Providers::can_use_premium(); // Set default options - Gemini settings only included for context but will be gated at runtime $defaults = array( 'ai_provider' => 'openai', 'openai_api_key' => '', 'default_size' => '1536x1024', 'default_style' => 'realistic', 'output_format' => 'webp', 'image_quality' => 90, 'ai_model' => 'gpt-image-2', 'max_attempts' => 3, 'auto_generate' => false, 'auto_alt_text' => 1, 'alt_text_source' => 'post_title', 'alt_text_custom_format' => '{title}', ); // Only set Gemini and Pro defaults for premium users if ( $can_use_premium ) { $defaults['gemini_api_key'] = ''; $defaults['gemini_aspect_ratio'] = '16:9'; $defaults['gemini_image_size'] = '2K'; $defaults['enabled_post_types'] = array('post', 'page'); // Pro users can customize this // WebP is the recommended default for Gemini/Grok (lightweight, SEO-friendly). $defaults['gemini_output_format'] = 'webp'; $defaults['grok_output_format'] = 'webp'; $defaults['inline_gemini_output_format'] = 'webp'; $defaults['inline_grok_output_format'] = 'webp'; } // Migrate legacy 'api_key' to 'openai_api_key' if it exists if ( is_array( $existing ) && isset( $existing['api_key'] ) && !empty( $existing['api_key'] ) ) { $defaults['openai_api_key'] = $existing['api_key']; } // Merge with existing settings to preserve user configuration $settings = ( is_array( $existing ) ? array_merge( $defaults, $existing ) : $defaults ); // Ensure new fields are present if ( !isset( $settings['ai_provider'] ) ) { $settings['ai_provider'] = 'openai'; } // Force OpenAI for non-premium users who somehow have Gemini selected if ( !$can_use_premium && isset( $settings['ai_provider'] ) && $settings['ai_provider'] === 'gemini' ) { $settings['ai_provider'] = 'openai'; $settings['ai_model'] = 'gpt-image-2'; } if ( !isset( $settings['openai_api_key'] ) && isset( $settings['api_key'] ) ) { $settings['openai_api_key'] = $settings['api_key']; } // Only set Gemini defaults if premium (or preserve existing for upgrade scenarios) if ( $can_use_premium ) { if ( !isset( $settings['gemini_api_key'] ) ) { $settings['gemini_api_key'] = ''; } if ( !isset( $settings['gemini_aspect_ratio'] ) ) { $settings['gemini_aspect_ratio'] = '16:9'; } if ( !isset( $settings['gemini_image_size'] ) ) { $settings['gemini_image_size'] = '2K'; } } if ( is_multisite() ) { update_network_option( null, 'aifi_settings', $settings ); } else { update_option( 'aifi_settings', $settings ); } // Daily removal of recording parts left behind. Sites that update without // reactivating get it from Audio::register_hooks() on their next request. \AIFI\Audio::schedule_cleanup(); } ); // Deactivation hook register_deactivation_hook( __FILE__, function () { global $wpdb; // Clean up the plugin's transients and lock rows. // They last up to a few minutes while a request runs (5 s after it ends), so they are not autoloaded and wp_load_alloptions() never lists them. // phpcs:ignore WordPress.DB.DirectDatabaseQuery // Locks (aifi_lock_*, aifi_block_lock_*, aifi_logo_lock_*), recording and voice caches // (aifi_audio_*), the kept photo and image checks (aifi_unify_*, aifi_audit_*), and the // plain aifi_lock_* rows of the audio and product locks. Other plugin transients stay. $like = array(); foreach ( array( 'aifi_lock_', 'aifi_block_lock_', 'aifi_logo_lock_', 'aifi_audio_', 'aifi_unify_', 'aifi_audit_' ) as $prefix ) { $like[] = $wpdb->esc_like( '_transient_' . $prefix ) . '%'; $like[] = $wpdb->esc_like( '_transient_timeout_' . $prefix ) . '%'; } $like[] = $wpdb->esc_like( 'aifi_lock_' ) . '%'; $placeholders = implode( ' OR ', array_fill( 0, count( $like ), 'option_name LIKE %s' ) ); $option_names = $wpdb->get_col( $wpdb->prepare( "SELECT option_name FROM {$wpdb->options} WHERE {$placeholders}", $like ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.PreparedSQL.InterpolatedNotPrepared // delete_option() also clears the cached copy of each row. foreach ( $option_names as $option_name ) { delete_option( $option_name ); } // Parts of recordings that never finished, and their daily cleanup. if ( class_exists( '\\AIFI\\Audio' ) ) { \AIFI\Audio::cleanup_all(); wp_unschedule_hook( \AIFI\Audio::CLEANUP_HOOK ); } // Automatic images still waiting to run (one event per post, so every argument set). wp_unschedule_hook( 'aifi_auto_generate' ); // The podcast feed's address stops working with the plugin. if ( class_exists( '\\AIFI\\Podcast' ) ) { \AIFI\Podcast::deactivate(); } } ); }