• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / wordpress-seo / dd6e866a9e6d253114633104d9e3858d807178ba

19 Jun 2024 10:03AM UTC coverage: 48.628% (-4.3%) from 52.936%
dd6e866a9e6d253114633104d9e3858d807178ba

push

github

web-flow
Merge pull request #21431 from Yoast/21429-update-copy-in-the-introduction-and-consent-modals

Updates the copy for the introduction and consent modals

7441 of 13454 branches covered (55.31%)

Branch coverage included in aggregate %.

0 of 3 new or added lines in 2 files covered. (0.0%)

3718 existing lines in 107 files now uncovered.

25100 of 53464 relevant lines covered (46.95%)

62392.47 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/inc/class-rewrite.php
1
<?php
2
/**
3
 * WPSEO plugin file.
4
 *
5
 * @package WPSEO\Frontend
6
 */
7

8
/**
9
 * This code handles the category rewrites.
10
 */
11
class WPSEO_Rewrite {
12

13
        /**
14
         * Class constructor.
15
         */
16
        public function __construct() {
×
17
                add_filter( 'query_vars', [ $this, 'query_vars' ] );
×
18
                add_filter( 'term_link', [ $this, 'no_category_base' ], 10, 3 );
×
19
                add_filter( 'request', [ $this, 'request' ] );
×
20
                add_filter( 'category_rewrite_rules', [ $this, 'category_rewrite_rules' ] );
×
21

22
                add_action( 'created_category', [ $this, 'schedule_flush' ] );
×
23
                add_action( 'edited_category', [ $this, 'schedule_flush' ] );
×
24
                add_action( 'delete_category', [ $this, 'schedule_flush' ] );
×
25
        }
26

27
        /**
28
         * Trigger a rewrite_rule flush on shutdown.
29
         *
30
         * @since 1.2.8
31
         *
32
         * @return void
33
         */
UNCOV
34
        public function schedule_flush() {
×
UNCOV
35
                add_action( 'shutdown', 'flush_rewrite_rules' );
×
36
        }
37

38
        /**
39
         * Override the category link to remove the category base.
40
         *
41
         * @param string  $link     Term link, overridden by the function for categories.
42
         * @param WP_Term $term     Unused, term object.
43
         * @param string  $taxonomy Taxonomy slug.
44
         *
45
         * @return string
46
         */
UNCOV
47
        public function no_category_base( $link, $term, $taxonomy ) {
×
UNCOV
48
                if ( $taxonomy !== 'category' ) {
×
49
                        return $link;
×
50
                }
51

UNCOV
52
                $category_base = get_option( 'category_base' );
×
53

UNCOV
54
                if ( empty( $category_base ) ) {
×
UNCOV
55
                        $category_base = 'category';
×
56
                }
57

58
                /*
59
                 * Remove initial slash, if there is one (we remove the trailing slash
60
                 * in the regex replacement and don't want to end up short a slash).
61
                 */
UNCOV
62
                if ( substr( $category_base, 0, 1 ) === '/' ) {
×
63
                        $category_base = substr( $category_base, 1 );
×
64
                }
65

UNCOV
66
                $category_base .= '/';
×
67

UNCOV
68
                return preg_replace( '`' . preg_quote( $category_base, '`' ) . '`u', '', $link, 1 );
×
69
        }
70

71
        /**
72
         * Update the query vars with the redirect var when stripcategorybase is active.
73
         *
74
         * @param array<string> $query_vars Main query vars to filter.
75
         *
76
         * @return array<string> The query vars.
77
         */
UNCOV
78
        public function query_vars( $query_vars ) {
×
UNCOV
79
                if ( WPSEO_Options::get( 'stripcategorybase' ) === true ) {
×
UNCOV
80
                        $query_vars[] = 'wpseo_category_redirect';
×
81
                }
82

UNCOV
83
                return $query_vars;
×
84
        }
85

86
        /**
87
         * Checks whether the redirect needs to be created.
88
         *
89
         * @param array<string> $query_vars Query vars to check for existence of redirect var.
90
         *
91
         * @return array<string> The query vars.
92
         */
UNCOV
93
        public function request( $query_vars ) {
×
UNCOV
94
                if ( ! isset( $query_vars['wpseo_category_redirect'] ) ) {
×
UNCOV
95
                        return $query_vars;
×
96
                }
97

UNCOV
98
                $this->redirect( $query_vars['wpseo_category_redirect'] );
×
UNCOV
99
                return [];
×
100
        }
101

102
        /**
103
         * This function taken and only slightly adapted from WP No Category Base plugin by Saurabh Gupta.
104
         *
105
         * @return array<string> The category rewrite rules.
106
         */
UNCOV
107
        public function category_rewrite_rules() {
×
UNCOV
108
                global $wp_rewrite;
×
109

UNCOV
110
                $category_rewrite = [];
×
111

UNCOV
112
                $taxonomy            = get_taxonomy( 'category' );
×
UNCOV
113
                $permalink_structure = get_option( 'permalink_structure' );
×
114

UNCOV
115
                $blog_prefix = '';
×
UNCOV
116
                if ( strpos( $permalink_structure, '/blog/' ) === 0 ) {
×
117
                        if ( ( is_multisite() && ! is_subdomain_install() ) || is_main_site() || is_main_network() ) {
×
118
                                $blog_prefix = 'blog/';
×
119
                        }
120
                }
121

UNCOV
122
                $categories = get_categories( [ 'hide_empty' => false ] );
×
UNCOV
123
                if ( is_array( $categories ) && $categories !== [] ) {
×
UNCOV
124
                        foreach ( $categories as $category ) {
×
UNCOV
125
                                $category_nicename = $category->slug;
×
UNCOV
126
                                if ( $category->parent === $category->cat_ID ) {
×
127
                                        // Recursive recursion.
128
                                        $category->parent = 0;
×
129
                                }
UNCOV
130
                                elseif ( $taxonomy->rewrite['hierarchical'] !== false && $category->parent !== 0 ) {
×
131
                                                $parents = get_category_parents( $category->parent, false, '/', true );
×
132
                                        if ( ! is_wp_error( $parents ) ) {
×
133
                                                $category_nicename = $parents . $category_nicename;
×
134
                                        }
135
                                        unset( $parents );
×
136
                                }
137

UNCOV
138
                                $category_rewrite = $this->add_category_rewrites( $category_rewrite, $category_nicename, $blog_prefix, $wp_rewrite->pagination_base );
×
139

140
                                // Adds rules for the uppercase encoded URIs.
UNCOV
141
                                $category_nicename_filtered = $this->convert_encoded_to_upper( $category_nicename );
×
142

UNCOV
143
                                if ( $category_nicename_filtered !== $category_nicename ) {
×
144
                                        $category_rewrite = $this->add_category_rewrites( $category_rewrite, $category_nicename_filtered, $blog_prefix, $wp_rewrite->pagination_base );
×
145
                                }
146
                        }
UNCOV
147
                        unset( $categories, $category, $category_nicename, $category_nicename_filtered );
×
148
                }
149

150
                // Redirect support from Old Category Base.
UNCOV
151
                $old_base                            = $wp_rewrite->get_category_permastruct();
×
UNCOV
152
                $old_base                            = str_replace( '%category%', '(.+)', $old_base );
×
UNCOV
153
                $old_base                            = trim( $old_base, '/' );
×
UNCOV
154
                $category_rewrite[ $old_base . '$' ] = 'index.php?wpseo_category_redirect=$matches[1]';
×
155

UNCOV
156
                return $category_rewrite;
×
157
        }
158

159
        /**
160
         * Adds required category rewrites rules.
161
         *
162
         * @param array<string> $rewrites        The current set of rules.
163
         * @param string        $category_name   Category nicename.
164
         * @param string        $blog_prefix     Multisite blog prefix.
165
         * @param string        $pagination_base WP_Query pagination base.
166
         *
167
         * @return array<string> The added set of rules.
168
         */
UNCOV
169
        protected function add_category_rewrites( $rewrites, $category_name, $blog_prefix, $pagination_base ) {
×
UNCOV
170
                $rewrite_name = $blog_prefix . '(' . $category_name . ')';
×
171

UNCOV
172
                global $wp_rewrite;
×
UNCOV
173
                $feed_regex = '(' . implode( '|', $wp_rewrite->feeds ) . ')';
×
174

UNCOV
175
                $rewrites[ $rewrite_name . '/(?:feed/)?' . $feed_regex . '/?$' ]         = 'index.php?category_name=$matches[1]&feed=$matches[2]';
×
UNCOV
176
                $rewrites[ $rewrite_name . '/' . $pagination_base . '/?([0-9]{1,})/?$' ] = 'index.php?category_name=$matches[1]&paged=$matches[2]';
×
UNCOV
177
                $rewrites[ $rewrite_name . '/?$' ]                                       = 'index.php?category_name=$matches[1]';
×
178

UNCOV
179
                return $rewrites;
×
180
        }
181

182
        /**
183
         * Walks through category nicename and convert encoded parts
184
         * into uppercase using $this->encode_to_upper().
185
         *
186
         * @param string $name The encoded category URI string.
187
         *
188
         * @return string The convered URI string.
189
         */
UNCOV
190
        protected function convert_encoded_to_upper( $name ) {
×
191
                // Checks if name has any encoding in it.
UNCOV
192
                if ( strpos( $name, '%' ) === false ) {
×
UNCOV
193
                        return $name;
×
194
                }
195

196
                $names = explode( '/', $name );
×
197
                $names = array_map( [ $this, 'encode_to_upper' ], $names );
×
198

199
                return implode( '/', $names );
×
200
        }
201

202
        /**
203
         * Converts the encoded URI string to uppercase.
204
         *
205
         * @param string $encoded The encoded string.
206
         *
207
         * @return string The uppercased string.
208
         */
209
        public function encode_to_upper( $encoded ) {
×
210
                if ( strpos( $encoded, '%' ) === false ) {
×
211
                        return $encoded;
×
212
                }
213

214
                return strtoupper( $encoded );
×
215
        }
216

217
        /**
218
         * Redirect the "old" category URL to the new one.
219
         *
220
         * @codeCoverageIgnore
221
         *
222
         * @param string $category_redirect The category page to redirect to.
223
         * @return void
224
         */
225
        protected function redirect( $category_redirect ) {
226
                $catlink = trailingslashit( get_option( 'home' ) ) . user_trailingslashit( $category_redirect, 'category' );
227

228
                wp_safe_redirect( $catlink, 301, 'Yoast SEO' );
229
                exit;
230
        }
231
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc