Completed
Push — master ( a7cd2a...eabd6c )
by Stephen
38:42
created

WP_Scripts::do_item()   F

Complexity

Conditions 31
Paths > 20000

Size

Total Lines 124
Code Lines 64

Duplication

Lines 10
Ratio 8.06 %
Metric Value
dl 10
loc 124
rs 2
cc 31
eloc 64
nc 118082
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Dependencies API: WP_Scripts class
4
 *
5
 * @since 2.6.0
6
 *
7
 * @package WordPress
8
 * @subpackage Dependencies
9
 */
10
11
/**
12
 * Core class used to register scripts.
13
 *
14
 * @package WordPress
15
 * @uses WP_Dependencies
16
 * @since 2.1.0
17
 */
18
class WP_Scripts extends WP_Dependencies {
19
	/**
20
	 * Base URL for scripts.
21
	 *
22
	 * Full URL with trailing slash.
23
	 *
24
	 * @since 2.6.0
25
	 * @access public
26
	 * @var string
27
	 */
28
	public $base_url;
29
30
	/**
31
	 * URL of the content directory.
32
	 *
33
	 * @since 2.8.0
34
	 * @access public
35
	 * @var string
36
	 */
37
	public $content_url;
38
39
	/**
40
	 * Default version string for stylesheets.
41
	 *
42
	 * @since 2.6.0
43
	 * @access public
44
	 * @var string
45
	 */
46
	public $default_version;
47
48
	/**
49
	 * Holds handles of scripts which are enqueued in footer.
50
	 *
51
	 * @since 2.8.0
52
	 * @access public
53
	 * @var array
54
	 */
55
	public $in_footer = array();
56
57
	/**
58
	 * Holds a list of script handles which will be concatenated.
59
	 *
60
	 * @since 2.8.0
61
	 * @access public
62
	 * @var string
63
	 */
64
	public $concat = '';
65
66
	/**
67
	 * Holds a string which contains script handles and their version.
68
	 *
69
	 * @since 2.8.0
70
	 * @deprecated 3.4.0
71
	 * @access public
72
	 * @var string
73
	 */
74
	public $concat_version = '';
75
76
	/**
77
	 * Whether to perform concatenation.
78
	 *
79
	 * @since 2.8.0
80
	 * @access public
81
	 * @var bool
82
	 */
83
	public $do_concat = false;
84
85
	/**
86
	 * Holds HTML markup of scripts and additional data if concatenation
87
	 * is enabled.
88
	 *
89
	 * @since 2.8.0
90
	 * @access public
91
	 * @var string
92
	 */
93
	public $print_html = '';
94
95
	/**
96
	 * HTML to print before the script handle.
97
	 *
98
	 * @since 4.5.0
99
	 * @access public
100
	 * @var string
101
	 */
102
	public $print_html_before = '';
103
104
	/**
105
	 * Holds inline code if concatenation is enabled.
106
	 *
107
	 * @since 2.8.0
108
	 * @access public
109
	 * @var string
110
	 */
111
	public $print_code = '';
112
113
	/**
114
	 * Holds a list of script handles which are not in the default directory
115
	 * if concatenation is enabled.
116
	 *
117
	 * Unused in core.
118
	 *
119
	 * @since 2.8.0
120
	 * @access public
121
	 * @var string
122
	 */
123
	public $ext_handles = '';
124
125
	/**
126
	 * Holds a string which contains handles and versions of scripts which
127
	 * are not in the default directory if concatenation is enabled.
128
	 *
129
	 * Unused in core.
130
	 *
131
	 * @since 2.8.0
132
	 * @access public
133
	 * @var string
134
	 */
135
	public $ext_version = '';
136
137
	/**
138
	 * List of default directories.
139
	 *
140
	 * @since 2.8.0
141
	 * @access public
142
	 * @var array
143
	 */
144
	public $default_dirs;
145
146
	/**
147
	 * Constructor.
148
	 *
149
	 * @since 2.6.0
150
	 * @access public
151
	 */
152
	public function __construct() {
153
		$this->init();
154
		add_action( 'init', array( $this, 'init' ), 0 );
155
	}
156
157
	/**
158
	 * Initialize the class.
159
	 *
160
	 * @since 3.4.0
161
	 * @access public
162
	 */
163
	public function init() {
164
		/**
165
		 * Fires when the WP_Scripts instance is initialized.
166
		 *
167
		 * @since 2.6.0
168
		 *
169
		 * @param WP_Scripts &$this WP_Scripts instance, passed by reference.
170
		 */
171
		do_action_ref_array( 'wp_default_scripts', array(&$this) );
172
	}
173
174
	/**
175
	 * Prints scripts.
176
	 *
177
	 * Prints the scripts passed to it or the print queue. Also prints all necessary dependencies.
178
	 *
179
	 * @since 2.1.0
180
	 * @since 2.8.0 Added the `$group` parameter.
181
	 * @access public
182
	 *
183
	 * @param mixed $handles Optional. Scripts to be printed. (void) prints queue, (string) prints
184
	 *                       that script, (array of strings) prints those scripts. Default false.
185
	 * @param int   $group   Optional. If scripts were queued in groups prints this group number.
186
	 *                       Default false.
187
	 * @return array Scripts that have been printed.
188
	 */
189
	public function print_scripts( $handles = false, $group = false ) {
190
		return $this->do_items( $handles, $group );
191
	}
192
193
	/**
194
	 * Prints extra scripts of a registered script.
195
	 *
196
	 * @since 2.1.0
197
	 * @since 2.8.0 Added the `$echo` parameter.
198
	 * @deprecated 3.3.0
199
	 * @access public
200
	 *
201
	 * @see print_extra_script()
202
	 *
203
	 * @param string $handle The script's registered handle.
204
	 * @param bool   $echo   Optional. Whether to echo the extra script instead of just returning it.
205
	 *                       Default true.
206
	 * @return bool|string|void Void if no data exists, extra scripts if `$echo` is true, true otherwise.
207
	 */
208
	public function print_scripts_l10n( $handle, $echo = true ) {
209
		_deprecated_function( __FUNCTION__, '3.3', 'print_extra_script()' );
210
		return $this->print_extra_script( $handle, $echo );
211
	}
212
213
	/**
214
	 * Prints extra scripts of a registered script.
215
	 *
216
	 * @since 3.3.0
217
	 * @access public
218
	 *
219
	 * @param string $handle The script's registered handle.
220
	 * @param bool   $echo   Optional. Whether to echo the extra script instead of just returning it.
221
	 *                       Default true.
222
	 * @return bool|string|void Void if no data exists, extra scripts if `$echo` is true, true otherwise.
223
	 */
224
	public function print_extra_script( $handle, $echo = true ) {
225
		if ( !$output = $this->get_data( $handle, 'data' ) )
226
			return;
227
228
		if ( !$echo )
229
			return $output;
230
231
		echo "<script type='text/javascript'>\n"; // CDATA and type='text/javascript' is not needed for HTML 5
232
		echo "/* <![CDATA[ */\n";
233
		echo "$output\n";
234
		echo "/* ]]> */\n";
235
		echo "</script>\n";
236
237
		return true;
238
	}
239
240
	/**
241
	 * Processes a script dependency.
242
	 *
243
	 * @since 2.6.0
244
	 * @since 2.8.0 Added the `$group` parameter.
245
	 * @access public
246
	 *
247
	 * @see WP_Dependencies::do_item()
248
	 *
249
	 * @param string $handle    The script's registered handle.
250
	 * @param int|false $group  Optional. Group level: (int) level, (false) no groups. Default false.
251
	 * @return bool True on success, false on failure.
252
	 */
253
	public function do_item( $handle, $group = false ) {
254
		if ( !parent::do_item($handle) )
255
			return false;
256
257
		if ( 0 === $group && $this->groups[$handle] > 0 ) {
258
			$this->in_footer[] = $handle;
259
			return false;
260
		}
261
262
		if ( false === $group && in_array($handle, $this->in_footer, true) )
263
			$this->in_footer = array_diff( $this->in_footer, (array) $handle );
264
265
		$obj = $this->registered[$handle];
266
267 View Code Duplication
		if ( null === $obj->ver ) {
268
			$ver = '';
269
		} else {
270
			$ver = $obj->ver ? $obj->ver : $this->default_version;
271
		}
272
273 View Code Duplication
		if ( isset($this->args[$handle]) )
274
			$ver = $ver ? $ver . '&amp;' . $this->args[$handle] : $this->args[$handle];
275
276
		$src = $obj->src;
277
		$cond_before = $cond_after = '';
278
		$conditional = isset( $obj->extra['conditional'] ) ? $obj->extra['conditional'] : '';
279
280
		if ( $conditional ) {
281
			$cond_before = "<!--[if {$conditional}]>\n";
282
			$cond_after = "<![endif]-->\n";
283
		}
284
285
		$before_handle = $this->print_inline_script( $handle, 'before', false );
286
		$after_handle = $this->print_inline_script( $handle, 'after', false );
287
288
		if ( $before_handle ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $before_handle of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
289
			$before_handle = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $before_handle );
290
		}
291
292
		if ( $after_handle ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $after_handle of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
293
			$after_handle = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $after_handle );
294
		}
