Below is a differential between the unpatched vulnerable code and the patched update, for reference.
--- a/ai-for-seo/ai-for-seo.php
+++ b/ai-for-seo/ai-for-seo.php
@@ -1,84 +1,78 @@
-<?php
-/**
- * Plugin Name: SOOZ - AI for SEO
- * Plugin URI: https://sooz.ai
- * Description: One-Click SEO solution. *SOOZ - AI for SEO* helps your website to rank higher in Web Search results.
- * Version: 2.4.2
- * Author: spacecodes
- * Author URI: https://spa.ce.codes
- * Text Domain: ai-for-seo
- * Domain Path: /languages
- * Copyright 2026 spacecodes
- * License: GPL-2.0-or-later
- * License URI: https://www.gnu.org/licenses/gpl-2.0.html
- * Requires PHP: 7.4
- *
- * @package AI_For_SEO
- */
-
-if ( ! defined( 'ABSPATH' ) ) {
- exit;
-}
-
-// Store the plugin entry file for hooks and URL/path helpers that require main-file semantics.
-const AI4SEO_PLUGIN_FILE = __FILE__;
-const AI4SEO_PLUGIN_DIR_PATH = __DIR__ . '/';
-
-// Handle early-exit support paths before the rest of the plugin registers hooks or loads modules.
-// The deactivation branch intentionally uses WordPress' page variable so no custom raw parameter remains.
-// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- The page variable is the requested early-loader trigger.
-if ( isset( $_GET['page'] ) && 'deactivate-ai-for-seo' === sanitize_key( wp_unslash( $_GET['page'] ) ) ) {
- deactivate_plugins( plugin_basename( AI4SEO_PLUGIN_FILE ) );
- return;
-}
-
+<?php
+/**
+ * Plugin Name: SOOZ - AI for SEO
+ * Plugin URI: https://sooz.ai
+ * Description: One-Click SEO solution. *SOOZ - AI for SEO* helps your website to rank higher in Web Search results.
+ * Version: 2.4.3
+ * Author: spacecodes
+ * Author URI: https://spa.ce.codes
+ * Text Domain: ai-for-seo
+ * Domain Path: /languages
+ * Copyright 2026 spacecodes
+ * License: GPL-2.0-or-later
+ * License URI: https://www.gnu.org/licenses/gpl-2.0.html
+ * Requires PHP: 7.4
+ *
+ * @package AI_For_SEO
+ */
+
+if ( ! defined( 'ABSPATH' ) ) {
+ exit;
+}
+
+// Store the plugin entry file for hooks and URL/path helpers that require main-file semantics.
+const AI4SEO_PLUGIN_FILE = __FILE__;
+const AI4SEO_PLUGIN_DIR_PATH = __DIR__ . '/';
+
// Load the early prohibit-token handler separately while keeping the final stop decision in this loader.
require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/prohibit-plugin.php';
-
-// Stop before normal module loading when the extracted handler validates a one-time prohibition token.
-if ( ai4seo_should_prohibit_plugin() ) {
- return;
-}
-
-// Declarations provide constants, static registries, and globals required by the core modules.
-require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/declarations.php';
-
-// Procedural modules load before bootstrap code registers hooks or calls helpers.
-require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/initialization.php';
-require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/rights.php';
-require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/plans.php';
-require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/helpers.php';
-require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/semaphores.php';
-require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/posts.php';
-require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/taxonomies.php';
-require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/output.php';
-require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/third-party-seo-plugins.php';
-require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/external-plugins.php';
-require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/multilingual-plugins.php';
-require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/cron-jobs.php';
-require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/generation-status-analysis.php';
-require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/metadata.php';
-
-// Metadata fallback helpers extend metadata and are used by settings and frontend injection callbacks.
-require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/metadata-fallbacks.php';
-
-// Custom-instruction helpers extend metadata and use the shared render helpers loaded above.
-require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/custom-instructions.php';
-require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/attachments-media.php';
-
-// Frontend injection callbacks depend on attachment helpers and load before bootstrap registers their hooks.
-require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/frontend-injections.php';
-require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/post-meta.php';
-require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/wordpress-options.php';
-require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/settings.php';
-require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/ajax.php';
-require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/bulk-generation.php';
-require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/native-bulk-actions.php';
-require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/environmental-variables.php';
+
+// Stop before normal module loading when the extracted handler validates a one-time prohibition token.
+if ( ai4seo_should_prohibit_plugin() ) {
+ return;
+}
+
+// Declarations provide constants, static registries, and globals required by the core modules.
+require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/declarations.php';
+
+// Procedural modules load before bootstrap code registers hooks or calls helpers.
+require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/initialization.php';
+require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/rights.php';
+require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/plans.php';
+require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/helpers.php';
+require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/semaphores.php';
+require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/posts.php';
+require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/taxonomies.php';
+require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/output.php';
+require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/third-party-seo-plugins.php';
+require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/external-plugins.php';
+require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/multilingual-plugins.php';
+require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/cron-jobs.php';
+require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/generation-status-analysis.php';
+require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/metadata.php';
+
+// Metadata fallback helpers extend metadata and are used by settings and frontend injection callbacks.
+require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/metadata-fallbacks.php';
+
+// Custom-instruction helpers extend metadata and use the shared render helpers loaded above.
+require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/custom-instructions.php';
+require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/attachments-media.php';
+
+// Frontend injection callbacks depend on attachment helpers and load before bootstrap registers their hooks.
+require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/frontend-injections.php';
+require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/post-meta.php';
+require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/wordpress-options.php';
+require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/settings.php';
+require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/ajax.php';
+require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/bulk-generation.php';
+require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/native-bulk-actions.php';
+require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/environmental-variables.php';
require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/notifications-notices.php';
require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/terms-of-service.php';
require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/latest-activity.php';
-require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/pay-as-you-go.php';
-// Register hooks last and return the bootstrap result so its early blockers exit this plugin file.
-return require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/bootstrap-hooks.php';
+// Account operations load after their plan, storage, notification, and API dependencies.
+require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/account.php';
+
+// Register hooks last and return the bootstrap result so its early blockers exit this plugin file.
+return require_once AI4SEO_PLUGIN_DIR_PATH . 'includes/core/bootstrap-hooks.php';
--- a/ai-for-seo/includes/ajax/display/attachment-attributes-editor.php
+++ b/ai-for-seo/includes/ajax/display/attachment-attributes-editor.php
@@ -42,8 +42,18 @@
ai4seo_send_ajax_error( esc_html__( 'Post id is invalid.', 'ai-for-seo' ), 291920824 );
}
-// get sanitized all_post_ids parameter.
-$ai4seo_all_attachment_post_ids = isset( $_REQUEST['all_attachment_post_ids'] ) && is_array( $_REQUEST['all_attachment_post_ids'] ) ? array_map( 'absint', $_REQUEST['all_attachment_post_ids'] ) : array();
+// The plugin-level role gate does not replace WordPress's object-level media permission check.
+if ( ! ai4seo_can_edit_post( $ai4seo_this_attachment_post_id ) ) {
+ ai4seo_send_ajax_error( esc_html__( 'You are not allowed to edit this media entry.', 'ai-for-seo' ), 291920825 );
+}
+
+// Normalize the navigation list before using it for authorization or next-entry selection.
+$ai4seo_all_attachment_post_ids = isset( $_REQUEST['all_attachment_post_ids'] ) && is_array( $_REQUEST['all_attachment_post_ids'] )
+ ? array_values( array_unique( array_filter( array_map( 'absint', (array) wp_unslash( $_REQUEST['all_attachment_post_ids'] ) ) ) ) )
+ : array();
+
+// Keep only editable navigation targets so one unrelated media entry cannot block the authorized editor.
+$ai4seo_all_attachment_post_ids = ai4seo_filter_editable_post_ids( $ai4seo_all_attachment_post_ids );
// Reuse the ordered-list helper so media and metadata editors calculate their next-entry targets identically.
$ai4seo_next_attachment_post_id = ai4seo_get_next_post_id_from_ordered_post_ids( $ai4seo_this_attachment_post_id, $ai4seo_all_attachment_post_ids );
--- a/ai-for-seo/includes/ajax/display/bulk-custom-instructions-modal.php
+++ b/ai-for-seo/includes/ajax/display/bulk-custom-instructions-modal.php
@@ -40,6 +40,11 @@
ai4seo_send_ajax_error( esc_html__( 'Please select at least one entry.', 'ai-for-seo' ), 1507062603 );
}
+// Validate every modal target because the submitted selection can differ from the visible admin table.
+if ( ! ai4seo_can_edit_post_ids( $ai4seo_post_ids ) ) {
+ ai4seo_send_ajax_error( esc_html__( 'You are not allowed to edit one or more selected entries.', 'ai-for-seo' ), 1507062604 );
+}
+
// === PREPARE =============================================================================== \
$ai4seo_selected_entries_count = count( $ai4seo_post_ids );
--- a/ai-for-seo/includes/ajax/display/metadata-editor.php
+++ b/ai-for-seo/includes/ajax/display/metadata-editor.php
@@ -1,63 +1,101 @@
-<?php
-/**
- * Displays the metadata editor. Called via AJAX.
- *
- * @since 1.0
- */
-
-if ( ! defined( 'ABSPATH' ) ) {
- exit;
-}
-
-if ( ! ai4seo_can_manage_this_plugin() ) {
- return;
-}
-
-// Recheck the global AJAX nonce before handling this protected admin request.
-if ( wp_verify_nonce( $GLOBALS['ai4seo_ajax_nonce'] ?? '', AI4SEO_GLOBAL_NONCE_IDENTIFIER ) === false ) {
- ai4seo_send_ajax_error( esc_html__( 'Nonce verification failed. Please refresh the page and try again.', 'ai-for-seo' ), 2306230636 );
- return;
-}
-
-
-// ___________________________________________________________________________________________ \
-// === PREPARE =============================================================================== \
-// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ \
-
-// === CHECK PARAMETER ============================================== \
-
-// Make sure that input-fields exist.
-if ( ! defined( 'AI4SEO_METADATA_DETAILS' ) ) {
- ai4seo_send_ajax_error( esc_html__( 'An error occurred! Please check your settings or contact the plugin developer.', 'ai-for-seo' ), 2306230642 );
-}
-
-$ai4seo_read_page_content_via_js = isset( $_REQUEST['read_page_content_via_js'] ) && 'true' === $_REQUEST['read_page_content_via_js'] ? 'true' : 'false';
-
-// Get sanitized post id parameter.
-$ai4seo_post_id = absint( $_REQUEST['post_id'] ?? 0 );
-
-// validate post id.
+<?php
+/**
+ * Displays the metadata editor. Called via AJAX.
+ *
+ * @since 1.0
+ */
+
+if ( ! defined( 'ABSPATH' ) ) {
+ exit;
+}
+
+if ( ! ai4seo_can_manage_this_plugin() ) {
+ return;
+}
+
+// Recheck the global AJAX nonce before handling this protected admin request.
+if ( wp_verify_nonce( $GLOBALS['ai4seo_ajax_nonce'] ?? '', AI4SEO_GLOBAL_NONCE_IDENTIFIER ) === false ) {
+ ai4seo_send_ajax_error( esc_html__( 'Nonce verification failed. Please refresh the page and try again.', 'ai-for-seo' ), 2306230636 );
+ return;
+}
+
+
+// ___________________________________________________________________________________________ \
+// === PREPARE =============================================================================== \
+// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ \
+
+// === CHECK PARAMETER ============================================== \
+
+// Make sure that input-fields exist.
+if ( ! defined( 'AI4SEO_METADATA_DETAILS' ) ) {
+ ai4seo_send_ajax_error( esc_html__( 'An error occurred! Please check your settings or contact the plugin developer.', 'ai-for-seo' ), 2306230642 );
+}
+
+$ai4seo_read_page_content_via_js = isset( $_REQUEST['read_page_content_via_js'] ) && 'true' === $_REQUEST['read_page_content_via_js'] ? 'true' : 'false';
+
+// Get sanitized post id parameter.
+$ai4seo_post_id = absint( $_REQUEST['post_id'] ?? 0 );
+
+// validate post id.
if ( $ai4seo_post_id <= 0 ) {
ai4seo_send_ajax_error( esc_html__( 'Post id is invalid.', 'ai-for-seo' ), 2306230638 );
}
-// get sanitized all_post_ids parameter.
-$ai4seo_all_post_ids = isset( $_REQUEST['all_post_ids'] ) && is_array( $_REQUEST['all_post_ids'] ) ? array_map( 'absint', $_REQUEST['all_post_ids'] ) : array();
-
-// Reuse the ordered-list helper so metadata and media editors calculate their next-entry targets identically.
-$ai4seo_next_post_id = ai4seo_get_next_post_id_from_ordered_post_ids( $ai4seo_post_id, $ai4seo_all_post_ids );
-
+// The plugin-level role gate does not replace WordPress's object-level post permission check.
+if ( ! ai4seo_can_edit_post( $ai4seo_post_id ) ) {
+ ai4seo_send_ajax_error( esc_html__( 'You are not allowed to edit this entry.', 'ai-for-seo' ), 2306230643 );
+}
-// === GET ADDITIONAL DETAILS ===================================================================== \
+// Normalize the navigation list before using it for authorization or next-entry selection.
+$ai4seo_all_post_ids = isset( $_REQUEST['all_post_ids'] ) && is_array( $_REQUEST['all_post_ids'] )
+ ? array_values( array_unique( array_filter( array_map( 'absint', (array) wp_unslash( $_REQUEST['all_post_ids'] ) ) ) ) )
+ : array();
-// Read post- or page-title and post custom fields.
-$ai4seo_this_post_title = get_the_title( $ai4seo_post_id );
+// Keep only editable navigation targets so one unrelated entry cannot block the authorized editor.
+$ai4seo_all_post_ids = ai4seo_filter_editable_post_ids( $ai4seo_all_post_ids );
+// Reuse the ordered-list helper so metadata and media editors calculate their next-entry targets identically.
+$ai4seo_next_post_id = ai4seo_get_next_post_id_from_ordered_post_ids( $ai4seo_post_id, $ai4seo_all_post_ids );
+
+
+// === GET ADDITIONAL DETAILS ===================================================================== \
+
+// Read post- or page-title and post custom fields.
+$ai4seo_this_post_title = get_the_title( $ai4seo_post_id );
+
// read all metadata values for this post.
$ai4seo_this_metadata_values = ai4seo_read_available_metadata_by_post_ids( array( $ai4seo_post_id ) );
-if ( $ai4seo_this_metadata_values ) {
+if ( is_array( $ai4seo_this_metadata_values ) ) {
$ai4seo_this_metadata_values = $ai4seo_this_metadata_values[ $ai4seo_post_id ] ?? array();
+} else {
+ $ai4seo_this_metadata_values = array();
+}
+
+// Start with no client snapshot so persisted metadata remains authoritative outside the current page editor.
+$ai4seo_live_yoast_metadata_identifiers = array();
+$ai4seo_live_yoast_metadata = array();
+
+// Unslash the nested snapshot as one unit; recognized fields are sanitized individually below.
+if ( isset( $_REQUEST['live_yoast_metadata'] ) && is_array( $_REQUEST['live_yoast_metadata'] ) ) {
+ $ai4seo_live_yoast_metadata = wp_unslash( $_REQUEST['live_yoast_metadata'] );
+}
+
+// Prefer Yoast's current editor values only for fields actively configured to synchronize with Yoast.
+$ai4seo_yoast_sync_metadata_identifiers = ai4seo_get_third_party_seo_plugin_sync_metadata_identifiers( AI4SEO_THIRD_PARTY_PLUGIN_YOAST_SEO );
+
+foreach ( $ai4seo_yoast_sync_metadata_identifiers as $ai4seo_live_yoast_metadata_identifier ) {
+ if ( ! array_key_exists( $ai4seo_live_yoast_metadata_identifier, $ai4seo_live_yoast_metadata )
+ || ! is_scalar( $ai4seo_live_yoast_metadata[ $ai4seo_live_yoast_metadata_identifier ] ) ) {
+ continue;
+ }
+
+ // Use the same field sanitizer as persisted editor values before allowing the snapshot into rendered form controls.
+ $ai4seo_live_yoast_metadata_value = ai4seo_sanitize_editor_field_value(
+ $ai4seo_live_yoast_metadata[ $ai4seo_live_yoast_metadata_identifier ]
+ );
+ $ai4seo_this_metadata_values[ $ai4seo_live_yoast_metadata_identifier ] = ai4seo_normalize_editor_input_value( $ai4seo_live_yoast_metadata_value );
+ $ai4seo_live_yoast_metadata_identifiers[] = $ai4seo_live_yoast_metadata_identifier;
}
// Source hints compare the active editor values with SOOZ and third-party snapshots before rendering labels.
@@ -66,246 +104,314 @@
$ai4seo_this_metadata_values
);
-// Prepare variables for prefixes and suffixes.
-$ai4seo_metadata_prefixes = ai4seo_get_setting( AI4SEO_SETTING_METADATA_PREFIXES );
-$ai4seo_metadata_suffixes = ai4seo_get_setting( AI4SEO_SETTING_METADATA_SUFFIXES );
-
-// Prepare variables for active meta tags.
-$ai4seo_active_meta_tags = ai4seo_get_active_meta_tags();
-
-$ai4seo_settings_url = ai4seo_get_subpage_url( 'settings' );
-$ai4seo_metadata_custom_instructions = ai4seo_read_custom_instructions_postmeta( $ai4seo_post_id, AI4SEO_POST_META_METADATA_CUSTOM_INSTRUCTIONS_META_KEY );
-
-
-// ___________________________________________________________________________________________ \
-// === OUTPUT ================================================================================ \
-// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ \
-
-// Use the shared AJAX modal headline shell so editor modals keep the same logo/title structure.
-ai4seo_echo_wp_kses( ai4seo_get_modal_headline_tag( __( 'Metadata Editor', 'ai-for-seo' ) ) );
-
-echo "<div class='ai4seo-metadata-editor-entry-context'>";
- echo "<div class='ai4seo-modal-sub-headline'>";
- ai4seo_echo_wp_kses(
- sprintf(
- /* translators: 1: Post title. 2: Post ID. */
- __( 'Manage metadata for <b>%1$s</b> (#%2$d)', 'ai-for-seo' ),
- $ai4seo_this_post_title,
- $ai4seo_post_id
- )
- );
- echo '</div>';
-
- // Related media uses a dedicated AJAX modal id so it can stack above the metadata editor.
- echo "<div class='ai4seo-metadata-editor-header-actions'>";
- ai4seo_echo_wp_kses( ai4seo_get_related_attachments_button( $ai4seo_post_id, esc_html__( 'Related Media', 'ai-for-seo' ) ) );
- echo '</div>';
- echo '</div>';
-
- if ( ! $ai4seo_active_meta_tags ) {
- ai4seo_echo_wp_kses(
- ai4seo_get_editor_no_active_fields_notice_tag(
- __( 'No meta tags are active. Please activate at least one meta tag in the plugin settings to manage metadata.', 'ai-for-seo' ),
- $ai4seo_settings_url
- )
- );
- return;
- }
-
- // GENERATE ALL BUTTON.
- echo "<div id='ai4seo-generate-all-metadata-button-hook'></div>";
-
- // Form.
- echo "<div class='ai4seo-form ai4seo-editor-form'>";
-
- // === CUSTOM INSTRUCTIONS ================================================================================= \
-
- $ai4seo_metadata_custom_instructions_input_name = ai4seo_get_prefixed_input_name( 'metadata_editor_custom_instructions' );
- $ai4seo_metadata_custom_instructions_description = esc_html__( 'These instructions are used only for future metadata generations for this entry.', 'ai-for-seo' );
-
- // Reuse the shared custom-instruction form item so editor-specific fields match settings fields.
- ai4seo_echo_wp_kses(
- ai4seo_get_custom_instructions_form_item_tag(
- $ai4seo_metadata_custom_instructions_input_name,
- $ai4seo_metadata_custom_instructions_input_name,
- $ai4seo_metadata_custom_instructions,
- esc_html__( 'Custom Instructions:', 'ai-for-seo' ),
- $ai4seo_metadata_custom_instructions_description,
- 'ai4seo-editor-textarea ai4seo-entry-custom-instructions-input',
- esc_html__( 'Metadata Custom Instructions', 'ai-for-seo' ),
- 'ai4seo-editor-custom-instructions-form-item',
- '',
- 'metadata-editor'
- )
- );
-
-
- // === GO THROUGH EACH FIELD ================================================================================= \
-
- $ai4seo_skipped_meta_tags = array();
-
- // Reuse the translated suffix while field names keep each help trigger distinct for assistive technology.
- $ai4seo_help_aria_label = __( 'Help', 'ai-for-seo' );
-
- foreach ( AI4SEO_METADATA_DETAILS as $ai4seo_this_metadata_identifier => $ai4seo_this_metadata_details ) {
- // Make sure that required value-entries exist.
- if ( ! isset( $ai4seo_this_metadata_details['name'] ) || ! isset( $ai4seo_this_metadata_details['input'] ) || ! isset( $ai4seo_this_metadata_details['hint'] ) ) {
- continue;
- }
-
- if ( ! in_array( $ai4seo_this_metadata_identifier, $ai4seo_active_meta_tags ) ) {
- $ai4seo_skipped_meta_tags[] = $ai4seo_this_metadata_identifier;
- continue;
- }
-
- // get the value of the post meta entry for the input-field.
- $ai4seo_this_metadata_input_value = sanitize_text_field( $ai4seo_this_metadata_values[ $ai4seo_this_metadata_identifier ] ?? '' );
+// Distinguish live unsaved Yoast state from values read from Yoast postmeta.
+foreach ( $ai4seo_live_yoast_metadata_identifiers as $ai4seo_live_yoast_metadata_identifier ) {
+ $ai4seo_metadata_source_details[ $ai4seo_live_yoast_metadata_identifier ] = ai4seo_get_editor_field_third_party_source_details(
+ 'Yoast SEO',
+ false,
+ true
+ );
+}
+
+// Prepare variables for prefixes and suffixes.
+$ai4seo_metadata_prefixes = ai4seo_get_setting( AI4SEO_SETTING_METADATA_PREFIXES );
+$ai4seo_metadata_suffixes = ai4seo_get_setting( AI4SEO_SETTING_METADATA_SUFFIXES );
+
+// Prepare variables for active meta tags.
+$ai4seo_active_meta_tags = ai4seo_get_active_meta_tags();
+
+$ai4seo_settings_url = ai4seo_get_subpage_url( 'settings' );
+$ai4seo_metadata_custom_instructions = ai4seo_read_custom_instructions_postmeta( $ai4seo_post_id, AI4SEO_POST_META_METADATA_CUSTOM_INSTRUCTIONS_META_KEY );
+
+
+// ___________________________________________________________________________________________ \
+// === OUTPUT ================================================================================ \
+// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ \
+
+// Use the shared AJAX modal headline shell so editor modals keep the same logo/title structure.
+ai4seo_echo_wp_kses( ai4seo_get_modal_headline_tag( __( 'Metadata Editor', 'ai-for-seo' ) ) );
+
+echo "<div class='ai4seo-metadata-editor-entry-context'>";
+ echo "<div class='ai4seo-modal-sub-headline'>";
+ ai4seo_echo_wp_kses(
+ sprintf(
+ /* translators: 1: Post title. 2: Post ID. */
+ __( 'Manage metadata for <b>%1$s</b> (#%2$d)', 'ai-for-seo' ),
+ $ai4seo_this_post_title,
+ $ai4seo_post_id
+ )
+ );
+ echo '</div>';
+
+ // Related media uses a dedicated AJAX modal id so it can stack above the metadata editor.
+ echo "<div class='ai4seo-metadata-editor-header-actions'>";
+ ai4seo_echo_wp_kses( ai4seo_get_related_attachments_button( $ai4seo_post_id, esc_html__( 'Related Media', 'ai-for-seo' ) ) );
+ echo '</div>';
+ echo '</div>';
+
+ if ( ! $ai4seo_active_meta_tags ) {
+ ai4seo_echo_wp_kses(
+ ai4seo_get_editor_no_active_fields_notice_tag(
+ __( 'No meta tags are active. Please activate at least one meta tag in the plugin settings to manage metadata.', 'ai-for-seo' ),
+ $ai4seo_settings_url
+ )
+ );
+ return;
+ }
+
+ // GENERATE ALL BUTTON.
+ echo "<div id='ai4seo-generate-all-metadata-button-hook'></div>";
+
+ // Form.
+ echo "<div class='ai4seo-form ai4seo-editor-form'>";
+
+ // === CUSTOM INSTRUCTIONS ================================================================================= \
+
+ $ai4seo_metadata_custom_instructions_input_name = ai4seo_get_prefixed_input_name( 'metadata_editor_custom_instructions' );
+ $ai4seo_metadata_custom_instructions_description = esc_html__( 'These instructions are used only for future metadata generations for this entry.', 'ai-for-seo' );
+
+ // Reuse the shared custom-instruction form item so editor-specific fields match settings fields.
+ ai4seo_echo_wp_kses(
+ ai4seo_get_custom_instructions_form_item_tag(
+ $ai4seo_metadata_custom_instructions_input_name,
+ $ai4seo_metadata_custom_instructions_input_name,
+ $ai4seo_metadata_custom_instructions,
+ esc_html__( 'Custom Instructions:', 'ai-for-seo' ),
+ $ai4seo_metadata_custom_instructions_description,
+ 'ai4seo-editor-textarea ai4seo-entry-custom-instructions-input',
+ esc_html__( 'Metadata Custom Instructions', 'ai-for-seo' ),
+ 'ai4seo-editor-custom-instructions-form-item',
+ '',
+ 'metadata-editor'
+ )
+ );
+
+
+ // === GO THROUGH EACH FIELD ================================================================================= \
+
+ $ai4seo_skipped_meta_tags = array();
+
+ // Reuse the translated suffix while field names keep each help trigger distinct for assistive technology.
+ $ai4seo_help_aria_label = __( 'Help', 'ai-for-seo' );
+
+ foreach ( AI4SEO_METADATA_DETAILS as $ai4seo_this_metadata_identifier => $ai4seo_this_metadata_details ) {
+ // Make sure that required value-entries exist.
+ if ( ! isset( $ai4seo_this_metadata_details['name'] ) || ! isset( $ai4seo_this_metadata_details['input'] ) || ! isset( $ai4seo_this_metadata_details['hint'] ) ) {
+ continue;
+ }
+
+ if ( ! in_array( $ai4seo_this_metadata_identifier, $ai4seo_active_meta_tags ) ) {
+ $ai4seo_skipped_meta_tags[] = $ai4seo_this_metadata_identifier;
+ continue;
+ }
+
+ // get the value of the post meta entry for the input-field.
+ $ai4seo_this_metadata_input_value = sanitize_text_field( $ai4seo_this_metadata_values[ $ai4seo_this_metadata_identifier ] ?? '' );
$ai4seo_this_metadata_input_name = sanitize_text_field( ai4seo_get_prefixed_input_name( 'metadata_' . $ai4seo_this_metadata_identifier ) );
$ai4seo_this_metadata_prefix = sanitize_text_field( $ai4seo_metadata_prefixes[ $ai4seo_this_metadata_identifier ] ?? '' );
$ai4seo_this_metadata_suffix = sanitize_text_field( $ai4seo_metadata_suffixes[ $ai4seo_this_metadata_identifier ] ?? '' );
- // Keep each metadata field in the shared editor form-item structure used by attachment fields.
- echo "<div class='ai4seo-form-item ai4seo-form-item-flush'>";
-
- // Separate the tooltip button from the native field label while preserving their visual grouping.
- echo "<span class='ai4seo-form-item-label'>";
- echo "<span class='ai4seo-label-with-tooltip'>";
- echo "<label for='" . esc_attr( $ai4seo_this_metadata_input_name ) . "'>";
- if ( isset( $ai4seo_this_metadata_details['icon'] ) ) {
- ai4seo_echo_wp_kses( ai4seo_get_svg_tag( $ai4seo_this_metadata_details['icon'], '', 'ai4seo-24x24-icon ai4seo-gray-icon' ) );
- echo ' ';
+ // Resolve the effective generation target once so initial markup and live feedback share the same bounds.
+ $ai4seo_this_quality_window = ai4seo_get_generation_length_quality_window( 'metadata', $ai4seo_this_metadata_identifier );
+ $ai4seo_this_minimum_length = absint( $ai4seo_this_quality_window['min-length'] ?? 0 );
+ $ai4seo_this_maximum_length = absint( $ai4seo_this_quality_window['max-length'] ?? 0 );
+ $ai4seo_has_quality_window = 0 < $ai4seo_this_minimum_length && $ai4seo_this_minimum_length <= $ai4seo_this_maximum_length;
+ $ai4seo_length_feedback_id = $ai4seo_this_metadata_input_name . '-length-feedback';
+ $ai4seo_length_input_class = $ai4seo_has_quality_window ? ' ai4seo-editor-length-tracked' : '';
+ $ai4seo_length_input_attributes = '';
+
+ // Expose valid bounds to JavaScript and associate the input with its live output element.
+ if ( $ai4seo_has_quality_window ) {
+ $ai4seo_length_input_attributes = ' aria-describedby="' . esc_attr( $ai4seo_length_feedback_id ) . '"'
+ . ' data-ai4seo-min-length="' . esc_attr( $ai4seo_this_minimum_length ) . '"'
+ . ' data-ai4seo-max-length="' . esc_attr( $ai4seo_this_maximum_length ) . '"';
}
-
- echo esc_html( $ai4seo_this_metadata_details['name'] );
- echo '</label>';
-
- // Keep the button out of the field label to avoid nested interactive controls.
- ai4seo_echo_wp_kses(
- ai4seo_get_icon_with_tooltip_tag(
- $ai4seo_this_metadata_details['hint'],
- '',
- 'circle-question',
- $ai4seo_this_metadata_details['name'] . ': ' . $ai4seo_help_aria_label
- )
- );
+
+ // Keep each metadata field in the shared editor form-item structure used by attachment fields.
+ echo "<div class='ai4seo-form-item ai4seo-form-item-flush'>";
+
+ // Separate the tooltip button from the native field label while preserving their visual grouping.
+ echo "<span class='ai4seo-form-item-label'>";
+ echo "<span class='ai4seo-label-with-tooltip'>";
+ echo "<label for='" . esc_attr( $ai4seo_this_metadata_input_name ) . "'>";
+ if ( isset( $ai4seo_this_metadata_details['icon'] ) ) {
+ ai4seo_echo_wp_kses( ai4seo_get_svg_tag( $ai4seo_this_metadata_details['icon'], '', 'ai4seo-24x24-icon ai4seo-gray-icon' ) );
+ echo ' ';
+ }
+
+ echo esc_html( $ai4seo_this_metadata_details['name'] );
+ echo '</label>';
+
+ // Keep the button out of the field label to avoid nested interactive controls.
+ ai4seo_echo_wp_kses(
+ ai4seo_get_icon_with_tooltip_tag(
+ $ai4seo_this_metadata_details['hint'],
+ '',
+ 'circle-question',
+ $ai4seo_this_metadata_details['name'] . ': ' . $ai4seo_help_aria_label
+ )
+ );
+ echo '</span>';
+
+ // Render the source hint directly under the label so it stays visually tied to this input.
+ if ( isset( $ai4seo_metadata_source_details[ $ai4seo_this_metadata_identifier ] ) ) {
+ ai4seo_echo_wp_kses(
+ ai4seo_get_editor_field_source_message_tag( $ai4seo_metadata_source_details[ $ai4seo_this_metadata_identifier ] )
+ );
+ }
+
+ // Keep the focus-keyphrase warning in the DOM so the editor can refresh it after AI generation and manual edits.
+ if ( 'focus-keyphrase' === $ai4seo_this_metadata_identifier ) {
+ // Match the initial state to the same condition used by the client-side refresh handler.
+ $ai4seo_should_show_focus_keyphrase_warning = ! $ai4seo_this_metadata_input_value
+ && ( ( isset( $ai4seo_this_metadata_values['meta-title'] ) && $ai4seo_this_metadata_values['meta-title'] )
+ || ( isset( $ai4seo_this_metadata_values['meta-description'] ) && $ai4seo_this_metadata_values['meta-description'] ) );
+ $ai4seo_focus_keyphrase_warning_classes = 'ai4seo-editor-field-warning-message ai4seo-sub-info ai4seo-red-message';
+
+ // Preserve the warning element for later updates while hiding it when no action is currently needed.
+ if ( ! $ai4seo_should_show_focus_keyphrase_warning ) {
+ $ai4seo_focus_keyphrase_warning_classes .= ' ai4seo-display-none';
+ }
+
+ // Escape the assembled state classes while retaining the existing translated warning markup.
+ echo "<span class='" . esc_attr( $ai4seo_focus_keyphrase_warning_classes ) . "'>";
+ ai4seo_echo_wp_kses( __( '<strong>Heads up:</strong> This entry currently has no focus keyphrase. We recommend using the <strong>Generate & Overwrite</strong> button to ensure the keyphrase is applied and reflected across all related metadata fields.', 'ai-for-seo' ) );
echo '</span>';
-
- // Render the source hint directly under the label so it stays visually tied to this input.
- if ( isset( $ai4seo_metadata_source_details[ $ai4seo_this_metadata_identifier ] ) ) {
- ai4seo_echo_wp_kses(
- ai4seo_get_editor_field_source_message_tag( $ai4seo_metadata_source_details[ $ai4seo_this_metadata_identifier ] )
- );
}
- // Keep the focus-keyphrase warning in the DOM so the editor can refresh it after AI generation and manual edits.
- if ( 'focus-keyphrase' === $ai4seo_this_metadata_identifier ) {
- // Match the initial state to the same condition used by the client-side refresh handler.
- $ai4seo_should_show_focus_keyphrase_warning = ! $ai4seo_this_metadata_input_value
- && ( ( isset( $ai4seo_this_metadata_values['meta-title'] ) && $ai4seo_this_metadata_values['meta-title'] )
- || ( isset( $ai4seo_this_metadata_values['meta-description'] ) && $ai4seo_this_metadata_values['meta-description'] ) );
- $ai4seo_focus_keyphrase_warning_classes = 'ai4seo-editor-field-warning-message ai4seo-sub-info ai4seo-red-message';
-
- // Preserve the warning element for later updates while hiding it when no action is currently needed.
- if ( ! $ai4seo_should_show_focus_keyphrase_warning ) {
- $ai4seo_focus_keyphrase_warning_classes .= ' ai4seo-display-none';
+ // Server-render the initial count so the target remains available before modal JavaScript initializes.
+ if ( $ai4seo_has_quality_window ) {
+ $ai4seo_this_input_length = ai4seo_mb_strlen( $ai4seo_this_metadata_input_value );
+ $ai4seo_length_feedback_classes = 'ai4seo-editor-length-feedback ai4seo-sub-info';
+
+ // Empty values stay neutral because the quality window is a generation target, not a required-field rule.
+ if ( 0 < $ai4seo_this_input_length
+ && ( $ai4seo_this_input_length < $ai4seo_this_minimum_length || $ai4seo_this_input_length > $ai4seo_this_maximum_length ) ) {
+ $ai4seo_length_feedback_classes .= ' ai4seo-editor-length-feedback-outside-target';
}
- // Escape the assembled state classes while retaining the existing translated warning markup.
- echo "<span class='" . esc_attr( $ai4seo_focus_keyphrase_warning_classes ) . "'>";
- ai4seo_echo_wp_kses( __( '<strong>Heads up:</strong> This entry currently has no focus keyphrase. We recommend using the <strong>Generate & Overwrite</strong> button to ensure the keyphrase is applied and reflected across all related metadata fields.', 'ai-for-seo' ) );
- echo '</span>';
- }
-
- echo '</span>';
+ /* translators: 1: Current character count. 2: Minimum target length. 3: Maximum target length. */
+ $ai4seo_length_feedback_text = sprintf(
+ _n( '%1$d character · target %2$d–%3$d', '%1$d characters · target %2$d–%3$d', $ai4seo_this_input_length, 'ai-for-seo' ),
+ $ai4seo_this_input_length,
+ $ai4seo_this_minimum_length,
+ $ai4seo_this_maximum_length
+ );
- // Render the registry-defined control inside the shared generate-button wrapper.
- echo "<div class='ai4seo-form-item-input-wrapper ai4seo-form-input-wrapper-with-generate-button'>";
- // Prefix.
- if ( $ai4seo_this_metadata_prefix ) {
- echo "<span class='ai4seo-editor-prefix ai4seo-gray-text'>";
- echo esc_html__( 'Prefix', 'ai-for-seo' ) . ': ' . esc_html( $ai4seo_this_metadata_prefix ) . ' ';
-
- // Give repeated prefix guidance a field-specific accessible trigger name.
- ai4seo_echo_wp_kses(
- ai4seo_get_icon_with_tooltip_tag(
- __( 'Prefix and suffix are added automatically when the page is rendered. Please do not include them in this input field.', 'ai-for-seo' ),
- '',
- 'circle-question',
- $ai4seo_this_metadata_details['name'] . ': ' . __( 'Prefix', 'ai-for-seo' ) . ': ' . $ai4seo_help_aria_label
- )
- );
- echo '</span><br>';
+ echo '<output'
+ . ' class="' . esc_attr( $ai4seo_length_feedback_classes ) . '"'
+ . ' id="' . esc_attr( $ai4seo_length_feedback_id ) . '"'
+ . ' for="' . esc_attr( $ai4seo_this_metadata_input_name ) . '"'
+ . '>'
+ . esc_html( $ai4seo_length_feedback_text )
+ . '</output>';
}
- // Text field.
+ echo '</span>';
+
+ // Render the registry-defined control inside the shared generate-button wrapper.
+ echo "<div class='ai4seo-form-item-input-wrapper ai4seo-form-input-wrapper-with-generate-button'>";
+ // Prefix.
+ if ( $ai4seo_this_metadata_prefix ) {
+ echo "<span class='ai4seo-editor-prefix ai4seo-gray-text'>";
+ echo esc_html__( 'Prefix', 'ai-for-seo' ) . ': ' . esc_html( $ai4seo_this_metadata_prefix ) . ' ';
+
+ // Give repeated prefix guidance a field-specific accessible trigger name.
+ ai4seo_echo_wp_kses(
+ ai4seo_get_icon_with_tooltip_tag(
+ __( 'Prefix and suffix are added automatically when the page is rendered. Please do not include them in this input field.', 'ai-for-seo' ),
+ '',
+ 'circle-question',
+ $ai4seo_this_metadata_details['name'] . ': ' . __( 'Prefix', 'ai-for-seo' ) . ': ' . $ai4seo_help_aria_label
+ )
+ );
+ echo '</span><br>';
+ }
+
+ // Text field.
if ( 'textfield' === $ai4seo_this_metadata_details['input'] ) {
- echo '<input type="text" class="ai4seo-textfield ai4seo-editor-textfield" name="' . esc_attr( $ai4seo_this_metadata_input_name ) . '" id="' . esc_attr( $ai4seo_this_metadata_input_name ) . '" value="' . esc_attr( $ai4seo_this_metadata_input_value ) . '" />';
+ echo '<input type="text"'
+ . ' class="ai4seo-textfield ai4seo-editor-textfield' . esc_attr( $ai4seo_length_input_class ) . '"'
+ . ' name="' . esc_attr( $ai4seo_this_metadata_input_name ) . '"'
+ . ' id="' . esc_attr( $ai4seo_this_metadata_input_name ) . '"'
+ . ' value="' . esc_attr( $ai4seo_this_metadata_input_value ) . '"'
+ . $ai4seo_length_input_attributes
+ . ' />';
} elseif ( 'textarea' === $ai4seo_this_metadata_details['input'] ) {
// Preserve multiline editing for metadata fields declared as textareas in the shared registry.
- echo '<textarea class="ai4seo-textarea ai4seo-editor-textarea ai4seo-auto-resize-textarea" name="' . esc_attr( $ai4seo_this_metadata_input_name ) . '" id="' . esc_attr( $ai4seo_this_metadata_input_name ) . '">' . esc_textarea( $ai4seo_this_metadata_input_value ) . '</textarea>';
+ echo '<textarea class="ai4seo-textarea ai4seo-editor-textarea ai4seo-auto-resize-textarea' . esc_attr( $ai4seo_length_input_class ) . '"'
+ . ' name="' . esc_attr( $ai4seo_this_metadata_input_name ) . '"'
+ . ' id="' . esc_attr( $ai4seo_this_metadata_input_name ) . '"'
+ . $ai4seo_length_input_attributes
+ . '>' . esc_textarea( $ai4seo_this_metadata_input_value ) . '</textarea>';
}
-
- // Suffix.
- if ( $ai4seo_this_metadata_suffix ) {
- echo "<br><span class='ai4seo-editor-suffix ai4seo-gray-text'>";
- echo esc_html__( 'Suffix', 'ai-for-seo' ) . ': ' . esc_html( $ai4seo_this_metadata_suffix ) . ' ';
-
- // Give repeated suffix guidance a field-specific accessible trigger name.
- ai4seo_echo_wp_kses(
- ai4seo_get_icon_with_tooltip_tag(
- __( 'Prefix and suffix are added automatically when the page is rendered. Please do not include them in this input field.', 'ai-for-seo' ),
- '',
- 'circle-question',
- $ai4seo_this_metadata_details['name'] . ': ' . __( 'Suffix', 'ai-for-seo' ) . ': ' . $ai4seo_help_aria_label
- )
- );
- echo '</span><br>';
- }
- echo '</div>';
-
- echo '</div>';
- }
-
- // friendly reminder: $ai4seo_skipped_meta_tags.
- if ( $ai4seo_skipped_meta_tags ) {
- ai4seo_echo_wp_kses(
- ai4seo_get_editor_inactive_fields_notice_tag(
- $ai4seo_skipped_meta_tags,
- AI4SEO_METADATA_DETAILS,
- /* translators: 1: Comma-separated list of meta tags. 2: URL to plugin settings. */
- __( '<strong>Note:</strong> The following meta tags are currently inactive and not shown in this editor: %1$s. You can activate them in the <a href="%2$s" target="_blank">plugin settings</a>.', 'ai-for-seo' ),
- $ai4seo_settings_url
- )
- );
- }
-
- // put the post id into a hidden field, so we have access to it after the form is submitted.
- echo "<input type='hidden' id='ai4seo-editor-modal-post-id' name='" . esc_attr( ai4seo_get_prefixed_input_name( 'metadata_editor_post_id' ) ) . "' value='" . esc_attr( $ai4seo_post_id ) . "' />";
- echo "<input type='hidden' id='ai4seo-read-page-content-via-js' value='" . esc_attr( $ai4seo_read_page_content_via_js ) . "' />";
-
-
- // === BUTTONS ROW ================================================================================= \
-
- // Collect footer buttons before rendering the shared wrapper so the optional next-entry action keeps its current order.
- $ai4seo_modal_footer_button_tags = array(
- ai4seo_get_modal_close_button_tag( esc_html__( 'Abort', 'ai-for-seo' ), 'ai4seo-big-button' ),
- );
-
- // Let the shared editor helper add the next-entry action only when this list has another post to edit.
- $ai4seo_modal_footer_button_tags[] = ai4seo_get_editor_save_next_button_tag(
- $ai4seo_next_post_id,
- 'ai4seo_validate_metadata_editor_inputs',
- 'ai4seo_open_metadata_editor_modal',
+
+ // Suffix.
+ if ( $ai4seo_this_metadata_suffix ) {
+ echo "<br><span class='ai4seo-editor-suffix ai4seo-gray-text'>";
+ echo esc_html__( 'Suffix', 'ai-for-seo' ) . ': ' . esc_html( $ai4seo_this_metadata_suffix ) . ' ';
+
+ // Give repeated suffix guidance a field-specific accessible trigger name.
+ ai4seo_echo_wp_kses(
+ ai4seo_get_icon_with_tooltip_tag(
+ __( 'Prefix and suffix are added automatically when the page is rendered. Please do not include them in this input field.', 'ai-for-seo' ),
+ '',
+ 'circle-question',
+ $ai4seo_this_metadata_details['name'] . ': ' . __( 'Suffix', 'ai-for-seo' ) . ': ' . $ai4seo_help_aria_label
+ )
+ );
+ echo '</span><br>';
+ }
+ echo '</div>';
+
+ echo '</div>';
+ }
+
+ // friendly reminder: $ai4seo_skipped_meta_tags.
+ if ( $ai4seo_skipped_meta_tags ) {
+ ai4seo_echo_wp_kses(
+ ai4seo_get_editor_inactive_fields_notice_tag(
+ $ai4seo_skipped_meta_tags,
+ AI4SEO_METADATA_DETAILS,
+ /* translators: 1: Comma-separated list of meta tags. 2: URL to plugin settings. */
+ __( '<strong>Note:</strong> The following meta tags are currently inactive and not shown in this editor: %1$s. You can activate them in the <a href="%2$s" target="_blank">plugin settings</a>.', 'ai-for-seo' ),
+ $ai4seo_settings_url
+ )
+ );
+ }
+
+ // put the post id into a hidden field, so we have access to it after the form is submitted.
+ echo "<input type='hidden' id='ai4seo-editor-modal-post-id' name='" . esc_attr( ai4seo_get_prefixed_input_name( 'metadata_editor_post_id' ) ) . "' value='" . esc_attr( $ai4seo_post_id ) . "' />";
+ echo "<input type='hidden' id='ai4seo-read-page-content-via-js' value='" . esc_attr( $ai4seo_read_page_content_via_js ) . "' />";
+
+
+ // === BUTTONS ROW ================================================================================= \
+
+ // Collect footer buttons before rendering the shared wrapper so the optional next-entry action keeps its current order.
+ $ai4seo_modal_footer_button_tags = array(
+ ai4seo_get_modal_close_button_tag( esc_html__( 'Abort', 'ai-for-seo' ), 'ai4seo-big-button' ),
+ );
+
+ // Let the shared editor helper add the next-entry action only when this list has another post to edit.
+ $ai4seo_modal_footer_button_tags[] = ai4seo_get_editor_save_next_button_tag(
+ $ai4seo_next_post_id,
+ 'ai4seo_validate_metadata_editor_inputs',
+ 'ai4seo_open_metadata_editor_modal',
array(
'true' === $ai4seo_read_page_content_via_js,
$ai4seo_all_post_ids,
- )
+ ),
+ 'ai4seo_handle_metadata_editor_save_success'
);
-
- // Keep the normal save action last, matching the previous explicit footer markup.
- $ai4seo_modal_footer_button_tags[] = ai4seo_get_submit_button_tag( esc_html__( 'Save changes', 'ai-for-seo' ), 'ai4seo-big-button ai4seo-lockable ai4seo-start-inactive', 'ai4seo_save_anything(jQuery(this), ai4seo_validate_metadata_editor_inputs, function() { ai4seo_safe_page_load(); });' );
-
- // Render the action row through the shared footer helper used by AJAX modals.
- ai4seo_echo_wp_kses( ai4seo_get_modal_footer_tag( $ai4seo_modal_footer_button_tags ) );
-
- echo '</div>';
+
+ // Keep the normal save action last, matching the previous explicit footer markup.
+ $ai4seo_modal_footer_button_tags[] = ai4seo_get_submit_button_tag(
+ esc_html__( 'Save changes', 'ai-for-seo' ),
+ 'ai4seo-big-button ai4seo-lockable ai4seo-start-inactive',
+ 'ai4seo_save_anything(jQuery(this), ai4seo_validate_metadata_editor_inputs, ai4seo_handle_metadata_editor_save_success);'
+ );
+
+ // Render the action row through the shared footer helper used by AJAX modals.
+ ai4seo_echo_wp_kses( ai4seo_get_modal_footer_tag( $ai4seo_modal_footer_button_tags ) );
+
+ echo '</div>';
--- a/ai-for-seo/includes/ajax/display/related-attachments.php
+++ b/ai-for-seo/includes/ajax/display/related-attachments.php
@@ -31,6 +31,11 @@
ai4seo_send_ajax_error( esc_html__( 'Post id is invalid.', 'ai-for-seo' ), 11032601 );
}
+// Related-media discovery can expose post context, so require object-level access to its source post.
+if ( ! ai4seo_can_edit_post( $ai4seo_post_id ) ) {
+ ai4seo_send_ajax_error( esc_html__( 'You are not allowed to edit this entry.', 'ai-for-seo' ), 11032604 );
+}
+
$ai4seo_post = get_post( $ai4seo_post_id );
if ( ! $ai4seo_post || is_wp_error( $ai4seo_post ) || ! isset( $ai4seo_post->post_type ) ) {
@@ -55,6 +60,10 @@
$ai4seo_is_related_attachments_modal = true;
$ai4seo_related_attachments_modal_post_id = $ai4seo_post_id;
+// Exclude malformed and unauthorized media IDs before the shared table renderer receives its filter.
+$ai4seo_attachment_post_ids_filter = array_values( array_unique( array_map( 'absint', $ai4seo_attachment_post_ids_filter ) ) );
+$ai4seo_attachment_post_ids_filter = array_values( array_filter( $ai4seo_attachment_post_ids_filter, 'ai4seo_can_edit_post' ) );
+
// Persist the full scanner result before table filtering so generation can use it as a context fallback later.
if ( ! ai4seo_update_attachment_related_post_id_for_attachment_post_ids(
$ai4seo_attachment_post_ids_filter,
--- a/ai-for-seo/includes/ajax/process/generate-attachment-attributes.php
+++ b/ai-for-seo/includes/ajax/process/generate-attachment-attributes.php
@@ -55,12 +55,17 @@
// get sanitized post id parameter.
$ai4seo_this_attachment_post_id = absint( wp_unslash( $_REQUEST['ai4seo_post_id'] ?? 0 ) );
-if ( $ai4seo_this_attachment_post_id <= 0 ) {
- ai4seo_send_ajax_error( esc_html__( 'Media post id is invalid.', 'ai-for-seo' ), 211823824 );
-}
-
-
-// === CHECK PARAMETER: GENERATION FIELDS ==================================================== \
+if ( $ai4seo_this_attachment_post_id <= 0 ) {
+ ai4seo_send_ajax_error( esc_html__( 'Media post id is invalid.', 'ai-for-seo' ), 211823824 );
+}
+
+// Generation can later persist attributes, so enforce WordPress's permission for this media object.
+if ( ! ai4seo_can_edit_post( $ai4seo_this_attachment_post_id ) ) {
+ ai4seo_send_ajax_error( esc_html__( 'You are not allowed to edit this media entry.', 'ai-for-seo' ), 211823825 );
+}
+
+
+// === CHECK PARAMETER: GENERATION FIELDS ==================================================== \
// Preserve the old parameter shape while making the direct request read WPCS-compliant.
$ai4seo_generation_fields = ai4seo_deep_sanitize(
@@ -163,7 +168,7 @@
$ai4seo_robhub_api_call_parameters['trigger'] = 'manual';
$ai4seo_robhub_api_call_parameters['website_context'] = ai4seo_get_website_context();
-$ai4seo_attachment_usage_context = ai4seo_get_attachment_post_related_context( $ai4seo_this_attachment_post_id );
+$ai4seo_attachment_usage_context = ai4seo_get_attachment_post_related_context( $ai4seo_this_attachment_post_id, true );
if ( ! empty( $ai4seo_attachment_usage_context ) ) {
$ai4seo_robhub_api_call_parameters['attachment_usage_context'] = $ai4seo_attachment_usage_context;
--- a/ai-for-seo/includes/ajax/process/generate-metadata.php
+++ b/ai-for-seo/includes/ajax/process/generate-metadata.php
@@ -52,6 +52,11 @@
ai4seo_send_ajax_error( esc_html__( 'Post id is invalid.', 'ai-for-seo' ), 34127323 );
}
+// Generation can later persist metadata, so enforce WordPress's permission for this post object.
+if ( ! ai4seo_can_edit_post( $ai4seo_post_id ) ) {
+ ai4seo_send_ajax_error( esc_html__( 'You are not allowed to edit this entry.', 'ai-for-seo' ), 34127324 );
+}
+
// === CHECK PARAMETER: CONTENT ========================================================== \
@@ -102,21 +107,14 @@
// === PREPARE POST CONTENT ================================================================== \
// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ \
-if ( $ai4seo_post_content ) {
- ai4seo_condense_raw_post_content( $ai4seo_post_content );
-}
-
-// we do not have post content yet, so we try to get it from the database.
-if ( ! $ai4seo_post_content ) {
- $ai4seo_post_content = ai4seo_get_condensed_post_content_from_database( $ai4seo_post_id );
-}
-
-$ai4seo_post_context = $ai4seo_post_content;
-ai4seo_add_post_context( $ai4seo_post_id, $ai4seo_post_context, false, false );
-
-if ( ! $ai4seo_post_content && $ai4seo_post_context ) {
- $ai4seo_post_content = $ai4seo_post_context;
-}
+// Reuse the same preparation contract as cron so manual and automated requests classify identical content equally.
+$ai4seo_prepared_content = ai4seo_prepare_metadata_generation_content_data(
+ $ai4seo_post_id,
+ $ai4seo_post_content
+);
+$ai4seo_post_content = $ai4seo_prepared_content['content'];
+$ai4seo_post_context = $ai4seo_prepared_content['post_context'];
+$ai4seo_content_analysis = $ai4seo_prepared_content['content_analysis'];
// check if content is too large (should never happen as we already condensed the content).
if ( ai4seo_mb_strlen( $ai4seo_post_content ) > AI4SEO_MAX_TOTAL_CONTENT_SIZE ) {
@@ -141,13 +139,14 @@
'language' => $ai4seo_metadata_generation_language,
);
-if ( $ai4seo_keyphrase ) {
+if ( '' !== $ai4seo_keyphrase ) {
$ai4seo_robhub_api_call_parameters['keyphrase'] = $ai4seo_keyphrase;
}
-$ai4seo_robhub_api_call_parameters['trigger'] = 'manual';
-$ai4seo_robhub_api_call_parameters['website_context'] = ai4seo_get_website_context();
-$ai4seo_robhub_api_call_parameters['post_context'] = $ai4seo_post_context;
+$ai4seo_robhub_api_call_parameters['trigger'] = 'manual';
+$ai4seo_robhub_api_call_parameters['website_context'] = ai4seo_get_website_context();
+$ai4seo_robhub_api_call_parameters['post_context'] = $ai4seo_post_context;
+$ai4seo_robhub_api_call_parameters['content_analysis'] = $ai4seo_content_analysis;
// url.
$ai4seo_post_permalink = get_permalink( $ai4seo_post_id );
@@ -169,6 +168,14 @@
foreach ( $ai4seo_active_meta_tags as $ai4seo_this_active_meta_tag ) {
$ai4seo_this_to_generate = in_array( $ai4seo_this_active_meta_tag, $ai4seo_generation_fields );
$ai4seo_this_old_value = $ai4seo_old_input_values[ $ai4seo_this_active_meta_tag ] ?? '';
+
+ // Normalize client context and reject unresolved third-party templates before building RobHub instructions.
+ $ai4seo_this_old_value = is_scalar( $ai4seo_this_old_value ) ? (string) $ai4seo_this_old_value : '';
+ $ai4seo_this_old_value = ai4seo_prepare_third_party_seo_metadata_value_for_generation_context(
+ $ai4seo_this_old_value,
+ $ai4seo_post_id,
+ $ai4seo_this_active_meta_tag
+ );
$ai4seo_this_prefix = $ai4seo_metadata_prefixes[ $ai4seo_this_active_meta_tag ] ?? '';
$ai4seo_this_suffix = $ai4seo_metadata_suffixes[ $ai4seo_this_active_meta_tag ] ?? '';
--- a/ai-for-seo/includes/ajax/process/save-anything-categories/save-attachment-attributes-editor-values.php
+++ b/ai-for-seo/includes/ajax/process/save-anything-categories/save-attachment-attributes-editor-values.php
@@ -32,6 +32,14 @@
// Preserve the editor's existing integer coercion before validating the attachment target.
$ai4seo_this_attachment_post_id = intval( $upcoming_save_anything_updates['attachment_attributes_editor_post_id'] );
+ // The shared save dispatcher authorizes the plugin, while this handler authorizes its concrete media target.
+ if ( ! ai4seo_can_edit_post( $ai4seo_this_attachment_post_id ) ) {
+ return new WP_Error(
+ 6911221025,
+ esc_html__( 'You are not allowed to edit this media entry.', 'ai-for-seo' )
+ );
+ }
+
// Track field presence separately because an empty instruction value intentionally clears its postmeta.
$ai4seo_custom_instructions_were_submitted = array_key_exists( 'attachment_attributes_editor_custom_instructions', $upcoming_save_anything_updates );
--- a/ai-for-seo/includes/ajax/process/save-anything-categories/save-metadata-editor-values.php
+++ b/ai-for-seo/includes/ajax/process/save-anything-categories/save-metadata-editor-values.php
@@ -16,7 +16,7 @@
* Processes metadata editor values from sanitized save-anything data.
*
* @param array $upcoming_save_anything_updates Sanitized updates shared by the ordered save-anything processors.
- * @return WP_Error|null Error on failure, null on success or no-op.
+ * @return WP_Error|array|null Error on failure, response data on metadata success, or null on no-op.
*/
function ai4seo_process_save_anything_metadata_editor_values( array &$upcoming_save_anything_updates ) {
// Preserve the category's existing silent no-op behavior for users without plugin-management rights.
@@ -32,6 +32,14 @@
// Preserve the editor's existing integer coercion before passing the target to postmeta helpers.
$ai4seo_this_post_id = intval( $upcoming_save_anything_updates['metadata_editor_post_id'] );
+ // The shared save dispatcher authorizes the plugin, while this handler authorizes its concrete post target.
+ if ( ! ai4seo_can_edit_post( $ai4seo_this_post_id ) ) {
+ return new WP_Error(
+ 6811221025,
+ esc_html__( 'You are not allowed to edit this entry.', 'ai-for-seo' )
+ );
+ }
+
// Track field presence separately because an empty instruction value intentionally clears its postmeta.
$ai4seo_custom_instructions_were_submitted = array_key_exists( 'metadata_editor_custom_instructions', $upcoming_save_anything_updates );
@@ -149,19 +157,134 @@
}
// Persist the complete validated metadata collection through the existing active-metadata mechanism.
- $ai4seo_metadata_was_updated = ai4seo_update_active_metadata( $ai4seo_this_post_id, $ai4seo_new_metadata, true );
+ $ai4seo_metadata_update_details = array();
+ $ai4seo_metadata_update_succeeded = ai4seo_update_active_metadata( $ai4seo_this_post_id, $ai4seo_new_metadata, true, $ai4seo_metadata_update_details );
+
+ // Refresh derived state whenever SOOZ persistence succeeded, including partial third-party synchronization.
+ if ( ! empty( $ai4seo_metadata_update_details['active_metadata_succeeded'] ) ) {
+ ai4seo_refresh_one_posts_metadata_coverage_status( $ai4seo_this_post_id );
+ ai4seo_remove_post_ids_from_all_generation_status_options( $ai4seo_this_post_id );
+ }
+
+ // A partial third-party result is still a successful editor save because SOOZ owns the submitted values.
+ $ai4seo_third_party_sync_failed = ! $ai4seo_metadata_update_succeeded
+ && ! empty( $ai4seo_metadata_update_details['active_metadata_succeeded'] )
+ && empty( $ai4seo_metadata_update_details['third_party_sync_succeeded'] );
+ $ai4seo_failed_third_party_syncs = $ai4seo_metadata_update_details['failed_third_party_syncs'] ?? array();
+ $ai4seo_third_party_sync_warning = '';
+
+ if ( $ai4seo_third_party_sync_failed ) {
+ $ai4seo_failed_plugin_names = ai4seo_get_third_party_seo_plugin_names(
+ array_keys( $ai4seo_failed_third_party_syncs )
+ );
+
+ if ( $ai4seo_failed_plugin_names ) {
+ // Keep warning text unescaped in the JSON payload; the toast renderer inserts it with jQuery.text().
+ $ai4seo_third_party_sync_warning = sprintf(
+ /* translators: %s: Comma-separated third-party SEO plugin names. */
+ __( 'Metadata was saved in SOOZ, but it could not be synchronized with %s.', 'ai-for-seo' ),
+ implode( ', ', $ai4seo_failed_plugin_names )
+ );
+ } else {
+ $ai4seo_third_party_sync_warning = __(
+ 'Metadata was saved in SOOZ, but one or more third-party SEO plugins could not be synchronized.',
+ 'ai-for-seo'
+ );
+ }
+
+ // Add storage-initialization guidance only for integrations that failed and commonly need it.
+ $ai4seo_squirrly_sync_failed = isset( $ai4seo_failed_third_party_syncs[ AI4SEO_THIRD_PARTY_PLUGIN_SQUIRRLY_SEO ] );
+ $ai4seo_aioseo_sync_failed = isset( $ai4seo_failed_third_party_syncs[ AI4SEO_THIRD_PARTY_PLUGIN_ALL_IN_ONE_SEO ] );
+
+ if ( $ai4seo_squirrly_sync_failed && $ai4seo_aioseo_sync_failed ) {
+ $ai4seo_third_party_sync_warning .= ' ' . __(
+ 'A common fix is to save this entry once in the Squirrly SEO Snippet editor and update it once in WordPress so All in One SEO can initialize its metadata, then save again in SOOZ.',
+ 'ai-for-seo'
+ );
+ } elseif ( $ai4seo_squirrly_sync_failed ) {
+ $ai4seo_third_party_sync_warning .= ' ' . __(
+ 'A common fix is to open the Squirrly SEO Snippet editor for this entry, click its Save button once, and then save again in SOOZ.',
+ 'ai-for-seo'
+ );
+ } elseif ( $ai4seo_aioseo_sync_failed ) {
+ $ai4seo_third_party_sync_warning .= ' ' . __(
+ 'A common fix is to update this entry once in WordPress so All in One SEO can initialize its metadata, and then save again in SOOZ.',
+ 'ai-for-seo'
+ );
+ }
+ }
- // Do not refresh derived status when the underlying metadata write did not complete.
- if ( ! $ai4seo_metadata_was_updated ) {
+ // Genuine SOOZ failures and unclassified failures retain the existing generic error response.
+ if ( ! $ai4seo_metadata_update_succeeded && ! $ai4seo_third_party_sync_failed ) {
return new WP_Error(
3518161025,
esc_html__( 'Failed to update metadata. Please try again.', 'ai-for-seo' )
);
}
- // Refresh both derived metadata coverage and generation queues after a successful editor write.
- ai4seo_refresh_one_posts_metadata_coverage_status( $ai4seo_this_post_id );
- ai4seo_remove_post_ids_from_all_generation_status_options( $ai4seo_this_post_id );
+ // Read back only the Yoast fields that this installation is configured to synchronize.
+ $ai4seo_yoast_metadata = array();
+ $ai4seo_yoast_sync_metadata_identifiers = ai4seo_get_third_party_seo_plugin_sync_metadata_identifiers(
+ AI4SEO_THIRD_PARTY_PLUGIN_YOAST_SEO
+ );
+ $ai4seo_third_party_seo_plugin_details = ai4seo_get_third_party_seo_plugin_details();
+ $ai4seo_yoast_plugin_details = $ai4seo_third_party_seo_plugin_details[ AI4SEO_THIRD_PARTY_PLUGIN_YOAST_SEO ] ?? array();
+ $ai4seo_yoast_postmeta_keys = $ai4seo_yoast_plugin_details['generation-field-postmeta-keys'] ?? array();
+
+ // Return only submitted fields that were configured for Yoast synchronization in this save request.
+ foreach ( $ai4seo_yoast_sync_metadata_identifiers as $ai4seo_yoast_sync_metadata_identifier ) {
+ if ( ! array_key_exists( $ai4seo_yoast_sync_metadata_identifier, $ai4seo_new_metadata )
+