PageRenderTime 58ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-content/plugins/wordpress-seo/inc/class-wpseo-replace-vars.php

https://gitlab.com/VTTE/sitios-vtte
PHP | 1399 lines | 732 code | 201 blank | 466 comment | 165 complexity | d210a9b90275c2dd2413163660a2c2b6 MD5 | raw file
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Internals
  6. * @since 1.5.4
  7. */
  8. // Avoid direct calls to this file.
  9. if ( ! defined( 'WPSEO_VERSION' ) ) {
  10. header( 'Status: 403 Forbidden' );
  11. header( 'HTTP/1.1 403 Forbidden' );
  12. exit();
  13. }
  14. /**
  15. * Class: WPSEO_Replace_Vars.
  16. *
  17. * This class implements the replacing of `%%variable_placeholders%%` with their real value based on the current
  18. * requested page/post/cpt/etc in text strings.
  19. */
  20. class WPSEO_Replace_Vars {
  21. /**
  22. * Default post/page/cpt information.
  23. *
  24. * @var array
  25. */
  26. protected $defaults = [
  27. 'ID' => '',
  28. 'name' => '',
  29. 'post_author' => '',
  30. 'post_content' => '',
  31. 'post_date' => '',
  32. 'post_excerpt' => '',
  33. 'post_modified' => '',
  34. 'post_title' => '',
  35. 'taxonomy' => '',
  36. 'term_id' => '',
  37. 'term404' => '',
  38. ];
  39. /**
  40. * Current post/page/cpt information.
  41. *
  42. * @var object
  43. */
  44. protected $args;
  45. /**
  46. * The date helper.
  47. *
  48. * @var WPSEO_Date_Helper
  49. */
  50. protected $date;
  51. /**
  52. * Help texts for use in WPSEO -> Search appearance tabs.
  53. *
  54. * @var array
  55. */
  56. protected static $help_texts = [];
  57. /**
  58. * Register of additional variable replacements registered by other plugins/themes.
  59. *
  60. * @var array
  61. */
  62. protected static $external_replacements = [];
  63. /**
  64. * Constructor.
  65. */
  66. public function __construct() {
  67. $this->date = new WPSEO_Date_Helper();
  68. }
  69. /**
  70. * Setup the help texts and external replacements as statics so they will be available to all instances.
  71. */
  72. public static function setup_statics_once() {
  73. if ( self::$help_texts === [] ) {
  74. self::set_basic_help_texts();
  75. self::set_advanced_help_texts();
  76. }
  77. if ( self::$external_replacements === [] ) {
  78. /**
  79. * Action: 'wpseo_register_extra_replacements' - Allows for registration of additional
  80. * variables to replace.
  81. */
  82. do_action( 'wpseo_register_extra_replacements' );
  83. }
  84. }
  85. /**
  86. * Register new replacement %%variables%%.
  87. * For use by other plugins/themes to register extra variables.
  88. *
  89. * @see wpseo_register_var_replacement() for a usage example.
  90. *
  91. * @param string $var The name of the variable to replace, i.e. '%%var%%'.
  92. * Note: the surrounding %% are optional.
  93. * @param mixed $replace_function Function or method to call to retrieve the replacement value for the variable.
  94. * Uses the same format as add_filter/add_action function parameter and
  95. * should *return* the replacement value. DON'T echo it.
  96. * @param string $type Type of variable: 'basic' or 'advanced', defaults to 'advanced'.
  97. * @param string $help_text Help text to be added to the help tab for this variable.
  98. *
  99. * @return bool Whether the replacement function was succesfully registered.
  100. */
  101. public static function register_replacement( $var, $replace_function, $type = 'advanced', $help_text = '' ) {
  102. $success = false;
  103. if ( is_string( $var ) && $var !== '' ) {
  104. $var = self::remove_var_delimiter( $var );
  105. if ( preg_match( '`^[A-Z0-9_-]+$`i', $var ) === false ) {
  106. 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 );
  107. }
  108. elseif ( strpos( $var, 'cf_' ) === 0 || strpos( $var, 'ct_' ) === 0 ) {
  109. 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 );
  110. }
  111. elseif ( ! method_exists( __CLASS__, 'retrieve_' . $var ) ) {
  112. if ( $var !== '' && ! isset( self::$external_replacements[ $var ] ) ) {
  113. self::$external_replacements[ $var ] = $replace_function;
  114. $replacement_variable = new WPSEO_Replacement_Variable( $var, $var, $help_text );
  115. self::register_help_text( $type, $replacement_variable );
  116. $success = true;
  117. }
  118. else {
  119. 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 );
  120. }
  121. }
  122. else {
  123. 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 );
  124. }
  125. }
  126. return $success;
  127. }
  128. /**
  129. * Replace `%%variable_placeholders%%` with their real value based on the current requested page/post/cpt/etc.
  130. *
  131. * @param string $string The string to replace the variables in.
  132. * @param array $args The object some of the replacement values might come from,
  133. * could be a post, taxonomy or term.
  134. * @param array $omit Variables that should not be replaced by this function.
  135. *
  136. * @return string
  137. */
  138. public function replace( $string, $args, $omit = [] ) {
  139. $string = wp_strip_all_tags( $string );
  140. // Let's see if we can bail super early.
  141. if ( strpos( $string, '%%' ) === false ) {
  142. return WPSEO_Utils::standardize_whitespace( $string );
  143. }
  144. $args = (array) $args;
  145. if ( isset( $args['post_content'] ) && ! empty( $args['post_content'] ) ) {
  146. $args['post_content'] = WPSEO_Utils::strip_shortcode( $args['post_content'] );
  147. }
  148. if ( isset( $args['post_excerpt'] ) && ! empty( $args['post_excerpt'] ) ) {
  149. $args['post_excerpt'] = WPSEO_Utils::strip_shortcode( $args['post_excerpt'] );
  150. }
  151. $this->args = (object) wp_parse_args( $args, $this->defaults );
  152. // Clean $omit array.
  153. if ( is_array( $omit ) && $omit !== [] ) {
  154. $omit = array_map( [ __CLASS__, 'remove_var_delimiter' ], $omit );
  155. }
  156. $replacements = [];
  157. if ( preg_match_all( '`%%([^%]+(%%single)?)%%?`iu', $string, $matches ) ) {
  158. $replacements = $this->set_up_replacements( $matches, $omit );
  159. }
  160. /**
  161. * Filter: 'wpseo_replacements' - Allow customization of the replacements before they are applied.
  162. *
  163. * @api array $replacements The replacements.
  164. *
  165. * @param array $args The object some of the replacement values might come from,
  166. * could be a post, taxonomy or term.
  167. */
  168. $replacements = apply_filters( 'wpseo_replacements', $replacements, $this->args );
  169. // Do the actual replacements.
  170. if ( is_array( $replacements ) && $replacements !== [] ) {
  171. $string = str_replace( array_keys( $replacements ), array_values( $replacements ), $string );
  172. }
  173. /**
  174. * Filter: 'wpseo_replacements_final' - Allow overruling of whether or not to remove placeholders
  175. * which didn't yield a replacement.
  176. *
  177. * @example <code>add_filter( 'wpseo_replacements_final', '__return_false' );</code>
  178. *
  179. * @api bool $final
  180. */
  181. if ( apply_filters( 'wpseo_replacements_final', true ) === true && ( isset( $matches[1] ) && is_array( $matches[1] ) ) ) {
  182. // Remove non-replaced variables.
  183. $remove = array_diff( $matches[1], $omit ); // Make sure the $omit variables do not get removed.
  184. $remove = array_map( [ __CLASS__, 'add_var_delimiter' ], $remove );
  185. $string = str_replace( $remove, '', $string );
  186. }
  187. // Undouble separators which have nothing between them, i.e. where a non-replaced variable was removed.
  188. if ( isset( $replacements['%%sep%%'] ) && ( is_string( $replacements['%%sep%%'] ) && $replacements['%%sep%%'] !== '' ) ) {
  189. $q_sep = preg_quote( $replacements['%%sep%%'], '`' );
  190. $string = preg_replace( '`' . $q_sep . '(?:\s*' . $q_sep . ')*`u', $replacements['%%sep%%'], $string );
  191. }
  192. // Remove superfluous whitespace.
  193. $string = WPSEO_Utils::standardize_whitespace( $string );
  194. return $string;
  195. }
  196. /**
  197. * Retrieve the replacements for the variables found.
  198. *
  199. * @param array $matches Variables found in the original string - regex result.
  200. * @param array $omit Variables that should not be replaced by this function.
  201. *
  202. * @return array Retrieved replacements - this might be a smaller array as some variables
  203. * may not yield a replacement in certain contexts.
  204. */
  205. private function set_up_replacements( $matches, $omit ) {
  206. $replacements = [];
  207. // @todo Figure out a way to deal with external functions starting with cf_/ct_.
  208. foreach ( $matches[1] as $k => $var ) {
  209. // Don't set up replacements which should be omitted.
  210. if ( in_array( $var, $omit, true ) ) {
  211. continue;
  212. }
  213. // Deal with variable variable names first.
  214. if ( strpos( $var, 'cf_' ) === 0 ) {
  215. $replacement = $this->retrieve_cf_custom_field_name( $var );
  216. }
  217. elseif ( strpos( $var, 'ct_desc_' ) === 0 ) {
  218. $replacement = $this->retrieve_ct_desc_custom_tax_name( $var );
  219. }
  220. elseif ( strpos( $var, 'ct_' ) === 0 ) {
  221. $single = ( isset( $matches[2][ $k ] ) && $matches[2][ $k ] !== '' ) ? true : false;
  222. $replacement = $this->retrieve_ct_custom_tax_name( $var, $single );
  223. } // Deal with non-variable variable names.
  224. elseif ( method_exists( $this, 'retrieve_' . $var ) ) {
  225. $method_name = 'retrieve_' . $var;
  226. $replacement = $this->$method_name();
  227. } // Deal with externally defined variable names.
  228. elseif ( isset( self::$external_replacements[ $var ] ) && ! is_null( self::$external_replacements[ $var ] ) ) {
  229. $replacement = call_user_func( self::$external_replacements[ $var ], $var, $this->args );
  230. }
  231. // Replacement retrievals can return null if no replacement can be determined, root those outs.
  232. if ( isset( $replacement ) ) {
  233. $var = self::add_var_delimiter( $var );
  234. $replacements[ $var ] = $replacement;
  235. }
  236. unset( $replacement, $single, $method_name );
  237. }
  238. return $replacements;
  239. }
  240. /* *********************** BASIC VARIABLES ************************** */
  241. /**
  242. * Retrieve the post/cpt categories (comma separated) for use as replacement string.
  243. *
  244. * @return string|null
  245. */
  246. private function retrieve_category() {
  247. $replacement = null;
  248. if ( ! empty( $this->args->ID ) ) {
  249. $cat = $this->get_terms( $this->args->ID, 'category' );
  250. if ( $cat !== '' ) {
  251. return $cat;
  252. }
  253. }
  254. if ( isset( $this->args->cat_name ) && ! empty( $this->args->cat_name ) ) {
  255. $replacement = $this->args->cat_name;
  256. }
  257. return $replacement;
  258. }
  259. /**
  260. * Retrieve the category description for use as replacement string.
  261. *
  262. * @return string|null
  263. */
  264. private function retrieve_category_description() {
  265. return $this->retrieve_term_description();
  266. }
  267. /**
  268. * Retrieve the date of the post/page/cpt for use as replacement string.
  269. *
  270. * @return string|null
  271. */
  272. private function retrieve_date() {
  273. $replacement = null;
  274. if ( $this->args->post_date !== '' ) {
  275. $replacement = $this->date->format_translated( $this->args->post_date, get_option( 'date_format' ) );
  276. }
  277. else {
  278. if ( get_query_var( 'day' ) && get_query_var( 'day' ) !== '' ) {
  279. $replacement = get_the_date();
  280. }
  281. else {
  282. if ( single_month_title( ' ', false ) && single_month_title( ' ', false ) !== '' ) {
  283. $replacement = single_month_title( ' ', false );
  284. }
  285. elseif ( get_query_var( 'year' ) !== '' ) {
  286. $replacement = get_query_var( 'year' );
  287. }
  288. }
  289. }
  290. return $replacement;
  291. }
  292. /**
  293. * Retrieve the post/page/cpt excerpt for use as replacement string.
  294. * The excerpt will be auto-generated if it does not exist.
  295. *
  296. * @return string|null
  297. */
  298. private function retrieve_excerpt() {
  299. $replacement = null;
  300. // The check `post_password_required` is because excerpt must be hidden for a post with a password.
  301. if ( ! empty( $this->args->ID ) && ! post_password_required( $this->args->ID ) ) {
  302. if ( $this->args->post_excerpt !== '' ) {
  303. $replacement = wp_strip_all_tags( $this->args->post_excerpt );
  304. }
  305. elseif ( $this->args->post_content !== '' ) {
  306. $content = strip_shortcodes( $this->args->post_content );
  307. $content = wp_strip_all_tags( $content );
  308. if ( strlen( utf8_decode( $content ) ) <= 156 ) {
  309. return $content;
  310. }
  311. $replacement = wp_html_excerpt( $content, 156 );
  312. // Trim the auto-generated string to a word boundary.
  313. $replacement = substr( $replacement, 0, strrpos( $replacement, ' ' ) );
  314. }
  315. }
  316. return $replacement;
  317. }
  318. /**
  319. * Retrieve the post/page/cpt excerpt for use as replacement string (without auto-generation).
  320. *
  321. * @return string|null
  322. */
  323. private function retrieve_excerpt_only() {
  324. $replacement = null;
  325. // The check `post_password_required` is because excerpt must be hidden for a post with a password.
  326. if ( ! empty( $this->args->ID ) && $this->args->post_excerpt !== '' && ! post_password_required( $this->args->ID ) ) {
  327. $replacement = wp_strip_all_tags( $this->args->post_excerpt );
  328. }
  329. return $replacement;
  330. }
  331. /**
  332. * Retrieve the title of the parent page of the current page/cpt for use as replacement string.
  333. * Only applicable for hierarchical post types.
  334. *
  335. * @todo Check: shouldn't this use $this->args as well ?
  336. *
  337. * @return string|null
  338. */
  339. private function retrieve_parent_title() {
  340. $replacement = null;
  341. if ( ( is_singular() || is_admin() ) && isset( $GLOBALS['post'] ) ) {
  342. if ( isset( $GLOBALS['post']->post_parent ) && $GLOBALS['post']->post_parent !== 0 ) {
  343. $replacement = get_the_title( $GLOBALS['post']->post_parent );
  344. }
  345. }
  346. return $replacement;
  347. }
  348. /**
  349. * Retrieve the current search phrase for use as replacement string.
  350. *
  351. * @return string|null
  352. */
  353. private function retrieve_searchphrase() {
  354. $replacement = null;
  355. $search = get_query_var( 's' );
  356. if ( $search !== '' ) {
  357. $replacement = esc_html( $search );
  358. }
  359. return $replacement;
  360. }
  361. /**
  362. * Retrieve the separator for use as replacement string.
  363. *
  364. * @return string
  365. */
  366. private function retrieve_sep() {
  367. return WPSEO_Utils::get_title_separator();
  368. }
  369. /**
  370. * Retrieve the site's tag line / description for use as replacement string.
  371. *
  372. * The `$replacement` variable is static because it doesn't change depending
  373. * on the context. See https://github.com/Yoast/wordpress-seo/pull/1172#issuecomment-46019482.
  374. *
  375. * @return string|null
  376. */
  377. private function retrieve_sitedesc() {
  378. static $replacement;
  379. if ( ! isset( $replacement ) ) {
  380. $description = wp_strip_all_tags( get_bloginfo( 'description' ) );
  381. if ( $description !== '' ) {
  382. $replacement = $description;
  383. }
  384. }
  385. return $replacement;
  386. }
  387. /**
  388. * Retrieve the site's name for use as replacement string.
  389. *
  390. * The `$replacement` variable is static because it doesn't change depending
  391. * on the context. See https://github.com/Yoast/wordpress-seo/pull/1172#issuecomment-46019482.
  392. *
  393. * @return string|null
  394. */
  395. private function retrieve_sitename() {
  396. static $replacement;
  397. if ( ! isset( $replacement ) ) {
  398. $sitename = WPSEO_Utils::get_site_name();
  399. if ( $sitename !== '' ) {
  400. $replacement = $sitename;
  401. }
  402. }
  403. return $replacement;
  404. }
  405. /**
  406. * Retrieve the current tag/tags for use as replacement string.
  407. *
  408. * @return string|null
  409. */
  410. private function retrieve_tag() {
  411. $replacement = null;
  412. if ( isset( $this->args->ID ) ) {
  413. $tags = $this->get_terms( $this->args->ID, 'post_tag' );
  414. if ( $tags !== '' ) {
  415. $replacement = $tags;
  416. }
  417. }
  418. return $replacement;
  419. }
  420. /**
  421. * Retrieve the tag description for use as replacement string.
  422. *
  423. * @return string|null
  424. */
  425. private function retrieve_tag_description() {
  426. return $this->retrieve_term_description();
  427. }
  428. /**
  429. * Retrieve the term description for use as replacement string.
  430. *
  431. * @return string|null
  432. */
  433. private function retrieve_term_description() {
  434. $replacement = null;
  435. if ( isset( $this->args->term_id ) && ! empty( $this->args->taxonomy ) ) {
  436. $term_desc = get_term_field( 'description', $this->args->term_id, $this->args->taxonomy );
  437. if ( $term_desc !== '' ) {
  438. $replacement = wp_strip_all_tags( $term_desc );
  439. }
  440. }
  441. return $replacement;
  442. }
  443. /**
  444. * Retrieve the term name for use as replacement string.
  445. *
  446. * @return string|null
  447. */
  448. private function retrieve_term_title() {
  449. $replacement = null;
  450. if ( ! empty( $this->args->taxonomy ) && ! empty( $this->args->name ) ) {
  451. $replacement = $this->args->name;
  452. }
  453. return $replacement;
  454. }
  455. /**
  456. * Retrieve the title of the post/page/cpt for use as replacement string.
  457. *
  458. * @return string|null
  459. */
  460. private function retrieve_title() {
  461. $replacement = null;
  462. if ( is_string( $this->args->post_title ) && $this->args->post_title !== '' ) {
  463. $replacement = stripslashes( $this->args->post_title );
  464. }
  465. return $replacement;
  466. }
  467. /**
  468. * Retrieve primary category for use as replacement string.
  469. *
  470. * @return bool|int|null
  471. */
  472. private function retrieve_primary_category() {
  473. $primary_category = null;
  474. if ( ! empty( $this->args->ID ) ) {
  475. $wpseo_primary_category = new WPSEO_Primary_Term( 'category', $this->args->ID );
  476. $term_id = $wpseo_primary_category->get_primary_term();
  477. $term = get_term( $term_id );
  478. if ( ! is_wp_error( $term ) && ! empty( $term ) ) {
  479. $primary_category = $term->name;
  480. }
  481. }
  482. return $primary_category;
  483. }
  484. /**
  485. * Retrieve the string generated by get_the_archive_title().
  486. *
  487. * @return string|null
  488. */
  489. private function retrieve_archive_title() {
  490. return get_the_archive_title();
  491. }
  492. /* *********************** ADVANCED VARIABLES ************************** */
  493. /**
  494. * Determine the page numbering of the current post/page/cpt.
  495. *
  496. * @param string $request Either 'nr'|'max' - whether to return the page number or the max number of pages.
  497. *
  498. * @return int|null
  499. */
  500. private function determine_pagenumbering( $request = 'nr' ) {
  501. global $wp_query, $post;
  502. $max_num_pages = null;
  503. $page_number = null;
  504. $max_num_pages = 1;
  505. if ( ! is_singular() ) {
  506. $page_number = get_query_var( 'paged' );
  507. if ( $page_number === 0 || $page_number === '' ) {
  508. $page_number = 1;
  509. }
  510. if ( ! empty( $wp_query->max_num_pages ) ) {
  511. $max_num_pages = $wp_query->max_num_pages;
  512. }
  513. }
  514. else {
  515. $page_number = get_query_var( 'page' );
  516. if ( $page_number === 0 || $page_number === '' ) {
  517. $page_number = 1;
  518. }
  519. if ( isset( $post->post_content ) ) {
  520. $max_num_pages = ( substr_count( $post->post_content, '<!--nextpage-->' ) + 1 );
  521. }
  522. }
  523. $return = null;
  524. switch ( $request ) {
  525. case 'nr':
  526. $return = $page_number;
  527. break;
  528. case 'max':
  529. $return = $max_num_pages;
  530. break;
  531. }
  532. return $return;
  533. }
  534. /**
  535. * Determine the post type names for the current post/page/cpt.
  536. *
  537. * @param string $request Either 'single'|'plural' - whether to return the single or plural form.
  538. *
  539. * @return string|null
  540. */
  541. private function determine_pt_names( $request = 'single' ) {
  542. global $wp_query;
  543. $pt_single = null;
  544. $pt_plural = null;
  545. $post_type = '';
  546. 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'] !== [] ) ) ) {
  547. $post_type = $wp_query->query_vars['post_type'];
  548. }
  549. elseif ( isset( $this->args->post_type ) && ( is_string( $this->args->post_type ) && $this->args->post_type !== '' ) ) {
  550. $post_type = $this->args->post_type;
  551. }
  552. else {
  553. // Make it work in preview mode.
  554. $post = $wp_query->get_queried_object();
  555. if ( $post instanceof WP_Post ) {
  556. $post_type = $post->post_type;
  557. }
  558. }
  559. if ( is_array( $post_type ) ) {
  560. $post_type = reset( $post_type );
  561. }
  562. if ( $post_type !== '' ) {
  563. $pt = get_post_type_object( $post_type );
  564. $pt_single = $pt->name;
  565. $pt_plural = $pt->name;
  566. if ( isset( $pt->labels->singular_name ) ) {
  567. $pt_single = $pt->labels->singular_name;
  568. }
  569. if ( isset( $pt->labels->name ) ) {
  570. $pt_plural = $pt->labels->name;
  571. }
  572. }
  573. $return = null;
  574. switch ( $request ) {
  575. case 'single':
  576. $return = $pt_single;
  577. break;
  578. case 'plural':
  579. $return = $pt_plural;
  580. break;
  581. }
  582. return $return;
  583. }
  584. /**
  585. * Retrieve the attachment caption for use as replacement string.
  586. *
  587. * @return string|null
  588. */
  589. private function retrieve_caption() {
  590. return $this->retrieve_excerpt_only();
  591. }
  592. /**
  593. * Retrieve a post/page/cpt's custom field value for use as replacement string.
  594. *
  595. * @param string $var The complete variable to replace which includes the name of
  596. * the custom field which value is to be retrieved.
  597. *
  598. * @return string|null
  599. */
  600. private function retrieve_cf_custom_field_name( $var ) {
  601. global $post;
  602. $replacement = null;
  603. if ( is_string( $var ) && $var !== '' ) {
  604. $field = substr( $var, 3 );
  605. if ( ( is_singular() || is_admin() ) && ( is_object( $post ) && isset( $post->ID ) ) ) {
  606. $name = get_post_meta( $post->ID, $field, true );
  607. if ( $name !== '' ) {
  608. $replacement = $name;
  609. }
  610. }
  611. elseif ( is_category() || is_tag() || is_tax() ) {
  612. $term = $GLOBALS['wp_query']->get_queried_object();
  613. $name = get_term_meta( $term->term_id, $field, true );
  614. if ( $name !== '' ) {
  615. $replacement = $name;
  616. }
  617. }
  618. }
  619. return $replacement;
  620. }
  621. /**
  622. * Retrieve a post/page/cpt's custom taxonomies for use as replacement string.
  623. *
  624. * @param string $var The complete variable to replace which includes the name of
  625. * the custom taxonomy which value(s) is to be retrieved.
  626. * @param bool $single Whether to retrieve only the first or all values for the taxonomy.
  627. *
  628. * @return string|null
  629. */
  630. private function retrieve_ct_custom_tax_name( $var, $single = false ) {
  631. $replacement = null;
  632. if ( ( is_string( $var ) && $var !== '' ) && ! empty( $this->args->ID ) ) {
  633. $tax = substr( $var, 3 );
  634. $name = $this->get_terms( $this->args->ID, $tax, $single );
  635. if ( $name !== '' ) {
  636. $replacement = $name;
  637. }
  638. }
  639. return $replacement;
  640. }
  641. /**
  642. * Retrieve a post/page/cpt's custom taxonomies description for use as replacement string.
  643. *
  644. * @param string $var The complete variable to replace which includes the name of
  645. * the custom taxonomy which description is to be retrieved.
  646. *
  647. * @return string|null
  648. */
  649. private function retrieve_ct_desc_custom_tax_name( $var ) {
  650. global $post;
  651. $replacement = null;
  652. if ( is_string( $var ) && $var !== '' ) {
  653. $tax = substr( $var, 8 );
  654. if ( is_object( $post ) && isset( $post->ID ) ) {
  655. $terms = get_the_terms( $post->ID, $tax );
  656. if ( is_array( $terms ) && $terms !== [] ) {
  657. $term = current( $terms );
  658. $term_desc = get_term_field( 'description', $term->term_id, $tax );
  659. if ( $term_desc !== '' ) {
  660. $replacement = wp_strip_all_tags( $term_desc );
  661. }
  662. }
  663. }
  664. }
  665. return $replacement;
  666. }
  667. /**
  668. * Retrieve the current date for use as replacement string.
  669. *
  670. * The `$replacement` variable is static because it doesn't change depending
  671. * on the context. See https://github.com/Yoast/wordpress-seo/pull/1172#issuecomment-46019482.
  672. *
  673. * @return string The formatted current date.
  674. */
  675. private function retrieve_currentdate() {
  676. static $replacement;
  677. if ( ! isset( $replacement ) ) {
  678. $replacement = date_i18n( get_option( 'date_format' ) );
  679. }
  680. return $replacement;
  681. }
  682. /**
  683. * Retrieve the current day for use as replacement string.
  684. *
  685. * The `$replacement` variable is static because it doesn't change depending
  686. * on the context. See https://github.com/Yoast/wordpress-seo/pull/1172#issuecomment-46019482.
  687. *
  688. * @return string The current day.
  689. */
  690. private function retrieve_currentday() {
  691. static $replacement;
  692. if ( ! isset( $replacement ) ) {
  693. $replacement = date_i18n( 'j' );
  694. }
  695. return $replacement;
  696. }
  697. /**
  698. * Retrieve the current month for use as replacement string.
  699. *
  700. * The `$replacement` variable is static because it doesn't change depending
  701. * on the context. See https://github.com/Yoast/wordpress-seo/pull/1172#issuecomment-46019482.
  702. *
  703. * @return string The current month.
  704. */
  705. private function retrieve_currentmonth() {
  706. static $replacement;
  707. if ( ! isset( $replacement ) ) {
  708. $replacement = date_i18n( 'F' );
  709. }
  710. return $replacement;
  711. }
  712. /**
  713. * Retrieve the current time for use as replacement string.
  714. *
  715. * The `$replacement` variable is static because it doesn't change depending
  716. * on the context. See https://github.com/Yoast/wordpress-seo/pull/1172#issuecomment-46019482.
  717. *
  718. * @return string The formatted current time.
  719. */
  720. private function retrieve_currenttime() {
  721. static $replacement;
  722. if ( ! isset( $replacement ) ) {
  723. $replacement = date_i18n( get_option( 'time_format' ) );
  724. }
  725. return $replacement;
  726. }
  727. /**
  728. * Retrieve the current year for use as replacement string.
  729. *
  730. * The `$replacement` variable is static because it doesn't change depending
  731. * on the context. See https://github.com/Yoast/wordpress-seo/pull/1172#issuecomment-46019482.
  732. *
  733. * @return string The current year.
  734. */
  735. private function retrieve_currentyear() {
  736. static $replacement;
  737. if ( ! isset( $replacement ) ) {
  738. $replacement = date_i18n( 'Y' );
  739. }
  740. return $replacement;
  741. }
  742. /**
  743. * Retrieve the post/page/cpt's focus keyword for use as replacement string.
  744. *
  745. * @return string|null
  746. */
  747. private function retrieve_focuskw() {
  748. // Retrieve focuskw from a Post.
  749. if ( ! empty( $this->args->ID ) ) {
  750. $focus_kw = WPSEO_Meta::get_value( 'focuskw', $this->args->ID );
  751. if ( $focus_kw !== '' ) {
  752. return $focus_kw;
  753. }
  754. return null;
  755. }
  756. // Retrieve focuskw from a Term.
  757. if ( ! empty( $this->args->term_id ) ) {
  758. $focus_kw = WPSEO_Taxonomy_Meta::get_term_meta( $this->args->term_id, $this->args->taxonomy, 'focuskw' );
  759. if ( $focus_kw !== '' ) {
  760. return $focus_kw;
  761. }
  762. }
  763. return null;
  764. }
  765. /**
  766. * Retrieve the post/page/cpt ID for use as replacement string.
  767. *
  768. * @return string|null
  769. */
  770. private function retrieve_id() {
  771. $replacement = null;
  772. if ( ! empty( $this->args->ID ) ) {
  773. $replacement = $this->args->ID;
  774. }
  775. return $replacement;
  776. }
  777. /**
  778. * Retrieve the post/page/cpt modified time for use as replacement string.
  779. *
  780. * @return string|null
  781. */
  782. private function retrieve_modified() {
  783. $replacement = null;
  784. if ( ! empty( $this->args->post_modified ) ) {
  785. $replacement = $this->date->format_translated( $this->args->post_modified, get_option( 'date_format' ) );
  786. }
  787. return $replacement;
  788. }
  789. /**
  790. * Retrieve the post/page/cpt author's "nice name" for use as replacement string.
  791. *
  792. * @return string|null
  793. */
  794. private function retrieve_name() {
  795. $replacement = null;
  796. $user_id = $this->retrieve_userid();
  797. $name = get_the_author_meta( 'display_name', $user_id );
  798. if ( $name !== '' ) {
  799. $replacement = $name;
  800. }
  801. return $replacement;
  802. }
  803. /**
  804. * Retrieve the post/page/cpt author's users description for use as a replacement string.
  805. *
  806. * @return null|string
  807. */
  808. private function retrieve_user_description() {
  809. $replacement = null;
  810. $user_id = $this->retrieve_userid();
  811. $description = get_the_author_meta( 'description', $user_id );
  812. if ( $description !== '' ) {
  813. $replacement = $description;
  814. }
  815. return $replacement;
  816. }
  817. /**
  818. * Retrieve the current page number with context (i.e. 'page 2 of 4') for use as replacement string.
  819. *
  820. * @return string
  821. */
  822. private function retrieve_page() {
  823. $replacement = null;
  824. $max = $this->determine_pagenumbering( 'max' );
  825. $nr = $this->determine_pagenumbering( 'nr' );
  826. $sep = $this->retrieve_sep();
  827. if ( $max > 1 && $nr > 1 ) {
  828. /* translators: 1: current page number, 2: total number of pages. */
  829. $replacement = sprintf( $sep . ' ' . __( 'Page %1$d of %2$d', 'wordpress-seo' ), $nr, $max );
  830. }
  831. return $replacement;
  832. }
  833. /**
  834. * Retrieve the current page number for use as replacement string.
  835. *
  836. * @return string|null
  837. */
  838. private function retrieve_pagenumber() {
  839. $replacement = null;
  840. $nr = $this->determine_pagenumbering( 'nr' );
  841. if ( isset( $nr ) && $nr > 0 ) {
  842. $replacement = (string) $nr;
  843. }
  844. return $replacement;
  845. }
  846. /**
  847. * Retrieve the current page total for use as replacement string.
  848. *
  849. * @return string|null
  850. */
  851. private function retrieve_pagetotal() {
  852. $replacement = null;
  853. $max = $this->determine_pagenumbering( 'max' );
  854. if ( isset( $max ) && $max > 0 ) {
  855. $replacement = (string) $max;
  856. }
  857. return $replacement;
  858. }
  859. /**
  860. * Retrieve the post type plural label for use as replacement string.
  861. *
  862. * @return string|null
  863. */
  864. private function retrieve_pt_plural() {
  865. $replacement = null;
  866. $name = $this->determine_pt_names( 'plural' );
  867. if ( isset( $name ) && $name !== '' ) {
  868. $replacement = $name;
  869. }
  870. return $replacement;
  871. }
  872. /**
  873. * Retrieve the post type single label for use as replacement string.
  874. *
  875. * @return string|null
  876. */
  877. private function retrieve_pt_single() {
  878. $replacement = null;
  879. $name = $this->determine_pt_names( 'single' );
  880. if ( isset( $name ) && $name !== '' ) {
  881. $replacement = $name;
  882. }
  883. return $replacement;
  884. }
  885. /**
  886. * Retrieve the slug which caused the 404 for use as replacement string.
  887. *
  888. * @return string|null
  889. */
  890. private function retrieve_term404() {
  891. $replacement = null;
  892. if ( $this->args->term404 !== '' ) {
  893. $replacement = sanitize_text_field( str_replace( '-', ' ', $this->args->term404 ) );
  894. }
  895. else {
  896. $error_request = get_query_var( 'pagename' );
  897. if ( $error_request !== '' ) {
  898. $replacement = sanitize_text_field( str_replace( '-', ' ', $error_request ) );
  899. }
  900. else {
  901. $error_request = get_query_var( 'name' );
  902. if ( $error_request !== '' ) {
  903. $replacement = sanitize_text_field( str_replace( '-', ' ', $error_request ) );
  904. }
  905. }
  906. }
  907. return $replacement;
  908. }
  909. /**
  910. * Retrieve the post/page/cpt author's user id for use as replacement string.
  911. *
  912. * @return string
  913. */
  914. private function retrieve_userid() {
  915. $replacement = ! empty( $this->args->post_author ) ? $this->args->post_author : get_query_var( 'author' );
  916. return $replacement;
  917. }
  918. /* *********************** HELP TEXT RELATED ************************** */
  919. /**
  920. * Set the help text for a user/plugin/theme defined extra variable.
  921. *
  922. * @param string $type Type of variable: 'basic' or 'advanced'.
  923. * @param WPSEO_Replacement_Variable $replacement_variable The replacement variable to register.
  924. */
  925. private static function register_help_text( $type, WPSEO_Replacement_Variable $replacement_variable ) {
  926. $identifier = $replacement_variable->get_variable();
  927. if ( ( is_string( $type ) && in_array( $type, [ 'basic', 'advanced' ], true ) )
  928. && ( $identifier !== '' && ! isset( self::$help_texts[ $type ][ $identifier ] ) )
  929. ) {
  930. self::$help_texts[ $type ][ $identifier ] = $replacement_variable;
  931. }
  932. }
  933. /**
  934. * Generates a list of replacement variables based on the help texts.
  935. *
  936. * @return array List of replace vars.
  937. */
  938. public function get_replacement_variables_list() {
  939. self::setup_statics_once();
  940. $replacement_variables = array_merge(
  941. $this->get_replacement_variables(),
  942. WPSEO_Custom_Fields::get_custom_fields(),
  943. WPSEO_Custom_Taxonomies::get_custom_taxonomies()
  944. );
  945. return array_map( [ $this, 'format_replacement_variable' ], $replacement_variables );
  946. }
  947. /**
  948. * Creates a merged associative array of both the basic and advanced help texts.
  949. *
  950. * @return array Array with the replacement variables.
  951. */
  952. private function get_replacement_variables() {
  953. $help_texts = array_merge( self::$help_texts['basic'], self::$help_texts['advanced'] );
  954. return array_filter( array_keys( $help_texts ), [ $this, 'is_not_prefixed' ] );
  955. }
  956. /**
  957. * Checks whether the replacement variable contains a `ct_` or `cf_` prefix, because they follow different logic.
  958. *
  959. * @param string $replacement_variable The replacement variable.
  960. *
  961. * @return bool True when the replacement variable is not prefixed.
  962. */
  963. private function is_not_prefixed( $replacement_variable ) {
  964. $prefixes = [ 'cf_', 'ct_' ];
  965. $prefix = $this->get_prefix( $replacement_variable );
  966. return ! in_array( $prefix, $prefixes, true );
  967. }
  968. /**
  969. * Strip the prefix from a replacement variable name.
  970. *
  971. * @param string $replacement_variable The replacement variable.
  972. *
  973. * @return string The replacement variable name without the prefix.
  974. */
  975. private function strip_prefix( $replacement_variable ) {
  976. return substr( $replacement_variable, 3 );
  977. }
  978. /**
  979. * Gets the prefix from a replacement variable name.
  980. *
  981. * @param string $replacement_variable The replacement variable.
  982. *
  983. * @return string The prefix of the replacement variable.
  984. */
  985. private function get_prefix( $replacement_variable ) {
  986. return substr( $replacement_variable, 0, 3 );
  987. }
  988. /**
  989. * Strips 'desc_' if present, and appends ' description' at the end.
  990. *
  991. * @param string $label The replacement variable.
  992. *
  993. * @return string The altered replacement variable name.
  994. */
  995. private function handle_description( $label ) {
  996. if ( strpos( $label, 'desc_' ) === 0 ) {
  997. return substr( $label, 5 ) . ' description';
  998. }
  999. return $label;
  1000. }
  1001. /**
  1002. * Creates a label for prefixed replacement variables that matches the format in the editors.
  1003. *
  1004. * @param string $replacement_variable The replacement variable.
  1005. *
  1006. * @return string The replacement variable label.
  1007. */
  1008. private function get_label( $replacement_variable ) {
  1009. $prefix = $this->get_prefix( $replacement_variable );
  1010. if ( $prefix === 'cf_' ) {
  1011. return $this->strip_prefix( $replacement_variable ) . ' (custom field)';
  1012. }
  1013. if ( $prefix === 'ct_' ) {
  1014. $label = $this->strip_prefix( $replacement_variable );
  1015. $label = $this->handle_description( $label );
  1016. return ucfirst( $label . ' (custom taxonomy)' );
  1017. }
  1018. if ( $prefix === 'pt_' ) {
  1019. if ( $replacement_variable === 'pt_single' ) {
  1020. return 'Post type (singular)';
  1021. }
  1022. return 'Post type (plural)';
  1023. }
  1024. return '';
  1025. }
  1026. /**
  1027. * Formats the replacement variables.
  1028. *
  1029. * @param string $replacement_variable The replacement variable to format.
  1030. *
  1031. * @return array The formatted replacement variable.
  1032. */
  1033. private function format_replacement_variable( $replacement_variable ) {
  1034. return [
  1035. 'name' => $replacement_variable,
  1036. 'value' => '',
  1037. 'label' => $this->get_label( $replacement_variable ),
  1038. ];
  1039. }
  1040. /**
  1041. * Set/translate the help texts for the WPSEO standard basic variables.
  1042. */
  1043. private static function set_basic_help_texts() {
  1044. /* translators: %s: wp_title() function. */
  1045. $separator_description = __( 'The separator defined in your theme\'s %s tag.', 'wordpress-seo' );
  1046. $separator_description = sprintf(
  1047. $separator_description,
  1048. // '<code>wp_title()</code>'
  1049. 'wp_title()'
  1050. );
  1051. $replacement_variables = [
  1052. new WPSEO_Replacement_Variable( 'date', __( 'Date', 'wordpress-seo' ), __( 'Replaced with the date of the post/page', 'wordpress-seo' ) ),
  1053. new WPSEO_Replacement_Variable( 'title', __( 'Title', 'wordpress-seo' ), __( 'Replaced with the title of the post/page', 'wordpress-seo' ) ),
  1054. new WPSEO_Replacement_Variable( 'parent_title', __( 'Parent title', 'wordpress-seo' ), __( 'Replaced with the title of the parent page of the current page', 'wordpress-seo' ) ),
  1055. new WPSEO_Replacement_Variable( 'archive_title', __( 'Archive title', 'wordpress-seo' ), __( 'Replaced with the normal title for an archive generated by WordPress', 'wordpress-seo' ) ),
  1056. new WPSEO_Replacement_Variable( 'sitename', __( 'Site title', 'wordpress-seo' ), __( 'The site\'s name', 'wordpress-seo' ) ),
  1057. new WPSEO_Replacement_Variable( 'sitedesc', __( 'Tagline', 'wordpress-seo' ), __( 'The site\'s tagline', 'wordpress-seo' ) ),
  1058. new WPSEO_Replacement_Variable( 'excerpt', __( 'Excerpt', 'wordpress-seo' ), __( 'Replaced with the post/page excerpt (or auto-generated if it does not exist)', 'wordpress-seo' ) ),
  1059. new WPSEO_Replacement_Variable( 'excerpt_only', __( 'Excerpt only', 'wordpress-seo' ), __( 'Replaced with the post/page excerpt (without auto-generation)', 'wordpress-seo' ) ),
  1060. new WPSEO_Replacement_Variable( 'tag', __( 'Tag', 'wordpress-seo' ), __( 'Replaced with the current tag/tags', 'wordpress-seo' ) ),
  1061. new WPSEO_Replacement_Variable( 'category', __( 'Category', 'wordpress-seo' ), __( 'Replaced with the post categories (comma separated)', 'wordpress-seo' ) ),
  1062. new WPSEO_Replacement_Variable( 'primary_category', __( 'Primary category', 'wordpress-seo' ), __( 'Replaced with the primary category of the post/page', 'wordpress-seo' ) ),
  1063. new WPSEO_Replacement_Variable( 'category_description', __( 'Category description', 'wordpress-seo' ), __( 'Replaced with the category description', 'wordpress-seo' ) ),
  1064. new WPSEO_Replacement_Variable( 'tag_description', __( 'Tag description', 'wordpress-seo' ), __( 'Replaced with the tag description', 'wordpress-seo' ) ),
  1065. new WPSEO_Replacement_Variable( 'term_description', __( 'Term description', 'wordpress-seo' ), __( 'Replaced with the term description', 'wordpress-seo' ) ),
  1066. new WPSEO_Replacement_Variable( 'term_title', __( 'Term title', 'wordpress-seo' ), __( 'Replaced with the term name', 'wordpress-seo' ) ),
  1067. new WPSEO_Replacement_Variable( 'searchphrase', __( 'Search phrase', 'wordpress-seo' ), __( 'Replaced with the current search phrase', 'wordpress-seo' ) ),
  1068. new WPSEO_Replacement_Variable( 'term_hierarchy', __( 'Term hierarchy', 'wordpress-seo' ), __( 'Replaced with the term ancestors hierarchy', 'wordpress-seo' ) ),
  1069. new WPSEO_Replacement_Variable( 'sep', __( 'Separator', 'wordpress-seo' ), $separator_description ),
  1070. ];
  1071. foreach ( $replacement_variables as $replacement_variable ) {
  1072. self::register_help_text( 'basic', $replacement_variable );
  1073. }
  1074. }
  1075. /**
  1076. * Set/translate the help texts for the WPSEO standard advanced variables.
  1077. */
  1078. private static function set_advanced_help_texts() {
  1079. $replacement_variables = [
  1080. new WPSEO_Replacement_Variable( 'pt_single', __( 'Post type (singular)', 'wordpress-seo' ), __( 'Replaced with the content type single label', 'wordpress-seo' ) ),
  1081. new WPSEO_Replacement_Variable( 'pt_plural', __( 'Post type (plural)', 'wordpress-seo' ), __( 'Replaced with the content type plural label', 'wordpress-seo' ) ),
  1082. new WPSEO_Replacement_Variable( 'modified', __( 'Modified', 'wordpress-seo' ), __( 'Replaced with the post/page modified time', 'wordpress-seo' ) ),
  1083. new WPSEO_Replacement_Variable( 'id', __( 'ID', 'wordpress-seo' ), __( 'Replaced with the post/page ID', 'wordpress-seo' ) ),
  1084. new WPSEO_Replacement_Variable( 'name', __( 'Name', 'wordpress-seo' ), __( 'Replaced with the post/page author\'s \'nicename\'', 'wordpress-seo' ) ),
  1085. new WPSEO_Replacement_Variable( 'user_description', __( 'User description', 'wordpress-seo' ), __( 'Replaced with the post/page author\'s \'Biographical Info\'', 'wordpress-seo' ) ),
  1086. 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' ) ),
  1087. new WPSEO_Replacement_Variable( 'pagetotal', __( 'Pagetotal', 'wordpress-seo' ), __( 'Replaced with the current page total', 'wordpress-seo' ) ),
  1088. new WPSEO_Replacement_Variable( 'pagenumber', __( 'Pagenumber', 'wordpress-seo' ), __( 'Replaced with the current page number', 'wordpress-seo' ) ),
  1089. new WPSEO_Replacement_Variable( 'caption', __( 'Caption', 'wordpress-seo' ), __( 'Attachment caption', 'wordpress-seo' ) ),
  1090. new WPSEO_Replacement_Variable( 'focuskw', __( 'Focus keyword', 'wordpress-seo' ), __( 'Replaced with the posts focus keyphrase', 'wordpress-seo' ) ),
  1091. new WPSEO_Replacement_Variable( 'term404', __( 'Term404', 'wordpress-seo' ), __( 'Replaced with the slug which caused the 404', 'wordpress-seo' ) ),
  1092. new WPSEO_Replacement_Variable( 'cf_<custom-field-name>', '<custom-field-name> ' . __( '(custom field)', 'wordpress-seo' ), __( 'Replaced with a posts custom field value', 'wordpress-seo' ) ),
  1093. 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' ) ),
  1094. 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' ) ),
  1095. ];
  1096. foreach ( $replacement_variables as $replacement_variable ) {
  1097. self::register_help_text( 'advanced', $replacement_variable );
  1098. }
  1099. }
  1100. /* *********************** GENERAL HELPER METHODS ************************** */
  1101. /**
  1102. * Remove the '%%' delimiters from a variable string.
  1103. *
  1104. * @param string $string Variable string to be cleaned.
  1105. *
  1106. * @return string
  1107. */
  1108. private static function remove_var_delimiter( $string ) {
  1109. return trim( $string, '%' );
  1110. }
  1111. /**
  1112. * Add the '%%' delimiters to a variable string.
  1113. *
  1114. * @param string $string Variable string to be delimited.
  1115. *
  1116. * @return string
  1117. */
  1118. private static function add_var_delimiter( $string ) {
  1119. return '%%' . $string . '%%';
  1120. }
  1121. /**
  1122. * Retrieve a post's terms, comma delimited.
  1123. *
  1124. * @param int $id ID of the post to get the terms for.
  1125. * @param string $taxonomy The taxonomy to get the terms for this post from.
  1126. * @param bool $return_single If true, return the first term.
  1127. *
  1128. * @return string Either a single term or a comma delimited string of terms.
  1129. */
  1130. public function get_terms( $id, $taxonomy, $return_single = false ) {
  1131. $output = '';
  1132. // If we're on a specific tag, category or taxonomy page, use that.
  1133. if ( is_category() || is_tag() || is_tax() ) {
  1134. $term = $GLOBALS['wp_query']->get_queried_object();
  1135. $output = $term->name;
  1136. }
  1137. elseif ( ! empty( $id ) && ! empty( $taxonomy ) ) {
  1138. $terms = get_the_terms( $id, $taxonomy );
  1139. if ( is_array( $terms ) && $terms !== [] ) {
  1140. foreach ( $terms as $term ) {
  1141. if ( $return_single ) {
  1142. $output = $term->name;
  1143. break;
  1144. }
  1145. else {
  1146. $output .= $term->name . ', ';
  1147. }
  1148. }
  1149. $output = rtrim( trim( $output ), ',' );
  1150. }
  1151. }
  1152. unset( $terms, $term );
  1153. /**
  1154. * Allows filtering of the terms list used to replace %%category%%, %%tag%%
  1155. * and %%ct_<custom-tax-name>%% variables.
  1156. *
  1157. * @api string $output Comma-delimited string containing the terms.
  1158. * @api string $taxonomy The taxonomy of the terms.
  1159. */
  1160. return apply_filters( 'wpseo_terms', $output, $taxonomy );
  1161. }
  1162. /**
  1163. * Gets a taxonomy term hierarchy including the term to get the parents for.
  1164. *
  1165. * @return string
  1166. */
  1167. private function get_term_hierarchy() {
  1168. if ( ! is_taxonomy_hierarchical( $this->args->taxonomy ) ) {
  1169. return '';
  1170. }
  1171. $separator = ' ' . $this->retrieve_sep() . ' ';
  1172. $args = [
  1173. 'format' => 'name',
  1174. 'separator' => $separator,
  1175. 'link' => false,
  1176. 'inclusive' => true,
  1177. ];
  1178. return rtrim(
  1179. get_term_parents_list( $this->args->term_id, $this->args->taxonomy, $args ),
  1180. $separator
  1181. );
  1182. }
  1183. /**
  1184. * Retrieves the term ancestors hierarchy.
  1185. *
  1186. * @return string|null The term ancestors hierarchy.
  1187. */
  1188. private function retrieve_term_hierarchy() {
  1189. $replacement = null;
  1190. if ( isset( $this->args->term_id ) && ! empty( $this->args->taxonomy ) ) {
  1191. $hierarchy = $this->get_term_hierarchy();
  1192. if ( $hierarchy !== '' ) {
  1193. $replacement = esc_html( $hierarchy );
  1194. }
  1195. }
  1196. return $replacement;
  1197. }
  1198. }