class-wpseo-rank.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Internals
  6. */
  7. /**
  8. * Holder for SEO Rank information.
  9. */
  10. class WPSEO_Rank {
  11. /**
  12. * Constant used for determining a bad SEO rating.
  13. *
  14. * @var string
  15. */
  16. const BAD = 'bad';
  17. /**
  18. * Constant used for determining an OK SEO rating.
  19. *
  20. * @var string
  21. */
  22. const OK = 'ok';
  23. /**
  24. * Constant used for determining a good SEO rating.
  25. *
  26. * @var string
  27. */
  28. const GOOD = 'good';
  29. /**
  30. * Constant used for determining that no focus keyphrase is set.
  31. *
  32. * @var string
  33. */
  34. const NO_FOCUS = 'na';
  35. /**
  36. * Constant used for determining that this content is not indexed.
  37. *
  38. * @var string
  39. */
  40. const NO_INDEX = 'noindex';
  41. /**
  42. * All possible ranks.
  43. *
  44. * @var array
  45. */
  46. protected static $ranks = [
  47. self::BAD,
  48. self::OK,
  49. self::GOOD,
  50. self::NO_FOCUS,
  51. self::NO_INDEX,
  52. ];
  53. /**
  54. * Holds the translation from seo score slug to actual score range.
  55. *
  56. * @var array
  57. */
  58. protected static $ranges = [
  59. self::NO_FOCUS => [
  60. 'start' => 0,
  61. 'end' => 0,
  62. ],
  63. self::BAD => [
  64. 'start' => 1,
  65. 'end' => 40,
  66. ],
  67. self::OK => [
  68. 'start' => 41,
  69. 'end' => 70,
  70. ],
  71. self::GOOD => [
  72. 'start' => 71,
  73. 'end' => 100,
  74. ],
  75. ];
  76. /**
  77. * The current rank.
  78. *
  79. * @var int
  80. */
  81. protected $rank;
  82. /**
  83. * WPSEO_Rank constructor.
  84. *
  85. * @param int $rank The actual rank.
  86. */
  87. public function __construct( $rank ) {
  88. if ( ! in_array( $rank, self::$ranks, true ) ) {
  89. $rank = self::BAD;
  90. }
  91. $this->rank = $rank;
  92. }
  93. /**
  94. * Returns the saved rank for this rank.
  95. *
  96. * @return string
  97. */
  98. public function get_rank() {
  99. return $this->rank;
  100. }
  101. /**
  102. * Returns a CSS class for this rank.
  103. *
  104. * @return string
  105. */
  106. public function get_css_class() {
  107. $labels = [
  108. self::NO_FOCUS => 'na',
  109. self::NO_INDEX => 'noindex',
  110. self::BAD => 'bad',
  111. self::OK => 'ok',
  112. self::GOOD => 'good',
  113. ];
  114. return $labels[ $this->rank ];
  115. }
  116. /**
  117. * Returns a label for this rank.
  118. *
  119. * @return string
  120. */
  121. public function get_label() {
  122. $labels = [
  123. self::NO_FOCUS => __( 'Not available', 'wordpress-seo' ),
  124. self::NO_INDEX => __( 'No index', 'wordpress-seo' ),
  125. self::BAD => __( 'Needs improvement', 'wordpress-seo' ),
  126. self::OK => __( 'OK', 'wordpress-seo' ),
  127. self::GOOD => __( 'Good', 'wordpress-seo' ),
  128. ];
  129. return $labels[ $this->rank ];
  130. }
  131. /**
  132. * Returns a label for use in a drop down.
  133. *
  134. * @return mixed
  135. */
  136. public function get_drop_down_label() {
  137. $labels = [
  138. self::NO_FOCUS => sprintf(
  139. /* translators: %s expands to the SEO score */
  140. __( 'SEO: %s', 'wordpress-seo' ),
  141. __( 'No Focus Keyphrase', 'wordpress-seo' )
  142. ),
  143. self::BAD => sprintf(
  144. /* translators: %s expands to the SEO score */
  145. __( 'SEO: %s', 'wordpress-seo' ),
  146. __( 'Needs improvement', 'wordpress-seo' )
  147. ),
  148. self::OK => sprintf(
  149. /* translators: %s expands to the SEO score */
  150. __( 'SEO: %s', 'wordpress-seo' ),
  151. __( 'OK', 'wordpress-seo' )
  152. ),
  153. self::GOOD => sprintf(
  154. /* translators: %s expands to the SEO score */
  155. __( 'SEO: %s', 'wordpress-seo' ),
  156. __( 'Good', 'wordpress-seo' )
  157. ),
  158. self::NO_INDEX => sprintf(
  159. /* translators: %s expands to the SEO score */
  160. __( 'SEO: %s', 'wordpress-seo' ),
  161. __( 'Post Noindexed', 'wordpress-seo' )
  162. ),
  163. ];
  164. return $labels[ $this->rank ];
  165. }
  166. /**
  167. * Gets the drop down labels for the readability score.
  168. *
  169. * @return string The readability rank label.
  170. */
  171. public function get_drop_down_readability_labels() {
  172. $labels = [
  173. self::BAD => sprintf(
  174. /* translators: %s expands to the readability score */
  175. __( 'Readability: %s', 'wordpress-seo' ),
  176. __( 'Needs improvement', 'wordpress-seo' )
  177. ),
  178. self::OK => sprintf(
  179. /* translators: %s expands to the readability score */
  180. __( 'Readability: %s', 'wordpress-seo' ),
  181. __( 'OK', 'wordpress-seo' )
  182. ),
  183. self::GOOD => sprintf(
  184. /* translators: %s expands to the readability score */
  185. __( 'Readability: %s', 'wordpress-seo' ),
  186. __( 'Good', 'wordpress-seo' )
  187. ),
  188. ];
  189. return $labels[ $this->rank ];
  190. }
  191. /**
  192. * Get the starting score for this rank.
  193. *
  194. * @return int The start score.
  195. */
  196. public function get_starting_score() {
  197. // No index does not have a starting score.
  198. if ( self::NO_INDEX === $this->rank ) {
  199. return -1;
  200. }
  201. return self::$ranges[ $this->rank ]['start'];
  202. }
  203. /**
  204. * Get the ending score for this rank.
  205. *
  206. * @return int The end score.
  207. */
  208. public function get_end_score() {
  209. // No index does not have an end score.
  210. if ( self::NO_INDEX === $this->rank ) {
  211. return -1;
  212. }
  213. return self::$ranges[ $this->rank ]['end'];
  214. }
  215. /**
  216. * Returns a rank for a specific numeric score.
  217. *
  218. * @param int $score The score to determine a rank for.
  219. *
  220. * @return self
  221. */
  222. public static function from_numeric_score( $score ) {
  223. // Set up the default value.
  224. $rank = new self( self::BAD );
  225. foreach ( self::$ranges as $rank_index => $range ) {
  226. if ( $range['start'] <= $score && $score <= $range['end'] ) {
  227. $rank = new self( $rank_index );
  228. break;
  229. }
  230. }
  231. return $rank;
  232. }
  233. /**
  234. * Returns a list of all possible SEO Ranks.
  235. *
  236. * @return WPSEO_Rank[]
  237. */
  238. public static function get_all_ranks() {
  239. return array_map( [ 'WPSEO_Rank', 'create_rank' ], self::$ranks );
  240. }
  241. /**
  242. * Returns a list of all possible Readability Ranks.
  243. *
  244. * @return WPSEO_Rank[]
  245. */
  246. public static function get_all_readability_ranks() {
  247. return array_map( [ 'WPSEO_Rank', 'create_rank' ], [ self::BAD, self::OK, self::GOOD ] );
  248. }
  249. /**
  250. * Converts a numeric rank into a WPSEO_Rank object, for use in functional array_* functions.
  251. *
  252. * @param string $rank SEO Rank.
  253. *
  254. * @return WPSEO_Rank
  255. */
  256. private static function create_rank( $rank ) {
  257. return new self( $rank );
  258. }
  259. }