Last active
February 5, 2020 09:57
-
-
Save kierzniak/b8fc18518837d61e3c9850c2b43736da to your computer and use it in GitHub Desktop.
Function to resolve assets url with attached content hash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Function to resolve assets url with attached content hash. | |
* | |
* Browser cache mechanism is used to store locally once downloaded assets. This | |
* improves website performance and saves network bandwidth. It may be also creating | |
* a problem when a user visits your website and do not see the newest changes in assets | |
* because the browser is serving an old file from cache. | |
* | |
* This function aims to resolve this issue by adding a content hash to each asset. | |
* If you want to load e.g. "/css/style.css" file function will find this file | |
* add a content hash to a filename and copy file to new path "/css/style.am51n9x...css". | |
* | |
* @author Motivast motivast.com | |
* @copyright 2019 - present, Motivast | |
* | |
* @license https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt GPL-2.0-or-later | |
* | |
* @link https://gist.github.com/kierzniak/b8fc18518837d61e3c9850c2b43736da | |
*/ | |
if ( ! function_exists( 'motivast_assets' ) ) { | |
/** | |
* Resolve assets url with attached content hash. | |
* | |
* @param string $path Relative path to asset | |
* | |
* @return string Full url to the assets with checksum attached | |
*/ | |
function motivast_assets( $path ) { | |
$theme_path = ( ! motivast_is_child_theme_scope() ) ? get_template_directory() : get_stylesheet_directory(); | |
$theme_url = ( ! motivast_is_child_theme_scope() ) ? get_template_directory_uri() : get_stylesheet_directory_uri(); | |
$full_path = sprintf('%s%s', $theme_path, $path); | |
if( !file_exists( $full_path ) ) { | |
throw new \Exception( sprintf( 'Provided assest do not exist in given path "%s"', $full_path )); | |
} | |
$checksum = sha1_file( $full_path ); | |
$dir = pathinfo( $full_path, PATHINFO_DIRNAME ); | |
$file = pathinfo( $full_path, PATHINFO_FILENAME ); | |
$ext = pathinfo( $full_path, PATHINFO_EXTENSION); | |
$checksum_path = sprintf( '%s/%s.%s.%s', $dir, $file, $checksum, $ext ); | |
$checksum_url = str_replace( $theme_path, $theme_url, $checksum_path ); | |
if( !file_exists( $checksum_path ) ) { | |
$copy_results = copy($full_path, $checksum_path); | |
if( !$copy_results ) { | |
throw new \Exception( sprintf( 'Failed to copy asset from path "%s" to "%s"', $full_path, $checksum_path )); | |
} | |
} | |
return $checksum_url; | |
} | |
} | |
if ( ! function_exists( 'motivast_is_child_theme_scope' ) ) { | |
/** | |
* Detect if executed code is inside parrent or child theme scope | |
* | |
* @return bool | |
*/ | |
function motivast_is_child_theme_scope() { | |
// If there is no child theme we can not execute code from it. | |
if( !is_child_theme() ){ | |
return false; | |
} | |
$trace = current( debug_backtrace() ); | |
$trace_file = $trace['file']; | |
$parrent_theme = get_template_directory(); | |
$child_theme = get_stylesheet_directory(); | |
if( strpos( $trace_file, $parrent_theme ) !== false ) { | |
return false; | |
} | |
if( strpos( $trace_file, $child_theme ) !== false ) { | |
return true; | |
} | |
throw new \Exception( 'Current scope is not executed from parent or child theme.' ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment