: global styles, * the block library, and one #is'; if ( ! preg_match_all( $pattern, $head, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER ) ) { return []; } $skip = (array) apply_filters( 'ablocks/perf/consolidate_css/skip_handles', [] ); $runs = []; $current = null; $prev_end = null; foreach ( $matches as $match ) { $tag = $match[0][0]; $start = $match[0][1]; $end = $start + strlen( $tag ); $handle = $match[1][0]; $css = $match[2][0]; $breaks_run = false; // A media query other than "all" changes when the CSS applies; it // cannot be folded in with unconditional rules. if ( preg_match( '#\bmedia=["\']([^"\']+)["\']#i', $tag, $media ) && 'all' !== strtolower( trim( $media[1] ) ) ) { $breaks_run = true; } if ( in_array( $handle, $skip, true ) ) { $breaks_run = true; } $adjacent = ( null !== $prev_end && '' === trim( substr( $head, $prev_end, $start - $prev_end ) ) ); if ( $breaks_run ) { if ( null !== $current ) { $runs[] = $current; $current = null; } $prev_end = $end; continue; } if ( null === $current || ! $adjacent ) { if ( null !== $current ) { $runs[] = $current; } $current = [ 'start' => $start, 'end' => $end, 'css' => $css, ]; } else { $current['end'] = $end; $current['css'] .= "\n" . $css; } $prev_end = $end; }//end foreach if ( null !== $current ) { $runs[] = $current; } return $runs; } /** * Write CSS to a content-addressed file and return its URL. * * The name is a hash of the contents, so the file is immutable: changed CSS * yields a different name rather than a stale file, and no invalidation * hook is needed anywhere. * * @param string $css Stylesheet contents. * @return string|null Public URL, or null on failure. */ private function store_css( $css ) { $hash = substr( sha1( $css ), 0, 20 ); $dir = self::base_dir(); $file = $dir . '/' . $hash . '.css'; if ( ! file_exists( $file ) ) { if ( ! is_dir( $dir ) && ! wp_mkdir_p( $dir ) ) { return null; } self::protect_dir(); // Written through the page-cache store so both features share one // atomic write-then-rename implementation. if ( ! \ABlocks\Classes\PageCache\Store::put_file( $file, $css ) ) { return null; } } return self::base_url() . '/' . $hash . '.css'; } /** * Absolute path to the consolidated CSS directory. * * @return string */ public static function base_dir() { $upload = new FileUpload(); return untrailingslashit( $upload->get_upload_dir() ) . '/' . self::DIR_NAME; } /** * Public URL of the consolidated CSS directory. * * @return string */ public static function base_url() { $upload = new FileUpload(); return untrailingslashit( $upload->get_upload_url() ) . '/' . self::DIR_NAME; } /** * Drop an index.php guard so the directory cannot be browsed. */ private static function protect_dir() { $index = self::base_dir() . '/index.php'; if ( ! file_exists( $index ) ) { \ABlocks\Classes\PageCache\Store::put_file( $index, "