[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * BackPress script procedural API.
   4   *
   5   * @package BackPress
   6   * @since r16
   7   */
   8  
   9  /**
  10   * Prints script tags in document head.
  11   *
  12   * Called by admin-header.php and by wp_head hook. Since it is called by wp_head
  13   * on every page load, the function does not instantiate the WP_Scripts object
  14   * unless script names are explicitly passed. Does make use of already
  15   * instantiated $wp_scripts if present. Use provided wp_print_scripts hook to
  16   * register/enqueue new scripts.
  17   *
  18   * @since r16
  19   * @see WP_Dependencies::print_scripts()
  20   */
  21  function wp_print_scripts( $handles = false ) {
  22      do_action( 'wp_print_scripts' );
  23      if ( '' === $handles ) // for wp_head
  24          $handles = false;
  25  
  26      global $wp_scripts;
  27      if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
  28          if ( ! did_action( 'init' ) )
  29              _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
  30                  '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
  31  
  32          if ( !$handles )
  33              return array(); // No need to instantiate if nothing is there.
  34          else
  35              $wp_scripts = new WP_Scripts();
  36      }
  37  
  38      return $wp_scripts->do_items( $handles );
  39  }
  40  
  41  /**
  42   * Register new Javascript file.
  43   *
  44   * @since r16
  45   * @param string $handle Script name
  46   * @param string $src Script url
  47   * @param array $deps (optional) Array of script names on which this script depends
  48   * @param string|bool $ver (optional) Script version (used for cache busting), set to null to disable
  49   * @param bool $in_footer (optional) Whether to enqueue the script before </head> or before </body>
  50   * @return null
  51   */
  52  function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
  53      global $wp_scripts;
  54      if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
  55          if ( ! did_action( 'init' ) )
  56              _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
  57                  '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
  58          $wp_scripts = new WP_Scripts();
  59      }
  60  
  61      $wp_scripts->add( $handle, $src, $deps, $ver );
  62      if ( $in_footer )
  63          $wp_scripts->add_data( $handle, 'group', 1 );
  64  }
  65  
  66  /**
  67   * Wrapper for $wp_scripts->localize().
  68   *
  69   * Used to localizes a script.
  70   * Works only if the script has already been added.
  71   * Accepts an associative array $l10n and creates JS object:
  72   * "$object_name" = {
  73   *   key: value,
  74   *   key: value,
  75   *   ...
  76   * }
  77   * See http://core.trac.wordpress.org/ticket/11520 for more information.
  78   *
  79   * @since r16
  80   *
  81   * @param string $handle The script handle that was registered or used in script-loader
  82   * @param string $object_name Name for the created JS object. This is passed directly so it should be qualified JS variable /[a-zA-Z0-9_]+/
  83   * @param array $l10n Associative PHP array containing the translated strings. HTML entities will be converted and the array will be JSON encoded.
  84   * @return bool Whether the localization was added successfully.
  85   */
  86  function wp_localize_script( $handle, $object_name, $l10n ) {
  87      global $wp_scripts;
  88      if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
  89          if ( ! did_action( 'init' ) )
  90              _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
  91                  '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
  92  
  93          return false;
  94      }
  95  
  96      return $wp_scripts->localize( $handle, $object_name, $l10n );
  97  }
  98  
  99  /**
 100   * Remove a registered script.
 101   *
 102   * @since r16
 103   * @see WP_Scripts::remove() For parameter information.
 104   */
 105  function wp_deregister_script( $handle ) {
 106      global $wp_scripts;
 107      if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
 108          if ( ! did_action( 'init' ) )
 109              _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
 110                  '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
 111          $wp_scripts = new WP_Scripts();
 112      }
 113  
 114      $wp_scripts->remove( $handle );
 115  }
 116  
 117  /**
 118   * Enqueues script.
 119   *
 120   * Registers the script if src provided (does NOT overwrite) and enqueues.
 121   *
 122   * @since r16
 123   * @see wp_register_script() For parameter information.
 124   */
 125  function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false, $in_footer = false ) {
 126      global $wp_scripts;
 127      if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
 128          if ( ! did_action( 'init' ) )
 129              _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
 130                  '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
 131          $wp_scripts = new WP_Scripts();
 132      }
 133  
 134      if ( $src ) {
 135          $_handle = explode('?', $handle);
 136          $wp_scripts->add( $_handle[0], $src, $deps, $ver );
 137          if ( $in_footer )
 138              $wp_scripts->add_data( $_handle[0], 'group', 1 );
 139      }
 140      $wp_scripts->enqueue( $handle );
 141  }
 142  
 143  /**
 144   * Remove an enqueued script.
 145   *
 146   * @since WP 3.1
 147   * @see WP_Scripts::dequeue() For parameter information.
 148   */
 149  function wp_dequeue_script( $handle ) {
 150      global $wp_scripts;
 151      if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
 152          if ( ! did_action( 'init' ) )
 153              _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
 154                  '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
 155          $wp_scripts = new WP_Scripts();
 156      }
 157  
 158      $wp_scripts->dequeue( $handle );
 159  }
 160  
 161  /**
 162   * Check whether script has been added to WordPress Scripts.
 163   *
 164   * The values for list defaults to 'queue', which is the same as enqueue for
 165   * scripts.
 166   *
 167   * @since WP unknown; BP unknown
 168   *
 169   * @param string $handle Handle used to add script.
 170   * @param string $list Optional, defaults to 'queue'. Others values are 'registered', 'queue', 'done', 'to_do'
 171   * @return bool
 172   */
 173  function wp_script_is( $handle, $list = 'queue' ) {
 174      global $wp_scripts;
 175      if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
 176          if ( ! did_action( 'init' ) )
 177              _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
 178                  '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
 179          $wp_scripts = new WP_Scripts();
 180      }
 181  
 182      $query = $wp_scripts->query( $handle, $list );
 183  
 184      if ( is_object( $query ) )
 185          return true;
 186  
 187      return $query;
 188  }


Generated: Fri May 25 03:56:23 2012 Hosted by follow the white rabbit.