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

https://bitbucket.org/joelkriteman/argento · PHP · 142 lines · 73 code · 25 blank · 44 comment · 16 complexity · 6b2253a452aff060282a32bd638bb5e9 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Frontend
  4. */
  5. if ( !defined('WPSEO_VERSION') ) {
  6. header('HTTP/1.0 403 Forbidden');
  7. die;
  8. }
  9. /**
  10. * This code handles the category rewrites.
  11. */
  12. class WPSEO_Rewrite {
  13. /**
  14. * Class constructor
  15. */
  16. function __construct() {
  17. add_filter( 'query_vars', array( $this, 'query_vars' ) );
  18. add_filter( 'category_link', array( $this, 'no_category_base' ) );
  19. add_filter( 'request', array( $this, 'request' ) );
  20. add_filter( 'category_rewrite_rules', array( $this, 'category_rewrite_rules' ) );
  21. add_action( 'created_category', array( $this, 'schedule_flush' ) );
  22. add_action( 'edited_category', array( $this, 'schedule_flush' ) );
  23. add_action( 'delete_category', array( $this, 'schedule_flush' ) );
  24. add_action( 'init', array( $this, 'flush' ), 999 );
  25. }
  26. /**
  27. * Save an option that triggers a flush on the next init.
  28. *
  29. * @since 1.2.8
  30. */
  31. function schedule_flush() {
  32. update_option( 'wpseo_flush_rewrite', 1 );
  33. }
  34. /**
  35. * If the flush option is set, flush the rewrite rules.
  36. *
  37. * @since 1.2.8
  38. */
  39. function flush() {
  40. if ( get_option( 'wpseo_flush_rewrite' ) ) {
  41. flush_rewrite_rules();
  42. delete_option( 'wpseo_flush_rewrite' );
  43. }
  44. }
  45. /**
  46. * Override the category link to remove the category base.
  47. *
  48. * @param string $link Unused, overridden by the function.
  49. * @return string
  50. */
  51. function no_category_base( $link ) {
  52. $category_base = get_option( 'category_base' );
  53. if ( '' == $category_base )
  54. $category_base = 'category';
  55. // 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)
  56. if ( '/' == substr( $category_base, 0, 1 ) )
  57. $category_base = substr( $category_base, 1 );
  58. $category_base .= '/';
  59. return preg_replace( '`' . preg_quote( $category_base, '`' ) . '`u', '', $link, 1 );
  60. }
  61. /**
  62. * Update the query vars with the redirect var when stripcategorybase is active
  63. *
  64. * @param $query_vars
  65. * @return array
  66. */
  67. function query_vars( $query_vars ) {
  68. $options = get_wpseo_options();
  69. if ( isset( $options['stripcategorybase'] ) && $options['stripcategorybase'] )
  70. $query_vars[] = 'wpseo_category_redirect';
  71. return $query_vars;
  72. }
  73. /**
  74. * Redirect the "old" category URL to the new one.
  75. *
  76. * @param array $query_vars Query vars to check for existence of redirect var
  77. * @return array
  78. */
  79. function request( $query_vars ) {
  80. if ( isset( $query_vars['wpseo_category_redirect'] ) ) {
  81. $catlink = trailingslashit( get_option( 'home' ) ) . user_trailingslashit( $query_vars['wpseo_category_redirect'], 'category' );
  82. wp_redirect( $catlink, 301 );
  83. exit;
  84. }
  85. return $query_vars;
  86. }
  87. /**
  88. * This function taken and only slightly adapted from WP No Category Base plugin by Saurabh Gupta
  89. *
  90. * @return array
  91. */
  92. function category_rewrite_rules() {
  93. global $wp_rewrite;
  94. $category_rewrite = array();
  95. $blog_prefix = '';
  96. if ( function_exists( 'is_multisite' ) && is_multisite() && !is_subdomain_install() && is_main_site() )
  97. $blog_prefix = 'blog/';
  98. foreach ( get_categories( array( 'hide_empty'=> false ) ) as $category ) {
  99. $category_nicename = $category->slug;
  100. if ( $category->parent == $category->cat_ID ) // recursive recursion
  101. $category->parent = 0;
  102. elseif ( $category->parent != 0 )
  103. $category_nicename = get_category_parents( $category->parent, false, '/', true ) . $category_nicename;
  104. $category_rewrite[$blog_prefix . '(' . $category_nicename . ')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
  105. $category_rewrite[$blog_prefix . '(' . $category_nicename . ')/page/?([0-9]{1,})/?$'] = 'index.php?category_name=$matches[1]&paged=$matches[2]';
  106. $category_rewrite[$blog_prefix . '(' . $category_nicename . ')/?$'] = 'index.php?category_name=$matches[1]';
  107. }
  108. // Redirect support from Old Category Base
  109. $old_base = $wp_rewrite->get_category_permastruct();
  110. $old_base = str_replace( '%category%', '(.+)', $old_base );
  111. $old_base = trim( $old_base, '/' );
  112. $category_rewrite[$old_base . '$'] = 'index.php?wpseo_category_redirect=$matches[1]';
  113. return $category_rewrite;
  114. }
  115. }
  116. global $wpseo_rewrite;
  117. $wpseo_rewrite = new WPSEO_Rewrite();