Plugin Directory

Changeset 2950564


Ignore:
Timestamp:
08/09/2023 01:30:19 PM (2 years ago)
Author:
andreadegiovine
Message:

2.0.0

Location:
webp-images
Files:
168 added
6 edited

Legend:

Unmodified
Added
Removed
  • webp-images/trunk/assets/admin-ui.css

    r2324216 r2950564  
    210210    }
    211211}
     212
     213.webp-images-loading::after {
     214    content: "";
     215    display: inline-block;
     216    margin-left: 10px;
     217    background-image: url("../../../../wp-admin/images/loading.gif");
     218    height: 16px;
     219    width: 16px;
     220    vertical-align: sub;
     221}
  • webp-images/trunk/assets/admin.js

    r2865263 r2950564  
    1 jQuery(document).ready( function() {
    2     var webp_is_converting = jQuery('span.webp-images-conversion-countdown');
    3     var webp_is_removing = jQuery('span.webp-images-remove-countdown');
    4     function check_webp_images_conversion_countdown(webp_images_action){
    5         jQuery.ajax({
     1jQuery(document).ready( function($) {
     2    var webp_images_conversion_btn = $("button.wepb-images-start-conversion");
     3    var webp_images_delete_btn = $("button.wepb-images-bulk-delete");
     4
     5    var webp_images_infos_wrap = $(".webp-images-bulk-infos");
     6    var totals_wrap = $(webp_images_infos_wrap).find(".total > div:last-child");
     7    var not_webp_wrap = $(webp_images_infos_wrap).find(".nowebp > div:last-child");
     8    var webp_wrap = $(webp_images_infos_wrap).find(".webp > div:last-child");
     9
     10    function webp_images_is_loading(action = 'conversion', loading = true){
     11        var current_label_1 = $(webp_images_conversion_btn).text();
     12        var new_label_1 = $(webp_images_conversion_btn).attr('data-webp-images-label');
     13
     14        var current_label_2 = $(webp_images_delete_btn).text();
     15        var new_label_2 = $(webp_images_delete_btn).attr('data-webp-images-label');
     16
     17        if(action === 'conversion'){
     18            $(webp_images_conversion_btn).attr('data-webp-images-label', current_label_1);
     19            $(webp_images_conversion_btn).html(new_label_1);
     20            $(webp_images_conversion_btn).toggleClass('webp-images-loading', loading);
     21        } else {
     22            $(webp_images_delete_btn).attr('data-webp-images-label', current_label_2);
     23            $(webp_images_delete_btn).html(new_label_2);
     24            $(webp_images_delete_btn).toggleClass('webp-images-loading', loading);
     25        }
     26        $(webp_images_conversion_btn).attr('disabled', loading);
     27        $(webp_images_delete_btn).attr('disabled', loading);
     28    }
     29
     30    function webp_images_error(message = WebpImages.error){
     31        alert(message);
     32    }
     33
     34    function webp_images_ajax(action, success_callback, bis = false){
     35        if(!bis){
     36            webp_images_is_loading(action);
     37            $(window).on("beforeunload", function() {
     38                return WebpImages.beforeunload;
     39            });
     40        }
     41        $.ajax({
    642            type : "post",
    743            dataType : "json",
    844            url : WebpImages.ajaxurl,
    945            data : {
    10                 action: "webp_images_countdown",
    11                 type: webp_images_action,
     46                action: "webp_images_ajax",
     47                type: action,
    1248                token: WebpImages.nonce,
    1349            },
     50            error: function(){
     51                webp_images_is_loading(action, false);
     52                webp_images_error();
     53            },
    1454            success: function(response) {
    15                 if('conversion' == webp_images_action){
    16                     jQuery(webp_is_converting).html(response);
     55                if(response.success !== true){
     56                    webp_images_is_loading(action, false);
     57                    webp_images_error();
     58                    return;
    1759                }
    18                 if('remove' == webp_images_action){
    19                     jQuery(webp_is_removing).html(response);
     60
     61                var webp_count = parseInt($(webp_wrap).text());
     62                var not_webp_count = parseInt($(not_webp_wrap).text());
     63                var new_count = parseInt(response.data);
     64
     65                var result = success_callback(new_count, webp_count, not_webp_count);
     66
     67                if(result === -1){ // Error
     68                    webp_images_is_loading(action, false);
     69                    webp_images_error();
     70                    return;
    2071                }
    21                 if(response == 0){
    22                     location.reload();
     72
     73                if(result === 1){ // One more run
     74                    webp_images_ajax(action, success_callback, true);
     75                    return;
     76                }
     77
     78                if(result === 2){ // End
     79                    webp_images_is_loading(action, false);
     80                    $(webp_images_conversion_btn).attr('disabled', action === 'conversion');
     81                    $(webp_images_delete_btn).attr('disabled', action !== 'conversion');
    2382                }
    2483            }
    2584        });
    2685    }
    27     if( jQuery(webp_is_converting).length ) {
    28         setInterval(function() {
    29             check_webp_images_conversion_countdown('conversion');
    30         }, 3000);
    31     }
    32     if( jQuery(webp_is_removing).length ) {
    33         setInterval(function() {
    34             check_webp_images_conversion_countdown('remove');
    35         }, 3000);
    36     }
     86
     87    $(webp_images_conversion_btn).click(function(e){
     88        e.preventDefault();
     89        webp_images_ajax('conversion', function(new_count, webp_count, not_webp_count){
     90            if(new_count === not_webp_count){
     91                return -1;
     92            }
     93
     94            $(not_webp_wrap).text(new_count);
     95            $(webp_wrap).text(webp_count + (not_webp_count - new_count));
     96
     97            if(new_count > 0){
     98                return 1;
     99            }
     100
     101            return 2;
     102        });
     103    });
     104
     105    $(webp_images_delete_btn).click(function(e){
     106        e.preventDefault();
     107        webp_images_ajax('delete', function(new_count, webp_count, not_webp_count){
     108            if(new_count === webp_count){
     109                return -1;
     110            }
     111
     112            $(webp_wrap).text(new_count);
     113            $(not_webp_wrap).text(not_webp_count + (webp_count - new_count));
     114
     115            if(new_count > 0){
     116                return 1;
     117            }
     118
     119            return 2;
     120        });
     121    });
    37122});
  • webp-images/trunk/parts/options-page-main.php

    r2324216 r2950564  
    33    die( 'Invalid request.' );
    44}
    5 if( has_action('webp_im_settings_page_main') ){ do_action('webp_im_settings_page_main'); } else { ?>
    6 <p><?php printf(__('To <span class="pro-required">set Conversion quality and Auto conversion on upload</span>, please <a href="%s" target="_blank" title="Get PRO version">get the PRO version</a>.', 'webp-images'), 'https://www.andreadegiovine.it/risorse/plugin/webp-images?utm_source=tools_plugin_page&utm_medium=plugin_page&utm_campaign=webp_images' ); ?></p>
     5if(!$this->is_pro_version_active()){ ?>
     6    <p><?php printf(__('To <span class="pro-required">set Conversion quality and Auto conversion on upload</span>, please <a href="%s" target="_blank" title="Get PRO version">get the PRO version</a>.', 'webp-images'), 'https://www.andreadegiovine.it/risorse/plugin/webp-images?utm_source=tools_plugin_page&utm_medium=plugin_page&utm_campaign=webp_images' ); ?></p>
     7<?php } ?>
    78<p><?php _e('This plugin use a special <strong>htaccess rules</strong> to shows the converted WebP versions (if they exist) instead the originals.<br>It works with <strong>all images used in frontend uploaded and managed by the WordPress media library</strong> (it does not work with images uploaded via ftp or alternatives).', 'webp-images');?></p>
    89<p><?php _e('If it generates errors, bugs or malfunctions, use the bulk function (below) to <strong>remove all Webp versions</strong> and just finished deactivate the plugin.<br>Check if the .htaccess file still contains "WebP Images" rules and delete them.', 'webp-images');?></p>
    910<p><?php _e('This plugin can <strong>automatically converts the uploaded images</strong> into WebP format.', 'webp-images');?></p>
    10 <form method="post" action="options.php">
    11     <?php settings_fields( 'webp-images-settings-group' ); ?>
    12     <?php do_settings_sections( 'webp-images-settings-group' ); ?>
    13     <div class="webp-images-bulk-infos">
    14         <div class="webp-images-bulk-info"><div class="pro-required"><?php _e('Conversion quality (0-100)', 'webp-images');?></div><div>
    15             <input type="number" disabled></div></div>
    16         <div class="webp-images-bulk-info last"><div class="pro-required"><?php _e('Auto conversion on upload', 'webp-images');?></div><div><label class="switch">
    17             <input type="checkbox" disabled />
    18             <span class="slider"></span>
    19             </label></div></div>
    20     </div>
    21     <div class="webp-images-bulk-actions">
    22         <?php submit_button(__('Save Settings', 'webp-images'),'secondary button-hero', 'submit', false); ?>
    23     </div>
    24 </form>
     11<?php
     12if(has_action('webp_images_options_page')){
     13    do_action('webp_images_options_page');
     14} else { ?>
     15    <form>
     16        <div class="webp-images-bulk-infos">
     17            <div class="webp-images-bulk-info"><div class="pro-required"><?php _e('Conversion quality (0-100)', 'webp-images');?></div><div>
     18                    <input type="number" disabled></div></div>
     19            <div class="webp-images-bulk-info last"><div class="pro-required"><?php _e('Auto conversion on upload', 'webp-images');?></div><div><label class="switch">
     20                        <input type="checkbox" disabled />
     21                        <span class="slider"></span>
     22                    </label></div></div>
     23        </div>
     24        <div class="webp-images-bulk-actions">
     25            <?php submit_button(__('Save Settings', 'webp-images'),'secondary button-hero', 'submit', false, ['disabled' => true]); ?>
     26        </div>
     27    </form>
     28<?php } ?>
    2529<h2><?php _e('Bulk images management', 'webp-images');?></h2>
    2630<p><?php _e('Use these tools to convert or delete images in WebP format.', 'webp-images');?></p>
     
    2933    <div class="webp-images-bulk-info nowebp"><div><?php _e('Images not converted', 'webp-images');?></div><div><?php echo $this->count_not_webp_media_images();?></div></div>
    3034    <div class="webp-images-bulk-info webp"><div><?php _e('Images converted to WebP', 'webp-images');?></div><div><?php echo $this->count_webp_media_images();?></div></div>
    31     <div class="webp-images-bulk-info time last"><div><?php _e('Estimated time for conversion', 'webp-images');?></div><div><?php printf(__('%s seconds', 'webp-images'), $this->estimated_conversion_time());?></div></div>
    3235</div>
    3336<div class="webp-images-bulk-actions">
    34     <?php if( wp_next_scheduled('init_bulk_webp_conversion') || wp_next_scheduled('init_bulk_webp_remove') || 0 == $this->count_not_webp_media_images() ) { ?>
    35     <button class="button button-primary button-hero" disabled><?php _e('Convert all images to WebP', 'webp-images');?></button>
    36     <?php } else { ?>
    37     <a href="<?php echo add_query_arg(array('action' => 'convert_webp', 'token' => wp_create_nonce('webp-images-convert')), menu_page_url('webp-images-settings', false));?>" class="button button-primary button-hero"><?php _e('Convert all images to WebP', 'webp-images');?></a>
    38     <?php } ?>
    39    
    40     <?php if( wp_next_scheduled('init_bulk_webp_conversion') || wp_next_scheduled('init_bulk_webp_remove') || 0 == $this->count_webp_media_images() ) { ?>
    41     <button class="button button-secondary button-hero" disabled><?php _e('Remove all WebP images', 'webp-images');?></button>
    42     <?php } else { ?>
    43     <a href="<?php echo add_query_arg(array('action' => 'remove_webp', 'token' => wp_create_nonce('webp-images-remove')), menu_page_url('webp-images-settings', false));?>" class="button button-secondary button-hero"><?php _e('Remove all WebP images', 'webp-images');?></a>
    44     <?php } ?>
     37    <button class="button button-primary button-hero wepb-images-start-conversion" data-webp-images-label="<?php _e('Conversion in progress', 'webp-images');?>"<?php echo 0 == $this->count_not_webp_media_images() ? 'disabled="disabled"' : '' ;?>><?php _e('Convert all images to WebP', 'webp-images');?></button>
     38    <button class="button button-secondary button-hero wepb-images-bulk-delete" data-webp-images-label="<?php _e('Deleting in progress', 'webp-images');?>"<?php echo 0 == $this->count_webp_media_images() ? 'disabled="disabled"' : '' ;?>><?php _e('Remove all WebP images', 'webp-images');?></button>
    4539</div>
    46 <?php } ?>
  • webp-images/trunk/parts/sidebar.php

    r2318991 r2950564  
    11<?php
     2
    23if ( ! defined( 'ABSPATH' ) ) {
    34    die( 'Invalid request.' );
    45}
     6
    57if( !$this->is_pro_version_active() ){ ?>
    6 <div class="webp-images-sidebar-content">
    7     <h2><?php _e('WebP Images PRO', 'webp-images');?></h2>
    8     <p><?php _e('The <u>PRO version</u> of this fantastic plugin allows you to set <u>Conversion quality</u> and <u>Auto conversion on upload</u>.', 'webp-images');?></p>
    9     <p><a href="https://www.andreadegiovine.it/risorse/plugin/webp-images?utm_source=tools_plugin_page&utm_medium=plugin_page&utm_campaign=webp_images" class="button button-primary button-hero" target="_blank" title="<?php _e( 'WordPress WebP plugin', 'webp-images' ); ?>"><?php _e( 'Get PRO', 'webp-images' ); ?></a></p>
    10     <p><a href="https://wordpress.org/plugins/webp-images/" class="button button-secondary button-hero" target="_blank"><?php _e( 'Get support', 'webp-images' ); ?></a></p>
    11     <p><a href="https://wordpress.org/support/plugin/webp-images/reviews/#new-post" class="button button-secondary button-hero" target="_blank"><?php _e( 'Write a review', 'webp-images' ); ?></a></p>
    12 </div>
     8    <div class="webp-images-sidebar-content">
     9        <h2><?php _e('WebP Images PRO', 'webp-images');?></h2>
     10        <p><?php _e('The <u>PRO version</u> of this fantastic plugin allows you to set <u>Conversion quality</u> and <u>Auto conversion on upload</u>.', 'webp-images');?></p>
     11        <p><a href="https://totalpress.org/plugins/webp-images?utm_source=wp-dashboard&utm_medium=installed-plugin&utm_campaign=webp-images" class="button button-primary button-hero" target="_blank" title="<?php _e( 'WordPress WebP plugin', 'webp-images' ); ?>"><?php _e( 'Get PRO', 'webp-images' ); ?></a></p>
     12        <p><a href="https://wordpress.org/plugins/webp-images/" class="button button-secondary button-hero" target="_blank"><?php _e( 'Get support', 'webp-images' ); ?></a></p>
     13        <p><a href="https://wordpress.org/support/plugin/webp-images/reviews/#new-post" class="button button-secondary button-hero" target="_blank"><?php _e( 'Write a review', 'webp-images' ); ?></a></p>
     14    </div>
    1315<?php } else { ?>
    14 <div class="webp-images-sidebar-content">
    15     <h2><?php _e('WebP Images PRO', 'webp-images');?></h2>
    16     <p><?php _e('Thanks for using the <u>PRO version</u> of this fantastic plugin.', 'webp-images');?></p>
    17     <p><a href="https://wordpress.org/plugins/webp-images/" class="button button-secondary button-hero" target="_blank"><?php _e( 'Get support', 'webp-images' ); ?></a></p>
    18     <p><a href="https://wordpress.org/support/plugin/webp-images/reviews/#new-post" class="button button-secondary button-hero" target="_blank"><?php _e( 'Write a review', 'webp-images' ); ?></a></p>
    19 </div>
     16    <div class="webp-images-sidebar-content">
     17        <h2><?php _e('WebP Images PRO', 'webp-images');?></h2>
     18        <p><?php _e('Thanks for using the <u>PRO version</u> of this fantastic plugin.', 'webp-images');?></p>
     19        <p><a href="https://wordpress.org/plugins/webp-images/" class="button button-secondary button-hero" target="_blank"><?php _e( 'Get support', 'webp-images' ); ?></a></p>
     20        <p><a href="https://wordpress.org/support/plugin/webp-images/reviews/#new-post" class="button button-secondary button-hero" target="_blank"><?php _e( 'Write a review', 'webp-images' ); ?></a></p>
     21    </div>
    2022<?php }
  • webp-images/trunk/readme.txt

    r2944702 r2950564  
    44Donate link: https://totalpress.org/donate?utm_source=wordpress_org&utm_medium=plugin_page&utm_campaign=webp-images
    55Requires at least: 4.0
    6 Tested up to: 6.2
    7 Stable tag: 1.0.6
     6Tested up to: 6.3
     7Stable tag: 2.0.0
    88Requires PHP: 5.6
    99License: GPLv2 or later
     
    5858
    5959== WebP Images development functions ==
    60 
    61 `
    62 function edit_time_between_conversion_group($time_in_seconds){
    63     $time_in_seconds = 10; // Edit this value to increase/decrease the waiting time
    64     return $time_in_seconds;
    65 }
    66 add_filter('webp_images_bulk_time_between','edit_time_between_conversion_group');
    67 `
    68 
    69 Add this code in the functions.php file to change the waiting time between image group conversions. **Default 5**.
    70 
    7160`
    7261function edit_max_images_per_group($images_per_group){
     
    7766`
    7867
    79 Add this code in the functions.php file to change the max number of images per group conversions. **Default 10**.
     68Add this code in the functions.php file to change the max number of images per group conversions. **Default 5**.
    8069
    8170== WebP Images credits ==
     
    9584
    9685== Changelog ==
     86
     87= 2.0.0 =
     88*2023-08-09*
     89
     90* Remove cron conversion/deletion management;
     91* Improve UI;
     92* Fix to remove converted webp version on original media deletion;
    9793
    9894= 1.0.6 =
  • webp-images/trunk/webp-images.php

    r2944702 r2950564  
    88Text Domain: webp-images
    99Domain Path: /languages/
    10 Version: 1.0.6
     10Version: 2.0.0
    1111*/
    1212
     
    1515}
    1616
    17 define('WEBP_IMAGES_BULK_TIME', 5); // Time between bulk actions
    18 define('WEBP_IMAGES_BULK_ELEMENTS', 10); // Elements for single bulk action
     17define('WEBP_IMAGES_BULK_ELEMENTS', 5); // Elements for single bulk action
    1918define('WEBP_IMAGES_VERSION', get_file_data(__FILE__, ['Version' => 'Version'], false)['Version']);
    2019
     
    2928    class webp_images
    3029    {
    31 
    3230        public function __construct()
    33         {
    34             //
    35         }
    36 
    37         public function init_plugin()
    3831        {
    3932            add_action('init', array($this, 'init_load_textdomain'));
     
    4134            add_action('admin_enqueue_scripts', array($this, 'init_admin_enqueue'));
    4235            add_action('init', array($this, 'dismiss_compatibility_notice'));
    43             add_action('init', array($this, 'start_bulk_conversion'));
    44             add_action('init', array($this, 'start_bulk_remove'));
    4536            add_action('admin_notices', array($this, 'init_admin_notices'));
    4637            add_filter('plugin_action_links', array($this, 'init_plugin_action_links'), 10, 2);
    47             add_action('init', array($this, 'force_stop_process'));
    48             add_action('wp_ajax_webp_images_countdown', array($this, 'webp_images_countdown'));
    49             // CRON
    50             add_filter('cron_schedules', array($this, 'add_cron_schedules'));
    51             add_action('init_bulk_webp_conversion', array($this, 'bulk_webp_conversion'));
    52             add_action('init_bulk_webp_remove', array($this, 'bulk_webp_remove'));
     38            add_action('wp_ajax_webp_images_ajax', array($this, 'ajax_actions'));
     39            add_action('webp_images_start_conversion', array($this, 'start_conversion'));
     40            add_action('delete_attachment', array($this, 'auto_delete'));
    5341
    5442
     
    153141                </div>
    154142            <?php }
    155             if (wp_next_scheduled('init_bulk_webp_conversion')) { ?>
    156                 <div class="notice notice-success is-dismissible">
    157                     <p><?php printf(__('<strong>WebP Images</strong> is converting your images to webp.<br><big><u><span class="webp-images-conversion-countdown">%s</span> images left</u></big> to complete the conversion.', 'webp-images'), $this->count_not_webp_media_images()); ?></p>
    158                     <p><a href="<?php echo admin_url('upload.php?page=webp-images-settings'); ?>" class="button button-secondary"><?php _e('WebP settings', 'webp-images'); ?></a> <a href="<?php echo add_query_arg(array('action' => 'stop_webp', 'token' => wp_create_nonce('webp-images-stop')), admin_url('upload.php?page=webp-images-settings')); ?>" class="button button-primary"><?php _e('Stop process', 'webp-images'); ?></a></p>
    159                 </div>
    160             <?php } elseif (wp_next_scheduled('init_bulk_webp_remove')) { ?>
    161                 <div class="notice notice-warning is-dismissible">
    162                     <p><?php printf(__('<strong>WebP Images</strong> is removing all webp versions of your images.<br><big><u><span class="webp-images-remove-countdown">%s</span> images left</u></big> to complete the removal.', 'webp-images'), $this->count_webp_media_images()); ?></p>
    163                     <p><a href="<?php echo admin_url('upload.php?page=webp-images-settings'); ?>" class="button button-secondary"><?php _e('WebP settings', 'webp-images'); ?></a> <a href="<?php echo add_query_arg(array('action' => 'stop_webp', 'token' => wp_create_nonce('webp-images-stop')), admin_url('upload.php?page=webp-images-settings')); ?>" class="button button-primary"><?php _e('Stop process', 'webp-images'); ?></a></p>
    164                 </div>
    165             <?php } else {
    166                 return;
    167             }
    168143        }
    169144
     
    217192            wp_localize_script('admin-webp-images-js', 'WebpImages', array(
    218193                'ajaxurl' => admin_url('admin-ajax.php'),
    219                 'nonce' => wp_create_nonce('webp-images-countdown'),
     194                'nonce' => wp_create_nonce('webp-images-ajax'),
     195                'error' => __('WebP Images: an error has occurred, check the logs for more details.', 'webp-images'),
     196                'beforeunload' => __('WebP Images: before leaving the page, wait for the end of the processes in progress.', 'webp-images'),
    220197            ));
    221198            wp_enqueue_script('jquery');
    222199            wp_enqueue_script('admin-webp-images-js');
    223         }
    224 
    225         public function estimated_conversion_time()
    226         {
    227             $conversion_time = 0;
    228 
    229             $count_images_not_converted = $this->count_not_webp_media_images();
    230             $max_elements_for_bulk = (int) apply_filters('webp_images_bulk_max_elements', WEBP_IMAGES_BULK_ELEMENTS);
    231             $time_between_bulk_actions = (int) apply_filters('webp_images_bulk_time_between', WEBP_IMAGES_BULK_TIME);
    232 
    233             $conversion_time = ($count_images_not_converted / $max_elements_for_bulk) * $time_between_bulk_actions;
    234 
    235             $time = (ceil($conversion_time) % $time_between_bulk_actions === 0) ? ceil($conversion_time) : round(($conversion_time + $time_between_bulk_actions / 2) / $time_between_bulk_actions) * $time_between_bulk_actions;
    236             return $time;
    237200        }
    238201
     
    383346        }
    384347
     348        public function convert_to_webp($files = []){
     349            $result = true;
     350            foreach ($files as $name => $path) {
     351                $base_path = dirname($path);
     352                $conversion = $this->convert_img_to_webp($path, $base_path . '/' . $name . '.webp');
     353                if (!$conversion) {
     354                    $result = false;
     355                    break;
     356                }
     357            }
     358            return $result;
     359        }
     360
    385361        public function remove_webp_image($webp_path = false)
    386362        {
     
    395371        }
    396372
     373        public function delete_webp($files = []){
     374            $result = true;
     375            foreach ($files as $name => $path) {
     376                $wepb_path = dirname($path) . '/' . $name . '.webp';
     377                $delete = $this->remove_webp_image($wepb_path);
     378                if (!$delete) {
     379                    $result = false;
     380                    break;
     381                }
     382            }
     383            return $result;
     384        }
     385
    397386        public function dismiss_compatibility_notice()
    398387        {
     
    402391            update_option('webp_images_compatibility_notice', 1);
    403392            wp_redirect(admin_url('upload.php?page=webp-images-settings&action=status'));
    404             exit;
    405         }
    406 
    407         public function start_bulk_conversion()
    408         {
    409             if (!$this->is_apache() || !$this->htaccess_is_ok() || !is_admin() || !isset($_GET['action']) || $_GET['action'] !== 'convert_webp' || !isset($_GET['token']) || !wp_verify_nonce($_GET['token'], "webp-images-convert")) {
    410                 return;
    411             }
    412             $this->stop_cron_schedule();
    413             $this->start_cron_schedule('init_bulk_webp_conversion');
    414             wp_redirect(admin_url('upload.php?page=webp-images-settings'));
    415             exit;
    416         }
    417 
    418         public function bulk_webp_conversion()
    419         {
    420             $result = 'completed';
    421 
    422             $max_elements_for_bulk = apply_filters('webp_images_bulk_max_elements', WEBP_IMAGES_BULK_ELEMENTS);
    423             $all_images = $this->get_not_webp_media_images($max_elements_for_bulk);
    424 
    425             if (count($all_images) < 1) {
    426                 $this->stop_cron_schedule();
    427                 return;
    428             }
    429 
    430             $original_time_limit = ini_get('max_execution_time');
    431             ini_set('max_execution_time', 0);
    432             foreach ($all_images as $id => $files) {
    433                 foreach ($files as $name => $path) {
    434                     $base_path = dirname($path);
    435                     $make_conversion = $this->convert_img_to_webp($path, $base_path . '/' . $name . '.webp');
    436                     if (!$make_conversion) {
    437                         update_post_meta($id, 'webp_src', 'error');
    438                         $result = 'errors';
    439                     }
    440                 }
    441                 if ($result == 'completed') {
    442                     update_post_meta($id, 'webp_src', 'done');
    443                 }
    444             }
    445             ini_set('max_execution_time', $original_time_limit);
    446         }
    447 
    448         public function start_bulk_remove()
    449         {
    450             if (!is_admin() || !isset($_GET['action']) || $_GET['action'] !== 'remove_webp' || !isset($_GET['token']) || !wp_verify_nonce($_GET['token'], "webp-images-remove")) {
    451                 return;
    452             }
    453             $this->stop_cron_schedule();
    454             $this->start_cron_schedule('init_bulk_webp_remove');
    455             wp_redirect(admin_url('upload.php?page=webp-images-settings'));
    456             exit;
    457         }
    458 
    459         public function bulk_webp_remove()
    460         {
    461             $result = 'completed';
    462 
    463             $max_elements_for_bulk = apply_filters('webp_images_bulk_max_elements', WEBP_IMAGES_BULK_ELEMENTS);
    464             $all_images = $this->get_webp_media_images($max_elements_for_bulk);
    465 
    466             if (count($all_images) < 1) {
    467                 $this->stop_cron_schedule();
    468                 return;
    469             }
    470 
    471             $original_time_limit = ini_get('max_execution_time');
    472             ini_set('max_execution_time', 0);
    473             foreach ($all_images as $id => $files) {
    474                 foreach ($files as $name => $path) {
    475                     $base_path = dirname($path);
    476                     $make_remove = $this->remove_webp_image($base_path . '/' . $name . '.webp');
    477                     if (!$make_remove) {
    478                         $result = 'errors';
    479                     }
    480                 }
    481                 if ($result == 'completed') {
    482                     delete_post_meta($id, 'webp_src');
    483                 }
    484             }
    485             ini_set('max_execution_time', $original_time_limit);
    486         }
    487 
    488         public function force_stop_process()
    489         {
    490             if (!is_admin() || !isset($_GET['action']) || $_GET['action'] !== 'stop_webp' || !isset($_GET['token']) || !wp_verify_nonce($_GET['token'], "webp-images-stop")) {
    491                 return;
    492             }
    493             $this->stop_cron_schedule();
    494             wp_redirect(admin_url('upload.php?page=webp-images-settings'));
    495393            exit;
    496394        }
     
    581479        }
    582480
    583         public function start_cron_schedule($schedule)
    584         {
    585             wp_schedule_event(time(), 'webp_images_bulk', $schedule);
    586             return;
    587         }
    588 
    589         public function stop_cron_schedule()
    590         {
    591             if (wp_next_scheduled('init_bulk_webp_conversion')) {
    592                 wp_clear_scheduled_hook('init_bulk_webp_conversion');
    593             }
    594             if (wp_next_scheduled('init_bulk_webp_remove')) {
    595                 wp_clear_scheduled_hook('init_bulk_webp_remove');
    596             }
    597             return;
    598         }
    599 
    600         public function add_cron_schedules($schedules)
    601         {
    602             $time_between_bulk_actions = apply_filters('webp_images_bulk_time_between', WEBP_IMAGES_BULK_TIME);
    603 
    604             $schedules['webp_images_bulk'] = array(
    605                 'interval' => $time_between_bulk_actions,
    606                 'display'  => sprintf(__('%s seconds'), $time_between_bulk_actions),
    607             );
    608             return $schedules;
    609         }
    610 
    611481        public function write_log($log)
    612482        {
     
    620490        }
    621491
    622         public function webp_images_countdown()
    623         {
    624             if (!in_array($_REQUEST['type'], array('conversion', 'remove')) || !wp_verify_nonce($_REQUEST['token'], "webp-images-countdown")) {
    625                 die();
    626             }
    627             $return = 0;
    628             if ('conversion' == $_REQUEST['type']) {
    629                 $result = (int) $this->count_not_webp_media_images();
    630                 $return = $return + $result;
    631             }
    632             if ('remove' == $_REQUEST['type']) {
    633                 $result = (int) $this->count_webp_media_images();
    634                 $return = $return + $result;
    635             }
    636             if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    637                 echo $return;
    638             }
    639             die();
     492        public function start_conversion($limit){
     493            $images_for_conversion = $this->get_not_webp_media_images($limit);
     494
     495            if (count($images_for_conversion) < 1) {
     496                return;
     497            }
     498
     499            $original_time_limit = ini_get('max_execution_time');
     500            ini_set('max_execution_time', 0);
     501
     502            foreach ($images_for_conversion as $id => $files) {
     503                $media_is_converted = $this->convert_to_webp($files);
     504                if($media_is_converted){
     505                    update_post_meta($id, 'webp_src', 'done');
     506                } else {
     507                    $this->delete_webp($files);
     508                    update_post_meta($id, 'webp_src', 'error');
     509                }
     510            }
     511
     512            ini_set('max_execution_time', $original_time_limit);
     513        }
     514
     515        public function auto_delete($attachment_id){
     516            $has_webp_version = get_post_meta($attachment_id, 'webp_src', true) == 'done';
     517            if(!$has_webp_version){
     518                return;
     519            }
     520
     521            $files = [];
     522
     523            $image_path = get_attached_file($attachment_id);
     524            $image_file = basename($image_path);
     525            $files[$image_file] = $image_path;
     526            $image_sizes = get_post_meta($attachment_id, '_wp_attachment_metadata', true);
     527            $sizes = isset($image_sizes['sizes']) ? $image_sizes['sizes'] : false;
     528            if ($sizes) {
     529                foreach ($sizes as $size => $value) {
     530                    $resized_image_path = dirname($image_path) . '/' . $value['file'];
     531                    if (!file_exists($resized_image_path)) {
     532                        continue;
     533                    }
     534                    $files[$value['file']] = $resized_image_path;
     535                }
     536            }
     537
     538            $media_webp_is_deleted = $this->delete_webp($files);
     539            if($media_webp_is_deleted){
     540                delete_post_meta($attachment_id, 'webp_src');
     541            }
     542        }
     543
     544        public function ajax_actions()
     545        {
     546            $action = !empty($_REQUEST['type']) && in_array($_REQUEST['type'], ['conversion', 'delete']) ? $_REQUEST['type'] : false;
     547            if (!$action) {
     548                wp_send_json_error();
     549            }
     550
     551            $nonce = !empty($_REQUEST['token']) && wp_verify_nonce($_REQUEST['token'], "webp-images-ajax");
     552            if (!$nonce) {
     553                wp_send_json_error();
     554            }
     555
     556            $max_elements_for_bulk = apply_filters('webp_images_bulk_max_elements', WEBP_IMAGES_BULK_ELEMENTS);
     557
     558            if($action == 'conversion'){
     559
     560                do_action('webp_images_start_conversion', $max_elements_for_bulk);
     561
     562                wp_send_json_success($this->count_not_webp_media_images());
     563
     564            } else {
     565                $images_for_deletion = $this->get_webp_media_images($max_elements_for_bulk);
     566
     567                if (count($images_for_deletion) < 1) {
     568                    wp_send_json_success(0);
     569                    return;
     570                }
     571
     572                $original_time_limit = ini_get('max_execution_time');
     573                ini_set('max_execution_time', 0);
     574
     575                foreach ($images_for_deletion as $id => $files) {
     576                    $media_webp_is_deleted = $this->delete_webp($files);
     577                    if($media_webp_is_deleted){
     578                        delete_post_meta($id, 'webp_src');
     579                    }
     580                }
     581
     582                ini_set('max_execution_time', $original_time_limit);
     583
     584                wp_send_json_success($this->count_webp_media_images());
     585            }
    640586        }
    641587
     
    644590            $currentVersion = WEBP_IMAGES_VERSION;
    645591            register_activation_hook(__FILE__, function () use ($currentVersion) {
    646 
    647                 $this->stop_cron_schedule();
    648592                $this->write_htaccess_rules();
    649 
    650593                $request_url = add_query_arg(
    651594                    ['id' => 589, 'action' => 'activate', 'domain' => md5(get_home_url()), 'v' => $currentVersion],
     
    655598            });
    656599            register_deactivation_hook(__FILE__, function () use ($currentVersion) {
    657 
    658                 $this->stop_cron_schedule();
    659600                $this->remove_htaccess_rules();
    660 
    661601                $request_url = add_query_arg(
    662602                    ['id' => 589, 'action' => 'deactivate', 'domain' => md5(get_home_url()), 'v' => $currentVersion],
     
    692632        }
    693633    }
    694     $webp_images = new webp_images();
    695     $webp_images->init_plugin();
     634    new webp_images();
     635
     636    do_action('webp_images_loaded');
    696637}
Note: See TracChangeset for help on using the changeset viewer.