295
296
		if ( $this->do_concat ) {
297
			/**
298
			 * Filter the script loader source.
299
			 *
300
			 * @since 2.2.0
301
			 *
302
			 * @param string $src    Script loader source path.
303
			 * @param string $handle Script handle.
304
			 */
305
			$srce = apply_filters( 'script_loader_src', $src, $handle );
306
307
			if ( $before_handle && ! $conditional ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $before_handle of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
308
				$this->print_html_before .= $before_handle;
309
			}
310
311
			if ( $this->in_default_dir( $srce ) && ! $conditional && ! $after_handle ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $after_handle of type false|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
312
				$this->print_code .= $this->print_extra_script( $handle, false );
313
				$this->concat .= "$handle,";
314
				$this->concat_version .= "$handle$ver";
0 ignored issues
show
Deprecated Code introduced by
The property WP_Scripts::$concat_version has been deprecated with message: 3.4.0

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
315
				return true;
316
			} else {
317
				$this->ext_handles .= "$handle,";
318
				$this->ext_version .= "$handle$ver";
319
			}
320
		}
321
322
		$has_conditional_data = $conditional && $this->get_data( $handle, 'data' );
323
324
		if ( $has_conditional_data ) {
325
			echo $cond_before;
326
		}
327
328
		$this->print_extra_script( $handle );
