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