diff --git a/php/class-string-replace.php b/php/class-string-replace.php index 0e8667df..905fa9a2 100644 --- a/php/class-string-replace.php +++ b/php/class-string-replace.php @@ -298,10 +298,13 @@ public function replace_strings( $content, $context = 'view' ) { if ( ! empty( $this->context ) ) { $context = $this->context; } + $is_json = false; if ( Utils::looks_like_json( $content ) ) { - $json_maybe = json_decode( $content, true ); - if ( ! empty( $json_maybe ) ) { + // Decode as objects, not associative arrays, so JSON object/array shape (e.g. empty objects `{}`) survives the round trip below. + $json_maybe = json_decode( $content ); + if ( null !== $json_maybe ) { $content = $json_maybe; + $is_json = true; } } $this->prime_replacements( $content, $context ); @@ -309,7 +312,7 @@ public function replace_strings( $content, $context = 'view' ) { $content = self::do_replace( $content ); } self::reset(); - $last_content = ! empty( $json_maybe ) ? wp_json_encode( $content ) : $content; + $last_content = $is_json ? wp_json_encode( $content ) : $content; return $last_content; }