'seo_title',
'_yoast_wpseo_metadesc' => 'seo_description',
'_yoast_wpseo_focuskw' => 'focus_keyword',
);
} elseif ( 'rankmath' === $plugin ) {
$fields = array(
'rank_math_title' => 'seo_title',
'rank_math_description' => 'seo_description',
'rank_math_focus_keyword' => 'focus_keyword',
);
}
foreach ( $fields as $meta_key => $field ) {
if ( empty( $item[ $field ] ) || '' !== (string) get_post_meta( $post_id, $meta_key, true ) ) {
continue;
}
update_post_meta( $post_id, $meta_key, sanitize_text_field( $item[ $field ] ) );
}
}
/**
* The SEO plugin running on this site, if any.
*
* @return string 'yoast', 'rankmath', 'other' or 'none'.
*/
public static function seo_plugin() {
if ( defined( 'WPSEO_VERSION' ) ) {
return 'yoast';
}
if ( class_exists( 'RankMath' ) || defined( 'RANK_MATH_VERSION' ) ) {
return 'rankmath';
}
if ( defined( 'SEOPRESS_VERSION' ) || defined( 'AIOSEO_VERSION' ) || class_exists( 'The_SEO_Framework\\Load' ) ) {
return 'other';
}
return 'none';
}
/**
* The AiM post being viewed, or 0.
*
* @return int
*/
private static function current_aim_post() {
if ( ! is_singular( 'post' ) ) {
return 0;
}
$post_id = get_queried_object_id();
return ( $post_id && get_post_meta( $post_id, '_aim_blog_id', true ) ) ? (int) $post_id : 0;
}
/**
* Print the schema, and the meta description when no SEO plugin prints one.
*/
public static function print_head() {
$post_id = self::current_aim_post();
if ( ! $post_id ) {
return;
}
$plugin = self::seo_plugin();
if ( 'none' === $plugin ) {
$description = (string) get_post_meta( $post_id, self::META_DESCRIPTION, true );
if ( '' !== $description ) {
echo '' . "\n";
}
}
$blocks = json_decode( (string) get_post_meta( $post_id, self::META_SCHEMA, true ), true );
$blocks = is_array( $blocks ) ? $blocks : array();
// An SEO plugin owns the site's organization schema; a copy on every post
// is wrong. Article, FAQ and breadcrumb stay, as on the other path.
if ( 'none' === $plugin ) {
$local = json_decode( (string) get_post_meta( $post_id, self::META_LOCAL, true ), true );
if ( is_array( $local ) ) {
$blocks[] = $local;
}
}
foreach ( $blocks as $block ) {
if ( ! is_array( $block ) ) {
continue;
}
// JSON_HEX_TAG turns < and > into escapes, so nothing in the data can
// close the script element.
echo '' . "\n";
}
}
/**
* Use the SEO title for the browser tab and search result when no SEO
* plugin manages titles.
*
* @param string $title Title another filter already chose, or ''.
* @return string
*/
public static function document_title( $title ) {
if ( '' !== $title || 'none' !== self::seo_plugin() ) {
return $title;
}
$post_id = self::current_aim_post();
if ( ! $post_id ) {
return $title;
}
$seo_title = (string) get_post_meta( $post_id, self::META_TITLE, true );
return '' !== $seo_title ? $seo_title : $title;
}
}
Aim_Be_Seo::init();