Head.php
4.83 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
namespace AIOSEO\Plugin\Common\Main;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use AIOSEO\Plugin\Common\Meta;
/**
* Outputs anything we need to the head of the site.
*
* @since 4.0.0
*/
class Head {
/**
* The page title.
*
* @since 4.0.5
*
* @var string
*/
private static $pageTitle = null;
/**
* GoogleAnalytics class instance.
*
* @since 4.2.7
*
* @var GoogleAnalytics
*/
protected $analytics = null;
/**
* Links class instance.
*
* @since 4.2.7
*
* @var Meta\Links
*/
protected $links = null;
/**
* Keywords class instance.
*
* @since 4.2.7
*
* @var Meta\Keywords
*/
protected $keywords = null;
/**
* Verification class instance.
*
* @since 4.2.7
*
* @var Meta\Verification
*/
protected $verification = null;
/**
* The views to output.
*
* @since 4.2.7
*
* @var array
*/
protected $views = [];
/**
* Class constructor.
*
* @since 4.0.0
*/
public function __construct() {
add_action( 'init', [ $this, 'addAnalytics' ] );
add_action( 'wp', [ $this, 'registerTitleHooks' ], 1000 );
add_action( 'wp_head', [ $this, 'init' ], 1 );
$this->analytics = new GoogleAnalytics();
$this->links = new Meta\Links();
$this->keywords = new Meta\Keywords();
$this->verification = new Meta\SiteVerification();
$this->views = [
'meta' => AIOSEO_DIR . '/app/Common/Views/main/meta.php',
'social' => AIOSEO_DIR . '/app/Common/Views/main/social.php',
'schema' => AIOSEO_DIR . '/app/Common/Views/main/schema.php',
'clarity' => AIOSEO_DIR . '/app/Common/Views/main/clarity.php'
];
}
/**
* Adds analytics to the views if needed.
*
* @since 4.0.5
*
* @return void
*/
public function addAnalytics() {
if ( $this->analytics->canShowScript() ) {
$this->views['analytics'] = AIOSEO_DIR . '/app/Common/Views/main/analytics.php';
}
}
/**
* Registers our title hooks.
*
* @since 4.0.5
*
* @return void
*/
public function registerTitleHooks() {
if ( apply_filters( 'aioseo_disable', false ) || apply_filters( 'aioseo_disable_title_rewrites', false ) ) {
return;
}
add_filter( 'pre_get_document_title', [ $this, 'getTitle' ], 99999 );
add_filter( 'wp_title', [ $this, 'getTitle' ], 99999 );
if ( ! current_theme_supports( 'title-tag' ) ) {
add_action( 'template_redirect', [ $this, 'startOutputBuffering' ] );
add_action( 'wp_footer', [ $this, 'endOutputBuffering' ], -2 );
}
}
/**
* Initializes the class.
*
* @since 4.0.5
*
* @return void
*/
public function init() {
$included = new Meta\Included();
if ( is_admin() || wp_doing_ajax() || wp_doing_cron() || ! $included->isIncluded() ) {
return;
}
$this->output();
}
/**
* Returns the page title.
*
* @since 4.0.5
*
* @param string $wpTitle The original page title from WordPress.
* @return string $pageTitle The page title.
*/
public function getTitle( $wpTitle = '' ) {
if ( null !== self::$pageTitle ) {
return self::$pageTitle;
}
self::$pageTitle = aioseo()->meta->title->filterPageTitle( $wpTitle );
return self::$pageTitle;
}
/**
* Starts our output buffering.
*
* @since 4.0.5
*
* @return void
*/
public function startOutputBuffering() {
ob_start( [ $this, 'rewriteTitle' ] );
}
/**
* Flush and send the output buffer.
*
* @since 4.3.2
*
* @return void
*/
public function endOutputBuffering() {
ob_flush();
}
/**
* Callback to fire when ob_flush() is called.
* Rewrites the page title using output buffering.
*
* @since 4.0.5
* @version 4.3.2
*
* @param string $content The buffer content.
* @return string
*/
public function rewriteTitle( $content ) {
$split = explode( '</head>', $content );
if ( empty( $split[1] ) ) {
return $content;
}
$head = $split[0] . '</head>';
unset( $split[0] );
$body = implode( '</head>', $split );
// Remove all existing title tags.
$head = preg_replace( '#<title.*?\/title>#s', '', $head );
// Add the new title tag to our own comment block.
$pageTitle = aioseo()->helpers->escapeRegexReplacement( $this->getTitle() );
$head = preg_replace( '/(<!--\sAll\sin\sOne\sSEO[a-z0-9\s.]+\s-\saioseo\.com\s-->)/i', "$1\r\n\t\t<title>$pageTitle</title>", $head, 1 );
return $head . $body;
}
/**
* The output function itself.
*
* @since 4.0.0
*
* @return void
*/
public function output() {
remove_action( 'wp_head', 'rel_canonical' );
$views = apply_filters( 'aioseo_meta_views', $this->views );
if ( empty( $views ) ) {
return;
}
echo "\n\t\t<!-- " . sprintf(
'%1$s %2$s',
esc_html( AIOSEO_PLUGIN_NAME ),
aioseo()->version // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
) . " - aioseo.com -->\n";
foreach ( $views as $view ) {
require $view;
}
echo "\t\t<!-- " . esc_html( AIOSEO_PLUGIN_NAME ) . " -->\n\n";
}
}