Skip to content

Commit de3741f

Browse files
committed
Code Modernization: Fix instances of using null as an array offset.
Addresses a new [https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_using_values_null_as_an_array_offset_and_when_calling_array_key_exists deprecation in PHP 8.5] in several block-related registry classes. Follow-up to [60809]. Props mukesh27, swissspidy. Fixes #63957. git-svn-id: https://develop.svn.wordpress.org/trunk@60904 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 761f391 commit de3741f

10 files changed

Lines changed: 93 additions & 11 deletions

src/wp-includes/class-wp-block-bindings-registry.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,11 @@ public function get_registered( string $source_name ) {
249249
*
250250
* @since 6.5.0
251251
*
252-
* @param string $source_name The name of the source.
252+
* @param string|null $source_name The name of the source.
253253
* @return bool `true` if the block bindings source is registered, `false` otherwise.
254254
*/
255255
public function is_registered( $source_name ) {
256-
return isset( $this->sources[ $source_name ] );
256+
return isset( $source_name, $this->sources[ $source_name ] );
257257
}
258258

259259
/**

src/wp-includes/class-wp-block-pattern-categories-registry.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,11 @@ public function get_all_registered( $outside_init_only = false ) {
138138
*
139139
* @since 5.5.0
140140
*
141-
* @param string $category_name Pattern category name including namespace.
141+
* @param string|null $category_name Pattern category name including namespace.
142142
* @return bool True if the pattern category is registered, false otherwise.
143143
*/
144144
public function is_registered( $category_name ) {
145-
return isset( $this->registered_categories[ $category_name ] );
145+
return isset( $category_name, $this->registered_categories[ $category_name ] );
146146
}
147147

148148
/**

src/wp-includes/class-wp-block-patterns-registry.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,11 @@ public function get_all_registered( $outside_init_only = false ) {
238238
*
239239
* @since 5.5.0
240240
*
241-
* @param string $pattern_name Block pattern name including namespace.
241+
* @param string|null $pattern_name Block pattern name including namespace.
242242
* @return bool True if the pattern is registered, false otherwise.
243243
*/
244244
public function is_registered( $pattern_name ) {
245-
return isset( $this->registered_patterns[ $pattern_name ] );
245+
return isset( $pattern_name, $this->registered_patterns[ $pattern_name ] );
246246
}
247247

248248
public function __wakeup() {

src/wp-includes/class-wp-block-styles-registry.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,12 @@ public function get_registered_styles_for_block( $block_name ) {
181181
*
182182
* @since 5.3.0
183183
*
184-
* @param string $block_name Block type name including namespace.
185-
* @param string $block_style_name Block style name.
184+
* @param string|null $block_name Block type name including namespace.
185+
* @param string|null $block_style_name Block style name.
186186
* @return bool True if the block style is registered, false otherwise.
187187
*/
188188
public function is_registered( $block_name, $block_style_name ) {
189-
return isset( $this->registered_block_styles[ $block_name ][ $block_style_name ] );
189+
return isset( $block_name, $block_style_name, $this->registered_block_styles[ $block_name ][ $block_style_name ] );
190190
}
191191

192192
/**

src/wp-includes/class-wp-block-templates-registry.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,11 @@ public function get_by_query( $query = array() ) {
204204
*
205205
* @since 6.7.0
206206
*
207-
* @param string $template_name Template name.
207+
* @param string|null $template_name Template name.
208208
* @return bool True if the template is registered, false otherwise.
209209
*/
210210
public function is_registered( $template_name ) {
211-
return isset( $this->registered_templates[ $template_name ] );
211+
return isset( $template_name, $this->registered_templates[ $template_name ] );
212212
}
213213

214214
/**

tests/phpunit/tests/block-bindings/wpBlockBindingsRegistry.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,4 +344,16 @@ public function test_is_registered_for_known_source() {
344344
$result = $this->registry->is_registered( self::$test_source_name );
345345
$this->assertTrue( $result );
346346
}
347+
348+
/**
349+
* Should return false when checking registration with a null source name.
350+
*
351+
* @ticket 63957
352+
*
353+
* @covers WP_Block_Bindings_Registry::is_registered
354+
*/
355+
public function test_is_registered_with_null_source_name() {
356+
$result = $this->registry->is_registered( null );
357+
$this->assertFalse( $result );
358+
}
347359
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Unit tests for WP_Block_Pattern_Categories_Registry::is_registered().
4+
*
5+
* @package WordPress
6+
* @subpackage Blocks
7+
* @since 6.9.0
8+
*
9+
* @group block-patterns
10+
* @covers WP_Block_Pattern_Categories_Registry::is_registered
11+
*/
12+
13+
class Tests_Block_Pattern_WPBlockPatternCategoriesRegistry extends WP_UnitTestCase {
14+
15+
/**
16+
* @ticket 63957
17+
*/
18+
public function test_is_registered_with_null_category_name() {
19+
$registry = WP_Block_Pattern_Categories_Registry::get_instance();
20+
$this->assertFalse( $registry->is_registered( null ) );
21+
}
22+
}

tests/phpunit/tests/block-templates/WpBlockTemplatesRegistry.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,15 @@ public function test_is_registered() {
247247
self::$registry->unregister( $template_name );
248248
}
249249

250+
/**
251+
* @ticket 63957
252+
*
253+
* @covers ::is_registered
254+
*/
255+
public function test_is_registered_with_null_template_name() {
256+
$this->assertFalse( self::$registry->is_registered( null ) );
257+
}
258+
250259
/**
251260
* Tests that unregister() correctly unregisters a registered template.
252261
*

tests/phpunit/tests/blocks/wpBlockPatternsRegistry.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,4 +695,11 @@ private function set_registered_patterns_variable_value( $value ) {
695695
$property->setAccessible( false );
696696
}
697697
}
698+
699+
/**
700+
* @ticket 63957
701+
*/
702+
public function test_is_registered_with_null_pattern_name() {
703+
$this->assertFalse( $this->registry->is_registered( null ) );
704+
}
698705
}

tests/phpunit/tests/blocks/wpBlockStylesRegistry.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,36 @@ public function test_register_block_style_with_array_of_block_names() {
6666
$this->assertTrue( $this->registry->is_registered( 'core/paragraph', 'plain' ) );
6767
$this->assertTrue( $this->registry->is_registered( 'core/group', 'plain' ) );
6868
}
69+
70+
/**
71+
* @ticket 63957
72+
*/
73+
public function test_is_registered_returns_false_for_null_block_name() {
74+
$style_name = 'fancy-style';
75+
$this->assertFalse(
76+
$this->registry->is_registered( null, $style_name ),
77+
'Empty block name should return false.'
78+
);
79+
}
80+
81+
/**
82+
* @ticket 63957
83+
*/
84+
public function test_is_registered_returns_false_for_null_style_name() {
85+
$block_name = 'core/paragraph';
86+
$this->assertFalse(
87+
$this->registry->is_registered( $block_name, null ),
88+
'Empty style name should return false.'
89+
);
90+
}
91+
92+
/**
93+
* @ticket 63957
94+
*/
95+
public function test_is_registered_returns_false_for_both_null_params() {
96+
$this->assertFalse(
97+
$this->registry->is_registered( null, null ),
98+
'Both empty block and style name should return false.'
99+
);
100+
}
69101
}

0 commit comments

Comments
 (0)