Changeset 2436643
- Timestamp:
- 12/10/2020 02:17:56 PM (5 years ago)
- File:
-
- 1 edited
-
webp-attachments/trunk/ngwebpattachments.php (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
webp-attachments/trunk/ngwebpattachments.php
r2436397 r2436643 3 3 Plugin Name: Webp Attachments 4 4 Description: generate and display webp attachments 5 Version: 1. 05 Version: 1.1 6 6 Requires PHP: 5.6 7 7 Author: Nicolas Guitton … … 156 156 157 157 public function filter_post_thumbnail_html($html, $post_id, $post_thumbnail_id, $size, $attr) { 158 // Add and remove <picture> ..... </picture> just to use filter_the_content 158 // Add and remove <figure> and </figure>, just to use filter_the_content() 159 return str_replace(array('<figure>', '</figure>'), '', $this->filter_the_content('<figure>'.$html.'</figure>')); 159 160 } 160 161 … … 169 170 if (preg_match_all('#<img[^>]*src[^>]*>#Usmi', $figureTag, $imgTags)) { 170 171 foreach ($imgTags[0] as $imgTagKey => $imgTag) { 171 // Add <picture> ..... </picture> if not present 172 173 // Add <picture> ..... </picture> in content if not present 172 174 if(stripos($figureTag, '<picture>') === false) 175 $content = str_replace($imgTag, '<picture>'.$imgTag.'</picture>', $content); 173 176 174 177 //extract img tag srcset attr 175 178 if (preg_match_all( '#srcset=(?:"|\')(?!data)(.*)(?:"|\')#Usmi', $imgTag, $srcsetAttrs, PREG_SET_ORDER)) { 179 $srcsetAttr = (isset($srcsetAttrs[0][1]))?$srcsetAttrs[0][1]:array(); 176 180 $allSrcsetSrcs = explode(',', $srcsetAttr); 181 177 182 foreach ($allSrcsetSrcs as $allSrcsetSrcKey => $allSrcsetSrc) { 178 183 $tmpAllSrcsetSrc = explode(' ', trim($allSrcsetSrc)); 184 185 $srcAttr = trim($tmpAllSrcsetSrc[0]); 179 186 if($allSrcsetSrcKey == 0) 180 187 $srcMedia = 'media="(min-width: '.str_replace('w', '', $tmpAllSrcsetSrc[1]).'px)"'; … … 196 203 else { 197 204 //TODO : fix str_replace and extension file 198 $webpPathFile = str_replace('.'.$pathParts['extension'], '-'.$attachmentFormat.'.webp', $ webpPathFile);205 $webpPathFile = str_replace('.'.$pathParts['extension'], '-'.$attachmentFormat.'.webp', $attachmentPathFile); 199 206 } 200 207 if(is_file($webpPathFile)) { 201 $webpUrlFile = $attachmentOriginalUrl;202 208 //TODO : fix str_replace and extension file 203 $webpUrlFile = str_replace('.'.$pathParts['extension'], '.webp', $ webpUrlFile);209 $webpUrlFile = str_replace('.'.$pathParts['extension'], '.webp', $attachmentOriginalUrl); 204 210 205 211 //add source tag to content 212 $sourceFiles = '<source srcset="'.$webpUrlFile.'" '.$srcMedia.' type="image/webp">'; 206 213 $sourceFiles .= '<source srcset="'.$attachmentOriginalUrl.'" '.$srcMedia.' type="'.$mimeType.'">'; 207 214 $content = str_replace($imgTag, $sourceFiles.$imgTag, $content);
Note: See TracChangeset
for help on using the changeset viewer.