Plugin Directory

Changeset 3397254


Ignore:
Timestamp:
11/17/2025 01:32:26 PM (3 months ago)
Author:
aurelienlws
Message:

Only options in Front-End now clear cache when changed (instead of all options) ; Cache is emptied less often now (removed on update, on preload start and on temporary deactivation cleaning) ; Updated sitemap fetching to prevent issues when WordPress can't find the original sitemap (due to plugins) ; will now check sitemap_index.xml and wp-sitemap.xml more reliably ; Fixed issue with the cache-cleaning cron that would be executed too soon and too often, preventing cache creation

Location:
lws-optimize/trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • lws-optimize/trunk/Classes/Admin/LwsOptimizeManageAdmin.php

    r3374142 r3397254  
    3030            if (substr(get_current_screen()->id, 0, 29) == "toplevel_page_lws-op-config") {
    3131                remove_all_actions('admin_notices');
    32                 if (get_option('lws_optimize_deactivate_temporarily') && (is_plugin_active('wp-rocket/wp-rocket.php') || is_plugin_active('powered-cache/powered-cache.php') || is_plugin_active('wp-super-cache/wp-cache.php')
     32                if (!get_option('lws_optimize_deactivate_temporarily') && (is_plugin_active('wp-rocket/wp-rocket.php') || is_plugin_active('powered-cache/powered-cache.php') || is_plugin_active('wp-super-cache/wp-cache.php')
    3333                    || is_plugin_active('wp-optimize/wp-optimize.php') || is_plugin_active('wp-fastest-cache/wpFastestCache.php') || is_plugin_active('w3-total-cache/w3-total-cache.php'))) {
    3434                    $this->lws_optimize_warning_incompatibiliy();
     
    749749                        wp_unschedule_event(wp_next_scheduled("lws_optimize_start_filebased_preload"), "lws_optimize_start_filebased_preload");
    750750                    }
    751                     wp_schedule_event(time(), "lws_minute", "lws_optimize_start_filebased_preload");
     751                    wp_schedule_event(time() + 60, "lws_minute", "lws_optimize_start_filebased_preload");
    752752                }
    753753            }
     
    755755
    756756        // Clear cache when updating data
    757         $this->lws_optimize_delete_directory(LWS_OP_UPLOADS, $this);
    758         $logger = fopen($this->log_file, 'a');
    759         fwrite($logger, '[' . gmdate('Y-m-d H:i:s') . '] Removed cache after configuration change' . PHP_EOL);
    760         fclose($logger);
    761 
    762         $this->after_cache_purge_preload();
     757        // $this->lws_optimize_delete_directory(LWS_OP_UPLOADS, $this);
     758        // $logger = fopen($this->log_file, 'a');
     759        // fwrite($logger, '[' . gmdate('Y-m-d H:i:s') . '] Removed cache after configuration change' . PHP_EOL);
     760        // fclose($logger);
     761
     762        // $this->after_cache_purge_preload();
    763763
    764764        if (function_exists("opcache_reset")) {
     
    16011601        $optimize_options['filebased_cache']['timer'] = $timer;
    16021602
     1603        $timer = $optimize_options['filebased_cache']['timer'] ?? "lws_yearly";
     1604        switch ($timer) {
     1605            case 'lws_daily':
     1606                $cdn_date = "86400";
     1607                break;
     1608            case 'lws_weekly':
     1609                $cdn_date = "604800";
     1610                break;
     1611            case 'lws_monthly':
     1612                $cdn_date = "2592000";
     1613                break;
     1614            case 'lws_thrice_monthly':
     1615                $cdn_date = "7776000";
     1616                break;
     1617            case 'lws_biyearly':
     1618                $cdn_date = "15552000";
     1619                break;
     1620            case 'lws_yearly':
     1621                $cdn_date = "31104000";
     1622                break;
     1623            case 'lws_two_years':
     1624                $cdn_date = "62208000";
     1625                break;
     1626            case 'lws_never':
     1627                $cdn_date = "93312000";
     1628                break;
     1629            default:
     1630                $cdn_date = "7776000";
     1631                break;
     1632        }
     1633
     1634
    16031635        // Update Cloudflare TTL to match filebased cache clear timer
    16041636        $this->cloudflare_manager->lws_optimize_change_cloudflare_ttl($timer);
     
    16131645        // Never start cron if timer is defined as zero (infinite)
    16141646        if ($timer != 0) {
    1615             wp_schedule_event(time(), $timer, 'lws_optimize_clear_filebased_cache_cron');
     1647            wp_schedule_event(time() + $cdn_date, $timer, 'lws_optimize_clear_filebased_cache_cron');
    16161648        }
    16171649
  • lws-optimize/trunk/Classes/FileCache/LwsOptimizeFileCache.php

    r3374948 r3397254  
    281281                $modified = $data['html'];
    282282
    283                 var_dump($modified);
    284 
    285283                $cached_elements['css']['file'] += $data['files']['file'];
    286284                $cached_elements['css']['size'] += $data['files']['size'];
  • lws-optimize/trunk/Classes/LwsOptimize.php

    r3374142 r3397254  
    295295        }
    296296
     297        $timer = $optimize_options['filebased_cache']['timer'] ?? "lws_yearly";
     298        switch ($timer) {
     299            case 'lws_daily':
     300                $cdn_date = "86400";
     301                break;
     302            case 'lws_weekly':
     303                $cdn_date = "604800";
     304                break;
     305            case 'lws_monthly':
     306                $cdn_date = "2592000";
     307                break;
     308            case 'lws_thrice_monthly':
     309                $cdn_date = "7776000";
     310                break;
     311            case 'lws_biyearly':
     312                $cdn_date = "15552000";
     313                break;
     314            case 'lws_yearly':
     315                $cdn_date = "31104000";
     316                break;
     317            case 'lws_two_years':
     318                $cdn_date = "62208000";
     319                break;
     320            case 'lws_never':
     321                $cdn_date = "93312000";
     322                break;
     323            default:
     324                $cdn_date = "7776000";
     325                break;
     326        }
     327
    297328        // Schedule the cache cleanout again if it has been deleted
    298329        // If the plugin is OFF or the filecached is deactivated, unregister the WPCron
    299330        if (isset($optimize_options['filebased_cache']['timer']) && !get_option('lws_optimize_deactivate_temporarily')) {
    300331            if (!wp_next_scheduled('lws_optimize_clear_filebased_cache_cron') && $optimize_options['filebased_cache']['timer'] != 0) {
    301                 wp_schedule_event(time(), $optimize_options['filebased_cache']['timer'], 'lws_optimize_clear_filebased_cache_cron');
     332                wp_schedule_event(time() + $cdn_date, $optimize_options['filebased_cache']['timer'], 'lws_optimize_clear_filebased_cache_cron');
    302333            }
    303334        } elseif (get_option('lws_optimize_deactivate_temporarily') || $this->lwsop_check_option('filebased_cache')['state'] === "false") {
     
    344375            $this->lws_optimize_reset_header_htaccess();
    345376
    346             $this->lws_optimize_delete_directory(LWS_OP_UPLOADS, $this);
    347             $logger = fopen($this->log_file, 'a');
    348             fwrite($logger, '[' . gmdate('Y-m-d H:i:s') . '] Removed cache after update' . PHP_EOL);
    349             fclose($logger);
    350 
    351             $this->after_cache_purge_preload();
     377            // $this->lws_optimize_delete_directory(LWS_OP_UPLOADS, $this);
     378            // $logger = fopen($this->log_file, 'a');
     379            // fwrite($logger, '[' . gmdate('Y-m-d H:i:s') . '] Removed cache after update' . PHP_EOL);
     380            // fclose($logger);
     381
     382            // $this->after_cache_purge_preload();
    352383        }
    353384    }
     
    535566        stream_context_set_default([
    536567            'ssl' => [
    537                 'verify_peer' => false,
    538                 'verify_peer_name' => false,
     568            'verify_peer' => false,
     569            'verify_peer_name' => false,
    539570            ],
    540571        ]);
    541572
    542         // Check if sitemap exists
    543         $headers = get_headers($sitemap);
    544         if (substr($headers[0], 9, 3) == 404) {
     573        // Check if sitemap exists and is valid XML
     574        $headers = @get_headers($sitemap);
     575        $is_valid = false;
     576
     577        if ($headers && is_array($headers) && isset($headers[0]) && intval(substr($headers[0], 9, 3)) === 200) {
     578            // Check if it's actually XML by trying to load it
     579            $sitemap_content = @file_get_contents($sitemap);
     580            if ($sitemap_content !== false) {
     581            libxml_use_internal_errors(true);
     582            $xml = @simplexml_load_string($sitemap_content);
     583            if ($xml !== false) {
     584                $is_valid = true;
     585            }
     586            libxml_clear_errors();
     587            }
     588        }
     589
     590        // Fall back to alternative sitemap if the first one is invalid
     591        if (!$is_valid) {
    545592            $sitemap = home_url('/sitemap_index.xml');
    546593        }
     
    680727
    681728            // If files exist and this is first run, count it as done
    682             if (!empty($file_exists) && (!isset($userAgents['mobile']) || !empty($file_exists_mobile))) {
     729            if (!empty($file_exists) && !empty($file_exists_mobile)) {
    683730                if ($first_run) {
    684731                    $done++;
     
    688735
    689736            // Fetch pages with appropriate user agents
    690             foreach ($userAgents as $agent) {
    691             // Ensure the nocache parameter is unique for each request
    692             $unique_nocache = 'nocache=' . time() . '-' . mt_rand(1000, 9999);
    693             $request_url = str_replace('nocache=' . time(), $unique_nocache, $url);
    694 
    695             // Make the request with additional cache-busting headers
    696             $response = wp_remote_get(
    697                 $request_url,
    698                 [
    699                 'timeout'     => 30,
    700                 'user-agent'  => $agent,
    701                 'headers'     => [
    702                     'Cache-Control' => 'no-cache, no-store, must-revalidate',
    703                     'Pragma'        => 'no-cache',
    704                     'Expires'       => '0',
    705                     'X-LWS-Preload' => time(), // Additional unique header
    706                     'X-No-Cache'    => '1'     // Some servers recognize this
    707                 ],
    708                 'sslverify'   => false,
    709                 'blocking'    => true,
    710                 'cookies'     => [], // Clean request with no cookies
    711                 'reject_unsafe_urls' => false, // Allow URLs with query strings
    712                 'redirection' => 3 // Don't follow too many redirects
    713                 ]
    714             );
     737            foreach ($userAgents as $type => $agent) {
     738                if ($type === 'mobile' && $file_exists_mobile) {
     739                    continue; // Skip if mobile cache already exists
     740                } else if ($type === 'desktop' && $file_exists) {
     741                    continue; // Skip if desktop cache already exists
     742                }
     743
     744                // Ensure the nocache parameter is unique for each request
     745                $unique_nocache = 'nocache=' . time() . '-' . mt_rand(1000, 9999);
     746                $request_url = str_replace('nocache=' . time(), $unique_nocache, $url);
     747
     748                // Make the request with additional cache-busting headers
     749                $response = wp_remote_get(
     750                    $request_url,
     751                    [
     752                    'timeout'     => 30,
     753                    'user-agent'  => $agent,
     754                    'headers'     => [
     755                        'Cache-Control' => 'no-cache, no-store, must-revalidate',
     756                        'Pragma'        => 'no-cache',
     757                        'Expires'       => '0',
     758                        'X-LWS-Preload' => time(), // Additional unique header
     759                        'X-No-Cache'    => '1'     // Some servers recognize this
     760                    ],
     761                    'sslverify'   => false,
     762                    'blocking'    => true,
     763                    'cookies'     => [], // Clean request with no cookies
     764                    'reject_unsafe_urls' => false, // Allow URLs with query strings
     765                    'redirection' => 3 // Don't follow too many redirects
     766                    ]
     767                );
    715768            }
    716769
     
    15291582                $cache_dirs = array_merge([$cache_desktop => 'desktop specific'], $cache_dirs);
    15301583            } else {
    1531                 fwrite($logger, '[' . gmdate('Y-m-d H:i:s') . "] Directory $cache_desktop not found." . PHP_EOL);
     1584                fwrite($logger, '[' . gmdate('Y-m-d H:i:s') . "] Directory for $directory not found. No cache to purge" . PHP_EOL);
    15321585            }
    15331586
     
    15361589                $cache_dirs = array_merge([$cache_mobile => 'mobile specific'], $cache_dirs);
    15371590            } else {
    1538                 fwrite($logger, '[' . gmdate('Y-m-d H:i:s') . "] Directory $cache_mobile not found." . PHP_EOL);
     1591                fwrite($logger, '[' . gmdate('Y-m-d H:i:s') . "] Directory for $directory not found. No cache to purge" . PHP_EOL);
    15391592            }
    15401593
     
    16891742    public function after_cache_purge_preload() {
    16901743        $logger = fopen($this->log_file, 'a');
     1744        fwrite($logger, '[' . gmdate('Y-m-d H:i:s') . "] Restarting preload process after cache purge..." . PHP_EOL);
    16911745
    16921746        // Handle preload configuration
     
    20532107
    20542108                wp_unschedule_event(wp_next_scheduled('lws_optimize_clear_filebased_cache_cron'), 'lws_optimize_clear_filebased_cache_cron');
    2055                 wp_schedule_event(time(), 'lws_yearly', 'lws_optimize_clear_filebased_cache_cron');
     2109                wp_schedule_event(time() + YEAR_IN_SECONDS, 'lws_yearly', 'lws_optimize_clear_filebased_cache_cron');
    20562110                wp_unschedule_event(wp_next_scheduled('lws_optimize_maintenance_db_weekly'), 'lws_optimize_maintenance_db_weekly');
    20572111
     
    20642118                        wp_unschedule_event(wp_next_scheduled('lws_optimize_start_filebased_preload'), 'lws_optimize_start_filebased_preload');
    20652119                    }
    2066                     wp_schedule_event(time(), "lws_minute", "lws_optimize_start_filebased_preload");
     2120                    wp_schedule_event(time() + 60, "lws_minute", "lws_optimize_start_filebased_preload");
    20672121                }
    20682122                break;
     
    21022156
    21032157                wp_unschedule_event(wp_next_scheduled('lws_optimize_clear_filebased_cache_cron'), 'lws_optimize_clear_filebased_cache_cron');
    2104                 wp_schedule_event(time(), 'lws_yearly', 'lws_optimize_clear_filebased_cache_cron');
     2158                wp_schedule_event(time() + YEAR_IN_SECONDS, 'lws_yearly', 'lws_optimize_clear_filebased_cache_cron');
    21052159                wp_unschedule_event(wp_next_scheduled('lws_optimize_maintenance_db_weekly'), 'lws_optimize_maintenance_db_weekly');
    21062160
     
    21092163                        wp_unschedule_event(wp_next_scheduled('lws_optimize_start_filebased_preload'), 'lws_optimize_start_filebased_preload');
    21102164                    }
    2111                     wp_schedule_event(time(), "lws_minute", "lws_optimize_start_filebased_preload");
     2165                    wp_schedule_event(time() + 60, "lws_minute", "lws_optimize_start_filebased_preload");
    21122166                }
    21132167                break;
     
    21472201
    21482202                wp_unschedule_event(wp_next_scheduled('lws_optimize_clear_filebased_cache'), 'lws_optimize_clear_filebased_cache');
    2149                 wp_schedule_event(time(), 'lws_biyearly', 'lws_optimize_clear_filebased_cache');
     2203                wp_schedule_event(time() + (6 * MONTH_IN_SECONDS), 'lws_biyearly', 'lws_optimize_clear_filebased_cache_cron');
    21502204                wp_unschedule_event(wp_next_scheduled('lws_optimize_maintenance_db_weekly'), 'lws_optimize_maintenance_db_weekly');
    21512205
     
    21542208                        wp_unschedule_event(wp_next_scheduled('lws_optimize_start_filebased_preload'), 'lws_optimize_start_filebased_preload');
    21552209                    }
    2156                     wp_schedule_event(time() + 10, "lws_minute", "lws_optimize_start_filebased_preload");
     2210                    wp_schedule_event(time() + 60, "lws_minute", "lws_optimize_start_filebased_preload");
    21572211                }
    21582212                break;
  • lws-optimize/trunk/lws-optimize.php

    r3374948 r3397254  
    99 * Plugin URI:        https://www.lws.fr/
    1010 * Description:       Reach better speed and performances with Optimize! Minification, Combination, Media convertion... Everything you need for a better website
    11  * Version:           3.3.16
     11 * Version:           3.3.17
    1212 * Author:            LWS
    1313 * Author URI:        https://www.lws.fr
  • lws-optimize/trunk/readme.txt

    r3374948 r3397254  
    44Requires at least: 6.0
    55Tested up to: 6.8
    6 Stable tag: 3.3.16
     6Stable tag: 3.3.17
    77Requires PHP: 7.4
    88Author: LWS
     
    187187== Changelog ==
    188188
     189= 3.3.17 =
     190* Only options in "Front-End" now clear cache when changed (instead of all options)
     191* Cache is emptied less often now (removed on update, on preload start and on temporary deactivation cleaning)
     192* Updated sitemap fetching to prevent issues when WordPress can't find the original sitemap (due to plugins) ; will now check sitemap_index.xml and wp-sitemap.xml more reliably
     193Fixed issue with the cache-cleaning cron that would be executed too soon and too often, preventing cache creation
     194
    189195= 3.3.16 =
    190196* Fixed issues where directories/files could not be created during page caching, resulting in CSS/JS not being loaded and white page appearing
Note: See TracChangeset for help on using the changeset viewer.