[ Index ]

WordPress Cross Reference

title

Body

[close]

/wp-includes/ -> functions.wp-scripts.php (source)

   1  <?php
   2  /**
   3   * BackPress Scripts Procedural API
   4   *
   5   * @since 2.6.0
   6   *
   7   * @package WordPress
   8   * @subpackage BackPress
   9   */
  10  
  11  /**
  12   * Print scripts in document head that are in the $handles queue.
  13   *
  14   * Called by admin-header.php and wp_head hook. Since it is called by wp_head on every page load,
  15   * the function does not instantiate the WP_Scripts object unless script names are explicitly passed.
  16   * Makes use of already-instantiated $wp_scripts global if present. Use provided wp_print_scripts
  17   * hook to register/enqueue new scripts.
  18   *
  19   * @see WP_Scripts::do_items()
  20   * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
  21   *
  22   * @since 2.6.0
  23   *
  24   * @param array|bool $handles Optional. Scripts to be printed. Default 'false'.
  25   * @return array On success, a processed array of WP_Dependencies items; otherwise, an empty array.
  26   */
  27  function wp_print_scripts( $handles = false ) {
  28      /**
  29       * Fires before scripts in the $handles queue are printed.
  30       *
  31       * @since 2.1.0
  32       */
  33      do_action( 'wp_print_scripts' );
  34      if ( '' === $handles ) // for wp_head
  35          $handles = false;
  36  
  37      global $wp_scripts;
  38      if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
  39          if ( ! did_action( 'init' ) )
  40              _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
  41                  '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
  42  
  43          if ( !$handles )
  44              return array(); // No need to instantiate if nothing is there.
  45          else
  46              $wp_scripts = new WP_Scripts();
  47      }
  48  
  49      return $wp_scripts->do_items( $handles );
  50  }
  51  
  52  /**
  53   * Register a new script.
  54   *
  55   * Registers a script to be linked later using the wp_enqueue_script() function.
  56   *
  57   * @see WP_Dependencies::add(), WP_Dependencies::add_data()
  58   * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
  59   *
  60   * @since 2.6.0
  61   *
  62   * @param string      $handle    Name of the script. Should be unique.
  63   * @param string      $src       Path to the script from the WordPress root directory. Example: '/js/myscript.js'.
  64   * @param array       $deps      Optional. An array of registered script handles this script depends on. Set to false if there
  65   *                               are no dependencies. Default empty array.
  66   * @param string|bool $ver       Optional. String specifying script version number, if it has one, which is concatenated
  67   *                               to end of path as a query string. If no version is specified or set to false, a version
  68   *                               number is automatically added equal to current installed WordPress version.
  69   *                               If set to null, no version is added. Default 'false'. Accepts 'false', 'null', or 'string'.
  70   * @param bool        $in_footer Optional. Whether to enqueue the script before </head> or before </body>.
  71   *                               Default 'false'. Accepts 'false' or 'true'.
  72   */
  73  function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
  74      global $wp_scripts;
  75      if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
  76          if ( ! did_action( 'init' ) )
  77              _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
  78                  '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
  79          $wp_scripts = new WP_Scripts();
  80      }
  81  
  82      $wp_scripts->add( $handle, $src, $deps, $ver );
  83      if ( $in_footer )
  84          $wp_scripts->add_data( $handle, 'group', 1 );
  85  }
  86  
  87  /**
  88   * Localize a script.
  89   *
  90   * Works only if the script has already been added.
  91   *
  92   * Accepts an associative array $l10n and creates a JavaScript object:
  93   * <code>
  94   * "$object_name" = {
  95   *       key: value,
  96   *       key: value,
  97   *       ...
  98   * }
  99   * </code>
 100   *
 101   * @see WP_Dependencies::localize()
 102   * @link http://core.trac.wordpress.org/ticket/11520
 103   * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
 104   *
 105   * @since 2.6.0
 106   *
 107   * @param string $handle      Script handle the data will be attached to.
 108   * @param string $object_name Name for the JavaScript object. Passed directly, so it should be qualified JS variable.
 109   *                            Example: '/[a-zA-Z0-9_]+/'.
 110   * @param array $l10n         The data itself. The data can be either a single or multi-dimensional array.
 111   * @return bool True if the script was successfully localized, false otherwise.
 112   */
 113  function wp_localize_script( $handle, $object_name, $l10n ) {
 114      global $wp_scripts;
 115      if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
 116          if ( ! did_action( 'init' ) )
 117              _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
 118                  '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
 119  
 120          return false;
 121      }
 122  
 123      return $wp_scripts->localize( $handle, $object_name, $l10n );
 124  }
 125  
 126  /**
 127   * Remove a registered script.
 128   *
 129   * Note: there are intentional safeguards in place to prevent critical admin scripts,
 130   * such as jQuery core, from being unregistered.
 131   *
 132   * @see WP_Dependencies::remove()
 133   * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
 134   *
 135   * @since 2.6.0
 136   *
 137   * @param string $handle Name of the script to be removed.
 138   */
 139  function wp_deregister_script( $handle ) {
 140      global $wp_scripts;
 141      if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
 142          if ( ! did_action( 'init' ) )
 143              _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
 144                  '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
 145          $wp_scripts = new WP_Scripts();
 146      }
 147  
 148      /**
 149       * Do not allow accidental or negligent de-registering of critical scripts in the admin.
 150       * Show minimal remorse if the correct hook is used.
 151       */
 152      $current_filter = current_filter();
 153      if ( ( is_admin() && 'admin_enqueue_scripts' !== $current_filter ) ||
 154          ( 'wp-login.php' === $GLOBALS['pagenow'] && 'login_enqueue_scripts' !== $current_filter )
 155      ) {
 156          $no = array(
 157              'jquery', 'jquery-core', 'jquery-migrate', 'jquery-ui-core', 'jquery-ui-accordion',
 158              'jquery-ui-autocomplete', 'jquery-ui-button', 'jquery-ui-datepicker', 'jquery-ui-dialog',
 159              'jquery-ui-draggable', 'jquery-ui-droppable', 'jquery-ui-menu', 'jquery-ui-mouse',
 160              'jquery-ui-position', 'jquery-ui-progressbar', 'jquery-ui-resizable', 'jquery-ui-selectable',
 161              'jquery-ui-slider', 'jquery-ui-sortable', 'jquery-ui-spinner', 'jquery-ui-tabs',
 162              'jquery-ui-tooltip', 'jquery-ui-widget', 'underscore', 'backbone',
 163          );
 164  
 165          if ( in_array( $handle, $no ) ) {
 166              $message = sprintf( __( 'Do not deregister the %1$s script in the administration area. To target the frontend theme, use the %2$s hook.' ),
 167                  "<code>$handle</code>", '<code>wp_enqueue_scripts</code>' );
 168              _doing_it_wrong( __FUNCTION__, $message, '3.6' );
 169              return;
 170          }
 171      }
 172  
 173      $wp_scripts->remove( $handle );
 174  }
 175  
 176  /**
 177   * Enqueue a script.
 178   *
 179   * Registers the script if $src provided (does NOT overwrite), and enqueues it.
 180   *
 181   * @see WP_Dependencies::add(), WP_Dependencies::add_data(), WP_Dependencies::enqueue()
 182   * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
 183   *
 184   * @since 2.6.0
 185  
 186   * @param string      $handle    Name of the script.
 187   * @param string|bool $src       Path to the script from the root directory of WordPress. Example: '/js/myscript.js'.
 188   * @param array       $deps      An array of registered handles this script depends on. Default empty array.
 189   * @param string|bool $ver       Optional. String specifying the script version number, if it has one. This parameter
 190   *                               is used to ensure that the correct version is sent to the client regardless of caching,
 191   *                               and so should be included if a version number is available and makes sense for the script.
 192   * @param bool        $in_footer Optional. Whether to enqueue the script before </head> or before </body>.
 193   *                               Default 'false'. Accepts 'false' or 'true'.
 194   */
 195  function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false, $in_footer = false ) {
 196      global $wp_scripts;
 197      if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
 198          if ( ! did_action( 'init' ) )
 199              _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
 200                  '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
 201          $wp_scripts = new WP_Scripts();
 202      }
 203  
 204      if ( $src ) {
 205          $_handle = explode('?', $handle);
 206          $wp_scripts->add( $_handle[0], $src, $deps, $ver );
 207          if ( $in_footer )
 208              $wp_scripts->add_data( $_handle[0], 'group', 1 );
 209      }
 210      $wp_scripts->enqueue( $handle );
 211  }
 212  
 213  /**
 214   * Remove a previously enqueued script.
 215   *
 216   * @see WP_Dependencies::dequeue()
 217   * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
 218   *
 219   * @since 3.1.0
 220   *
 221   * @param string $handle Name of the script to be removed.
 222   */
 223  function wp_dequeue_script( $handle ) {
 224      global $wp_scripts;
 225      if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
 226          if ( ! did_action( 'init' ) )
 227              _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
 228                  '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
 229          $wp_scripts = new WP_Scripts();
 230      }
 231  
 232      $wp_scripts->dequeue( $handle );
 233  }
 234  
 235  /**
 236   * Check whether a script has been added to the queue.
 237   *
 238   * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
 239   *
 240   * @since 2.8.0
 241   * @since 3.5.0 'enqueued' added as an alias of the 'queue' list.
 242   *
 243   * @param string $handle Name of the script.
 244   * @param string $list   Optional. Status of the script to check. Default 'enqueued'.
 245   *                       Accepts 'enqueued', 'registered', 'queue', 'to_do', and 'done'.
 246   * @return bool Whether the script script is queued.
 247   */
 248  function wp_script_is( $handle, $list = 'enqueued' ) {
 249      global $wp_scripts;
 250      if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
 251          if ( ! did_action( 'init' ) )
 252              _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
 253                  '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
 254          $wp_scripts = new WP_Scripts();
 255      }
 256  
 257      return (bool) $wp_scripts->query( $handle, $list );
 258  }


Generated: Tue Mar 25 01:41:18 2014 WordPress honlapkészítés: online1.hu