'postMessage' instead of the default 'transport' * => 'refresh' * * @return void */ public static function live_preview() { /* Live Preview using Javascript and postMessage Transport */ } /** * Enqueue customizer scripts. * * @return void */ public static function enqueue_scripts() { wp_enqueue_script( 'chosen', THEMEOO_THEME_URI . '/assets/plugins/chosen/chosen.jquery.min.js', ['jquery'], THEMEOO_THEME_VERSION, true ); wp_enqueue_script( 'themeoo-customizer', THEMEOO_THEME_URI . 'assets/dist/js/customizer.js', [ 'jquery', 'wp-color-picker' ], THEMEOO_THEME_VERSION, true ); wp_localize_script( 'themeoo-customizer', 'themeooCustomizerFontsL10n', themeoo_get_all_fonts() ); wp_enqueue_style( 'chosen', THEMEOO_THEME_URI . '/assets/plugins/chosen/chosen.min.css', [], THEMEOO_THEME_VERSION ); wp_enqueue_style( 'themeoo-customizer', THEMEOO_THEME_URI . 'assets/dist/css/customizer.css', [], THEMEOO_THEME_VERSION ); } public static function preconnect_google_font_api( $urls, $relation_type ) { if ( 'preconnect' === $relation_type ) { $urls[] = 'https://fonts.googleapis.com'; $urls[] = [ 'href' => 'https://fonts.gstatic.com', 'crossorigin' => true, ]; } return $urls; } /** * * @param string $context for which display prop is being used. E.G. body, logo, etc. * * @return false|string */ public static function get_google_font_display( $context = 'body' ) { $display = apply_filters( 'themeoo_google_font_display', 'swap', $context ); // https://www.w3.org/TR/css-fonts-4/#font-display-desc $display_values = [ 'auto' => true, 'block' => true, 'swap' => true, 'fallback' => true, 'optional' => true, ]; // https://stackoverflow.com/questions/13483219/what-is-faster-in-array-or-isset if ( $display && isset( $display_values[ $display ] ) ) { return $display; } return false; } public static function get_google_font_variants_string( $font_family ) { $google_fonts = themeoo_get_google_fonts(); if ( ! isset( $google_fonts[ $font_family ] ) ) { return false; } $variants_list = $google_fonts[ $font_family ]['variants']; $variants = maybe_serialize( $variants_list ); $has_ital = false !== strpos( $variants, 'regular' ) && false !== strpos( $variants, 'italic' ); $variants = $has_ital ? 'ital,wght@' : 'wght@'; $exclude = apply_filters( 'themeoo_google_font_exclude_variants', null, $font_family, $variants_list ); // @FIXME not all variant has italic, don't just blindly treat all as regular and italic. // just filter out italic and regular with array-filter->fn->strpos[italic] and array-intersec to separate // variants and then add them to the url. $variants_data = array_map( function ( $variant ) use ( $has_ital, $exclude ) { if ( 'regular' === $variant || 'italic' === $variant ) { $variant = 400; } $variant = intval( $variant ); if ( is_array( $exclude ) && in_array( $variant, $exclude ) ) { return null; } return ( $has_ital ? '0,' : '' ) . $variant; }, $variants_list ); $variants_data = array_filter( $variants_data ); $variants_data = array_unique( $variants_data ); $variants_data = implode( ';', $variants_data ); $variants .= $variants_data; if ( $has_ital ) { $variants .= ';'; $variants .= str_replace( '0,', '1,', $variants_data ); } return apply_filters( 'themeoo_google_font_variants_string', $variants, $font_family, $variants_list ); } public static function generate_google_font_url() { $google_fonts = []; $defaults = array_keys( themeoo_get_standard_fonts() ); $fonts_settings = [ themeoo_get_mod( 'typography_global_font_family' ), themeoo_get_mod( 'typography_heading_h1_font_family' ), themeoo_get_mod( 'typography_heading_h2_font_family' ), themeoo_get_mod( 'typography_heading_h3_font_family' ), themeoo_get_mod( 'typography_heading_h4_font_family' ), themeoo_get_mod( 'typography_heading_h5_font_family' ), themeoo_get_mod( 'typography_heading_h6_font_family' ), themeoo_get_mod( 'typography_menu_font_family' ), themeoo_get_mod( 'typography_menu_sub_font_family' ), themeoo_get_mod( 'typography_menu_mobile_font_family' ), themeoo_get_mod( 'typography_sidebar_title_font_family' ), themeoo_get_mod( 'typography_sidebar_body_font_family' ), themeoo_get_mod( 'typography_footer_title_font_family' ), themeoo_get_mod( 'typography_footer_body_font_family' ), themeoo_get_mod( 'typography_footer_text_font_family' ), /** * @see Themeoo_Customize::enqueue_fonts() */ themeoo_get_mod( 'typography_site_title_font_family' ), themeoo_get_mod( 'typography_site_tagline_font_family' ), ]; foreach ( $fonts_settings as $font_family ) { if ( ! empty( $font_family ) && ! in_array( $font_family, $defaults ) ) { $variants = self::get_google_font_variants_string( $font_family ); if ( ! isset( $google_fonts[ $font_family ] ) ) { $google_fonts[ $font_family ] = $font_family . ':' . $variants; } } } if ( ! empty( $google_fonts ) ) { $google_fonts = 'https://fonts.googleapis.com/css2?family=' . implode( '&family=', $google_fonts ); $display = self::get_google_font_display(); if ( $display ) { $google_fonts .= '&display='; } return apply_filters( 'themeoo_google_font_url', str_replace( [ ' ' ], '+', $google_fonts ) ); } return false; } /** * Enqueue fonts. * * @return void */ public static function enqueue_fonts() { if ( false === apply_filters( 'themeoo_enqueue_google_fonts', true ) ) { return; } add_filter( 'wp_resource_hints', [ __CLASS__, 'preconnect_google_font_api' ], 10, 2 ); // @TODO if not title_tagline_hide_title load specific google font for title & tagline with text param ... // @see https://developers.google.com/fonts/docs/css2#optimizing_your_font_requests $font_api = self::generate_google_font_url(); if ( $font_api ) { wp_enqueue_style( 'themeoo_google-fonts', $font_api, [], null ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion } } /** * Prints Customizer Generated CSS & CSS Variables. * * @return void */ public static function generate_css() { // phpcs:disable WPThemeReview.CoreFunctionality.FileInclude.FileIncludeFound $mappings = require THEMEOO_THEME_DIR . 'inc/customizer/css-variable-mapping.php'; $bg = [ 'bg_image_global_site' => '.boxed', 'title_bar_title_container' => '#title-container', 'footer_top_bg_img' => '.themeoo-footer-top', ]; // phpcs:enable $css = ''; $vars = ''; // Font Family $fallback_fonts = '-apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"'; // Type scale. $type_scale = abs( floatval( get_theme_mod( 'typography_global_scale' ) ) ); $base_font_size = absint( get_theme_mod( 'typography_global_font_size' ) ); if ( ! $base_font_size ) { $base_font_size = 16; } // See scss/partials/_variables.scss, scss/partials/_root.scss, scss/partials/_type-scale.scss $scale_base = ($base_font_size/16); if ( 16 !== $base_font_size ) { $css .= "html{font-size:{$base_font_size}px}"; $vars .= "--fs-h6:{$scale_base}rem;"; } if ( $type_scale && 1.2 !== $type_scale ) { $size = $scale_base; foreach( ['h5', 'h4', 'h3', 'h2', 'h1'] as $tag ) { $size = $size*$type_scale; $vars .= "--fs-{$tag}:" . round( $size, 3 ) . "rem;"; } } foreach ( $bg as $mod => $selector ) { $css .= self::background_image_mod( $mod, $selector ); } /* Container Fluid Max Width */ $mod = themeoo_get_mod( 'layout_global_content_layout_max_width' ); if ( $mod ) { $css .= '.container-fluid{max-width:' . absint( $mod ) . 'px}'; } /* Logo Max Width */ $mod = themeoo_get_mod( 'logo_width' ); if ( $mod ) { $css .= '.custom-logo-link img{max-width:' . absint( $mod ) . 'px;width:' . absint( $mod ) . 'px;}'; } // Print Css. if ( ! empty( $css ) ) { ?> $v ) { $mod = themeoo_get_mod( $k ); if ( is_array( $mod ) ) { // @XXX in some case font_variant return's array (maybe for old installation/not selected). $mod = reset( $mod ); } $mod = esc_attr( (string) $mod ); if ( $mod && 'default' !== strtolower( $mod ) ) { // font family. if ( false !== strpos( $k, 'font_family' ) ) { $vars.= "$v:\"{$mod}\""; if ( ! in_array( $mod, [ 'Monospaced', 'Serif', 'Sans Serif'] ) ) { $vars.= ','.$fallback_fonts; } $vars.= ';'; continue; } // font weight & style. if ( false !== strpos( $k, 'font_variant' ) ) { $style = str_replace( 'fw', 'font-style', $v ); if ( in_array( $mod, [ 'regular', 'italic' ] ) ) { $vars .= "$v:400;"; if ( 'italic' === $mod ) { $vars .= "$style:italic;"; } continue; } if ( strpos( $mod, 'regular' ) || strpos( $mod, 'italic' ) ) { $vars .= $v . ':' . str_replace( [ 'regular', 'italic' ], '', $mod ) . ';'; if ( strpos( $mod, 'italic' ) ) { $vars .= "$style:italic;"; } continue; } } $vars .= "$v:{$mod}"; if ( strpos( $k, '_main_menu_width' ) || strpos( $k, '_button_width' ) ) { $vars .= '%'; } elseif ( ( false !== strpos( $k, 'layout_' ) && ! ( false !== strpos( $k, 'align' ) ) ) || strpos( $k, 'font_size' ) || strpos( $k, 'letter_spacing' ) || strpos( $k, 'word_spacing' ) || false !== strpos( $k, '_logo_width' ) ) { $vars .= 'px'; } $vars .= ';'; } } if ( ! empty( $vars ) ) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { $vars = str_replace( [ ';' ], ';' . PHP_EOL . "\t", $vars ); } ?>