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