*/ namespace RankMath; use RankMath\KB; use RankMath\Post; use RankMath\Helper; use RankMath\Traits\Hooker; use RankMath\Traits\Shortcode; use RankMath\Admin\Admin_Helper; defined( 'ABSPATH' ) || exit; /** * Frontend_SEO_Score class. */ class Frontend_SEO_Score { use Hooker, Shortcode; /** * SEO Score. * * @var array */ private $score = 0; /** * Flag to only add CSS once. * * @var array */ private $css_added = false; /** * Convenience method to output as string. * * @return string */ public function __toString() { return $this->get_output(); } /** * The Constructor * * @codeCoverageIgnore */ public function __construct() { $this->filter( 'the_content', 'insert_score' ); $this->add_shortcode( 'rank_math_seo_score', 'shortcode' ); } /** * Insert score before/after content. * * @param string $content Original post content. * @return string $content New content. */ public function insert_score( $content ) { if ( ! $this->score_enabled() ) { return $content; } $score_location = Helper::get_settings( 'general.frontend_seo_score_position' ); if ( 'custom' === $score_location ) { return $content; } if ( 'top' === $score_location || 'both' === $score_location ) { $content = $this->get_output( [ 'class' => 'before-content' ] ) . $content; } if ( 'bottom' === $score_location || 'both' === $score_location ) { $content = $content . $this->get_output( [ 'class' => 'after-content' ] ); } return $content; } /** * Check if front end SEO score is enabled for this post. * * @return bool */ public function score_enabled() { /* * The loop_start check ensures this only runs after wp_head. */ if ( is_front_page() || ! is_singular() || ! did_action( 'loop_start' ) ) { return false; } $post_type = get_post_type(); $post_id = get_the_ID(); $score_enabled = Helper::get_settings( 'general.frontend_seo_score' ) && Helper::is_score_enabled() && in_array( $post_type, (array) Helper::get_settings( 'general.frontend_seo_score_post_types' ), true ) && get_post_meta( $post_id, 'rank_math_dont_show_seo_score', true ) !== 'on'; return $score_enabled; } /** * Get the SEO score HTML. * * @param array $args Arguments. * @return string */ public function get_output( $args = [] ) { $args = $this->do_filter( 'frontend/seo_score/args', wp_parse_args( $args, [ 'template' => Helper::get_settings( 'general.frontend_seo_score_template' ), 'backlink' => Helper::get_settings( 'general.support_rank_math' ), 'post_id' => '0', 'class' => '', ] ) ); $score = (int) $this->get_score( $args['post_id'] ); $rating = $this->get_rating( $score ); if ( ! $score ) { return $this->do_filter( 'frontend/seo_score/html', '', $args, $score ); } // If template is empty we output $score value directly. $html = $score; $backlink = 'Rank Math SEO'; if ( ! empty( $args['template'] ) ) { ob_start(); ?>
/ 100
add_css(); $html = ob_get_clean(); } return $this->do_filter( 'frontend/seo_score/html', $html, $args, $score ); } /** * Turn numeric score into textual rating. * * @param int $score SEO Score. * @return string */ public function get_rating( $score ) { $hash = [ 'unknown' => 0, 'bad' => 50, 'good' => 80, 'great' => 100, ]; foreach ( $hash as $key => $value ) { if ( $score <= $value ) { return $key; } } return array_keys( $hash )[0]; } /** * Get the SEO score for given post. * * @param int $post_id Post ID. * @return int */ public function get_score( $post_id = 0 ) { global $post; if ( empty( $post_id ) ) { $post_id = $post->ID; } return get_post_meta( $post_id, 'rank_math_seo_score', true ); } /** * Show field check callback. * * @param CMB2_Field $field The current field. * @return boolean */ public static function show_on( $field = [] ) { // Early Bail if is sttic homepage. if ( Admin_Helper::is_home_page() ) { return false; } $post_type = get_post_type(); return Helper::get_settings( 'general.frontend_seo_score' ) && in_array( $post_type, (array) Helper::get_settings( 'general.frontend_seo_score_post_types' ), true ); } /** * Shortcode output. * * @param array $atts Shortcode attributes. */ public function shortcode( $atts ) { if ( ! $this->score_enabled() ) { return ''; } $atts = shortcode_atts( [ 'class' => 'as-shortcode', ], $atts, 'rank-math-seo-score' ); return $this->get_output( $atts ); } /** * Add CSS inline, once. */ public function add_css() { if ( $this->css_added ) { return; } ?> css_added = true; } /** * Settings field default callback. */ public static function post_types_field_default() { $seo_score = Helper::get_settings( 'general.frontend_seo_score' ); $post_types = Helper::get_settings( 'general.frontend_seo_score_post_types' ); if ( 'on' === $seo_score && '' === $post_types ) { return []; } return [ 'post' ]; } }