class-rewrite.php 6.8 KB

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