Changeset 3483686
- Timestamp:
- 03/16/2026 10:17:26 AM (12 days ago)
- Location:
- images-to-avif/trunk
- Files:
-
- 4 edited
-
images-to-avif.php (modified) (6 diffs)
-
readme.txt (modified) (3 diffs)
-
tabs/tab-convert.php (modified) (1 diff)
-
tabs/tab-general.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
images-to-avif/trunk/images-to-avif.php
r3439319 r3483686 4 4 Plugin URI: https://www.paypal.me/jakubnovaksl 5 5 Description: Convert JPG, PNG, GIF and WEBP images to AVIF and speed up your web 6 Version: 1.16 Version: 2.0 7 7 Author: KubiQ 8 8 Author URI: https://kubiq.sk … … 33 33 add_action( 'fly_image_created', [ $this, 'fly_images_to_avif' ], 10, 2 ); 34 34 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 } 35 58 } 36 59 … … 106 129 107 130 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>' ); 109 132 return $links; 110 133 } … … 202 225 203 226 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 ); 233 252 } 234 253 exit(); … … 274 293 275 294 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; 313 325 } 326 }else{ 327 $converted += $this->convert_image( $folder . '/' . $file ) ? 1 : 0; 328 file_put_contents( $avif_processed_file, $folder . '/' . $file, LOCK_EX ); 314 329 } 315 330 } 316 331 } 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 ) );322 332 } 323 333 } 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 ) ); 324 339 } 325 340 } … … 345 360 } 346 361 347 function wp_update_attachment_metadata( $data, $attachment Id ){362 function wp_update_attachment_metadata( $data, $attachment_id ){ 348 363 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 ] ] ); 367 366 } 368 367 } -
images-to-avif/trunk/readme.txt
r3439319 r3483686 6 6 Requires PHP: 7.0 7 7 Tested up to: 6.9 8 Stable tag: 1.18 Stable tag: 2.0 9 9 License: GPLv2 or later 10 10 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 146 146 }` 147 147 148 or without `map` directive: 149 150 `if ( $http_accept ~ "image/avif" ) { 151 set $avif .avif ; 152 } 153 154 location ~* (.*)\.(?: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 148 163 then you need to add this to your server block, usually site.conf or /nginx/sites-enabled/default ( inside of the server{} section ): 149 164 … … 168 183 == Changelog == 169 184 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 170 189 = 1.1 = 171 190 * optimize old images convert process -
images-to-avif/trunk/tabs/tab-convert.php
r3438659 r3483686 1 <?php defined('ABSPATH') || exit ?>1 <?php 2 2 3 <?php if( isset( $this->settings['delete_originals'] ) && $this->settings['delete_originals'] === 1 ): ?> 3 defined('ABSPATH') || exit; 4 5 if( isset( $this->settings['delete_originals'] ) && $this->settings['delete_originals'] === 1 ): ?> 4 6 <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> 5 7 <?php else: ?> -
images-to-avif/trunk/tabs/tab-general.php
r3439319 r3483686 64 64 } 65 65 66 if( isset( $_POST['plugin_sent'] ) ) echo '<div class="updated"><p>' . esc_html__( 'Settings saved.', 'images-to-avif' ) . '</p></div>'; ?>66 if( 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>'; ?> 67 67 68 68 <form method="post" action="">
Note: See TracChangeset
for help on using the changeset viewer.