diff --git a/src/wp-includes/interactivity-api/class-wp-interactivity-api.php b/src/wp-includes/interactivity-api/class-wp-interactivity-api.php index fdadb6518159b..0fd23231ca5f1 100644 --- a/src/wp-includes/interactivity-api/class-wp-interactivity-api.php +++ b/src/wp-includes/interactivity-api/class-wp-interactivity-api.php @@ -421,7 +421,15 @@ private function process_directives_args( string $html, array &$context_stack, a */ private function evaluate( $directive_value, string $default_namespace, $context = false ) { list( $ns, $path ) = $this->extract_directive_value( $directive_value, $default_namespace ); - if ( empty( $path ) ) { + if ( 'null' === $default_namespace ) { + $message = 'The namespace defined cannot be null.'; + _doing_it_wrong( __METHOD__, $message, '6.6.0' ); + return null; + } + if ( empty( $ns ) || empty( $path ) ) { + /* translators: %s: The directive value referenced. */ + $message = sprintf( 'Namespace or reference path cannot be empty. Directive value referenced: %s ', $directive_value ); + _doing_it_wrong( __METHOD__, $message, '6.6.0' ); return null; } diff --git a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php index 8af2da1cd0861..86eeabc05fd75 100644 --- a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php +++ b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php @@ -814,7 +814,7 @@ public function test_process_directives_does_not_change_inner_html_in_math() { * @param string $directive_value The directive attribute value to evaluate. * @return mixed The result of the evaluate method. */ - private function evaluate( $directive_value ) { + private function evaluate( $directive_value, $default_namespace = 'myPlugin' ) { $generate_state = function ( $name ) { return array( 'key' => $name, @@ -829,7 +829,7 @@ private function evaluate( $directive_value ) { ); $evaluate = new ReflectionMethod( $this->interactivity, 'evaluate' ); $evaluate->setAccessible( true ); - return $evaluate->invokeArgs( $this->interactivity, array( $directive_value, 'myPlugin', $context ) ); + return $evaluate->invokeArgs( $this->interactivity, array( $directive_value, $default_namespace, $context ) ); } /** @@ -923,6 +923,25 @@ public function test_evaluate_nested_value() { $this->assertEquals( 'otherPlugin-context-nested', $result ); } + /** + * Tests the `evaluate` method for non valid namespace values. + * + * @ticket 61044 + * + * @covers ::evaluate + * @expectedIncorrectUsage WP_Interactivity_API::evaluate + */ + public function test_evaluate_unvalid_namespaces() { + $result = $this->evaluate( 'path', 'null' ); + $this->assertNull( $result ); + + $result = $this->evaluate( 'path', '' ); + $this->assertNull( $result ); + + $result = $this->evaluate( 'path', '{}' ); + $this->assertNull( $result ); + } + /** * Tests the `kebab_to_camel_case` method. *