Use Google Libraries - Version 1.5.2

Version Description

  • using protocol-relative URLS with WordPress >
Download this release

Release Info

Developer jczorkmid
Plugin Icon wp plugin Use Google Libraries
Version 1.5.2
Comparing to
See all releases

Code changes from version 1.5.1 to 1.5.2

Files changed (2) hide show
  1. README.txt +16 -4
  2. use-google-libraries.php +103 -43
README.txt CHANGED
@@ -3,8 +3,8 @@ Contributors: jczorkmid
3
  Donate link: http://jasonpenney.net/donate
4
  Tags: javascript, performance, CDN, Google, jQuery, Prototype, MooTools, Dojo, Google AJAX Libraries API, YSlow, Page Speed
5
  Requires at least: 2.9.1
6
- Tested up to: 3.5.1
7
- Stable tag: 1.5.1
8
 
9
  Allows your site to use common javascript libraries from Google's AJAX
10
  Libraries CDN, rather than from WordPress's own copies.
@@ -102,6 +102,18 @@ using K2.
102
 
103
  == Changelog ==
104
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  = 1.5 =
106
 
107
  + using `wp_remote_head` to query that the replacement URL is actually
@@ -192,8 +204,8 @@ from [Peter Wilson](http://peterwilson.cc/).
192
 
193
  == Upgrade Notice ==
194
 
195
- = 1.5 =
196
- Detects when Google isn't hosting requested version. Required for WordPress 3.4!
197
 
198
  == A Request ==
199
 
3
  Donate link: http://jasonpenney.net/donate
4
  Tags: javascript, performance, CDN, Google, jQuery, Prototype, MooTools, Dojo, Google AJAX Libraries API, YSlow, Page Speed
5
  Requires at least: 2.9.1
6
+ Tested up to: 3.6b3
7
+ Stable tag: 1.5.2
8
 
9
  Allows your site to use common javascript libraries from Google's AJAX
10
  Libraries CDN, rather than from WordPress's own copies.
102
 
103
  == Changelog ==
104
 
105
+ = 1.5.2 =
106
+
107
+ + using protocol-relative URLS with WordPress >= 3.5
108
+ + use WordPress provided `is_ssl` rather than custom check (only for
109
+ WordPress < 3.5)
110
+ + Detect if 'jquery' is a meta-script registration, and actual jQuery
111
+ is loaded as 'jquery-core' tag (WordPress 3.6 Beta).
112
+
113
+ = 1.5.1 =
114
+
115
+ + fix bug in `is_ssl` check.
116
+
117
  = 1.5 =
118
 
119
  + using `wp_remote_head` to query that the replacement URL is actually
204
 
205
  == Upgrade Notice ==
206
 
207
+ = 1.5.2 =
208
+ Better SSL support. Required for WordPress 3.6!
209
 
210
  == A Request ==
211
 
use-google-libraries.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: Use Google Libraries
4
  Plugin URI: http://jasonpenney.net/wordpress-plugins/use-google-libraries/
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.1
7
  Author: Jason Penney
8
  Author URI: http://jasonpenney.net/
9
  */
@@ -28,34 +28,90 @@
28
 
29
  if ( !class_exists( 'JCP_UseGoogleLibraries' ) ) {
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
  class JCP_UseGoogleLibraries {
41
 
42
  private static $instance;
43
- private static $version = '1.5';
44
  public static function get_instance() {
45
  if ( !isset( self::$instance ) ) {
46
  self::$instance = new JCP_UseGoogleLibraries();
47
  }
48
  return self::$instance;
49
  }
50
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  protected $google_scripts;
52
- protected $noconflict_url;
 
 
 
 
 
 
53
  protected $noconflict_next;
54
- protected $is_ssl;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  protected static $cache_id = 'JCP_UseGoogleLibraries_cache';
 
 
 
 
 
 
56
  protected static $cache_len = 90000; // 25 hours
 
 
 
 
 
 
57
  protected static $script_before_init_notice =
58
  'Another plugin has registered or enqued a script before the "init" action. Attempting to work around it.';
 
59
  /**
60
  * PHP 4 Compatible Constructor
61
  */
@@ -65,10 +121,11 @@ if ( !class_exists( 'JCP_UseGoogleLibraries' ) ) {
65
  * PHP 5 Constructor
66
  */
67
  function __construct() {
 
68
  $this->google_scripts =
69
  array(
70
  // any extra scripts listed here not provided by WordPress
71
- // or another plugin will not be registered. This liste
72
  // is just used to chancge where things load from.
73
 
74
  // 'script-handle' => ( 'google-lib-path', 'google-file-name', 'google-combined-into')
@@ -139,25 +196,10 @@ if ( !class_exists( 'JCP_UseGoogleLibraries' ) ) {
139
  'ext-core' => array( 'ext-core', 'ext-core', '' )
140
 
141
  );
142
- $this->noconflict_url = WP_PLUGIN_URL . '/use-google-libraries/js/jQnc.js';
143
-
144
  $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;
161
  }
162
 
163
  static function configure_plugin() {
@@ -182,7 +224,10 @@ if ( !class_exists( 'JCP_UseGoogleLibraries' ) ) {
182
  }
183
  }
184
 
185
-
 
 
 
186
  static function script_before_init_admin_notice() {
187
  echo '<div class="error fade"><p>Use Google Libraries: ' . self::$script_before_init_notice . '</p></div>';
188
  }
@@ -241,12 +286,20 @@ if ( !class_exists( 'JCP_UseGoogleLibraries' ) ) {
241
  function build_newscripts( &$scripts ) {
242
  $newscripts = array();
243
  $combine_ok = array();
 
 
 
 
 
 
 
 
 
244
  foreach ( $this->google_scripts as $name => $values ) {
245
  if ( $script = $scripts->query( $name ) ) {
246
  $lib = $values[0];
247
  $js = $values[1];
248
  $combined = $values[2];
249
-
250
  // default to requested ver
251
  $ver = $script->ver;
252
 
@@ -278,11 +331,14 @@ if ( !class_exists( 'JCP_UseGoogleLibraries' ) ) {
278
  // it around for dependencies
279
  if ( $lib != '' ) {
280
  // 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 ) {
283
  self::debug( "Google servers do not seem to be hosting requested version of $name (version $ver). Using version provided by WordPress." );
284
  continue;
285
  }
 
 
 
286
  $script->src = $url;
287
  } else {
288
  $script->src = "";
@@ -330,7 +386,7 @@ if ( !class_exists( 'JCP_UseGoogleLibraries' ) ) {
330
  }
331
  // need to handle ssl after cache load, because it may swap
332
  // back and forth depending on the site config/usage
333
- if ( $this->is_ssl === true ) {
334
  foreach ( $newscripts as $script ) {
335
  $script->src = preg_replace( '/^http:/', 'https:', $script->src );
336
  }
@@ -339,8 +395,8 @@ if ( !class_exists( 'JCP_UseGoogleLibraries' ) ) {
339
  }
340
 
341
  /**
342
- * Replace as many of the wordpress default script registrations as
343
- * possible with ones from google
344
  *
345
  * @param object $scripts WP_Scripts object.
346
  */
@@ -351,10 +407,11 @@ if ( !class_exists( 'JCP_UseGoogleLibraries' ) ) {
351
  $scripts->remove( $script->handle );
352
  // re-register with original ver
353
  $scripts->add( $script->handle, $script->src, $script->deps, $script->ver );
354
- if ( $olddata )
355
  foreach ( $olddata as $data_name => $data ) {
356
  $scripts->add_data( $script->handle, $data_name, $data );
357
  }
 
358
  }
359
  }
360
 
@@ -370,7 +427,10 @@ if ( !class_exists( 'JCP_UseGoogleLibraries' ) ) {
370
  if ( !$data_name )
371
  return $dep_obj->registered[$handle]->extra;
372
 
373
- return $dep_obj->registered[$handle]->extra[$data_name];
 
 
 
374
  }
375
 
376
 
@@ -388,7 +448,7 @@ if ( !class_exists( 'JCP_UseGoogleLibraries' ) ) {
388
  }
389
  if ( preg_match( '/ajax\.googleapis\.com\//', $src ) ) {
390
  $src = remove_query_arg( 'ver', $src );
391
- if ( strpos( $src, $this->google_scripts['jquery'][1] . ".js" ) ) {
392
  $this->noconflict_next = TRUE;
393
  }
394
  }
3
  Plugin Name: Use Google Libraries
4
  Plugin URI: http://jasonpenney.net/wordpress-plugins/use-google-libraries/
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.2
7
  Author: Jason Penney
8
  Author URI: http://jasonpenney.net/
9
  */
28
 
29
  if ( !class_exists( 'JCP_UseGoogleLibraries' ) ) {
30
 
 
 
 
 
 
 
 
 
 
31
  class JCP_UseGoogleLibraries {
32
 
33
  private static $instance;
34
+ private static $version = '1.5.2';
35
  public static function get_instance() {
36
  if ( !isset( self::$instance ) ) {
37
  self::$instance = new JCP_UseGoogleLibraries();
38
  }
39
  return self::$instance;
40
  }
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
+ */
64
  protected $google_scripts;
65
+
66
+ /**
67
+ * Used internally to ensure jQuery.noconflict is executed as close to
68
+ * how core WordPress would.
69
+ *
70
+ * @var bool
71
+ */
72
  protected $noconflict_next;
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
+ */
98
  protected static $cache_id = 'JCP_UseGoogleLibraries_cache';
99
+
100
+ /**
101
+ * transient expiration
102
+ *
103
+ * @var int
104
+ */
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
+ */
112
  protected static $script_before_init_notice =
113
  'Another plugin has registered or enqued a script before the "init" action. Attempting to work around it.';
114
+
115
  /**
116
  * PHP 4 Compatible Constructor
117
  */
121
  * PHP 5 Constructor
122
  */
123
  function __construct() {
124
+ $this->jquery_tag = 'jquery';
125
  $this->google_scripts =
126
  array(
127
  // any extra scripts listed here not provided by WordPress
128
+ // or another plugin will not be registered. This list
129
  // is just used to chancge where things load from.
130
 
131
  // 'script-handle' => ( 'google-lib-path', 'google-file-name', 'google-combined-into')
196
  'ext-core' => array( 'ext-core', 'ext-core', '' )
197
 
198
  );
 
 
199
  $this->noconflict_next = FALSE;
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', '>=' );
 
 
 
 
 
 
 
 
 
 
 
 
 
203
  }
204
 
205
  static function configure_plugin() {
224
  }
225
  }
226
 
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
+ */
231
  static function script_before_init_admin_notice() {
232
  echo '<div class="error fade"><p>Use Google Libraries: ' . self::$script_before_init_notice . '</p></div>';
233
  }
286
  function build_newscripts( &$scripts ) {
287
  $newscripts = array();
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
+
298
  foreach ( $this->google_scripts as $name => $values ) {
299
  if ( $script = $scripts->query( $name ) ) {
300
  $lib = $values[0];
301
  $js = $values[1];
302
  $combined = $values[2];
 
303
  // default to requested ver
304
  $ver = $script->ver;
305
 
331
  // it around for dependencies
332
  if ( $lib != '' ) {
333
  // build new URL
334
+ $url = "//ajax.googleapis.com/ajax/libs/$lib/$ver/$js.js";
335
+ if ( wp_remote_retrieve_response_code( wp_remote_head( "http:$url" ) ) !== 200 ) {
336
  self::debug( "Google servers do not seem to be hosting requested version of $name (version $ver). Using version provided by WordPress." );
337
  continue;
338
  }
339
+ if ( ! $this->protocol_relative_supported ) {
340
+ $url = "http:$url";
341
+ }
342
  $script->src = $url;
343
  } else {
344
  $script->src = "";
386
  }
387
  // need to handle ssl after cache load, because it may swap
388
  // back and forth depending on the site config/usage
389
+ if ( ( ! $this->protocol_relative_supported ) && ( is_ssl() ) ) {
390
  foreach ( $newscripts as $script ) {
391
  $script->src = preg_replace( '/^http:/', 'https:', $script->src );
392
  }
395
  }
396
 
397
  /**
398
+ * Replace as many of the WordPress default script registrations as
399
+ * possible with ones from Google
400
  *
401
  * @param object $scripts WP_Scripts object.
402
  */
407
  $scripts->remove( $script->handle );
408
  // re-register with original ver
409
  $scripts->add( $script->handle, $script->src, $script->deps, $script->ver );
410
+ if ( $olddata ) {
411
  foreach ( $olddata as $data_name => $data ) {
412
  $scripts->add_data( $script->handle, $data_name, $data );
413
  }
414
+ }
415
  }
416
  }
417
 
427
  if ( !$data_name )
428
  return $dep_obj->registered[$handle]->extra;
429
 
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 );
434
  }
435
 
436
 
448
  }
449
  if ( preg_match( '/ajax\.googleapis\.com\//', $src ) ) {
450
  $src = remove_query_arg( 'ver', $src );
451
+ if ( strpos( $src, $this->google_scripts[$this->jquery_tag][1] . ".js" ) ) {
452
  $this->noconflict_next = TRUE;
453
  }
454
  }