PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/server/wordpress/wp-content/plugins/wordpress-seo/inc/class-rewrite.php

https://gitlab.com/suporte.spturis/carnaval2015.spturis.com.br
PHP | 245 lines | 113 code | 38 blank | 94 comment | 18 complexity | 296aedc46d5744b45badc7b81b252445 MD5 | raw file
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Frontend
  6. */
  7. /**
  8. * This code handles the category rewrites.
  9. */
  10. class WPSEO_Rewrite {
  11. /**
  12. * Class constructor.
  13. */
  14. public function __construct() {
  15. add_filter( 'query_vars', [ $this, 'query_vars' ] );
  16. add_filter( 'term_link', [ $this, 'no_category_base' ], 10, 3 );
  17. add_filter( 'request', [ $this, 'request' ] );
  18. add_filter( 'category_rewrite_rules', [ $this, 'category_rewrite_rules' ] );
  19. add_action( 'created_category', [ $this, 'schedule_flush' ] );
  20. add_action( 'edited_category', [ $this, 'schedule_flush' ] );
  21. add_action( 'delete_category', [ $this, 'schedule_flush' ] );
  22. }
  23. /**
  24. * Trigger a rewrite_rule flush on shutdown.
  25. *
  26. * @since 1.2.8
  27. */
  28. public function schedule_flush() {
  29. add_action( 'shutdown', 'flush_rewrite_rules' );
  30. }
  31. /**
  32. * If the flush option is set, flush the rewrite rules.
  33. *
  34. * @since 1.2.8
  35. * @deprecated 17.4
  36. * @codeCoverageIgnore
  37. *
  38. * @return bool
  39. */
  40. public function flush() {
  41. _deprecated_function( __METHOD__, 'WPSEO 17.4', __CLASS__ . '::schedule_flush' );
  42. if ( get_option( 'wpseo_flush_rewrite' ) ) {
  43. add_action( 'shutdown', 'flush_rewrite_rules' );
  44. delete_option( 'wpseo_flush_rewrite' );
  45. return true;
  46. }
  47. return false;
  48. }
  49. /**
  50. * Override the category link to remove the category base.
  51. *
  52. * @param string $link Term link, overridden by the function for categories.
  53. * @param WP_Term $term Unused, term object.
  54. * @param string $taxonomy Taxonomy slug.
  55. *
  56. * @return string
  57. */
  58. public function no_category_base( $link, $term, $taxonomy ) {
  59. if ( $taxonomy !== 'category' ) {
  60. return $link;
  61. }
  62. $category_base = get_option( 'category_base' );
  63. if ( empty( $category_base ) ) {
  64. $category_base = 'category';
  65. }
  66. /*
  67. * Remove initial slash, if there is one (we remove the trailing slash
  68. * in the regex replacement and don't want to end up short a slash).
  69. */
  70. if ( substr( $category_base, 0, 1 ) === '/' ) {
  71. $category_base = substr( $category_base, 1 );
  72. }
  73. $category_base .= '/';
  74. return preg_replace( '`' . preg_quote( $category_base, '`' ) . '`u', '', $link, 1 );
  75. }
  76. /**
  77. * Update the query vars with the redirect var when stripcategorybase is active.
  78. *
  79. * @param array $query_vars Main query vars to filter.
  80. *
  81. * @return array
  82. */
  83. public function query_vars( $query_vars ) {
  84. if ( WPSEO_Options::get( 'stripcategorybase' ) === true ) {
  85. $query_vars[] = 'wpseo_category_redirect';
  86. }
  87. return $query_vars;
  88. }
  89. /**
  90. * Checks whether the redirect needs to be created.
  91. *
  92. * @param array $query_vars Query vars to check for existence of redirect var.
  93. *
  94. * @return array|void The query vars.
  95. */
  96. public function request( $query_vars ) {
  97. if ( ! isset( $query_vars['wpseo_category_redirect'] ) ) {
  98. return $query_vars;
  99. }
  100. $this->redirect( $query_vars['wpseo_category_redirect'] );
  101. }
  102. /**
  103. * This function taken and only slightly adapted from WP No Category Base plugin by Saurabh Gupta.
  104. *
  105. * @return array
  106. */
  107. public function category_rewrite_rules() {
  108. global $wp_rewrite;
  109. $category_rewrite = [];
  110. $taxonomy = get_taxonomy( 'category' );
  111. $permalink_structure = get_option( 'permalink_structure' );
  112. $blog_prefix = '';
  113. if ( is_multisite() && ! is_subdomain_install() && is_main_site() && strpos( $permalink_structure, '/blog/' ) === 0 ) {
  114. $blog_prefix = 'blog/';
  115. }
  116. $categories = get_categories( [ 'hide_empty' => false ] );
  117. if ( is_array( $categories ) && $categories !== [] ) {
  118. foreach ( $categories as $category ) {
  119. $category_nicename = $category->slug;
  120. if ( $category->parent === $category->cat_ID ) {
  121. // Recursive recursion.
  122. $category->parent = 0;
  123. }
  124. elseif ( $taxonomy->rewrite['hierarchical'] !== false && $category->parent !== 0 ) {
  125. $parents = get_category_parents( $category->parent, false, '/', true );
  126. if ( ! is_wp_error( $parents ) ) {
  127. $category_nicename = $parents . $category_nicename;
  128. }
  129. unset( $parents );
  130. }
  131. $category_rewrite = $this->add_category_rewrites( $category_rewrite, $category_nicename, $blog_prefix, $wp_rewrite->pagination_base );
  132. // Adds rules for the uppercase encoded URIs.
  133. $category_nicename_filtered = $this->convert_encoded_to_upper( $category_nicename );
  134. if ( $category_nicename_filtered !== $category_nicename ) {
  135. $category_rewrite = $this->add_category_rewrites( $category_rewrite, $category_nicename_filtered, $blog_prefix, $wp_rewrite->pagination_base );
  136. }
  137. }
  138. unset( $categories, $category, $category_nicename, $category_nicename_filtered );
  139. }
  140. // Redirect support from Old Category Base.
  141. $old_base = $wp_rewrite->get_category_permastruct();
  142. $old_base = str_replace( '%category%', '(.+)', $old_base );
  143. $old_base = trim( $old_base, '/' );
  144. $category_rewrite[ $old_base . '$' ] = 'index.php?wpseo_category_redirect=$matches[1]';
  145. return $category_rewrite;
  146. }
  147. /**
  148. * Adds required category rewrites rules.
  149. *
  150. * @param array $rewrites The current set of rules.
  151. * @param string $category_name Category nicename.
  152. * @param string $blog_prefix Multisite blog prefix.
  153. * @param string $pagination_base WP_Query pagination base.
  154. *
  155. * @return array The added set of rules.
  156. */
  157. protected function add_category_rewrites( $rewrites, $category_name, $blog_prefix, $pagination_base ) {
  158. $rewrite_name = $blog_prefix . '(' . $category_name . ')';
  159. $rewrites[ $rewrite_name . '/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$' ] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
  160. $rewrites[ $rewrite_name . '/' . $pagination_base . '/?([0-9]{1,})/?$' ] = 'index.php?category_name=$matches[1]&paged=$matches[2]';
  161. $rewrites[ $rewrite_name . '/?$' ] = 'index.php?category_name=$matches[1]';
  162. return $rewrites;
  163. }
  164. /**
  165. * Walks through category nicename and convert encoded parts
  166. * into uppercase using $this->encode_to_upper().
  167. *
  168. * @param string $name The encoded category URI string.
  169. *
  170. * @return string The convered URI string.
  171. */
  172. protected function convert_encoded_to_upper( $name ) {
  173. // Checks if name has any encoding in it.
  174. if ( strpos( $name, '%' ) === false ) {
  175. return $name;
  176. }
  177. $names = explode( '/', $name );
  178. $names = array_map( [ $this, 'encode_to_upper' ], $names );
  179. return implode( '/', $names );
  180. }
  181. /**
  182. * Converts the encoded URI string to uppercase.
  183. *
  184. * @param string $encoded The encoded string.
  185. *
  186. * @return string The uppercased string.
  187. */
  188. public function encode_to_upper( $encoded ) {
  189. if ( strpos( $encoded, '%' ) === false ) {
  190. return $encoded;
  191. }
  192. return strtoupper( $encoded );
  193. }
  194. /**
  195. * Redirect the "old" category URL to the new one.
  196. *
  197. * @codeCoverageIgnore
  198. *
  199. * @param string $category_redirect The category page to redirect to.
  200. * @return void
  201. */
  202. protected function redirect( $category_redirect ) {
  203. $catlink = trailingslashit( get_option( 'home' ) ) . user_trailingslashit( $category_redirect, 'category' );
  204. wp_safe_redirect( $catlink, 301, 'Yoast SEO' );
  205. exit;
  206. }
  207. }