[ Index ] |
|
Code source de WordPress 2.1.2 |
1 <?php 2 class WP_Scripts { 3 var $scripts = array(); 4 var $queue = array(); 5 var $printed = array(); 6 var $args = array(); 7 8 function WP_Scripts() { 9 $this->default_scripts(); 10 } 11 12 function default_scripts() { 13 $this->add( 'dbx', '/wp-includes/js/dbx.js', false, '2.05' ); 14 $this->add( 'fat', '/wp-includes/js/fat.js', false, '1.0-RC1_3660' ); 15 $this->add( 'sack', '/wp-includes/js/tw-sack.js', false, '1.6.1' ); 16 $this->add( 'quicktags', '/wp-includes/js/quicktags.js', false, '3517' ); 17 $this->add( 'colorpicker', '/wp-includes/js/colorpicker.js', false, '3517' ); 18 $this->add( 'tiny_mce', '/wp-includes/js/tinymce/tiny_mce_gzip.php', false, '20061113' ); 19 $mce_config = apply_filters('tiny_mce_config_url', '/wp-includes/js/tinymce/tiny_mce_config.php'); 20 $this->add( 'wp_tiny_mce', $mce_config, array('tiny_mce'), '20070225' ); 21 $this->add( 'prototype', '/wp-includes/js/prototype.js', false, '1.5.0'); 22 $this->add( 'autosave', '/wp-includes/js/autosave-js.php', array('prototype', 'sack'), '20070116'); 23 $this->add( 'wp-ajax', '/wp-includes/js/wp-ajax-js.php', array('prototype'), '20070118'); 24 $this->add( 'listman', '/wp-includes/js/list-manipulation-js.php', array('wp-ajax', 'fat'), '20070118'); 25 $this->add( 'scriptaculous-root', '/wp-includes/js/scriptaculous/wp-scriptaculous.js', array('prototype'), '1.6.1'); 26 $this->add( 'scriptaculous-builder', '/wp-includes/js/scriptaculous/builder.js', array('scriptaculous-root'), '1.6.1'); 27 $this->add( 'scriptaculous-dragdrop', '/wp-includes/js/scriptaculous/dragdrop.js', array('scriptaculous-builder'), '1.6.1'); 28 $this->add( 'scriptaculous-effects', '/wp-includes/js/scriptaculous/effects.js', array('scriptaculous-root'), '1.6.1'); 29 $this->add( 'scriptaculous-slider', '/wp-includes/js/scriptaculous/slider.js', array('scriptaculous-effects'), '1.6.1'); 30 $this->add( 'scriptaculous-controls', '/wp-includes/js/scriptaculous/controls.js', array('scriptaculous-root'), '1.6.1'); 31 $this->add( 'scriptaculous', '', array('scriptaculous-dragdrop', 'scriptaculous-slider', 'scriptaculous-controls'), '1.6.1'); 32 $this->add( 'cropper', '/wp-includes/js/crop/cropper.js', array('scriptaculous-dragdrop'), '20070118'); 33 if ( is_admin() ) { 34 $this->add( 'dbx-admin-key', '/wp-admin/dbx-admin-key-js.php', array('dbx'), '3651' ); 35 $this->add( 'ajaxcat', '/wp-admin/cat-js.php', array('listman'), '20070118' ); 36 $this->add( 'admin-categories', '/wp-admin/categories.js', array('listman'), '3684' ); 37 $this->add( 'admin-custom-fields', '/wp-admin/custom-fields.js', array('listman'), '3733' ); 38 $this->add( 'admin-comments', '/wp-admin/edit-comments.js', array('listman'), '3847' ); 39 $this->add( 'admin-users', '/wp-admin/users.js', array('listman'), '4583' ); 40 $this->add( 'xfn', '/wp-admin/xfn.js', false, '3517' ); 41 $this->add( 'upload', '/wp-admin/upload-js.php', array('prototype'), '20070118' ); 42 } 43 } 44 45 /** 46 * Prints script tags 47 * 48 * Prints the scripts passed to it or the print queue. Also prints all necessary dependencies. 49 * 50 * @param mixed handles (optional) Scripts to be printed. (void) prints queue, (string) prints that script, (array of strings) prints those scripts. 51 * @return array Scripts that have been printed 52 */ 53 function print_scripts( $handles = false ) { 54 // Print the queue if nothing is passed. If a string is passed, print that script. If an array is passed, print those scripts. 55 $handles = false === $handles ? $this->queue : (array) $handles; 56 $handles = $this->all_deps( $handles ); 57 $this->_print_scripts( $handles ); 58 return $this->printed; 59 } 60 61 /** 62 * Internally used helper function for printing script tags 63 * 64 * @param array handles Hierarchical array of scripts to be printed 65 * @see WP_Scripts::all_deps() 66 */ 67 function _print_scripts( $handles ) { 68 global $wp_db_version; 69 70 foreach( array_keys($handles) as $handle ) { 71 if ( !$handles[$handle] ) 72 return; 73 elseif ( is_array($handles[$handle]) ) 74 $this->_print_scripts( $handles[$handle] ); 75 if ( !in_array($handle, $this->printed) && isset($this->scripts[$handle]) ) { 76 if ( $this->scripts[$handle]->src ) { // Else it defines a group. 77 $ver = $this->scripts[$handle]->ver ? $this->scripts[$handle]->ver : $wp_db_version; 78 if ( isset($this->args[$handle]) ) 79 $ver .= '&' . $this->args[$handle]; 80 $src = 0 === strpos($this->scripts[$handle]->src, 'http://') ? $this->scripts[$handle]->src : get_option( 'siteurl' ) . $this->scripts[$handle]->src; 81 $src = add_query_arg('ver', $ver, $src); 82 echo "<script type='text/javascript' src='$src'></script>\n"; 83 } 84 $this->printed[] = $handle; 85 } 86 } 87 } 88 89 90 /** 91 * Determines dependencies of scripts 92 * 93 * Recursively builds hierarchical array of script dependencies. Does NOT catch infinite loops. 94 * 95 * @param mixed handles Accepts (string) script name or (array of strings) script names 96 * @param bool recursion Used internally when function calls itself 97 * @return array Hierarchical array of dependencies 98 */ 99 function all_deps( $handles, $recursion = false ) { 100 if ( ! $handles = (array) $handles ) 101 return array(); 102 $return = array(); 103 foreach ( $handles as $handle ) { 104 $handle = explode('?', $handle); 105 if ( isset($handle[1]) ) 106 $this->args[$handle[0]] = $handle[1]; 107 $handle = $handle[0]; 108 if ( is_null($return[$handle]) ) // Prime the return array with $handles 109 $return[$handle] = true; 110 if ( $this->scripts[$handle]->deps ) { 111 if ( false !== $return[$handle] && array_diff($this->scripts[$handle]->deps, array_keys($this->scripts)) ) 112 $return[$handle] = false; // Script required deps which don't exist 113 else 114 $return[$handle] = $this->all_deps( $this->scripts[$handle]->deps, true ); // Build the hierarchy 115 } 116 if ( $recursion && false === $return[$handle] ) 117 return false; // Cut the branch 118 } 119 return $return; 120 } 121 122 /** 123 * Adds script 124 * 125 * Adds the script only if no script of that name already exists 126 * 127 * @param string handle Script name 128 * @param string src Script url 129 * @param array deps (optional) Array of script names on which this script depends 130 * @param string ver (optional) Script version (used for cache busting) 131 * @return array Hierarchical array of dependencies 132 */ 133 function add( $handle, $src, $deps = array(), $ver = false ) { 134 if ( isset($this->scripts[$handle]) ) 135 return false; 136 $this->scripts[$handle] = new _WP_Script( $handle, $src, $deps, $ver ); 137 return true; 138 } 139 140 function remove( $handles ) { 141 foreach ( (array) $handles as $handle ) 142 unset($this->scripts[$handle]); 143 } 144 145 function enqueue( $handles ) { 146 foreach ( (array) $handles as $handle ) { 147 $handle = explode('?', $handle); 148 if ( !in_array($handle[0], $this->queue) && isset($this->scripts[$handle[0]]) ) { 149 $this->queue[] = $handle[0]; 150 if ( isset($handle[1]) ) 151 $this->args[$handle[0]] = $handle[1]; 152 } 153 } 154 } 155 156 function dequeue( $handles ) { 157 foreach ( (array) $handles as $handle ) 158 unset( $this->queue[$handle] ); 159 } 160 161 function query( $handle, $list = 'scripts' ) { // scripts, queue, or printed 162 switch ( $list ) : 163 case 'scripts': 164 if ( isset($this->scripts[$handle]) ) 165 return $this->scripts[$handle]; 166 break; 167 default: 168 if ( in_array($handle, $this->$list) ) 169 return true; 170 break; 171 endswitch; 172 return false; 173 } 174 175 } 176 177 class _WP_Script { 178 var $handle; 179 var $src; 180 var $deps = array(); 181 var $ver = false; 182 var $args = false; 183 184 function _WP_Script() { 185 @list($this->handle, $this->src, $this->deps, $this->ver) = func_get_args(); 186 if ( !is_array($this->deps) ) 187 $this->deps = array(); 188 if ( !$this->ver ) 189 $this->ver = false; 190 } 191 } 192 193 /** 194 * Prints script tags in document head 195 * 196 * Called by admin-header.php and by wp_head hook. Since it is called by wp_head on every page load, 197 * the function does not instantiate the WP_Scripts object unless script names are explicitly passed. 198 * Does make use of already instantiated $wp_scripts if present. 199 * Use provided wp_print_scripts hook to register/enqueue new scripts. 200 * 201 * @see WP_Scripts::print_scripts() 202 */ 203 function wp_print_scripts( $handles = false ) { 204 do_action( 'wp_print_scripts' ); 205 if ( '' === $handles ) // for wp_head 206 $handles = false; 207 208 global $wp_scripts; 209 if ( !is_a($wp_scripts, 'WP_Scripts') ) { 210 if ( !$handles ) 211 return array(); // No need to instantiate if nothing's there. 212 else 213 $wp_scripts = new WP_Scripts(); 214 } 215 216 return $wp_scripts->print_scripts( $handles ); 217 } 218 219 function wp_register_script( $handle, $src, $deps = array(), $ver = false ) { 220 global $wp_scripts; 221 if ( !is_a($wp_scripts, 'WP_Scripts') ) 222 $wp_scripts = new WP_Scripts(); 223 224 $wp_scripts->add( $handle, $src, $deps, $ver ); 225 } 226 227 function wp_deregister_script( $handle ) { 228 global $wp_scripts; 229 if ( !is_a($wp_scripts, 'WP_Scripts') ) 230 $wp_scripts = new WP_Scripts(); 231 232 $wp_scripts->remove( $handle ); 233 } 234 235 /** 236 * Equeues script 237 * 238 * Registers the script if src provided (does NOT overwrite) and enqueues. 239 * 240 * @see WP_Script::add(), WP_Script::enqueue() 241 */ 242 function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false ) { 243 global $wp_scripts; 244 if ( !is_a($wp_scripts, 'WP_Scripts') ) 245 $wp_scripts = new WP_Scripts(); 246 247 if ( $src ) { 248 $_handle = explode('?', $handle); 249 $wp_scripts->add( $_handle[0], $src, $deps, $ver ); 250 } 251 $wp_scripts->enqueue( $handle ); 252 } 253 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 30 19:41:27 2007 | par Balluche grâce à PHPXref 0.7 |