l, 0, $local + 11 ); } $url = '/' . ltrim( str_replace( $home_url, '', $url ), '/\\' ); } /** * Modify URL to uploaded font * * @since 4.8.8 * @param string $url * @param string $orig_url * @param string $file * @param array $info * @return string */ $url = apply_filters( 'avf_custom_uploaded_fonts_url', $url, $orig_url, $file, $info ); switch( $ext ) { case 'eot': $iefix = $url . "?#iefix"; // IE6-IE8 $urls[] = " url('{$url}') {$this->css_formats[ $ext ]}"; $urls[] = " url('{$iefix}') {$this->css_formats[ $ext ]}"; break; case 'svg': $urls[] = " url('{$url}#{$font['key']}') {$this->css_formats[ $ext ]}"; break; default: $urls[] = " url('{$url}') {$this->css_formats[ $ext ]}"; break; } } if( empty( $urls ) ) { continue; } /** * Allow to change default behaviour of browsers when loading external fonts * https://developers.google.com/web/updates/2016/02/font-display * * @since 4.5.6 * @param string $font_display * @param string $font_name * @return string auto | block | swap | fallback | optional */ $font_display = apply_filters( 'avf_font_display', avia_get_option( 'custom_font_display', '' ), $font['key'] ); $font_display = empty( $font_display ) ? 'auto' : $font_display; $css .= "@font-face { \r\n"; $css .= " font-family: '{$font['key']}';\r\n"; $css .= " src: "; $css .= implode( ",\r\n ", $urls ); $css .= ";\r\n"; $css .= " font-style: {$info['style']};\r\n"; if( false === strpos( $info['weight'], 'variablefont' ) ) { $css .= " font-weight: {$info['weight']};\r\n"; } $css .= " font-display: {$font_display};\r\n"; $css .= "}\r\n"; } return $css; } /** * Output styles for custom uploaded fonts * * @since 4.3 */ public function handler_wp_print_styles() { $uploaded = $this->get_font_list(); $out = '' . "\r\n"; echo $out; } /** * Output styles for custom uploaded fonts to dynamic stylesheet * * @since ??? * @since 5.4 modified to support loading @font-face via CDN * @param string $output * @param avia_style_generator $style_generator * @param string $context 'before' | 'after' * @return string */ public function handler_create_dynamic_stylesheet( $output, avia_style_generator $style_generator, $context = '' ) { if( ! in_array( $context, array( 'before' ) ) ) { return $output; } $uploaded = $this->get_font_list(); if( empty( $uploaded ) ) { return $output; } $output .= "\r\n"; if( 'cdn' == avia_get_option( 'custom_font_source' ) ) { $output .= '/* @font-face for custom uploaded fonts must be handled by custom CSS to load from a custom location (e.g. a CDN) */' . "\r\n"; $output .= '/* ============================================================================================================== */'; } else { foreach( $uploaded as $font_info ) { $output .= $this->get_css_custom_font_face( $font_info ); } } $output .= "\r\n"; return $output; } /** * Splits the selected font string (option value from font select box) * * @since 4.3 * @param string $selection * @return array */ public function split_font_info( $selection ) { /** * Split font sizes for Google fonts */ $info = explode( ':', $selection ); $font = array( 'family' => trim( $info[0] ), 'weight' => isset( $info[1] ) ? trim( $info[1] ) : '' ); return $font; } /** * Returns the type of font selected from a select box * * @since 4.3 * @param array $font_info * @return string 'websave' | 'custom' | 'google' */ public function get_selected_font_type( array $font_info ) { if( strpos( $font_info['family'], '-websave' ) !== false ) { return 'websave'; } if( strpos( $font_info['family'], '-custom' ) !== false ) { return 'custom'; } return 'google'; } /** * Removes any font type mark from first font name, removes ",' and returns the fontname in lowercase * * @since 4.3 * @param array $font_info * @return array */ public function set_font_family( array $font_info ) { $type = $this->get_selected_font_type( $font_info ); switch( $type ) { case 'websave': case 'custom': $replace = '-' . $type; break; default: return $font_info; } /** * In case we have several fonts we only take the first one */ $font_family = explode( ',', $font_info['family'] ); $font_family = strtolower( $font_family[0] ); $font_family = str_replace( array( '"', "'" ), '', $font_family ); $font_family = str_replace( $replace, '', $font_family ); $font_info['family'] = $font_family; return $font_info; } } }