Plugin Directory

Changeset 2876008


Ignore:
Timestamp:
03/07/2023 03:46:29 PM (3 years ago)
Author:
fastware
Message:

v1.0.6

Location:
fastware-webpavif/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • fastware-webpavif/trunk/fastware-webpavif.php

    r2874818 r2876008  
    33 * Plugin Name: WebP/AVIF image optimizer
    44 * Description: Fast & simple plugin to automatically serve WebP & AVIF images to browsers which support those formats (with fallback to original format).
    5  * Version: 1.0.5
     5 * Version: 1.0.6
    66 * License: GPLv3
    77 * Author: Fastware
     
    113113
    114114                    // redirect to current url after scheduling convert
    115                     wp_redirect($redirectUrl);
     115                    wp_redirect($redirectUrl.'?skip');
    116116                    exit(0);
    117117                }
  • fastware-webpavif/trunk/includes/ImageConverter.php

    r2874818 r2876008  
    1515
    1616            // skip webp
    17             if ($this->supportsWebp())
    18                 touch($this->getWorkingPath($path,'webp') . '.skip');
     17            $webpPath = $this->getWorkingPath($path,'webp');
     18            if ($this->supportsWebp() && !is_file($webpPath))
     19                touch($webpPath . '.skip');
    1920
    2021            // skip avif
    21             if ($this->supportsAvif())
    22                 touch($this->getWorkingPath($path,'avif') . '.skip');
     22            $avifPath = $this->getWorkingPath($path,'avif');
     23            if ($this->supportsAvif() && !is_file($avifPath))
     24                touch($avifPath . '.skip');
    2325
    2426            // add to queue
     
    3133         */
    3234        public function convertQueuedJpgPngToWebpAvif(): void {
     35            // clear stat cache first
     36            clearstatcache();
     37
     38            // get lock (timeout lock after 5 minutes)
     39            $lockPath = $this->getWorkingDir() . '/process.lock';
     40            if (is_file($lockPath) && filemtime($lockPath) > time()-300)
     41                return;
     42            touch($lockPath);
     43
    3344            $loopCount = 1;
    3445            do {
     
    3647                $fileList = glob($this->getWorkingDir() . '/*.queued');
    3748                foreach ($fileList as $path) {
     49                    // reset timeout counter to keep process running - max 300 seconds per image
     50                    set_time_limit(min(300,(int) ini_get('max_execution_time')));
     51
    3852                    $imagePath = trim(file_get_contents($path));
    3953                    if ($this->validatePath($imagePath, ['.jpeg', '.jpg', '.png'])) {
    4054                        $this->convertJpgPngToWebpAvif($imagePath);
    4155                    }
    42                     unlink($path);
     56                    @unlink($path);
     57
     58                    // keep lock alive
     59                    touch($lockPath);
    4360                }
    4461            } while ($loopCount++ < 10 && count($fileList) > 0);
     62
     63            @unlink($lockPath);
    4564        }
    4665
     
    108127                return;
    109128
     129            // get convert path
     130            $convertPath = $this->getWorkingPath($path, $format);
     131
     132            // add skip file (serve original file while converting)
     133            $skipPath = $convertPath . '.skip';
     134            touch($skipPath);
     135
     136            // skip conversion when converted file already exists, and its size > 0
     137            if (is_file($convertPath) && filesize($convertPath) > 0) {
     138                @unlink($skipPath);
     139                return;
     140            }
     141
     142            // keep script running until conversion is done
     143            ignore_user_abort( true );
     144
    110145            // @temporary: clean up previously cached files which were generated in plugin version < 1.0.5 (to remove in future)
    111146            foreach (['','.skip','.tmp'] as $suffix) {
    112147                $removePath = $path . '.' . $format . $suffix;
    113148                if (is_file($removePath)) {
    114                     unlink($removePath);
    115                 }
    116             }
    117 
    118             // get convert path
    119             $convertPath = $this->getWorkingPath($path, $format);
    120 
    121             // skip conversion when converted file already exists, and its size > 0
    122             if (is_file($convertPath) && filesize($convertPath) > 0)
    123                 return;
    124 
    125             // keep script running until conversion is done
    126             ignore_user_abort( true );
    127 
    128             // add skip file (serve original file while converting)
    129             $skipPath = $convertPath . '.skip';
    130             touch($skipPath);
     149                    @unlink($removePath);
     150                }
     151            }
    131152
    132153            // initialize
     
    206227            }
    207228
     229            // clear stat cache before checking filesize
     230            clearstatcache();
    208231            if ($convertResult
    209232                && ($filesize = filesize($tmpPath)) > 0
     
    212235
    213236                // unlink skip path after successful conversion
    214                 unlink($skipPath);
     237                @unlink($skipPath);
    215238            } else {
    216239                // remove temp file when conversion failed
    217240                if (is_file($tmpPath))
    218                     unlink($tmpPath);
     241                    @unlink($tmpPath);
    219242            }
    220243        }
     
    241264                    $removePath = $this->getWorkingPath($path,  $extension) . $suffix;
    242265                    if (is_file($removePath)) {
    243                         unlink($removePath);
     266                        @unlink($removePath);
    244267                    }
    245268                }
     
    268291                        $this->rmdir($path);
    269292                    if (is_file($path))
    270                         unlink($path);
     293                        @unlink($path);
    271294                }
    272295            }
     
    327350            // write htaccess
    328351            if (strlen($htaccessContent) === 0)
    329                 unlink($htaccessPath);
     352                @unlink($htaccessPath);
    330353            else
    331354                file_put_contents($htaccessPath, $htaccessContent);
  • fastware-webpavif/trunk/readme.txt

    r2875519 r2876008  
    22Contributors: fastware
    33Tags: webp avif image optimize pagespeed
    4 Stable tag: 1.0.5
     4Stable tag: 1.0.6
    55Requires at least: 5.9
    66Tested up to: 6.1
     
    4747
    4848== Changelog ==
     49- 1.0.6 [07-03-2023] Fixed redirect bug on servers without avif support
    4950- 1.0.5 [04-03-2023] Moved working directory, to improve cleanup after uninstall
    5051- 1.0.4 [27-02-2023] Added support for PHP 7.4, fixed issue with scheduled converts
  • fastware-webpavif/trunk/templates/.htaccess

    r2874818 r2876008  
    99  RewriteRule .* - [T=image/%1,L]
    1010
    11   ## IF AVIF SUPPORTED ###
     11  ### IF AVIF SUPPORTED ###
    1212  # serve avif instead of jpg|jpeg|png?
    1313  RewriteCond %{HTTP_ACCEPT} image/avif
Note: See TracChangeset for help on using the changeset viewer.