Changeset 933423
- Timestamp:
- 06/17/2014 03:27:21 AM (12 years ago)
- Location:
- combine-js
- Files:
-
- 11 added
- 3 edited
-
tags/1.4 (added)
-
tags/1.4/classes (added)
-
tags/1.4/classes/combine-js-class.php (added)
-
tags/1.4/classes/jsmin.php (added)
-
tags/1.4/combine-js.php (added)
-
tags/1.4/js.php (added)
-
tags/1.4/lang (added)
-
tags/1.4/readme.txt (added)
-
tags/1.4/screenshot-1.png (added)
-
tags/1.4/views (added)
-
tags/1.4/views/admin_settings_form.php (added)
-
trunk/classes/combine-js-class.php (modified) (15 diffs)
-
trunk/combine-js.php (modified) (1 diff)
-
trunk/readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
combine-js/trunk/classes/combine-js-class.php
r931595 r933423 8 8 const nspace = 'combine-js'; 9 9 const pname = 'Combine JS'; 10 const version = 1. 3;10 const version = 1.4; 11 11 12 12 protected $_plugin_file; … … 34 34 var $debug = false; 35 35 var $combined = false; 36 var $paths_set = false; 36 37 37 38 /** … … 50 51 */ 51 52 function init() { 53 54 static $init = false; 55 56 if ( $init ) return; 57 58 $init = true; 52 59 53 60 // if delete js button is clicked, delete cache … … 223 230 if( file_exists( $path ) && filesize( $path ) ) $mtime = @filemtime( $path ); 224 231 if ( ( time() - $mtime ) > $this->cachetime ) { 225 if ( $debug ) $this->debug( 'C ACHE EXPIRED(' . $path . ')' );226 if ( $debug ) $this->debug( 'T IME 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 . ')' ); 227 234 return true; 228 235 } 229 if ( $debug ) $this->debug( 'U SING CACHE(' . $path . ')' );236 if ( $debug ) $this->debug( 'Using cache (' . $path . ')' ); 230 237 return false; 231 238 } … … 260 267 */ 261 268 function gather_js ( $to_do ) { 269 270 if ( empty( $to_do ) ) return $to_do; 271 262 272 global $wp_scripts; 263 273 foreach ( $to_do as $key => $handle ) { … … 294 304 if( ! in_array( $context . ':' . $js_file, $this->js_files_ignore ) && ! in_array( $js_file, $this->js_files_ignore ) 295 305 && @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 ); 298 312 $this->js_handles_found[$handle] = $js_src; 299 313 unset( $to_do[$key] ); … … 301 315 } 302 316 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 } 325 329 } 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 } 330 334 } 331 335 } … … 366 370 *@since 0.1 367 371 */ 368 function combine_js () { 372 function combine_js ( $force_to_footer = false ) { 373 374 $this->debug( 'function combine_js' ); 369 375 370 376 $this->combined = true; 371 377 372 378 if ( ! @count( @array_keys( $this->js_handles_found ) ) ) { 373 $this->debug( ' no handles found' );379 $this->debug( 'No handles found' ); 374 380 return; 375 381 } … … 389 395 if ( preg_match( "/\.php/", $js_src ) ) $js_content = $this->curl_file_get_contents ( $js_src ); 390 396 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 ) { 392 399 $footer_content .= $this->compress( $js_content, $handle, $js_src ); 393 unset( $this->js_footer_handles_found[$handle] );394 400 } 395 401 else $header_content .= $this->compress( $js_content, $handle, $js_src ); 402 $this->unset_handle( $handle ); 396 403 } 397 404 else $this->debug( 'SRC NOT FOUND: ' . ABSPATH . $js_src ); … … 401 408 402 409 $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; 403 444 } 404 445 … … 434 475 function write_file ( $file, $content ) { 435 476 if ( is_writable ( dirname( $file ) ) ) { 477 $this->debug( 'Write: ' . $file ); 436 478 $fp = fopen( $file, "w" ); 437 479 if ( flock( $fp, LOCK_EX ) ) { // do an exclusive lock … … 505 547 function install_combined_js () { 506 548 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(); 508 556 509 557 // move temp file to real path … … 515 563 $this->combine_js(); 516 564 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 . ')' ); 518 568 @rename( $this->js_path_tmp, $this->js_path ); 519 569 } … … 534 584 function install_combined_js_footer () { 535 585 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(); 539 593 540 594 // move temp file to real path 541 595 542 596 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 . ')' ); 544 605 @rename( $this->js_path_tmp_footer, $this->js_path_footer ); 545 }546 547 // add handles548 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";551 606 } 552 607 … … 632 687 $label = '-- please make a selection --'; 633 688 if (@strlen($custom_label)) { 634 $label = $custom_label;689 $label = $custom_label; 635 690 } 636 691 -
combine-js/trunk/combine-js.php
r931595 r933423 5 5 Description: WordPress plugin that attempts to combine, minify, and compress JS. 6 6 Author: Convoy 7 Version: 1. 37 Version: 1.4 8 8 Author URI: http://www.weareconvoy.com 9 9 Requires at least: 3.0.0 -
combine-js/trunk/readme.txt
r931595 r933423 5 5 Requires at least: 3.0.1 6 6 Tested up to: 3.9.1 7 Stable tag: 1. 37 Stable tag: 1.4 8 8 License: GPLv2 or later 9 9 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 38 38 39 39 == Changelog == 40 41 = 1.4 = 42 * Optimized how js files are gathered and combined. Added more debugging and created a few functions. 40 43 41 44 = 1.3 =
Note: See TracChangeset
for help on using the changeset viewer.