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