Plugin Directory

Changeset 3478429


Ignore:
Timestamp:
03/09/2026 06:17:21 PM (3 weeks ago)
Author:
solankisoftware
Message:

Fix batch limit input issue, improve batch processing, add validation, timeouts, loading indicators, and dedicated WebP storage folder

Location:
mediahue-webp-converter
Files:
6 added
4 edited

Legend:

Unmodified
Added
Removed
  • mediahue-webp-converter/trunk/assets/admin-webp.css

    r3476630 r3478429  
    412412}
    413413
     414/* Progress spinner */
     415.mhwc-admin .mhwc-progress-spinner {
     416  display: inline-block;
     417  width: 16px;
     418  height: 16px;
     419  border: 2px solid rgba(31, 27, 23, 0.3);
     420  border-radius: 50%;
     421  border-top-color: var(--mhwc-accent);
     422  animation: mhwc-spin 1s ease-in-out infinite;
     423  margin-right: 8px;
     424}
     425
    414426@keyframes mhwc-spin {
    415427  to {
  • mediahue-webp-converter/trunk/assets/admin-webp.js

    r3476630 r3478429  
    1313            $(document).on('click', '.mhwc-convert-batch-btn', function(e) {
    1414                e.preventDefault();
    15                 var $button = $(this);
    16                 var $container = $button.closest('.mhwc-card');
    17                 var limit = $container.find('input[name="limit"]').val();
    18 
    19                 // Store original button text
    20                 $button.data('original-text', $button.html());
    21                 self.startBatchProcess('convert', $container, limit);
     15                var limit = $('#convert_limit').val();
     16                limit = parseInt(limit) || 50;
     17                self.startBatchProcess('convert', $(this).closest('.mhwc-card'), limit);
    2218            });
    2319
     
    2521            $(document).on('click', '.mhwc-delete-batch-btn', function(e) {
    2622                e.preventDefault();
    27                 var $button = $(this);
    28                 var $container = $button.closest('.mhwc-card');
    29                 var limit = $container.find('input[name="limit"]').val();
    30 
    31                 // Store original button text
    32                 $button.data('original-text', $button.html());
    33                 self.startBatchProcess('delete', $container, limit);
     23                var limit = $('#delete_limit').val();
     24                limit = parseInt(limit) || 50;
     25                self.startBatchProcess('delete', $(this).closest('.mhwc-card'), limit);
    3426            });
    3527
     
    4537                var $container = $(this).closest('.mhwc-card');
    4638                var type = $container.find('.mhwc-batch-progress').hasClass('mhwc-error') ? self.type : 'convert';
    47                 var limit = $container.find('input[name="limit"]').val();
     39                var limit = type === 'convert' ? $('#convert_limit').val() : $('#delete_limit').val();
     40                limit = parseInt(limit) || 50;
    4841
    4942                // Remove error UI and restart
     
    126119                url: mhwc_ajax.ajax_url,
    127120                type: 'POST',
    128                 timeout: 30000, // 30 seconds timeout
     121                timeout: 120000, // 120 seconds timeout
    129122                data: {
    130123                    action: 'mhwc_bulk_' + this.type,
     
    156149
    157150        updateProgressDetails: function(message) {
    158             this.$container.find('.mhwc-progress-details').html('<span class="mhwc-processing-indicator">⏳</span> ' + message);
     151            this.$container.find('.mhwc-progress-details').html('<span class="mhwc-progress-spinner"></span> ' + message);
    159152        },
    160153
  • mediahue-webp-converter/trunk/mediahue-webp-converter.php

    r3476630 r3478429  
    33 * Plugin Name: MediaHue – Webp Converter for WordPress
    44 * Description: Automatically convert WordPress images to WebP format to improve website performance, reduce image size, and boost SEO.
    5  * Version:     1.0.1
    6  * Author:      Solanki Software
     5 * Version:     1.0.2
     6 * Author:      Kirtikumar Solanki
    77 * Author URI:  https://profiles.wordpress.org/solankisoftware/
    88 * License:     GPLv2 or later
     
    332332
    333333    public static function mhwc_get_webp_path( $file_path ) {
     334        $upload_dir = wp_get_upload_dir();
     335        $base_dir = trailingslashit( $upload_dir['basedir'] );
     336        $webp_base_dir = trailingslashit( $base_dir . 'mediahue-webp-converter-attachments' );
     337
     338        // Ensure the directory exists
     339        if ( ! file_exists( $webp_base_dir ) ) {
     340            wp_mkdir_p( $webp_base_dir );
     341        }
     342
     343        // Get relative path from uploads
     344        if ( strpos( $file_path, $base_dir ) === 0 ) {
     345            $relative = substr( $file_path, strlen( $base_dir ) );
     346            $webp_path = $webp_base_dir . preg_replace( '/\.(jpe?g|png)$/i', '.webp', $relative );
     347
     348            // Ensure subdirectory exists
     349            $webp_dir = dirname( $webp_path );
     350            if ( ! file_exists( $webp_dir ) ) {
     351                wp_mkdir_p( $webp_dir );
     352            }
     353
     354            return $webp_path;
     355        }
     356
     357        // Fallback for files outside uploads
    334358        return preg_replace( '/\.(jpe?g|png)$/i', '.webp', $file_path );
    335359    }
     
    339363        $base_dir = trailingslashit( $upload_dir['basedir'] );
    340364        $base_url = trailingslashit( $upload_dir['baseurl'] );
     365        $webp_base_dir = trailingslashit( $base_dir . 'mediahue-webp-converter-attachments' );
     366        $webp_base_url = trailingslashit( $base_url . 'mediahue-webp-converter-attachments' );
     367
     368        if ( strpos( $file_path, $webp_base_dir ) === 0 ) {
     369            $relative = substr( $file_path, strlen( $webp_base_dir ) );
     370            return $webp_base_url . $relative;
     371        }
     372
     373        // Fallback
    341374        if ( strpos( $file_path, $base_dir ) !== 0 ) return false;
    342375        $relative = ltrim( str_replace( $base_dir, '', $file_path ), '/' );
     
    509542                        <label>
    510543                            <?php echo esc_html__( 'Batch size', 'mediahue-webp-converter' ); ?>
    511                             <input type="number" name="convert_limit" min="1" max="200" value="50" />
     544                            <input type="number" id="convert_limit" name="convert_limit" min="1" max="200" value="50" />
    512545                        </label>
    513546                        <button type="button" class="button button-primary mhwc-convert-batch-btn">
     
    526559                        <label>
    527560                            <?php echo esc_html__( 'Batch size', 'mediahue-webp-converter' ); ?>
    528                             <input type="number" name="delete_limit" min="1" max="200" value="50" />
     561                            <input type="number" id="delete_limit" name="delete_limit" min="1" max="200" value="50" />
    529562                        </label>
    530563                        <button type="button" class="button button-delete mhwc-delete-batch-btn">
     
    553586            plugin_dir_url( __FILE__ ) . 'assets/admin-webp.css',
    554587            array(),
    555             '1.0.0'
     588            '1.0.2'
    556589        );
    557590        wp_enqueue_script(
     
    559592            plugin_dir_url( __FILE__ ) . 'assets/admin-webp.js',
    560593            array( 'jquery' ),
    561             '1.0.0',
     594            '1.0.6',
    562595            true
    563596        );
     
    608641        check_admin_referer( self::mhwc_nonce_action( 'bulk_convert' ) );
    609642
     643        // Prevent PHP timeout for long processes
     644        set_time_limit( 600 ); // 10 minutes
     645
    610646        $limit = isset( $_POST['limit'] ) ? max( 1, min( 200, intval( $_POST['limit'] ) ) ) : 50;
    611647        $offset = isset( $_POST['offset'] ) ? max( 0, intval( $_POST['offset'] ) ) : 0;
    612648
    613         $ids = get_posts( array(
     649        $all_ids = get_posts( array(
    614650            'post_type'      => 'attachment',
    615651            'post_mime_type' => array( 'image/jpeg', 'image/png' ),
    616652            'fields'         => 'ids',
    617             'posts_per_page' => $limit,
    618             'offset'         => $offset,
     653            'posts_per_page' => -1,
     654            'orderby'        => 'ID',
     655            'order'          => 'ASC',
    619656        ) );
     657
     658        $ids = array();
     659        foreach ( $all_ids as $attachment_id ) {
     660            $file = get_attached_file( $attachment_id );
     661            if ( $file && ! file_exists( self::mhwc_get_webp_path( $file ) ) ) {
     662                $ids[] = $attachment_id;
     663                if ( count( $ids ) >= $limit ) {
     664                    break;
     665                }
     666            }
     667        }
    620668
    621669        $converted = 0;
     
    638686        }
    639687
    640         $has_more = ( count( $ids ) === $limit );
    641         $total_processed = $offset + count( $ids );
     688        $has_more = false; // Process only one batch of $limit
     689        $total_processed = count( $ids );
    642690        $stats = self::mhwc_get_conversion_stats();
    643691        $current_percentage = $stats['total'] > 0 ? round( ( $stats['converted'] / $stats['total'] ) * 100, 1 ) : 0;
     
    646694            'page'      => self::mhwc_tools_page_slug(),
    647695            'converted' => $converted,
    648             'offset'    => $offset + $limit,
    649             'has_more'  => $has_more ? 1 : 0,
     696            'offset'    => 0, // Reset offset since we process only one batch
     697            'has_more'  => 0,
    650698            'processed' => $total_processed,
    651699            'percentage' => $current_percentage,
     
    670718        check_admin_referer( self::mhwc_nonce_action( 'bulk_delete' ) );
    671719
     720        // Prevent PHP timeout for long processes
     721        set_time_limit( 600 ); // 10 minutes
     722
    672723        $limit = isset( $_POST['limit'] ) ? max( 1, min( 200, intval( $_POST['limit'] ) ) ) : 50;
    673724        $offset = isset( $_POST['offset'] ) ? max( 0, intval( $_POST['offset'] ) ) : 0;
    674725
    675         $ids = get_posts( array(
     726        $all_ids = get_posts( array(
    676727            'post_type'      => 'attachment',
    677728            'post_mime_type' => array( 'image/jpeg', 'image/png' ),
    678729            'fields'         => 'ids',
    679             'posts_per_page' => $limit,
    680             'offset'         => $offset,
     730            'posts_per_page' => -1,
     731            'orderby'        => 'ID',
     732            'order'          => 'ASC',
    681733        ) );
     734
     735        $ids = array();
     736        foreach ( $all_ids as $attachment_id ) {
     737            $file = get_attached_file( $attachment_id );
     738            if ( $file && file_exists( self::mhwc_get_webp_path( $file ) ) ) {
     739                $ids[] = $attachment_id;
     740                if ( count( $ids ) >= $limit ) {
     741                    break;
     742                }
     743            }
     744        }
    682745
    683746        $deleted = 0;
     
    686749        }
    687750
    688         $has_more = ( count( $ids ) === $limit );
     751        $has_more = false; // Process only one batch of $limit
    689752        $redirect = add_query_arg(
    690753            array(
    691754                'page'    => self::mhwc_tools_page_slug(),
    692755                'deleted' => $deleted,
    693                 'offset'  => $offset + $limit,
    694                 'has_more_delete' => $has_more ? 1 : 0,
     756                'offset'  => 0, // Reset offset
     757                'has_more_delete' => 0,
    695758            ),
    696759            admin_url( 'upload.php' )
     
    726789            $offset = isset( $_POST['offset'] ) ? max( 0, intval( $_POST['offset'] ) ) : 0;
    727790
    728             $ids = get_posts( array(
     791            $all_ids = get_posts( array(
    729792                'post_type'      => 'attachment',
    730793                'post_mime_type' => array( 'image/jpeg', 'image/png' ),
    731794                'fields'         => 'ids',
    732                 'posts_per_page' => $limit,
    733                 'offset'         => $offset,
     795                'posts_per_page' => -1,
     796                'orderby'        => 'ID',
     797                'order'          => 'ASC',
    734798            ) );
     799
     800            $ids = array();
     801            foreach ( $all_ids as $attachment_id ) {
     802                $file = get_attached_file( $attachment_id );
     803                if ( $file && ! file_exists( self::mhwc_get_webp_path( $file ) ) ) {
     804                    $ids[] = $attachment_id;
     805                    if ( count( $ids ) >= $limit ) {
     806                        break;
     807                    }
     808                }
     809            }
    735810
    736811            $converted = 0;
     
    753828            }
    754829
    755             $has_more = ( count( $ids ) === $limit );
     830            $has_more = false; // Process only one batch of $limit
    756831            $stats = self::mhwc_get_conversion_stats();
    757832
     
    761836                'failed'            => $failed,
    762837                'has_more'          => $has_more,
    763                 'offset'            => $offset + $limit,
     838                'offset'            => 0, // Reset offset
    764839                'total_converted'   => $stats['converted'],
    765840                'total_images'      => $stats['total'],
     
    782857            $offset = isset( $_POST['offset'] ) ? max( 0, intval( $_POST['offset'] ) ) : 0;
    783858
    784             $ids = get_posts( array(
     859            $all_ids = get_posts( array(
    785860                'post_type'      => 'attachment',
    786861                'post_mime_type' => array( 'image/jpeg', 'image/png' ),
    787862                'fields'         => 'ids',
    788                 'posts_per_page' => $limit,
    789                 'offset'         => $offset,
     863                'posts_per_page' => -1,
     864                'orderby'        => 'ID',
     865                'order'          => 'ASC',
    790866            ) );
     867
     868            $ids = array();
     869            foreach ( $all_ids as $attachment_id ) {
     870                $file = get_attached_file( $attachment_id );
     871                if ( $file && file_exists( self::mhwc_get_webp_path( $file ) ) ) {
     872                    $ids[] = $attachment_id;
     873                    if ( count( $ids ) >= $limit ) {
     874                        break;
     875                    }
     876                }
     877            }
    791878
    792879            $deleted = 0;
     
    795882            }
    796883
    797             $has_more = ( count( $ids ) === $limit );
     884            $has_more = false; // Process only one batch of $limit
    798885
    799886            wp_send_json_success( array(
    800887                'deleted'  => $deleted,
    801888                'has_more' => $has_more,
    802                 'offset'   => $offset + $limit,
     889                'offset'   => 0, // Reset offset
    803890            ) );
    804891        } catch ( Exception $e ) {
  • mediahue-webp-converter/trunk/readme.txt

    r3476630 r3478429  
    66Tested up to: 6.9
    77Requires PHP: 7.4
    8 Stable tag: 1.0.1
     8Stable tag: 1.0.2
    99License: GPLv2 or later
    1010License URI: https://www.gnu.org/licenses/gpl-2.0.html
     
    5151
    5252== Changelog ==
     53= 1.0.2 =
     54* Fixed batch limit input selection issue.
     55* Added input validation with fallback value (50).
     56* Increased AJAX timeout and PHP execution time for large batches.
     57* Ensured consistent batch processing with ID ASC ordering.
     58* Updated batch logic to process one batch per run.
     59* Added loading indicators for convert/delete actions.
     60* Added dedicated folder for WebP files with automatic creation.
     61* Improved file organization and cache busting.
     62
    5363= 1.0.1
    5464* Improve Design and Set Bacth process using Ajax
Note: See TracChangeset for help on using the changeset viewer.