-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathclass-string-replace.php
More file actions
353 lines (316 loc) · 8.99 KB
/
class-string-replace.php
File metadata and controls
353 lines (316 loc) · 8.99 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
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
<?php
/**
* Cloudinary string replace class, to replace URLS and other strings on shutdown.
*
* @package Cloudinary
*/
namespace Cloudinary;
use Cloudinary\Component\Setup;
/**
* String replace class.
*/
class String_Replace implements Setup {
/**
* Holds the plugin instance.
*
* @var Plugin Instance of the global plugin.
*/
public $plugin;
/**
* Holds the context.
*
* @var string
*/
protected $context;
/**
* Holds the list of strings and replacements.
*
* @var array
*/
protected static $replacements = array();
/**
* Holds the list of strings and replacements.
*
* @var bool
*/
protected static $doing_save = false;
/**
* Site Cache constructor.
*
* @param Plugin $plugin Global instance of the main plugin.
*/
public function __construct( Plugin $plugin ) {
$this->plugin = $plugin;
}
/**
* Setup the object.
*/
public function setup() {
if ( Utils::is_admin() ) {
$this->admin_filters();
} elseif ( Utils::is_frontend_ajax() || Utils::is_rest_api() ) {
$this->context = 'view';
$this->start_capture();
} else {
$this->public_filters();
}
$this->add_rest_filters();
}
/**
* Add admin filters.
*/
protected function admin_filters() {
// Admin filters can call String_Replace frequently, which is fine, as performance is not an issue.
add_filter( 'media_send_to_editor', array( $this, 'replace_strings' ), 10, 2 );
add_filter( 'the_editor_content', array( $this, 'replace_strings' ), 10, 2 );
add_filter( 'wp_prepare_attachment_for_js', array( $this, 'replace_strings' ), 11 );
add_action( 'admin_init', array( $this, 'start_capture' ) );
}
/**
* Add Public Filters.
*/
protected function public_filters() {
add_action( 'template_include', array( $this, 'init_debug' ), PHP_INT_MAX );
if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) { // Not needed on REST API.
add_action( 'parse_request', array( $this, 'init' ), - 1000 ); // Not crazy low, but low enough to catch most cases, but not too low that it may break AMP.
}
}
/**
* Add filters for REST API.
*/
protected function add_rest_filters() {
$types = get_post_types();
foreach ( $types as $type ) {
$post_type = get_post_type_object( $type );
// Check if this is a rest supported type.
if ( property_exists( $post_type, 'show_in_rest' ) && true === $post_type->show_in_rest ) {
// Add filter only to rest supported types.
add_filter( 'rest_prepare_' . $type, array( $this, 'pre_filter_rest_content' ), 10, 3 );
}
}
add_filter( 'rest_pre_echo_response', array( $this, 'pre_filter_rest_echo' ), 900, 3 );
}
/**
* Filter the result of a rest Request before it's echoed.
*
* @param array $result The result of the REST API.
* @param \WP_REST_Server $server The REST server instance.
* @param \WP_REST_Request $request The original request.
*
* @return array
*/
public function pre_filter_rest_echo( $result, $server, $request ) {
$route = trim( $request->get_route(), '/' );
if ( 0 !== strpos( $route, REST_API::BASE ) ) {
// Only for non-Cloudinary requests.
$context = $request->get_param( 'context' );
if ( ! $context || 'view' === $context ) {
$result = $this->replace_strings( $result, $context );
}
}
return $result;
}
/**
* Filter out local urls in an 'edit' context rest request ( i.e for Gutenberg ).
*
* @param \WP_REST_Response $response The post data array to save.
* @param \WP_Post $post The current post.
* @param \WP_REST_Request $request The request object.
*
* @return \WP_REST_Response
*/
public function pre_filter_rest_content( $response, $post, $request ) {
$context = $request->get_param( 'context' );
if ( 'edit' === $context ) {
// Updating or creating a post.
if ( in_array( $request->get_method(), array( 'PUT', 'POST' ), true ) ) {
static::$doing_save = true;
}
$data = $response->get_data();
$data = $this->replace_strings( $data, $context );
$response->set_data( $data );
}
return $response;
}
/**
* Init the buffer capture and set the output callback.
*/
public function init() {
if ( ! defined( 'CLD_DEBUG' ) || false === CLD_DEBUG ) {
$this->context = 'view';
$this->start_capture();
}
}
/**
* Stop the buffer capture and set the output callback.
*/
public function start_capture() {
ob_start( array( $this, 'replace_strings' ) );
ob_start( array( $this, 'replace_strings' ) ); // Second call to catch early buffer flushing.
}
/**
* Init the buffer capture in debug mode.
*
* @param string $template The template being loaded.
*
* @return null|string
*/
public function init_debug( $template ) {
if ( defined( 'CLD_DEBUG' ) && true === CLD_DEBUG && ! Utils::get_sanitized_text( '_bypass' ) ) {
$this->context = 'view';
ob_start();
include $template; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable
$html = ob_get_clean();
echo $this->replace_strings( $html ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
$template = $this->plugin->template_path . 'blank-template.php';
}
return $template;
}
/**
* Check if a string is set for replacement.
*
* @param string $string String to check.
*
* @return bool
*/
public static function string_set( $string ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.stringFound
return isset( self::$replacements[ $string ] );
}
/**
* Check if a string is not set for replacement.
*
* @param string $string String to check.
*
* @return bool
*/
public static function string_not_set( $string ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.stringFound
return ! self::string_set( $string );
}
/**
* Replace a string.
*
* @param string $search The string to be replaced.
* @param string $replace The string replacement.
*/
public static function replace( $search, $replace ) {
self::$replacements[ $search ] = $replace;
}
/**
* Flatten an array into content.
*
* @param array|string $content The array to flatten.
*
* @return string
*/
public function flatten( $content ) {
$flat = '';
if ( Utils::looks_like_json( $content ) ) {
$maybe_content = json_decode( $content, true );
if ( ! empty( $maybe_content ) ) {
$content = $maybe_content;
}
}
if ( self::is_iterable( $content ) ) {
foreach ( $content as $item ) {
$flat .= "\r\n" . $this->flatten( $item );
}
} else {
$flat = $content;
}
return $flat;
}
/**
* Check if the item is iterable.
*
* @param mixed $thing Thing to check.
*
* @return bool
*/
public static function is_iterable( $thing ) {
return is_array( $thing ) || is_object( $thing );
}
/**
* Prime replacement strings.
*
* @param mixed $content The content to prime replacements for.
* @param string $context The context to use.
*/
protected function prime_replacements( $content, $context = 'view' ) {
if ( self::is_iterable( $content ) ) {
$content = $this->flatten( $content );
}
/**
* Do replacement action.
*
* @hook cloudinary_string_replace
* @since 3.0.3 Added the `$context` argument.
*
* @param $content {string} The html of the page.
* @param $context {string} The render context.
*/
do_action( 'cloudinary_string_replace', $content, $context );
}
/**
* Replace string in HTML.
*
* @param string|array $content The HTML.
* @param string $context The context to use.
*
* @return string
*/
public function replace_strings( $content, $context = 'view' ) {
static $last_content;
if ( empty( $content ) || $last_content === $content ) {
return $content; // Bail if nothing to replace.
}
// Captured a front end request, since the $context will be an int.
if ( ! empty( $this->context ) ) {
$context = $this->context;
}
if ( Utils::looks_like_json( $content ) ) {
$json_maybe = json_decode( $content, true );
if ( ! empty( $json_maybe ) ) {
$content = $json_maybe;
}
}
$this->prime_replacements( $content, $context );
if ( ! empty( self::$replacements ) ) {
$content = self::do_replace( $content );
}
self::reset();
$last_content = ! empty( $json_maybe ) ? wp_json_encode( $content ) : $content;
return $last_content;
}
/**
* Do string replacements.
*
* @param array|string $content The content to do replacements on.
*
* @return array|string
*/
public static function do_replace( $content ) {
if ( self::is_iterable( $content ) ) {
foreach ( $content as &$item ) {
$item = self::do_replace( $item );
}
} elseif ( is_string( $content ) ) {
$content = str_replace( array_keys( self::$replacements ), array_values( self::$replacements ), $content );
}
return $content;
}
/**
* Check if we are currently saving a REST request (i.e. Gutenberg).
* This is used to prevent double replacements.
*
* @return bool
*/
public function doing_save() {
return static::$doing_save;
}
/**
* Reset internal replacements.
*/
public static function reset() {
self::$replacements = array();
}
}