mbined_fonts() { $api_v1_fonts = []; $api_v2_fonts = []; foreach ( $this->fonts as $font ) { if ( $font->get_api_version() === Google_Font::API_V2 ) { $api_v2_fonts = array_merge( $api_v2_fonts, $font->get_families() ); } else { $api_v1_fonts = array_merge( $api_v1_fonts, $font->get_families() ); } } $combined_urls = []; if ( ! empty( $api_v1_fonts ) ) { $combined_urls[] = add_query_arg( [ 'family' => implode( '|', $api_v1_fonts ), 'display' => 'swap', ], 'https://fonts.googleapis.com/css' ); } if ( ! empty( $api_v2_fonts ) ) { $combined_urls[] = 'https://fonts.googleapis.com/css2?family=' . implode( '&family=', $api_v2_fonts ) . '&display=swap'; } return $combined_urls; } /** * Convert Google Fonts URLs to local URLs * * @param string $src Style source URL * @param string $handle Style handle * @return string */ public function load_google_fonts_locally( $src, $handle ) { if ( ! Google_Font::is_google_font_url( $src ) ) { return $src; } if ( ! $this->can_load_locally() ) { return $src; } return wptt_get_webfont_url( $src ); } /** * Process fonts for local loading when not using optimization * * This method only runs when optimization is disabled. * When optimization is enabled, output_optimized_fonts() handles everything. * * @return void */ public function process_local_fonts() { if ( $this->optimize ) { return; } if ( ! $this->can_load_locally() ) { return; } global $wp_styles; if ( ! is_object( $wp_styles ) ) { return; } foreach ( $wp_styles->registered as $handle => $style ) { if ( ! Google_Font::is_google_font_url( $style->src ) ) { continue; } // Handle protocol-relative URLs. if ( strpos( $style->src, '//' ) === 0 ) { $style->src = 'https:' . $style->src; } $wp_styles->registered[ $handle ]->src = wptt_get_webfont_url( $style->src ); } } /** * Check if WPTT WebFont Loader is available * * @return bool */ private function can_load_locally() { return function_exists( 'wptt_get_webfont_url' ); } /** * Setup filters for WebFontLoader. * * @return void */ private function setup_wptt_filters() { // Set custom font storage path add_filter( 'wptt_get_local_fonts_base_path', function ( $path ) { return Helpers::get_plugin_content_dir(); } ); // Set custom font storage URL add_filter( 'wptt_get_local_fonts_base_url', function ( $url ) { return Helpers::get_plugin_content_dir_url(); } ); // Set custom subfolder name add_filter( 'wptt_get_local_fonts_subfolder_name', function ( $subfolder_name ) { return 'fonts'; } ); } /** * Load WPTT_WebFont_Loader. * * @return void */ private function load_webfont_loader() { if ( class_exists( 'WPTT_WebFont_Loader' ) ) { return; } require_once SWCFPC_PLUGIN_PATH . 'vendor/wptt/webfont-loader/wptt-webfont-loader.php'; } }