Skip to content

Commit 0f7bf57

Browse files
author
Marco Pereirinha
committed
Flag saving action
1 parent 45a5b12 commit 0f7bf57

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

php/class-delivery.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1819,6 +1819,11 @@ public function prepare_delivery( $content ) {
18191819
*/
18201820
public function catch_urls( $content, $context = 'view' ) {
18211821

1822+
// Check if we are saving. If so, bail.
1823+
// This is to prevent the replacement from happening in the shutdown, signaling content changes in the editor.
1824+
if ( $this->plugin->get_component( 'replace' )->doing_save() ) {
1825+
return;
1826+
}
18221827
$this->init_delivery();
18231828
$this->prepare_delivery( $content );
18241829

php/class-string-replace.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
namespace Cloudinary;
99

1010
use Cloudinary\Component\Setup;
11-
use \Traversable;
1211

1312
/**
1413
* String replace class.
@@ -36,6 +35,13 @@ class String_Replace implements Setup {
3635
*/
3736
protected static $replacements = array();
3837

38+
/**
39+
* Holds the list of strings and replacements.
40+
*
41+
* @var bool
42+
*/
43+
protected static $doing_save = false;
44+
3945
/**
4046
* Site Cache constructor.
4147
*
@@ -130,7 +136,12 @@ public function pre_filter_rest_echo( $result, $server, $request ) {
130136
*/
131137
public function pre_filter_rest_content( $response, $post, $request ) {
132138
$context = $request->get_param( 'context' );
139+
133140
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+
}
134145
$data = $response->get_data();
135146
$data = $this->replace_strings( $data, $context );
136147
$response->set_data( $data );
@@ -323,6 +334,16 @@ public static function do_replace( $content ) {
323334
return $content;
324335
}
325336

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+
326347
/**
327348
* Reset internal replacements.
328349
*/

0 commit comments

Comments
 (0)