| @@ -238,8 +238,13 @@ | ||
| 238 | 238 | |
| 239 | 239 | return false; |
| 240 | 240 | } |
| 241 | 241 | |
| 242 | + | |
| 243 | +function berqwp_home_url() { | |
| 244 | + return untrailingslashit(apply_filters('berqwp_site_url', home_url())); | |
| 245 | +} | |
| 246 | + | |
| 242 | 247 | function berqwp_get_page_params($page_url, $is_forced = false) |
| 243 | 248 | { |
| 244 | 249 | |
| 245 | 250 | if (empty($page_url)) { |
| @@ -262,8 +267,10 @@ | ||
| 262 | 267 | } elseif ($optimization_mode == 1) { |
| 263 | 268 | $optimization_mode = 'basic'; |
| 264 | 269 | } |
| 265 | 270 | |
| 271 | + $home_url = berqwp_home_url(); | |
| 272 | + | |
| 266 | 273 | // Data to send as POST parameters |
| 267 | 274 | $post_data = array( |
| 268 | 275 | 'site_id' => $berqwp_configs['site_id'], |
| 269 | 276 | 'license_key' => berqwp_get_license_key(), |
| @@ -268,13 +275,14 @@ | ||
| 268 | 275 | 'site_id' => $berqwp_configs['site_id'], |
| 269 | 276 | 'license_key' => berqwp_get_license_key(), |
| 270 | 277 | 'page_url' => $page_url, |
| 271 | 278 | 'page_slug' => $page_slug, |
| 272 | - 'site_url' => home_url(), | |
| 279 | + 'site_url' => $home_url, | |
| 273 | 280 | 'webp_max_width' => (int) get_option('berqwp_webp_max_width'), |
| 274 | 281 | 'webp_quality' => (int) get_option('berqwp_webp_quality'), |
| 275 | 282 | 'fluid_images' => get_option('berqwp_fluid_images'), |
| 276 | 283 | 'enable_critical_css' => get_option('berqwp_enable_critical_css'), |
| 284 | + 'enable_used_css' => get_option('berqwp_enable_used_css'), | |
| 277 | 285 | 'force_include_critical_css'=> get_option('berqwp_force_include_critical_css', []), |
| 278 | 286 | 'exclude_css' => get_option('berqwp_exclude_css', []), |
| 279 | 287 | 'exclude_js' => get_option('berqwp_exclude_js', []), |
| 280 | 288 | 'exclude_third_party_js' => get_option('berqwp_exclude_third_party_js', []), |
| @@ -1301,9 +1309,9 @@ | ||
| 1301 | 1309 | |
| 1302 | 1310 | function bwp_sluguri_into_path($slug_uri) |
| 1303 | 1311 | { |
| 1304 | 1312 | $is_multisite = function_exists('is_multisite') && is_multisite(); |
| 1305 | - $home_path = parse_url(home_url(), PHP_URL_PATH); | |
| 1313 | + $home_path = parse_url(berqwp_home_url(), PHP_URL_PATH); | |
| 1306 | 1314 | |
| 1307 | 1315 | // if (berqwp_is_sub_dir_wp() && !$is_multisite) { |
| 1308 | 1316 | if (!$is_multisite && $home_path !== null) { |
| 1309 | 1317 | // Parse strings to extract paths |
| @@ -1324,9 +1332,9 @@ | ||
| 1324 | 1332 | { |
| 1325 | 1333 | $parsed_url = parse_url($url); |
| 1326 | 1334 | $path = isset($parsed_url['path']) ? $parsed_url['path'] : ''; |
| 1327 | 1335 | $query = isset($parsed_url['query']) ? $parsed_url['query'] : ''; |
| 1328 | - $homeurl = home_url(); | |
| 1336 | + $homeurl = berqwp_home_url(); | |
| 1329 | 1337 | |
| 1330 | 1338 | if (!empty($query)) { |
| 1331 | 1339 | $path = $path . '?' . $query; |
| 1332 | 1340 | } |
| @@ -1450,9 +1458,9 @@ | ||
| 1450 | 1458 | // Set the number of posts per batch to handle |
| 1451 | 1459 | $posts_per_page = 10000; // Adjust this based on server capacity |
| 1452 | 1460 | |
| 1453 | 1461 | // Get current page from query string (for pagination) |
| 1454 | - $paged = isset($_GET['paged']) ? intval($_GET['paged']) : 1; | |
| 1462 | + $paged = isset($_GET['paged']) ? intval($_GET['paged']) : 1; | |
| 1455 | 1463 | |
| 1456 | 1464 | // Build the query arguments with pagination |
| 1457 | 1465 | $args = array( |
| 1458 | 1466 | 'post_type' => $post_types, |
| @@ -2161,8 +2169,40 @@ | ||
| 2161 | 2169 | // Fallback: no <head> tag found, return unchanged |
| 2162 | 2170 | return $buffer; |
| 2163 | 2171 | } |
| 2164 | 2172 | |
| 2173 | +function berqwp_earlyHeadHtml($buffer, $htmlToInsert) | |
| 2174 | +{ | |
| 2175 | + // Locate <head> opening tag | |
| 2176 | + if (!preg_match('/<head(\s[^>]*)?>/i', $buffer, $headMatch, PREG_OFFSET_CAPTURE)) { | |
| 2177 | + return berqwp_prependHtmlToHead($buffer, $htmlToInsert); | |
| 2178 | + } | |
| 2179 | + | |
| 2180 | + $headStart = $headMatch[0][1] + strlen($headMatch[0][0]); | |
| 2181 | + | |
| 2182 | + // Bound the search to content inside <head>...</head> | |
| 2183 | + $headEndPos = stripos($buffer, '</head>', $headStart); | |
| 2184 | + if ($headEndPos === false) { | |
| 2185 | + return berqwp_prependHtmlToHead($buffer, $htmlToInsert); | |
| 2186 | + } | |
| 2187 | + | |
| 2188 | + $headContent = substr($buffer, $headStart, $headEndPos - $headStart); | |
| 2189 | + | |
| 2190 | + // Find first <script, <link, or <style in the head | |
| 2191 | + // if (preg_match('/<(script|link|style)[\s>\/]/i', $headContent, $tagMatch, PREG_OFFSET_CAPTURE)) { | |
| 2192 | + | |
| 2193 | + $regex = "/<(script|link[^>]+rel=['\"]+(modulepreload|stylesheet|preload)['\"]|style)[\s>\/]/i"; | |
| 2194 | + | |
| 2195 | + if (preg_match($regex, $headContent, $tagMatch, PREG_OFFSET_CAPTURE)) { | |
| 2196 | + $insertPos = $headStart + $tagMatch[0][1]; | |
| 2197 | + return substr_replace($buffer, $htmlToInsert . PHP_EOL, $insertPos, 0); | |
| 2198 | + } | |
| 2199 | + | |
| 2200 | + // Fallback: no matching tag in head | |
| 2201 | + return berqwp_prependHtmlToHead($buffer, $htmlToInsert); | |
| 2202 | +} | |
| 2203 | + | |
| 2204 | + | |
| 2165 | 2205 | function berqwp_setup_dropin() |
| 2166 | 2206 | { |
| 2167 | 2207 | if (defined('BERQWP_ADVANCED_CACHE_PATH')) { |
| 2168 | 2208 | $adv_cache_path = BERQWP_ADVANCED_CACHE_PATH; |
| @@ -2334,9 +2374,9 @@ | ||
| 2334 | 2374 | } |
| 2335 | 2375 | |
| 2336 | 2376 | function bwp_remove_recently_optimized_entry($page_url) |
| 2337 | 2377 | { |
| 2338 | - if (empty($page_url)) return; | |
| 2378 | + if (empty($page_url)) return; | |
| 2339 | 2379 | |
| 2340 | 2380 | $log = json_decode(get_option('berqwp_recently_optimized', '[]'), true); |
| 2341 | 2381 | if (!is_array($log)) return; |
| 2342 | 2382 | |
| @@ -2356,5 +2396,5 @@ | ||
| 2356 | 2396 | $blog_id = get_current_blog_id(); |
| 2357 | 2397 | $network_id = function_exists('get_current_network_id') ? get_current_network_id() : 1; |
| 2358 | 2398 | $siteurl = get_option('siteurl'); |
| 2359 | 2399 | return md5("berqwp|$network_id|$blog_id|$siteurl"); |
| 2360 | -} | |
| 2400 | +} | |