PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/server/wordpress/wp-content/plugins/wordpress-seo/admin/class-admin-editor-specific-replace-vars.php

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