[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Dependencies API: Scripts functions
   4   *
   5   * @since 2.6.0
   6   *
   7   * @package WordPress
   8   * @subpackage Dependencies
   9   */
  10  
  11  /**
  12   * Initialize $wp_scripts if it has not been set.
  13   *
  14   * @global WP_Scripts $wp_scripts
  15   *
  16   * @since 4.2.0
  17   *
  18   * @return WP_Scripts WP_Scripts instance.
  19   */
  20  function wp_scripts() {
  21      global $wp_scripts;
  22      if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
  23          $wp_scripts = new WP_Scripts();
  24      }
  25      return $wp_scripts;
  26  }
  27  
  28  /**
  29   * Helper function to output a _doing_it_wrong message when applicable.
  30   *
  31   * @ignore
  32   * @since 4.2.0
  33   *
  34   * @param string $function Function name.
  35   */
  36  function _wp_scripts_maybe_doing_it_wrong( $function ) {
  37      if ( did_action( 'init' ) || did_action( 'admin_enqueue_scripts' ) || did_action( 'wp_enqueue_scripts' ) || did_action( 'login_enqueue_scripts' ) ) {
  38          return;
  39      }
  40  
  41      _doing_it_wrong(
  42          $function,
  43          sprintf(
  44              /* translators: 1: wp_enqueue_scripts, 2: admin_enqueue_scripts, 3: login_enqueue_scripts */
  45              __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
  46              '<code>wp_enqueue_scripts</code>',
  47              '<code>admin_enqueue_scripts</code>',
  48              '<code>login_enqueue_scripts</code>'
  49          ),
  50          '3.3.0'
  51      );
  52  }
  53  
  54  /**
  55   * Prints scripts in document head that are in the $handles queue.
  56   *
  57   * Called by admin-header.php and {@see 'wp_head'} hook. Since it is called by wp_head on every page load,
  58   * the function does not instantiate the WP_Scripts object unless script names are explicitly passed.
  59   * Makes use of already-instantiated $wp_scripts global if present. Use provided {@see 'wp_print_scripts'}
  60   * hook to register/enqueue new scripts.
  61   *
  62   * @see WP_Scripts::do_items()
  63   * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
  64   *
  65   * @since 2.1.0
  66   *
  67   * @param string|bool|array $handles Optional. Scripts to be printed. Default 'false'.
  68   * @return array On success, a processed array of WP_Dependencies items; otherwise, an empty array.
  69   */
  70  function wp_print_scripts( $handles = false ) {
  71      /**
  72       * Fires before scripts in the $handles queue are printed.
  73       *
  74       * @since 2.1.0
  75       */
  76      do_action( 'wp_print_scripts' );
  77      if ( '' === $handles ) { // for wp_head
  78          $handles = false;
  79      }
  80  
  81      _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
  82  
  83      global $wp_scripts;
  84      if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
  85          if ( ! $handles ) {
  86              return array(); // No need to instantiate if nothing is there.
  87          }
  88      }
  89  
  90      return wp_scripts()->do_items( $handles );
  91  }
  92  
  93  /**
  94   * Adds extra code to a registered script.
  95   *
  96   * Code will only be added if the script is already in the queue.
  97   * Accepts a string $data containing the Code. If two or more code blocks
  98   * are added to the same script $handle, they will be printed in the order
  99   * they were added, i.e. the latter added code can redeclare the previous.
 100   *
 101   * @since 4.5.0
 102   *
 103   * @see WP_Scripts::add_inline_script()
 104   *
 105   * @param string $handle   Name of the script to add the inline script to.
 106   * @param string $data     String containing the javascript to be added.
 107   * @param string $position Optional. Whether to add the inline script before the handle
 108   *                         or after. Default 'after'.
 109   * @return bool True on success, false on failure.
 110   */
 111  function wp_add_inline_script( $handle, $data, $position = 'after' ) {
 112      _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
 113  
 114      if ( false !== stripos( $data, '</script>' ) ) {
 115          _doing_it_wrong(
 116              __FUNCTION__,
 117              sprintf(
 118                  /* translators: 1: <script>, 2: wp_add_inline_script() */
 119                  __( 'Do not pass %1$s tags to %2$s.' ),
 120                  '<code>&lt;script&gt;</code>',
 121                  '<code>wp_add_inline_script()</code>'
 122              ),
 123              '4.5.0'
 124          );
 125          $data = trim( preg_replace( '#<script[^>]*>(.*)</script>#is', '$1', $data ) );
 126      }
 127  
 128      return wp_scripts()->add_inline_script( $handle, $data, $position );
 129  }
 130  
 131  /**
 132   * Register a new script.
 133   *
 134   * Registers a script to be enqueued later using the wp_enqueue_script() function.
 135   *
 136   * @see WP_Dependencies::add()
 137   * @see WP_Dependencies::add_data()
 138   *
 139   * @since 2.1.0
 140   * @since 4.3.0 A return value was added.
 141   *
 142   * @param string           $handle    Name of the script. Should be unique.
 143   * @param string|bool      $src       Full URL of the script, or path of the script relative to the WordPress root directory.
 144   *                                    If source is set to false, script is an alias of other scripts it depends on.
 145   * @param array            $deps      Optional. An array of registered script handles this script depends on. Default empty array.
 146   * @param string|bool|null $ver       Optional. String specifying script version number, if it has one, which is added to the URL
 147   *                                    as a query string for cache busting purposes. If version is set to false, a version
 148   *                                    number is automatically added equal to current installed WordPress version.
 149   *                                    If set to null, no version is added.
 150   * @param bool             $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>.
 151   *                                    Default 'false'.
 152   * @return bool Whether the script has been registered. True on success, false on failure.
 153   */
 154  function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
 155      $wp_scripts = wp_scripts();
 156      _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
 157  
 158      $registered = $wp_scripts->add( $handle, $src, $deps, $ver );
 159      if ( $in_footer ) {
 160          $wp_scripts->add_data( $handle, 'group', 1 );
 161      }
 162  
 163      return $registered;
 164  }
 165  
 166  /**
 167   * Localize a script.
 168   *
 169   * Works only if the script has already been added.
 170   *
 171   * Accepts an associative array $l10n and creates a JavaScript object:
 172   *
 173   *     "$object_name" = {
 174   *         key: value,
 175   *         key: value,
 176   *         ...
 177   *     }
 178   *
 179   * @see WP_Scripts::localize()
 180   * @link https://core.trac.wordpress.org/ticket/11520
 181   * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
 182   *
 183   * @since 2.2.0
 184   *
 185   * @todo Documentation cleanup
 186   *
 187   * @param string $handle      Script handle the data will be attached to.
 188   * @param string $object_name Name for the JavaScript object. Passed directly, so it should be qualified JS variable.
 189   *                            Example: '/[a-zA-Z0-9_]+/'.
 190   * @param array $l10n         The data itself. The data can be either a single or multi-dimensional array.
 191   * @return bool True if the script was successfully localized, false otherwise.
 192   */
 193  function wp_localize_script( $handle, $object_name, $l10n ) {
 194      global $wp_scripts;
 195      if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
 196          _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
 197          return false;
 198      }
 199  
 200      return $wp_scripts->localize( $handle, $object_name, $l10n );
 201  }
 202  
 203  /**
 204   * Sets translated strings for a script.
 205   *
 206   * Works only if the script has already been added.
 207   *
 208   * @see WP_Scripts::set_translations()
 209   * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
 210   *
 211   * @since 5.0.0
 212   * @since 5.1.0 The `$domain` parameter was made optional.
 213   *
 214   * @param string $handle Script handle the textdomain will be attached to.
 215   * @param string $domain Optional. Text domain. Default 'default'.
 216   * @param string $path   Optional. The full file path to the directory containing translation files.
 217   * @return bool True if the text domain was successfully localized, false otherwise.
 218   */
 219  function wp_set_script_translations( $handle, $domain = 'default', $path = null ) {
 220      global $wp_scripts;
 221      if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
 222          _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
 223          return false;
 224      }
 225  
 226      return $wp_scripts->set_translations( $handle, $domain, $path );
 227  }
 228  
 229  /**
 230   * Remove a registered script.
 231   *
 232   * Note: there are intentional safeguards in place to prevent critical admin scripts,
 233   * such as jQuery core, from being unregistered.
 234   *
 235   * @see WP_Dependencies::remove()
 236   *
 237   * @since 2.1.0
 238   *
 239   * @param string $handle Name of the script to be removed.
 240   */
 241  function wp_deregister_script( $handle ) {
 242      _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
 243  
 244      /**
 245       * Do not allow accidental or negligent de-registering of critical scripts in the admin.
 246       * Show minimal remorse if the correct hook is used.
 247       */
 248      $current_filter = current_filter();
 249      if ( ( is_admin() && 'admin_enqueue_scripts' !== $current_filter ) ||
 250          ( 'wp-login.php' === $GLOBALS['pagenow'] && 'login_enqueue_scripts' !== $current_filter )
 251      ) {
 252          $no = array(
 253              'jquery',
 254              'jquery-core',
 255              'jquery-migrate',
 256              'jquery-ui-core',
 257              'jquery-ui-accordion',
 258              'jquery-ui-autocomplete',
 259              'jquery-ui-button',
 260              'jquery-ui-datepicker',
 261              'jquery-ui-dialog',
 262              'jquery-ui-draggable',
 263              'jquery-ui-droppable',
 264              'jquery-ui-menu',
 265              'jquery-ui-mouse',
 266              'jquery-ui-position',
 267              'jquery-ui-progressbar',
 268              'jquery-ui-resizable',
 269              'jquery-ui-selectable',
 270              'jquery-ui-slider',
 271              'jquery-ui-sortable',
 272              'jquery-ui-spinner',
 273              'jquery-ui-tabs',
 274              'jquery-ui-tooltip',
 275              'jquery-ui-widget',
 276              'underscore',
 277              'backbone',
 278          );
 279  
 280          if ( in_array( $handle, $no ) ) {
 281              $message = sprintf(
 282                  /* translators: 1: script name, 2: wp_enqueue_scripts */
 283                  __( 'Do not deregister the %1$s script in the administration area. To target the front-end theme, use the %2$s hook.' ),
 284                  "<code>$handle</code>",
 285                  '<code>wp_enqueue_scripts</code>'
 286              );
 287              _doing_it_wrong( __FUNCTION__, $message, '3.6.0' );
 288              return;
 289          }
 290      }
 291  
 292      wp_scripts()->remove( $handle );
 293  }
 294  
 295  /**
 296   * Enqueue a script.
 297   *
 298   * Registers the script if $src provided (does NOT overwrite), and enqueues it.
 299   *
 300   * @see WP_Dependencies::add()
 301   * @see WP_Dependencies::add_data()
 302   * @see WP_Dependencies::enqueue()
 303   *
 304   * @since 2.1.0
 305   *
 306   * @param string           $handle    Name of the script. Should be unique.
 307   * @param string           $src       Full URL of the script, or path of the script relative to the WordPress root directory.
 308   *                                    Default empty.
 309   * @param array            $deps      Optional. An array of registered script handles this script depends on. Default empty array.
 310   * @param string|bool|null $ver       Optional. String specifying script version number, if it has one, which is added to the URL
 311   *                                    as a query string for cache busting purposes. If version is set to false, a version
 312   *                                    number is automatically added equal to current installed WordPress version.
 313   *                                    If set to null, no version is added.
 314   * @param bool             $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>.
 315   *                                    Default 'false'.
 316   */
 317  function wp_enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $in_footer = false ) {
 318      $wp_scripts = wp_scripts();
 319  
 320      _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
 321  
 322      if ( $src || $in_footer ) {
 323          $_handle = explode( '?', $handle );
 324  
 325          if ( $src ) {
 326              $wp_scripts->add( $_handle[0], $src, $deps, $ver );
 327          }
 328  
 329          if ( $in_footer ) {
 330              $wp_scripts->add_data( $_handle[0], 'group', 1 );
 331          }
 332      }
 333  
 334      $wp_scripts->enqueue( $handle );
 335  }
 336  
 337  /**
 338   * Remove a previously enqueued script.
 339   *
 340   * @see WP_Dependencies::dequeue()
 341   *
 342   * @since 3.1.0
 343   *
 344   * @param string $handle Name of the script to be removed.
 345   */
 346  function wp_dequeue_script( $handle ) {
 347      _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
 348  
 349      wp_scripts()->dequeue( $handle );
 350  }
 351  
 352  /**
 353   * Determines whether a script has been added to the queue.
 354   *
 355   * For more information on this and similar theme functions, check out
 356   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 357   * Conditional Tags} article in the Theme Developer Handbook.
 358   *
 359   * @since 2.8.0
 360   * @since 3.5.0 'enqueued' added as an alias of the 'queue' list.
 361   *
 362   * @param string $handle Name of the script.
 363   * @param string $list   Optional. Status of the script to check. Default 'enqueued'.
 364   *                       Accepts 'enqueued', 'registered', 'queue', 'to_do', and 'done'.
 365   * @return bool Whether the script is queued.
 366   */
 367  function wp_script_is( $handle, $list = 'enqueued' ) {
 368      _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
 369  
 370      return (bool) wp_scripts()->query( $handle, $list );
 371  }
 372  
 373  /**
 374   * Add metadata to a script.
 375   *
 376   * Works only if the script has already been added.
 377   *
 378   * Possible values for $key and $value:
 379   * 'conditional' string Comments for IE 6, lte IE 7, etc.
 380   *
 381   * @since 4.2.0
 382   *
 383   * @see WP_Dependencies::add_data()
 384   *
 385   * @param string $handle Name of the script.
 386   * @param string $key    Name of data point for which we're storing a value.
 387   * @param mixed  $value  String containing the data to be added.
 388   * @return bool True on success, false on failure.
 389   */
 390  function wp_script_add_data( $handle, $key, $value ) {
 391      return wp_scripts()->add_data( $handle, $key, $value );
 392  }


Generated: Sun Jul 21 01:00:03 2019 Cross-referenced by PHPXref 0.7.1