label */ public static function known() { return array( 'yoast' => 'Yoast SEO', 'rankmath' => 'Rank Math', 'aioseo' => 'All in One SEO', 'seopress' => 'SEOPress', 'tsf' => 'The SEO Framework', 'slimseo' => 'Slim SEO', 'surerank' => 'SureRank', 'squirrly' => 'Squirrly SEO', 'smartcrawl' => 'SmartCrawl', 'visibility' => 'Visibility', ); } /** * Active SEO plugin slug, or ''. * * @return string */ public static function active() { if ( null !== self::$active ) { return self::$active; } $map = array( 'yoast' => defined( 'WPSEO_VERSION' ), 'rankmath' => defined( 'RANK_MATH_VERSION' ) || class_exists( 'RankMath' ), 'aioseo' => defined( 'AIOSEO_VERSION' ) || function_exists( 'aioseo' ), 'seopress' => defined( 'SEOPRESS_VERSION' ), 'tsf' => defined( 'THE_SEO_FRAMEWORK_VERSION' ) || function_exists( 'the_seo_framework' ), 'slimseo' => defined( 'SLIM_SEO_VER' ) || class_exists( 'SlimSEO\Plugin' ), 'surerank' => defined( 'SURERANK_VERSION' ), 'squirrly' => defined( 'SQ_VERSION' ), 'smartcrawl' => defined( 'SMARTCRAWL_VERSION' ) || class_exists( 'Smartcrawl_Loader' ), 'visibility' => defined( 'NATIVE_AEO_PACK_VERSION' ), ); self::$active = ''; foreach ( $map as $slug => $is ) { if ( $is ) { self::$active = $slug; break; } } self::$active = (string) apply_filters( 'aeoora_active_seo_plugin', self::$active ); return self::$active; } /** * Label of active plugin. * * @return string */ public static function active_label() { $known = self::known(); $slug = self::active(); return isset( $known[ $slug ] ) ? $known[ $slug ] : ''; } /** * Can we write meta into the active plugin? * * @return bool */ public static function can_write() { return in_array( self::active(), array( '', 'yoast', 'rankmath', 'aioseo', 'seopress', 'tsf', 'slimseo' ), true ); } /** * Meta description of a post (raw stored value, variables not expanded). * * @param int $post_id Post ID. * @return string */ public static function get_description( $post_id ) { $v = ''; switch ( self::active() ) { case 'yoast': $v = get_post_meta( $post_id, '_yoast_wpseo_metadesc', true ); break; case 'rankmath': $v = get_post_meta( $post_id, 'rank_math_description', true ); break; case 'aioseo': $v = self::aioseo_field( $post_id, 'description' ); break; case 'seopress': $v = get_post_meta( $post_id, '_seopress_titles_desc', true ); break; case 'tsf': $v = get_post_meta( $post_id, '_genesis_description', true ); break; case 'slimseo': $data = get_post_meta( $post_id, 'slim_seo', true ); $v = is_array( $data ) && isset( $data['description'] ) ? $data['description'] : ''; break; } if ( '' === (string) $v ) { $v = get_post_meta( $post_id, '_aeoora_meta_desc', true ); } return self::expand( (string) $v, $post_id ); } /** * SEO title of a post. * * @param int $post_id Post ID. * @return string */ public static function get_title( $post_id ) { $v = ''; switch ( self::active() ) { case 'yoast': $v = get_post_meta( $post_id, '_yoast_wpseo_title', true ); break; case 'rankmath': $v = get_post_meta( $post_id, 'rank_math_title', true ); break; case 'aioseo': $v = self::aioseo_field( $post_id, 'title' ); break; case 'seopress': $v = get_post_meta( $post_id, '_seopress_titles_title', true ); break; case 'tsf': $v = get_post_meta( $post_id, '_genesis_title', true ); break; case 'slimseo': $data = get_post_meta( $post_id, 'slim_seo', true ); $v = is_array( $data ) && isset( $data['title'] ) ? $data['title'] : ''; break; } if ( '' === (string) $v ) { $v = get_post_meta( $post_id, '_aeoora_meta_title', true ); } $v = self::expand( (string) $v, $post_id ); return '' !== $v ? $v : get_the_title( $post_id ); } /** * Focus keyword, if the SEO plugin has one. * * @param int $post_id Post ID. * @return string */ public static function get_focus_keyword( $post_id ) { switch ( self::active() ) { case 'yoast': return (string) get_post_meta( $post_id, '_yoast_wpseo_focuskw', true ); case 'rankmath': $kw = (string) get_post_meta( $post_id, 'rank_math_focus_keyword', true ); return trim( explode( ',', $kw )[0] ); case 'seopress': $kw = (string) get_post_meta( $post_id, '_seopress_analysis_target_kw', true ); return trim( explode( ',', $kw )[0] ); } return (string) get_post_meta( $post_id, '_aeoora_focus_kw', true ); } /** * Write meta description. * * @param int $post_id Post ID. * @param string $value Value. * @return bool */ public static function set_description( $post_id, $value ) { return self::write( $post_id, 'description', sanitize_text_field( $value ) ); } /** * Write SEO title. * * @param int $post_id Post ID. * @param string $value Value. * @return bool */ public static function set_title( $post_id, $value ) { return self::write( $post_id, 'title', sanitize_text_field( $value ) ); } /** * Write a field to the active plugin. * * @param int $post_id Post ID. * @param string $field title|description. * @param string $value Value. * @return bool */ private static function write( $post_id, $field, $value ) { $keys = array( 'yoast' => array( 'title' => '_yoast_wpseo_title', 'description' => '_yoast_wpseo_metadesc', ), 'rankmath' => array( 'title' => 'rank_math_title', 'description' => 'rank_math_description', ), 'seopress' => array( 'title' => '_seopress_titles_title', 'description' => '_seopress_titles_desc', ), 'tsf' => array( 'title' => '_genesis_title', 'description' => '_genesis_description', ), ); $active = self::active(); try { if ( isset( $keys[ $active ] ) ) { update_post_meta( $post_id, $keys[ $active ][ $field ], $value ); if ( 'yoast' === $active ) { self::yoast_reindex( $post_id ); } } elseif ( 'aioseo' === $active ) { self::aioseo_write( $post_id, $field, $value ); } elseif ( 'slimseo' === $active ) { $data = get_post_meta( $post_id, 'slim_seo', true ); $data = is_array( $data ) ? $data : array(); $data[ $field ] = $value; update_post_meta( $post_id, 'slim_seo', $data ); } else { update_post_meta( $post_id, 'title' === $field ? '_aeoora_meta_title' : '_aeoora_meta_desc', $value ); } } catch ( Throwable $e ) { update_post_meta( $post_id, 'title' === $field ? '_aeoora_meta_title' : '_aeoora_meta_desc', $value ); return false; } clean_post_cache( $post_id ); do_action( 'aeoora_meta_written', $post_id, $field, $value, $active ); return true; } /** * Is the post set to noindex in the SEO plugin (or in aeoora)? * * @param int $post_id Post ID. * @return bool */ public static function is_noindex( $post_id ) { $no = false; switch ( self::active() ) { case 'yoast': $no = '1' === (string) get_post_meta( $post_id, '_yoast_wpseo_meta-robots-noindex', true ); break; case 'rankmath': $robots = get_post_meta( $post_id, 'rank_math_robots', true ); $no = is_array( $robots ) && in_array( 'noindex', $robots, true ); break; case 'aioseo': $no = (bool) self::aioseo_field( $post_id, 'robots_noindex' ); break; case 'seopress': $no = 'yes' === get_post_meta( $post_id, '_seopress_robots_index', true ); break; case 'tsf': $no = '1' === (string) get_post_meta( $post_id, '_genesis_noindex', true ); break; case 'slimseo': $data = get_post_meta( $post_id, 'slim_seo', true ); $no = is_array( $data ) && ! empty( $data['noindex'] ); break; } return (bool) apply_filters( 'aeoora_is_noindex', $no, $post_id ); } /** * Does the SEO plugin output its own JSON-LD graph (Article, Organization, Breadcrumbs)? * * @return bool */ public static function outputs_schema() { return in_array( self::active(), array( 'yoast', 'rankmath', 'aioseo', 'seopress', 'tsf', 'slimseo', 'surerank', 'squirrly', 'smartcrawl', 'visibility' ), true ); } /** * Expand common template variables for display. * * @param string $value Value. * @param int $post_id Post ID. * @return string */ private static function expand( $value, $post_id ) { if ( '' === $value || false === strpos( $value, '%' ) && false === strpos( $value, '#' ) && false === strpos( $value, '{' ) ) { return $value; } $post = get_post( $post_id ); $map = array( '%%title%%' => get_the_title( $post_id ), '%%sitename%%' => get_bloginfo( 'name' ), '%%sitedesc%%' => get_bloginfo( 'description' ), '%%sep%%' => '-', '%%page%%' => '', '%%excerpt%%' => $post ? wp_trim_words( $post->post_excerpt ? $post->post_excerpt : $post->post_content, 25, '' ) : '', '%%primary_category%%' => '', '%title%' => get_the_title( $post_id ), '%sitename%' => get_bloginfo( 'name' ), '%sep%' => '-', '%page%' => '', '%excerpt%' => $post ? wp_trim_words( $post->post_excerpt ? $post->post_excerpt : $post->post_content, 25, '' ) : '', '#post_title' => get_the_title( $post_id ), '#site_title' => get_bloginfo( 'name' ), '#separator_sa' => '-', '%%post_title%%' => get_the_title( $post_id ), '%%sitetitle%%' => get_bloginfo( 'name' ), '%%tagline%%' => get_bloginfo( 'description' ), '%%post_excerpt%%' => $post ? wp_trim_words( $post->post_excerpt, 25, '' ) : '', '{{title}}' => get_the_title( $post_id ), '{{site.title}}' => get_bloginfo( 'name' ), ); $value = strtr( $value, $map ); $value = preg_replace( '/%%[a-z_]+%%|%[a-z_]+%|#[a-z_]+/i', '', $value ); return trim( preg_replace( '/\s+/', ' ', (string) $value ) ); } /** * Read a field from AIOSEO. * * @param int $post_id Post ID. * @param string $field Field. * @return string */ private static function aioseo_field( $post_id, $field ) { try { if ( class_exists( '\AIOSEO\Plugin\Common\Models\Post' ) ) { $p = \AIOSEO\Plugin\Common\Models\Post::getPost( $post_id ); if ( $p && isset( $p->$field ) ) { return (string) $p->$field; } } } catch ( Throwable $e ) { unset( $e ); } $meta = array( 'title' => '_aioseo_title', 'description' => '_aioseo_description', 'robots_noindex' => '_aioseo_noindex', ); return isset( $meta[ $field ] ) ? (string) get_post_meta( $post_id, $meta[ $field ], true ) : ''; } /** * Write to AIOSEO. * * @param int $post_id Post ID. * @param string $field Field. * @param string $value Value. */ private static function aioseo_write( $post_id, $field, $value ) { if ( class_exists( '\AIOSEO\Plugin\Common\Models\Post' ) ) { $p = \AIOSEO\Plugin\Common\Models\Post::getPost( $post_id ); if ( $p ) { if ( ! $p->exists() ) { $p->post_id = $post_id; } $p->$field = $value; $p->save(); } } update_post_meta( $post_id, 'title' === $field ? '_aioseo_title' : '_aioseo_description', $value ); } /** * Ask Yoast to rebuild the indexable so the new meta shows immediately. * * @param int $post_id Post ID. */ private static function yoast_reindex( $post_id ) { try { if ( function_exists( 'YoastSEO' ) && class_exists( '\Yoast\WP\SEO\Builders\Indexable_Builder' ) ) { $builder = YoastSEO()->classes->get( \Yoast\WP\SEO\Builders\Indexable_Builder::class ); if ( $builder && method_exists( $builder, 'build_for_id_and_type' ) ) { $builder->build_for_id_and_type( $post_id, 'post' ); } } } catch ( Throwable $e ) { unset( $e ); } } }