'integer', 'default' => 60, 'sanitize_callback' => function ( $val ) { $val = absint( $val ); return $val < 30 ? 60 : $val; }, ) ); register_setting( $group, 'wtofe_seo_meta_desc_length', array( 'type' => 'integer', 'default' => 155, 'sanitize_callback' => function ( $val ) { $val = absint( $val ); return $val < 70 ? 155 : $val; }, ) ); register_setting( $group, 'wtofe_ai_model_meta', array( 'type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_text_field', ) ); register_setting( $group, 'wtofe_seo_meta_brand_name', array( 'type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_text_field', ) ); } /** * Adds the "AI SEO Meta" page to the settings app — only while a * supported SEO plugin is active, like the settings themselves. * * @param array $sections * @return array */ public static function add_settings_app_section( $sections ) { $provider = WTOFE_SEO_Meta_Provider_Registry::get_active_provider(); if ( ! $provider ) { return $sections; } $sections['seo-meta'] = array( 'order' => 40, 'group' => 'general', 'values' => array( 'wtofe_seo_meta_brand_name' => WTOFE_SEO_Meta_Generator::get_brand_name(), 'wtofe_seo_meta_title_length' => (int) get_option( 'wtofe_seo_meta_title_length', 60 ), 'wtofe_seo_meta_desc_length' => (int) get_option( 'wtofe_seo_meta_desc_length', 155 ), 'wtofe_ai_model_meta' => WTOFE_Settings_App::selectable_model( get_option( 'wtofe_ai_model_meta', '' ) ), ), 'data' => array( 'providerLabel' => $provider->get_label(), ), ); return $sections; } // ------------------------------------------------------------------------- // Asset Enqueueing // ------------------------------------------------------------------------- /** * Enqueues the SEO-meta admin CSS and JS on the product edit screen, but * only when a supported SEO plugin is active (otherwise there's no Yoast * metabox to attach the button to). * * @param string $hook Current admin page hook suffix. */ public static function enqueue_scripts( $hook ) { if ( ! in_array( $hook, array( 'post.php', 'post-new.php' ), true ) ) { return; } $screen = get_current_screen(); if ( ! $screen || 'product' !== $screen->post_type ) { return; } $provider = WTOFE_SEO_Meta_Provider_Registry::get_active_provider(); if ( ! $provider ) { return; } wp_enqueue_style( 'wtofe-seo-meta-admin', WTOFE_AI_PLUGIN_URL . 'admin/modules/seo-meta/assets/css/seo-meta-admin.css', array(), WTOFE_AI_VERSION ); wp_enqueue_script( 'wtofe-seo-meta-admin', WTOFE_AI_PLUGIN_URL . 'admin/modules/seo-meta/assets/js/seo-meta-admin.js', array( 'jquery' ), WTOFE_AI_VERSION, true ); wp_localize_script( 'wtofe-seo-meta-admin', 'wtofeSM', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'wtofe_seo_meta_nonce' ), 'postId' => get_the_ID(), 'providerId' => $provider->get_id(), 'providerLabel' => $provider->get_label(), 'titleLength' => WTOFE_SEO_Meta_Generator::get_title_length(), 'descLength' => WTOFE_SEO_Meta_Generator::get_description_length(), 'i18n' => array( 'buttonLabel' => __( 'Generate SEO Meta', 'ai-content-generator-for-woocommerce' ), 'generating' => __( 'Generating SEO title & description…', 'ai-content-generator-for-woocommerce' ), 'apply' => __( 'Apply', 'ai-content-generator-for-woocommerce' ), 'regenerate' => __( 'Generate Another', 'ai-content-generator-for-woocommerce' ), 'applying' => __( 'Applying…', 'ai-content-generator-for-woocommerce' ), 'titleLabel' => __( 'SEO Title', 'ai-content-generator-for-woocommerce' ), 'descLabel' => __( 'Meta Description', 'ai-content-generator-for-woocommerce' ), 'error' => __( 'Generation failed. Please try again.', 'ai-content-generator-for-woocommerce' ), 'applyError' => __( 'Could not save the SEO meta. Please try again.', 'ai-content-generator-for-woocommerce' ), 'applied' => __( 'Applied! Reloading…', 'ai-content-generator-for-woocommerce' ), 'characters' => __( 'characters', 'ai-content-generator-for-woocommerce' ), 'pluginBadge' => __( 'by AI Content Generator', 'ai-content-generator-for-woocommerce' ), ), ) ); } // ------------------------------------------------------------------------- // AJAX // ------------------------------------------------------------------------- /** * Handles the AJAX "Generate" (preview only, nothing saved yet) request. */ public static function ajax_generate() { self::verify_request(); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce verified above in self::verify_request(). $product_id = isset( $_POST['post_id'] ) ? absint( $_POST['post_id'] ) : 0; if ( ! $product_id ) { wp_send_json_error( array( 'message' => __( 'Invalid product ID.', 'ai-content-generator-for-woocommerce' ) ) ); } $result = WTOFE_SEO_Meta_Generator::generate( $product_id ); if ( is_wp_error( $result ) ) { wp_send_json_error( array( 'message' => $result->get_error_message() ) ); } wp_send_json_success( array( 'title' => $result['title'], 'description' => $result['description'], 'model' => $result['model'], ) ); } /** * Handles the AJAX "Apply" request — saves the (possibly user-edited) * title & description via the active SEO plugin provider. */ public static function ajax_apply() { self::verify_request(); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce verified above in self::verify_request(). $product_id = isset( $_POST['post_id'] ) ? absint( $_POST['post_id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce verified above in self::verify_request(). $title = isset( $_POST['title'] ) ? sanitize_text_field( wp_unslash( $_POST['title'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce verified above in self::verify_request(). $description = isset( $_POST['description'] ) ? sanitize_textarea_field( wp_unslash( $_POST['description'] ) ) : ''; if ( ! $product_id ) { wp_send_json_error( array( 'message' => __( 'Invalid product ID.', 'ai-content-generator-for-woocommerce' ) ) ); } if ( '' === trim( $title ) ) { wp_send_json_error( array( 'message' => __( 'The SEO title cannot be empty.', 'ai-content-generator-for-woocommerce' ) ) ); } $result = WTOFE_SEO_Meta_Generator::save( $product_id, $title, $description ); if ( is_wp_error( $result ) ) { wp_send_json_error( array( 'message' => $result->get_error_message() ) ); } // Mark the product as AI-generated, matching every other feature. update_post_meta( $product_id, '_wtofe_ai_generated', '1' ); wp_send_json_success( array( 'message' => __( 'SEO meta saved successfully.', 'ai-content-generator-for-woocommerce' ) ) ); } /** * Shared nonce + capability check for both AJAX handlers. */ private static function verify_request() { if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'wtofe_seo_meta_nonce' ) ) { wp_send_json_error( array( 'message' => __( 'Invalid nonce.', 'ai-content-generator-for-woocommerce' ) ) ); } if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_send_json_error( array( 'message' => __( 'Insufficient permissions.', 'ai-content-generator-for-woocommerce' ) ) ); } } }