Skip to content

Commit

Permalink
Merge pull request #2008 from Codeinwp/fix/issue-2001
Browse files Browse the repository at this point in the history
fix: undefined array key in block conditions
  • Loading branch information
HardeepAsrani authored Dec 18, 2023
2 parents 017dfa9 + 2f5c5d4 commit 8cbf1fb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion inc/class-registration.php
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,7 @@ public function load_condition_hide_on_styles( $block_content, $block ) {

foreach ( $block['attrs']['otterConditions'] as $group ) {
foreach ( $group as $condition ) {
if ( 'screenSize' === $condition['type'] && isset( $condition['screen_sizes'] ) && is_array( $condition['screen_sizes'] ) ) {
if ( array_key_exists( 'type', $condition ) && 'screenSize' === $condition['type'] && isset( $condition['screen_sizes'] ) && is_array( $condition['screen_sizes'] ) ) {
$has_condition = true;
break;
}
Expand Down
38 changes: 38 additions & 0 deletions tests/test-block-conditions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* @package gutenberg-blocks
*/

use ThemeIsle\GutenbergBlocks\Registration;
use ThemeIsle\GutenbergBlocks\Plugins\Block_Conditions;
use Yoast\PHPUnitPolyfills\Polyfills\AssertEqualsCanonicalizing;
use Yoast\PHPUnitPolyfills\Polyfills\AssertNotEqualsCanonicalizing;
Expand Down Expand Up @@ -591,4 +592,41 @@ public function test_get_css_hide_condition_no_hide() {

$this->assertFalse( $result );
}

public function test_load_condition_hide_on_styles() {
$registration = new Registration();

$block_content = '<p>Hello!</p>';

$block = [
"name" => "core/paragraph",
"attrs" => [
"otterConditions" => [[]]
]
];

$result = $registration->load_condition_hide_on_styles( $block_content, $block );

// Make sure styles are not loaded.
$this->assertEquals( Registration::$scripts_loaded['condition_hide_on'], false );

$block = [
"name" => "core/paragraph",
"attrs" => [
"otterConditions" => [
[
[
"type" => "screenSize",
"screen_sizes" => ["mobile"]
]
]
]
]
];

$result = $registration->load_condition_hide_on_styles( $block_content, $block );

// Make sure styles are loaded.
$this->assertEquals( Registration::$scripts_loaded['condition_hide_on'], true );
}
}

0 comments on commit 8cbf1fb

Please sign in to comment.