329
330
		if ( $has_conditional_data ) {
331
			echo $cond_after;
332
		}
333
334
		// A single item may alias a set of items, by having dependencies, but no source.
335
		if ( ! $obj->src ) {
336
			return true;
337
		}
338
339 View Code Duplication
		if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && 0 === strpos( $src, $this->content_url ) ) ) {
340
			$src = $this->base_url . $src;
341
		}
342
343
		if ( ! empty( $ver ) )
344
			$src = add_query_arg( 'ver', $ver, $src );
345
346
		/** This filter is documented in wp-includes/class.wp-scripts.php */
347
		$src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) );
348
349
		if ( ! $src )
350
			return true;
351
352
		$tag = "{$cond_before}{$before_handle}<script type='text/javascript' src='$src'></script>\n{$after_handle}{$cond_after}";
353
354
		/**
355
		 * Filter the HTML script tag of an enqueued script.
356
		 *
357
		 * @since 4.1.0
358
		 *
359
		 * @param string $tag    The `<script>` tag for the enqueued script.
360
		 * @param string $handle The script's registered handle.
361
		 * @param string $src    The script's source URL.
362
		 */
363
		$tag = apply_filters( 'script_loader_tag', $tag, $handle, $src );
364
365
		if ( $this->do_concat ) {
366
			if ( $after_handle ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $after_handle of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
367
				$this->print_html_before .= $tag;
368
			} else {
369
				$this->print_html .= $tag;
370
			}
371
		} else {
372
			echo $tag;
373
		}
