Plugin Directory

Changeset 933423


Ignore:
Timestamp:
06/17/2014 03:27:21 AM (12 years ago)
Author:
timmcdaniels
Message:

version 1.4

Location:
combine-js
Files:
11 added
3 edited

Legend:

Unmodified
Added
Removed
  • combine-js/trunk/classes/combine-js-class.php

    r931595 r933423  
    88    const nspace = 'combine-js';
    99    const pname = 'Combine JS';
    10     const version = 1.3;
     10    const version = 1.4;
    1111
    1212    protected $_plugin_file;
     
    3434    var $debug = false;
    3535    var $combined = false;
     36    var $paths_set = false;
    3637
    3738    /**
     
    5051    */
    5152    function init() {
     53
     54        static $init = false;
     55
     56        if ( $init ) return;
     57
     58        $init = true;
    5259
    5360        // if delete js button is clicked, delete cache
     
    223230        if( file_exists( $path ) && filesize( $path ) ) $mtime = @filemtime( $path );
    224231        if ( ( time() - $mtime ) > $this->cachetime ) {
    225             if ( $debug ) $this->debug( 'CACHE EXPIRED (' . $path . ')' );
    226             if ( $debug ) $this->debug( 'TIME SINCE (' . ( time() - $mtime ) . ') and Cache Time (' . $this->cachetime . ')' );
     232            if ( $debug ) $this->debug( 'Cache expired (' . $path . ')' );
     233            if ( $debug ) $this->debug( 'Time since (' . ( time() - $mtime ) . ') and cache time (' . $this->cachetime . ')' );
    227234            return true;
    228235        }
    229         if ( $debug ) $this->debug( 'USING CACHE (' . $path . ')' );
     236        if ( $debug ) $this->debug( 'Using cache (' . $path . ')' );
    230237        return false;
    231238    }
     
    260267    */
    261268    function gather_js ( $to_do ) {
     269
     270        if ( empty( $to_do ) ) return $to_do;
     271
    262272        global $wp_scripts;
    263273        foreach ( $to_do as $key => $handle ) {
     
    294304            if( ! in_array( $context . ':' . $js_file, $this->js_files_ignore ) && ! in_array( $js_file, $this->js_files_ignore )
    295305                && @strlen( $js_src ) && $this->file_exists( $js_src ) ) {
    296                 if ( $context ) $this->debug( 'JS context & file found (' . $context . ':' . $js_file . ')' );
    297                 else $this->debug( 'JS file found (' . $js_file . ')' );
     306                $msg = 'JS context & file found (' . $context . ':' . $js_file;
     307                if ( ! $context ) $msg = 'JS file found (' . $js_file;
     308                if ( isset( $this->js_footer_handles_found[$handle] ) ) $msg .= ' [footer]';
     309                else $msg .= ' [header]';
     310                $msg .= ')';
     311                $this->debug( $msg );
    298312                $this->js_handles_found[$handle] = $js_src;
    299313                unset( $to_do[$key] );
     
    301315        }
    302316
    303         // get name of file (token) based on md5 hash of js handles
    304 
    305         $this->js_token = md5( implode( '', array_keys( $this->js_handles_found ) ) );
    306 
    307         // set paths
    308 
    309         $this->js_path = $this->upload_path . $this->js_token . '.js';
    310         $this->js_path_footer = $this->upload_path . $this->js_token . '-footer.js';
    311         $this->js_uri = $this->upload_uri . $this->js_token . '.js';
    312         $this->js_uri_footer = $this->upload_uri . $this->js_token . '-footer.js';
    313         $this->js_path_tmp = $this->js_path . '.tmp';
    314         $this->js_path_tmp_footer = $this->js_path_footer . '.tmp';
    315 
    316         // loop through and unset scripts
    317 
    318         foreach ( $to_do as $key => $handle ) {
    319             $js_src = $this->strip_domain( $wp_scripts->registered[$handle]->src );
    320             $js_file = $this->get_file_from_src( $js_src );
    321             $context = $this->get_context( $js_src );
    322             if( ! in_array( $context . ':' . $js_file, $this->js_files_ignore ) &&
    323                 ! in_array( $js_file, $this->js_files_ignore )  && $this->file_exists( $js_src ) ) {
    324                 wp_deregister_script( $handle );
     317        if ( array_keys( $this->js_handles_found ) ) {
     318
     319            // loop through and unset scripts
     320
     321            foreach ( $to_do as $key => $handle ) {
     322                $js_src = $this->strip_domain( $wp_scripts->registered[$handle]->src );
     323                $js_file = $this->get_file_from_src( $js_src );
     324                $context = $this->get_context( $js_src );
     325                if( ! in_array( $context . ':' . $js_file, $this->js_files_ignore ) &&
     326                    ! in_array( $js_file, $this->js_files_ignore )  && $this->file_exists( $js_src ) ) {
     327                    wp_deregister_script( $handle );
     328                }
    325329            }
    326         }
    327         foreach ( $wp_scripts->queue as $key => $handle ) {
    328             if ( isset( $this->js_handles_found[$handle] ) ) {
    329                 unset( $wp_scripts->queue[$key] );
     330            foreach ( $wp_scripts->queue as $key => $handle ) {
     331                if ( isset( $this->js_handles_found[$handle] ) ) {
     332                    unset( $wp_scripts->queue[$key] );
     333                }
    330334            }
    331335        }
     
    366370    *@since 0.1
    367371    */
    368     function combine_js () {
     372    function combine_js ( $force_to_footer = false ) {
     373
     374        $this->debug( 'function combine_js' );
    369375
    370376        $this->combined = true;
    371377
    372378        if ( ! @count( @array_keys( $this->js_handles_found ) ) ) {
    373             $this->debug( 'no handles found' );
     379            $this->debug( 'No handles found' );
    374380            return;
    375381        }
     
    389395                if ( preg_match( "/\.php/", $js_src ) ) $js_content = $this->curl_file_get_contents ( $js_src );
    390396                else $js_content = file_get_contents( ABSPATH . $js_src );
    391                 if ( isset( $this->js_footer_handles_found[$handle] ) ) {
     397                $this->debug( 'combine - ' . ABSPATH . $js_src );
     398                if ( isset( $this->js_footer_handles_found[$handle] ) || $force_to_footer ) {
    392399                    $footer_content .= $this->compress( $js_content, $handle, $js_src );
    393                     unset( $this->js_footer_handles_found[$handle] );
    394400                }
    395401                else $header_content .= $this->compress( $js_content, $handle, $js_src );
     402                $this->unset_handle( $handle );
    396403            }
    397404            else $this->debug( 'SRC NOT FOUND: ' . ABSPATH . $js_src );
     
    401408
    402409        $this->cache_content( $header_content, $footer_content );
     410    }
     411
     412    /**
     413    *Unset handle
     414    *
     415    *@return void
     416    *@since 1.4
     417    */
     418    function unset_handle ( $handle = '' ) {
     419        unset( $this->js_footer_handles_found[$handle] );
     420        unset( $this->js_handles_found[$handle] );
     421    }
     422
     423    /**
     424    *Set paths
     425    *
     426    *@return void
     427    *@since 1.4
     428    */
     429    function set_paths () {
     430
     431        // get name of file (token) based on md5 hash of js handles
     432
     433        $this->js_token = md5( implode( '', array_keys( $this->js_handles_found ) ) );
     434
     435        // set paths (only do once)
     436
     437        $this->js_path = $this->upload_path . $this->js_token . '.js';
     438        $this->js_path_footer = $this->upload_path . $this->js_token . '-footer.js';
     439        $this->js_uri = $this->upload_uri . $this->js_token . '.js';
     440        $this->js_uri_footer = $this->upload_uri . $this->js_token . '-footer.js';
     441        $this->js_path_tmp = $this->js_path . '.tmp';
     442        $this->js_path_tmp_footer = $this->js_path_footer . '.tmp';
     443        $this->paths_set = true;
    403444    }
    404445
     
    434475    function write_file ( $file, $content ) {
    435476        if ( is_writable ( dirname( $file ) ) ) {
     477            $this->debug( 'Write: ' . $file );
    436478            $fp = fopen( $file, "w" );
    437479            if ( flock( $fp, LOCK_EX ) ) { // do an exclusive lock
     
    505547    function install_combined_js () {
    506548
    507         if ( ! ( $this->js_path ) ) return;
     549        // if no header handles found, return
     550
     551        if ( ! @count( @array_keys( $this->js_handles_found ) ) ) return;
     552
     553        // set paths
     554
     555        $this->set_paths();
    508556
    509557        // move temp file to real path
     
    515563            $this->combine_js();
    516564
    517             $this->debug( 'Create Combined Header File (' . $this->js_path . ')' );
     565            // move temp file to actual path
     566
     567            $this->debug( 'Create combined header file (' . $this->js_path . ')' );
    518568            @rename( $this->js_path_tmp, $this->js_path );
    519569        }
     
    534584    function install_combined_js_footer () {
    535585
    536         if ( ! ( $this->js_path_footer ) ) return;
    537 
    538         if ( ! $this->combined ) $this->combine_js();
     586        // if no header handles found, return
     587
     588        if ( ! @count( @array_keys( $this->js_footer_handles_found ) ) ) return;
     589
     590        // set paths
     591
     592        if ( ! $this->paths_set ) $this->set_paths();
    539593
    540594        // move temp file to real path
    541595
    542596        if ( $this->cache_expired( $this->js_path_footer ) )  {
    543             $this->debug( 'Create Combined Footer File (' . $this->js_path_footer . ')' );
     597
     598            // combine javascript, if necessary
     599       
     600            if ( ! $this->combined ) $this->combine_js( true );
     601
     602            // move temp file to actual path
     603
     604            $this->debug( 'Create combined footer file (' . $this->js_path_footer . ')' );
    544605            @rename( $this->js_path_tmp_footer, $this->js_path_footer );
    545         }
    546 
    547         // add handles
    548 
    549         foreach ( $this->js_footer_handles_found as $handle => $src ) {
    550             echo "\t\t" . '<script type="text/javascript" src="' . str_replace( get_option( 'siteurl' ), $this->js_domain, $src ) . '"></script>' . "\n";
    551606        }
    552607
     
    632687        $label = '-- please make a selection --';
    633688        if (@strlen($custom_label)) {
    634                 $label = $custom_label;
     689            $label = $custom_label;
    635690        }
    636691
  • combine-js/trunk/combine-js.php

    r931595 r933423  
    55Description: WordPress plugin that attempts to combine, minify, and compress JS.
    66Author: Convoy
    7 Version: 1.3
     7Version: 1.4
    88Author URI: http://www.weareconvoy.com
    99Requires at least: 3.0.0
  • combine-js/trunk/readme.txt

    r931595 r933423  
    55Requires at least: 3.0.1
    66Tested up to: 3.9.1
    7 Stable tag: 1.3
     7Stable tag: 1.4
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    3838
    3939== Changelog ==
     40
     41= 1.4 =
     42* Optimized how js files are gathered and combined. Added more debugging and created a few functions.
    4043
    4144= 1.3 =
Note: See TracChangeset for help on using the changeset viewer.