Make WordPress Core

Ticket #42438: 42438-function.diff

File 42438-function.diff, 3.6 KB (added by adamsilverstein, 2 years ago)
  • src/wp-includes/general-template.php

    diff --git src/wp-includes/general-template.php src/wp-includes/general-template.php
    index 3f042865ad..10120a11de 100644
    function wp_resource_hints() { 
    34233423        }
    34243424}
    34253425
     3426/**
     3427 * Specify resources to preload.
     3428 *
     3429 * @global array $wp_preload_resources
     3430 * @since 6.1.0
     3431 *
     3432 * @param array  $preload_resources {
     3433 *     Array of resources and their attributes, or URLs to print for resource preloads.
     3434 *
     3435 *     @type array ...$0 {
     3436 *         Array of resource attributes.
     3437 *
     3438 *         @type string $href        URL to include in resource preloads. Required.
     3439 *         @type string $as          How the browser should treat the resource
     3440 *                                   (`script`, `style`, `image`, `document`, etc).
     3441 *         @type string $crossorigin Indicates the CORS policy of the specified resource.
     3442 *         @type string $type        Type of the resource (`text/html`, `text/css`, etc).
     3443 *         @type string $media       Accepts media types or media queries. Allows responsive preloading.
     3444 *         @type string $imagesizes  Responsive source size to the source Set.
     3445 *         @type string $imagesrcset Responsive image sources to the source set.
     3446 *     }
     3447 * }
     3448 */
     3449function wp_add_preload_resources( $preload_resources ) {
     3450        global $wp_preload_resources;
     3451
     3452        if ( ! is_array( $wp_preload_resources ) ) {
     3453                $wp_preload_resources = array();
     3454        }
     3455        $wp_preload_resources = array_merge( $wp_preload_resources, $preload_resources );
     3456}
     3457
    34263458/**
    34273459 * Prints resource preloads directives to browsers.
    34283460 *
    function wp_resource_hints() { 
    34373469 * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types/preload
    34383470 * @link https://web.dev/preload-responsive-images/
    34393471 *
     3472 * @global array $wp_preload_resources
    34403473 * @since 6.1.0
    34413474 */
    34423475function wp_preload_resources() {
     3476        global $wp_preload_resources;
    34433477        /**
    34443478         * Filters domains and URLs for resource preloads.
    34453479         *
    function wp_preload_resources() { 
    34623496         *     }
    34633497         * }
    34643498         */
    3465         $preload_resources = apply_filters( 'wp_preload_resources', array() );
    3466 
     3499        $preload_resources = apply_filters( 'wp_preload_resources', is_array( $wp_preload_resources ) ? $wp_preload_resources : array() );
    34673500        if ( ! is_array( $preload_resources ) ) {
    34683501                return;
    34693502        }
  • tests/phpunit/tests/general/wpPreloadResources.php

    diff --git tests/phpunit/tests/general/wpPreloadResources.php tests/phpunit/tests/general/wpPreloadResources.php
    index 53fd2646a6..b0c43e79cd 100644
     
    55 * @group template
    66 * @ticket 42438
    77 * @covers ::wp_preload_resources
     8 * @covers ::wp_add_preload_links
    89 */
    910class Tests_General_wpPreloadResources extends WP_UnitTestCase {
     11        /**
     12         * Set up.
     13         */
     14        public function set_up() {
     15                parent::set_up();
    1016
     17                global $wp_preload_resources;
     18                $wp_preload_resources = array();
     19        }
    1120        /**
    1221         * @dataProvider data_preload_resources
    1322         *
    class Tests_General_wpPreloadResources extends WP_UnitTestCase { 
    2534                $this->assertSame( $expected, $actual );
    2635        }
    2736
     37        /**
     38         * Test the `wp_add_preload_links()` function.
     39         *
     40         * @dataProvider data_preload_resources
     41         *
     42         * @ticket 42438
     43         */
     44        public function test_wp_add_preload_resources( $expected, $preload_resources ) {
     45                wp_add_preload_resources( $preload_resources );
     46                $actual = get_echo( 'wp_preload_resources' );
     47                $this->assertSame( $expected, $actual );
     48        }
     49
    2850        /**
    2951         * Test provider for all preload link possible combinations.
    3052         *
    class Tests_General_wpPreloadResources extends WP_UnitTestCase { 
    249271                        ),
    250272                );
    251273        }
    252 
    253274}