← All changes
|
src/integrations/admin/crawl-settings-integration.php
+258
-80
19.0
→
trunk
View file →
| @@ -1,16 +1,14 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Yoast\WP\SEO\Integrations\Admin; |
| 4 | 4 | |
| 5 | -use WPSEO_Admin_Utils; | |
| 5 | +use WPSEO_Admin_Asset_Manager; | |
| 6 | 6 | use WPSEO_Option; |
| 7 | -use WPSEO_Option_Tab; | |
| 8 | -use WPSEO_Option_Tabs; | |
| 9 | 7 | use WPSEO_Shortlinker; |
| 10 | 8 | use Yoast\WP\SEO\Conditionals\Admin_Conditional; |
| 11 | -use Yoast\WP\SEO\Helpers\Product_Helper; | |
| 12 | 9 | use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 10 | +use Yoast\WP\SEO\Presenters\Admin\Alert_Presenter; | |
| 13 | 11 | use Yoast_Form; |
| 14 | 12 | |
| 15 | 13 | /** |
| 16 | 14 | * Crawl_Settings_Integration class |
| @@ -17,29 +15,62 @@ | ||
| 17 | 15 | */ |
| 18 | 16 | class Crawl_Settings_Integration implements Integration_Interface { |
| 19 | 17 | |
| 20 | 18 | /** |
| 21 | - * The product helper. | |
| 19 | + * The admin asset manager. | |
| 22 | 20 | * |
| 23 | - * @var Product_Helper | |
| 21 | + * @var WPSEO_Admin_Asset_Manager | |
| 24 | 22 | */ |
| 25 | - private $product_helper; | |
| 23 | + private $admin_asset_manager; | |
| 26 | 24 | |
| 27 | 25 | /** |
| 28 | - * Crawl_Settings_Integration constructor. | |
| 26 | + * Holds the settings + labels for the head clean up piece. | |
| 29 | 27 | * |
| 30 | - * @param Product_Helper $product_helper The product helper. | |
| 28 | + * @var array | |
| 31 | 29 | */ |
| 32 | - public function __construct( | |
| 33 | - Product_Helper $product_helper | |
| 34 | - ) { | |
| 35 | - $this->product_helper = $product_helper; | |
| 36 | - } | |
| 30 | + private $basic_settings; | |
| 37 | 31 | |
| 38 | 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 | + /** | |
| 39 | 68 | * Returns the conditionals based in which this loadable should be active. |
| 40 | 69 | * |
| 41 | 70 | * In this case: when on an admin page. |
| 71 | + * | |
| 72 | + * @return array<string> | |
| 42 | 73 | */ |
| 43 | 74 | public static function get_conditionals() { |
| 44 | 75 | return [ Admin_Conditional::class ]; |
| 45 | 76 | } |
| @@ -44,105 +75,252 @@ | ||
| 44 | 75 | return [ Admin_Conditional::class ]; |
| 45 | 76 | } |
| 46 | 77 | |
| 47 | 78 | /** |
| 79 | + * Crawl_Settings_Integration constructor. | |
| 80 | + * | |
| 81 | + * @param WPSEO_Admin_Asset_Manager $admin_asset_manager The admin asset manager. | |
| 82 | + * @param WPSEO_Shortlinker $shortlinker The shortlinker. | |
| 83 | + */ | |
| 84 | + public function __construct( WPSEO_Admin_Asset_Manager $admin_asset_manager, WPSEO_Shortlinker $shortlinker ) { | |
| 85 | + $this->admin_asset_manager = $admin_asset_manager; | |
| 86 | + $this->shortlinker = $shortlinker; | |
| 87 | + } | |
| 88 | + | |
| 89 | + /** | |
| 48 | 90 | * Registers an action to add a new tab to the General page. |
| 91 | + * | |
| 92 | + * @return void | |
| 49 | 93 | */ |
| 50 | 94 | public function register_hooks() { |
| 51 | - \add_action( 'wpseo_settings_tabs_dashboard', [ $this, 'add_crawl_settings_tab' ] ); | |
| 52 | - if ( ! $this->product_helper->is_premium() || ! $this->is_premium_upgraded() ) { | |
| 53 | - \add_action( 'wpseo_settings_tab_crawl_cleanup', [ $this, 'add_crawl_settings_tab_content' ] ); | |
| 54 | - \add_action( 'wpseo_settings_tab_crawl_cleanup_network', [ $this, 'add_crawl_settings_tab_content_network' ] ); | |
| 55 | - } | |
| 95 | + $this->register_setting_labels(); | |
| 96 | + | |
| 97 | + \add_action( 'wpseo_settings_tab_crawl_cleanup_network', [ $this, 'add_crawl_settings_tab_content_network' ] ); | |
| 98 | + \add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] ); | |
| 56 | 99 | } |
| 57 | 100 | |
| 58 | 101 | /** |
| 59 | - * Checks if Premium is installed and upgraded to the right version. | |
| 102 | + * Enqueue the workouts app. | |
| 60 | 103 | * |
| 61 | - * @return bool Whether Premium is installed and upgraded to the right version. | |
| 104 | + * @return void | |
| 62 | 105 | */ |
| 63 | - public function is_premium_upgraded() { | |
| 64 | - $premium_version = $this->product_helper->get_premium_version(); | |
| 65 | - return $premium_version !== null && \version_compare( $premium_version, '18.6-RC1', '>=' ); | |
| 106 | + public function enqueue_assets() { | |
| 107 | + if ( ! \is_network_admin() ) { | |
| 108 | + return; | |
| 109 | + } | |
| 110 | + | |
| 111 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Page is not processed or saved. | |
| 112 | + if ( ! isset( $_GET['page'] ) || $_GET['page'] !== 'wpseo_dashboard' ) { | |
| 113 | + return; | |
| 114 | + } | |
| 115 | + $this->admin_asset_manager->enqueue_script( 'crawl-settings' ); | |
| 66 | 116 | } |
| 67 | 117 | |
| 68 | 118 | /** |
| 69 | - * Adds a dedicated tab in the General sub-page. | |
| 119 | + * Connects the settings to their labels. | |
| 70 | 120 | * |
| 71 | - * @param WPSEO_Option_Tabs $dashboard_tabs Object representing the tabs of the General sub-page. | |
| 121 | + * @return void | |
| 72 | 122 | */ |
| 73 | - public function add_crawl_settings_tab( $dashboard_tabs ) { | |
| 74 | - $premium = $this->product_helper->is_premium() && $this->is_premium_upgraded(); | |
| 123 | + private function register_setting_labels() { | |
| 124 | + $this->feed_settings = [ | |
| 125 | + 'remove_feed_global' => \__( 'Global feed', 'wordpress-seo' ), | |
| 126 | + 'remove_feed_global_comments' => \__( 'Global comment feeds', 'wordpress-seo' ), | |
| 127 | + 'remove_feed_post_comments' => \__( 'Post comments feeds', 'wordpress-seo' ), | |
| 128 | + 'remove_feed_authors' => \__( 'Post authors feeds', 'wordpress-seo' ), | |
| 129 | + 'remove_feed_post_types' => \__( 'Post type feeds', 'wordpress-seo' ), | |
| 130 | + 'remove_feed_categories' => \__( 'Category feeds', 'wordpress-seo' ), | |
| 131 | + 'remove_feed_tags' => \__( 'Tag feeds', 'wordpress-seo' ), | |
| 132 | + 'remove_feed_custom_taxonomies' => \__( 'Custom taxonomy feeds', 'wordpress-seo' ), | |
| 133 | + 'remove_feed_search' => \__( 'Search results feeds', 'wordpress-seo' ), | |
| 134 | + 'remove_atom_rdf_feeds' => \__( 'Atom/RDF feeds', 'wordpress-seo' ), | |
| 135 | + ]; | |
| 75 | 136 | |
| 76 | - $dashboard_tabs->add_tab( | |
| 77 | - new WPSEO_Option_Tab( | |
| 78 | - 'crawl-settings', | |
| 79 | - \__( 'Crawl settings', 'wordpress-seo' ), | |
| 80 | - [ | |
| 81 | - 'save_button' => $premium, | |
| 82 | - 'beta' => $premium, | |
| 83 | - 'premium' => ! $premium, | |
| 84 | - ] | |
| 85 | - ) | |
| 86 | - ); | |
| 137 | + $this->basic_settings = [ | |
| 138 | + 'remove_shortlinks' => \__( 'Shortlinks', 'wordpress-seo' ), | |
| 139 | + 'remove_rest_api_links' => \__( 'REST API links', 'wordpress-seo' ), | |
| 140 | + 'remove_rsd_wlw_links' => \__( 'RSD / WLW links', 'wordpress-seo' ), | |
| 141 | + 'remove_oembed_links' => \__( 'oEmbed links', 'wordpress-seo' ), | |
| 142 | + 'remove_generator' => \__( 'Generator tag', 'wordpress-seo' ), | |
| 143 | + 'remove_pingback_header' => \__( 'Pingback HTTP header', 'wordpress-seo' ), | |
| 144 | + 'remove_powered_by_header' => \__( 'Powered by HTTP header', 'wordpress-seo' ), | |
| 145 | + ]; | |
| 146 | + | |
| 147 | + $this->permalink_cleanup_settings = [ | |
| 148 | + 'clean_campaign_tracking_urls' => \__( 'Campaign tracking URL parameters', 'wordpress-seo' ), | |
| 149 | + 'clean_permalinks' => \__( 'Unregistered URL parameters', 'wordpress-seo' ), | |
| 150 | + ]; | |
| 151 | + | |
| 152 | + $this->search_cleanup_settings = [ | |
| 153 | + 'search_cleanup' => \__( 'Filter search terms', 'wordpress-seo' ), | |
| 154 | + 'search_cleanup_emoji' => \__( 'Filter searches with emojis and other special characters', 'wordpress-seo' ), | |
| 155 | + 'search_cleanup_patterns' => \__( 'Filter searches with common spam patterns', 'wordpress-seo' ), | |
| 156 | + 'deny_search_crawling' => \__( 'Prevent search engines from crawling site search URLs', 'wordpress-seo' ), | |
| 157 | + 'redirect_search_pretty_urls' => \__( 'Redirect pretty URLs for search pages to raw format', 'wordpress-seo' ), | |
| 158 | + ]; | |
| 159 | + | |
| 160 | + $this->unused_resources_settings = [ | |
| 161 | + 'remove_emoji_scripts' => \__( 'Emoji scripts', 'wordpress-seo' ), | |
| 162 | + 'deny_wp_json_crawling' => \__( 'Prevent search engines from crawling /wp-json/', 'wordpress-seo' ), | |
| 163 | + 'deny_adsbot_crawling' => \__( 'Prevent Google AdsBot from crawling', 'wordpress-seo' ), | |
| 164 | + ]; | |
| 87 | 165 | } |
| 88 | 166 | |
| 89 | 167 | /** |
| 90 | - * Adds content to the Crawl Cleanup tab. | |
| 168 | + * Adds content to the Crawl Cleanup network tab. | |
| 91 | 169 | * |
| 92 | 170 | * @param Yoast_Form $yform The yoast form object. |
| 171 | + * | |
| 172 | + * @return void | |
| 93 | 173 | */ |
| 94 | - public function add_crawl_settings_tab_content( $yform ) { | |
| 95 | - $this->display_premium_upsell_btn(); | |
| 174 | + public function add_crawl_settings_tab_content_network( $yform ) { | |
| 175 | + $this->add_crawl_settings( $yform ); | |
| 176 | + } | |
| 96 | 177 | |
| 97 | - $yform->toggle_switch( | |
| 98 | - 'remove_feed_post_comments_free', | |
| 99 | - [ | |
| 100 | - 'off' => __( 'Keep', 'wordpress-seo' ), | |
| 101 | - 'on' => __( 'Remove', 'wordpress-seo' ), | |
| 102 | - ], | |
| 103 | - __( 'Post comment feeds', 'wordpress-seo' ), | |
| 104 | - '', | |
| 105 | - [ | |
| 106 | - 'disabled' => true, | |
| 107 | - ] | |
| 178 | + /** | |
| 179 | + * Print the settings sections. | |
| 180 | + * | |
| 181 | + * @param Yoast_Form $yform The Yoast form class. | |
| 182 | + * | |
| 183 | + * @return void | |
| 184 | + */ | |
| 185 | + private function add_crawl_settings( $yform ) { | |
| 186 | + $this->print_toggles( $this->basic_settings, $yform, \__( 'Basic crawl settings', 'wordpress-seo' ) ); | |
| 187 | + | |
| 188 | + $this->print_toggles( $this->feed_settings, $yform, \__( 'Feed crawl settings', 'wordpress-seo' ) ); | |
| 189 | + $this->print_toggles( $this->unused_resources_settings, $yform, \__( 'Remove unused resources', 'wordpress-seo' ) ); | |
| 190 | + | |
| 191 | + $first_search_setting = \array_slice( $this->search_cleanup_settings, 0, 1 ); | |
| 192 | + $rest_search_settings = \array_slice( $this->search_cleanup_settings, 1 ); | |
| 193 | + $search_settings_toggles = [ | |
| 194 | + 'off' => \__( 'Disabled', 'wordpress-seo' ), | |
| 195 | + 'on' => \__( 'Enabled', 'wordpress-seo' ), | |
| 196 | + ]; | |
| 197 | + | |
| 198 | + $this->print_toggles( $first_search_setting, $yform, \__( 'Search cleanup settings', 'wordpress-seo' ), $search_settings_toggles ); | |
| 199 | + | |
| 200 | + $this->print_toggles( $rest_search_settings, $yform, '', $search_settings_toggles ); | |
| 201 | + | |
| 202 | + $permalink_warning = \sprintf( | |
| 203 | + /* 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. */ | |
| 204 | + \esc_html__( | |
| 205 | + '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.', | |
| 206 | + 'wordpress-seo', | |
| 207 | + ), | |
| 208 | + '<a href="' . \esc_url( $this->shortlinker->build_shortlink( 'https://yoa.st/permalink-cleanup' ) ) . '" target="_blank" rel="noopener noreferrer">', | |
| 209 | + '</a>', | |
| 108 | 210 | ); |
| 211 | + | |
| 212 | + $this->print_toggles( $this->permalink_cleanup_settings, $yform, \__( 'Permalink cleanup settings', 'wordpress-seo' ), [], $permalink_warning ); | |
| 213 | + | |
| 214 | + // Add the original option as hidden, so as not to lose any values if it's disabled and the form is saved. | |
| 215 | + $yform->hidden( 'clean_permalinks_extra_variables', 'clean_permalinks_extra_variables' ); | |
| 109 | 216 | } |
| 110 | 217 | |
| 111 | 218 | /** |
| 112 | - * Adds content to the Crawl Cleanup network tab. | |
| 219 | + * Prints a list of toggles for an array of settings with labels. | |
| 113 | 220 | * |
| 114 | - * @param Yoast_Form $yform The yoast form object. | |
| 221 | + * @param array $settings The settings being displayed. | |
| 222 | + * @param Yoast_Form $yform The Yoast form class. | |
| 223 | + * @param string $title Optional title for the settings being displayed. | |
| 224 | + * @param array $toggles Optional naming of the toggle buttons. | |
| 225 | + * @param string $warning Optional warning to be displayed above the toggles. | |
| 226 | + * | |
| 227 | + * @return void | |
| 115 | 228 | */ |
| 116 | - public function add_crawl_settings_tab_content_network( $yform ) { | |
| 117 | - $this->display_premium_upsell_btn(); | |
| 229 | + private function print_toggles( array $settings, Yoast_Form $yform, $title = '', $toggles = [], $warning = '' ) { | |
| 230 | + if ( ! empty( $title ) ) { | |
| 231 | + echo '<h3 class="yoast-crawl-settings">', \esc_html( $title ), '</h3>'; | |
| 232 | + } | |
| 118 | 233 | |
| 119 | - $yform->toggle_switch( | |
| 120 | - WPSEO_Option::ALLOW_KEY_PREFIX . 'remove_feed_post_comments_free', | |
| 121 | - [ | |
| 122 | - 'on' => __( 'Allow Control', 'wordpress-seo' ), | |
| 123 | - 'off' => __( 'Disable', 'wordpress-seo' ), | |
| 124 | - ], | |
| 125 | - __( 'Post comment feeds', 'wordpress-seo' ), | |
| 126 | - '', | |
| 127 | - [ | |
| 128 | - 'disabled' => true, | |
| 129 | - ] | |
| 234 | + if ( ! empty( $warning ) ) { | |
| 235 | + // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output escaped in Alert_Presenter. | |
| 236 | + echo new Alert_Presenter( $warning, 'warning' ); | |
| 237 | + } | |
| 238 | + | |
| 239 | + if ( empty( $toggles ) ) { | |
| 240 | + $toggles = [ | |
| 241 | + 'off' => \__( 'Keep', 'wordpress-seo' ), | |
| 242 | + 'on' => \__( 'Remove', 'wordpress-seo' ), | |
| 243 | + ]; | |
| 244 | + } | |
| 245 | + | |
| 246 | + $setting_prefix = WPSEO_Option::ALLOW_KEY_PREFIX; | |
| 247 | + $toggles = [ | |
| 248 | + // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch -- Reason: text is originally from Yoast SEO. | |
| 249 | + 'on' => \__( 'Allow Control', 'wordpress-seo' ), | |
| 250 | + // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch -- Reason: text is originally from Yoast SEO. | |
| 251 | + 'off' => \__( 'Disable', 'wordpress-seo' ), | |
| 252 | + ]; | |
| 253 | + | |
| 254 | + foreach ( $settings as $setting => $label ) { | |
| 255 | + $attr = []; | |
| 256 | + $variable = $setting_prefix . $setting; | |
| 257 | + | |
| 258 | + if ( $this->should_feature_be_disabled_permalink( $setting ) ) { | |
| 259 | + $attr = [ | |
| 260 | + 'disabled' => true, | |
| 261 | + ]; | |
| 262 | + $variable = $setting_prefix . $setting . '_disabled'; | |
| 263 | + | |
| 264 | + // Also add the original option as hidden, so as not to lose any values if it's disabled and the form is saved. | |
| 265 | + $yform->hidden( $setting_prefix . $setting, $setting_prefix . $setting ); | |
| 266 | + } | |
| 267 | + elseif ( $this->should_feature_be_disabled_multisite( $setting ) ) { | |
| 268 | + $attr = [ | |
| 269 | + 'disabled' => true, | |
| 270 | + 'preserve_disabled_value' => false, | |
| 271 | + ]; | |
| 272 | + } | |
| 273 | + | |
| 274 | + $yform->toggle_switch( | |
| 275 | + $variable, | |
| 276 | + $toggles, | |
| 277 | + $label, | |
| 278 | + '', | |
| 279 | + $attr, | |
| 280 | + ); | |
| 281 | + if ( $this->should_feature_be_disabled_permalink( $setting ) ) { | |
| 282 | + echo '<p class="yoast-crawl-settings-help">'; | |
| 283 | + if ( \current_user_can( 'manage_options' ) ) { | |
| 284 | + \printf( | |
| 285 | + /* translators: 1: Link start tag to the Permalinks settings page, 2: Link closing tag. */ | |
| 286 | + \esc_html__( 'This feature is disabled when your site is not using %1$spretty permalinks%2$s.', 'wordpress-seo' ), | |
| 287 | + '<a href="' . \esc_url( \admin_url( 'options-permalink.php' ) ) . '">', | |
| 288 | + '</a>', | |
| 289 | + ); | |
| 290 | + } | |
| 291 | + else { | |
| 292 | + echo \esc_html__( 'This feature is disabled when your site is not using pretty permalinks.', 'wordpress-seo' ); | |
| 293 | + } | |
| 294 | + echo '</p>'; | |
| 295 | + } | |
| 296 | + } | |
| 297 | + } | |
| 298 | + | |
| 299 | + /** | |
| 300 | + * Checks if the feature should be disabled due to non-pretty permalinks. | |
| 301 | + * | |
| 302 | + * @param string $setting The setting to be displayed. | |
| 303 | + * | |
| 304 | + * @return bool | |
| 305 | + */ | |
| 306 | + protected function should_feature_be_disabled_permalink( $setting ) { | |
| 307 | + return ( | |
| 308 | + \in_array( $setting, [ 'clean_permalinks', 'clean_campaign_tracking_urls' ], true ) | |
| 309 | + && empty( \get_option( 'permalink_structure' ) ) | |
| 130 | 310 | ); |
| 131 | 311 | } |
| 132 | 312 | |
| 133 | 313 | /** |
| 134 | - * Displays the Premium upsell button. | |
| 314 | + * Checks if the feature should be disabled due to the site being a multisite. | |
| 315 | + * | |
| 316 | + * @param string $setting The setting to be displayed. | |
| 317 | + * | |
| 318 | + * @return bool | |
| 135 | 319 | */ |
| 136 | - public function display_premium_upsell_btn() { | |
| 137 | - echo '<a class="yoast-button-upsell" href="'; | |
| 138 | - echo \esc_url( WPSEO_Shortlinker::get( 'http://yoa.st/crawl-settings-upsell' ) ); | |
| 139 | - echo '" target="_blank" style=" margin-top: 16px; margin-bottom: 16px; ">'; | |
| 140 | - | |
| 141 | - $button_msg = ( $this->product_helper->is_premium() && ! $this->is_premium_upgraded() ) ? \esc_html__( 'Upgrade Premium', 'wordpress-seo' ) : \esc_html__( 'Unlock with Premium', 'wordpress-seo' ); | |
| 142 | - // phpcs:ignore WordPress.Security.EscapeOutput -- Already escaped. | |
| 143 | - echo $button_msg | |
| 144 | - // phpcs:ignore WordPress.Security.EscapeOutput -- Already escapes correctly. | |
| 145 | - . WPSEO_Admin_Utils::get_new_tab_message(); | |
| 146 | - echo '<span aria-hidden="true" class="yoast-button-upsell__caret"></span></a>'; | |
| 320 | + protected function should_feature_be_disabled_multisite( $setting ) { | |
| 321 | + return ( | |
| 322 | + \in_array( $setting, [ 'deny_search_crawling', 'deny_wp_json_crawling', 'deny_adsbot_crawling' ], true ) | |
| 323 | + && \is_multisite() | |
| 324 | + ); | |
| 147 | 325 | } |
| 148 | 326 | } |