Skip to content

Commit 4c71786

Browse files
Merge branch 'trac-59649-php8-improvements' of https://github.com/vidushigupta0607/wordpress-develop into trac-59649-php8-improvements
2 parents 9a6f484 + c6d9646 commit 4c71786

4 files changed

Lines changed: 64 additions & 6 deletions

File tree

src/wp-includes/IXR/class-IXR-value.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,16 @@ function getXml()
121121
/**
122122
* Checks whether or not the supplied array is a struct or not
123123
*
124-
* @param array $array
124+
* @param array $values List or map to inspect.
125125
* @return bool
126126
*/
127-
function isStruct($array)
128-
{
127+
function isStruct( $values ) {
129128
$expected = 0;
130-
foreach ($array as $key => $value) {
131-
if ((string)$key !== (string)$expected) {
129+
foreach ( $values as $key => $value ) {
130+
if ( (string) $key !== (string) $expected ) {
132131
return true;
133132
}
134-
$expected++;
133+
++$expected;
135134
}
136135
return false;
137136
}

src/wp-includes/class-wp-hook.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,12 @@ public function apply_filters( $value, $args ) {
318318
return $value;
319319
}
320320

321+
$args = wp_normalize_call_user_func_args( $args );
322+
323+
if ( ! $this->doing_action && array_key_exists( 0, $args ) ) {
324+
$value = $args[0];
325+
}
326+
321327
$nesting_level = $this->nesting_level++;
322328

323329
$this->iterations[ $nesting_level ] = $this->priorities;

src/wp-includes/plugin.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ function apply_filters( $hook_name, $value, ...$args ) {
228228
function apply_filters_ref_array( $hook_name, $args ) {
229229
global $wp_filter, $wp_filters, $wp_current_filter;
230230

231+
$args = wp_normalize_call_user_func_args( $args );
232+
231233
if ( ! isset( $wp_filters[ $hook_name ] ) ) {
232234
$wp_filters[ $hook_name ] = 1;
233235
} else {

tests/phpunit/tests/filters.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,57 @@ public function test_filter_ref_array_result() {
449449
$this->assertNotEmpty( $args[0][1]->foo );
450450
}
451451

452+
/**
453+
* PHP 8.0+ interprets string keys in call_user_func_array() as named parameters.
454+
* Hook callbacks must still receive arguments positionally when the args array uses string keys.
455+
*
456+
* @covers ::apply_filters_ref_array
457+
* @covers ::wp_normalize_call_user_func_args
458+
*/
459+
public function test_apply_filters_ref_array_with_string_keys_passes_arguments_positionally() {
460+
$hook_name = __FUNCTION__;
461+
add_filter(
462+
$hook_name,
463+
static function ( $first, $second ) {
464+
return $first . $second;
465+
},
466+
10,
467+
2
468+
);
469+
470+
$result = apply_filters_ref_array(
471+
$hook_name,
472+
array(
473+
'foo' => 'hello',
474+
'bar' => 'world',
475+
)
476+
);
477+
478+
$this->assertSame( 'helloworld', $result );
479+
}
480+
481+
/**
482+
* @covers ::do_action_ref_array
483+
* @covers ::wp_normalize_call_user_func_args
484+
*/
485+
public function test_do_action_ref_array_with_string_keys_passes_arguments_positionally() {
486+
$a = new MockAction();
487+
$hook_name = __FUNCTION__;
488+
489+
add_action( $hook_name, array( $a, 'action' ), 10, 2 );
490+
491+
do_action_ref_array(
492+
$hook_name,
493+
array(
494+
'foo' => 'first',
495+
'bar' => 'second',
496+
)
497+
);
498+
499+
$args = $a->get_args();
500+
$this->assertSame( array( 'first', 'second' ), $args[0] );
501+
}
502+
452503
/**
453504
* @ticket 29070
454505
*/

0 commit comments

Comments
 (0)