|
8 | 8 | namespace Cloudinary; |
9 | 9 |
|
10 | 10 | use Cloudinary\Component\Setup; |
11 | | -use \Traversable; |
12 | 11 |
|
13 | 12 | /** |
14 | 13 | * String replace class. |
@@ -36,6 +35,13 @@ class String_Replace implements Setup { |
36 | 35 | */ |
37 | 36 | protected static $replacements = array(); |
38 | 37 |
|
| 38 | + /** |
| 39 | + * Holds the list of strings and replacements. |
| 40 | + * |
| 41 | + * @var bool |
| 42 | + */ |
| 43 | + protected static $doing_save = false; |
| 44 | + |
39 | 45 | /** |
40 | 46 | * Site Cache constructor. |
41 | 47 | * |
@@ -130,7 +136,12 @@ public function pre_filter_rest_echo( $result, $server, $request ) { |
130 | 136 | */ |
131 | 137 | public function pre_filter_rest_content( $response, $post, $request ) { |
132 | 138 | $context = $request->get_param( 'context' ); |
| 139 | + |
133 | 140 | if ( 'edit' === $context ) { |
| 141 | + // Updating or creating a post. |
| 142 | + if ( in_array( $request->get_method(), array( 'PUT', 'POST' ), true ) ) { |
| 143 | + static::$doing_save = true; |
| 144 | + } |
134 | 145 | $data = $response->get_data(); |
135 | 146 | $data = $this->replace_strings( $data, $context ); |
136 | 147 | $response->set_data( $data ); |
@@ -323,6 +334,16 @@ public static function do_replace( $content ) { |
323 | 334 | return $content; |
324 | 335 | } |
325 | 336 |
|
| 337 | + /** |
| 338 | + * Check if we are currently saving a REST request (i.e. Gutenberg). |
| 339 | + * This is used to prevent double replacements. |
| 340 | + * |
| 341 | + * @return bool |
| 342 | + */ |
| 343 | + public function doing_save() { |
| 344 | + return static::$doing_save; |
| 345 | + } |
| 346 | + |
326 | 347 | /** |
327 | 348 | * Reset internal replacements. |
328 | 349 | */ |
|
0 commit comments