| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Helpers; |
| 4 |
|
| 5 |
use WPSEO_Rank; |
| 6 |
use Yoast\WP\SEO\Models\Indexable; |
| 7 |
use Yoast\WP\SEO\Presenters\Score_Icon_Presenter; |
| 8 |
|
| 9 |
/** |
| 10 |
* A helper object for score icons. |
| 11 |
*/ |
| 12 |
class Score_Icon_Helper { |
| 13 |
|
| 14 |
/** |
| 15 |
* Holds the Robots_Helper. |
| 16 |
* |
| 17 |
* @var Robots_Helper |
| 18 |
*/ |
| 19 |
protected $robots_helper; |
| 20 |
|
| 21 |
/** |
| 22 |
* Constructs a Score_Helper. |
| 23 |
* |
| 24 |
* @param Robots_Helper $robots_helper The Robots_Helper. |
| 25 |
*/ |
| 26 |
public function __construct( Robots_Helper $robots_helper ) { |
| 27 |
$this->robots_helper = $robots_helper; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Creates a Score_Icon_Presenter for the readability analysis. |
| 32 |
* |
| 33 |
* @param int $score The readability analysis score. |
| 34 |
* @param string $extra_class Optional. Any extra class. |
| 35 |
* |
| 36 |
* @return Score_Icon_Presenter The Score_Icon_Presenter. |
| 37 |
*/ |
| 38 |
public function for_readability( $score, $extra_class = '' ) { |
| 39 |
$rank = WPSEO_Rank::from_numeric_score( (int) $score ); |
| 40 |
$class = $rank->get_css_class(); |
| 41 |
if ( $extra_class ) { |
| 42 |
$class .= " $extra_class"; |
| 43 |
} |
| 44 |
|
| 45 |
return new Score_Icon_Presenter( $rank->get_label(), $class ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Creates a Score_Icon_Presenter for the inclusive language analysis. |
| 50 |
* |
| 51 |
* @param int $score The inclusive language analysis score. |
| 52 |
* @param string $extra_class Optional. Any extra class. |
| 53 |
* |
| 54 |
* @return Score_Icon_Presenter The Score_Icon_Presenter. |
| 55 |
*/ |
| 56 |
public function for_inclusive_language( $score, $extra_class = '' ) { |
| 57 |
$rank = WPSEO_Rank::from_numeric_score( (int) $score ); |
| 58 |
$class = $rank->get_css_class(); |
| 59 |
if ( $extra_class ) { |
| 60 |
$class .= " $extra_class"; |
| 61 |
} |
| 62 |
|
| 63 |
return new Score_Icon_Presenter( $rank->get_inclusive_language_label(), $class ); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Creates a Score_Icon_Presenter for the SEO analysis from an indexable. |
| 68 |
* |
| 69 |
* @param Indexable|false $indexable The Indexable. |
| 70 |
* @param string $extra_class Optional. Any extra class. |
| 71 |
* @param string $no_index_title Optional. Override the title when not indexable. |
| 72 |
* |
| 73 |
* @return Score_Icon_Presenter The Score_Icon_Presenter. |
| 74 |
*/ |
| 75 |
public function for_seo( $indexable, $extra_class = '', $no_index_title = '' ) { |
| 76 |
$is_indexable = $indexable && $this->robots_helper->is_indexable( $indexable ); |
| 77 |
|
| 78 |
if ( ! $is_indexable ) { |
| 79 |
$rank = new WPSEO_Rank( WPSEO_Rank::NO_INDEX ); |
| 80 |
$title = empty( $no_index_title ) ? $rank->get_label() : $no_index_title; |
| 81 |
} |
| 82 |
elseif ( empty( $indexable && $indexable->primary_focus_keyword ) ) { |
| 83 |
$rank = new WPSEO_Rank( WPSEO_Rank::BAD ); |
| 84 |
$title = \__( 'Focus keyphrase not set', 'wordpress-seo' ); |
| 85 |
} |
| 86 |
else { |
| 87 |
$rank = WPSEO_Rank::from_numeric_score( ( $indexable ) ? $indexable->primary_focus_keyword_score : 0 ); |
| 88 |
$title = $rank->get_label(); |
| 89 |
} |
| 90 |
|
| 91 |
$class = $rank->get_css_class(); |
| 92 |
if ( $extra_class ) { |
| 93 |
$class .= " $extra_class"; |
| 94 |
} |
| 95 |
|
| 96 |
return new Score_Icon_Presenter( $title, $class ); |
| 97 |
} |
| 98 |
} |
| 99 |
|