get( 'Version' ), get_bloginfo( 'version' ), defined( 'SUBTLEFORMS_VERSION' ) ? SUBTLEFORMS_VERSION : '', (string) get_option( self::REVISION_OPTION, 0 ), ) ) ); } /** * The assets, cached. * * @return array{stylesheets:string[],inlineCss:string,globalCss:string,fontFaces:string,isBlockTheme:bool,bodyClasses:string[],contentClass:string,degraded:bool} */ public static function collect(): array { $key = self::cacheKey(); $cached = get_transient( $key ); if ( is_array( $cached ) && isset( $cached['stylesheets'], $cached['bodyClasses'] ) ) { return $cached; } $data = self::compute(); // A degraded answer is not kept: the next request tries again. if ( empty( $data['degraded'] ) ) { set_transient( $key, $data, self::TTL ); } return $data; } /** * Run the theme's enqueue and read the result. Never throws. * * @return array */ public static function compute(): array { $base = self::baseData(); try { $found = self::runEnqueue(); } catch ( \Throwable $e ) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) { // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log error_log( 'SubtleForms: theme preview assets fell back to the stylesheet files: ' . $e->getMessage() ); } return array_merge( $base, array( 'stylesheets' => self::fallbackStylesheets() ), array( 'degraded' => true ) ); } if ( array() === $found['stylesheets'] ) { // A theme that enqueues nothing of its own still has its stylesheet file. $found['stylesheets'] = self::fallbackStylesheets(); } return array_merge( $base, $found, array( 'degraded' => false ) ); } /** * The parts that do not depend on running the enqueue. * * @return array */ private static function baseData(): array { $is_block = function_exists( 'wp_is_block_theme' ) && wp_is_block_theme(); $slug = sanitize_html_class( get_stylesheet() ); if ( $is_block ) { $body = array( 'wp-singular', 'page', 'wp-embed-responsive', 'wp-theme-' . $slug ); $content = 'wp-block-post-content is-layout-constrained has-global-padding entry-content'; } else { $body = array( 'page', 'wp-embed-responsive', $slug ); $content = 'entry-content'; } return array( 'stylesheets' => array(), 'inlineCss' => '', 'globalCss' => function_exists( 'wp_get_global_stylesheet' ) ? (string) wp_get_global_stylesheet() : '', 'fontFaces' => ThemeStyles::fontFaceCss(), 'isBlockTheme' => $is_block, 'bodyClasses' => $body, 'contentClass' => $content, 'degraded' => false, ); } /** * The theme's parent and child stylesheet files, as the preview loaded * them before it ran the theme's enqueue. * * @return string[] */ private static function fallbackStylesheets(): array { $hrefs = array(); if ( is_child_theme() ) { $hrefs[] = get_template_directory_uri() . '/style.css'; } $own = get_stylesheet_uri(); if ( is_string( $own ) && '' !== $own && ! in_array( $own, $hrefs, true ) ) { $hrefs[] = $own; } return $hrefs; } /** * Whether a stylesheet address belongs to the theme or to WordPress. * * @param string $href Address. * @return bool */ private static function isKept( string $href ): bool { $strip = static function ( string $url ): string { return (string) preg_replace( '#^https?:#i', '', $url ); }; $href = $strip( $href ); foreach ( array( get_template_directory_uri(), get_stylesheet_directory_uri(), includes_url() ) as $root ) { $root = trailingslashit( $strip( (string) $root ) ); if ( 0 === strpos( $href, $root ) ) { return true; } } return false; } /** * Run the enqueue hook with the queues swapped out and read what it queued. * * @return array{stylesheets:string[],inlineCss:string} * @throws \Throwable When a theme's callback fails. */ private static function runEnqueue(): array { $saved_styles = $GLOBALS['wp_styles'] ?? null; $saved_scripts = $GLOBALS['wp_scripts'] ?? null; $level = ob_get_level(); $quiet = '__return_false'; // A theme that checks the main query before it exists says so with a notice. add_filter( 'doing_it_wrong_trigger_error', $quiet ); ob_start(); try { // A fresh queue, with WordPress's own styles registered as on the front end. $GLOBALS['wp_styles'] = null; $GLOBALS['wp_scripts'] = null; $styles = wp_styles(); do_action( 'wp_enqueue_scripts' ); $styles->all_deps( $styles->queue ); $hrefs = array(); $inline = array(); foreach ( $styles->to_do as $handle ) { $item = $styles->registered[ $handle ] ?? null; if ( ! $item || ! is_string( $item->src ) || '' === $item->src ) { continue; } if ( ! empty( $item->extra['conditional'] ) || ( ! empty( $item->args ) && 'print' === $item->args ) ) { continue; } $href = (string) $styles->_css_href( $item->src, $item->ver, $handle ); if ( '' === $href || ! self::isKept( $href ) || in_array( $href, $hrefs, true ) ) { continue; } $hrefs[] = $href; $after = $styles->get_data( $handle, 'after' ); if ( is_array( $after ) ) { foreach ( $after as $rule ) { if ( is_string( $rule ) && '' !== trim( $rule ) ) { $inline[] = trim( $rule ); } } } } } finally { while ( ob_get_level() > $level ) { ob_end_clean(); } remove_filter( 'doing_it_wrong_trigger_error', $quiet ); $GLOBALS['wp_styles'] = $saved_styles; $GLOBALS['wp_scripts'] = $saved_scripts; } return array( 'stylesheets' => $hrefs, 'inlineCss' => implode( "\n", $inline ), ); } }