/shop quần áo starloveshop.com/wp-content/plugins/wordpress-seo/inc/class-rewrite.php

https://gitlab.com/phamngsinh/baitaplon_sinhvien · PHP · 169 lines · 89 code · 31 blank · 49 comment · 20 complexity · 0fea8d421b877bddd6070d453f310be8 MD5 · raw file

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