`SeoPluginDetector::get_seo_info()` / * `SeoMetaWriter::buildWriteData()` field key. * * @var array */ private const INFO_KEYS = array( 'title' => 'meta_title', 'description' => 'meta_description', 'focus' => 'focus_keyword', ); /** * Duck-typed `SeoMetaWriter` (bulkWrite(int,array,string):array); injectable for tests. * * @var object */ private $writer; /** * @param object|null $writer `SeoMetaWriter`-shaped collaborator; a default instance is constructed when omitted. */ public function __construct( $writer = null ) { $this->writer = $writer ?? new SeoMetaWriter(); } /** * Register the guard's `wp_after_insert_post` hook once. * * @return void */ public static function init(): void { add_action( 'wp_after_insert_post', array( new self(), 'restore' ), 10, 3 ); } /** * Remember a product-screen SEO apply's before/after values for one hour. * While an unconsumed record exists for the same plugin, its original * "before" values are kept and only "after" moves forward. * * @param int $productId Product id. * @param array{title:string,description:string,focus:string} $before Values read before the write. * @param array{title:string,description:string,focus:string} $after Values that were written (unwritten fields repeat `$before`). * @param string $pluginSlug Active SEO plugin slug at apply time. * @return void */ public function record( int $productId, array $before, array $after, string $pluginSlug ): void { $before = self::normalizeFields( $before ); $existing = get_transient( self::transientKey( $productId ) ); if ( is_array( $existing ) && $pluginSlug === (string) ( $existing['pluginSlug'] ?? '' ) && is_array( $existing['before'] ?? null ) ) { $before = self::normalizeFields( $existing['before'] ); } set_transient( self::transientKey( $productId ), array( 'before' => $before, 'after' => self::normalizeFields( $after ), 'pluginSlug' => $pluginSlug, ), HOUR_IN_SECONDS ); } /** * `wp_after_insert_post` callback: re-applies a field's AI value when the * product's next real save reverted it to its exact pre-apply value. * * @param int $postId Post id. * @param WP_Post $post Post object. * @param bool $update Whether this is an existing post being updated. * @return void */ public function restore( int $postId, WP_Post $post, bool $update ): void { if ( 'product' !== $post->post_type ) { return; } if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { return; } $key = self::transientKey( $postId ); $record = get_transient( $key ); delete_transient( $key ); if ( ! is_array( $record ) ) { return; } $plugin_slug = (string) ( $record['pluginSlug'] ?? '' ); if ( '' === $plugin_slug ) { return; } $before = self::normalizeFields( is_array( $record['before'] ?? null ) ? $record['before'] : array() ); $after = self::normalizeFields( is_array( $record['after'] ?? null ) ? $record['after'] : array() ); $current = $this->readCurrent( $postId, $plugin_slug ); $reverted = array(); foreach ( self::INFO_KEYS as $field => $info_key ) { if ( $current[ $field ] === $before[ $field ] && $current[ $field ] !== $after[ $field ] ) { $reverted[ $info_key ] = $after[ $field ]; } } if ( empty( $reverted ) ) { return; } $write_data = SeoMetaWriter::buildWriteData( $reverted, $plugin_slug ); if ( empty( $write_data ) ) { return; } $this->writer->bulkWrite( $postId, $write_data, $plugin_slug ); } /** * @param int $productId Product id. * @param string $pluginSlug Stored plugin slug. * @return array{title:string,description:string,focus:string} */ private function readCurrent( int $productId, string $pluginSlug ): array { $meta_keys = SeoPluginDetector::get_meta_keys( $pluginSlug ); $plugin_config = array( 'plugin_slug' => $pluginSlug, 'plugin_name' => null, 'meta_keys' => $meta_keys ?? array( 'title_key' => null, 'description_key' => null, 'focus_keyword_key' => null, 'score_key' => null, 'score_type' => null, ), ); $info = SeoPluginDetector::get_seo_info( $productId, $plugin_config, false ); return array( 'title' => (string) ( $info['meta_title'] ?? '' ), 'description' => (string) ( $info['meta_description'] ?? '' ), 'focus' => (string) ( $info['focus_keyword'] ?? '' ), ); } /** * @param array $fields Partial {title,description,focus} map. * @return array{title:string,description:string,focus:string} */ private static function normalizeFields( array $fields ): array { return array( 'title' => (string) ( $fields['title'] ?? '' ), 'description' => (string) ( $fields['description'] ?? '' ), 'focus' => (string) ( $fields['focus'] ?? '' ), ); } /** * @param int $productId Product id. * @return string */ private static function transientKey( int $productId ): string { return self::TRANSIENT_PREFIX . $productId; } }