'title_key', 'metaDescription' => 'description_key', 'focusKeyword' => 'focus_keyword_key', ); private const FALLBACK_KEYS = array( 'title_key' => '_aipt_seo_title', 'description_key' => '_aipt_seo_description', 'focus_keyword_key' => '_aipt_seo_focus_keyword', ); private $detected_plugin; private $meta_keys; private $writer; /** * Falls back to native _aipt_seo_* meta keys when no supported SEO plugin is detected. */ public function __construct( ?SeoMetaWriter $writer = null ) { $this->writer = $writer ?? new SeoMetaWriter(); $detected = SeoPluginDetector::detect(); if ( is_array( $detected ) && isset( $detected['plugin_slug'], $detected['meta_keys'] ) && is_array( $detected['meta_keys'] ) ) { $this->detected_plugin = (string) $detected['plugin_slug']; $this->meta_keys = $detected['meta_keys']; return; } $this->detected_plugin = 'standalone'; $this->meta_keys = self::FALLBACK_KEYS; } public function reads(): array { $fields = array(); foreach ( array_keys( self::FIELD_TO_KEY ) as $field_id ) { if ( null !== $this->meta_key_for_field( $field_id ) ) { $fields[] = $field_id; } } return $fields; } public function get_detected_plugin(): string { return $this->detected_plugin; } public function read( \WC_Product $product, string $field_id ) { $meta_key = $this->meta_key_for_field( $field_id ); if ( null === $meta_key ) { return null; } if ( 'aioseo' === $this->detected_plugin ) { $info = SeoPluginDetector::get_seo_info( (int) $product->get_id(), array( 'plugin_slug' => $this->detected_plugin, 'plugin_name' => null, 'meta_keys' => $this->meta_keys ), false ); $keys = array( 'seoTitle' => 'meta_title', 'metaDescription' => 'meta_description', 'focusKeyword' => 'focus_keyword' ); return $info[ $keys[ $field_id ] ] ?? ''; } return SeoFieldValueNormalizer::flatten( \get_post_meta( (int) $product->get_id(), $meta_key, true ) ); } public function write( \WC_Product $product, string $field_id, $value ): array { $meta_key = $this->meta_key_for_field( $field_id ); if ( null === $meta_key ) { if ( isset( self::FIELD_TO_KEY[ $field_id ] ) ) { return array( 'saved' => false, 'message' => 'SEO field is not supported by the detected SEO plugin.' ); } return array( 'saved' => false, 'message' => 'Unsupported SEO field.' ); } $sanitized = SeoMetaWriter::sanitize_value( (string) $value, 'metaDescription' === $field_id ); if ( 'aioseo' === $this->detected_plugin ) { $keys = array( 'seoTitle' => 'title', 'metaDescription' => 'description', 'focusKeyword' => 'focus_keyword' ); $saved = $this->writer->write( (int) $product->get_id(), $keys[ $field_id ], $sanitized, $this->detected_plugin ); return array( 'saved' => $saved, 'message' => $saved ? null : 'Failed to write SEO meta.' ); } $post_id = (int) $product->get_id(); $existing = \get_post_meta( $post_id, $meta_key, true ); if ( SeoFieldValueNormalizer::flatten( $existing ) === $sanitized ) { return array( 'saved' => true, 'changed' => false, 'message' => null ); } $result = \update_post_meta( $post_id, $meta_key, \wp_slash( $sanitized ) ); return array( 'saved' => false !== $result, 'changed' => false !== $result, 'message' => false === $result ? 'Failed to write SEO meta.' : null, ); } private function meta_key_for_field( string $field_id ) { if ( ! isset( self::FIELD_TO_KEY[ $field_id ] ) ) { return null; } $key_name = self::FIELD_TO_KEY[ $field_id ]; if ( empty( $this->meta_keys[ $key_name ] ) ) { return null; } return $this->meta_keys[ $key_name ]; } }