class-rewrite.php 6.6 KB

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