Changeset 716899
- Timestamp:
- 05/22/2013 10:11:28 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
use-google-libraries/trunk/use-google-libraries.php
r715027 r716899 4 4 Plugin URI: http://jasonpenney.net/wordpress-plugins/use-google-libraries/ 5 5 Description: Allows your site to use common javascript libraries from Google's AJAX Libraries CDN, rather than from WordPress's own copies. 6 Version: 1.5. 16 Version: 1.5.2 7 7 Author: Jason Penney 8 8 Author URI: http://jasonpenney.net/ … … 29 29 if ( !class_exists( 'JCP_UseGoogleLibraries' ) ) { 30 30 31 if ( ! defined( 'WP_CONTENT_URL' ) )32 define( 'WP_CONTENT_URL', get_option( 'siteurl' ) . '/wp-content' );33 if ( ! defined( 'WP_CONTENT_DIR' ) )34 define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );35 if ( ! defined( 'WP_PLUGIN_URL' ) )36 define( 'WP_PLUGIN_URL', WP_CONTENT_URL. '/plugins' );37 if ( ! defined( 'WP_PLUGIN_DIR' ) )38 define( 'WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins' );39 40 31 class JCP_UseGoogleLibraries { 41 32 42 33 private static $instance; 43 private static $version = '1.5 ';34 private static $version = '1.5.2'; 44 35 public static function get_instance() { 45 36 if ( !isset( self::$instance ) ) { … … 48 39 return self::$instance; 49 40 } 50 41 42 /** 43 * Registry of script replacement rules 44 * 45 * Entries are formatted as follows: 46 * <code> 47 * 'script-handle' => array( 48 * 'google-lib-path', 49 * 'google-file-name', 50 * 'google-combined-into') 51 * </code> 52 * 53 * - 'script-handle' -- the handle used by WordPress script 54 * registration 55 * - 'google-lib-path' -- path to location on Google CDN( empty 56 * string if script has been combined). 57 * - 'google-file-name' -- file name (minus .js) on Google CDN (empty 58 * string if script has been combined). 59 * - 'google-combined-into' -- If not empty string, then the given 60 * handle has been combined into a file loaded by this handle. 61 * 62 * @var array 63 */ 51 64 protected $google_scripts; 52 protected $noconflict_url; 65 66 /** 67 * Used internally to ensure jQuery.noconflict is executed as close to 68 * how core WordPress would. 69 * 70 * @var bool 71 */ 53 72 protected $noconflict_next; 54 protected $is_ssl; 73 74 /** 75 * script id used for actual jquery script 76 * 77 * @var string 78 * 79 * @since 5.2 80 */ 81 protected $jquery_tag; 82 83 /** 84 * True if using a version of WordPress that allows 85 * `wp_register_script` to take protocol-relative URLs, otherwise False 86 * 87 * @since 1.5.2 88 * 89 * @var bool 90 */ 91 protected $protocol_relative_supported; 92 93 /** 94 * transient name used when caching 95 * 96 * @var string 97 */ 55 98 protected static $cache_id = 'JCP_UseGoogleLibraries_cache'; 99 100 /** 101 * transient expiration 102 * 103 * @var int 104 */ 56 105 protected static $cache_len = 90000; // 25 hours 106 107 /** 108 * Message displayed and logged when a WP_Scripts has been created before it's time 109 * 110 * @var unknown_type 111 */ 57 112 protected static $script_before_init_notice = 58 113 'Another plugin has registered or enqued a script before the "init" action. Attempting to work around it.'; 114 59 115 /** 60 116 * PHP 4 Compatible Constructor … … 66 122 */ 67 123 function __construct() { 124 $this->jquery_tag = 'jquery'; 68 125 $this->google_scripts = 69 126 array( 70 127 // any extra scripts listed here not provided by WordPress 71 // or another plugin will not be registered. This list e128 // or another plugin will not be registered. This list 72 129 // is just used to chancge where things load from. 73 130 … … 140 197 141 198 ); 142 $this->noconflict_url = WP_PLUGIN_URL . '/use-google-libraries/js/jQnc.js';143 144 199 $this->noconflict_next = FALSE; 145 // test for SSL 146 // thanks to suggestions from Peter Wilson (http://peterwilson.cc/) 147 // and Richard Hearne 148 $is_ssl = false; 149 if ( ( function_exists( 'getenv' ) and 150 ( ( getenv( 'HTTPS' ) != '' and getenv( 'HTTPS' ) != 'off' ) 151 or 152 ( getenv( 'SERVER_PORT' ) == '443' ) ) ) 153 or 154 ( isset( $_SERVER ) and 155 ( ( isset( $_SERVER['HTTPS'] ) and $_SERVER['HTTPS'] !='' and $_SERVER['HTTPS'] != 'off' ) 156 or 157 ( isset( $_SERVER['SERVER_PORT'] ) and $_SERVER['SERVER_PORT'] == '443' ) ) ) ) { 158 $is_ssl = true; 159 } 160 $this->is_ssl = $is_ssl; 200 // protocol-relative URLS accepted by `wp_register_scripts` 201 // starting with version 3.5 202 $this->protocol_relative_supported = version_compare( get_bloginfo( 'version' ), '3.5', '>=' ); 161 203 } 162 204 … … 183 225 } 184 226 185 227 /** 228 * Get markup to show error message in admin when $WP_Script created befor it's time 229 * @returns string markup for notice display 230 */ 186 231 static function script_before_init_admin_notice() { 187 232 echo '<div class="error fade"><p>Use Google Libraries: ' . self::$script_before_init_notice . '</p></div>'; … … 242 287 $newscripts = array(); 243 288 $combine_ok = array(); 289 290 // jquery may really be loaded under jquery-core 291 // if so, we'll adjust google_scripts here 292 if ( $scripts->query( 'jquery-core' ) && array_key_exists( 'jquery', $this->google_scripts ) ) { 293 $this->google_scripts['jquery-core'] = $this->google_scripts['jquery']; 294 unset($this->google_scripts['jquery']); 295 $this->jquery_tag = 'jquery-core'; 296 } 297 244 298 foreach ( $this->google_scripts as $name => $values ) { 245 299 if ( $script = $scripts->query( $name ) ) { … … 247 301 $js = $values[1]; 248 302 $combined = $values[2]; 249 250 303 // default to requested ver 251 304 $ver = $script->ver; … … 279 332 if ( $lib != '' ) { 280 333 // build new URL 281 $url = " http://ajax.googleapis.com/ajax/libs/$lib/$ver/$js.js";282 if ( wp_remote_retrieve_response_code( wp_remote_head( $url) ) !== 200 ) {334 $url = "//ajax.googleapis.com/ajax/libs/$lib/$ver/$js.js"; 335 if ( wp_remote_retrieve_response_code( wp_remote_head( "http:$url" ) ) !== 200 ) { 283 336 self::debug( "Google servers do not seem to be hosting requested version of $name (version $ver). Using version provided by WordPress." ); 284 337 continue; 338 } 339 if ( ! $this->protocol_relative_supported ) { 340 $url = "http:$url"; 285 341 } 286 342 $script->src = $url; … … 331 387 // need to handle ssl after cache load, because it may swap 332 388 // back and forth depending on the site config/usage 333 if ( $this->is_ssl === true) {389 if ( ( ! $this->protocol_relative_supported ) && ( is_ssl() ) ) { 334 390 foreach ( $newscripts as $script ) { 335 391 $script->src = preg_replace( '/^http:/', 'https:', $script->src ); … … 340 396 341 397 /** 342 * Replace as many of the wordpress default script registrations as343 * possible with ones from google398 * Replace as many of the WordPress default script registrations as 399 * possible with ones from Google 344 400 * 345 401 * @param object $scripts WP_Scripts object. … … 352 408 // re-register with original ver 353 409 $scripts->add( $script->handle, $script->src, $script->deps, $script->ver ); 354 if ( $olddata ) 410 if ( $olddata ) { 355 411 foreach ( $olddata as $data_name => $data ) { 356 412 $scripts->add_data( $script->handle, $data_name, $data ); 357 413 } 414 } 358 415 } 359 416 } … … 371 428 return $dep_obj->registered[$handle]->extra; 372 429 373 return $dep_obj->registered[$handle]->extra[$data_name]; 430 if ( !method_exists( $dep_obj, 'get_data' ) ) 431 return $dep_obj->registered[$handle]->extra[$data_name]; 432 433 return $dep_obj->get_data( $handle, $data_name ); 374 434 } 375 435 … … 389 449 if ( preg_match( '/ajax\.googleapis\.com\//', $src ) ) { 390 450 $src = remove_query_arg( 'ver', $src ); 391 if ( strpos( $src, $this->google_scripts[ 'jquery'][1] . ".js" ) ) {451 if ( strpos( $src, $this->google_scripts[$this->jquery_tag][1] . ".js" ) ) { 392 452 $this->noconflict_next = TRUE; 393 453 }
Note: See TracChangeset
for help on using the changeset viewer.