Plugin Directory

Changeset 3183932


Ignore:
Timestamp:
11/07/2024 02:50:16 PM (17 months ago)
Author:
blizhost
Message:

Release version 5.0.2

Location:
blizhost-cache-purge
Files:
17 added
2 edited

Legend:

Unmodified
Added
Removed
  • blizhost-cache-purge/trunk/blizhost-cache-purge.php

    r3180223 r3183932  
    44Plugin URI: https://www.blizhost.com
    55Description: Automatic Cache Clearing and CloudCache Integration to Boost Speed and Protect Your Site with Enhanced Security.
    6 Version: 5.0.1
     6Version: 5.0.2
    77Author: Blizhost
    88Author URI: https://www.blizhost.com
     
    2424    protected $purgeUrls = array();         // URLs to be purged
    2525    protected $processedPosts = array();    // Posts that have been processed
    26     public $p_version = '5.0.1';            // Plugin version
     26    public $p_version = '5.0.2';            // Plugin version
    2727    public $do_full_purge = false;          // Flag to trigger a full cache purge
    2828   
     
    862862        // Store the original URL
    863863        $original_url = $url;
    864        
     864
    865865        // Handle protocol-relative URLs
    866866        if ( strpos( $url, '//' ) === 0 ) {
    867867            $url = ( $this->is_ssl ? 'https:' : 'http:' ) . $url;
    868868        }
    869        
    870         // Use get_option('home') to get the base URL, including subdirectory but excluding language paths and other paths customized by plugins
     869
     870        // Use get_option('home') to get the base URL, including subdirectory
    871871        $home_url_original = get_option( 'home' );
    872872
     
    900900        $dirname = dirname( $path );
    901901
     902        // Initialize width and height
     903        $width = null;
     904        $height = null;
     905
     906        // Check if filename has size suffix like -670x443
    902907        $pattern = '/^(.+)-(\d+)x(\d+)\.' . preg_quote( $extension, '/' ) . '$/i';
    903908        if ( preg_match( $pattern, $filename, $matches ) ) {
     
    907912            $height = $matches[3];
    908913
    909             // Before reconstructing the original path, check if this filename is actually the original image
    910             // Cache attachment IDs to avoid redundant queries
    911             static $attachment_id_cache = array();
    912             if ( isset( $attachment_id_cache[ $url ] ) ) {
    913                 $attachment_id = $attachment_id_cache[ $url ];
     914            // Reconstruct the original filename
     915            $original_filename = $base_filename . '.' . $extension;
     916            $original_path = trailingslashit( $dirname ) . $original_filename;
     917
     918            // Build the full file paths
     919            $upload_dir = wp_upload_dir();
     920            $upload_base_dir = $upload_dir['basedir']; // Absolute path to uploads directory
     921            $upload_base_url = $upload_dir['baseurl']; // Base URL to uploads directory
     922
     923            // Remove the upload base URL from the original URL to get the relative path
     924            $relative_original_path = str_replace( $upload_base_url, '', $home_url_original . $original_path );
     925            $original_file_full_path = trailingslashit( $upload_base_dir ) . ltrim( str_replace( '/', DIRECTORY_SEPARATOR, $relative_original_path ), DIRECTORY_SEPARATOR );
     926
     927            // Check if original file exists without using file_exists unnecessarily
     928            static $file_exists_cache = array();
     929            if ( isset( $file_exists_cache[ $original_file_full_path ] ) ) {
     930                $original_file_exists = $file_exists_cache[ $original_file_full_path ];
    914931            } else {
    915                 $attachment_id = attachment_url_to_postid( $url );
    916                 $attachment_id_cache[ $url ] = $attachment_id;
    917             }
    918 
    919             if ( $attachment_id ) {
    920                 $metadata = wp_get_attachment_metadata( $attachment_id );
    921                 if ( $metadata && isset( $metadata['file'] ) ) {
    922                     // Get the original filename from metadata
    923                     $original_file = basename( $metadata['file'] );
    924                     if ( $original_file === $filename ) {
    925                         // The filename is the original image, do not modify
    926                         $original_path = $path;
    927                         $width = null;
    928                         $height = null;
    929                     } else {
    930                         // Reconstruct the path without size suffix
    931                         $original_path = trailingslashit( $dirname ) . $base_filename . '.' . $extension;
    932                     }
    933                 } else {
    934                     // Could not get metadata, proceed as before
    935                     $original_path = trailingslashit( $dirname ) . $base_filename . '.' . $extension;
    936                 }
     932                $original_file_exists = file_exists( $original_file_full_path );
     933                $file_exists_cache[ $original_file_full_path ] = $original_file_exists;
     934            }
     935
     936            if ( $original_file_exists ) {
     937                // Original file exists, use it
     938                $cdn_image_path = $original_path;
    937939            } else {
    938                 // Could not get attachment ID, proceed as before
    939                 $original_path = trailingslashit( $dirname ) . $base_filename . '.' . $extension;
     940                // Original file does not exist, use the current path
     941                $cdn_image_path = $path;
     942                // Width and height are from filename
    940943            }
    941944        } else {
    942945            // No size suffix found; use the original path
    943             $original_path = $path;
    944             $width = null;
    945             $height = null;
     946            $cdn_image_path = $path;
     947
     948            // Attempt to get width and height from image metadata
     949            // Build the full file path
     950            $upload_dir = wp_upload_dir();
     951            $upload_base_dir = $upload_dir['basedir']; // Absolute path to uploads directory
     952            $upload_base_url = $upload_dir['baseurl']; // Base URL to uploads directory
     953
     954            // Remove the upload base URL from the URL to get the relative path
     955            $relative_path = str_replace( $upload_base_url, '', $url );
     956            $image_file_full_path = trailingslashit( $upload_base_dir ) . ltrim( str_replace( '/', DIRECTORY_SEPARATOR, $relative_path ), DIRECTORY_SEPARATOR );
     957
     958            // Check if file exists and get dimensions
     959            static $image_size_cache = array();
     960            if ( isset( $image_size_cache[ $image_file_full_path ] ) ) {
     961                list( $width, $height ) = $image_size_cache[ $image_file_full_path ];
     962            } else {
     963                if ( file_exists( $image_file_full_path ) ) {
     964                    $image_info = getimagesize( $image_file_full_path );
     965                    if ( $image_info ) {
     966                        $width = $image_info[0];
     967                        $height = $image_info[1];
     968                        $image_size_cache[ $image_file_full_path ] = array( $width, $height );
     969                    }
     970                }
     971            }
    946972        }
    947973
     
    951977        if ( ! $cdn_domain ) {
    952978            // If CDN domain is not available, return original URL
    953             return $url;
    954         }
    955 
    956         // Build the CDN URL including the original host and the original path
    957         $cdn_url = 'https://i' . $this->bliz_cdn_get_server_num( $url ) . '.' . $cdn_domain . '/' . $host . $original_path;
     979            return $original_url;
     980        }
     981
     982        // Build the CDN URL including the original host and the image path
     983        $cdn_url = 'https://i' . $this->bliz_cdn_get_server_num( $url ) . '.' . $cdn_domain . '/' . $host . $cdn_image_path;
    958984
    959985        // Build the query parameters
  • blizhost-cache-purge/trunk/readme.txt

    r3180223 r3183932  
    55Tested up to: 6.7
    66Requires PHP: 5.6
    7 Stable tag: 5.0.1
     7Stable tag: 5.0.2
    88License: GPLv3
    99License URI: https://www.gnu.org/licenses/gpl-3.0.html
     
    102102
    103103== Changelog ==
     104
     105= 5.0.2 =
     106* Optimized CDN Image URL Replacement by removing database queries and utilizing filesystem checks.
     107* Enhanced Image Dimension Handling by extracting width and height from image files when size suffixes are absent.
     108* Implemented Caching Mechanism for file existence and image dimensions to improve performance.
     109* Maintained Compatibility with various scenarios and plugins, ensuring existing functionalities remain intact.
    104110
    105111= 5.0.1 =
Note: See TracChangeset for help on using the changeset viewer.