PluginProbe
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript / 1.9.7
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript v1.9.7
4.1.16 4.1.15 4.1.14 4.1.13 4.1.12 4.1.11 4.1.10 4.0.30 4.0.29 4.0.28 4.0.27 4.0.26 4.0.24 4.0.25 4.0.23 4.0.22 4.0.21 4.0.19 4.0.18 4.0.17 4.0.16 1.9.3 1.9.4 1.9.5 1.9.6 All 170 releases
searchpro / inc / photon / image-optimize / bg_image_opt.php

bg_image_opt.php in BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript 1.9.7, at inc/photon/image-optimize/bg_image_opt.php

125 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if (!defined('ABSPATH')) exit;
3
4 $buffer = preg_replace_callback(
5 '/background(?:-image)?\s*:\s*url\(["\']?(\/[^"\'\)]+\.(?:jpg|jpeg|png)|[^"\'\)]+\.(?:jpg|jpeg|png))["\']?\)/i',
6 function ($matches) {
7 $webpUrl = $matches[1]; // Adjusted index to $matches[1]
8
9 if (strpos($webpUrl, 'data:') === 0) {
10 return $matches[0];
11 }
12
13 // Fix relative bg image url
14 if (!str_contains($webpUrl, home_url()) && strpos($webpUrl, '/') === 0) {
15 $webpUrl = home_url() . $webpUrl;
16 }
17
18
19 if (str_contains($webpUrl, home_url())) {
20
21 global $berqCDN;
22 $berqCDN->add_file_in_queue($webpUrl);
23
24 // $this->add_into_download_queue($webpUrl);
25
26 // $webp_path = $this->convert_url_to_webp($webpUrl);
27 // $webpUrl = preg_replace('/\.(jpg|jpeg|png)$/', '.webp', $webpUrl); // Adjusted index to $matches[1]
28
29 // Extract the file extension
30 $file_extension = pathinfo($webpUrl, PATHINFO_EXTENSION);
31
32 if ($file_extension == 'png' || $file_extension == 'jpg' || $file_extension == 'jpeg') {
33 $webpUrl = update_image_url_extension($webpUrl, 'webp');
34 }
35
36 $file_path = str_replace(home_url(), ABSPATH, $webpUrl);
37 // $store_webp = $this->store_image($webpUrl, $webp_path);
38 $webpUrl = $webpUrl . '?t=' . $this->time;
39 // var_dump($store_webp, $webp_path);
40
41 if (file_exists($webp_path) && filesize($webp_path) > 0 && $store_webp) {
42 unlink($webp_path);
43 return 'background-image: url(' . $webpUrl . ')'; // Adjusted return string
44 }
45 }
46
47
48 return $matches[0]; // Return the original string if the conditions aren't met
49 },
50 $buffer
51 );
52
53 // important webp bg images
54 $buffer = str_replace(".webp?t=$this->time);", ".webp?t=$this->time)!important;", $buffer);
55
56
57 $buffer = preg_replace_callback(
58 '/url(\(((?:[^()]+|(?1))+)\))/i',
59 function ($matches) {
60
61 if (!str_contains($matches[0], '.png') && !str_contains($matches[0], '.jpg') && !str_contains($matches[0], '.jpeg')) {
62 return $matches[0];
63 }
64
65 $webpUrl = str_replace("'", '', $matches[2]); // Adjusted index to $matches[1]
66 // var_dump($matches, strpos($webpUrl, '//'));
67
68 if (strpos($webpUrl, 'data:') === 0) {
69 return $matches[0];
70 }
71
72 if (str_contains($webpUrl, '//') && !str_contains($webpUrl, '://')) {
73 $webpUrl = str_replace('//', 'https://', $webpUrl);
74 }
75
76 // Fix relative image url
77 if (!str_contains($webpUrl, home_url()) && strpos($webpUrl, '/') === 0 && strpos($webpUrl, '//') === false) {
78 $webpUrl = home_url() . $webpUrl;
79 }
80
81 $is_writeable = !str_contains($webpUrl, '/wp-content/themes/') && !str_contains($webpUrl, '/wp-content/plugins/');
82
83 if (!$is_writeable) {
84 return $matches[0];
85 }
86
87 if (str_contains($webpUrl, home_url())) {
88
89 global $berqCDN;
90 $berqCDN->add_file_in_queue($webpUrl);
91 // $this->add_into_download_queue($webpUrl);
92
93 // $webp_path = $this->convert_url_to_webp($webpUrl);
94 // $webpUrl = preg_replace('/\.(jpg|jpeg|png)$/', '.webp', $webpUrl); // Adjusted index to $matches[1]
95
96 // Extract the file extension
97 $file_extension = pathinfo($webpUrl, PATHINFO_EXTENSION);
98
99 if ($file_extension == 'png' || $file_extension == 'jpg' || $file_extension == 'jpeg') {
100 $webpUrl = update_image_url_extension($webpUrl, 'webp');
101 }
102
103 $file_path = str_replace(home_url(), ABSPATH, $webpUrl);
104 // $store_webp = $this->store_image($webpUrl, $webp_path);
105 $webpUrl = $webpUrl . '?t=' . $this->time;
106
107 // var_dump($store_webp, $webp_path);
108
109 if (file_exists($webp_path) && filesize($webp_path) > 0 && $store_webp) {
110 unlink($webp_path);
111 // Adjusted return string to include url() function without background or background-image property
112 return 'url(' . $webpUrl . ')';
113 }
114 }
115
116
117
118 return $matches[0]; // Return the original string if the conditions aren't met
119 },
120 $buffer
121 );
122
123 // echo $buffer;
124 // exit;
125