'15px', '--wp--custom--border' => "$border_width $border_style $border_color", '--wp--custom--body--background' => $bodyBackground, '--wp--custom--body--color' => $body_color, '--wp--custom--heading--font-weight' => $heading_font_weight, '--wp--custom--heading--font-family' => $heading_font_family, // Used by .button. '--wp--custom--button--background' => $button_background, '--wp--custom--button--color' => $button_text, '--wp--custom--button--padding-top' => $button_padding['top'] ?? null, '--wp--custom--button--padding-right' => $button_padding['right'] ?? null, '--wp--custom--button--padding-bottom' => $button_padding['bottom'] ?? null, '--wp--custom--button--padding-left' => $button_padding['left'] ?? null, '--wp--custom--button--border-radius' => $button_border_radius, '--wp--custom--button--border-width' => $button_border_width, '--wp--custom--button--font-size' => $button_font_size, '--wp--custom--button--font-weight' => $button_font_weight, '--wp--custom--button--line-height' => $button_line_height, ]; if ( $box_shadow ) { if ( is_array( $box_shadow ) ) { $inset = $box_shadow['inset'] ?? ' '; $x = $box_shadow['x'] ?? null; $y = $box_shadow['y'] ?? null; $blur = $box_shadow['blur'] ?? null; $spread = $box_shadow['spread'] ?? null; $color = $box_shadow['color'] ?? null; $box_shadow = "$inset $x $y $blur $spread $color"; } $all = array_merge( $all, [ '--wp--custom--box-shadow' => $box_shadow, ] ); } $css = $element . '{' . css_array_to_string( $all ) . '}'; wp_add_inline_style( $handle, $css ); } /** * Enqueues front end scripts. * * @since 0.0.2 * * @return void */ function add_block_styles(): void { global $wp_styles; wp_dequeue_style( 'wp-block-library-theme' ); if ( ! is_a( $wp_styles, 'WP_Styles' ) ) { return; } $handles = array_flip( $wp_styles->queue ); foreach ( $wp_styles->registered as $handle => $style ) { if ( ! isset( $handles[ $handle ] ) ) { continue; } $slug = str_replace( 'wp-block-', '', $handle ); $file = DIR . 'assets/css/blocks/' . $slug . '.css'; if ( file_exists( $file ) ) { wp_add_inline_style( $handle, file_get_contents( $file ) ); } } } /** * Get all stylesheets to load conditionally. * * @since 0.9.10 * * @return array */ function get_conditional_stylesheets(): array { return [ ...( is_admin() ? glob( DIR . 'assets/css/blocks/*.css' ) : [] ), ...glob( DIR . 'assets/css/elements/*.css' ), ...glob( DIR . 'assets/css/components/*.css' ), ...glob( DIR . 'assets/css/formats/*.css' ), ...glob( DIR . 'assets/css/extensions/*.css' ), ...glob( DIR . 'assets/css/block-styles/*.css' ), ...glob( DIR . 'assets/css/utility/*.css' ), ...glob( DIR . 'assets/css/plugins/*.css' ), ]; } /** * Adds split styles. * * @since 0.0.27 * * @param array $stylesheets Stylesheets to load. * @param string $content Page content. * * @return array */ function get_stylesheet_conditions( array $stylesheets, string $content ): array { $conditions = []; // Load all block CSS in admin. foreach ( $stylesheets as $stylesheet ) { $dir = basename( dirname( $stylesheet ) ); $file = basename( $stylesheet, '.css' ); if ( $dir === 'blocks' ) { $conditions['blocks'][ $file ] = true; } } $conditions['block-styles'] = [ 'accordion' => str_contains( $content, 'is-style-accordion' ), 'button-outline' => str_contains( $content, 'is-style-outline' ), 'button-secondary' => str_contains( $content, 'is-style-secondary' ), 'checklist-circle' => str_contains( $content, 'is-style-checklist-circle' ), 'checklist' => str_contains( $content, 'is-style-checklist' ), 'curved-text' => str_contains( $content, 'is-style-curved-text' ), 'divider-angle' => str_contains( $content, 'is-style-angle' ), 'divider-curve' => str_contains( $content, 'is-style-curve' ), 'divider-fade' => str_contains( $content, 'is-style-fade' ), 'divider-round' => str_contains( $content, 'is-style-round' ), 'divider-wave' => str_contains( $content, 'is-style-wave' ), 'mega-menu' => str_contains( $content, 'is-style-mega-menu' ), 'notice' => str_contains( $content, 'is-style-notice' ), 'numbered-list' => str_contains( $content, 'is-style-numbered' ), 'search-toggle' => str_contains( $content, 'is-style-toggle' ), 'square-list' => str_contains( $content, 'is-style-square' ), 'sub-heading' => str_contains( $content, 'is-style-sub-heading' ), 'surface' => str_contains( $content, 'is-style-surface' ), ]; $conditions['elements'] = [ 'all' => true, 'big' => str_contains( $content, ' str_contains( $content, ' true, 'button' => str_contains_any( $content, [ ' str_contains( $content, ' str_contains( $content, ' str_contains( $content, ' str_contains_any( $content, [ ' true, 'link' => str_contains( $content, ' str_contains( $content, ' str_contains( $content, ' str_contains( $content, ' str_contains( $content, ' str_contains( $content, ' str_contains( $content, ' str_contains( $content, ' str_contains( $content, ' str_contains( $content, ' is_admin_bar_showing(), 'border' => str_contains( $content, 'border-width:' ), 'drop-cap' => str_contains( $content, 'has-drop-cap' ), 'layout' => true, 'placeholder-image' => str_contains( $content, 'is-placeholder' ), 'screen-reader-text' => true, 'site-blocks' => true, ]; $conditions['extensions'] = [ 'animation' => str_contains_any( $content, [ 'has-animation', 'will-animate' ] ), 'box-shadow' => str_contains( $content, 'has-box-shadow' ), 'counter' => str_contains( $content, 'is-style-counter' ), 'dark-mode' => str_contains( $content, 'toggle-switch' ), 'filter' => str_contains( $content, 'has-filter' ), 'icon' => str_contains( $content, 'is-style-icon' ), 'marquee' => str_contains( $content, 'is-marquee' ), 'transform' => str_contains( $content, 'has-transform' ), ]; $conditions['formats'] = [ 'arrow' => str_contains( $content, 'is-underline-arrow' ), 'brush' => str_contains( $content, 'is-underline-brush' ), 'circle' => str_contains( $content, 'is-underline-circle' ), 'gradient' => str_contains( $content, 'has-text-gradient' ), 'highlight' => str_contains( $content, 'has-inline-color' ), 'underline' => str_contains( $content, 'has-text-underline' ), 'font-size' => str_contains( $content, 'has-custom-font-size' ), 'inline-svg' => str_contains( $content, 'inline-svg' ), 'outline' => str_contains( $content, 'has-text-outline' ), ]; $conditions['utility'] = [ 'button-width' => str_contains( $content, 'wp-block-button__width-' ), 'margin' => str_contains( $content, ' margin-auto' ), ]; $conditions['plugins'] = [ 'ninja-forms' => str_contains( $content, 'nf-form' ), 'syntax-highlighting-code-block' => defined( 'Syntax_Highlighting_Code_Block\\PLUGIN_VERSION' ), 'edd' => class_exists( 'EDD_Requirements_Check' ), 'gravity-forms' => class_exists( 'GFForms' ), 'woocommerce' => class_exists( 'WooCommerce' ), ]; return $conditions; } /** * Adds conditional stylesheets inline. * * @since 0.0.27 * * @param array $stylesheets Stylesheets. * @param array $conditions Conditions. * @param string $handle Stylesheet handle. * @param string $content Block content. * * @return void */ function add_conditional_stylesheets( array $stylesheets, array $conditions, string $handle, string $content ): void { $styles = ''; $url = get_site_url(); foreach ( $stylesheets as $stylesheet ) { $dir = basename( dirname( $stylesheet ) ); $condition = $conditions[ $dir ][ basename( $stylesheet, '.css' ) ]; // Fix for icons. if ( str_contains( $url, 'wp-themes.com' ) && ! is_front_page() ) { $condition = true; } if ( $condition || $content === '' ) { $styles .= trim( file_get_contents( $stylesheet ) ); } if ( is_admin() ) { add_editor_style( 'assets/css/' . $dir . DS . basename( $stylesheet ) ); } } wp_add_inline_style( $handle, $styles ); } /** * Adds responsive styles. * * @since 0.0.27 * * @param string $content Page content. * @param string $handle Stylesheet handle. * * @return void */ function add_responsive_styles( string $content, string $handle ): void { $properties = array_keys( get_responsive_settings() ); $mobile = ''; $desktop = ''; foreach ( $properties as $property ) { $split = preg_split( '/(?=[A-Z])/', $property ); $property = implode( '-', array_map( 'strtolower', $split ) ); if ( ! is_admin() && ! str_contains( $content, 'has-' . $property ) ) { continue; } $mobile .= ".has-{$property}{{$property}:var(--{$property})!important}"; $desktop .= ".has-{$property}{{$property}:var(--{$property}-desktop,var(--{$property}))!important}"; } wp_add_inline_style( $handle, "{$mobile}@media(min-width:782px){{$desktop}}" ); } add_action( 'admin_init', NS . 'add_editor_stylesheets' ); /** * Description of expected behavior. * * @since 0.9.10 * * @return void */ function add_editor_stylesheets() { $dirs = glob( DIR . 'assets/css/*', GLOB_ONLYDIR ); foreach ( $dirs as $dir ) { $files = glob( $dir . '/*.css' ); foreach ( $files as $file ) { $stylesheet = 'assets/css/' . basename( $dir ) . DS . basename( $file ); add_editor_style( $stylesheet ); } } } add_action( 'blockify_editor_scripts', NS . 'enqueue_editor_only_styles', 9 ); /** * Enqueues editor assets. * * @since 0.3.3 * * @return void */ function enqueue_editor_only_styles(): void { wp_dequeue_style( 'wp-block-library-theme' ); wp_register_style( 'blockify-editor', get_url() . 'assets/css/editor.css', [], filemtime( DIR . 'assets/css/editor.css' ) ); wp_enqueue_style( 'blockify-editor' ); } add_filter( 'theme_json_theme', NS . 'fix_layout_sizes' ); add_filter( 'wp_theme_json_data_theme', NS . 'fix_layout_sizes' ); /** * Filters theme.json font families. * * @todo Move layout settings to separate file. * * @since 0.4.2 * * @param mixed $theme_json WP_Theme_JSON_Data | WP_Theme_JSON_Data_Gutenberg. * * @return mixed */ function fix_layout_sizes( $theme_json ) { $default = $theme_json->get_data(); $new = []; $content_size = $default['settings']['layout']['contentSize'] ?? 'min(calc(100vw - 3rem), 800px)'; $wide_size = $default['settings']['layout']['wideSize'] ?? 'min(calc(100vw - 3rem), 1200px)'; if ( is_admin() ) { $content_size = str_replace( 'vw', '%', $content_size ); $wide_size = str_replace( 'vw', '%', $wide_size ); } $new['settings']['layout']['contentSize'] = $content_size; $new['settings']['layout']['wideSize'] = $wide_size; $theme_json->update_with( array_merge( $default, $new ) ); return $theme_json; } add_filter( 'wp_theme_json_data_theme', NS . 'theme_json_fix', 11 ); /** * Temporary fix for parent theme.json not loading in wp.org. * * @since 1.0.0 * * @param mixed $theme_json \WP_Theme_JSON_Data|\WP_Theme_JSON_Data_Gutenberg. * * @return mixed */ function theme_json_fix( $theme_json ) { if ( ! str_contains( home_url(), 'wp-themes.com' ) || ! is_child_theme() ) { return $theme_json; } $theme_json->update_with( array_replace_recursive( wp_json_file_decode( DIR . 'theme.json', [ 'associative' => true ] ), wp_json_file_decode( get_stylesheet_directory() . '/theme.json', [ 'associative' => true ] ) ) ); return $theme_json; }