Plugin Directory

Changeset 3155289


Ignore:
Timestamp:
09/20/2024 04:06:55 PM (18 months ago)
Author:
usetopic
Message:

Cache busting mechanism

Location:
topic
Files:
29 added
2 edited

Legend:

Unmodified
Added
Removed
  • topic/trunk/index.php

    r3153093 r3155289  
    77  * License: GPL v2 or later
    88  * License URI: https://www.gnu.org/licenses/gpl-2.0.html
    9   * Version: 1.0.28
     9  * Version: 1.0.29
    1010  */
    1111if( ! defined( 'ABSPATH') ) {
     
    3939    }
    4040
     41    /**
     42     * Function to find the file path for index.[hash].js or index-classic.[hash].js
     43     * @param string $filePattern - The file pattern to search for (e.g., 'index' or 'index-classic')
     44     * @param string $directory - The directory where the build files are located
     45     * @return string|null - Returns the full file path or null if not found
     46    */
     47    public function getHashedFilePath($filePattern, $directory = 'build') {
     48        // Get the full path to the plugin directory
     49        $plugin_dir = plugin_dir_path(__FILE__);
     50        $full_directory_path = $plugin_dir . $directory;
     51
     52        if (!is_dir($full_directory_path)) {
     53            return null;
     54        }
     55
     56        // Define the regex pattern to match files like index.[hash].js or index-classic.[hash].js
     57        $pattern = sprintf('/^%s\.[a-f0-9]{8}\.js$/', preg_quote($filePattern, '/'));
     58
     59        // Scan the directory for files
     60        $files = scandir($full_directory_path);
     61
     62        // Iterate through the files to find a match
     63        foreach ($files as $file) {
     64            if (preg_match($pattern, $file)) {
     65                // Return the relative path of the matched file
     66                return $directory . '/' . $file;
     67            }
     68        }
     69
     70        // If no match was found, return null
     71        return null;
     72    }
     73
    4174    public function topic_enqueue_assets() {
    42       wp_enqueue_script(
    43         'topic-gutenberg-sidebar',
    44         plugins_url( 'build/index.js', __FILE__ ),
    45         array( 'wp-plugins', 'wp-edit-post', 'wp-i18n', 'wp-element' )
    46       );
    47       wp_enqueue_style('topic-sidebar-global', plugins_url( 'build/index.css', __FILE__ ), array(), time(), 'all');
     75        $index_file_path = $this->getHashedFilePath('index');
     76        if ($index_file_path === null) {
     77            $index_file_path = 'build/index.js';
     78        }
     79        wp_enqueue_script(
     80            'topic-gutenberg-sidebar',
     81            plugins_url( $index_file_path, __FILE__ ),
     82            array( 'wp-plugins', 'wp-edit-post', 'wp-i18n', 'wp-element' ),
     83            null
     84        );
     85        wp_enqueue_style('topic-sidebar-global', plugins_url( 'build/index.css', __FILE__ ), array(), 'all');
    4886    }
    4987   
    5088    public function topic_enqueue_assets_classic() {
     89        $index_file_path = $this->getHashedFilePath('index-classic');
     90        if ($index_file_path === null) {
     91            $index_file_path = 'build/index-classic.js';
     92        }
    5193          wp_enqueue_script(
    5294                'topic-gutenberg-sidebar-classic',
    53                 plugins_url( 'build/index-classic.js', __FILE__ ), array('wp-plugins'), time(), true
     95                plugins_url( $index_file_path , __FILE__ ), array('wp-plugins'), null
    5496          );
    55           wp_enqueue_style('topic-classic-sidebar', plugins_url( 'classic-editor/classic.css', __FILE__ ), array(), time(), 'all');
    56           wp_enqueue_style('topic-sidebar-global', plugins_url( 'build/index.css', __FILE__ ), array(), time(), 'all');
     97          wp_enqueue_style('topic-classic-sidebar', plugins_url( 'classic-editor/classic.css', __FILE__ ), array(), 'all');
     98          wp_enqueue_style('topic-sidebar-global', plugins_url( 'build/index.css', __FILE__ ), array(), 'all');
    5799    }
    58100   
  • topic/trunk/readme.txt

    r3153093 r3155289  
    8484
    8585== Changelog ==
     86= 1.0.29 =
     87Release Date: SEP 25, 2024
     88Cache Busting Mechanism
     89
    8690= 1.0.28 =
    8791Release Date: SEP 12, 2024
Note: See TracChangeset for help on using the changeset viewer.