Make WordPress Core


Ignore:
Timestamp:
10/19/2016 09:28:22 AM (10 years ago)
Author:
swissspidy
Message:

Resource Hints: Allow passing custom attributes to resource hints.

[37920] introduced resource hints that allow browsers to prefetch specific pages or render them in the background. With this change, the as, crossorigin, pr, and type attributes can be passed in addition to the URLs/hosts.

Props peterwilsoncc, swissspidy.
Fixes #38121.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/general/resourceHints.php

    r38447 r38826  
    22
    33/**
    4  * @group  template
     4 * @group template
    55 * @ticket 34292
    66 */
     
    243243                return $hints;
    244244        }
     245
     246        /**
     247         * @group 38121
     248         */
     249        function test_custom_attributes() {
     250                $expected = "<link rel='dns-prefetch' href='//s.w.org' />\n" .
     251                            "<link rel='preconnect' href='https://make.wordpress.org' />\n" .
     252                            "<link crossorigin as='image' pr='0.5' href='https://example.com/foo.jpeg' rel='prefetch' />\n" .
     253                            "<link crossorigin='use-credentials' as='style' href='https://example.com/foo.css' rel='prefetch' />\n" .
     254                            "<link href='http://wordpress.org' rel='prerender' />\n";
     255
     256                add_filter( 'wp_resource_hints', array( $this, '_add_url_with_attributes' ), 10, 2 );
     257
     258                $actual = get_echo( 'wp_resource_hints' );
     259
     260                remove_filter( 'wp_resource_hints', array( $this, '_add_url_with_attributes' ) );
     261
     262                $this->assertEquals( $expected, $actual );
     263        }
     264
     265        function _add_url_with_attributes( $hints, $method ) {
     266                // Ignore hints with missing href attributes.
     267                $hints[] = array(
     268                        'rel'  => 'foo',
     269                );
     270
     271                if ( 'preconnect' === $method ) {
     272                        // Should ignore rel attributes.
     273                        $hints[] = array(
     274                                'rel'  => 'foo',
     275                                'href' => 'https://make.wordpress.org/great-again',
     276                        );
     277                } elseif ( 'prefetch' === $method ) {
     278                        $hints[] = array(
     279                                'crossorigin',
     280                                'as'   => 'image',
     281                                'pr'   => 0.5,
     282                                'href' => 'https://example.com/foo.jpeg',
     283                        );
     284                        $hints[] = array(
     285                                'crossorigin' => 'use-credentials',
     286                                'as'          => 'style',
     287                                'href'        => 'https://example.com/foo.css',
     288                        );
     289                } elseif ( 'prerender' === $method ) {
     290                        // Ignore invalid attributes.
     291                        $hints[] = array(
     292                                'foo'  => 'bar',
     293                                'bar'  => 'baz',
     294                                'href' => 'http://wordpress.org',
     295                        );
     296                }
     297
     298                return $hints;
     299        }
    245300}
Note: See TracChangeset for help on using the changeset viewer.