loading = $loading; } function run_optimization($photonClass, $buffer) { /* if ($this->loading == 'default') { */ /* return $buffer; */ /* } */ $this->js_mode = $photonClass->js_mode; // Combine patterns to extract script src attributes and inline script contents $pattern = '/]*?(?:src=["\'](.*?)["\']|>(.*?)<\/script>)/is'; preg_match_all($pattern, $buffer, $matches, PREG_SET_ORDER); // Initialize arrays for script sources and inline scripts $scripts = []; // Process the matches foreach ($matches as $match) { if (!empty($match[1])) { // Matched script src attribute if (!str_contains($match[0], 'data-berqwp')) { $src = $match[1]; $scripts[] = $src; } } elseif (!empty($match[2])) { // Matched inline script content if (!str_contains($match[0], 'data-berqwp')) { $script = $match[2]; $scripts[] = $script; } } unset($match); } // Define the regular expression pattern for inline and external script tags $pattern = '/(]*>(.*?)<\/script>)/is'; $deffer_js = ''; // Replace with cdn script urls when they're ready $script_tags_to_replace = []; // Remove JavaScript using preg_replace_callback $buffer = preg_replace_callback($pattern, function ($matches) use (&$photonClass, &$script_tags_to_replace) { $tag = $matches[0]; if (strpos($tag, 'document.write(') !== false) { return $tag; // Keep the script tag with id="berqWP" } // Create a Simple HTML DOM object $html = str_get_html($tag); // Find all script tags foreach ($html->find('script') as $scriptTag) { if (!empty($scriptTag->type) && $scriptTag->type !== 'text/javascript' && $scriptTag->type !== 'application/javascript' && $scriptTag->type !== 'module') { return $tag; } } $tag = $html->save(); // Clear Simple HTML DOM object $html->clear(); unset($html); if ($photonClass->use_cdn) { // Create a Simple HTML DOM object $html = str_get_html($tag); // Find all script tags foreach ($html->find('script') as $scriptTag) { $src = $scriptTag->src; $kw_found = false; foreach ($photonClass->external_js_excluded_keywords as $keyword) { if (stripos($src, $keyword) !== false) { $kw_found = true; } } // Check if the script source contains any excluded keywords if (!$kw_found) { // Use wp_remote_get to fetch the script content $response = wp_remote_get($src); if (!is_wp_error($response)) { $scriptContent = wp_remote_retrieve_body($response); // Send the file URL to CDN as GET parameters /* $cdnUrl = 'https://cdn.berqwp.com/'; */ global $berqCDN; $berqCDN->add_file_in_queue($src); // $cdnUrl = 'https://boost.berqwp.com/photon/cdn/'; // $cdnUrl .= '?url=' . urlencode($src); // $cdnUrl .= '&domain=' . $photonClass->domain; // $photonClass->add_into_cdn_queue($cdnUrl, $src); } } } $tag = $html->save(); // Clear Simple HTML DOM object $html->clear(); unset($html); } // Return the script tag after optimizing with CDN if (strpos($tag, 'data-berqwp') !== false) { return $tag; // Keep the script tag with id="berqWP" } if ($this->loading == 'default') { return $tag; } // If tag has a match for exclude url list if (!empty($photonClass->js_css_exclude_urls)) { foreach ($photonClass->js_css_exclude_urls as $js_exclude_keyword) { if (!empty($js_exclude_keyword)) { if (strpos($tag, $js_exclude_keyword) !== false) { return $tag; } } } } if ($photonClass->js_mode == 1) { $html = str_get_html($tag); // Find all script tags foreach ($html->find('script') as $scriptTag) { if (empty($scriptTag->type) || $scriptTag->type == 'text/javascript') { $scriptTag->setAttribute('data-type', 'text/javascript'); $scriptTag->type = 'text/bwp-script'; } else { $scriptTag->setAttribute('data-type', esc_attr($scriptTag->type)); $scriptTag->type = 'text/bwp-script'; } } // Clear Simple HTML DOM object $tag = $html->save(); $html->clear(); unset($html); return $tag; } if ($photonClass->js_mode == 0) { if ($photonClass->cache_js && !$photonClass->use_cdn) { $tag = $photonClass->optimize_external_js($tag); } $tag_src = $photonClass->get_src_from_script($tag); if (!empty($tag_src) && $photonClass->use_cdn) { $script_tags_to_replace[] = $tag; } $tag = base64_encode($tag); return ''; // Remove other script tags } elseif ($photonClass->js_mode == 2) { if ($photonClass->cache_js && !$photonClass->use_cdn) { return $photonClass->optimize_external_js($tag); } else { return $tag; } } }, $buffer); // if ($photonClass->use_cdn) { // $cdn_file_responses = $photonClass->parallelCurlRequests($photonClass->cdn_upload_queue, [], 'GET'); // for ($i = 0; $i < count($cdn_file_responses); $i++) { // $original_file = $photonClass->original_files_for_cdn[$i]; // $cdnData = json_decode($cdn_file_responses[$i]); // if ($cdnData && isset($cdnData->status) && $cdnData->status === 'success' && isset($cdnData->filePath)) { // // Use the CDN file path in your WordPress code // $cdnFilePath = $cdnData->filePath; // /* $cdn_file = 'https://cdn.berqwp.com' . $cdnFilePath; */ // $cdn_file = $cdnFilePath; // if (($photonClass->js_mode == 1 || $photonClass->js_mode == 0) && strpos($cdn_file, '.js') !== false) { // foreach ($script_tags_to_replace as $script_tag) { // if (strpos($script_tag, $original_file) !== false) { // $origional_tag_base64 = esc_attr(base64_encode($script_tag)); // $script_tag = str_replace($original_file, $cdn_file, $script_tag); // $tag = esc_attr(base64_encode($script_tag)); // $buffer = str_replace($origional_tag_base64, $tag, $buffer); // } // } // // $original_file = base64_encode($original_file); // // $cdn_file = base64_encode($cdn_file); // } // $buffer = str_replace($original_file, $cdn_file, $buffer); // } // } // } if ($this->loading !== 'default') { add_filter('berqwp_buffer_before_closing_body', [$this, 'script']); } unset($photonClass); return $buffer; } function script($script_html) { $script_html .= " "; $script_html .= " "; return $script_html; } }