AI Content Generator as a React app (source in app/src, * compiled into build/ by `npm run build` in app/). * * Responsibilities: * - Renders the app container and enqueues the bundle with its boot data: * URLs, nonces, translations, and one "section" per page. Modules add * their own page through the `wtofe_ai_settings_app_sections` filter * (see WTOFE_Settings_App_Sections for the core pages). * - Saves an option group over AJAX. Options keep the sanitize callbacks * they were registered with, since update_option() runs them. * - Keeps the screen to this plugin's own admin notices. * - Asks for a wordpress.org review in the admin footer. * * @package AI_Content_Generator_For_WooCommerce * @subpackage Admin/Modules/SettingsApp * @since 1.1.8 */ if ( ! defined( 'ABSPATH' ) ) { exit; } require_once __DIR__ . '/class-settings-app-sections.php'; class WTOFE_Settings_App { const PAGE_SLUG = 'wc-ai-content-generator'; const NONCE = 'wtofe_settings_app'; const SUPPORT_URL = 'https://wordpress.org/support/plugin/ai-content-generator-for-woocommerce/#new-topic-0'; const REVIEW_URL = 'https://wordpress.org/support/plugin/ai-content-generator-for-woocommerce/reviews/#new-post'; /** Option groups the app saves, by the key the app uses for them. */ const GROUPS = array( 'general' => 'wc_product_image_generator', 'chatbot' => 'wtofe_chatbot_settings', ); /** @var array|null Per-request cache of get_connector_models(). */ private static $models = null; // ------------------------------------------------------------------------- // Bootstrap // ------------------------------------------------------------------------- /** * Registers all admin hooks for this module. * Called once from the main plugin class. */ public static function register_hooks() { add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_scripts' ) ); add_filter( 'admin_body_class', array( __CLASS__, 'add_body_class' ) ); add_filter( 'admin_footer_text', array( __CLASS__, 'footer_review_text' ) ); // Last on in_admin_header, right before the notice hooks fire. add_action( 'in_admin_header', array( __CLASS__, 'remove_foreign_notices' ), PHP_INT_MAX ); add_action( 'wp_ajax_wtofe_save_settings', array( __CLASS__, 'ajax_save_settings' ) ); add_action( 'wp_ajax_wtofe_get_settings_state', array( __CLASS__, 'ajax_get_state' ) ); WTOFE_Settings_App_Sections::register_hooks(); } /** Prints the container the app mounts into. */ public static function render_page() { echo '
'; } /** @return bool */ private static function is_settings_screen() { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; return $screen && false !== strpos( (string) $screen->id, self::PAGE_SLUG ); } // ------------------------------------------------------------------------- // Assets // ------------------------------------------------------------------------- /** * @param string $hook Current admin page hook suffix. */ public static function enqueue_scripts( $hook ) { if ( false === strpos( (string) $hook, self::PAGE_SLUG ) ) { return; } $build_url = WTOFE_AI_PLUGIN_URL . 'admin/modules/settings-app/build/'; // The readable build with SCRIPT_DEBUG, as core does (see app/vite.config.ts). $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; // After WordPress's component styles, which the app restyles (app/src/styles/components.css). wp_enqueue_style( 'wtofe-settings-app', $build_url . 'settings-app' . $suffix . '.css', array( 'wp-components' ), WTOFE_AI_VERSION ); // React, the components, translations and requests come from WordPress, not the bundle // (app/src/wordpress/react.ts, and WORDPRESS_SCRIPTS in app/vite.config.ts). wp_enqueue_script( 'wtofe-settings-app', $build_url . 'settings-app' . $suffix . '.js', array( 'react', 'react-dom', 'wp-element', 'wp-components', 'wp-i18n', 'wp-api-fetch' ), WTOFE_AI_VERSION, true ); // Language-pack translations of the app's strings, loaded into wp.i18n before the app. wp_set_script_translations( 'wtofe-settings-app', 'ai-content-generator-for-woocommerce' ); // JSON rather than wp_localize_script(), which would turn booleans and numbers into strings. wp_add_inline_script( 'wtofe-settings-app', 'window.wtofeSettingsApp = ' . wp_json_encode( self::get_app_data() ) . ';', 'before' ); } /** * @param string $classes * @return string */ public static function add_body_class( $classes ) { return self::is_settings_screen() ? $classes . ' wtofe-settings-app-page' : $classes; } /** * Replaces the admin footer's "Thank you for creating with WordPress" * with a request to rate the plugin on wordpress.org. * * @param string $text Footer text. * @return string */ public static function footer_review_text( $text ) { if ( ! self::is_settings_screen() ) { return $text; } $url = esc_url( self::REVIEW_URL ); $rate = esc_attr__( 'Give us a 5-star rating!', 'ai-content-generator-for-woocommerce' ); return sprintf( /* translators: 1: plugin name, 2: five stars, 3: "WordPress.org"; both 2 and 3 link to the review form. */ esc_html__( 'Please rate %1$s %2$s on %3$s to help us spread the word. Thank you from the WebToffee team!', 'ai-content-generator-for-woocommerce' ), '' . esc_html__( 'AI Content Generator', 'ai-content-generator-for-woocommerce' ) . '', sprintf( '★★★★★', $url, $rate ), sprintf( 'WordPress.org', $url, $rate ) ); } // ------------------------------------------------------------------------- // Data // ------------------------------------------------------------------------- /** * Everything the app boots with. * * @return array */ private static function get_app_data() { $data = array_merge( array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( self::NONCE ), 'urls' => array( 'guide' => AI_Content_Generator_For_WooCommerce_Helper::get_user_guide_url(), 'support' => self::SUPPORT_URL, 'wizard' => WTOFE_Setup_Wizard::get_url(), 'connectors' => admin_url( 'options-connectors.php' ), ), 'locale' => self::get_locale_data(), ), self::get_state() ); /** * Filters the settings app's boot data, for features outside the * pages themselves (e.g. the "Request a feature" dialog). * * @param array $data */ return apply_filters( 'wtofe_ai_settings_app_data', $data ); } /** * The parts that change when settings are saved: available models and * the sections (pages) with their current values. * * @return array{models: array, sections: array} */ public static function get_state() { self::$models = null; /** * Filters the settings app's pages. Each section, keyed by page ID: * * 'order' => Nav position. * 'group' => App group key the values save into ('general' | 'chatbot'). * 'values' => option_name => current value. * 'data' => Read-only data the page needs. * * The page itself, its title and its strings live in the app * (app/src/pages/registry.ts). * * @param array $sections */ $sections = apply_filters( 'wtofe_ai_settings_app_sections', array() ); return array( 'models' => self::get_models(), 'sections' => $sections, ); } /** * get_connector_models(), fetched once per request since several * sections need it. * * @return array{text_generation?: array, image_generation?: array} */ public static function get_models() { if ( null === self::$models ) { self::$models = AI_Content_Generator_For_WooCommerce_Helper::get_connector_models(); } return self::$models; } /** * The saved model for a model select, or '' when that model isn't on * offer any more. Mirrors what the old