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

Yoast / wordpress-seo / 6987097851

25 Nov 2023 04:49AM UTC coverage: 49.206% (-0.1%) from 49.302%
6987097851

push

github

web-flow
Merge pull request #20878 from Yoast/JRF/ghactions-minor-tweak

GH Actions: update a few links in inline comments

15305 of 31104 relevant lines covered (49.21%)

4.03 hits per line

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

71.93
/src/integrations/front-end/redirects.php
1
<?php
2

3
namespace Yoast\WP\SEO\Integrations\Front_End;
4

5
use Yoast\WP\SEO\Conditionals\Front_End_Conditional;
6
use Yoast\WP\SEO\Helpers\Current_Page_Helper;
7
use Yoast\WP\SEO\Helpers\Meta_Helper;
8
use Yoast\WP\SEO\Helpers\Options_Helper;
9
use Yoast\WP\SEO\Helpers\Redirect_Helper;
10
use Yoast\WP\SEO\Helpers\Url_Helper;
11
use Yoast\WP\SEO\Integrations\Integration_Interface;
12

13
/**
14
 * Class Redirects.
15
 */
16
class Redirects implements Integration_Interface {
17

18
        /**
19
         * The options helper.
20
         *
21
         * @var Options_Helper
22
         */
23
        protected $options;
24

25
        /**
26
         * The meta helper.
27
         *
28
         * @var Meta_Helper
29
         */
30
        protected $meta;
31

32
        /**
33
         * The current page helper.
34
         *
35
         * @var Current_Page_Helper
36
         */
37
        protected $current_page;
38

39
        /**
40
         * The redirect helper.
41
         *
42
         * @var Redirect_Helper
43
         */
44
        private $redirect;
45

46
        /**
47
         * The URL helper.
48
         *
49
         * @var Url_Helper
50
         */
51
        private $url;
52

53
        /**
54
         * Holds the WP_Query variables we should get rid of.
55
         *
56
         * @var string[]
57
         */
58
        private $date_query_variables = [
59
                'year',
60
                'm',
61
                'monthnum',
62
                'day',
63
                'hour',
64
                'minute',
65
                'second',
66
        ];
67

68
        /**
69
         * Sets the helpers.
70
         *
71
         * @codeCoverageIgnore
72
         *
73
         * @param Options_Helper      $options      Options helper.
74
         * @param Meta_Helper         $meta         Meta helper.
75
         * @param Current_Page_Helper $current_page The current page helper.
76
         * @param Redirect_Helper     $redirect     The redirect helper.
77
         * @param Url_Helper          $url          The URL helper.
78
         */
79
        public function __construct( Options_Helper $options, Meta_Helper $meta, Current_Page_Helper $current_page, Redirect_Helper $redirect, Url_Helper $url ) {
80
                $this->options      = $options;
81
                $this->meta         = $meta;
82
                $this->current_page = $current_page;
83
                $this->redirect     = $redirect;
84
                $this->url          = $url;
85
        }
86

87
        /**
88
         * Returns the conditionals based in which this loadable should be active.
89
         *
90
         * @return array
91
         */
92
        public static function get_conditionals() {
2✔
93
                return [ Front_End_Conditional::class ];
2✔
94
        }
95

96
        /**
97
         * Initializes the integration.
98
         *
99
         * This is the place to register hooks and filters.
100
         *
101
         * @return void
102
         */
103
        public function register_hooks() {
2✔
104
                \add_action( 'wp', [ $this, 'archive_redirect' ] );
2✔
105
                \add_action( 'wp', [ $this, 'page_redirect' ], 99 );
2✔
106
                \add_action( 'wp', [ $this, 'category_redirect' ] );
2✔
107
                \add_action( 'template_redirect', [ $this, 'attachment_redirect' ], 1 );
2✔
108
                \add_action( 'template_redirect', [ $this, 'disable_date_queries' ] );
2✔
109
        }
1✔
110

111
        /**
112
         * Disable date queries, if they're disabled in Yoast SEO settings, to prevent indexing the wrong things.
113
         *
114
         * @return void
115
         */
116
        public function disable_date_queries() {
×
117
                if ( $this->options->get( 'disable-date', false ) ) {
×
118
                        $exploded_url                    = \explode( '?', $this->url->recreate_current_url(), 2 );
×
119
                        list( $base_url, $query_string ) = \array_pad( $exploded_url, 2, '' );
×
120
                        \parse_str( $query_string, $query_vars );
×
121
                        foreach ( $this->date_query_variables as $variable ) {
×
122
                                if ( \in_array( $variable, \array_keys( $query_vars ), true ) ) {
×
123
                                        $this->do_date_redirect( $query_vars, $base_url );
×
124
                                }
125
                        }
126
                }
127
        }
128

129
        /**
130
         * When certain archives are disabled, this redirects those to the homepage.
131
         */
132
        public function archive_redirect() {
4✔
133
                if ( $this->need_archive_redirect() ) {
4✔
134
                        $this->redirect->do_safe_redirect( \get_bloginfo( 'url' ), 301 );
2✔
135
                }
136
        }
2✔
137

138
        /**
139
         * Based on the redirect meta value, this function determines whether it should redirect the current post / page.
140
         */
141
        public function page_redirect() {
8✔
142
                if ( ! $this->current_page->is_simple_page() ) {
8✔
143
                        return;
2✔
144
                }
145

146
                $post = \get_post();
6✔
147
                if ( ! \is_object( $post ) ) {
6✔
148
                        return;
2✔
149
                }
150

151
                $redirect = $this->meta->get_value( 'redirect', $post->ID );
4✔
152
                if ( $redirect === '' ) {
4✔
153
                        return;
2✔
154
                }
155

156
                $this->redirect->do_safe_redirect( $redirect, 301 );
2✔
157
        }
1✔
158

159
        /**
160
         * If the option to disable attachment URLs is checked, this performs the redirect to the attachment.
161
         */
162
        public function attachment_redirect() {
8✔
163
                if ( ! $this->current_page->is_attachment() ) {
8✔
164
                        return;
2✔
165
                }
166

167
                if ( $this->options->get( 'disable-attachment', false ) === false ) {
6✔
168
                        return;
2✔
169
                }
170

171
                $url = $this->get_attachment_url();
4✔
172
                if ( empty( $url ) ) {
4✔
173
                        return;
2✔
174
                }
175

176
                $this->redirect->do_unsafe_redirect( $url, 301 );
2✔
177
        }
1✔
178

179
        /**
180
         * Checks if certain archive pages are disabled to determine if a archive redirect is needed.
181
         *
182
         * @codeCoverageIgnore
183
         *
184
         * @return bool Whether or not to redirect an archive page.
185
         */
186
        protected function need_archive_redirect() {
187
                if ( $this->options->get( 'disable-date', false ) && $this->current_page->is_date_archive() ) {
188
                        return true;
189
                }
190

191
                if ( $this->options->get( 'disable-author', false ) && $this->current_page->is_author_archive() ) {
192
                        return true;
193
                }
194

195
                if ( $this->options->get( 'disable-post_format', false ) && $this->current_page->is_post_format_archive() ) {
196
                        return true;
197
                }
198

199
                return false;
200
        }
201

202
        /**
203
         * Retrieves the attachment url for the current page.
204
         *
205
         * @codeCoverageIgnore It wraps WordPress functions.
206
         *
207
         * @return string The attachment url.
208
         */
209
        protected function get_attachment_url() {
210
                /**
211
                 * Allows the developer to change the target redirection URL for attachments.
212
                 *
213
                 * @api string $attachment_url The attachment URL for the queried object.
214
                 * @api object $queried_object The queried object.
215
                 *
216
                 * @since 7.5.3
217
                 */
218
                return \apply_filters(
219
                        'wpseo_attachment_redirect_url',
220
                        \wp_get_attachment_url( \get_queried_object_id() ),
221
                        \get_queried_object()
222
                );
223
        }
224

225
        /**
226
         * Redirects away query variables that shouldn't work.
227
         *
228
         * @param array  $query_vars The query variables in the current URL.
229
         * @param string $base_url   The base URL without query string.
230
         *
231
         * @return void
232
         */
233
        private function do_date_redirect( $query_vars, $base_url ) {
×
234
                foreach ( $this->date_query_variables as $variable ) {
×
235
                        unset( $query_vars[ $variable ] );
×
236
                }
237
                $url = $base_url;
×
238
                if ( \count( $query_vars ) > 0 ) {
×
239
                        $url .= '?' . \http_build_query( $query_vars );
×
240
                }
241

242
                $this->redirect->do_safe_redirect( $url, 301 );
×
243
        }
244

245
        /**
246
         * Strips `cat=-1` from the URL and redirects to the resulting URL.
247
         */
248
        public function category_redirect() {
4✔
249
                /**
250
                 * Allows the developer to keep cat=-1 GET parameters
251
                 *
252
                 * @since 19.9
253
                 *
254
                 * @param bool $remove_cat_parameter Whether to remove the `cat=-1` GET parameter. Default true.
255
                 */
256
                $should_remove_parameter = \apply_filters( 'wpseo_remove_cat_parameter', true );
4✔
257

258
                // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Data is not processed or saved.
259
                if ( $should_remove_parameter && isset( $_GET['cat'] ) && $_GET['cat'] === '-1' ) {
4✔
260
                        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Data is not processed or saved.
261
                        unset( $_GET['cat'] );
2✔
262
                        if ( isset( $_SERVER['REQUEST_URI'] ) ) {
2✔
263
                                // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- This is just a replace and the data is never saved.
264
                                $_SERVER['REQUEST_URI'] = \remove_query_arg( 'cat' );
×
265
                        }
266
                        $this->redirect->do_safe_redirect( $this->url->recreate_current_url(), 301, 'Stripping cat=-1 from the URL' );
2✔
267
                }
268
        }
2✔
269
}
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

© 2026 Coveralls, Inc