array( 'title' => '_yoast_wpseo_title', 'description' => '_yoast_wpseo_metadesc', 'focus_keyword' => '_yoast_wpseo_focuskw', ), 'rankmath' => array( 'title' => 'rank_math_title', 'description' => 'rank_math_description', 'focus_keyword' => 'rank_math_focus_keyword', ), ); /** * Plugin SEO actiu: 'yoast' | 'rankmath' | null. */ public static function active_plugin() { if (defined('WPSEO_VERSION') || class_exists('WPSEO_Options')) { return 'yoast'; } if (defined('RANK_MATH_VERSION') || class_exists('RankMath\\Helper')) { return 'rankmath'; } return null; } /** * Llegeix la meta SEO d'un post. * * @return array{plugin: ?string, title: string, description: string, focus_keyword: string} */ public static function read($post_id) { $plugin = self::active_plugin(); $out = array( 'plugin' => $plugin, 'title' => '', 'description' => '', 'focus_keyword' => '', ); if (! $plugin) { return $out; } foreach (self::KEYS[$plugin] as $field => $meta_key) { $out[$field] = (string) get_post_meta($post_id, $meta_key, true); } return $out; } /** * Escriu la meta SEO d'un post (només els camps presents a $seo). * * @param array{title?: string, description?: string, focus_keyword?: string} $seo * @return bool True si s'ha pogut escriure (hi ha un plugin SEO actiu). */ public static function write($post_id, array $seo) { $plugin = self::active_plugin(); if (! $plugin) { return false; } foreach (self::KEYS[$plugin] as $field => $meta_key) { if (array_key_exists($field, $seo)) { update_post_meta($post_id, $meta_key, sanitize_text_field($seo[$field])); } } return true; } }