1818* with this program; if not, write to the Free Software Foundation, Inc., 51
1919* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2020*/
21- class Tiny_Picture
22- {
23- public static function init ()
24- {
25- if (is_admin () || is_customize_preview ()) {
21+ class Tiny_Picture {
22+
23+ public static function init () {
24+ if ( is_admin () || is_customize_preview () ) {
2625 return ;
2726 }
2827
29- if (defined ('DOING_AJAX ' ) && DOING_AJAX ) {
28+ if ( defined ( 'DOING_AJAX ' ) && DOING_AJAX ) {
3029 return ;
3130 }
3231
33- if (defined ('DOING_CRON ' ) && DOING_CRON ) {
32+ if ( defined ( 'DOING_CRON ' ) && DOING_CRON ) {
3433 return ;
3534 }
3635
3736 add_action ('template_redirect ' , function () {
38- ob_start ([ self ::class, 'replace_img_with_picture_tag ' ] );
37+ ob_start ( [ self ::class, 'replace_img_with_picture_tag ' ] );
3938 }, 1000 );
4039 }
4140
42- public static function replace_img_with_picture_tag ($ content )
43- {
44- $ images = Tiny_Picture::filter_images ($ content );
41+ public static function replace_img_with_picture_tag ( $ content ) {
42+ $ images = Tiny_Picture::filter_images ( $ content );
4543
46- foreach ($ images as $ image ) {
47- $ content = Tiny_Picture::replace_image ($ content , $ image );
44+ foreach ( $ images as $ image ) {
45+ $ content = Tiny_Picture::replace_image ( $ content , $ image );
4846 }
4947 return $ content ;
5048 }
5149
52- private static function replace_image ($ content , $ image )
53- {
54- $ content = str_replace ($ image ->img_element , $ image ->get_picture_element (), $ content );
50+ private static function replace_image ( $ content , $ image ) {
51+ $ content = str_replace ( $ image ->img_element , $ image ->get_picture_element (), $ content );
5552 return $ content ;
5653 }
5754
@@ -60,35 +57,34 @@ private static function replace_image($content, $image)
6057 *
6158 * @return Tiny_Image[]
6259 */
63- private static function filter_images ($ content )
64- {
65- if (preg_match ('/(?=<body).*<\/body>/is ' , $ content , $ body )) {
60+ private static function filter_images ( $ content ) {
61+ if ( preg_match ( '/(?=<body).*<\/body>/is ' , $ content , $ body ) ) {
6662 $ content = $ body [0 ];
6763 }
6864
69- $ content = preg_replace ('/<!--(.*)-->/Uis ' , '' , $ content );
65+ $ content = preg_replace ( '/<!--(.*)-->/Uis ' , '' , $ content );
7066
71- $ content = preg_replace ('#<noscript(.*?)>(.*?)</noscript>#is ' , '' , $ content );
67+ $ content = preg_replace ( '#<noscript(.*?)>(.*?)</noscript>#is ' , '' , $ content );
7268
73- if (! preg_match_all ('/<img\s.*>/isU ' , $ content , $ matches) ) {
69+ if ( ! preg_match_all ( '/<img\s.*>/isU ' , $ content , $ matches ) ) {
7470 return array ();
7571 }
7672
77- $ images = array_map (function ($ img ) {
78- return new Tiny_Picture_Element ($ img );
73+ $ images = array_map (function ( $ img ) {
74+ return new Tiny_Picture_Element ( $ img );
7975 }, $ matches [0 ]);
80- $ images = array_filter ($ images );
76+ $ images = array_filter ( $ images );
8177
82- if (! $ images || ! is_array ($ images) ) {
78+ if ( ! $ images || ! is_array ( $ images ) ) {
8379 return array ();
8480 }
8581
8682 return $ images ;
8783 }
8884}
8985
90- class Tiny_Picture_Element
91- {
86+ class Tiny_Picture_Element {
87+
9288
9389
9490 /**
@@ -104,15 +100,14 @@ class Tiny_Picture_Element
104100 private $ img_element_node ;
105101
106102 private $ base_dir ;
107-
103+
108104 private $ site_url ;
109105
110- public function __construct ($ img_element )
111- {
106+ public function __construct ( $ img_element ) {
112107 $ this ->img_element = $ img_element ;
113108 $ dom = new \DOMDocument ();
114- $ dom ->loadHTML ($ img_element );
115- $ this ->img_element_node = $ dom ->getElementsByTagName ('img ' )->item (0 );
109+ $ dom ->loadHTML ( $ img_element );
110+ $ this ->img_element_node = $ dom ->getElementsByTagName ( 'img ' )->item ( 0 );
116111
117112 $ this ->base_dir = get_home_path ();
118113 $ this ->site_url = get_site_url ();
@@ -123,48 +118,46 @@ public function __construct($img_element)
123118 *
124119 * @return array{path: string, size: string}[] The image sources
125120 */
126- private function get_image_srcsets ()
127- {
121+ private function get_image_srcsets () {
128122 $ result = array ();
129- $ srcset = $ this ->img_element_node ->getAttribute ('srcset ' );
123+ $ srcset = $ this ->img_element_node ->getAttribute ( 'srcset ' );
130124
131- if ($ srcset ) {
125+ if ( $ srcset ) {
132126 // Split the srcset by commas to get individual entries
133- $ srcset_entries = explode (', ' , $ srcset );
127+ $ srcset_entries = explode ( ', ' , $ srcset );
134128
135- foreach ($ srcset_entries as $ entry ) {
129+ foreach ( $ srcset_entries as $ entry ) {
136130 // Trim whitespace
137- $ entry = trim ($ entry );
131+ $ entry = trim ( $ entry );
138132
139133 // Split by whitespace to separate path and size descriptor
140- $ parts = preg_split ('/\s+/ ' , $ entry , 2 );
134+ $ parts = preg_split ( '/\s+/ ' , $ entry , 2 );
141135
142- if (count ($ parts ) === 2 ) {
136+ if ( count ( $ parts ) === 2 ) {
143137 // We have both path and size
144138 $ result [] = array (
145139 'path ' => $ parts [0 ],
146140 'size ' => $ parts [1 ],
147141 );
148- } elseif (count ($ parts ) === 1 ) {
142+ } elseif ( count ( $ parts ) === 1 ) {
149143 // We only have a path (unusual in srcset)
150144 $ result [] = array (
151145 'path ' => $ parts [0 ],
152146 'size ' => '' ,
153147 );
154148 }
155149 }
156- } elseif ($ this ->img_element_node ->hasAttribute ('src ' ) ) {
150+ } elseif ( $ this ->img_element_node ->hasAttribute ( 'src ' ) ) {
157151 // No srcset, but we have a src attribute
158152 $ result [] = array (
159- 'path ' => $ this ->img_element_node ->getAttribute ('src ' ),
153+ 'path ' => $ this ->img_element_node ->getAttribute ( 'src ' ),
160154 'size ' => '' ,
161155 );
162156 }
163157 return $ result ;
164158 }
165159
166- private function get_local_path ($ url )
167- {
160+ private function get_local_path ( $ url ) {
168161 if ( strpos ( $ url , 'http ' ) === 0 ) {
169162 if ( strpos ( $ url , $ this ->site_url ) !== 0 ) {
170163 // url is not the current site
@@ -179,44 +172,42 @@ private function get_local_path($url)
179172 return $ url ;
180173 }
181174
182- private function get_formatted_source ($ imgsrcs , $ mimetype )
183- {
175+ private function get_formatted_source ( $ imgsrcs , $ mimetype ) {
184176 $ formatted_src_set = array ();
185- foreach ($ imgsrcs as $ imgsrc ) {
186- $ format_url = Tiny_Helpers::replace_file_extension ($ mimetype , $ imgsrc ['path ' ]);
187- $ local_path = $ this ->get_local_path ($ format_url );
188- if (empty ($ local_path) ) {
177+ foreach ( $ imgsrcs as $ imgsrc ) {
178+ $ format_url = Tiny_Helpers::replace_file_extension ( $ mimetype , $ imgsrc ['path ' ] );
179+ $ local_path = $ this ->get_local_path ( $ format_url );
180+ if ( empty ( $ local_path ) ) {
189181 continue ;
190182 }
191- $ exists_local = file_exists ($ local_path );
192- if ($ exists_local ) {
183+ $ exists_local = file_exists ( $ local_path );
184+ if ( $ exists_local ) {
193185 $ formatted_src_set [] = $ format_url . ' ' . $ imgsrc ['size ' ];
194186 }
195187 }
196188
197- if (empty ($ formatted_src_set) ) {
189+ if ( empty ( $ formatted_src_set ) ) {
198190 // no alternative sources found
199191 return '' ;
200192 }
201193
202- $ source_set = implode (', ' , $ formatted_src_set );
203- $ trimmed_source_set = trim ($ source_set );
194+ $ source_set = implode ( ', ' , $ formatted_src_set );
195+ $ trimmed_source_set = trim ( $ source_set );
204196 return '<source type=" ' . $ mimetype . '" srcset=" ' . $ trimmed_source_set . '"> ' ;
205197 }
206198
207- public function get_picture_element ()
208- {
199+ public function get_picture_element () {
209200 $ srcsets = $ this ->get_image_srcsets ();
210201
211- $ avif = $ this ->get_formatted_source ($ srcsets , 'image/avif ' );
212- $ webp = $ this ->get_formatted_source ($ srcsets , 'image/webp ' );
213- if (empty ($ avif ) && empty ($ webp) ) {
202+ $ avif = $ this ->get_formatted_source ( $ srcsets , 'image/avif ' );
203+ $ webp = $ this ->get_formatted_source ( $ srcsets , 'image/webp ' );
204+ if ( empty ( $ avif ) && empty ( $ webp ) ) {
214205 return $ this ->img_element ;
215206 }
216207
217208 $ picture = '<picture> ' ;
218- $ picture .= $ this ->get_formatted_source ($ srcsets , 'image/avif ' );
219- $ picture .= $ this ->get_formatted_source ($ srcsets , 'image/webp ' );
209+ $ picture .= $ this ->get_formatted_source ( $ srcsets , 'image/avif ' );
210+ $ picture .= $ this ->get_formatted_source ( $ srcsets , 'image/webp ' );
220211 $ picture .= $ this ->img_element ;
221212
222213 $ picture .= '</picture> ' ;
0 commit comments