PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.0
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.0
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-replace-vars.php

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

1,604 lines 50.2 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 * @since 1.5.4
7 */
8
9 // Avoid direct calls to this file.
10 if ( ! defined( 'WPSEO_VERSION' ) ) {
11 header( 'Status: 403 Forbidden' );
12 header( 'HTTP/1.1 403 Forbidden' );
13 exit();
14 }
15
16 /**
17 * Class: WPSEO_Replace_Vars.
18 *
19 * This class implements the replacing of `%%variable_placeholders%%` with their real value based on the current
20 * requested page/post/cpt/etc in text strings.
21 */
22 class WPSEO_Replace_Vars {
23
24 /**
25 * Default post/page/cpt information.
26 *
27 * @var array
28 */
29 protected $defaults = [
30 'ID' => '',
31 'name' => '',
32 'post_author' => '',
33 'post_content' => '',
34 'post_date' => '',
35 'post_excerpt' => '',
36 'post_modified' => '',
37 'post_title' => '',
38 'taxonomy' => '',
39 'term_id' => '',
40 'term404' => '',
41 ];
42
43 /**
44 * Current post/page/cpt information.
45 *
46 * @var stdClass
47 */
48 protected $args;
49
50 /**
51 * Help texts for use in WPSEO -> Search appearance tabs.
52 *
53 * @var array
54 */
55 protected static $help_texts = [];
56
57 /**
58 * Register of additional variable replacements registered by other plugins/themes.
59 *
60 * @var array
61 */
62 protected static $external_replacements = [];
63
64 /**
65 * Setup the help texts and external replacements as statics so they will be available to all instances.
66 */
67 public static function setup_statics_once() {
68 if ( self::$help_texts === [] ) {
69 self::set_basic_help_texts();
70 self::set_advanced_help_texts();
71 }
72
73 if ( self::$external_replacements === [] ) {
74 /**
75 * Action: 'wpseo_register_extra_replacements' - Allows for registration of additional
76 * variables to replace.
77 */
78 do_action( 'wpseo_register_extra_replacements' );
79 }
80 }
81
82 /**
83 * Register new replacement %%variables%%.
84 * For use by other plugins/themes to register extra variables.
85 *
86 * @see wpseo_register_var_replacement() for a usage example.
87 *
88 * @param string $var_to_replace The name of the variable to replace, i.e. '%%var%%'.
89 * Note: the surrounding %% are optional.
90 * @param mixed $replace_function Function or method to call to retrieve the replacement value for the variable.
91 * Uses the same format as add_filter/add_action function parameter and
92 * should *return* the replacement value. DON'T echo it.
93 * @param string $type Type of variable: 'basic' or 'advanced', defaults to 'advanced'.
94 * @param string $help_text Help text to be added to the help tab for this variable.
95 *
96 * @return bool Whether the replacement function was succesfully registered.
97 */
98 public static function register_replacement( $var_to_replace, $replace_function, $type = 'advanced', $help_text = '' ) {
99 $success = false;
100
101 if ( is_string( $var_to_replace ) && $var_to_replace !== '' ) {
102 $var_to_replace = self::remove_var_delimiter( $var_to_replace );
103
104 if ( preg_match( '`^[A-Z0-9_-]+$`i', $var_to_replace ) === false ) {
105 trigger_error( esc_html__( 'A replacement variable can only contain alphanumeric characters, an underscore or a dash. Try renaming your variable.', 'wordpress-seo' ), E_USER_WARNING );
106 }
107 elseif ( strpos( $var_to_replace, 'cf_' ) === 0 || strpos( $var_to_replace, 'ct_' ) === 0 ) {
108 trigger_error( esc_html__( 'A replacement variable can not start with "%%cf_" or "%%ct_" as these are reserved for the WPSEO standard variable variables for custom fields and custom taxonomies. Try making your variable name unique.', 'wordpress-seo' ), E_USER_WARNING );
109 }
110 elseif ( ! method_exists( __CLASS__, 'retrieve_' . $var_to_replace ) ) {
111 if ( $var_to_replace !== '' && ! isset( self::$external_replacements[ $var_to_replace ] ) ) {
112 self::$external_replacements[ $var_to_replace ] = $replace_function;
113 $replacement_variable = new WPSEO_Replacement_Variable( $var_to_replace, $var_to_replace, $help_text );
114 self::register_help_text( $type, $replacement_variable );
115 $success = true;
116 }
117 else {
118 trigger_error( esc_html__( 'A replacement variable with the same name has already been registered. Try making your variable name unique.', 'wordpress-seo' ), E_USER_WARNING );
119 }
120 }
121 else {
122 trigger_error( esc_html__( 'You cannot overrule a WPSEO standard variable replacement by registering a variable with the same name. Use the "wpseo_replacements" filter instead to adjust the replacement value.', 'wordpress-seo' ), E_USER_WARNING );
123 }
124 }
125
126 return $success;
127 }
128
129 /**
130 * Replace `%%variable_placeholders%%` with their real value based on the current requested page/post/cpt/etc.
131 *
132 * @param string $text The string to replace the variables in.
133 * @param array $args The object some of the replacement values might come from,
134 * could be a post, taxonomy or term.
135 * @param array $omit Variables that should not be replaced by this function.
136 *
137 * @return string
138 */
139 public function replace( $text, $args, $omit = [] ) {
140
141 $text = wp_strip_all_tags( $text );
142
143 // Let's see if we can bail super early.
144 if ( strpos( $text, '%%' ) === false ) {
145 return YoastSEO()->helpers->string->standardize_whitespace( $text );
146 }
147
148 $args = (array) $args;
149 if ( isset( $args['post_content'] ) && ! empty( $args['post_content'] ) ) {
150 $args['post_content'] = YoastSEO()->helpers->string->strip_shortcode( $args['post_content'] );
151 }
152 if ( isset( $args['post_excerpt'] ) && ! empty( $args['post_excerpt'] ) ) {
153 $args['post_excerpt'] = YoastSEO()->helpers->string->strip_shortcode( $args['post_excerpt'] );
154 }
155 $this->args = (object) wp_parse_args( $args, $this->defaults );
156
157 // Clean $omit array.
158 if ( is_array( $omit ) && $omit !== [] ) {
159 $omit = array_map( [ __CLASS__, 'remove_var_delimiter' ], $omit );
160 }
161
162 $replacements = [];
163 if ( preg_match_all( '`%%([^%]+(%%single)?)%%?`iu', $text, $matches ) ) {
164 $replacements = $this->set_up_replacements( $matches, $omit );
165 }
166
167 /**
168 * Filter: 'wpseo_replacements' - Allow customization of the replacements before they are applied.
169 *
170 * @api array $replacements The replacements.
171 *
172 * @param array $args The object some of the replacement values might come from,
173 * could be a post, taxonomy or term.
174 */
175 $replacements = apply_filters( 'wpseo_replacements', $replacements, $this->args );
176
177 // Do the actual replacements.
178 if ( is_array( $replacements ) && $replacements !== [] ) {
179 $text = str_replace(
180 array_keys( $replacements ),
181 // Make sure to exclude replacement values that are arrays e.g. coming from a custom field serialized value.
182 array_filter( array_values( $replacements ), 'is_scalar' ),
183 $text
184 );
185 }
186
187 /**
188 * Filter: 'wpseo_replacements_final' - Allow overruling of whether or not to remove placeholders
189 * which didn't yield a replacement.
190 *
191 * @example <code>add_filter( 'wpseo_replacements_final', '__return_false' );</code>
192 *
193 * @api bool $final
194 */
195 if ( apply_filters( 'wpseo_replacements_final', true ) === true && ( isset( $matches[1] ) && is_array( $matches[1] ) ) ) {
196 // Remove non-replaced variables.
197 $remove = array_diff( $matches[1], $omit ); // Make sure the $omit variables do not get removed.
198 $remove = array_map( [ __CLASS__, 'add_var_delimiter' ], $remove );
199 $text = str_replace( $remove, '', $text );
200 }
201
202 // Undouble separators which have nothing between them, i.e. where a non-replaced variable was removed.
203 if ( isset( $replacements['%%sep%%'] ) && ( is_string( $replacements['%%sep%%'] ) && $replacements['%%sep%%'] !== '' ) ) {
204 $q_sep = preg_quote( $replacements['%%sep%%'], '`' );
205 $text = preg_replace( '`' . $q_sep . '(?:\s*' . $q_sep . ')*`u', $replacements['%%sep%%'], $text );
206 }
207
208 // Remove superfluous whitespace.
209 $text = YoastSEO()->helpers->string->standardize_whitespace( $text );
210
211 return $text;
212 }
213
214 /**
215 * Register a new replacement variable if it has not been registered already.
216 *
217 * @param string $var_to_replace The name of the variable to replace, i.e. '%%var%%'.
218 * Note: the surrounding %% are optional.
219 * @param mixed $replace_function Function or method to call to retrieve the replacement value for the variable.
220 * Uses the same format as add_filter/add_action function parameter and
221 * should *return* the replacement value. DON'T echo it.
222 * @param string $type Type of variable: 'basic' or 'advanced', defaults to 'advanced'.
223 * @param string $help_text Help text to be added to the help tab for this variable.
224 *
225 * @return bool `true` if the replace var has been registered, `false` if not.
226 */
227 public function safe_register_replacement( $var_to_replace, $replace_function, $type = 'advanced', $help_text = '' ) {
228 if ( ! $this->has_been_registered( $var_to_replace ) ) {
229 return self::register_replacement( $var_to_replace, $replace_function, $type, $help_text );
230 }
231 return false;
232 }
233
234 /**
235 * Checks whether the given replacement variable has already been registered or not.
236 *
237 * @param string $replacement_variable The replacement variable to check, including the variable delimiter (e.g. `%%var%%`).
238 *
239 * @return bool `true` if the replacement variable has already been registered.
240 */
241 public function has_been_registered( $replacement_variable ) {
242 $replacement_variable = self::remove_var_delimiter( $replacement_variable );
243
244 return isset( self::$external_replacements[ $replacement_variable ] );
245 }
246
247 /**
248 * Returns the list of hidden replace vars.
249 *
250 * E.g. the replace vars that should work, but are not advertised.
251 *
252 * @return string[] The list of hidden replace vars.
253 */
254 public function get_hidden_replace_vars() {
255 return [
256 'currentdate',
257 'currentyear',
258 'currentmonth',
259 'currentday',
260 'post_year',
261 'post_month',
262 'post_day',
263 'author_first_name',
264 'author_last_name',
265 'permalink',
266 'post_content',
267 'category_title',
268 ];
269 }
270
271 /**
272 * Retrieve the replacements for the variables found.
273 *
274 * @param array $matches Variables found in the original string - regex result.
275 * @param array $omit Variables that should not be replaced by this function.
276 *
277 * @return array Retrieved replacements - this might be a smaller array as some variables
278 * may not yield a replacement in certain contexts.
279 */
280 private function set_up_replacements( $matches, $omit ) {
281
282 $replacements = [];
283
284 // @todo Figure out a way to deal with external functions starting with cf_/ct_.
285 foreach ( $matches[1] as $k => $var ) {
286
287 // Don't set up replacements which should be omitted.
288 if ( in_array( $var, $omit, true ) ) {
289 continue;
290 }
291
292 // Deal with variable variable names first.
293 if ( strpos( $var, 'cf_' ) === 0 ) {
294 $replacement = $this->retrieve_cf_custom_field_name( $var );
295 }
296 elseif ( strpos( $var, 'ct_desc_' ) === 0 ) {
297 $replacement = $this->retrieve_ct_desc_custom_tax_name( $var );
298 }
299 elseif ( strpos( $var, 'ct_' ) === 0 ) {
300 $single = ( isset( $matches[2][ $k ] ) && $matches[2][ $k ] !== '' );
301 $replacement = $this->retrieve_ct_custom_tax_name( $var, $single );
302 }
303 // Deal with non-variable variable names.
304 elseif ( method_exists( $this, 'retrieve_' . $var ) ) {
305 $method_name = 'retrieve_' . $var;
306 $replacement = $this->$method_name();
307 }
308 // Deal with externally defined variable names.
309 elseif ( isset( self::$external_replacements[ $var ] ) && ! is_null( self::$external_replacements[ $var ] ) ) {
310 $replacement = call_user_func( self::$external_replacements[ $var ], $var, $this->args );
311 }
312
313 // Replacement retrievals can return null if no replacement can be determined, root those outs.
314 if ( isset( $replacement ) ) {
315 $var = self::add_var_delimiter( $var );
316 $replacements[ $var ] = $replacement;
317 }
318 unset( $replacement, $single, $method_name );
319 }
320
321 return $replacements;
322 }
323
324 /* *********************** BASIC VARIABLES ************************** */
325
326 /**
327 * Retrieve the post/cpt categories (comma separated) for use as replacement string.
328 *
329 * @return string|null
330 */
331 private function retrieve_category() {
332 $replacement = null;
333
334 if ( ! empty( $this->args->ID ) ) {
335 $cat = $this->get_terms( $this->args->ID, 'category' );
336 if ( $cat !== '' ) {
337 return $cat;
338 }
339 }
340
341 if ( isset( $this->args->cat_name ) && ! empty( $this->args->cat_name ) ) {
342 $replacement = $this->args->cat_name;
343 }
344
345 return $replacement;
346 }
347
348 /**
349 * Retrieve the category description for use as replacement string.
350 *
351 * @return string|null
352 */
353 private function retrieve_category_description() {
354 return $this->retrieve_term_description();
355 }
356
357 /**
358 * Retrieve the date of the post/page/cpt for use as replacement string.
359 *
360 * @return string|null
361 */
362 private function retrieve_date() {
363 $replacement = null;
364
365 if ( $this->args->post_date !== '' ) {
366 // Returns a string.
367 $replacement = YoastSEO()->helpers->date->format_translated( $this->args->post_date, get_option( 'date_format' ) );
368 }
369 else {
370 if ( get_query_var( 'day' ) && get_query_var( 'day' ) !== '' ) {
371 // Returns a string.
372 $replacement = get_the_date();
373 }
374 else {
375 if ( single_month_title( ' ', false ) && single_month_title( ' ', false ) !== '' ) {
376 // Returns a string.
377 $replacement = single_month_title( ' ', false );
378 }
379 elseif ( get_query_var( 'year' ) !== '' ) {
380 // Returns an integer, let's cast to string.
381 $replacement = (string) get_query_var( 'year' );
382 }
383 }
384 }
385
386 return $replacement;
387 }
388
389 /**
390 * Retrieve the post/page/cpt excerpt for use as replacement string.
391 * The excerpt will be auto-generated if it does not exist.
392 *
393 * @return string|null
394 */
395 private function retrieve_excerpt() {
396 $replacement = null;
397 $locale = \get_locale();
398 $limit = ( $locale === 'ja' ) ? 80 : 156;
399
400 // The check `post_password_required` is because excerpt must be hidden for a post with a password.
401 if ( ! empty( $this->args->ID ) && ! post_password_required( $this->args->ID ) ) {
402 if ( $this->args->post_excerpt !== '' ) {
403 $replacement = wp_strip_all_tags( $this->args->post_excerpt );
404 }
405 elseif ( $this->args->post_content !== '' ) {
406 $content = strip_shortcodes( $this->args->post_content );
407 $content = wp_strip_all_tags( $content );
408
409 if ( strlen( utf8_decode( $content ) ) <= $limit ) {
410 return $content;
411 }
412
413 $replacement = wp_html_excerpt( $content, $limit );
414
415 // Check if the description has space and trim the auto-generated string to a word boundary.
416 if ( strrpos( $replacement, ' ' ) ) {
417 $replacement = substr( $replacement, 0, strrpos( $replacement, ' ' ) );
418 }
419 }
420 }
421
422 return $replacement;
423 }
424
425 /**
426 * Retrieve the post/page/cpt excerpt for use as replacement string (without auto-generation).
427 *
428 * @return string|null
429 */
430 private function retrieve_excerpt_only() {
431 $replacement = null;
432
433 // The check `post_password_required` is because excerpt must be hidden for a post with a password.
434 if ( ! empty( $this->args->ID ) && $this->args->post_excerpt !== '' && ! post_password_required( $this->args->ID ) ) {
435 $replacement = wp_strip_all_tags( $this->args->post_excerpt );
436 }
437
438 return $replacement;
439 }
440
441 /**
442 * Retrieve the title of the parent page of the current page/cpt for use as replacement string.
443 * Only applicable for hierarchical post types.
444 *
445 * @todo Check: shouldn't this use $this->args as well ?
446 *
447 * @return string|null
448 */
449 private function retrieve_parent_title() {
450 $replacement = null;
451
452 if ( ! empty( $this->args->ID ) ) {
453 $parent_id = wp_get_post_parent_id( $this->args->ID );
454 if ( $parent_id ) {
455 $replacement = get_the_title( $parent_id );
456 }
457 }
458
459 return $replacement;
460 }
461
462 /**
463 * Retrieve the current search phrase for use as replacement string.
464 *
465 * @return string|null
466 */
467 private function retrieve_searchphrase() {
468 $replacement = null;
469
470 $search = get_query_var( 's' );
471 if ( $search !== '' ) {
472 $replacement = esc_html( $search );
473 }
474
475 return $replacement;
476 }
477
478 /**
479 * Retrieve the separator for use as replacement string.
480 *
481 * @return string Retrieves the title separator.
482 */
483 private function retrieve_sep() {
484 return YoastSEO()->helpers->options->get_title_separator();
485 }
486
487 /**
488 * Retrieve the site's tag line / description for use as replacement string.
489 *
490 * The `$replacement` variable is static because it doesn't change depending
491 * on the context. See https://github.com/Yoast/wordpress-seo/pull/1172#issuecomment-46019482.
492 *
493 * @return string|null
494 */
495 private function retrieve_sitedesc() {
496 static $replacement;
497
498 if ( ! isset( $replacement ) ) {
499 $description = wp_strip_all_tags( get_bloginfo( 'description' ) );
500 if ( $description !== '' ) {
501 $replacement = $description;
502 }
503 }
504
505 return $replacement;
506 }
507
508 /**
509 * Retrieve the site's name for use as replacement string.
510 *
511 * The `$replacement` variable is static because it doesn't change depending
512 * on the context. See https://github.com/Yoast/wordpress-seo/pull/1172#issuecomment-46019482.
513 *
514 * @return string|null
515 */
516 private function retrieve_sitename() {
517 static $replacement;
518
519 if ( ! isset( $replacement ) ) {
520 $sitename = YoastSEO()->helpers->site->get_site_name();
521 if ( $sitename !== '' ) {
522 $replacement = $sitename;
523 }
524 }
525
526 return $replacement;
527 }
528
529 /**
530 * Retrieve the current tag/tags for use as replacement string.
531 *
532 * @return string|null
533 */
534 private function retrieve_tag() {
535 $replacement = null;
536
537 if ( ! empty( $this->args->ID ) ) {
538 $tags = $this->get_terms( $this->args->ID, 'post_tag' );
539 if ( $tags !== '' ) {
540 $replacement = $tags;
541 }
542 }
543
544 return $replacement;
545 }
546
547 /**
548 * Retrieve the tag description for use as replacement string.
549 *
550 * @return string|null
551 */
552 private function retrieve_tag_description() {
553 return $this->retrieve_term_description();
554 }
555
556 /**
557 * Retrieve the term description for use as replacement string.
558 *
559 * @return string|null
560 */
561 private function retrieve_term_description() {
562 $replacement = null;
563
564 if ( ! empty( $this->args->term_id ) && ! empty( $this->args->taxonomy ) ) {
565 $term_desc = get_term_field( 'description', $this->args->term_id, $this->args->taxonomy );
566 if ( $term_desc !== '' ) {
567 $replacement = wp_strip_all_tags( $term_desc );
568 }
569 }
570
571 return $replacement;
572 }
573
574 /**
575 * Retrieve the term name for use as replacement string.
576 *
577 * @return string|null
578 */
579 private function retrieve_term_title() {
580 $replacement = null;
581
582 if ( ! empty( $this->args->taxonomy ) && ! empty( $this->args->name ) ) {
583 $replacement = $this->args->name;
584 }
585
586 return $replacement;
587 }
588
589 /**
590 * Retrieve the title of the post/page/cpt for use as replacement string.
591 *
592 * @return string|null
593 */
594 private function retrieve_title() {
595 $replacement = null;
596
597 if ( is_string( $this->args->post_title ) && $this->args->post_title !== '' ) {
598 $replacement = $this->args->post_title;
599 }
600
601 return $replacement;
602 }
603
604 /**
605 * Retrieve primary category for use as replacement string.
606 *
607 * @return bool|int|null
608 */
609 private function retrieve_primary_category() {
610 $primary_category = null;
611
612 if ( ! empty( $this->args->ID ) ) {
613 $wpseo_primary_category = new WPSEO_Primary_Term( 'category', $this->args->ID );
614
615 $term_id = $wpseo_primary_category->get_primary_term();
616 $term = get_term( $term_id );
617
618 if ( ! is_wp_error( $term ) && ! empty( $term ) ) {
619 $primary_category = $term->name;
620 }
621 }
622
623 return $primary_category;
624 }
625
626 /**
627 * Retrieve the string generated by get_the_archive_title().
628 *
629 * @return string|null
630 */
631 private function retrieve_archive_title() {
632 return get_the_archive_title();
633 }
634
635 /* *********************** ADVANCED VARIABLES ************************** */
636
637 /**
638 * Determine the page numbering of the current post/page/cpt.
639 *
640 * @param string $request Either 'nr'|'max' - whether to return the page number or the max number of pages.
641 *
642 * @return int|null
643 */
644 private function determine_pagenumbering( $request = 'nr' ) {
645 global $wp_query, $post;
646 $max_num_pages = null;
647 $page_number = null;
648
649 $max_num_pages = 1;
650
651 if ( ! is_singular() ) {
652 $page_number = get_query_var( 'paged' );
653 if ( $page_number === 0 || $page_number === '' ) {
654 $page_number = 1;
655 }
656
657 if ( ! empty( $wp_query->max_num_pages ) ) {
658 $max_num_pages = $wp_query->max_num_pages;
659 }
660 }
661 else {
662 $page_number = get_query_var( 'page' );
663 if ( $page_number === 0 || $page_number === '' ) {
664 $page_number = 1;
665 }
666
667 if ( isset( $post->post_content ) ) {
668 $max_num_pages = ( substr_count( $post->post_content, '<!--nextpage-->' ) + 1 );
669 }
670 }
671
672 $return = null;
673
674 switch ( $request ) {
675 case 'nr':
676 $return = $page_number;
677 break;
678 case 'max':
679 $return = $max_num_pages;
680 break;
681 }
682
683 return $return;
684 }
685
686 /**
687 * Determine the post type names for the current post/page/cpt.
688 *
689 * @param string $request Either 'single'|'plural' - whether to return the single or plural form.
690 *
691 * @return string|null
692 */
693 private function determine_pt_names( $request = 'single' ) {
694 global $wp_query;
695 $pt_single = null;
696 $pt_plural = null;
697 $post_type = '';
698
699 if ( isset( $wp_query->query_vars['post_type'] ) && ( ( is_string( $wp_query->query_vars['post_type'] ) && $wp_query->query_vars['post_type'] !== '' ) || ( is_array( $wp_query->query_vars['post_type'] ) && $wp_query->query_vars['post_type'] !== [] ) ) ) {
700 $post_type = $wp_query->query_vars['post_type'];
701 }
702 elseif ( isset( $this->args->post_type ) && ( is_string( $this->args->post_type ) && $this->args->post_type !== '' ) ) {
703 $post_type = $this->args->post_type;
704 }
705 else {
706 // Make it work in preview mode.
707 $post = $wp_query->get_queried_object();
708 if ( $post instanceof WP_Post ) {
709 $post_type = $post->post_type;
710 }
711 }
712
713 if ( is_array( $post_type ) ) {
714 $post_type = reset( $post_type );
715 }
716
717 if ( $post_type !== '' ) {
718 $pt = get_post_type_object( $post_type );
719 $pt_single = $pt->name;
720 $pt_plural = $pt->name;
721 if ( isset( $pt->labels->singular_name ) ) {
722 $pt_single = $pt->labels->singular_name;
723 }
724 if ( isset( $pt->labels->name ) ) {
725 $pt_plural = $pt->labels->name;
726 }
727 }
728
729 $return = null;
730
731 switch ( $request ) {
732 case 'single':
733 $return = $pt_single;
734 break;
735 case 'plural':
736 $return = $pt_plural;
737 break;
738 }
739
740 return $return;
741 }
742
743 /**
744 * Retrieve the attachment caption for use as replacement string.
745 *
746 * @return string|null
747 */
748 private function retrieve_caption() {
749 return $this->retrieve_excerpt_only();
750 }
751
752 /**
753 * Retrieve a post/page/cpt's custom field value for use as replacement string.
754 *
755 * @param string $var_to_replace The complete variable to replace which includes the name of
756 * the custom field which value is to be retrieved.
757 *
758 * @return string|null
759 */
760 private function retrieve_cf_custom_field_name( $var_to_replace ) {
761 $replacement = null;
762
763 if ( is_string( $var_to_replace ) && $var_to_replace !== '' ) {
764 $field = substr( $var_to_replace, 3 );
765 if ( ! empty( $this->args->ID ) ) {
766 // Post meta can be arrays and in this case we need to exclude them.
767 $name = get_post_meta( $this->args->ID, $field, true );
768 if ( $name !== '' && ! is_array( $name ) ) {
769 $replacement = $name;
770 }
771 }
772 elseif ( ! empty( $this->args->term_id ) ) {
773 $name = get_term_meta( $this->args->term_id, $field, true );
774 if ( $name !== '' ) {
775 $replacement = $name;
776 }
777 }
778 }
779
780 return $replacement;
781 }
782
783 /**
784 * Retrieve a post/page/cpt's custom taxonomies for use as replacement string.
785 *
786 * @param string $var_to_replace The complete variable to replace which includes the name of
787 * the custom taxonomy which value(s) is to be retrieved.
788 * @param bool $single Whether to retrieve only the first or all values for the taxonomy.
789 *
790 * @return string|null
791 */
792 private function retrieve_ct_custom_tax_name( $var_to_replace, $single = false ) {
793 $replacement = null;
794
795 if ( ( is_string( $var_to_replace ) && $var_to_replace !== '' ) && ! empty( $this->args->ID ) ) {
796 $tax = substr( $var_to_replace, 3 );
797 $name = $this->get_terms( $this->args->ID, $tax, $single );
798 if ( $name !== '' ) {
799 $replacement = $name;
800 }
801 }
802
803 return $replacement;
804 }
805
806 /**
807 * Retrieve a post/page/cpt's custom taxonomies description for use as replacement string.
808 *
809 * @param string $var_to_replace The complete variable to replace which includes the name of
810 * the custom taxonomy which description is to be retrieved.
811 *
812 * @return string|null
813 */
814 private function retrieve_ct_desc_custom_tax_name( $var_to_replace ) {
815 $replacement = null;
816
817 if ( is_string( $var_to_replace ) && $var_to_replace !== '' ) {
818 $tax = substr( $var_to_replace, 8 );
819 if ( ! empty( $this->args->ID ) ) {
820 $terms = get_the_terms( $this->args->ID, $tax );
821 if ( is_array( $terms ) && $terms !== [] ) {
822 $term = current( $terms );
823 $term_desc = get_term_field( 'description', $term->term_id, $tax );
824 if ( $term_desc !== '' ) {
825 $replacement = wp_strip_all_tags( $term_desc );
826 }
827 }
828 }
829 }
830
831 return $replacement;
832 }
833
834 /**
835 * Retrieve the current date for use as replacement string.
836 *
837 * The `$replacement` variable is static because it doesn't change depending
838 * on the context. See https://github.com/Yoast/wordpress-seo/pull/1172#issuecomment-46019482.
839 *
840 * @return string The formatted current date.
841 */
842 private function retrieve_currentdate() {
843 static $replacement;
844
845 if ( ! isset( $replacement ) ) {
846 $replacement = date_i18n( get_option( 'date_format' ) );
847 }
848
849 return $replacement;
850 }
851
852 /**
853 * Retrieve the current day for use as replacement string.
854 *
855 * The `$replacement` variable is static because it doesn't change depending
856 * on the context. See https://github.com/Yoast/wordpress-seo/pull/1172#issuecomment-46019482.
857 *
858 * @return string The current day.
859 */
860 private function retrieve_currentday() {
861 static $replacement;
862
863 if ( ! isset( $replacement ) ) {
864 $replacement = date_i18n( 'j' );
865 }
866
867 return $replacement;
868 }
869
870 /**
871 * Retrieve the current month for use as replacement string.
872 *
873 * The `$replacement` variable is static because it doesn't change depending
874 * on the context. See https://github.com/Yoast/wordpress-seo/pull/1172#issuecomment-46019482.
875 *
876 * @return string The current month.
877 */
878 private function retrieve_currentmonth() {
879 static $replacement;
880
881 if ( ! isset( $replacement ) ) {
882 $replacement = date_i18n( 'F' );
883 }
884
885 return $replacement;
886 }
887
888 /**
889 * Retrieve the current time for use as replacement string.
890 *
891 * The `$replacement` variable is static because it doesn't change depending
892 * on the context. See https://github.com/Yoast/wordpress-seo/pull/1172#issuecomment-46019482.
893 *
894 * @return string The formatted current time.
895 */
896 private function retrieve_currenttime() {
897 static $replacement;
898
899 if ( ! isset( $replacement ) ) {
900 $replacement = date_i18n( get_option( 'time_format' ) );
901 }
902
903 return $replacement;
904 }
905
906 /**
907 * Retrieve the current year for use as replacement string.
908 *
909 * The `$replacement` variable is static because it doesn't change depending
910 * on the context. See https://github.com/Yoast/wordpress-seo/pull/1172#issuecomment-46019482.
911 *
912 * @return string The current year.
913 */
914 private function retrieve_currentyear() {
915 static $replacement;
916
917 if ( ! isset( $replacement ) ) {
918 $replacement = date_i18n( 'Y' );
919 }
920
921 return $replacement;
922 }
923
924 /**
925 * Retrieve the post/page/cpt's focus keyword for use as replacement string.
926 *
927 * @return string|null
928 */
929 private function retrieve_focuskw() {
930 // Retrieve focuskw from a Post.
931 if ( ! empty( $this->args->ID ) ) {
932 $focus_kw = WPSEO_Meta::get_value( 'focuskw', $this->args->ID );
933 if ( $focus_kw !== '' ) {
934 return $focus_kw;
935 }
936
937 return null;
938 }
939
940 // Retrieve focuskw from a Term.
941 if ( ! empty( $this->args->term_id ) ) {
942 $focus_kw = WPSEO_Taxonomy_Meta::get_term_meta( $this->args->term_id, $this->args->taxonomy, 'focuskw' );
943 if ( $focus_kw !== '' ) {
944 return $focus_kw;
945 }
946 }
947
948 return null;
949 }
950
951 /**
952 * Retrieve the post/page/cpt ID for use as replacement string.
953 *
954 * @return string|null
955 */
956 private function retrieve_id() {
957 $replacement = null;
958
959 if ( ! empty( $this->args->ID ) ) {
960 // The post/page/cpt ID is an integer, let's cast to string.
961 $replacement = (string) $this->args->ID;
962 }
963
964 return $replacement;
965 }
966
967 /**
968 * Retrieve the post/page/cpt modified time for use as replacement string.
969 *
970 * @return string|null
971 */
972 private function retrieve_modified() {
973 $replacement = null;
974
975 if ( ! empty( $this->args->post_modified ) ) {
976 $replacement = YoastSEO()->helpers->date->format_translated( $this->args->post_modified, get_option( 'date_format' ) );
977 }
978
979 return $replacement;
980 }
981
982 /**
983 * Retrieve the post/page/cpt author's "nice name" for use as replacement string.
984 *
985 * @return string|null
986 */
987 private function retrieve_name() {
988 $replacement = null;
989
990 $user_id = (int) $this->retrieve_userid();
991 $name = get_the_author_meta( 'display_name', $user_id );
992 if ( $name !== '' ) {
993 $replacement = $name;
994 }
995
996 return $replacement;
997 }
998
999 /**
1000 * Retrieve the post/page/cpt author's users description for use as a replacement string.
1001 *
1002 * @return string|null
1003 */
1004 private function retrieve_user_description() {
1005 $replacement = null;
1006
1007 $user_id = (int) $this->retrieve_userid();
1008 $description = get_the_author_meta( 'description', $user_id );
1009 if ( $description !== '' ) {
1010 $replacement = $description;
1011 }
1012
1013 return $replacement;
1014 }
1015
1016 /**
1017 * Retrieve the current page number with context (i.e. 'page 2 of 4') for use as replacement string.
1018 *
1019 * @return string
1020 */
1021 private function retrieve_page() {
1022 $replacement = null;
1023
1024 $max = $this->determine_pagenumbering( 'max' );
1025 $nr = $this->determine_pagenumbering( 'nr' );
1026 $sep = $this->retrieve_sep();
1027
1028 if ( $max > 1 && $nr > 1 ) {
1029 /* translators: 1: current page number, 2: total number of pages. */
1030 $replacement = sprintf( $sep . ' ' . __( 'Page %1$d of %2$d', 'wordpress-seo' ), $nr, $max );
1031 }
1032
1033 return $replacement;
1034 }
1035
1036 /**
1037 * Retrieve the current page number for use as replacement string.
1038 *
1039 * @return string|null
1040 */
1041 private function retrieve_pagenumber() {
1042 $replacement = null;
1043
1044 $nr = $this->determine_pagenumbering( 'nr' );
1045 if ( isset( $nr ) && $nr > 0 ) {
1046 $replacement = (string) $nr;
1047 }
1048
1049 return $replacement;
1050 }
1051
1052 /**
1053 * Retrieve the current page total for use as replacement string.
1054 *
1055 * @return string|null
1056 */
1057 private function retrieve_pagetotal() {
1058 $replacement = null;
1059
1060 $max = $this->determine_pagenumbering( 'max' );
1061 if ( isset( $max ) && $max > 0 ) {
1062 $replacement = (string) $max;
1063 }
1064
1065 return $replacement;
1066 }
1067
1068 /**
1069 * Retrieve the post type plural label for use as replacement string.
1070 *
1071 * @return string|null
1072 */
1073 private function retrieve_pt_plural() {
1074 $replacement = null;
1075
1076 $name = $this->determine_pt_names( 'plural' );
1077 if ( isset( $name ) && $name !== '' ) {
1078 $replacement = $name;
1079 }
1080
1081 return $replacement;
1082 }
1083
1084 /**
1085 * Retrieve the post type single label for use as replacement string.
1086 *
1087 * @return string|null
1088 */
1089 private function retrieve_pt_single() {
1090 $replacement = null;
1091
1092 $name = $this->determine_pt_names( 'single' );
1093 if ( isset( $name ) && $name !== '' ) {
1094 $replacement = $name;
1095 }
1096
1097 return $replacement;
1098 }
1099
1100 /**
1101 * Retrieve the slug which caused the 404 for use as replacement string.
1102 *
1103 * @return string|null
1104 */
1105 private function retrieve_term404() {
1106 $replacement = null;
1107
1108 if ( $this->args->term404 !== '' ) {
1109 $replacement = sanitize_text_field( str_replace( '-', ' ', $this->args->term404 ) );
1110 }
1111 else {
1112 $error_request = get_query_var( 'pagename' );
1113 if ( $error_request !== '' ) {
1114 $replacement = sanitize_text_field( str_replace( '-', ' ', $error_request ) );
1115 }
1116 else {
1117 $error_request = get_query_var( 'name' );
1118 if ( $error_request !== '' ) {
1119 $replacement = sanitize_text_field( str_replace( '-', ' ', $error_request ) );
1120 }
1121 }
1122 }
1123
1124 return $replacement;
1125 }
1126
1127 /**
1128 * Retrieve the post/page/cpt author's user id for use as replacement string.
1129 *
1130 * @return string
1131 */
1132 private function retrieve_userid() {
1133 // The user ID is an integer, let's cast to string.
1134 $replacement = ! empty( $this->args->post_author ) ? (string) $this->args->post_author : (string) get_query_var( 'author' );
1135
1136 return $replacement;
1137 }
1138
1139 /**
1140 * Retrieve the post/page/cpt's published year for use as replacement string.
1141 *
1142 * @return string|null
1143 */
1144 private function retrieve_post_year() {
1145 if ( empty( $this->args->ID ) ) {
1146 return null;
1147 }
1148
1149 return \get_the_date( 'Y', $this->args->ID );
1150 }
1151
1152 /**
1153 * Retrieve the post/page/cpt's published month for use as replacement string.
1154 *
1155 * @return string|null
1156 */
1157 private function retrieve_post_month() {
1158 if ( empty( $this->args->ID ) ) {
1159 return null;
1160 }
1161
1162 return \get_the_date( 'F', $this->args->ID );
1163 }
1164
1165 /**
1166 * Retrieve the post/page/cpt's published day for use as replacement string.
1167 *
1168 * @return string|null
1169 */
1170 private function retrieve_post_day() {
1171 if ( empty( $this->args->ID ) ) {
1172 return null;
1173 }
1174
1175 return \get_the_date( 'd', $this->args->ID );
1176 }
1177
1178 /**
1179 * Retrieve the post/page/cpt author's first name for use as replacement string.
1180 *
1181 * @return string|null
1182 */
1183 private function retrieve_author_first_name() {
1184 $replacement = null;
1185
1186 $user_id = (int) $this->retrieve_userid();
1187 $name = get_the_author_meta( 'first_name', $user_id );
1188 if ( $name !== '' ) {
1189 $replacement = $name;
1190 }
1191
1192 return $replacement;
1193 }
1194
1195 /**
1196 * Retrieve the post/page/cpt author's last name for use as replacement string.
1197 *
1198 * @return string|null
1199 */
1200 private function retrieve_author_last_name() {
1201 $replacement = null;
1202
1203 $user_id = (int) $this->retrieve_userid();
1204 $name = get_the_author_meta( 'last_name', $user_id );
1205 if ( $name !== '' ) {
1206 $replacement = $name;
1207 }
1208
1209 return $replacement;
1210 }
1211
1212 /**
1213 * Retrieve the post/page/cpt permalink for use as replacement string.
1214 *
1215 * @return string|null
1216 */
1217 private function retrieve_permalink() {
1218 if ( empty( $this->args->ID ) ) {
1219 return null;
1220 }
1221
1222 return \get_permalink( $this->args->ID );
1223 }
1224
1225 /**
1226 * Retrieve the post/page/cpt content for use as replacement string.
1227 *
1228 * @return string|null
1229 */
1230 private function retrieve_post_content() {
1231 $replacement = null;
1232
1233 // The check `post_password_required` is because content must be hidden for a post with a password.
1234 if ( ! empty( $this->args->ID ) && $this->args->post_content !== '' && ! post_password_required( $this->args->ID ) ) {
1235 $content = strip_shortcodes( $this->args->post_content );
1236 $replacement = wp_strip_all_tags( $content );
1237 }
1238
1239 return $replacement;
1240 }
1241
1242 /**
1243 * Retrieve the current or first category title. To be used for import data from AIOSEO.
1244 * The code derives from AIOSEO's way of dealing with that var, so we can ensure 100% seamless transition.
1245 *
1246 * @return string|null
1247 */
1248 private function retrieve_category_title() {
1249 if ( empty( $this->args ) || empty( $this->args->ID ) ) {
1250 return null;
1251 }
1252 $post_id = $this->args->ID;
1253
1254 $post = get_post( $post_id );
1255 $taxonomies = get_object_taxonomies( $post, 'objects' );
1256
1257 foreach ( $taxonomies as $taxonomy_slug => $taxonomy ) {
1258 if ( ! $taxonomy->hierarchical ) {
1259 continue;
1260 }
1261 $post_terms = get_the_terms( $post_id, $taxonomy_slug );
1262 if ( is_array( $post_terms ) && count( $post_terms ) > 0 ) {
1263 // AiOSEO takes the name of whatever the first hierarchical taxonomy is.
1264 $term = $post_terms[0];
1265 if ( $term ) {
1266 return $term->name;
1267 }
1268 }
1269 }
1270
1271 return null;
1272 }
1273
1274 /* *********************** HELP TEXT RELATED ************************** */
1275
1276 /**
1277 * Set the help text for a user/plugin/theme defined extra variable.
1278 *
1279 * @param string $type Type of variable: 'basic' or 'advanced'.
1280 * @param WPSEO_Replacement_Variable $replacement_variable The replacement variable to register.
1281 */
1282 private static function register_help_text( $type, WPSEO_Replacement_Variable $replacement_variable ) {
1283 $identifier = $replacement_variable->get_variable();
1284
1285 if ( ( is_string( $type ) && in_array( $type, [ 'basic', 'advanced' ], true ) )
1286 && ( $identifier !== '' && ! isset( self::$help_texts[ $type ][ $identifier ] ) )
1287 ) {
1288 self::$help_texts[ $type ][ $identifier ] = $replacement_variable;
1289 }
1290 }
1291
1292 /**
1293 * Generates a list of replacement variables based on the help texts.
1294 *
1295 * @return array List of replace vars.
1296 */
1297 public function get_replacement_variables_list() {
1298 self::setup_statics_once();
1299
1300 $replacement_variables = array_merge(
1301 $this->get_replacement_variables(),
1302 WPSEO_Custom_Fields::get_custom_fields(),
1303 WPSEO_Custom_Taxonomies::get_custom_taxonomies()
1304 );
1305
1306 return array_map( [ $this, 'format_replacement_variable' ], $replacement_variables );
1307 }
1308
1309 /**
1310 * Creates a merged associative array of both the basic and advanced help texts.
1311 *
1312 * @return array Array with the replacement variables.
1313 */
1314 private function get_replacement_variables() {
1315 $help_texts = array_merge( self::$help_texts['basic'], self::$help_texts['advanced'] );
1316
1317 return array_filter( array_keys( $help_texts ), [ $this, 'is_not_prefixed' ] );
1318 }
1319
1320 /**
1321 * Checks whether the replacement variable contains a `ct_` or `cf_` prefix, because they follow different logic.
1322 *
1323 * @param string $replacement_variable The replacement variable.
1324 *
1325 * @return bool True when the replacement variable is not prefixed.
1326 */
1327 private function is_not_prefixed( $replacement_variable ) {
1328 $prefixes = [ 'cf_', 'ct_' ];
1329 $prefix = $this->get_prefix( $replacement_variable );
1330
1331 return ! in_array( $prefix, $prefixes, true );
1332 }
1333
1334 /**
1335 * Strip the prefix from a replacement variable name.
1336 *
1337 * @param string $replacement_variable The replacement variable.
1338 *
1339 * @return string The replacement variable name without the prefix.
1340 */
1341 private function strip_prefix( $replacement_variable ) {
1342 return substr( $replacement_variable, 3 );
1343 }
1344
1345 /**
1346 * Gets the prefix from a replacement variable name.
1347 *
1348 * @param string $replacement_variable The replacement variable.
1349 *
1350 * @return string The prefix of the replacement variable.
1351 */
1352 private function get_prefix( $replacement_variable ) {
1353 return substr( $replacement_variable, 0, 3 );
1354 }
1355
1356 /**
1357 * Strips 'desc_' if present, and appends ' description' at the end.
1358 *
1359 * @param string $label The replacement variable.
1360 *
1361 * @return string The altered replacement variable name.
1362 */
1363 private function handle_description( $label ) {
1364 if ( strpos( $label, 'desc_' ) === 0 ) {
1365 return substr( $label, 5 ) . ' description';
1366 }
1367
1368 return $label;
1369 }
1370
1371 /**
1372 * Creates a label for prefixed replacement variables that matches the format in the editors.
1373 *
1374 * @param string $replacement_variable The replacement variable.
1375 *
1376 * @return string The replacement variable label.
1377 */
1378 private function get_label( $replacement_variable ) {
1379 $prefix = $this->get_prefix( $replacement_variable );
1380 if ( $prefix === 'cf_' ) {
1381 return $this->strip_prefix( $replacement_variable ) . ' (custom field)';
1382 }
1383
1384 if ( $prefix === 'ct_' ) {
1385 $label = $this->strip_prefix( $replacement_variable );
1386 $label = $this->handle_description( $label );
1387 return ucfirst( $label . ' (custom taxonomy)' );
1388 }
1389
1390 if ( $prefix === 'pt_' ) {
1391 if ( $replacement_variable === 'pt_single' ) {
1392 return 'Post type (singular)';
1393 }
1394
1395 return 'Post type (plural)';
1396 }
1397
1398 return '';
1399 }
1400
1401 /**
1402 * Formats the replacement variables.
1403 *
1404 * @param string $replacement_variable The replacement variable to format.
1405 *
1406 * @return array The formatted replacement variable.
1407 */
1408 private function format_replacement_variable( $replacement_variable ) {
1409 return [
1410 'name' => $replacement_variable,
1411 'value' => '',
1412 'label' => $this->get_label( $replacement_variable ),
1413 ];
1414 }
1415
1416 /**
1417 * Set/translate the help texts for the WPSEO standard basic variables.
1418 */
1419 private static function set_basic_help_texts() {
1420 /* translators: %s: wp_title() function. */
1421 $separator_description = __( 'The separator defined in your theme\'s %s tag.', 'wordpress-seo' );
1422 $separator_description = sprintf(
1423 $separator_description,
1424 // '<code>wp_title()</code>'
1425 'wp_title()'
1426 );
1427
1428 $replacement_variables = [
1429 new WPSEO_Replacement_Variable( 'date', __( 'Date', 'wordpress-seo' ), __( 'Replaced with the date of the post/page', 'wordpress-seo' ) ),
1430 new WPSEO_Replacement_Variable( 'title', __( 'Title', 'wordpress-seo' ), __( 'Replaced with the title of the post/page', 'wordpress-seo' ) ),
1431 new WPSEO_Replacement_Variable( 'parent_title', __( 'Parent title', 'wordpress-seo' ), __( 'Replaced with the title of the parent page of the current page', 'wordpress-seo' ) ),
1432 new WPSEO_Replacement_Variable( 'archive_title', __( 'Archive title', 'wordpress-seo' ), __( 'Replaced with the normal title for an archive generated by WordPress', 'wordpress-seo' ) ),
1433 new WPSEO_Replacement_Variable( 'sitename', __( 'Site title', 'wordpress-seo' ), __( 'The site\'s name', 'wordpress-seo' ) ),
1434 new WPSEO_Replacement_Variable( 'sitedesc', __( 'Tagline', 'wordpress-seo' ), __( 'The site\'s tagline', 'wordpress-seo' ) ),
1435 new WPSEO_Replacement_Variable( 'excerpt', __( 'Excerpt', 'wordpress-seo' ), __( 'Replaced with the post/page excerpt (or auto-generated if it does not exist)', 'wordpress-seo' ) ),
1436 new WPSEO_Replacement_Variable( 'excerpt_only', __( 'Excerpt only', 'wordpress-seo' ), __( 'Replaced with the post/page excerpt (without auto-generation)', 'wordpress-seo' ) ),
1437 new WPSEO_Replacement_Variable( 'tag', __( 'Tag', 'wordpress-seo' ), __( 'Replaced with the current tag/tags', 'wordpress-seo' ) ),
1438 new WPSEO_Replacement_Variable( 'category', __( 'Category', 'wordpress-seo' ), __( 'Replaced with the post categories (comma separated)', 'wordpress-seo' ) ),
1439 new WPSEO_Replacement_Variable( 'primary_category', __( 'Primary category', 'wordpress-seo' ), __( 'Replaced with the primary category of the post/page', 'wordpress-seo' ) ),
1440 new WPSEO_Replacement_Variable( 'category_description', __( 'Category description', 'wordpress-seo' ), __( 'Replaced with the category description', 'wordpress-seo' ) ),
1441 new WPSEO_Replacement_Variable( 'tag_description', __( 'Tag description', 'wordpress-seo' ), __( 'Replaced with the tag description', 'wordpress-seo' ) ),
1442 new WPSEO_Replacement_Variable( 'term_description', __( 'Term description', 'wordpress-seo' ), __( 'Replaced with the term description', 'wordpress-seo' ) ),
1443 new WPSEO_Replacement_Variable( 'term_title', __( 'Term title', 'wordpress-seo' ), __( 'Replaced with the term name', 'wordpress-seo' ) ),
1444 new WPSEO_Replacement_Variable( 'searchphrase', __( 'Search phrase', 'wordpress-seo' ), __( 'Replaced with the current search phrase', 'wordpress-seo' ) ),
1445 new WPSEO_Replacement_Variable( 'term_hierarchy', __( 'Term hierarchy', 'wordpress-seo' ), __( 'Replaced with the term ancestors hierarchy', 'wordpress-seo' ) ),
1446 new WPSEO_Replacement_Variable( 'sep', __( 'Separator', 'wordpress-seo' ), $separator_description ),
1447 new WPSEO_Replacement_Variable( 'currentdate', __( 'Current date', 'wordpress-seo' ), __( 'Replaced with the current date', 'wordpress-seo' ) ),
1448 new WPSEO_Replacement_Variable( 'currentyear', __( 'Current year', 'wordpress-seo' ), __( 'Replaced with the current year', 'wordpress-seo' ) ),
1449 new WPSEO_Replacement_Variable( 'currentmonth', __( 'Current month', 'wordpress-seo' ), __( 'Replaced with the current month', 'wordpress-seo' ) ),
1450 new WPSEO_Replacement_Variable( 'currentday', __( 'Current day', 'wordpress-seo' ), __( 'Replaced with the current day', 'wordpress-seo' ) ),
1451 new WPSEO_Replacement_Variable( 'post_year', __( 'Post year', 'wordpress-seo' ), __( 'Replaced with the year the post was published', 'wordpress-seo' ) ),
1452 new WPSEO_Replacement_Variable( 'post_month', __( 'Post month', 'wordpress-seo' ), __( 'Replaced with the month the post was published', 'wordpress-seo' ) ),
1453 new WPSEO_Replacement_Variable( 'post_day', __( 'Post day', 'wordpress-seo' ), __( 'Replaced with the day the post was published', 'wordpress-seo' ) ),
1454 new WPSEO_Replacement_Variable( 'author_first_name', __( 'Author first name', 'wordpress-seo' ), __( 'Replaced with the first name of the author', 'wordpress-seo' ) ),
1455 new WPSEO_Replacement_Variable( 'author_last_name', __( 'Author last name', 'wordpress-seo' ), __( 'Replaced with the last name of the author', 'wordpress-seo' ) ),
1456 new WPSEO_Replacement_Variable( 'permalink', __( 'Permalink', 'wordpress-seo' ), __( 'Replaced with the permalink', 'wordpress-seo' ) ),
1457 new WPSEO_Replacement_Variable( 'post_content', __( 'Post Content', 'wordpress-seo' ), __( 'Replaced with the post content', 'wordpress-seo' ) ),
1458 new WPSEO_Replacement_Variable( 'category_title', __( 'Category Title', 'wordpress-seo' ), __( 'Current or first category title', 'wordpress-seo' ) ),
1459 ];
1460
1461 foreach ( $replacement_variables as $replacement_variable ) {
1462 self::register_help_text( 'basic', $replacement_variable );
1463 }
1464 }
1465
1466 /**
1467 * Set/translate the help texts for the WPSEO standard advanced variables.
1468 */
1469 private static function set_advanced_help_texts() {
1470 $replacement_variables = [
1471 new WPSEO_Replacement_Variable( 'pt_single', __( 'Post type (singular)', 'wordpress-seo' ), __( 'Replaced with the content type single label', 'wordpress-seo' ) ),
1472 new WPSEO_Replacement_Variable( 'pt_plural', __( 'Post type (plural)', 'wordpress-seo' ), __( 'Replaced with the content type plural label', 'wordpress-seo' ) ),
1473 new WPSEO_Replacement_Variable( 'modified', __( 'Modified', 'wordpress-seo' ), __( 'Replaced with the post/page modified time', 'wordpress-seo' ) ),
1474 new WPSEO_Replacement_Variable( 'id', __( 'ID', 'wordpress-seo' ), __( 'Replaced with the post/page ID', 'wordpress-seo' ) ),
1475 new WPSEO_Replacement_Variable( 'name', __( 'Name', 'wordpress-seo' ), __( 'Replaced with the post/page author\'s \'nicename\'', 'wordpress-seo' ) ),
1476 new WPSEO_Replacement_Variable( 'user_description', __( 'User description', 'wordpress-seo' ), __( 'Replaced with the post/page author\'s \'Biographical Info\'', 'wordpress-seo' ) ),
1477 new WPSEO_Replacement_Variable( 'page', __( 'Page number', 'wordpress-seo' ), __( 'Replaced with the current page number with context (i.e. page 2 of 4)', 'wordpress-seo' ) ),
1478 new WPSEO_Replacement_Variable( 'pagetotal', __( 'Pagetotal', 'wordpress-seo' ), __( 'Replaced with the current page total', 'wordpress-seo' ) ),
1479 new WPSEO_Replacement_Variable( 'pagenumber', __( 'Pagenumber', 'wordpress-seo' ), __( 'Replaced with the current page number', 'wordpress-seo' ) ),
1480 new WPSEO_Replacement_Variable( 'caption', __( 'Caption', 'wordpress-seo' ), __( 'Attachment caption', 'wordpress-seo' ) ),
1481 new WPSEO_Replacement_Variable( 'focuskw', __( 'Focus keyword', 'wordpress-seo' ), __( 'Replaced with the posts focus keyphrase', 'wordpress-seo' ) ),
1482 new WPSEO_Replacement_Variable( 'term404', __( 'Term404', 'wordpress-seo' ), __( 'Replaced with the slug which caused the 404', 'wordpress-seo' ) ),
1483 new WPSEO_Replacement_Variable( 'cf_<custom-field-name>', '<custom-field-name> ' . __( '(custom field)', 'wordpress-seo' ), __( 'Replaced with a posts custom field value', 'wordpress-seo' ) ),
1484 new WPSEO_Replacement_Variable( 'ct_<custom-tax-name>', '<custom-tax-name> ' . __( '(custom taxonomy)', 'wordpress-seo' ), __( 'Replaced with a posts custom taxonomies, comma separated.', 'wordpress-seo' ) ),
1485 new WPSEO_Replacement_Variable( 'ct_desc_<custom-tax-name>', '<custom-tax-name> ' . __( 'description (custom taxonomy)', 'wordpress-seo' ), __( 'Replaced with a custom taxonomies description', 'wordpress-seo' ) ),
1486 ];
1487
1488 foreach ( $replacement_variables as $replacement_variable ) {
1489 self::register_help_text( 'advanced', $replacement_variable );
1490 }
1491 }
1492
1493 /* *********************** GENERAL HELPER METHODS ************************** */
1494
1495 /**
1496 * Remove the '%%' delimiters from a variable string.
1497 *
1498 * @param string $text Variable string to be cleaned.
1499 *
1500 * @return string
1501 */
1502 private static function remove_var_delimiter( $text ) {
1503 return trim( $text, '%' );
1504 }
1505
1506 /**
1507 * Add the '%%' delimiters to a variable string.
1508 *
1509 * @param string $text Variable string to be delimited.
1510 *
1511 * @return string
1512 */
1513 private static function add_var_delimiter( $text ) {
1514 return '%%' . $text . '%%';
1515 }
1516
1517 /**
1518 * Retrieve a post's terms, comma delimited.
1519 *
1520 * @param int $id ID of the post to get the terms for.
1521 * @param string $taxonomy The taxonomy to get the terms for this post from.
1522 * @param bool $return_single If true, return the first term.
1523 *
1524 * @return string Either a single term or a comma delimited string of terms.
1525 */
1526 public function get_terms( $id, $taxonomy, $return_single = false ) {
1527 $output = '';
1528
1529 // If we're on a specific tag, category or taxonomy page, use that.
1530 if ( ! empty( $this->args->term_id ) ) {
1531 $output = $this->args->name;
1532 }
1533 elseif ( ! empty( $id ) && ! empty( $taxonomy ) ) {
1534 $terms = get_the_terms( $id, $taxonomy );
1535 if ( is_array( $terms ) && $terms !== [] ) {
1536 foreach ( $terms as $term ) {
1537 if ( $return_single ) {
1538 $output = $term->name;
1539 break;
1540 }
1541 else {
1542 $output .= $term->name . ', ';
1543 }
1544 }
1545 $output = rtrim( trim( $output ), ',' );
1546 }
1547 }
1548 unset( $terms, $term );
1549
1550 /**
1551 * Allows filtering of the terms list used to replace %%category%%, %%tag%%
1552 * and %%ct_<custom-tax-name>%% variables.
1553 *
1554 * @api string $output Comma-delimited string containing the terms.
1555 * @api string $taxonomy The taxonomy of the terms.
1556 */
1557 return apply_filters( 'wpseo_terms', $output, $taxonomy );
1558 }
1559
1560 /**
1561 * Gets a taxonomy term hierarchy including the term to get the parents for.
1562 *
1563 * @return string
1564 */
1565 private function get_term_hierarchy() {
1566 if ( ! is_taxonomy_hierarchical( $this->args->taxonomy ) ) {
1567 return '';
1568 }
1569
1570 $separator = ' ' . $this->retrieve_sep() . ' ';
1571
1572 $args = [
1573 'format' => 'name',
1574 'separator' => $separator,
1575 'link' => false,
1576 'inclusive' => true,
1577 ];
1578
1579 return rtrim(
1580 get_term_parents_list( $this->args->term_id, $this->args->taxonomy, $args ),
1581 $separator
1582 );
1583 }
1584
1585 /**
1586 * Retrieves the term ancestors hierarchy.
1587 *
1588 * @return string|null The term ancestors hierarchy.
1589 */
1590 private function retrieve_term_hierarchy() {
1591 $replacement = null;
1592
1593 if ( ! empty( $this->args->term_id ) && ! empty( $this->args->taxonomy ) ) {
1594 $hierarchy = $this->get_term_hierarchy();
1595
1596 if ( $hierarchy !== '' ) {
1597 $replacement = esc_html( $hierarchy );
1598 }
1599 }
1600
1601 return $replacement;
1602 }
1603 }
1604