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

Yoast / wordpress-seo / 5025760653

pending completion
5025760653

push

github

aidamarfuaty
Merge branch 'trunk' of github.com:Yoast/wordpress-seo into feature/html-parser

97 of 172 new or added lines in 42 files covered. (56.4%)

9584 of 24000 relevant lines covered (39.93%)

3.32 hits per line

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

9.18
/src/integrations/admin/crawl-settings-integration.php
1
<?php
2

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

5
use WPSEO_Admin_Asset_Manager;
6
use WPSEO_Option;
7
use WPSEO_Shortlinker;
8
use Yoast\WP\SEO\Conditionals\Admin_Conditional;
9
use Yoast\WP\SEO\Integrations\Integration_Interface;
10
use Yoast\WP\SEO\Presenters\Admin\Alert_Presenter;
11
use Yoast_Form;
12

13
/**
14
 * Crawl_Settings_Integration class
15
 */
16
class Crawl_Settings_Integration implements Integration_Interface {
17

18
        /**
19
         * The admin asset manager.
20
         *
21
         * @var WPSEO_Admin_Asset_Manager
22
         */
23
        private $admin_asset_manager;
24

25
        /**
26
         * Holds the settings + labels for the head clean up piece.
27
         *
28
         * @var array
29
         */
30
        private $basic_settings;
31

32
        /**
33
         * Holds the settings + labels for the feeds clean up.
34
         *
35
         * @var array
36
         */
37
        private $feed_settings;
38

39
        /**
40
         * Holds the settings + labels for permalink cleanup settings.
41
         *
42
         * @var array
43
         */
44
        private $permalink_cleanup_settings;
45

46
        /**
47
         * Holds the settings + labels for search cleanup settings.
48
         *
49
         * @var array
50
         */
51
        private $search_cleanup_settings;
52

53
        /**
54
         * Holds the settings + labels for unused resources settings.
55
         *
56
         * @var array
57
         */
58
        private $unused_resources_settings;
59

60
        /**
61
         * The shortlinker.
62
         *
63
         * @var WPSEO_Shortlinker
64
         */
65
        private $shortlinker;
66

67
        /**
68
         * Returns the conditionals based in which this loadable should be active.
69
         *
70
         * In this case: when on an admin page.
71
         */
72
        public static function get_conditionals() {
73
                return [ Admin_Conditional::class ];
74
        }
75

76
        /**
77
         * Crawl_Settings_Integration constructor.
78
         *
79
         * @param WPSEO_Admin_Asset_Manager $admin_asset_manager The admin asset manager.
80
         * @param WPSEO_Shortlinker         $shortlinker         The shortlinker.
81
         */
82
        public function __construct( WPSEO_Admin_Asset_Manager $admin_asset_manager, WPSEO_Shortlinker $shortlinker ) {
83
                $this->admin_asset_manager = $admin_asset_manager;
84
                $this->shortlinker         = $shortlinker;
85
        }
86

87
        /**
88
         * Registers an action to add a new tab to the General page.
89
         */
90
        public function register_hooks() {
91
                $this->register_setting_labels();
92

93
                \add_action( 'wpseo_settings_tab_crawl_cleanup_network', [ $this, 'add_crawl_settings_tab_content_network' ] );
94
                \add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
95
        }
96

97
        /**
98
         * Enqueue the workouts app.
99
         */
100
        public function enqueue_assets() {
101
                if ( ! \is_network_admin() ) {
102
                        return;
103
                }
104

105
                // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Page is not processed or saved.
106
                if ( ! isset( $_GET['page'] ) || $_GET['page'] !== 'wpseo_dashboard' ) {
107
                        return;
108
                }
109
                $this->admin_asset_manager->enqueue_script( 'crawl-settings' );
110
        }
111

112
        /**
113
         * Connects the settings to their labels.
114
         *
115
         * @return void
116
         */
117
        private function register_setting_labels() {
118
                $this->feed_settings = [
119
                        'remove_feed_global'            => \__( 'Global feed', 'wordpress-seo' ),
120
                        'remove_feed_global_comments'   => \__( 'Global comment feeds', 'wordpress-seo' ),
121
                        'remove_feed_post_comments'     => \__( 'Post comments feeds', 'wordpress-seo' ),
122
                        'remove_feed_authors'           => \__( 'Post authors feeds', 'wordpress-seo' ),
123
                        'remove_feed_post_types'        => \__( 'Post type feeds', 'wordpress-seo' ),
124
                        'remove_feed_categories'        => \__( 'Category feeds', 'wordpress-seo' ),
125
                        'remove_feed_tags'              => \__( 'Tag feeds', 'wordpress-seo' ),
126
                        'remove_feed_custom_taxonomies' => \__( 'Custom taxonomy feeds', 'wordpress-seo' ),
127
                        'remove_feed_search'            => \__( 'Search results feeds', 'wordpress-seo' ),
128
                        'remove_atom_rdf_feeds'         => \__( 'Atom/RDF feeds', 'wordpress-seo' ),
129
                ];
130

131
                $this->basic_settings = [
132
                        'remove_shortlinks'        => \__( 'Shortlinks', 'wordpress-seo' ),
133
                        'remove_rest_api_links'    => \__( 'REST API links', 'wordpress-seo' ),
134
                        'remove_rsd_wlw_links'     => \__( 'RSD / WLW links', 'wordpress-seo' ),
135
                        'remove_oembed_links'      => \__( 'oEmbed links', 'wordpress-seo' ),
136
                        'remove_generator'         => \__( 'Generator tag', 'wordpress-seo' ),
137
                        'remove_pingback_header'   => \__( 'Pingback HTTP header', 'wordpress-seo' ),
138
                        'remove_powered_by_header' => \__( 'Powered by HTTP header', 'wordpress-seo' ),
139
                ];
140

141
                $this->permalink_cleanup_settings = [
142
                        'clean_campaign_tracking_urls' => \__( 'Campaign tracking URL parameters', 'wordpress-seo' ),
143
                        'clean_permalinks'             => \__( 'Unregistered URL parameters', 'wordpress-seo' ),
144
                ];
145

146
                $this->search_cleanup_settings = [
147
                        'search_cleanup'              => \__( 'Filter search terms', 'wordpress-seo' ),
148
                        'search_cleanup_emoji'        => \__( 'Filter searches with emojis and other special characters', 'wordpress-seo' ),
149
                        'search_cleanup_patterns'     => \__( 'Filter searches with common spam patterns', 'wordpress-seo' ),
150
                        'deny_search_crawling'        => \__( 'Prevent search engines from crawling site search URLs', 'wordpress-seo' ),
151
                        'redirect_search_pretty_urls' => \__( 'Redirect pretty URLs for search pages to raw format', 'wordpress-seo' ),
152
                ];
153

154
                $this->unused_resources_settings = [
155
                        'remove_emoji_scripts'  => \__( 'Emoji scripts', 'wordpress-seo' ),
156
                        'deny_wp_json_crawling' => \__( 'Prevent search engines from crawling /wp-json/', 'wordpress-seo' ),
157
                ];
158
        }
159

160
        /**
161
         * Adds content to the Crawl Cleanup tab.
162
         *
163
         * @deprecated 20.4
164
         * @codeCoverageIgnore
165
         *
166
         * @param Yoast_Form $yform The yoast form object.
167
         */
168
        public function add_crawl_settings_tab_content( $yform ) {
169
                \_deprecated_function( __METHOD__, 'Yoast SEO 20.4' );
170
                $this->add_crawl_settings( $yform );
171
        }
172

173
        /**
174
         * Adds content to the Crawl Cleanup network tab.
175
         *
176
         * @param Yoast_Form $yform The yoast form object.
177
         */
178
        public function add_crawl_settings_tab_content_network( $yform ) {
NEW
179
                $this->add_crawl_settings( $yform );
180
        }
181

182
        /**
183
         * Print the settings sections.
184
         *
185
         * @param Yoast_Form $yform The Yoast form class.
186
         *
187
         * @return void
188
         */
189
        private function add_crawl_settings( $yform ) {
190
                $this->print_toggles( $this->basic_settings, $yform, \__( 'Basic crawl settings', 'wordpress-seo' ) );
191

192
                $this->print_toggles( $this->feed_settings, $yform, \__( 'Feed crawl settings', 'wordpress-seo' ) );
193
                $this->print_toggles( $this->unused_resources_settings, $yform, \__( 'Remove unused resources', 'wordpress-seo' ) );
194

195
                $first_search_setting    = \array_slice( $this->search_cleanup_settings, 0, 1 );
196
                $rest_search_settings    = \array_slice( $this->search_cleanup_settings, 1 );
197
                $search_settings_toggles = [
198
                        'off' => \__( 'Disabled', 'wordpress-seo' ),
199
                        'on'  => \__( 'Enabled', 'wordpress-seo' ),
200
                ];
201

202
                $this->print_toggles( $first_search_setting, $yform, \__( 'Search cleanup settings', 'wordpress-seo' ), $search_settings_toggles );
203

204
                $this->print_toggles( $rest_search_settings, $yform, '', $search_settings_toggles );
205

206
                $permalink_warning = \sprintf(
207
                /* Translators: %1$s expands to an opening anchor tag for a link leading to the Yoast SEO page of the Permalink Cleanup features, %2$s expands to a closing anchor tag. */
208
                        \esc_html__(
209
                                'These are expert features, so make sure you know what you\'re doing before removing the parameters. %1$sRead more about how your site can be affected%2$s.',
210
                                'wordpress-seo'
211
                        ),
212
                        '<a href="' . \esc_url( $this->shortlinker->build_shortlink( 'https://yoa.st/permalink-cleanup' ) ) . '" target="_blank" rel="noopener noreferrer">',
213
                        '</a>'
214
                );
215

216
                $this->print_toggles( $this->permalink_cleanup_settings, $yform, \__( 'Permalink cleanup settings', 'wordpress-seo' ), [], $permalink_warning );
217

218
                // Add the original option as hidden, so as not to lose any values if it's disabled and the form is saved.
219
                $yform->hidden( 'clean_permalinks_extra_variables', 'clean_permalinks_extra_variables' );
220
        }
221

222
        /**
223
         * Prints a list of toggles for an array of settings with labels.
224
         *
225
         * @param array      $settings The settings being displayed.
226
         * @param Yoast_Form $yform    The Yoast form class.
227
         * @param string     $title    Optional title for the settings being displayed.
228
         * @param array      $toggles  Optional naming of the toggle buttons.
229
         * @param string     $warning  Optional warning to be displayed above the toggles.
230
         *
231
         * @return void
232
         */
233
        private function print_toggles( array $settings, Yoast_Form $yform, $title = '', $toggles = [], $warning = '' ) {
234
                if ( ! empty( $title ) ) {
235
                        echo '<h3 class="yoast-crawl-settings">', \esc_html( $title ), '</h3>';
236
                }
237

238
                if ( ! empty( $warning ) ) {
239
                        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output escaped in Alert_Presenter.
240
                        echo new Alert_Presenter( $warning, 'warning' );
241
                }
242

243
                if ( empty( $toggles ) ) {
244
                        $toggles = [
245
                                'off' => \__( 'Keep', 'wordpress-seo' ),
246
                                'on'  => \__( 'Remove', 'wordpress-seo' ),
247
                        ];
248
                }
249

250
                $setting_prefix = WPSEO_Option::ALLOW_KEY_PREFIX;
251
                $toggles        = [
252
                        // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch -- Reason: text is originally from Yoast SEO.
253
                        'on'  => \__( 'Allow Control', 'wordpress-seo' ),
254
                        // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch -- Reason: text is originally from Yoast SEO.
255
                        'off' => \__( 'Disable', 'wordpress-seo' ),
256
                ];
257

258
                foreach ( $settings as $setting => $label ) {
259
                        $attr     = [];
260
                        $variable = $setting_prefix . $setting;
261

262
                        if ( $this->should_feature_be_disabled_permalink( $setting ) ) {
263
                                $attr     = [
264
                                        'disabled' => true,
265
                                ];
266
                                $variable = $setting_prefix . $setting . '_disabled';
267

268
                                // Also add the original option as hidden, so as not to lose any values if it's disabled and the form is saved.
269
                                $yform->hidden( $setting_prefix . $setting, $setting_prefix . $setting );
270
                        }
271
                        elseif ( $this->should_feature_be_disabled_multisite( $setting ) ) {
272
                                $attr = [
273
                                        'disabled'                => true,
274
                                        'preserve_disabled_value' => false,
275
                                ];
276
                        }
277

278
                        $yform->toggle_switch(
279
                                $variable,
280
                                $toggles,
281
                                $label,
282
                                '',
283
                                $attr
284
                        );
285
                        if ( $this->should_feature_be_disabled_permalink( $setting ) ) {
286
                                echo '<p class="yoast-crawl-settings-help">';
287
                                if ( \current_user_can( 'manage_options' ) ) {
288
                                        echo \sprintf(
289
                                        /* translators: 1: Link start tag to the Permalinks settings page, 2: Link closing tag. */
290
                                                \esc_html__( 'This feature is disabled when your site is not using %1$spretty permalinks%2$s.', 'wordpress-seo' ),
291
                                                '<a href="' . \esc_url( \admin_url( 'options-permalink.php' ) ) . '">',
292
                                                '</a>'
293
                                        );
294
                                }
295
                                else {
296
                                        echo \esc_html__( 'This feature is disabled when your site is not using pretty permalinks.', 'wordpress-seo' );
297
                                }
298
                                echo '</p>';
299
                        }
300
                }
301
        }
302

303
        /**
304
         * Checks if the feature should be disabled due to non-pretty permalinks.
305
         *
306
         * @param string $setting The setting to be displayed.
307
         *
308
         * @return bool
309
         */
310
        protected function should_feature_be_disabled_permalink( $setting ) {
311
                return (
312
                        \in_array( $setting, [ 'clean_permalinks', 'clean_campaign_tracking_urls' ], true )
313
                        && empty( \get_option( 'permalink_structure' ) )
314
                );
315
        }
316

317
        /**
318
         * Checks if the feature should be disabled due to the site being a multisite.
319
         *
320
         * @param string $setting The setting to be displayed.
321
         *
322
         * @return bool
323
         */
324
        protected function should_feature_be_disabled_multisite( $setting ) {
325
                return (
326
                        \in_array( $setting, [ 'deny_search_crawling', 'deny_wp_json_crawling' ], true )
327
                        && \is_multisite()
328
                );
329
        }
330
}
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