374
375
		return true;
376
	}
377
378
	/**
379
	 * Adds extra code to a registered script.
380
	 *
381
	 * @since 4.5.0
382
	 * @access public
383
	 *
384
	 * @param string $handle   Name of the script to add the inline script to. Must be lowercase.
385
	 * @param string $data     String containing the javascript to be added.
386
	 * @param string $position Optional. Whether to add the inline script before the handle
387
	 *                         or after. Default 'after'.
388
	 * @return bool True on success, false on failure.
389
	 */
390 View Code Duplication
	public function add_inline_script( $handle, $data, $position = 'after' ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
391
		if ( ! $data ) {
392
			return false;
393
		}
394
395
		if ( 'after' !== $position ) {
396
			$position = 'before';
397
		}
398
399
		$script   = (array) $this->get_data( $handle, $position );
400
		$script[] = $data;
401
402
		return $this->add_data( $handle, $position, $script );
403
	}
404
405
	/**
406
	 * Prints inline scripts registered for a specific handle.
407
	 *
408
	 * @since 4.5.0
409
	 * @access public
410
	 *
411
	 * @param string $handle   Name of the script to add the inline script to. Must be lowercase.
412
	 * @param string $position Optional. Whether to add the inline script before the handle
413
	 *                         or after. Default 'after'.
414
	 * @param bool $echo       Optional. Whether to echo the script instead of just returning it.
415
	 *                         Default true.
416
	 * @return string|false Script on success, false otherwise.
417
	 */
418 View Code Duplication
	public function print_inline_script( $handle, $position = 'after', $echo = true ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
419
		$output = $this->get_data( $handle, $position );
420
421
		if ( empty( $output ) ) {
422
			return false;
423
		}
424
425
		$output = trim( implode( "\n", $output ), "\n" );
426
427
		if ( $echo ) {
428
			printf( "<script type='text/javascript'>\n%s\n</script>\n", $output );
429
		}
430
431
		return $output;
432
	}
433
434
	/**
435
	 * Localizes a script, only if the script has already been added.
436
	 *
437
	 * @since 2.1.0
438
	 * @access public
439
	 *
440
	 * @param string $handle
441
	 * @param string $object_name
442
	 * @param array $l10n
443
	 * @return bool
444
	 */
445
	public function localize( $handle, $object_name, $l10n ) {
446
		if ( $handle === 'jquery' )
447
			$handle = 'jquery-core';
448
449
		if ( is_array($l10n) && isset($l10n['l10n_print_after']) ) { // back compat, preserve the code in 'l10n_print_after' if present
450
			$after = $l10n['l10n_print_after'];
451
			unset($l10n['l10n_print_after']);
452
		}
453
454
		foreach ( (array) $l10n as $key => $value ) {
455
			if ( !is_scalar($value) )
456
				continue;
457
458
			$l10n[$key] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8');
459
		}
460
461
		$script = "var $object_name = " . wp_json_encode( $l10n ) . ';';
462
463
		if ( !empty($after) )
464
			$script .= "\n$after;";
465
466
		$data = $this->get_data( $handle, 'data' );
467
468
		if ( !empty( $data ) )
469
			$script = "$data\n$script";
470
471
		return $this->add_data( $handle, 'data', $script );
472
	}
473
474
	/**
475
	 * Sets handle group.
476
	 *
477
	 * @since 2.8.0
478
	 * @access public
479
	 *
480
	 * @see WP_Dependencies::set_group()
481
	 *
482
	 * @param string    $handle    Name of the item. Should be unique.
483
	 * @param bool      $recursion Internal flag that calling function was called recursively.
484
	 * @param int|false $group     Optional. Group level: (int) level, (false) no groups. Default false.
485
	 * @return bool Not already in the group or a lower group
486
	 */
