load-styles.php 2.5 KB

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