/** * Filter Yoast post metadata. * * When Yoast builds HTML title and meta description, it looks in tree places: * - Actual post_title, * - Title and Description from Yoast Snippet (fancy metabox for each post), * - Rules (%%title%% %%sep%% %%page%%) in the SEO Settings. * Yoast gets confused when not all languages are filled in consistently * (one language has post_title, another one - only Snippet, others - should work * from the Rules). * We are trying to hook into the `get_post_metadata` and return filtered values * to Yoast, so when it should be empty - it's empty and not * {:xx}Something from another language{:} * * @scope front * @since 1.4.0 (original) * 1.5.5 (restored and rewritten) * * @param null|array $metadata Comes as NULL. Return something to short-circuit. * @param int $post_id Post ID. * @param string $meta_key Empty because the array of all metas is returned. * @param bool $single False in this case. * * @return null|array Return metadata array if we are "in business". */ public static function filter__get_post_metadata($metadata, $post_id, $meta_key, $single) { // Yoast does not pass any `meta_key`, and does not ask for `single`. // Checking it here is faster than going to backtrace directly. if ($meta_key || $single) { return $metadata; } // We only need to deal with these two callers: if (WPGlobus_WP::is_functions_in_backtrace(array(array('WPSEO_Frontend', 'get_content_title'), array('WPSEO_Frontend', 'generate_metadesc')))) { /** * The part of getting meta / updating cache is copied from * @see get_metadata * (except for doing serialize - we believe it's not necessary for Yoast). */ /** @var array $post_meta */ $post_meta = wp_cache_get($post_id, 'post_meta'); if (!$post_meta) { $meta_cache = update_meta_cache('post', array($post_id)); $post_meta = $meta_cache[$post_id]; } /** * Filter both title and meta description to the current language. * * @important Return empty if no current language text exists, * do not use the default. Yoast must receive empty string to realize * that meta is not set for that language. */ foreach (array('_yoast_wpseo_title', '_yoast_wpseo_metadesc') as $_) { if (!empty($post_meta[$_][0])) { $post_meta[$_][0] = WPGlobus_Core::text_filter($post_meta[$_][0], WPGlobus::Config()->language, WPGlobus::RETURN_EMPTY); } } // ... and return it. $metadata = $post_meta; } return $metadata; }