PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / trunk
Yoast SEO – Advanced SEO with real-time guidance and built-in AI vtrunk
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / inc / class-wpseo-rank.php

class-wpseo-rank.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI trunk, at inc/class-wpseo-rank.php

344 lines 7.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 public const BAD = 'bad';
19
20 /**
21 * Constant used for determining an OK SEO rating.
22 *
23 * @var string
24 */
25 public const OK = 'ok';
26
27 /**
28 * Constant used for determining a good SEO rating.
29 *
30 * @var string
31 */
32 public const GOOD = 'good';
33
34 /**
35 * Constant used for determining that no focus keyphrase is set.
36 *
37 * @var string
38 */
39 public const NO_FOCUS = 'na';
40
41 /**
42 * Constant used for determining that this content is not indexed.
43 *
44 * @var string
45 */
46 public 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 an inclusive language label for this rank.
150 * The only difference with get_label above is that we return "Potentially non-inclusive" for an OK rank.
151 *
152 * @return string
153 */
154 public function get_inclusive_language_label() {
155 if ( $this->rank === self::OK ) {
156 return __( 'Potentially non-inclusive', 'wordpress-seo' );
157 }
158 return $this->get_label();
159 }
160
161 /**
162 * Returns a label for use in a drop down.
163 *
164 * @return mixed
165 */
166 public function get_drop_down_label() {
167 $labels = [
168 self::NO_FOCUS => sprintf(
169 /* translators: %s expands to the SEO score */
170 __( 'SEO: %s', 'wordpress-seo' ),
171 __( 'No Focus Keyphrase', 'wordpress-seo' ),
172 ),
173 self::BAD => sprintf(
174 /* translators: %s expands to the SEO score */
175 __( 'SEO: %s', 'wordpress-seo' ),
176 __( 'Needs improvement', 'wordpress-seo' ),
177 ),
178 self::OK => sprintf(
179 /* translators: %s expands to the SEO score */
180 __( 'SEO: %s', 'wordpress-seo' ),
181 __( 'OK', 'wordpress-seo' ),
182 ),
183 self::GOOD => sprintf(
184 /* translators: %s expands to the SEO score */
185 __( 'SEO: %s', 'wordpress-seo' ),
186 __( 'Good', 'wordpress-seo' ),
187 ),
188 self::NO_INDEX => sprintf(
189 /* translators: %s expands to the SEO score */
190 __( 'SEO: %s', 'wordpress-seo' ),
191 __( 'Post Noindexed', 'wordpress-seo' ),
192 ),
193 ];
194
195 return $labels[ $this->rank ];
196 }
197
198 /**
199 * Gets the drop down labels for the readability score.
200 *
201 * @return string The readability rank label.
202 */
203 public function get_drop_down_readability_labels() {
204 $labels = [
205 self::BAD => sprintf(
206 /* translators: %s expands to the readability score */
207 __( 'Readability: %s', 'wordpress-seo' ),
208 __( 'Needs improvement', 'wordpress-seo' ),
209 ),
210 self::OK => sprintf(
211 /* translators: %s expands to the readability score */
212 __( 'Readability: %s', 'wordpress-seo' ),
213 __( 'OK', 'wordpress-seo' ),
214 ),
215 self::GOOD => sprintf(
216 /* translators: %s expands to the readability score */
217 __( 'Readability: %s', 'wordpress-seo' ),
218 __( 'Good', 'wordpress-seo' ),
219 ),
220 self::NO_FOCUS => sprintf(
221 /* translators: %s expands to the readability score */
222 __( 'Readability: %s', 'wordpress-seo' ),
223 __( 'Not analyzed', 'wordpress-seo' ),
224 ),
225 ];
226
227 return $labels[ $this->rank ];
228 }
229
230 /**
231 * Gets the drop down labels for the inclusive language score.
232 *
233 * @return string The inclusive language rank label.
234 */
235 public function get_drop_down_inclusive_language_labels() {
236 $labels = [
237 self::BAD => sprintf(
238 /* translators: %s expands to the inclusive language score */
239 __( 'Inclusive language: %s', 'wordpress-seo' ),
240 __( 'Needs improvement', 'wordpress-seo' ),
241 ),
242 self::OK => sprintf(
243 /* translators: %s expands to the inclusive language score */
244 __( 'Inclusive language: %s', 'wordpress-seo' ),
245 __( 'Potentially non-inclusive', 'wordpress-seo' ),
246 ),
247 self::GOOD => sprintf(
248 /* translators: %s expands to the inclusive language score */
249 __( 'Inclusive language: %s', 'wordpress-seo' ),
250 __( 'Good', 'wordpress-seo' ),
251 ),
252 ];
253
254 return $labels[ $this->rank ];
255 }
256
257 /**
258 * Get the starting score for this rank.
259 *
260 * @return int The start score.
261 */
262 public function get_starting_score() {
263 // No index does not have a starting score.
264 if ( $this->rank === self::NO_INDEX ) {
265 return -1;
266 }
267
268 return self::$ranges[ $this->rank ]['start'];
269 }
270
271 /**
272 * Get the ending score for this rank.
273 *
274 * @return int The end score.
275 */
276 public function get_end_score() {
277 // No index does not have an end score.
278 if ( $this->rank === self::NO_INDEX ) {
279 return -1;
280 }
281
282 return self::$ranges[ $this->rank ]['end'];
283 }
284
285 /**
286 * Returns a rank for a specific numeric score.
287 *
288 * @param int $score The score to determine a rank for.
289 *
290 * @return self
291 */
292 public static function from_numeric_score( $score ) {
293 // Set up the default value.
294 $rank = new self( self::BAD );
295
296 foreach ( self::$ranges as $rank_index => $range ) {
297 if ( $range['start'] <= $score && $score <= $range['end'] ) {
298 $rank = new self( $rank_index );
299 break;
300 }
301 }
302
303 return $rank;
304 }
305
306 /**
307 * Returns a list of all possible SEO Ranks.
308 *
309 * @return WPSEO_Rank[]
310 */
311 public static function get_all_ranks() {
312 return array_map( [ 'WPSEO_Rank', 'create_rank' ], self::$ranks );
313 }
314
315 /**
316 * Returns a list of all possible Readability Ranks.
317 *
318 * @return WPSEO_Rank[]
319 */
320 public static function get_all_readability_ranks() {
321 return array_map( [ 'WPSEO_Rank', 'create_rank' ], [ self::BAD, self::OK, self::GOOD, self::NO_FOCUS ] );
322 }
323
324 /**
325 * Returns a list of all possible Inclusive Language Ranks.
326 *
327 * @return WPSEO_Rank[]
328 */
329 public static function get_all_inclusive_language_ranks() {
330 return array_map( [ 'WPSEO_Rank', 'create_rank' ], [ self::BAD, self::OK, self::GOOD ] );
331 }
332
333 /**
334 * Converts a numeric rank into a WPSEO_Rank object, for use in functional array_* functions.
335 *
336 * @param string $rank SEO Rank.
337 *
338 * @return WPSEO_Rank
339 */
340 private static function create_rank( $rank ) {
341 return new self( $rank );
342 }
343 }
344