diff --git a/assets-version.js b/assets-version.js deleted file mode 100644 index 72446fe..0000000 --- a/assets-version.js +++ /dev/null @@ -1,46 +0,0 @@ -/** - * Currently this script will generate a new version - * number for the theme's asset files. It writes in inc/theme-version.php - */ - -// The two packages required to write in the theme-version.php file -var fs = require('fs'); -var moment = require('moment'); - -// Generate version for theme-version.php for the assets -let someFile = './inc/theme-version.php'; - -fs.readFile(someFile, 'utf8', function (err,data) { - if (err) { - return console.log(err); - } - - // The REGEX to find the assets version - let versionRegex = /define\( 'DX_ASSETS_VERSION', '*(.*?)' \);/g; - - // Find the line where the version is at. - let found = data.match(versionRegex); - - // This will get and increment the version of the assets file. - // 39 must be replaced with automated find - let verIndexOf = found[0].indexOf(', \'') + 3 + 9; // 3 for the ", '" bit and 9 for the date + dash - currentVersion = parseInt(found[0].substring(verIndexOf, found[0].length - 4)); - currentVersion++; - - // Create the date format. Of course you can change that to anything you like - // or even remove it so that you have only the version number and nothing else. - let time = moment().format("YYYYMMDD"); - - // Create the string that we will replace with the existing one. - let themeVersionString = "define( 'DX_ASSETS_VERSION', '" + time + '-' + currentVersion + "' );"; - - // Once again find the string to replace and set the new version - let result = data.replace(versionRegex, themeVersionString); - - // End it all by writing in the file (or throw error :) - fs.writeFile(someFile, result, 'utf8', function (err) { - if (err) return console.log(err); - }); - - console.log("Assets version has been updated!"); -}); diff --git a/functions.php b/functions.php index caf25f0..3bccfcd 100644 --- a/functions.php +++ b/functions.php @@ -7,9 +7,25 @@ * @package DevriX_Starter */ -// Dynamic grab master CSS mod time. -$master_modified_time = filemtime( get_theme_file_path() . '/assets/dist/css/master.min.css' ); -define( 'DX_ASSETS_VERSION', $master_modified_time . '-0000' ); +/* + * Recursive auto version bump function. + * Loops through all assets in the supplied folder and returns the + * latest date of modification. +*/ +function dx_get_assets_version( $dir ) { + $riterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir ) ); + $files = array(); + $assets_modification_time = array(); + foreach ($riterator as $file) { + if ( ! $file->isDir() ) { + array_push( $assets_modification_time, date( "YmdHi", filemtime( $file->getPathname() ) ) ); + } + } + + return max( $assets_modification_time ); +} + +define( 'DX_ASSETS_VERSION', dx_get_assets_version( get_template_directory() . '/assets/dist/' ) ); /** * Implement the Custom Header feature.