ID) { $attr["loading"] = "eager"; $attr["fetchpriority"] = "high"; } return $attr; } public static function content(string $html): string { if (is_admin() || !Core::settings()["lazy"]) { return $html; } $tags = new \WP_HTML_Tag_Processor($html); $n = 0; $hero = Core::settings()["hero"]; while ($tags->next_tag("IMG")) { ++$n; $class = (string) $tags->get_attribute("class"); if ( ($hero > 0 && preg_match("/\bwp-image-" . $hero . "\b/", $class)) || $tags->get_attribute("fetchpriority") === "high" ) { $tags->set_attribute("loading", "eager"); continue; } if ($n > 2 && $tags->get_attribute("loading") === null) { $tags->set_attribute("loading", "lazy"); $tags->set_attribute("decoding", "async"); } } return $tags->get_updated_html(); } public static function protected_handle(string $h): bool { return (bool) preg_match( "/^(?:jquery|wp-|wc-|woocommerce|ssfspeed|elementor|contact-form|wpcf7|gform|fluentform)/", $h, ); } public static function eligible_handle(string $h): bool { $scripts = wp_scripts(); if (self::protected_handle($h) || empty($scripts->registered[$h])) { return false; } $r = $scripts->registered[$h]; if ( !empty($r->extra["before"]) || !empty($r->extra["after"]) || !empty($r->extra["data"]) || !empty($r->textdomain) || !empty($r->deps) ) { return false; } foreach ($scripts->registered as $other) { if (in_array($h, $other->deps ?? [], true)) { return false; } } return true; } public static function defer(): void { foreach (Core::settings()["defer"] as $h) { if (self::eligible_handle($h)) { wp_script_add_data($h, "strategy", "defer"); } } } public static function video(array $atts = []): string { $a = shortcode_atts(["id" => "", "title" => "Play video"], $atts); if (!preg_match('/^[A-Za-z0-9_-]{11}$/', $a["id"])) { return ""; } wp_enqueue_script( "ssfspeed-media", plugins_url("assets/media.js", SSFSPEED_FILE), [], SSFSPEED_VERSION, true, ); wp_enqueue_style( "ssfspeed-media", plugins_url("assets/media.css", SSFSPEED_FILE), [], SSFSPEED_VERSION, ); // Poster is intentionally local CSS: no YouTube request until the visitor clicks. return '
'; } }