Writer meta type => get_seo_info() key. */ private const INFO_KEYS = array( 'title' => 'meta_title', 'description' => 'meta_description', 'focus_keyword' => 'focus_keyword', ); /** @var string */ private $key; /** @var string 'title', 'description' or 'focus_keyword'. */ private $metaType; /** @var callable(): (array|null) The active plugin's config, as SeoPluginDetector::detect() answers. */ private $detector; /** @var object `SeoMetaWriter`-shaped (write(int,string,string,string):bool). */ private $writer; /** * @param string $key Fingerprint key. * @param string $metaType 'title', 'description' or 'focus_keyword'. * @param callable|null $detector Active plugin config; SeoPluginDetector::detect() when omitted. * @param object|null $writer `SeoMetaWriter`-shaped writer; a new one when omitted. */ public function __construct( string $key, string $metaType, ?callable $detector = null, $writer = null ) { if ( ! isset( self::INFO_KEYS[ $metaType ] ) ) { // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Developer-facing exception message, not user output. throw new \InvalidArgumentException( 'Unknown SEO meta type: ' . $metaType ); } $this->key = $key; $this->metaType = $metaType; $this->detector = $detector ?? array( SeoPluginDetector::class, 'detect' ); $this->writer = $writer; } /** * @return string */ public function key(): string { return $this->key; } /** * @param int $productId Product id. * @return array{plugin:string,value:string}|null */ public function read( int $productId ) { $config = $this->activePlugin(); if ( null === $config ) { return null; } $info = SeoPluginDetector::get_seo_info( $productId, $config, false ); return array( 'plugin' => (string) $config['plugin_slug'], 'value' => (string) ( $info[ self::INFO_KEYS[ $this->metaType ] ] ?? '' ), ); } /** * The plugin and the visible text: a form opened under another SEO plugin * never matches. * * @param mixed $value What read() returned. * @return string */ public function fingerprint( $value ): string { $plugin = is_array( $value ) ? (string) ( $value['plugin'] ?? '' ) : ''; $text = is_array( $value ) ? (string) ( $value['value'] ?? '' ) : ''; return md5( $plugin . "\n" . StaleProductFormGuard::textFingerprint( $text ) ); } /** * @param int $productId Product id. * @param mixed $value What read() returned before the save. * @return void * @throws \RuntimeException When the writer could not store the value. */ public function restore( int $productId, $value ): void { $plugin = is_array( $value ) ? (string) ( $value['plugin'] ?? '' ) : ''; $text = is_array( $value ) ? (string) ( $value['value'] ?? '' ) : ''; $writer = $this->writer ?? new SeoMetaWriter(); if ( ! $writer->write( $productId, $this->metaType, $text, $plugin ) ) { throw new \RuntimeException( 'The SEO plugin field was not written.' ); } } /** * The active plugin's config when this field applies to it. * * @return array|null */ private function activePlugin(): ?array { $config = call_user_func( $this->detector ); if ( ! is_array( $config ) || empty( $config['plugin_slug'] ) ) { return null; } $slug = (string) $config['plugin_slug']; if ( 'aioseo' === $slug && ( ! defined( 'AIOSEO_VERSION' ) || version_compare( (string) AIOSEO_VERSION, '5.0.0', '<' ) ) ) { return null; } if ( 'aioseo' !== $slug && empty( $config['meta_keys'][ $this->metaType . '_key' ] ) ) { return null; } return $config; } }