force-rewrite-title.php
4.4 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
<?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;
use Yoast\WP\SEO\Wrappers\WP_Query_Wrapper;
/**
* Class Force_Rewrite_Title.
*/
class Force_Rewrite_Title implements Integration_Interface {
/**
* The options helper.
*
* @var Options_Helper
*/
private $options;
/**
* Toggle indicating whether output buffering has been started.
*
* @var bool
*/
private $ob_started = false;
/**
* The WP Query wrapper.
*
* @var WP_Query_Wrapper
*/
private $wp_query;
/**
* Sets the helpers.
*
* @codeCoverageIgnore It just handles dependencies.
*
* @param Options_Helper $options Options helper.
* @param WP_Query_Wrapper $wp_query WP query wrapper.
*/
public function __construct( Options_Helper $options, WP_Query_Wrapper $wp_query ) {
$this->options = $options;
$this->wp_query = $wp_query;
}
/**
* Returns the conditionals based in which this loadable should be active.
*
* @return array
*/
public static function get_conditionals() {
return [ Front_End_Conditional::class ];
}
/**
* Initializes the integration.
*
* This is the place to register hooks and filters.
*
* @codeCoverageIgnore
*
* @return void
*/
public function register_hooks() {
// When the option is disabled.
if ( ! $this->options->get( 'forcerewritetitle', false ) ) {
return;
}
// For WordPress versions below 4.4.
if ( \current_theme_supports( 'title-tag' ) ) {
return;
}
\add_action( 'template_redirect', [ $this, 'force_rewrite_output_buffer' ], 99999 );
\add_action( 'wp_footer', [ $this, 'flush_cache' ], -1 );
}
/**
* Used in the force rewrite functionality this retrieves the output, replaces the title with the proper SEO
* title and then flushes the output.
*
* @return bool
*/
public function flush_cache() {
if ( $this->ob_started !== true ) {
return false;
}
$content = $this->get_buffered_output();
$old_wp_query = $this->wp_query->get_query();
\wp_reset_query();
// When the file has the debug mark.
if ( \preg_match( '/(?\'before\'.*)<!-- This site is optimized with the Yoast SEO.*<!-- \/ Yoast SEO( Premium)? plugin. -->(?\'after\'.*)/is', $content, $matches ) ) {
$content = $this->replace_titles_from_content( $content, $matches );
unset( $matches );
}
// phpcs:ignore WordPress.WP.GlobalVariablesOverride -- The query gets reset here with the original query.
$GLOBALS['wp_query'] = $old_wp_query;
// phpcs:ignore WordPress.Security.EscapeOutput -- The output should already have been escaped, we are only filtering it.
echo $content;
return true;
}
/**
* Starts the output buffer so it can later be fixed by flush_cache().
*/
public function force_rewrite_output_buffer() {
$this->ob_started = true;
$this->start_output_buffering();
}
/**
* Replaces the titles from the parts that contains a title.
*
* @param string $content The content to remove the titles from.
* @param array $parts_with_title The parts containing a title.
*
* @return string The modified content.
*/
protected function replace_titles_from_content( $content, $parts_with_title ) {
if ( isset( $parts_with_title['before'] ) && \is_string( $parts_with_title['before'] ) ) {
$content = $this->replace_title( $parts_with_title['before'], $content );
}
if ( isset( $parts_with_title['after'] ) ) {
$content = $this->replace_title( $parts_with_title['after'], $content );
}
return $content;
}
/**
* Removes the title from the part that contains the title and put this modified part back
* into the content.
*
* @param string $part_with_title The part with the title that needs to be replaced.
* @param string $content The entire content.
*
* @return string The altered content.
*/
protected function replace_title( $part_with_title, $content ) {
$part_without_title = \preg_replace( '/<title.*?\/title>/i', '', $part_with_title );
return \str_replace( $part_with_title, $part_without_title, $content );
}
/**
* Starts the output buffering.
*
* @codeCoverageIgnore
*/
protected function start_output_buffering() {
\ob_start();
}
/**
* Retrieves the buffered output.
*
* @codeCoverageIgnore
*
* @return false|string The buffered output.
*/
protected function get_buffered_output() {
return \ob_get_clean();
}
}