Plugin Directory

Changeset 2436643


Ignore:
Timestamp:
12/10/2020 02:17:56 PM (5 years ago)
Author:
nicolasguitton
Message:

Fix bug thumbnail not displayed

File:
1 edited

Legend:

Unmodified
Added
Removed
  • webp-attachments/trunk/ngwebpattachments.php

    r2436397 r2436643  
    33Plugin Name: Webp Attachments
    44Description: generate and display webp attachments
    5 Version: 1.0
     5Version: 1.1
    66Requires PHP: 5.6
    77Author: Nicolas Guitton
     
    156156
    157157    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>'));
    159160    }
    160161
     
    169170                        if (preg_match_all('#<img[^>]*src[^>]*>#Usmi', $figureTag, $imgTags)) {
    170171                            foreach ($imgTags[0] as $imgTagKey => $imgTag) {
    171                                 // Add <picture> ..... </picture> if not present
     172
     173                                // Add <picture> ..... </picture> in content if not present
    172174                                if(stripos($figureTag, '<picture>') === false)
     175                                    $content = str_replace($imgTag, '<picture>'.$imgTag.'</picture>', $content);
    173176
    174177                                //extract img tag srcset attr
    175178                                if (preg_match_all( '#srcset=(?:"|\')(?!data)(.*)(?:"|\')#Usmi', $imgTag, $srcsetAttrs, PREG_SET_ORDER)) {
     179                                    $srcsetAttr = (isset($srcsetAttrs[0][1]))?$srcsetAttrs[0][1]:array();
    176180                                    $allSrcsetSrcs = explode(',', $srcsetAttr);
     181                                   
    177182                                    foreach ($allSrcsetSrcs as $allSrcsetSrcKey => $allSrcsetSrc) {
    178183                                        $tmpAllSrcsetSrc = explode(' ', trim($allSrcsetSrc));
     184
     185                                        $srcAttr = trim($tmpAllSrcsetSrc[0]);
    179186                                        if($allSrcsetSrcKey == 0)
    180187                                            $srcMedia = 'media="(min-width: '.str_replace('w', '', $tmpAllSrcsetSrc[1]).'px)"';
     
    196203                                        else {
    197204                                            //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);
    199206                                        }
    200207                                        if(is_file($webpPathFile)) {
    201                                             $webpUrlFile = $attachmentOriginalUrl;
    202208                                            //TODO : fix str_replace and extension file
    203                                             $webpUrlFile = str_replace('.'.$pathParts['extension'], '.webp', $webpUrlFile);
     209                                            $webpUrlFile = str_replace('.'.$pathParts['extension'], '.webp', $attachmentOriginalUrl);
    204210
    205211                                            //add source tag to content
     212                                            $sourceFiles = '<source srcset="'.$webpUrlFile.'" '.$srcMedia.' type="image/webp">';
    206213                                            $sourceFiles .= '<source srcset="'.$attachmentOriginalUrl.'" '.$srcMedia.' type="'.$mimeType.'">';
    207214                                            $content = str_replace($imgTag, $sourceFiles.$imgTag, $content);
Note: See TracChangeset for help on using the changeset viewer.