| @@ -16,9 +16,9 @@ | ||
| 16 | 16 | public function __construct() { |
| 17 | 17 | add_filter( 'query_vars', [ $this, 'query_vars' ] ); |
| 18 | 18 | add_filter( 'term_link', [ $this, 'no_category_base' ], 10, 3 ); |
| 19 | 19 | add_filter( 'request', [ $this, 'request' ] ); |
| 20 | - add_filter( 'category_rewrite_rules', [ $this, 'category_rewrite_rules' ] ); | |
| 20 | + add_filter( 'category_rewrite_rules', [ $this, 'category_rewrite_rules_wrapper' ] ); | |
| 21 | 21 | |
| 22 | 22 | add_action( 'created_category', [ $this, 'schedule_flush' ] ); |
| 23 | 23 | add_action( 'edited_category', [ $this, 'schedule_flush' ] ); |
| 24 | 24 | add_action( 'delete_category', [ $this, 'schedule_flush' ] ); |
| @@ -27,33 +27,15 @@ | ||
| 27 | 27 | /** |
| 28 | 28 | * Trigger a rewrite_rule flush on shutdown. |
| 29 | 29 | * |
| 30 | 30 | * @since 1.2.8 |
| 31 | + * | |
| 32 | + * @return void | |
| 31 | 33 | */ |
| 32 | 34 | public function schedule_flush() { |
| 33 | - add_action( 'shutdown', 'flush_rewrite_rules' ); | |
| 34 | - } | |
| 35 | - | |
| 36 | - /** | |
| 37 | - * If the flush option is set, flush the rewrite rules. | |
| 38 | - * | |
| 39 | - * @since 1.2.8 | |
| 40 | - * @deprecated 17.4 | |
| 41 | - * @codeCoverageIgnore | |
| 42 | - * | |
| 43 | - * @return bool | |
| 44 | - */ | |
| 45 | - public function flush() { | |
| 46 | - _deprecated_function( __METHOD__, 'WPSEO 17.4', __CLASS__ . '::schedule_flush' ); | |
| 47 | - if ( get_option( 'wpseo_flush_rewrite' ) ) { | |
| 48 | - | |
| 35 | + if ( WPSEO_Options::get( 'stripcategorybase' ) === true ) { | |
| 49 | 36 | add_action( 'shutdown', 'flush_rewrite_rules' ); |
| 50 | - delete_option( 'wpseo_flush_rewrite' ); | |
| 51 | - | |
| 52 | - return true; | |
| 53 | 37 | } |
| 54 | - | |
| 55 | - return false; | |
| 56 | 38 | } |
| 57 | 39 | |
| 58 | 40 | /** |
| 59 | 41 | * Override the category link to remove the category base. |
| @@ -64,8 +46,12 @@ | ||
| 64 | 46 | * |
| 65 | 47 | * @return string |
| 66 | 48 | */ |
| 67 | 49 | public function no_category_base( $link, $term, $taxonomy ) { |
| 50 | + if ( WPSEO_Options::get( 'stripcategorybase' ) !== true ) { | |
| 51 | + return $link; | |
| 52 | + } | |
| 53 | + | |
| 68 | 54 | if ( $taxonomy !== 'category' ) { |
| 69 | 55 | return $link; |
| 70 | 56 | } |
| 71 | 57 | |
| @@ -90,11 +76,11 @@ | ||
| 90 | 76 | |
| 91 | 77 | /** |
| 92 | 78 | * Update the query vars with the redirect var when stripcategorybase is active. |
| 93 | 79 | * |
| 94 | - * @param array $query_vars Main query vars to filter. | |
| 80 | + * @param array<string> $query_vars Main query vars to filter. | |
| 95 | 81 | * |
| 96 | - * @return array | |
| 82 | + * @return array<string> The query vars. | |
| 97 | 83 | */ |
| 98 | 84 | public function query_vars( $query_vars ) { |
| 99 | 85 | if ( WPSEO_Options::get( 'stripcategorybase' ) === true ) { |
| 100 | 86 | $query_vars[] = 'wpseo_category_redirect'; |
| @@ -105,24 +91,44 @@ | ||
| 105 | 91 | |
| 106 | 92 | /** |
| 107 | 93 | * Checks whether the redirect needs to be created. |
| 108 | 94 | * |
| 109 | - * @param array $query_vars Query vars to check for existence of redirect var. | |
| 95 | + * @param array<string> $query_vars Query vars to check for existence of redirect var. | |
| 110 | 96 | * |
| 111 | - * @return array|void The query vars. | |
| 97 | + * @return array<string> The query vars. | |
| 112 | 98 | */ |
| 113 | 99 | public function request( $query_vars ) { |
| 100 | + if ( WPSEO_Options::get( 'stripcategorybase' ) !== true ) { | |
| 101 | + return $query_vars; | |
| 102 | + } | |
| 103 | + | |
| 114 | 104 | if ( ! isset( $query_vars['wpseo_category_redirect'] ) ) { |
| 115 | 105 | return $query_vars; |
| 116 | 106 | } |
| 117 | 107 | |
| 118 | 108 | $this->redirect( $query_vars['wpseo_category_redirect'] ); |
| 109 | + return []; | |
| 119 | 110 | } |
| 120 | 111 | |
| 121 | 112 | /** |
| 113 | + * Wrapper for the category_rewrite_rules() below, so we can add the $rules param in a BC way. | |
| 114 | + * | |
| 115 | + * @param array<string> $rules Rewrite rules generated for the current permastruct, keyed by their regex pattern. | |
| 116 | + * | |
| 117 | + * @return array<string> The category rewrite rules. | |
| 118 | + */ | |
| 119 | + public function category_rewrite_rules_wrapper( $rules ) { | |
| 120 | + if ( WPSEO_Options::get( 'stripcategorybase' ) !== true ) { | |
| 121 | + return $rules; | |
| 122 | + } | |
| 123 | + | |
| 124 | + return $this->category_rewrite_rules(); | |
| 125 | + } | |
| 126 | + | |
| 127 | + /** | |
| 122 | 128 | * This function taken and only slightly adapted from WP No Category Base plugin by Saurabh Gupta. |
| 123 | 129 | * |
| 124 | - * @return array | |
| 130 | + * @return array<string> The category rewrite rules. | |
| 125 | 131 | */ |
| 126 | 132 | public function category_rewrite_rules() { |
| 127 | 133 | global $wp_rewrite; |
| 128 | 134 | |
| @@ -131,10 +137,12 @@ | ||
| 131 | 137 | $taxonomy = get_taxonomy( 'category' ); |
| 132 | 138 | $permalink_structure = get_option( 'permalink_structure' ); |
| 133 | 139 | |
| 134 | 140 | $blog_prefix = ''; |
| 135 | - if ( is_multisite() && ! is_subdomain_install() && is_main_site() && strpos( $permalink_structure, '/blog/' ) === 0 ) { | |
| 136 | - $blog_prefix = 'blog/'; | |
| 141 | + if ( strpos( $permalink_structure, '/blog/' ) === 0 ) { | |
| 142 | + if ( ( is_multisite() && ! is_subdomain_install() ) || is_main_site() || is_main_network() ) { | |
| 143 | + $blog_prefix = 'blog/'; | |
| 144 | + } | |
| 137 | 145 | } |
| 138 | 146 | |
| 139 | 147 | $categories = get_categories( [ 'hide_empty' => false ] ); |
| 140 | 148 | if ( is_array( $categories ) && $categories !== [] ) { |
| @@ -175,19 +183,22 @@ | ||
| 175 | 183 | |
| 176 | 184 | /** |
| 177 | 185 | * Adds required category rewrites rules. |
| 178 | 186 | * |
| 179 | - * @param array $rewrites The current set of rules. | |
| 180 | - * @param string $category_name Category nicename. | |
| 181 | - * @param string $blog_prefix Multisite blog prefix. | |
| 182 | - * @param string $pagination_base WP_Query pagination base. | |
| 187 | + * @param array<string> $rewrites The current set of rules. | |
| 188 | + * @param string $category_name Category nicename. | |
| 189 | + * @param string $blog_prefix Multisite blog prefix. | |
| 190 | + * @param string $pagination_base WP_Query pagination base. | |
| 183 | 191 | * |
| 184 | - * @return array The added set of rules. | |
| 192 | + * @return array<string> The added set of rules. | |
| 185 | 193 | */ |
| 186 | 194 | protected function add_category_rewrites( $rewrites, $category_name, $blog_prefix, $pagination_base ) { |
| 187 | 195 | $rewrite_name = $blog_prefix . '(' . $category_name . ')'; |
| 188 | 196 | |
| 189 | - $rewrites[ $rewrite_name . '/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$' ] = 'index.php?category_name=$matches[1]&feed=$matches[2]'; | |
| 197 | + global $wp_rewrite; | |
| 198 | + $feed_regex = '(' . implode( '|', $wp_rewrite->feeds ) . ')'; | |
| 199 | + | |
| 200 | + $rewrites[ $rewrite_name . '/(?:feed/)?' . $feed_regex . '/?$' ] = 'index.php?category_name=$matches[1]&feed=$matches[2]'; | |
| 190 | 201 | $rewrites[ $rewrite_name . '/' . $pagination_base . '/?([0-9]{1,})/?$' ] = 'index.php?category_name=$matches[1]&paged=$matches[2]'; |
| 191 | 202 | $rewrites[ $rewrite_name . '/?$' ] = 'index.php?category_name=$matches[1]'; |
| 192 | 203 | |
| 193 | 204 | return $rewrites; |
| @@ -239,7 +250,7 @@ | ||
| 239 | 250 | protected function redirect( $category_redirect ) { |
| 240 | 251 | $catlink = trailingslashit( get_option( 'home' ) ) . user_trailingslashit( $category_redirect, 'category' ); |
| 241 | 252 | |
| 242 | 253 | wp_safe_redirect( $catlink, 301, 'Yoast SEO' ); |
| 243 | - exit; | |
| 254 | + exit(); | |
| 244 | 255 | } |
| 245 | 256 | } |