Created
July 12, 2017 01:30
-
-
Save hellofromtonya/085a36e7f3081ebc3e7bde2b5c4b4034 to your computer and use it in GitHub Desktop.
Asset versioning helpers and asset loader for Twenty Seventeen theme
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 | |
/** | |
* Asset versioning helpers and asset loader | |
* | |
* @package Twenty Seventeen | |
* @since 1.0.0 | |
* @author hellofromTonya | |
* @link https://KnowTheCode.io | |
* @license GNU-2.0+ | |
*/ | |
add_action( 'wp_enqueue_scripts', 'twentyseventeen_scripts' ); | |
/** | |
* Enqueue scripts and styles. | |
*/ | |
function twentyseventeen_scripts() { | |
$version_number = get_theme_version(); | |
$theme_dir = get_stylesheet_directory(); | |
// Add custom fonts, used in the main stylesheet. | |
wp_enqueue_style( 'twentyseventeen-fonts', twentyseventeen_fonts_url(), array(), null ); | |
// Theme stylesheet. | |
wp_enqueue_style( 'twentyseventeen-style', get_stylesheet_uri(), array(), $version_number ); | |
// Load the dark colorscheme. | |
if ( 'dark' === get_theme_mod( 'colorscheme', 'light' ) || is_customize_preview() ) { | |
$asset_file = '/assets/css/colors-dark.css'; | |
wp_enqueue_style( | |
'twentyseventeen-colors-dark', | |
get_theme_file_uri( $asset_file ), | |
array( 'twentyseventeen-style' ), | |
get_asset_current_version_number( $theme_dir . $asset_file ) | |
); | |
} | |
// Load the Internet Explorer 9 specific stylesheet, to fix display issues in the Customizer. | |
if ( is_customize_preview() ) { | |
$asset_file = '/assets/css/ie9.css'; | |
wp_enqueue_style( | |
'twentyseventeen-ie9', | |
get_theme_file_uri( $asset_file ), | |
array( 'twentyseventeen-style' ), | |
get_asset_current_version_number( $theme_dir . $asset_file ) | |
); | |
wp_style_add_data( 'twentyseventeen-ie9', 'conditional', 'IE 9' ); | |
} | |
// Load the Internet Explorer 8 specific stylesheet. | |
$asset_file = '/assets/css/ie8.css'; | |
wp_enqueue_style( | |
'twentyseventeen-ie8', | |
get_theme_file_uri( $asset_file ), | |
array( 'twentyseventeen-style' ), | |
get_asset_current_version_number( $theme_dir . $asset_file ) | |
); | |
wp_style_add_data( 'twentyseventeen-ie8', 'conditional', 'lt IE 9' ); | |
// Load the html5 shiv. | |
$asset_file = '/assets/js/html5.js'; | |
wp_enqueue_script( | |
'html5', | |
get_theme_file_uri( $asset_file ), | |
array(), | |
get_asset_current_version_number( $theme_dir . $asset_file ) | |
); | |
wp_script_add_data( 'html5', 'conditional', 'lt IE 9' ); | |
$asset_file = '/assets/js/skip-link-focus-fix.js'; | |
wp_enqueue_script( | |
'twentyseventeen-skip-link-focus-fix', | |
get_theme_file_uri( $asset_file ), | |
array(), | |
get_asset_current_version_number( $theme_dir . $asset_file ), | |
true | |
); | |
$twentyseventeen_l10n = array( | |
'quote' => twentyseventeen_get_svg( array( 'icon' => 'quote-right' ) ), | |
); | |
if ( has_nav_menu( 'top' ) ) { | |
wp_enqueue_script( 'twentyseventeen-navigation', get_theme_file_uri( '/assets/js/navigation.js' ), | |
array( 'jquery' ), '1.0', true ); | |
$twentyseventeen_l10n['expand'] = __( 'Expand child menu', 'twentyseventeen' ); | |
$twentyseventeen_l10n['collapse'] = __( 'Collapse child menu', 'twentyseventeen' ); | |
$twentyseventeen_l10n['icon'] = twentyseventeen_get_svg( array( | |
'icon' => 'angle-down', | |
'fallback' => true, | |
) ); | |
} | |
$asset_file = '/assets/js/global.js'; | |
wp_enqueue_script( | |
'twentyseventeen-global', | |
get_theme_file_uri( $asset_file ), | |
array( 'jquery' ), | |
get_asset_current_version_number( $theme_dir . $asset_file ), | |
true | |
); | |
$asset_file = '/assets/js/jquery.scrollTo.js'; | |
wp_enqueue_script( | |
'jquery-scrollto', | |
get_theme_file_uri( $asset_file ), | |
array( 'jquery' ), | |
get_asset_current_version_number( $theme_dir . $asset_file ), | |
true | |
); | |
wp_localize_script( | |
'twentyseventeen-skip-link-focus-fix', | |
'twentyseventeenScreenReaderText', | |
$twentyseventeen_l10n | |
); | |
if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) { | |
wp_enqueue_script( 'comment-reply' ); | |
} | |
} | |
/** | |
* Get the theme's version. | |
* | |
* When in development/mode mode, it uses the style.css modification time. | |
* Else, it grabs the version number from the stylesheet. | |
* | |
* @since 1.0.0 | |
* | |
* @return string|int | |
*/ | |
function get_theme_version() { | |
if ( site_is_in_debug_mode() ) { | |
return get_asset_current_version_number( | |
get_stylesheet_directory() . '/style.css' | |
); | |
} | |
$theme = wp_get_theme(); | |
return $theme->get( 'Version' ); | |
} | |
/** | |
* Get the specified asset file's current version number. | |
* This function gets the file's modification time when | |
* the site is in development/debug mode. | |
* | |
* @since 1.0.0 | |
* | |
* @param $asset_file | |
* | |
* @return bool|int | |
*/ | |
function get_asset_current_version_number( $asset_file ) { | |
return filemtime( $asset_file ); | |
} | |
/** | |
* Checks if the site is in development/debug mode. | |
* | |
* @since 1.0.0 | |
* | |
* @return bool | |
*/ | |
function site_is_in_debug_mode() { | |
if ( ! defined( 'SCRIPT_DEBUG' ) ) { | |
return false; | |
} | |
return ( (bool) SCRIPT_DEBUG === true ); | |
} | |
add_filter( 'stylesheet_uri', 'change_theme_stylesheet_uri_to_min_version', 9999, 2 ); | |
/** | |
* Change the theme's stylesheet URI to minified version when not | |
* in development/debug mode. | |
* | |
* @since 1.0.0 | |
* | |
* @param string $stylesheet_uri Stylesheet URI for the current theme/child theme. | |
* @param string $stylesheet_dir_uri Stylesheet directory URI for the current theme/child theme. | |
* | |
* @return string | |
*/ | |
function change_theme_stylesheet_uri_to_min_version( $stylesheet_uri, $stylesheet_dir_uri ) { | |
if ( site_is_in_debug_mode() ) { | |
return $stylesheet_uri; | |
} | |
$minified_stylesheet_filename = '/style.min.css'; | |
if ( ! file_exists( get_stylesheet_directory() . $minified_stylesheet_filename ) ) { | |
return $stylesheet_uri; | |
} | |
return $stylesheet_dir_uri . $minified_stylesheet_filename; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment