{writerKey for the payload sent to SeoMetaApplier, infoKey as returned by SeoPluginDetector::get_seo_info()}. * * @var array */ private const FIELD_MAP = array( 'metaTitle' => array( 'writerKey' => 'meta_title', 'infoKey' => 'meta_title' ), 'metaDescription' => array( 'writerKey' => 'meta_description', 'infoKey' => 'meta_description' ), 'focusKeyword' => array( 'writerKey' => 'focus_keyword', 'infoKey' => 'focus_keyword' ), ); /** * `$outputs` key => the single-field `field_type` {@see \AIPT\Core\ContentReview\SeoMetaApplier} * accepts as plain text (the same shape `SeoMetaGeneratorController`'s own * review approval already sends it, one field per row). * * @var array */ private const APPLIER_FIELD_TYPE = array( 'metaTitle' => 'seo_meta_title', 'metaDescription' => 'seo_meta_description', 'focusKeyword' => 'seo_focus_keyword', ); /** * @inheritDoc */ public function apply( array $outputs, array $context ): array { $product_id = (int) ( $context['productId'] ?? 0 ); $overwrite = (string) ( $context['overwrite'] ?? 'replace' ); $plugin = SeoPluginDetector::detect(); $plugin_slug = is_array( $plugin ) ? (string) $plugin['plugin_slug'] : ''; $current = ( '' !== $plugin_slug ) ? SeoPluginDetector::get_seo_info( $product_id, $plugin, false ) : array(); $keyword_unsupported = '' !== $plugin_slug && empty( $plugin['meta_keys']['focus_keyword_key'] ); $fields = array(); $plan = array(); $keyword_dropped = false; foreach ( self::FIELD_MAP as $output_key => $map ) { if ( 'focusKeyword' === $output_key && $keyword_unsupported ) { $keyword_dropped = '' !== trim( (string) ( $outputs['focusKeyword'] ?? '' ) ); continue; } $value = isset( $outputs[ $output_key ] ) ? trim( (string) $outputs[ $output_key ] ) : ''; if ( '' === $value ) { continue; } $current_value = trim( SeoFieldValueNormalizer::flatten( $current[ $map['infoKey'] ] ?? '' ) ); if ( 'empty_only' === $overwrite && '' !== $current_value ) { $fields[] = array( 'field' => $output_key, 'outcome' => 'skipped_not_empty' ); continue; } $plan[ $output_key ] = $value; $fields[] = array( 'field' => $output_key, 'outcome' => 'written' ); } if ( $keyword_dropped && ! empty( $plan ) ) { $fields[] = array( 'field' => 'focusKeyword', 'outcome' => 'skipped_unsupported' ); } if ( empty( $plan ) ) { return array( 'status' => 'skipped', 'fields' => $fields, 'message' => __( 'Nothing to apply.', 'ai-product-tools' ) ); } $row_base = array_merge( array( 'product_id' => $product_id, 'plugin_slug' => $plugin_slug ), $this->blueprintRowFields( $context ) ); $any_written = false; $any_failed = false; $refusal = null; foreach ( $plan as $output_key => $value ) { $row = array_merge( $row_base, array( 'field_type' => self::APPLIER_FIELD_TYPE[ $output_key ], 'generated_value' => $value ) ); $outcome = $this->callApplier( 'seo_meta', $row ); if ( null !== $outcome['refusalMessage'] || ! $outcome['ok'] ) { $any_failed = true; $refusal = $refusal ?? $outcome['refusalMessage']; $fields = $this->markFieldsFailed( $fields, array( $output_key ) ); continue; } $any_written = true; } if ( $any_failed ) { $message = $refusal ?? ( $any_written ? __( 'Some SEO meta was applied; the rest could not be written.', 'ai-product-tools' ) : __( 'The write did not complete.', 'ai-product-tools' ) ); return array( 'status' => 'skipped', 'fields' => $fields, 'message' => $message ); } return array( 'status' => 'applied', 'fields' => $fields, 'message' => __( 'SEO meta applied.', 'ai-product-tools' ) ); } }