Plugin Directory

Changeset 716899


Ignore:
Timestamp:
05/22/2013 10:11:28 PM (13 years ago)
Author:
jczorkmid
Message:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • use-google-libraries/trunk/use-google-libraries.php

    r715027 r716899  
    44  Plugin URI: http://jasonpenney.net/wordpress-plugins/use-google-libraries/
    55  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
     6  Version: 1.5.2
    77  Author: Jason Penney
    88  Author URI: http://jasonpenney.net/
     
    2929if ( !class_exists( 'JCP_UseGoogleLibraries' ) ) {
    3030
    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 
    4031    class JCP_UseGoogleLibraries {
    4132
    4233        private static $instance;
    43         private static $version = '1.5';
     34        private static $version = '1.5.2';
    4435        public static function get_instance() {
    4536            if ( !isset( self::$instance ) ) {
     
    4839            return self::$instance;
    4940        }
    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         */
    5164        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         */
    5372        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         */
    5598        protected static $cache_id = 'JCP_UseGoogleLibraries_cache';
     99       
     100        /**
     101         * transient expiration
     102         *
     103         * @var int
     104         */
    56105        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         */
    57112        protected static $script_before_init_notice =
    58113            'Another plugin has registered or enqued a script before the "init" action.  Attempting to work around it.';
     114       
    59115        /**
    60116         * PHP 4 Compatible Constructor
     
    66122         */
    67123        function __construct() {
     124            $this->jquery_tag = 'jquery';
    68125            $this->google_scripts =
    69126                array(
    70127                // any extra scripts listed here not provided by WordPress
    71                 // or another plugin will not be registered.  This liste
     128                // or another plugin will not be registered.  This list
    72129                // is just used to chancge where things load from.
    73130
     
    140197
    141198            );
    142             $this->noconflict_url = WP_PLUGIN_URL . '/use-google-libraries/js/jQnc.js';
    143 
    144199            $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', '>=' );
    161203        }
    162204
     
    183225        }
    184226
    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         */
    186231        static function script_before_init_admin_notice() {
    187232            echo '<div class="error fade"><p>Use Google Libraries: ' . self::$script_before_init_notice . '</p></div>';
     
    242287            $newscripts = array();
    243288            $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           
    244298            foreach ( $this->google_scripts as $name => $values ) {
    245299                if ( $script = $scripts->query( $name ) ) {
     
    247301                    $js = $values[1];
    248302                    $combined = $values[2];
    249 
    250303                    // default to requested ver
    251304                    $ver = $script->ver;
     
    279332                    if ( $lib != '' ) {
    280333                        // 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 ) {
    283336                            self::debug( "Google servers do not seem to be hosting requested version of $name (version $ver). Using version provided by WordPress." );
    284337                            continue;
     338                        }
     339                        if ( ! $this->protocol_relative_supported ) {
     340                            $url = "http:$url";
    285341                        }
    286342                        $script->src = $url;
     
    331387            // need to handle ssl after cache load, because it may swap
    332388            // back and forth depending on the site config/usage
    333             if ( $this->is_ssl === true ) {
     389            if ( ( ! $this->protocol_relative_supported ) && ( is_ssl() ) ) {
    334390                foreach ( $newscripts as $script ) {
    335391                    $script->src = preg_replace( '/^http:/', 'https:', $script->src );
     
    340396
    341397        /**
    342          * Replace as many of the wordpress default script registrations as
    343          * possible with ones from google
     398         * Replace as many of the WordPress default script registrations as
     399         * possible with ones from Google
    344400         *
    345401         * @param object  $scripts WP_Scripts object.
     
    352408                // re-register with original ver
    353409                $scripts->add( $script->handle, $script->src, $script->deps, $script->ver );
    354                 if ( $olddata )
     410                if ( $olddata ) {
    355411                    foreach ( $olddata as $data_name => $data ) {
    356412                        $scripts->add_data( $script->handle, $data_name, $data );
    357413                    }
     414                }
    358415            }
    359416        }
     
    371428                return $dep_obj->registered[$handle]->extra;
    372429
    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 );
    374434        }
    375435
     
    389449            if ( preg_match( '/ajax\.googleapis\.com\//', $src ) ) {
    390450                $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" ) ) {
    392452                    $this->noconflict_next = TRUE;
    393453                }
Note: See TracChangeset for help on using the changeset viewer.