Make WordPress Core

Changeset 38901


Ignore:
Timestamp:
10/25/2016 04:46:36 AM (9 years ago)
Author:
westonruter
Message:

Customize: Prevent absent site icon link from outputting an empty string as href in customizer; use /favicon.ico as fallback.

An empty string causes some browsers to use the current URL as the href. When using history.replaceState() Chrome will re-fetch the favicon with each call, meaning that customize.php gets hit with wasted requests which tax the server.

Fixes #38377.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/js/customize-controls.js

    r38900 r38901  
    33343334         */
    33353335        setImageFromAttachment: function( attachment ) {
    3336             var sizes = [ 'site_icon-32', 'thumbnail', 'full' ],
     3336            var sizes = [ 'site_icon-32', 'thumbnail', 'full' ], link,
    33373337                icon;
    33383338
     
    33483348            this.setting( attachment.id );
    33493349
     3350            if ( ! icon ) {
     3351                return;
     3352            }
     3353
    33503354            // Update the icon in-browser.
    3351             $( 'link[sizes="32x32"]' ).attr( 'href', icon.url );
     3355            link = $( 'link[rel="icon"][sizes="32x32"]' );
     3356            link.attr( 'href', icon.url );
    33523357        },
    33533358
     
    33663371            this.setting( '' );
    33673372            this.renderContent(); // Not bound to setting change when emptying.
    3368             $( 'link[rel="icon"]' ).attr( 'href', '' );
     3373            $( 'link[rel="icon"][sizes="32x32"]' ).attr( 'href', '/favicon.ico' ); // Set to default.
    33693374        }
    33703375    });
  • trunk/src/wp-includes/general-template.php

    r38857 r38901  
    27772777    }
    27782778
    2779     $meta_tags = array(
    2780         sprintf( '<link rel="icon" href="%s" sizes="32x32" />', esc_url( get_site_icon_url( 32 ) ) ),
    2781         sprintf( '<link rel="icon" href="%s" sizes="192x192" />', esc_url( get_site_icon_url( 192 ) ) ),
    2782         sprintf( '<link rel="apple-touch-icon-precomposed" href="%s" />', esc_url( get_site_icon_url( 180 ) ) ),
    2783         sprintf( '<meta name="msapplication-TileImage" content="%s" />', esc_url( get_site_icon_url( 270 ) ) ),
    2784     );
     2779    $meta_tags = array();
     2780    $icon_32 = get_site_icon_url( 32 );
     2781    if ( empty( $icon_32 ) && is_customize_preview() ) {
     2782        $icon_32 = '/favicon.ico'; // Serve default favicon URL in customizer so element can be updated for preview.
     2783    }
     2784    if ( $icon_32 ) {
     2785        $meta_tags[] = sprintf( '<link rel="icon" href="%s" sizes="32x32" />', esc_url( $icon_32 ) );
     2786    }
     2787    $icon_192 = get_site_icon_url( 192 );
     2788    if ( $icon_192 ) {
     2789        $meta_tags[] = sprintf( '<link rel="icon" href="%s" sizes="192x192" />', esc_url( $icon_192 ) );
     2790    }
     2791    $icon_180 = get_site_icon_url( 180 );
     2792    if ( $icon_180 ) {
     2793        $meta_tags[] = sprintf( '<link rel="apple-touch-icon-precomposed" href="%s" />', esc_url( $icon_180 ) );
     2794    }
     2795    $icon_270 = get_site_icon_url( 270 );
     2796    if ( $icon_270 ) {
     2797        $meta_tags[] = sprintf( '<meta name="msapplication-TileImage" content="%s" />', esc_url( $icon_270 ) );
     2798    }
    27852799
    27862800    /**
  • trunk/tests/phpunit/tests/general/template.php

    r38786 r38901  
    2424
    2525    function tearDown() {
     26        global $wp_customize;
    2627        $this->_remove_custom_logo();
     28        $this->_remove_site_icon();
     29        $wp_customize = null;
    2730
    2831        parent::tearDown();
     
    5255        $this->expectOutputString( $this->site_icon_url );
    5356        site_icon_url();
    54         $this->_remove_site_icon();
    5557    }
    5658
     
    118120        $this->expectOutputString( $output );
    119121        wp_site_icon();
    120 
    121         $this->_remove_site_icon();
    122122    }
    123123
     
    144144        wp_site_icon();
    145145        remove_filter( 'site_icon_meta_tags', array( $this, '_custom_site_icon_meta_tag' ) );
    146 
    147         $this->_remove_site_icon();
     146    }
     147
     148    /**
     149     * @group site_icon
     150     * @ticket 38377
     151     */
     152    function test_customize_preview_wp_site_icon_empty() {
     153        global $wp_customize;
     154        wp_set_current_user( $this->factory()->user->create( array( 'role' => 'administrator' ) ) );
     155
     156        require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
     157        $wp_customize = new WP_Customize_Manager();
     158        $wp_customize->register_controls();
     159        $wp_customize->start_previewing_theme();
     160
     161        $this->expectOutputString( '<link rel="icon" href="/favicon.ico" sizes="32x32" />' . "\n" );
     162        wp_site_icon();
     163    }
     164
     165    /**
     166     * @group site_icon
     167     * @ticket 38377
     168     */
     169    function test_customize_preview_wp_site_icon_dirty() {
     170        global $wp_customize;
     171        wp_set_current_user( $this->factory()->user->create( array( 'role' => 'administrator' ) ) );
     172
     173        require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
     174        $wp_customize = new WP_Customize_Manager();
     175        $wp_customize->register_controls();
     176        $wp_customize->start_previewing_theme();
     177
     178        $attachment_id = $this->_insert_attachment();
     179        $wp_customize->set_post_value( 'site_icon', $attachment_id );
     180        $wp_customize->get_setting( 'site_icon' )->preview();
     181        $output = array(
     182            sprintf( '<link rel="icon" href="%s" sizes="32x32" />', esc_url( wp_get_attachment_image_url( $attachment_id, 32 ) ) ),
     183            sprintf( '<link rel="icon" href="%s" sizes="192x192" />', esc_url( wp_get_attachment_image_url( $attachment_id, 192 ) ) ),
     184            sprintf( '<link rel="apple-touch-icon-precomposed" href="%s" />', esc_url( wp_get_attachment_image_url( $attachment_id, 180 ) ) ),
     185            sprintf( '<meta name="msapplication-TileImage" content="%s" />', esc_url( wp_get_attachment_image_url( $attachment_id, 270 ) ) ),
     186            '',
     187        );
     188        $output = implode( "\n", $output );
     189        $this->expectOutputString( $output );
     190        wp_site_icon();
    148191    }
    149192
Note: See TracChangeset for help on using the changeset viewer.