WPSEO_Rank

Holder for SEO Rank information.

Defined (1)

The class is defined in the following location(s).

/inc/class-wpseo-rank.php  
  1. class WPSEO_Rank { 
  2.  
  3. const BAD = 'bad'; 
  4. const OK = 'ok'; 
  5. const GOOD = 'good'; 
  6. const NO_FOCUS = 'na'; 
  7. const NO_INDEX = 'noindex'; 
  8.  
  9. /** 
  10. * @var array All possible ranks. 
  11. */ 
  12. protected static $ranks = array( 
  13. self::BAD,  
  14. self::OK,  
  15. self::GOOD,  
  16. self::NO_FOCUS,  
  17. self::NO_INDEX,  
  18. ); 
  19.  
  20. /** 
  21. * Holds the translation from seo score slug to actual score range 
  22. * @var array 
  23. */ 
  24. protected static $ranges = array( 
  25. self::NO_FOCUS => array( 
  26. 'start' => 0,  
  27. 'end' => 0,  
  28. ),  
  29. self::BAD => array( 
  30. 'start' => 1,  
  31. 'end' => 40,  
  32. ),  
  33. self::OK => array( 
  34. 'start' => 41,  
  35. 'end' => 70,  
  36. ),  
  37. self::GOOD => array( 
  38. 'start' => 71,  
  39. 'end' => 100,  
  40. ),  
  41. ); 
  42.  
  43. /** 
  44. * @var int 
  45. */ 
  46. protected $rank; 
  47.  
  48. /** 
  49. * @param int $rank The actual rank. 
  50. */ 
  51. public function __construct( $rank ) { 
  52. if ( ! in_array( $rank, self::$ranks ) ) { 
  53. $rank = self::BAD; 
  54.  
  55. $this->rank = $rank; 
  56.  
  57. /** 
  58. * Returns the saved rank for this rank. 
  59. * @return string 
  60. */ 
  61. public function get_rank() { 
  62. return $this->rank; 
  63.  
  64. /** 
  65. * Returns a CSS class for this rank 
  66. * @return string 
  67. */ 
  68. public function get_css_class() { 
  69. $labels = array( 
  70. self::NO_FOCUS => 'na',  
  71. self::NO_INDEX => 'noindex',  
  72. self::BAD => 'bad',  
  73. self::OK => 'ok',  
  74. self::GOOD => 'good',  
  75. ); 
  76.  
  77. return $labels[ $this->rank ]; 
  78.  
  79. /** 
  80. * Returns a label for this rank 
  81. * @return string 
  82. */ 
  83. public function get_label() { 
  84. $labels = array( 
  85. self::NO_FOCUS => __( 'Not available', 'wordpress-seo' ),  
  86. self::NO_INDEX => __( 'No index', 'wordpress-seo' ),  
  87. self::BAD => __( 'Bad', 'wordpress-seo' ),  
  88. self::OK => __( 'OK', 'wordpress-seo' ),  
  89. self::GOOD => __( 'Good', 'wordpress-seo' ),  
  90. ); 
  91.  
  92. return $labels[ $this->rank ]; 
  93.  
  94. /** 
  95. * Returns a label for use in a drop down 
  96. * @return mixed 
  97. */ 
  98. public function get_drop_down_label() { 
  99. $labels = array( 
  100. self::NO_FOCUS => __( 'SEO: No Focus Keyword', 'wordpress-seo' ),  
  101. self::BAD => __( 'SEO: Bad', 'wordpress-seo' ),  
  102. self::OK => __( 'SEO: OK', 'wordpress-seo' ),  
  103. self::GOOD => __( 'SEO: Good', 'wordpress-seo' ),  
  104. self::NO_INDEX => __( 'SEO: Post Noindexed', 'wordpress-seo' ),  
  105. ); 
  106.  
  107. return $labels[ $this->rank ]; 
  108.  
  109. /** 
  110. * @return int The starting score for this rank. 
  111. */ 
  112. public function get_starting_score() { 
  113. // No index does not have a starting score. 
  114. if ( self::NO_INDEX === $this->rank ) { 
  115. return -1; 
  116.  
  117. return self::$ranges[ $this->rank ]['start']; 
  118.  
  119. /** 
  120. * @return int The end score for this rank. 
  121. */ 
  122. public function get_end_score() { 
  123. // No index does not have an end score. 
  124. if ( self::NO_INDEX === $this->rank ) { 
  125. return -1; 
  126.  
  127. return self::$ranges[ $this->rank ]['end']; 
  128.  
  129. /** 
  130. * Returns a rank for a specific numeric score 
  131. * @param int $score The score to determine a rank for. 
  132. * @return self 
  133. */ 
  134. public static function from_numeric_score( $score ) { 
  135. // Set up the default value. 
  136. $rank = new self( self::BAD ); 
  137.  
  138. foreach ( self::$ranges as $rank_index => $range ) { 
  139. if ( $range['start'] <= $score && $score <= $range['end'] ) { 
  140. $rank = new self( $rank_index ); 
  141. break; 
  142.  
  143. return $rank; 
  144.  
  145. /** 
  146. * Returns a list of all possible SEO Ranks 
  147. * @return WPSEO_Rank[] 
  148. */ 
  149. public static function get_all_ranks() { 
  150. return array_map( array( 'WPSEO_Rank', 'create_rank' ), self::$ranks ); 
  151.  
  152. /** 
  153. * Converts a numeric rank into a WPSEO_Rank object, for use in functional array_* functions 
  154. * @param string $rank SEO Rank. 
  155. * @return WPSEO_Rank 
  156. */ 
  157. private static function create_rank( $rank ) { 
  158. return new self( $rank );