active = true; } /** * Add resource hints for preconnect and so on. * * @param string $html Full DOM HTML. * @return void */ public function add_hints($html) { if (!$this->active || !Plugin::options()->optimize_gfonts) { return $html; } // preconnect with dns-prefetch fallback for Firefox. Due to safari bug, can't be in same rel. $hint = ''; $hint .= ''; // Only needed if not inlined. if (!Plugin::options()->optimize_css || !Plugin::options()->optimize_gfonts_inline) { $hint .= ''; $hint .= ''; } // Add in to head. $html = str_replace('', '' . $hint, $html); // No longer needed as preconnect's better and we always want to use https. $html = str_replace("", '', $html); return $html; } /** * Late processing and injecting data in HTML DOM. * * @param string $html * @return string */ public function render_in_dom($html) { if ($this->renders) { $html = str_replace('', '' . implode('', $this->renders), $html); } $html = $this->add_hints($html); return $html; } public function optimize(Stylesheet $sheet) { if (!$sheet->is_google_fonts()) { return; } // Set normal render if optimizations are disabled. if (!Plugin::options()->optimize_gfonts) { $sheet->render_type = 'normal'; return; } $sheet->delay_type = 'preload'; if (strpos($sheet->url, 'display=') === false) { $sheet->url .= '&display=swap'; } } public function do_render(Stylesheet $sheet) { return $sheet->render(); // $orig_media = $sheet->media; // // If no display= or if display=auto. // if (!preg_match('/display=(?!auto)/', $sheet->url)) { // $sheet->media = 'x'; // } // // Add the JS to render with display: swap on mobile. // $extra = ""; // $this->renders[] = $sheet->render() . $extra; // // Return empty space to strip out the tag. // return ' '; } }