Use Google Libraries - Version 1.5b1

Version Description

= 1.2.1 = Required for WordPress 3.3.2

Download this release

Release Info

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

Code changes from version 1.2.1 to 1.5b1

Files changed (2) hide show
  1. README.txt +24 -1
  2. use-google-libraries.php +109 -27
README.txt CHANGED
@@ -3,7 +3,7 @@ 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.3.2
7
  Stable tag: 1.2.1
8
 
9
  Allows your site to use common javascript libraries from Google's AJAX
@@ -75,6 +75,21 @@ The same way you use them when Use Google Libraries isn't installed using
75
  'wp_enqueue_script'. In fact you should develop your theme or plugin without
76
  Use Google Libraries being enabled at all, then test it both ways.
77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  == Incompatible Plugins ==
79
 
80
 
@@ -87,6 +102,14 @@ using K2.
87
 
88
  == Changelog ==
89
 
 
 
 
 
 
 
 
 
90
  = 1.2.1 =
91
 
92
  + Added check for WordPress including non-standard versions of scripts (fixes
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.4rc1
7
  Stable tag: 1.2.1
8
 
9
  Allows your site to use common javascript libraries from Google's AJAX
75
  'wp_enqueue_script'. In fact you should develop your theme or plugin without
76
  Use Google Libraries being enabled at all, then test it both ways.
77
 
78
+ = Why do some scripts continue to use the WordPress supplied versions? =
79
+
80
+ Assuming some other plugin or theme isn't the cause, Use Google Libries does
81
+ it's very best to ensure your site behaves as it should when using the stock
82
+ WordPress scripts. If WordPress is asking for a version of a script that
83
+ Google isn't hosting, then it will continue to use the WordPress supplied
84
+ version.
85
+
86
+ = Can I always load the latest version of a hosted script? =
87
+
88
+ No. Use Google Libraries doesn't do that because it would almost certainly
89
+ break WordPress. Even if it didn't, the less 'version specific' URLs supported
90
+ by Google's CDN set a short term 'Expires' header, so there wouldn't be much
91
+ point.
92
+
93
  == Incompatible Plugins ==
94
 
95
 
102
 
103
  == Changelog ==
104
 
105
+ = 1.5 =
106
+
107
+ + using `wp_remote_head` to query that the replacement URL is actually
108
+ hosted by google. If it's not, then the WordPress supplied version will be
109
+ used.
110
+ + Using the Transient API to store the replacement URLS, rather than
111
+ recalculating and re-querying them every load.
112
+
113
  = 1.2.1 =
114
 
115
  + Added check for WordPress including non-standard versions of scripts (fixes
use-google-libraries.php CHANGED
@@ -2,8 +2,8 @@
2
  /*
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.2.1
7
  Author: Jason Penney
8
  Author URI: http://jasonpenney.net/
9
  */
@@ -40,7 +40,7 @@ if ( !class_exists( 'JCP_UseGoogleLibraries' ) ) {
40
  class JCP_UseGoogleLibraries {
41
 
42
  private static $instance;
43
-
44
  public static function get_instance() {
45
  if ( !isset( self::$instance ) ) {
46
  self::$instance = new JCP_UseGoogleLibraries();
@@ -52,8 +52,10 @@ if ( !class_exists( 'JCP_UseGoogleLibraries' ) ) {
52
  protected $noconflict_url;
53
  protected $noconflict_next;
54
  protected $is_ssl;
 
 
55
  protected static $script_before_init_notice =
56
- '<strong>Use Google Libraries</strong>: Another plugin has registered or enqued a script before the "init" action. Attempting to work around it.';
57
  /**
58
  * PHP 4 Compatible Constructor
59
  */
@@ -174,10 +176,7 @@ if ( !class_exists( 'JCP_UseGoogleLibraries' ) ) {
174
  // that.
175
  global $wp_scripts;
176
  if ( is_a( $wp_scripts, 'WP_Scripts' ) ) {
177
- if ( WP_DEBUG !== false ) {
178
- error_log( self::$script_before_init_notice );
179
- }
180
-
181
  $ugl = self::get_instance();
182
  $ugl->replace_default_scripts( $wp_scripts );
183
  }
@@ -185,7 +184,7 @@ if ( !class_exists( 'JCP_UseGoogleLibraries' ) ) {
185
 
186
 
187
  static function script_before_init_admin_notice() {
188
- echo '<div class="error fade"><p>' . self::$script_before_init_notice . '</p></div>';
189
  }
190
 
191
  static function setup_filter() {
@@ -193,6 +192,22 @@ if ( !class_exists( 'JCP_UseGoogleLibraries' ) ) {
193
  $ugl->setup();
194
  }
195
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
196
  /**
197
  * Disables script concatination, which breaks when dependencies are not
198
  * all loaded locally.
@@ -210,13 +225,22 @@ if ( !class_exists( 'JCP_UseGoogleLibraries' ) ) {
210
  }
211
 
212
  /**
213
- * Replace as many of the wordpress default script registrations as possible
214
- * with ones from google
215
  *
216
- * @param object $scripts WP_Scripts object.
 
 
 
 
 
 
 
 
 
217
  */
218
- function replace_default_scripts( &$scripts ) {
219
  $newscripts = array();
 
220
  foreach ( $this->google_scripts as $name => $values ) {
221
  if ( $script = $scripts->query( $name ) ) {
222
  $lib = $values[0];
@@ -225,11 +249,9 @@ if ( !class_exists( 'JCP_UseGoogleLibraries' ) ) {
225
 
226
  // default to requested ver
227
  $ver = $script->ver;
228
-
229
  if ( strpos( $ver, '-' ) !== false ) {
230
- if ( WP_DEBUG !== false ) {
231
- error_log("WordPress appears to be using a non-standard version of $name (version $ver). Use Google Libraries not enabled for $name.");
232
- }
233
  continue;
234
  }
235
 
@@ -239,10 +261,16 @@ if ( !class_exists( 'JCP_UseGoogleLibraries' ) ) {
239
  $ver = '1.8';
240
  }
241
 
242
- if ( ( $combined !== '' ) && ( ! in_array( $combined, $script->deps ) ) ) {
243
- // if this script has been combined into another script
244
- // ensure this handle depends on the combined handle
245
- $script->deps[] = $combined;
 
 
 
 
 
 
246
  }
247
 
248
  // if $lib is empty, then this script does not need to be
@@ -250,19 +278,74 @@ if ( !class_exists( 'JCP_UseGoogleLibraries' ) ) {
250
  // it around for dependencies
251
  if ( $lib != '' ) {
252
  // build new URL
253
- $script->src = "http://ajax.googleapis.com/ajax/libs/$lib/$ver/$js.js";
254
-
255
- if ( $this->is_ssl ) {
256
- //use ssl
257
- $script->src = preg_replace( '/^http:/', 'https:', $script->src );
258
  }
 
259
  } else {
260
  $script->src = "";
261
  }
262
  $newscripts[] = $script;
 
263
  }
264
  }
 
265
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
266
  foreach ( $newscripts as $script ) {
267
  $olddata = $this->WP_Dependency_get_data( $scripts, $script->handle );
268
  $scripts->remove( $script->handle );
@@ -273,7 +356,6 @@ if ( !class_exists( 'JCP_UseGoogleLibraries' ) ) {
273
  $scripts->add_data( $script->handle, $data_name, $data );
274
  }
275
  }
276
-
277
  }
278
 
279
 
2
  /*
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.5b1
7
  Author: Jason Penney
8
  Author URI: http://jasonpenney.net/
9
  */
40
  class JCP_UseGoogleLibraries {
41
 
42
  private static $instance;
43
+ private static $version = '1.5b1';
44
  public static function get_instance() {
45
  if ( !isset( self::$instance ) ) {
46
  self::$instance = new JCP_UseGoogleLibraries();
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 = 43200; // 12 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
  */
176
  // that.
177
  global $wp_scripts;
178
  if ( is_a( $wp_scripts, 'WP_Scripts' ) ) {
179
+ self::debug( self::$script_before_init_notice );
 
 
 
180
  $ugl = self::get_instance();
181
  $ugl->replace_default_scripts( $wp_scripts );
182
  }
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
  }
189
 
190
  static function setup_filter() {
192
  $ugl->setup();
193
  }
194
 
195
+ /**
196
+ * Log message if `WP_DEBUG` enabled.
197
+ *
198
+ * @since 1.5
199
+ *
200
+ * @param mixed $message string to log, or object to log via `print_r`
201
+ */
202
+ static function debug( $message ) {
203
+ if ( WP_DEBUG !== false ) {
204
+ if ( is_array( $message ) || is_object( $message ) ) {
205
+ $message = print_r( $message, true );
206
+ }
207
+ error_log( "Use Google Libraries: " . $message );
208
+ }
209
+ }
210
+
211
  /**
212
  * Disables script concatination, which breaks when dependencies are not
213
  * all loaded locally.
225
  }
226
 
227
  /**
228
+ * Collects replacement script registration data.
 
229
  *
230
+ * Processes standard WordPress script registrations against list of
231
+ * scripts hosted on Google's CDN. Will exclude any scripts that
232
+ * contain '-' in the version number (used by WordPress devs to signify
233
+ * a non-standard version). Also, the new url will be queried to ensure
234
+ * it's valid (via `wp_remote_head`).
235
+ *
236
+ * @since 1.5
237
+ *
238
+ * @param object $scripts WP_Scripts object
239
+ * @return array updated script registration data
240
  */
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];
249
 
250
  // default to requested ver
251
  $ver = $script->ver;
252
+
253
  if ( strpos( $ver, '-' ) !== false ) {
254
+ self::debug( "WordPress appears to be requesting a non-standard version of $name (version $ver). Using version provided by WordPress to ensure compatability." );
 
 
255
  continue;
256
  }
257
 
261
  $ver = '1.8';
262
  }
263
 
264
+ if ( $combined !== '' ) {
265
+ if ( ! in_array( $combined, $combine_ok ) ) {
266
+ self::debug( "Google servers not hosting combined library for $name (version $ver). Using version provided by WordPress to ensure compatability." );
267
+ continue;
268
+ }
269
+ if ( ! in_array( $combined, $script->deps ) ) {
270
+ // if this script has been combined into another script
271
+ // ensure this handle depends on the combined handle
272
+ $script->deps[] = $combined;
273
+ }
274
  }
275
 
276
  // if $lib is empty, then this script does not need to be
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 = "";
289
  }
290
  $newscripts[] = $script;
291
+ $combine_ok[] = $name;
292
  }
293
  }
294
+ return $newscripts;
295
 
296
+ }
297
+
298
+
299
+ /**
300
+ * Get new script registration data.
301
+ *
302
+ * Attempts to load script registration data from the transient cache.
303
+ * If not in cache, or if cached data is from a different version of
304
+ * either WordPress or this plug-in, then it will be rebuilt. Also
305
+ * handles forcing URLS to use SSL if site is currently loaded over
306
+ * SSL.
307
+ *
308
+ * @since 1.5
309
+ *
310
+ * @param object $scripts WP_Scripts object
311
+ * @return array updated script registration data
312
+ */
313
+ function get_newscripts( &$scripts ) {
314
+ $wp_ver = get_bloginfo( 'version' );
315
+ if ( false === ( $cache = get_transient( self::$cache_id ) ) ) {
316
+ $cache = array();
317
+ }
318
+ if ( ( ! isset( $cache['ugl_ver'] ) ) || ( $cache['ugl_ver'] !== self::$version ) ||
319
+ ( ! isset( $cache['wp_ver'] ) ) || ( $cache['wp_ver'] !== $wp_ver ) ||
320
+ ( ! isset( $cache['newscripts'] ) ) ) {
321
+ $newscripts = $this->build_newscripts( $scripts );
322
+ $cache = array(
323
+ 'ugl_ver' => self::$version,
324
+ 'wp_ver' => $wp_ver,
325
+ 'newscripts' => $newscripts,
326
+ );
327
+ set_transient( self::$cache_id, $cache, self::$cache_len );
328
+ } else {
329
+ $newscripts = $cache['newscripts'];
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
+ }
337
+ }
338
+ return $newscripts;
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
+ */
347
+ function replace_default_scripts( &$scripts ) {
348
+ $newscripts = $this->get_newscripts( $scripts );
349
  foreach ( $newscripts as $script ) {
350
  $olddata = $this->WP_Dependency_get_data( $scripts, $script->handle );
351
  $scripts->remove( $script->handle );
356
  $scripts->add_data( $script->handle, $data_name, $data );
357
  }
358
  }
 
359
  }
360
 
361