| 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 |
* @return array<string> |
| 73 |
*/ |
| 74 |
public static function get_conditionals() { |
| 75 |
return [ Admin_Conditional::class ]; |
| 76 |
} |
| 77 |
|
| 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 |
/** |
| 90 |
* Registers an action to add a new tab to the General page. |
| 91 |
* |
| 92 |
* @return void |
| 93 |
*/ |
| 94 |
public function register_hooks() { |
| 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' ] ); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Enqueue the workouts app. |
| 103 |
* |
| 104 |
* @return void |
| 105 |
*/ |
| 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' ); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Connects the settings to their labels. |
| 120 |
* |
| 121 |
* @return void |
| 122 |
*/ |
| 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 |
]; |
| 136 |
|
| 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 |
]; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Adds content to the Crawl Cleanup network tab. |
| 169 |
* |
| 170 |
* @param Yoast_Form $yform The yoast form object. |
| 171 |
* |
| 172 |
* @return void |
| 173 |
*/ |
| 174 |
public function add_crawl_settings_tab_content_network( $yform ) { |
| 175 |
$this->add_crawl_settings( $yform ); |
| 176 |
} |
| 177 |
|
| 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>', |
| 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' ); |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Prints a list of toggles for an array of settings with labels. |
| 220 |
* |
| 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 |
| 228 |
*/ |
| 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 |
} |
| 233 |
|
| 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' ) ) |
| 310 |
); |
| 311 |
} |
| 312 |
|
| 313 |
/** |
| 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 |
| 319 |
*/ |
| 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 |
); |
| 325 |
} |
| 326 |
} |
| 327 |
|