WordPress.org

Make WordPress Core

Ticket #43825: 43825.patch

File 43825.patch, 4.3 KB (added by kanishk.dudeja, 4 years ago)

A patch illustrating the enhancement

  • src/wp-admin/load-scripts.php

     
    3333$compress       = ( isset( $_GET['c'] ) && $_GET['c'] );
    3434$force_gzip     = ( $compress && 'gzip' == $_GET['c'] );
    3535$expires_offset = 31536000; // 1 year
     36$cache_method   = isset( $_GET['cache-method'] ) ? trim( $_GET['cache-method'] ) : 'wp-version';
    3637$out            = '';
    3738
    3839$wp_scripts = new WP_Scripts();
    3940wp_default_scripts( $wp_scripts );
    4041
    41 if ( isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) && stripslashes( $_SERVER['HTTP_IF_NONE_MATCH'] ) === $wp_version ) {
     42$valid_handles     = array();
     43$bundle_identifier = '';
     44
     45foreach ( $load as $handle ) {
     46        if ( ! array_key_exists( $handle, $wp_scripts->registered ) ) {
     47                continue;
     48        }
     49
     50        $valid_handles[] = $handle;
     51
     52        $version = $wp_scripts->registered[$handle]->ver;
     53
     54        if ( false === $version || is_null( $version ) ) {
     55                $version = '1';
     56        }
     57
     58        $bundle_identifier = $bundle_identifier . $handle . '-' . $version . '-';
     59}
     60
     61if ( $cache_method == 'script-versions' ) {
     62        $etag_header_value = md5( $bundle_identifier );
     63} else {
     64        $etag_header_value = $wp_version;
     65}
     66
     67if ( isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) && stripslashes( $_SERVER['HTTP_IF_NONE_MATCH'] ) === $etag_header_value ) {
    4268        $protocol = $_SERVER['SERVER_PROTOCOL'];
    4369        if ( ! in_array( $protocol, array( 'HTTP/1.1', 'HTTP/2', 'HTTP/2.0' ) ) ) {
    4470                $protocol = 'HTTP/1.0';
     
    4773        exit();
    4874}
    4975
    50 foreach ( $load as $handle ) {
    51         if ( ! array_key_exists( $handle, $wp_scripts->registered ) ) {
    52                 continue;
    53         }
    54 
     76foreach ( $valid_handles as $handle ) {
    5577        $path = ABSPATH . $wp_scripts->registered[ $handle ]->src;
    5678        $out .= get_file( $path ) . "\n";
    5779}
    5880
    59 header( "Etag: $wp_version" );
     81header( "Etag: $etag_header_value" );
    6082header( 'Content-Type: application/javascript; charset=UTF-8' );
    6183header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + $expires_offset ) . ' GMT' );
    6284header( "Cache-Control: public, max-age=$expires_offset" );
  • src/wp-admin/load-styles.php

     
    3333$force_gzip     = ( $compress && 'gzip' == $_GET['c'] );
    3434$rtl            = ( isset( $_GET['dir'] ) && 'rtl' == $_GET['dir'] );
    3535$expires_offset = 31536000; // 1 year
     36$cache_method   = isset( $_GET['cache-method'] ) ? trim( $_GET['cache-method'] ) : 'wp-version';
    3637$out            = '';
    3738
    3839$wp_styles = new WP_Styles();
    3940wp_default_styles( $wp_styles );
    4041
    41 if ( isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) && stripslashes( $_SERVER['HTTP_IF_NONE_MATCH'] ) === $wp_version ) {
     42$valid_handles     = array();
     43$bundle_identifier = '';
     44
     45foreach ( $load as $handle ) {
     46        if ( ! array_key_exists( $handle, $wp_styles->registered ) )
     47                continue;
     48
     49        if ( empty( $wp_styles->registered[$handle]->src ) ) {
     50                continue;
     51        }
     52
     53        $valid_handles[] = $handle;
     54
     55        $version = $wp_styles->registered[$handle]->ver;
     56
     57        if ( false === $version || is_null( $version ) ) {
     58                $version = '1';
     59        }
     60
     61        $bundle_identifier = $bundle_identifier . $handle . '-' . $version . '-';
     62}
     63
     64if ( $cache_method == 'style-versions' ) {
     65        $etag_header_value = md5( $bundle_identifier );
     66} else {
     67        $etag_header_value = $wp_version;
     68}
     69
     70if ( isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) && stripslashes( $_SERVER['HTTP_IF_NONE_MATCH'] ) === $etag_header_value ) {
    4271        $protocol = $_SERVER['SERVER_PROTOCOL'];
    4372        if ( ! in_array( $protocol, array( 'HTTP/1.1', 'HTTP/2', 'HTTP/2.0' ) ) ) {
    4473                $protocol = 'HTTP/1.0';
     
    4776        exit();
    4877}
    4978
    50 foreach ( $load as $handle ) {
    51         if ( ! array_key_exists( $handle, $wp_styles->registered ) ) {
    52                 continue;
    53         }
    54 
     79foreach ( $valid_handles as $handle ) {
    5580        $style = $wp_styles->registered[ $handle ];
    5681
    57         if ( empty( $style->src ) ) {
    58                 continue;
    59         }
    60 
    6182        $path = ABSPATH . $style->src;
    6283
    6384        if ( $rtl && ! empty( $style->extra['rtl'] ) ) {
     
    7798        }
    7899}
    79100
    80 header( "Etag: $wp_version" );
     101
     102header( "Etag: $etag_header_value" );
    81103header( 'Content-Type: text/css; charset=UTF-8' );
    82104header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + $expires_offset ) . ' GMT' );
    83105header( "Cache-Control: public, max-age=$expires_offset" );