Make WordPress Core

Ticket #56180: 56180.1.diff

File 56180.1.diff, 2.3 KB (added by costdev, 3 years ago)

Add filter to get_header_image(). Includes unit tests.

  • src/wp-includes/theme.php

    diff --git a/src/wp-includes/theme.php b/src/wp-includes/theme.php
    index c3d5003f75..76cc489b2e 100644
    a b function get_header_image() { 
    11791179                $url = get_random_header_image();
    11801180        }
    11811181
     1182        /**
     1183         * Filters the header image URL.
     1184         *
     1185         * @since 6.1.0
     1186         *
     1187         * @param string $url Header image URL.
     1188         */
     1189        $url = apply_filters( 'header_image', $url );
     1190
     1191        if ( ! is_string( $url ) ) {
     1192                return false;
     1193        }
     1194
     1195        $url = trim( $url );
     1196
    11821197        return sanitize_url( set_url_scheme( $url ) );
    11831198}
    11841199
  • tests/phpunit/tests/theme/customHeader.php

    diff --git a/tests/phpunit/tests/theme/customHeader.php b/tests/phpunit/tests/theme/customHeader.php
    index 958462afef..0b4b0d4f56 100644
    a b class Tests_Theme_CustomHeader extends WP_UnitTestCase { 
    119119                $this->assertStringContainsString( sprintf( 'src="%s"', $custom ), $html );
    120120        }
    121121
     122        /**
     123         * Tests the "header_image" filter.
     124         *
     125         * @ticket 56180
     126         *
     127         * @covers get_header_image
     128         *
     129         * @dataProvider data_filter_header_image
     130         *
     131         * @param mixed  $header_image The header image.
     132         * @param string $expected     The expected return value from get_header_image().
     133         */
     134        public function test_filter_header_image( $header_image, $expected ) {
     135                add_filter(
     136                        'header_image',
     137                        static function() use ( $header_image ) {
     138                                return $header_image;
     139                        }
     140                );
     141
     142                $this->assertSame( $expected, get_header_image() );
     143        }
     144
     145        /**
     146         * Data provider.
     147         *
     148         * @return array
     149         */
     150        public function data_filter_header_image() {
     151                return array(
     152                        'an image url'         => array(
     153                                'header_image' => 'http://example.org/image.png',
     154                                'expected'     => 'http://example.org/image.png',
     155                        ),
     156                        'an empty string'      => array(
     157                                'header_image' => '',
     158                                'expected'     => '',
     159                        ),
     160                        'a string with spaces' => array(
     161                                'header_image' => ' ',
     162                                'expected'     => '',
     163                        ),
     164                        'null'                 => array(
     165                                'header_image' => null,
     166                                'expected'     => false,
     167                        ),
     168                        'false'                => array(
     169                                'header_image' => false,
     170                                'expected'     => false,
     171                        ),
     172                );
     173        }
     174
    122175        public function test_get_custom_header_markup_without_registered_default_image() {
    123176                $this->add_theme_support();
    124177