487
	public function set_group( $handle, $recursion, $group = false ) {
488
		if ( isset( $this->registered[$handle]->args ) && $this->registered[$handle]->args === 1 )
489
			$grp = 1;
490
		else
491
			$grp = (int) $this->get_data( $handle, 'group' );
492
493
		if ( false !== $group && $grp > $group )
494
			$grp = $group;
495
496
		return parent::set_group( $handle, $recursion, $grp );
497
	}
498
499
	/**
500
	 * Determines script dependencies.
501
     *
502
	 * @since 2.1.0
503
	 * @access public
504
	 *
505
	 * @see WP_Dependencies::all_deps()
506
	 *
507
	 * @param mixed     $handles   Item handle and argument (string) or item handles and arguments (array of strings).
508
	 * @param bool      $recursion Internal flag that function is calling itself.
509
	 * @param int|false $group     Optional. Group level: (int) level, (false) no groups. Default false.
510
	 * @return bool True on success, false on failure.
511
	 */
512 View Code Duplication
	public function all_deps( $handles, $recursion = false, $group = false ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
513
		$r = parent::all_deps( $handles, $recursion, $group );
514
		if ( ! $recursion ) {
515
			/**
516
			 * Filter the list of script dependencies left to print.
517
			 *
518
			 * @since 2.3.0
519
			 *
520
			 * @param array $to_do An array of script dependencies.
521
			 */
522
			$this->to_do = apply_filters( 'print_scripts_array', $this->to_do );
0 ignored issues
show
Documentation Bug introduced by
It seems like apply_filters('print_scr...s_array', $this->to_do) of type * is incompatible with the declared type array of property $to_do.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
523
		}
524
		return $r;
525
	}
526
527
	/**
528
	 * Processes items and dependencies for the head group.
529
	 *
530
	 * @since 2.8.0
531
	 * @access public
532
	 *
533
	 * @see WP_Dependencies::do_items()
534
	 *
535
	 * @return array Handles of items that have been processed.
536
	 */
537
	public function do_head_items() {
538
		$this->do_items(false, 0);
539
		return $this->done;
540
	}
541
542
	/**
543
	 * Processes items and dependencies for the footer group.
544
	 *
545
	 * @since 2.8.0
546
	 * @access public
547
	 *
548
	 * @see WP_Dependencies::do_items()
549
	 *
550
	 * @return array Handles of items that have been processed.
551
	 */
552
	public function do_footer_items() {
553
		$this->do_items(false, 1);
554
		return $this->done;
555
	}
556
557
	/**
558
	 * Whether a handle's source is in a default directory.
559
	 *
560
	 * @since 2.8.0
561
	 * @access public
562
	 *
563
	 * @param string $src The source of the enqueued script.
564
	 * @return bool True if found, false if not.
565
	 */
566
	public function in_default_dir( $src ) {
567
		if ( ! $this->default_dirs ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->default_dirs of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
568
			return true;
569
		}
570
571
		if ( 0 === strpos( $src, '/' . WPINC . '/js/l10n' ) ) {
572
			return false;
573
		}
574
575
		foreach ( (array) $this->default_dirs as $test ) {
576
			if ( 0 === strpos( $src, $test ) ) {
577
				return true;
578
			}
579
		}
580
		return false;
581
	}
582
583
	/**
584
	 * Resets class properties.
585
	 *
586
	 * @since 2.8.0
587
	 * @access public
588
	 */
589
	public function reset() {
590
		$this->do_concat = false;
591
		$this->print_code = '';
592
		$this->concat = '';
593
		$this->concat_version = '';
0 ignored issues
show
Deprecated Code introduced by
The property WP_Scripts::$concat_version has been deprecated with message: 3.4.0

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
594
		$this->print_html = '';
595
		$this->print_html_before = '';
596
		$this->ext_version = '';
597
		$this->ext_handles = '';
598
	}
599
}
600