Skip to content

Commit

Permalink
Tweaks to also include potential sub scripts in the cache invalidatio…
Browse files Browse the repository at this point in the history
…n + check if the file exists before altering the URL
  • Loading branch information
barnemax-pilotin committed Apr 5, 2023
1 parent 30980c0 commit 20c3be7
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions includes/classes/main/class-main.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ public function admin_enqueue_pip_style() {
* @return string
*/
public function invalidate_pilopress_scripts_cache( $url ) {
// Identify Pilo'Press layouts folder
$needle_pilopress_layout = 'pilopress/layouts/';

// Target only Pilo'Press layouts scripts
if ( strpos( $url, 'pilopress/layouts' ) === false ) {
if ( strpos( $url, $needle_pilopress_layout ) === false ) {
return $url;
}

Expand All @@ -70,17 +73,16 @@ public function invalidate_pilopress_scripts_cache( $url ) {
// Replace url structure by a path
$script = str_replace( home_url(), '', $url );

// Use pathinfo to get filename
$script = pathinfo( $script );
$script_name = acf_maybe_get( $script, 'filename' );
if ( !is_array( $script ) || !$script_name ) {
//Get only content after "pilopress/layouts/" to be used with PIP_THEME_LAYOUTS_PATH and PIP_THEME_LAYOUTS_URL later
$script_path_in_layout = substr( $script, strpos( $script, $needle_pilopress_layout ) + strlen( $needle_pilopress_layout ) );

// If script file doesn't exist, return original url
if ( !file_exists( PIP_THEME_LAYOUTS_PATH . $script_path_in_layout ) ) {
return $url;
}

// Return script url including file modification date timestamp as "?ver" string
$url = PIP_THEME_LAYOUTS_URL .
"$script_name/$script_name.js?ver=" .
filemtime( PIP_THEME_LAYOUTS_PATH . "$script_name/$script_name.js" );
$url = PIP_THEME_LAYOUTS_URL . $script_path_in_layout . '?ver=' . filemtime( PIP_THEME_LAYOUTS_PATH . $script_path_in_layout );

return $url;
}
Expand Down

0 comments on commit 20c3be7

Please sign in to comment.