Make WordPress Core


Ignore:
Timestamp:
06/04/2024 07:16:48 AM (2 years ago)
Author:
gziolo
Message:

Interactivity API: Print debug warning when server directives processing encounters errors

Aims to improve the developer experience of the Interactivity API server directives processing.

Props cbravobernal, jonsurrell, westonruter, darerodz, czapla, gziolo.
Fixes #61044.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/interactivity-api/class-wp-interactivity-api.php

    r58320 r58321  
    273273         *
    274274         * @since 6.5.0
     275         * @since 6.6.0 The function displays a warning message when the HTML contains unbalanced tags or a directive appears in a MATH or SVG tag.
    275276         *
    276277         * @param string $html            The HTML content to process.
     
    296297                         */
    297298                        if ( 'SVG' === $tag_name || 'MATH' === $tag_name ) {
     299                                if ( $p->get_attribute_names_with_prefix( 'data-wp-' ) ) {
     300                                        /* translators: 1: SVG or MATH HTML tag, 2: Namespace of the interactive block. */
     301                                        $message = sprintf( __( 'Interactivity directives were detected on an incompatible %1$s tag when processing "%2$s". These directives will be ignored in the server side render.' ), $tag_name, end( $namespace_stack ) );
     302                                        _doing_it_wrong( __METHOD__, $message, '6.6.0' );
     303                                }
    298304                                $p->skip_to_tag_closer();
    299305                                continue;
     
    383389                        }
    384390                }
    385 
    386391                /*
    387392                 * It returns null if the HTML is unbalanced because unbalanced HTML is
    388393                 * not safe to process. In that case, the Interactivity API runtime will
    389                  * update the HTML on the client side during the hydration.
     394                 * update the HTML on the client side during the hydration. It will also
     395                 * display a notice to the developer to inform them about the issue.
    390396                 */
    391                 return $unbalanced || 0 < count( $tag_stack ) ? null : $p->get_updated_html();
     397                if ( $unbalanced || 0 < count( $tag_stack ) ) {
     398                        $tag_errored = 0 < count( $tag_stack ) ? end( $tag_stack )[0] : $tag_name;
     399                        /* translators: %1s: Namespace processed, %2s: The tag that caused the error; could be any HTML tag.  */
     400                        $message = sprintf( __( 'Interactivity directives failed to process in "%1$s" due to a missing "%2$s" end tag.' ), end( $namespace_stack ), $tag_errored );
     401                        _doing_it_wrong( __METHOD__, $message, '6.6.0' );
     402                        return null;
     403                }
     404
     405                return $p->get_updated_html();
    392406        }
    393407
     
    397411         *
    398412         * @since 6.5.0
     413         * @since 6.6.0 The function now adds a warning when the namespace is null, falsy, or the directive value is empty.
    399414         *
    400415         * @param string|true $directive_value   The directive attribute value string or `true` when it's a boolean attribute.
     
    403418         * @param array|false $context           The current context for evaluating the directive or false if there is no
    404419         *                                       context.
    405          * @return mixed|null The result of the evaluation. Null if the reference path doesn't exist.
     420         * @return mixed|null The result of the evaluation. Null if the reference path doesn't exist or the namespace is falsy.
    406421         */
    407422        private function evaluate( $directive_value, string $default_namespace, $context = false ) {
    408423                list( $ns, $path ) = $this->extract_directive_value( $directive_value, $default_namespace );
    409                 if ( empty( $path ) ) {
     424                if ( ! $ns || ! $path ) {
     425                        /* translators: %s: The directive value referenced. */
     426                        $message = sprintf( __( 'Namespace or reference path cannot be empty. Directive value referenced: %s' ), $directive_value );
     427                        _doing_it_wrong( __METHOD__, $message, '6.6.0' );
    410428                        return null;
    411429                }
Note: See TracChangeset for help on using the changeset viewer.