| 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\Options_Helper; |
| 7 |
use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 8 |
|
| 9 |
/** |
| 10 |
* Class Crawl_Cleanup_Basic. |
| 11 |
*/ |
| 12 |
class Crawl_Cleanup_Basic implements Integration_Interface { |
| 13 |
|
| 14 |
/** |
| 15 |
* The options helper. |
| 16 |
* |
| 17 |
* @var Options_Helper |
| 18 |
*/ |
| 19 |
private $options_helper; |
| 20 |
|
| 21 |
/** |
| 22 |
* Crawl Cleanup Basic integration constructor. |
| 23 |
* |
| 24 |
* @param Options_Helper $options_helper The option helper. |
| 25 |
*/ |
| 26 |
public function __construct( Options_Helper $options_helper ) { |
| 27 |
$this->options_helper = $options_helper; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Initializes the integration. |
| 32 |
* |
| 33 |
* This is the place to register hooks and filters. |
| 34 |
* |
| 35 |
* @return void |
| 36 |
*/ |
| 37 |
public function register_hooks() { |
| 38 |
// Remove HTTP headers we don't want. |
| 39 |
\add_action( 'wp', [ $this, 'clean_headers' ], 0 ); |
| 40 |
|
| 41 |
if ( $this->is_true( 'remove_shortlinks' ) ) { |
| 42 |
// Remove shortlinks. |
| 43 |
\remove_action( 'wp_head', 'wp_shortlink_wp_head' ); |
| 44 |
\remove_action( 'template_redirect', 'wp_shortlink_header', 11 ); |
| 45 |
} |
| 46 |
|
| 47 |
if ( $this->is_true( 'remove_rest_api_links' ) ) { |
| 48 |
// Remove REST API links. |
| 49 |
\remove_action( 'wp_head', 'rest_output_link_wp_head' ); |
| 50 |
\remove_action( 'template_redirect', 'rest_output_link_header', 11 ); |
| 51 |
} |
| 52 |
|
| 53 |
if ( $this->is_true( 'remove_rsd_wlw_links' ) ) { |
| 54 |
// Remove RSD and WLW Manifest links. |
| 55 |
\remove_action( 'wp_head', 'rsd_link' ); |
| 56 |
\remove_action( 'xmlrpc_rsd_apis', 'rest_output_rsd' ); |
| 57 |
\remove_action( 'wp_head', 'wlwmanifest_link' ); |
| 58 |
} |
| 59 |
|
| 60 |
if ( $this->is_true( 'remove_oembed_links' ) ) { |
| 61 |
// Remove JSON+XML oEmbed links. |
| 62 |
\remove_action( 'wp_head', 'wp_oembed_add_discovery_links' ); |
| 63 |
} |
| 64 |
|
| 65 |
if ( $this->is_true( 'remove_generator' ) ) { |
| 66 |
\remove_action( 'wp_head', 'wp_generator' ); |
| 67 |
} |
| 68 |
|
| 69 |
if ( $this->is_true( 'remove_emoji_scripts' ) ) { |
| 70 |
// Remove emoji scripts and additional stuff they cause. |
| 71 |
\remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); |
| 72 |
\remove_action( 'wp_print_styles', 'print_emoji_styles' ); |
| 73 |
\remove_action( 'admin_print_scripts', 'print_emoji_detection_script' ); |
| 74 |
\remove_action( 'admin_print_styles', 'print_emoji_styles' ); |
| 75 |
\add_filter( 'wp_resource_hints', [ $this, 'resource_hints_plain_cleanup' ], 1 ); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Returns the conditionals based in which this loadable should be active. |
| 81 |
* |
| 82 |
* @return array The array of conditionals. |
| 83 |
*/ |
| 84 |
public static function get_conditionals() { |
| 85 |
return [ Front_End_Conditional::class ]; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Removes X-Pingback and X-Powered-By headers as they're unneeded. |
| 90 |
* |
| 91 |
* @return void |
| 92 |
*/ |
| 93 |
public function clean_headers() { |
| 94 |
if ( \headers_sent() ) { |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
if ( $this->is_true( 'remove_powered_by_header' ) ) { |
| 99 |
\header_remove( 'X-Powered-By' ); |
| 100 |
} |
| 101 |
if ( $this->is_true( 'remove_pingback_header' ) ) { |
| 102 |
\header_remove( 'X-Pingback' ); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Remove the core s.w.org hint as it's only used for emoji stuff we don't use. |
| 108 |
* |
| 109 |
* @param array $hints The hints we're adding to. |
| 110 |
* |
| 111 |
* @return array |
| 112 |
*/ |
| 113 |
public function resource_hints_plain_cleanup( $hints ) { |
| 114 |
foreach ( $hints as $key => $hint ) { |
| 115 |
if ( \is_array( $hint ) && isset( $hint['href'] ) ) { |
| 116 |
if ( \strpos( $hint['href'], '//s.w.org' ) !== false ) { |
| 117 |
unset( $hints[ $key ] ); |
| 118 |
} |
| 119 |
} |
| 120 |
elseif ( \strpos( $hint, '//s.w.org' ) !== false ) { |
| 121 |
unset( $hints[ $key ] ); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
return $hints; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Checks if the value of an option is set to true. |
| 130 |
* |
| 131 |
* @param string $option_name The option name. |
| 132 |
* |
| 133 |
* @return bool |
| 134 |
*/ |
| 135 |
private function is_true( $option_name ) { |
| 136 |
return $this->options_helper->get( $option_name ) === true; |
| 137 |
} |
| 138 |
} |
| 139 |
|