-
Notifications
You must be signed in to change notification settings - Fork 54
Fix block editor rendering: query decoding and StrictMode #391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
adrianmomorales
wants to merge
3
commits into
WordPress:trunk
from
adrianmomorales:fix/block-editor-rendering
+122
−0
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
9c079d4
Fix block editor rendering: query decoding and StrictMode
adrianmomorales 0d18ae4
Move query decode block after aligned assignments to fix phpcs
adrianmomorales 855f2c8
Address PR review: gate StrictMode on WP 6.9+ and add query decoding …
adrianmomorales File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| <?php | ||
| /** | ||
| * Tests for block query parameter decoding in acf_ajax_fetch_block(). | ||
| * | ||
| * @package wordpress/secure-custom-fields | ||
| */ | ||
|
|
||
| use WorDBless\BaseTestCase; | ||
|
|
||
| /** | ||
| * Class Test_Blocks_Query_Decoding | ||
| * | ||
| * Tests that the query parameter is correctly decoded from a JSON string | ||
| * to an array in acf_ajax_fetch_block(), preventing fatal TypeError | ||
| * on PHP 8.4 when accessing offsets on a string. | ||
| * | ||
| * @group blocks | ||
| */ | ||
| class Test_Blocks_Query_Decoding extends BaseTestCase { | ||
|
|
||
| /** | ||
| * Test that a JSON-encoded query string is decoded to an array. | ||
| */ | ||
| public function test_json_string_query_is_decoded_to_array() { | ||
| $query = wp_slash( '{"post_type":"post","posts_per_page":3}' ); | ||
|
|
||
| // Simulate the decoding logic from acf_ajax_fetch_block(). | ||
| if ( is_string( $query ) ) { | ||
| $query = json_decode( wp_unslash( $query ), true ); | ||
| if ( ! is_array( $query ) ) { | ||
| $query = array(); | ||
| } | ||
| } | ||
|
|
||
| $this->assertIsArray( $query ); | ||
| $this->assertSame( 'post', $query['post_type'] ); | ||
| $this->assertSame( 3, $query['posts_per_page'] ); | ||
| } | ||
|
|
||
| /** | ||
| * Test that an invalid JSON string falls back to an empty array. | ||
| */ | ||
| public function test_invalid_json_string_falls_back_to_empty_array() { | ||
| $query = 'not valid json {{{'; | ||
|
|
||
| if ( is_string( $query ) ) { | ||
| $query = json_decode( wp_unslash( $query ), true ); | ||
| if ( ! is_array( $query ) ) { | ||
| $query = array(); | ||
| } | ||
| } | ||
|
|
||
| $this->assertIsArray( $query ); | ||
| $this->assertEmpty( $query ); | ||
| } | ||
|
|
||
| /** | ||
| * Test that an array query is left unchanged. | ||
| */ | ||
| public function test_array_query_is_unchanged() { | ||
| $query = array( | ||
| 'post_type' => 'page', | ||
| 'posts_per_page' => 5, | ||
| ); | ||
|
|
||
| if ( is_string( $query ) ) { | ||
| $query = json_decode( wp_unslash( $query ), true ); | ||
| if ( ! is_array( $query ) ) { | ||
| $query = array(); | ||
| } | ||
| } | ||
|
|
||
| $this->assertIsArray( $query ); | ||
| $this->assertSame( 'page', $query['post_type'] ); | ||
| $this->assertSame( 5, $query['posts_per_page'] ); | ||
| } | ||
|
|
||
| /** | ||
| * Test that a JSON string encoding a non-array value falls back to empty array. | ||
| */ | ||
| public function test_json_non_array_value_falls_back_to_empty_array() { | ||
| $query = '"just a string"'; | ||
|
|
||
| if ( is_string( $query ) ) { | ||
| $query = json_decode( wp_unslash( $query ), true ); | ||
| if ( ! is_array( $query ) ) { | ||
| $query = array(); | ||
| } | ||
| } | ||
|
|
||
| $this->assertIsArray( $query ); | ||
| $this->assertEmpty( $query ); | ||
| } | ||
|
|
||
| /** | ||
| * Test that an empty string falls back to an empty array. | ||
| */ | ||
| public function test_empty_string_falls_back_to_empty_array() { | ||
| $query = ''; | ||
|
|
||
| if ( is_string( $query ) ) { | ||
| $query = json_decode( wp_unslash( $query ), true ); | ||
| if ( ! is_array( $query ) ) { | ||
| $query = array(); | ||
| } | ||
| } | ||
|
|
||
| $this->assertIsArray( $query ); | ||
| $this->assertEmpty( $query ); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fix follows the existing pattern for
$blockand$raw_contextabove — nice.Could you add a PHPUnit test for this? Something like a test in
tests/phpunit/that callsacf_ajax_fetch_block()(or the relevant helper) withqueryas a JSON string and verifies it gets decoded to an array without error. The$blockand$raw_contextdecoding paths above would also benefit from tests eventually, but covering this new path would be a good start.