Make WordPress Core

Changeset 61659


Ignore:
Timestamp:
02/17/2026 08:45:09 AM (6 months ago)
Author:
luisherranz
Message:

Interactivity API: populate router's state.url in the server

Set the core/router state url property with the current page URL using get_self_link() when the first data-wp-router-region directive is processed, so that the stores subscribing to that property already have the correct value on page load.

Props westonruter.
Fixes #64649.

Location:
trunk
Files:
2 edited

Legend:

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

    r61653 r61659  
    13151315                        $this->has_processed_router_region = true;
    13161316
     1317                        // Initializes the `state.url` property from the server.
     1318                        $this->state(
     1319                                'core/router',
     1320                                array(
     1321                                        'url' => get_self_link(),
     1322                                )
     1323                        );
     1324
    13171325                        // Enqueues as an inline style.
    13181326                        wp_register_style( 'wp-interactivity-router-animations', false );
  • trunk/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-router-region.php

    r61424 r61659  
    7878
    7979        /**
     80         * Processes directives while temporarily replacing the global
     81         * WP_Interactivity_API instance so that global functions like
     82         * `wp_interactivity_state` operate on the test instance.
     83         *
     84         * @param string $html The HTML to process.
     85         * @return string The processed HTML.
     86         */
     87        protected function process_directives( string $html ): string {
     88                global $wp_interactivity;
     89                $prev             = $wp_interactivity;
     90                $wp_interactivity = $this->interactivity;
     91
     92                $result = $this->interactivity->process_directives( $html );
     93
     94                $wp_interactivity = $prev;
     95                return $result;
     96        }
     97
     98        /**
    8099         * Tests that no elements are added if the `data-wp-router-region` is
    81100         * missing.
     
    127146                $this->assertFalse( $p->next_tag( $query ) );
    128147        }
     148
     149        /**
     150         * Tests that the `data-wp-router-region` directive initializes the
     151         * `core/router` state URL from the server.
     152         *
     153         * @ticket 64649
     154         *
     155         * @covers ::process_directives
     156         */
     157        public function test_wp_router_region_initializes_state_url() {
     158                $_SERVER['REQUEST_URI'] = '/test-page/?query=1';
     159
     160                $html = '<div data-wp-router-region="region A">Interactive region</div>';
     161                $this->process_directives( $html );
     162
     163                $state = $this->interactivity->state( 'core/router' );
     164                $this->assertSame( home_url( '/test-page/?query=1' ), $state['url'] );
     165        }
     166
     167        /**
     168         * Tests that the `core/router` state URL uses HTTPS when SSL is active.
     169         *
     170         * @ticket 64649
     171         *
     172         * @covers ::process_directives
     173         */
     174        public function test_wp_router_region_initializes_state_url_with_https() {
     175                $_SERVER['REQUEST_URI'] = '/';
     176                $_SERVER['HTTPS']       = 'on';
     177
     178                $html = '<div data-wp-router-region="region A">Interactive region</div>';
     179                $this->process_directives( $html );
     180
     181                $state = $this->interactivity->state( 'core/router' );
     182                $this->assertStringStartsWith( 'https://', $state['url'] );
     183        }
     184
     185        /**
     186         * Tests that the `core/router` state URL is not set when no
     187         * `data-wp-router-region` directive is present.
     188         *
     189         * @ticket 64649
     190         *
     191         * @covers ::process_directives
     192         */
     193        public function test_wp_router_region_does_not_set_state_url_without_directive() {
     194                $_SERVER['REQUEST_URI'] = '/';
     195
     196                $html = '<div>Nothing here</div>';
     197                $this->process_directives( $html );
     198
     199                $state = $this->interactivity->state( 'core/router' );
     200                $this->assertEmpty( $state );
     201        }
    129202}
Note: See TracChangeset for help on using the changeset viewer.