Make WordPress Core

Ticket #47322: load-styles.php

File load-styles.php, 2.2 KB (added by asimbaki, 6 years ago)

Optimized and better way to stream-out the style contents.

Line 
1<?php
2
3/**
4 * Disable error reporting
5 *
6 * Set this to error_reporting( -1 ) for debugging
7 */
8error_reporting( 0 );
9
10/** Set ABSPATH for execution */
11if ( ! defined( 'ABSPATH' ) ) {
12        define( 'ABSPATH', dirname( dirname( __FILE__ ) ) . '/' );
13}
14
15define( 'WPINC', 'wp-includes' );
16
17require( ABSPATH . 'wp-admin/includes/noop.php' );
18require( ABSPATH . WPINC . '/script-loader.php' );
19require( ABSPATH . WPINC . '/version.php' );
20
21$load = $_GET['load'];
22if ( is_array( $load ) ) {
23        $load = implode( '', $load );
24}
25$load = preg_replace( '/[^a-z0-9,_-]+/i', '', $load );
26$load = array_unique( explode( ',', $load ) );
27
28if ( empty( $load ) ) {
29        exit;
30}
31
32$rtl            = ( isset( $_GET['dir'] ) && 'rtl' == $_GET['dir'] );
33$expires_offset = 31536000; // 1 year
34$out            = '';
35
36$wp_styles = new WP_Styles();
37wp_default_styles( $wp_styles );
38
39if ( isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) && stripslashes( $_SERVER['HTTP_IF_NONE_MATCH'] ) === $wp_version ) {
40        $protocol = $_SERVER['SERVER_PROTOCOL'];
41        if ( ! in_array( $protocol, array( 'HTTP/1.1', 'HTTP/2', 'HTTP/2.0' ) ) ) {
42                $protocol = 'HTTP/1.0';
43        }
44        header( "$protocol 304 Not Modified" );
45        exit();
46}
47
48header( "Etag: $wp_version" );
49header( 'Content-Type: text/css; charset=UTF-8' );
50header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + $expires_offset ) . ' GMT' );
51header( "Cache-Control: public, max-age=$expires_offset" );
52
53foreach ( $load as $handle ) {
54        if ( ! array_key_exists( $handle, $wp_styles->registered ) ) {
55                continue;
56        }
57
58        $style = $wp_styles->registered[ $handle ];
59
60        if ( empty( $style->src ) ) {
61                continue;
62        }
63
64        $path = ABSPATH . $style->src;
65
66        if ( $rtl && ! empty( $style->extra['rtl'] ) ) {
67                // All default styles have fully independent RTL files.
68                $path = str_replace( '.min.css', '-rtl.min.css', $path );
69        }
70
71        $content = get_file( $path ) . "\r\n";
72
73        if ( strpos( $style->src, '/' . WPINC . '/css/' ) === 0 ) {
74                $content = str_replace( '../images/', '../' . WPINC . '/images/', $content );
75                $content = str_replace( '../js/tinymce/', '../' . WPINC . '/js/tinymce/', $content );
76                $content = str_replace( '../fonts/', '../' . WPINC . '/fonts/', $content );
77                echo $content;
78        } else {
79                echo str_replace( '../images/', 'images/', $content );
80        }
81}
82
83exit;