PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

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