[wp-trac] [WordPress Trac] #16343: API for WP_Scripts: allow async loading, version checks

WordPress Trac wp-trac at lists.automattic.com
Mon Jan 24 00:57:11 UTC 2011


#16343: API for WP_Scripts: allow async loading, version checks
-------------------------------------------------+-------------------------
 Reporter:  jltallon                             |       Owner:
     Type:  enhancement                          |      Status:  assigned
 Priority:  normal                               |   Milestone:  Awaiting
Component:  Performance                          |  Review
 Severity:  minor                                |     Version:
 Keywords:  WP_Scripts, async-load, cache-       |  Resolution:
  friendliness                                   |
-------------------------------------------------+-------------------------

Comment (by jltallon):

 public static function scriptSrc($handle)
 {
   global $wp_scripts;
         if ( !is_a($wp_scripts, 'WP_Scripts') )
         {
                 $wp_scripts = new WP_Scripts();
                 return 'ERROR';
         }

         if ( !isset($wp_scripts->registered[$handle]) )
                 return '';      // script not registered

         $item =& $wp_scripts->registered[$handle];
         $wps =& $wp_scripts;

         // Code from WP_Scripts
         if ( null === $item->ver )
           $ver = '';
         else
           $ver = $item->ver ? $item->ver : $wps->default_version;

         if ( isset($wps->args[$handle]) )
           $ver = $ver ? $ver.'&'.$wps->args[$handle] :
 $wps->args[$handle];

         $src = $item->src;
         if ( !preg_match('|^https?://|', $src) && ! ( $wps->content_url &&
 0 === strpos($src, $wps->content_url) ) )
           $src = $wps->base_url . $src;

         //if ( !empty($ver) )
         //  $src = add_query_arg('ver', $ver, $src);

         //$src = esc_url(apply_filters( 'script_loader_src', $src, $handle
 ));

         return "{$src}?$ver";
 }

 public static function scriptVer($handle)
     {
      global $wp_scripts;
         if ( !is_a($wp_scripts, 'WP_Scripts') )
         {
                 $wp_scripts = new WP_Scripts();
                 return 'ERROR';
         }

         if ( !isset($wp_scripts->registered[$handle]) )
                 return false;   // script not registered

         $item =& $wp_scripts->registered[$handle];
         if ( null === $item->ver )
                 $ver = '';
         else
                 $ver = $item->ver ? $item->ver :
 $wp_scripts->default_version;

         return $ver;
     }

 (these would be much better as members of the WP_Scripts class -- I'm only
 abusing the fact that PHP4-style classes lack any protection for their
 members whatsoever)

 Use case for #2:  "hotlink" *precisely the same* version that WP would
 have used, in order to ensure compatibility (and also to allow plugins
 check for version mismatches)
 For example, I have a plugin which loads the components of jQueryUI which
 are not bundled with WP, for use by other components (plugins and theme).
 When a certain (or more recent) version of a script is required, plugins
 can check for potential conflicts.
 e.g.: WP 3.0's admin scripts don't play well with the "stock" version of
 jQuery 1.4.2 / jQueryUI 1.8.6. With WP 3.1rc, however, the bundled version
 of jQuery is perfectly fine.

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/16343#comment:4>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list