Plugin Directory

Changeset 3483686


Ignore:
Timestamp:
03/16/2026 10:17:26 AM (12 days ago)
Author:
kubiq
Message:

2.0

  • now images are optimized in the background instead of during upload which was slowing down the workflow
  • fix a few small things based on the current Plugin Check results
Location:
images-to-avif/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • images-to-avif/trunk/images-to-avif.php

    r3439319 r3483686  
    44    Plugin URI: https://www.paypal.me/jakubnovaksl
    55    Description: Convert JPG, PNG, GIF and WEBP images to AVIF and speed up your web
    6     Version: 1.1
     6    Version: 2.0
    77    Author: KubiQ
    88    Author URI: https://kubiq.sk
     
    3333        add_action( 'fly_image_created', [ $this, 'fly_images_to_avif' ], 10, 2 );
    3434        add_action( 'bis_image_created', [ $this, 'bis_images_to_avif' ], 10, 2 );
     35        add_action( 'images_to_avif_cron_convert_attachment', [ $this, 'cron_convert_attachment' ] );
     36    }
     37
     38    function cron_convert_attachment( $args ){
     39        if( isset( $args['data']['file'], $args['data']['sizes'] ) ){
     40            $upload = wp_upload_dir();
     41            $path = $upload['basedir'] . '/' . dirname( $args['data']['file'] ) . '/';
     42            $sizes = [];
     43            $sizes['source'] = $upload['basedir'] . '/' . $args['data']['file'];
     44            foreach( $args['data']['sizes'] as $key => $size ){
     45                $url = $path . $size['file'];
     46                if( in_array( $url, $sizes ) ) continue;
     47                $sizes[ $key ] = $url;
     48            }
     49
     50            $sizes = apply_filters( 'images_to_avif_sizes', $sizes, $args['attachment_id'] );
     51
     52            foreach( $sizes as $size ){
     53                if( ! file_exists( $size . '.avif' ) ){
     54                    $this->convert_image( $size );
     55                }
     56            }
     57        }
    3558    }
    3659
     
    106129
    107130    function filter_plugin_actions( $links, $file ){
    108         array_unshift( $links, '<a href="upload.php?page=' . basename( __FILE__ ) . '">' . __( 'Settings', 'images-to-avif' ) . '</a>' );
     131        array_unshift( $links, '<a href="upload.php?page=' . basename( __FILE__ ) . '">' . esc_html__( 'Settings', 'images-to-avif' ) . '</a>' );
    109132        return $links;
    110133    }
     
    202225
    203226    function images_to_avif_subdirectories(){
    204         if( defined('DOING_AJAX') && DOING_AJAX ){
    205             if( current_user_can('administrator') ){
    206                 if( check_ajax_referer('images_to_avif_convert') ){
    207                     $ABSPATH = str_replace( '\\', '/', $this->get_abspath() );
    208 
    209                     if( $_REQUEST['folder'] == '#' ){
    210                         $dir = $ABSPATH;
    211                     }else{
    212                         $dir = $ABSPATH . $_REQUEST['folder'] . '/';
    213                     }
    214 
    215                     $response = [];
    216                     $directories = scandir( $dir );
    217                     foreach( $directories as $sub ){
    218                         if( $sub != '.' && $sub != '..' && is_dir( $dir . $sub ) ){
    219                             $id = explode( $ABSPATH, $dir . $sub );
    220                             if( isset( $id[1] ) ){
    221                                 $response[] = [
    222                                     'id' => esc_attr( $id[1] ),
    223                                     'parent' => esc_attr( sanitize_text_field( $_REQUEST['folder'] ) ),
    224                                     'text' => esc_html( $sub ),
    225                                     'children' => true
    226                                 ];
    227                             }
    228                         }
    229                     }
    230                     wp_send_json( $response );
    231                 }
    232             }
     227        if( defined('DOING_AJAX') && DOING_AJAX && current_user_can('administrator') && check_ajax_referer('images_to_avif_convert') ){
     228            $ABSPATH = str_replace( '\\', '/', $this->get_abspath() );
     229
     230            if( $_REQUEST['folder'] == '#' ){
     231                $dir = $ABSPATH;
     232            }else{
     233                $dir = $ABSPATH . $_REQUEST['folder'] . '/';
     234            }
     235
     236            $response = [];
     237            $directories = scandir( $dir );
     238            foreach( $directories as $sub ){
     239                if( $sub != '.' && $sub != '..' && is_dir( $dir . $sub ) ){
     240                    $id = explode( $ABSPATH, $dir . $sub );
     241                    if( isset( $id[1] ) ){
     242                        $response[] = [
     243                            'id' => esc_attr( $id[1] ),
     244                            'parent' => esc_attr( sanitize_text_field( $_REQUEST['folder'] ) ),
     245                            'text' => esc_html( $sub ),
     246                            'children' => true
     247                        ];
     248                    }
     249                }
     250            }
     251            wp_send_json( $response );
    233252        }
    234253        exit();
     
    274293
    275294    function images_to_avif_convert_old_images(){
    276         if( defined('DOING_AJAX') && DOING_AJAX ){
    277             if( current_user_can('administrator') ){
    278                 if( check_ajax_referer('images_to_avif_convert') ){
    279                     $only_missing = intval( $_POST['only_missing'] );
    280                     if( $only_missing !== 0 ){
    281                         $only_missing = 1;
    282                     }
    283                     $ABSPATH = str_replace( '\\', '/', $this->get_abspath() );
    284                     $folder = str_replace( ':\\\\', ':/', $_POST['folder'] );
    285                     $folder = str_replace( '\\\\', '/', $folder );
    286                     $folder = preg_replace( '#^' . $ABSPATH . '#', '', $folder );
    287                     $folder = realpath( $ABSPATH . $folder );
    288                     if( is_dir( $folder ) ){
    289                         $secure_path = realpath( $ABSPATH );
    290                         $secure_path_len = strlen( $secure_path );
    291                         if( substr( $folder, 0, $secure_path_len ) === $secure_path ){
    292                             $files = scandir( $folder );
    293                             $converted = 0;
    294 
    295                             $skip_until = false;
    296                             $avif_processed_file = $folder . '/.avif-processed.json';
    297                             if( file_exists( $avif_processed_file ) ){
    298                                 $skip_until = trim( file_get_contents( $avif_processed_file ) );
    299                             }
    300 
    301                             foreach( $files as $file ){
    302                                 if( ! $only_missing || ! file_exists( $folder . '/' . $file . '.avif' ) ){
    303                                     if( substr( $file, -5 ) != '.avif' ){
    304                                         if( substr( $file, -5 ) != '.webp' || ! file_exists( substr( $folder . '/' . $file, 0, -5 ) ) ){
    305                                             if( $skip_until ){
    306                                                 if( $skip_until == $folder . '/' . $file ){
    307                                                     $skip_until = false;
    308                                                 }
    309                                             }else{
    310                                                 $converted += $this->convert_image( $folder . '/' . $file ) ? 1 : 0;
    311                                                 file_put_contents( $avif_processed_file, $folder . '/' . $file, LOCK_EX );
    312                                             }
     295        if( defined('DOING_AJAX') && DOING_AJAX && current_user_can('administrator') && check_ajax_referer('images_to_avif_convert') ){
     296            $only_missing = intval( $_POST['only_missing'] );
     297            if( $only_missing !== 0 ){
     298                $only_missing = 1;
     299            }
     300            $ABSPATH = str_replace( '\\', '/', $this->get_abspath() );
     301            $folder = str_replace( ':\\\\', ':/', $_POST['folder'] );
     302            $folder = str_replace( '\\\\', '/', $folder );
     303            $folder = preg_replace( '#^' . $ABSPATH . '#', '', $folder );
     304            $folder = realpath( $ABSPATH . $folder );
     305            if( is_dir( $folder ) ){
     306                $secure_path = realpath( $ABSPATH );
     307                $secure_path_len = strlen( $secure_path );
     308                if( substr( $folder, 0, $secure_path_len ) === $secure_path ){
     309                    $files = scandir( $folder );
     310                    $converted = 0;
     311
     312                    $skip_until = false;
     313                    $avif_processed_file = $folder . '/.avif-processed.json';
     314                    if( file_exists( $avif_processed_file ) ){
     315                        $skip_until = trim( file_get_contents( $avif_processed_file ) );
     316                    }
     317
     318                    foreach( $files as $file ){
     319                        if( ! $only_missing || ! file_exists( $folder . '/' . $file . '.avif' ) ){
     320                            if( substr( $file, -5 ) != '.avif' ){
     321                                if( substr( $file, -5 ) != '.webp' || ! file_exists( substr( $folder . '/' . $file, 0, -5 ) ) ){
     322                                    if( $skip_until ){
     323                                        if( $skip_until == $folder . '/' . $file ){
     324                                            $skip_until = false;
    313325                                        }
     326                                    }else{
     327                                        $converted += $this->convert_image( $folder . '/' . $file ) ? 1 : 0;
     328                                        file_put_contents( $avif_processed_file, $folder . '/' . $file, LOCK_EX );
    314329                                    }
    315330                                }
    316331                            }
    317 
    318                             wp_delete_file( $avif_processed_file );
    319 
    320                             /* translators: amount of converted files */
    321                             printf( esc_html__( '%d converted', 'images-to-avif' ), intval( $converted ) );
    322332                        }
    323333                    }
     334
     335                    wp_delete_file( $avif_processed_file );
     336
     337                    /* translators: amount of converted files */
     338                    printf( esc_html__( '%d converted', 'images-to-avif' ), intval( $converted ) );
    324339                }
    325340            }
     
    345360    }
    346361
    347     function wp_update_attachment_metadata( $data, $attachmentId ){
     362    function wp_update_attachment_metadata( $data, $attachment_id ){
    348363        if( $this->settings['upload_convert'] == 1 ){
    349             if( $data && isset( $data['file'] ) && isset( $data['sizes'] ) ){
    350                 $upload = wp_upload_dir();
    351                 $path = $upload['basedir'] . '/' . dirname( $data['file'] ) . '/';
    352                 $sizes = [];
    353                 $sizes['source'] = $upload['basedir'] . '/' . $data['file'];
    354                 foreach( $data['sizes'] as $key => $size ){
    355                     $url = $path . $size['file'];
    356                     if( in_array( $url, $sizes ) ) continue;
    357                     $sizes[ $key ] = $url;
    358                 }
    359 
    360                 $sizes = apply_filters( 'images_to_avif_sizes', $sizes, $attachmentId );
    361 
    362                 foreach( $sizes as $size ){
    363                     if( ! file_exists( $size . '.avif' ) ){
    364                         $this->convert_image( $size );
    365                     }
    366                 }
     364            if( isset( $data['file'], $data['sizes'] ) ){
     365                wp_schedule_single_event( time() + 60, 'images_to_avif_cron_convert_attachment', [ [ 'data' => $data, 'attachment_id' => $attachment_id ] ] );
    367366            }
    368367        }
  • images-to-avif/trunk/readme.txt

    r3439319 r3483686  
    66Requires PHP: 7.0
    77Tested up to: 6.9
    8 Stable tag: 1.1
     8Stable tag: 2.0
    99License: GPLv2 or later
    1010License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    146146}`
    147147
     148or without `map` directive:
     149
     150`if ( $http_accept ~ "image/avif" ) {
     151    set $avif .avif ;
     152}
     153
     154location ~* (.*)\.(?:png|jpe?g|gif|webp)$ {
     155    if ( -f $document_root$1.avif ) {
     156            set $avif serve$avif ;
     157    }
     158    if ( $avif = "serve.avif" ) {
     159        return 301 '$1.avif';
     160    }
     161}`
     162
    148163then you need to add this to your server block, usually site.conf or /nginx/sites-enabled/default ( inside of the server{} section ):
    149164
     
    168183== Changelog ==
    169184
     185= 2.0 =
     186* now images are optimized in the background instead of during upload which was slowing down the workflow
     187* fix a few small things based on the current Plugin Check results
     188
    170189= 1.1 =
    171190* optimize old images convert process
  • images-to-avif/trunk/tabs/tab-convert.php

    r3438659 r3483686  
    1 <?php defined('ABSPATH') || exit ?>
     1<?php
    22
    3 <?php if( isset( $this->settings['delete_originals'] ) && $this->settings['delete_originals'] === 1 ): ?>
     3defined('ABSPATH') || exit;
     4
     5if( isset( $this->settings['delete_originals'] ) && $this->settings['delete_originals'] === 1 ): ?>
    46    <div class="below-h2 error"><p><?php esc_html_e( 'This operation will PERMANENTLY DELETE original images, because you set this in general settings. It is a good idea to create some backup.', 'images-to-avif' ) ?></p></div>
    57<?php else: ?>
  • images-to-avif/trunk/tabs/tab-general.php

    r3439319 r3483686  
    6464}
    6565
    66 if( isset( $_POST['plugin_sent'] ) ) echo '<div class="updated"><p>' . esc_html__( 'Settings saved.', 'images-to-avif' ) . '</p></div>'; ?>
     66if( isset( $_POST['plugin_sent'] ) && check_admin_referer('images_to_avif_general') ) echo '<div class="updated"><p>' . esc_html__( 'Settings saved.', 'images-to-avif' ) . '</p></div>'; ?>
    6767
    6868<form method="post" action="">
Note: See TracChangeset for help on using the changeset viewer.