From 9d4123cee1867ee7199b06bdc92d40611f547ecc Mon Sep 17 00:00:00 2001 From: Zach van Rijn Date: Wed, 21 Jul 2021 14:54:07 -0500 Subject: Initial unmodified import from Astra (Version: 3.6.5) @ /wp-content/themes/astra/. --- inc/customizer/class-astra-fonts.php | 200 +++++++++++++++++++++++++++++++++++ 1 file changed, 200 insertions(+) create mode 100644 inc/customizer/class-astra-fonts.php (limited to 'inc/customizer/class-astra-fonts.php') diff --git a/inc/customizer/class-astra-fonts.php b/inc/customizer/class-astra-fonts.php new file mode 100644 index 0000000..fc2ae34 --- /dev/null +++ b/inc/customizer/class-astra-fonts.php @@ -0,0 +1,200 @@ + (array) $variants, + ); + } + } + + /** + * Get Fonts + */ + public static function get_fonts() { + + do_action( 'astra_get_fonts' ); + return apply_filters( 'astra_add_fonts', self::$fonts ); + } + + /** + * Renders the tag for all fonts in the $fonts array. + * + * @since 1.0.16 Added the filter 'astra_render_fonts' to support custom fonts. + * @since 1.0.0 + * @return void + */ + public static function render_fonts() { + + $font_list = apply_filters( 'astra_render_fonts', self::get_fonts() ); + + $google_fonts = array(); + $font_subset = array(); + + $system_fonts = Astra_Font_Families::get_system_fonts(); + + foreach ( $font_list as $name => $font ) { + if ( ! empty( $name ) && ! isset( $system_fonts[ $name ] ) ) { + + // Add font variants. + $google_fonts[ $name ] = $font['variants']; + + // Add Subset. + $subset = apply_filters( 'astra_font_subset', '', $name ); + if ( ! empty( $subset ) ) { + $font_subset[] = $subset; + } + } + } + + $google_font_url = self::google_fonts_url( $google_fonts, $font_subset ); + + // Do not load Google font remote or local font asset if not any font selected. + if ( '' === $google_font_url ) { + return; + } + + /** + * Support self hosted Google Fonts. + * + * @since 3.6.0 + */ + if ( astra_get_option( 'load-google-fonts-locally' ) && ! is_customize_preview() && ! is_admin() ) { + if ( astra_get_option( 'preload-local-fonts' ) ) { + ast_load_preload_local_fonts( $google_font_url ); + } + wp_enqueue_style( 'astra-google-fonts', ast_get_webfont_url( $google_font_url ), array(), ASTRA_THEME_VERSION, 'all' ); + } else { + wp_enqueue_style( 'astra-google-fonts', $google_font_url, array(), ASTRA_THEME_VERSION, 'all' ); + } + } + + /** + * Google Font URL + * Combine multiple google font in one URL + * + * @link https://shellcreeper.com/?p=1476 + * @param array $fonts Google Fonts array. + * @param array $subsets Font's Subsets array. + * + * @return string + */ + public static function google_fonts_url( $fonts, $subsets = array() ) { + + /* URL */ + $base_url = 'https://fonts.googleapis.com/css'; + $font_args = array(); + $family = array(); + + // This is deprecated filter hook. + $fonts = apply_filters( 'astra_google_fonts', $fonts ); + + $fonts = apply_filters( 'astra_google_fonts_selected', $fonts ); + + /* Format Each Font Family in Array */ + foreach ( $fonts as $font_name => $font_weight ) { + $font_name = str_replace( ' ', '+', $font_name ); + if ( ! empty( $font_weight ) ) { + if ( is_array( $font_weight ) ) { + $font_weight = implode( ',', $font_weight ); + } + $font_family = explode( ',', $font_name ); + $font_family = str_replace( "'", '', astra_get_prop( $font_family, 0 ) ); + $family[] = trim( $font_family . ':' . rawurlencode( trim( $font_weight ) ) ); + } else { + $family[] = trim( $font_name ); + } + } + + /* Only return URL if font family defined. */ + if ( ! empty( $family ) ) { + + /* Make Font Family a String */ + $family = implode( '|', $family ); + + /* Add font family in args */ + $font_args['family'] = $family; + + /* Add font subsets in args */ + if ( ! empty( $subsets ) ) { + + /* format subsets to string */ + if ( is_array( $subsets ) ) { + $subsets = implode( ',', $subsets ); + } + + $font_args['subset'] = rawurlencode( trim( $subsets ) ); + } + + $font_args['display'] = astra_get_fonts_display_property(); + + return add_query_arg( $font_args, $base_url ); + } + + return ''; + } +} -- cgit v1.2.3-70-g09d2