Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if( !defined('DS') ) define('DS', DIRECTORY_SEPARATOR);
- define('TC_PATH', realpath(dirname(__FILE__)) . DS);
- /**
- * Combine CSS/JS File
- *
- * This is used to add the combined file to the $wp_scripts->done property so WordPress
- * knows it has been added already and it also determines the absolute path for the file
- * so it can be accessed via the file system.
- *
- * @filter tc_combined_js_path Allows filtering of the full js file path. The filter function
- * is passed 3 optional parameters, 'site url' (bloginfo('url')),
- * 'handle' (the $handle param), and the 'src' value for the script.
- * @filter tc_combined_css_path Allows filtering of the full css file path. The filter function
- * is passed 3 optional parameters, 'site url' (bloginfo('url')),
- * 'handle' (the $handle param), and the 'src' value for the stylesheet.
- * @param string $handle The script/stylesheet's handle it was registered with.
- * @param string $type Valid values are 'css' or 'js'. This determines which
- * type of file we're adding.
- * @return string The full path to the file, if it could be determined,
- * (bool)false otherwise.
- * @access private
- */
- function _tc_add_combine_file( $handle, $type )
- {
- if( $type != 'js' && $type != 'css' ) {
- trigger_error('Invalid file type given! Must be `css` or `js`.');
- return false;
- }
- $siteurl = get_bloginfo('url');
- if( $type == 'js' )
- {
- global $wp_scripts;
- // Strip the site URL from the file so we can determine it's path ...
- $uri = ltrim(str_replace($siteurl, '', $wp_scripts->registered[$handle]->src), '/');
- $path = realpath(ABSPATH . DS . $uri);
- $path = apply_filters('tc_combine_js_path', $path, $siteurl, $handle, $wp_scripts->registered[$handle]->src);
- // Only add it to the done queue if a path was found, this way we don't break the theme...
- if( $path )
- $wp_scripts->done[] = $handle;
- }
- else
- {
- global $wp_styles;
- $uri = ltrim(str_replace($url, '', $wp_styles->registered[$handle]->src), '/');
- $path = realpath(ABSPATH . DS . $uri);
- $path = apply_filters('tc_combine_css_path', $path, $siteurl, $handle, $wp_styles->registered[$handle]->src);
- if( $path )
- $wp_styles->done[] = $handle;
- }
- return $path;
- }
- /**
- * Recursive Combine JS Dependencies
- *
- * A function used by _tc_combine_js() to recursively add queued script's dependencies
- * and perform any that are excluded from the combined JS file.
- *
- * @param string $hdl The queued script handle to process the dependencies for.
- * @param object $wp_scripts The WP_Scripts object, passed by reference.
- * @param array $exclude An array of script handles to exclude from the combined file.
- * @return array An array of all of the dependencies that are included in the combined JS.
- * @access private
- */
- function _tc_recursive_deps( $hdl, &$wp_scripts, $exclude )
- {
- $files = array();
- foreach($wp_scripts->registered[$hdl]->deps as $dep)
- {
- if( count($wp_scripts->registered[$dep]->deps) )
- {
- $files = array_merge($files, _tc_recursive_deps($dep, $wp_scripts, $exclude));
- }
- else
- {
- // If the dependency is in the excluded array, we must go ahead and add it
- // so the scripts depending on it won't break.
- if( in_array($dep, $exclude) ) {
- $wp_scripts->do_items(array($dep));
- continue;
- }
- // Make sure the dependency isn't in the 'done' array so we don't add it twice
- elseif( !in_array($dep, $wp_scripts->done) ) {
- $path = _tc_add_combine_file( $dep, 'js' );
- if( $path ) $files[] = $path;
- }
- }
- }
- // Finally, if the passed queue is in the exclude array, we have to go ahead and add it
- // as well, for the same reason mentioned above.
- if( in_array($hdl, $exclude) ) {
- $wp_scripts->do_items(array($dep));
- }
- else
- {
- $path = _tc_add_combine_file( $hdl, 'js' );
- if( $path ) $files[] = $path;
- }
- return $files;
- }
- /**
- * Combine JS Files
- *
- * This combines all of the Javascript files that have been registered/enqueued via
- * WordPress's script queueing system.
- *
- * The filter 'tc_combine_js' allows for exclusions of specific files.
- * Files are grouped together into two groups, header JS and footer JS, this way
- * they still load in the correct places.
- *
- * @param void
- * @return void
- * @access private
- */
- function _tc_combine_js()
- {
- global $wp_scripts;
- $footer = array();
- $header = array();
- $cachefor = apply_filters('tc_combine_js_cache_for', 60*60);
- // Specify any scripts by handle that should be excluded from the combined JS
- $exclude = apply_filters('tc_combine_js_exclude', array('admin-bar'));
- // First process the header Javascripts
- foreach($wp_scripts->queue as $queue)
- {
- $curqueue = $wp_scripts->registered[$queue];
- // Skip footer scripts and just process header scripts for now...
- if(isset($curqueue->extra['group']) && $curqueue->extra['group'] == '1')
- continue;
- // Recursively add all dependencies
- $header = array_merge($header, _tc_recursive_deps($queue, $wp_scripts, $exclude));
- }
- $header = apply_filters('tc_header_combine_js', $header);
- // Now process the footer Javascripts
- foreach($wp_scripts->queue as $queue)
- {
- if( in_array($queue, $wp_scripts->done) ) continue;
- $footer = array_merge($footer, _tc_recursive_deps($queue, $wp_scripts, $exclude));
- }
- $footer = apply_filters('tc_footer_combine_js', $footer);
- // Use converted MD5 hash to verify all files are included since someone logged in
- // might have different JS that loads than someone who isn't, etc.
- $file_hash = base_convert(md5(implode('', $header)), 10, 36);
- $headjs_cache = apply_filters('tc_headjs_cache_file', 'hjs.cache.'. $file_hash .'.js', $file_hash);
- // Determine if we need to create a new cached header js file or not.
- if( !file_exists(TC_PATH . $headjs_cache) || (filemtime(TC_PATH . $headjs_cache)+intval($cachefor)) < time() )
- {
- $hdl = fopen(TC_PATH . $headjs_cache, "w");
- foreach($header as $file)
- {
- if( !file_exists($file) )
- trigger_error('File `'. $file .'` does not exist!', E_USER_WARNING);
- else
- fwrite($hdl, file_get_contents($file));
- }
- fclose($hdl);
- }
- $file_hash = base_convert(md5(implode('', $footer)), 10, 36);
- $footjs_cache = apply_filters('tc_footjs_cache_file', 'fjs.cache.'. $file_hash .'.js', $file_hash);
- // Determine if we need to create a new cached footer js file or not.
- if( !file_exists(TC_PATH . $footjs_cache) || (filemtime(TC_PATH . $footjs_cache)+intval($cachefor)) < time() )
- {
- $hdl = fopen(TC_PATH . $footjs_cache, "w");
- foreach($footer as $file)
- {
- if( !file_exists($file) )
- trigger_error('File `'. $file .'` does not exist!', E_USER_WARNING);
- else
- fwrite($hdl, file_get_contents($file));
- }
- fclose($hdl);
- }
- // Form the URLs and correct slashes for valid URLs.
- $hjs_url = get_bloginfo('template_directory') .'/'. str_replace('\\', '/', $headjs_cache);
- $fjs_url = get_bloginfo('template_directory') .'/'. str_replace('\\', '/', $footjs_cache);
- wp_enqueue_script('hjs-cache', $hjs_url);
- wp_enqueue_script('fjs-cache', $fjs_url, false, '', true);
- }
- if( !is_admin() ) add_action('wp_print_scripts', '_tc_combine_js');
- /////////////////
- // Filter Test
- /////////////////
- function removeit($excluded) {
- return array_merge($excluded, array('jquery'));
- }
- add_filter('tc_combine_js_exclude', 'removeit', 10, 1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement