class-admin-editor-specific-replace-vars.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * Determines the editor specific replacement variables.
  9. */
  10. class WPSEO_Admin_Editor_Specific_Replace_Vars {
  11. /**
  12. * @var array The editor specific replacement variables.
  13. */
  14. protected $replacement_variables = array(
  15. // Posts types.
  16. 'page' => array( 'id', 'pt_single', 'pt_plural', 'parent_title' ),
  17. 'post' => array( 'id', 'term404', 'pt_single', 'pt_plural' ),
  18. // Custom post type.
  19. 'custom_post_type' => array( 'id', 'term404', 'pt_single', 'pt_plural', 'parent_title' ),
  20. // Settings - archive pages.
  21. 'custom-post-type_archive' => array( 'pt_single', 'pt_plural' ),
  22. // Taxonomies.
  23. 'category' => array( 'term_title', 'term_description', 'category_description', 'parent_title' ),
  24. 'post_tag' => array( 'term_title', 'term_description', 'tag_description' ),
  25. 'post_format' => array(),
  26. // Custom taxonomy.
  27. 'term-in-custom-taxonomy' => array( 'term_title', 'term_description', 'category_description', 'parent_title' ),
  28. // Settings - special pages.
  29. 'search' => array( 'searchphrase' ),
  30. );
  31. /**
  32. * WPSEO_Admin_Editor_Specific_Replace_Vars constructor.
  33. */
  34. public function __construct() {
  35. $this->add_for_page_types(
  36. array( 'page', 'post', 'custom_post_type' ),
  37. WPSEO_Custom_Fields::get_custom_fields()
  38. );
  39. $this->add_for_page_types(
  40. array( 'post', 'term-in-custom-taxonomies' ),
  41. WPSEO_Custom_Taxonomies::get_custom_taxonomies()
  42. );
  43. }
  44. /**
  45. * Retrieves the editor specific replacement variables.
  46. *
  47. * @return array The editor specific replacement variables.
  48. */
  49. public function get() {
  50. /**
  51. * Filter: Adds the possibility to add extra editor specific replacement variables.
  52. *
  53. * @api array $replacement_variables Empty array to add the editor specific replace vars to.
  54. */
  55. $replacement_variables = apply_filters(
  56. 'wpseo_editor_specific_replace_vars',
  57. $this->replacement_variables
  58. );
  59. if ( ! is_array( $replacement_variables ) ) {
  60. $replacement_variables = $this->replacement_variables;
  61. }
  62. return array_filter( $replacement_variables, 'is_array' );
  63. }
  64. /**
  65. * Retrieves the generic replacement variable names.
  66. *
  67. * Which are the replacement variables without the editor specific ones.
  68. *
  69. * @param array $replacement_variables Possibly generic replacement variables.
  70. *
  71. * @return array The generic replacement variable names.
  72. */
  73. public function get_generic( $replacement_variables ) {
  74. $shared_variables = array_diff(
  75. $this->extract_names( $replacement_variables ),
  76. $this->get_unique_replacement_variables()
  77. );
  78. return array_values( $shared_variables );
  79. }
  80. /**
  81. * Determines the page type of the current term.
  82. *
  83. * @param string $taxonomy The taxonomy name.
  84. *
  85. * @return string The page type.
  86. */
  87. public function determine_for_term( $taxonomy ) {
  88. $replacement_variables = $this->get();
  89. if ( array_key_exists( $taxonomy, $replacement_variables ) ) {
  90. return $taxonomy;
  91. }
  92. return 'term-in-custom-taxonomy';
  93. }
  94. /**
  95. * Determines the page type of the current post.
  96. *
  97. * @param WP_Post $post A WordPress post instance.
  98. *
  99. * @return string The page type.
  100. */
  101. public function determine_for_post( $post ) {
  102. if ( $post instanceof WP_Post === false ) {
  103. return 'post';
  104. }
  105. $replacement_variables = $this->get();
  106. if ( array_key_exists( $post->post_type, $replacement_variables ) ) {
  107. return $post->post_type;
  108. }
  109. return 'custom_post_type';
  110. }
  111. /**
  112. * Determines the page type for a post type.
  113. *
  114. * @param string $post_type The name of the post_type.
  115. * @param string $fallback The page type to fall back to.
  116. *
  117. * @return string The page type.
  118. */
  119. public function determine_for_post_type( $post_type, $fallback = 'custom_post_type' ) {
  120. if ( ! $this->has_for_page_type( $post_type ) ) {
  121. return $fallback;
  122. }
  123. return $post_type;
  124. }
  125. /**
  126. * Determines the page type for an archive page.
  127. *
  128. * @param string $name The name of the archive.
  129. * @param string $fallback The page type to fall back to.
  130. *
  131. * @return string The page type.
  132. */
  133. public function determine_for_archive( $name, $fallback = 'custom-post-type_archive' ) {
  134. $page_type = $name . '_archive';
  135. if ( ! $this->has_for_page_type( $page_type ) ) {
  136. return $fallback;
  137. }
  138. return $page_type;
  139. }
  140. /**
  141. * Adds the replavement variables for the given page types.
  142. *
  143. * @param array $page_types Page types to add variables for.
  144. * @param array $replacement_variables_to_add The variables to add.
  145. *
  146. * @return void
  147. */
  148. protected function add_for_page_types( array $page_types, array $replacement_variables_to_add ) {
  149. if ( empty( $replacement_variables_to_add ) ) {
  150. return;
  151. }
  152. $replacement_variables_to_add = array_fill_keys( $page_types, $replacement_variables_to_add );
  153. $replacement_variables = $this->replacement_variables;
  154. $this->replacement_variables = array_merge_recursive( $replacement_variables, $replacement_variables_to_add );
  155. }
  156. /**
  157. * Extracts the names from the given replacements variables.
  158. *
  159. * @param array $replacement_variables Replacement variables to extract the name from.
  160. *
  161. * @return array Extracted names.
  162. */
  163. protected function extract_names( $replacement_variables ) {
  164. $extracted_names = array();
  165. foreach ( $replacement_variables as $replacement_variable ) {
  166. if ( empty( $replacement_variable['name'] ) ) {
  167. continue;
  168. }
  169. $extracted_names[] = $replacement_variable['name'];
  170. }
  171. return $extracted_names;
  172. }
  173. /**
  174. * Returns whether the given page type has editor specific replace vars.
  175. *
  176. * @param string $page_type The page type to check.
  177. *
  178. * @return bool True if there are associated editor specific replace vars.
  179. */
  180. protected function has_for_page_type( $page_type ) {
  181. $replacement_variables = $this->get();
  182. return ( ! empty( $replacement_variables[ $page_type ] ) && is_array( $replacement_variables[ $page_type ] ) );
  183. }
  184. /**
  185. * Merges all editor specific replacement variables into one array and removes duplicates.
  186. *
  187. * @return array The list of unique editor specific replacement variables.
  188. */
  189. protected function get_unique_replacement_variables() {
  190. $merged_replacement_variables = call_user_func_array( 'array_merge', $this->get() );
  191. return array_unique( $merged_replacement_variables );
  192. }
  193. }