This repository has been archived by the owner on Jan 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
the-seo-framework-title-fix.php
380 lines (313 loc) · 9.42 KB
/
the-seo-framework-title-fix.php
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<?php
/**
* Plugin Name: The SEO Framework - Title Fix
* Plugin URI: https://wordpress.org/plugins/the-seo-framework-title-fix/
* Description: The Title Fix extension for The SEO Framework makes sure your title output is as configured. Even if your theme is doing it wrong.
* Version: 1.0.1.2
* Author: Sybre Waaijer
* Author URI: https://cyberwire.nl/
* License: GPLv3
*/
/**
* Title Fix extension plugin for The SEO Framework
* Copyright (C) 2016 Sybre Waaijer, CyberWire (https://cyberwire.nl/)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//* Notify the existence of this plugin through a lovely definition.
define( 'THE_SEO_FRAMEWORK_TITLE_FIX', true );
//* Define version, for future things.
define( 'THE_SEO_FRAMEWORK_TITLE_FIX_VERSION', '1.0.2' );
add_action( 'plugins_loaded', 'the_seo_framework_title_fix_init' );
/**
* Initialize the plugin.
*
* @since 1.0.0
*
* @return bool True if class is loaded
*/
function the_seo_framework_title_fix_init() {
static $loaded = null;
//* Don't init the class twice.
if ( isset( $loaded ) )
return $loaded;
//* Check if the SEO Framework is active and get the Version
if ( function_exists( 'the_seo_framework_version' ) ) {
//* Get the version number.
$theseoframework_version = the_seo_framework_version();
/**
* Only run the plugin on The SEO Framework 2.5.2 and higher.
*
* @since 1.0.0
*/
if ( isset( $theseoframework_version ) && version_compare( $theseoframework_version, '2.5.1.99' , '>' ) ) {
//* Initialize class.
new The_SEO_Framework_Title_Fix();
return $loaded = true;
}
}
return $loaded = false;
}
/**
* Class The_SEO_Framework_Title_Fix
*
* @since 1.0.0
*
* @final Please don't extend this extension.
*/
final class The_SEO_Framework_Title_Fix {
/**
* Force the fix when no title-tag is present.
*
* @since 1.0.0
*
* @var bool Whether the fix should be forced or not.
*/
protected $force_title_fix = false;
/**
* Check if title has been found, otherwise continue flushing till the bottom of plugin output.
*
* @since 1.0.0
*
* @var bool Whether title has been found and replaced already.
*/
protected $title_found_and_flushed = false;
/**
* If Output Buffering is started or not.
* If started, don't start again.
* If stopped, don't stop again.
*
* @since 1.0.0
*
* @var bool Whether ob has started.
*/
protected $ob_started = false;
/**
* The constructor, initialize plugin.
*/
public function __construct() {
//* Start the plugin at header, where theme support has just been initialized.
add_action( 'get_header', array( $this, 'start_plugin' ), -1 );
}
/**
* Start plugin at get_header. A semi constructor.
*
* @since 1.0.0
*/
public function start_plugin() {
/**
* Don't fix the title in admin.
* get_header doesn't run in admin, but another plugin might init it.
*/
if ( false === is_admin() ) {
if ( false === $this->current_theme_supports_title_tag() ) {
/**
* Applies filters 'the_seo_framework_force_title_fix'
* @since 1.0.1
* @param bool Whether to force the title fixing.
*/
$this->force_title_fix = (bool) apply_filters( 'the_seo_framework_force_title_fix', version_compare( the_seo_framework_version(), '2.6.99' , '<' ) );
}
/**
* Only do something if the theme is doing it wrong. Or when the filter has been applied.
* Requires initial load after theme switch.
*/
if ( $this->force_title_fix || false === the_seo_framework()->theme_title_doing_it_right() ) {
/**
* First run.
* Start at HTTP header.
* Stop right at where wp_head is run.
*/
add_action( 'get_header', array( $this, 'start_ob' ), 0 );
add_action( 'wp_head', array( $this, 'maybe_rewrite_title' ), 0 );
add_action( 'wp_head', array( $this, 'maybe_stop_ob' ), 0 );
/**
* Second run. Capture WP head.
* {add_action( 'wp_head', 'wp_title' );.. who knows?}
* Start at where wp_head is run (last run left off).
* Stop right at the end of wp_head.
*/
add_action( 'wp_head', array( $this, 'maybe_start_ob' ), 0 );
add_action( 'wp_head', array( $this, 'maybe_rewrite_title' ), 9999 );
add_action( 'wp_head', array( $this, 'maybe_stop_ob' ), 9999 );
/**
* Third run. Capture the page.
* Start at where wp_head has ended (last run left off),
* or at wp_head start (first run left off).
* Stop at the footer.
*/
add_action( 'wp_head', array( $this, 'maybe_start_ob' ), 9999 );
add_action( 'get_footer', array( $this, 'maybe_rewrite_title' ), -1 );
add_action( 'get_footer', array( $this, 'maybe_stop_ob' ), -1 );
/**
* Stop OB if it's still running at shutdown.
* Might prevent AJAX issues, if any.
*/
add_action( 'shutdown', array( $this, 'stop_ob' ), 0 );
}
}
}
/**
* Start the Output Buffer.
*
* @since 1.0.0
*/
public function start_ob() {
if ( false === $this->ob_started ) {
ob_start();
$this->ob_started = true;
}
}
/**
* Clean the buffer and turn off the output buffering.
*
* @since 1.0.0
*/
public function stop_ob() {
if ( $this->ob_started ) {
ob_end_clean();
$this->ob_started = false;
}
}
/**
* Maybe start the Output Buffer if the title is not yet replaced.
*
* @since 1.0.0
*/
public function maybe_start_ob() {
//* Reset the output buffer if not found.
if ( false === $this->ob_started && false === $this->title_found_and_flushed ) {
$this->start_ob();
}
}
/**
* Maybe stop OB flush if title has been replaced already.
*
* @since 1.0.0
*/
public function maybe_stop_ob() {
//* Let's not buffer all the way down.
if ( $this->ob_started && $this->title_found_and_flushed ) {
$this->stop_ob();
}
}
/**
* Maybe rewrite the title, if not rewritten yet.
*
* @since 1.0.0
*/
public function maybe_rewrite_title( $content ) {
$this->find_title_tag( $content );
if ( $this->ob_started && false === $this->title_found_and_flushed ) {
$content = ob_get_clean();
$this->ob_started = false;
$this->find_title_tag( $content );
}
}
/**
* Finds the title tag and replaces it if found; will echo content from buffer otherwise.
*
* @uses _wp_can_use_pcre_u() WP Core function
* (Compat for lower than WP 4.1.0 provided within The SEO Framework)
*
* @since 1.0.0
* @since 1.0.2: Echos $content, always.
*
* @param string $content The content with possible title tag.
* @return void When title is found.
*/
public function find_title_tag( $content ) {
//* Check if we can use preg_match.
if ( _wp_can_use_pcre_u() ) {
//* Let's use regex.
if ( 1 === preg_match( '/<title.*?<\/title>/is', $content, $matches ) ) {
$title_tag = isset( $matches[0] ) ? $matches[0] : null;
if ( isset( $title_tag ) ) {
$this->replace_title_tag( $title_tag, $content );
$this->title_found_and_flushed = true;
return;
}
}
} else {
//* Let's count. 0.0003s faster, but less reliable.
$start = stripos( $content, '<title' );
if ( false !== $start ) {
$end = stripos( $content, '</title>', $start );
if ( false !== $end ) {
//* +8 is "</title>" length
$title_tag = substr( $content, $start, $end - $start + 8 );
if ( false !== $title_tag ) {
$this->replace_title_tag( $title_tag, $content );
$this->title_found_and_flushed = true;
return;
}
}
}
}
//* Can't be escaped, as content is unknown.
echo $content;
}
/**
* Replaces the title tag.
*
* @since 1.0.0
*
* @param string $title_tag the Title tag with the title
* @param string $content The content containing the $title_tag
* @return string the content with replaced title tag.
*/
public function replace_title_tag( $title_tag, $content ) {
$new_title = '<title>' . the_seo_framework()->title_from_cache( '', '' , '', true ) . '</title>' . $this->indicator();
$count = 1;
//* Replace the title tag within the header.
$content = str_replace( $title_tag, $new_title, $content, $count );
//* Can't be escaped, as content is unknown.
echo $content;
}
/**
* Checks a theme's support for title-tag.
*
* @since 1.0.0
* @global array $_wp_theme_features
* @staticvar bool $supports
*
* @return bool True if the theme supports the title tag, false otherwise.
*/
public function current_theme_supports_title_tag() {
static $supports = null;
if ( isset( $supports ) )
return $supports;
global $_wp_theme_features;
if ( false === empty( $_wp_theme_features['title-tag'] ) )
return $supports = true;
return $supports = false;
}
/**
* Returns a small indicator.
*
* @since 1.0.1
*
* @return string
*/
public function indicator() {
/**
* Applies filters 'the_seo_framework_title_fixed_indicator'
* @since 1.0.1
* @param bool Whether to output an indicator or not.
*/
$indicator = (bool) apply_filters( 'the_seo_framework_title_fixed_indicator', true );
if ( $indicator )
return '<!-- fixed -->';
return '';
}
}