Completed
Push — fix/flickr-shortcode ( 3e5712...02d728 )
by
unknown
24:33 queued 14:33
created

Assets::add_async_script()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Jetpack Assets package.
4
 *
5
 * @package  automattic/jetpack-assets
6
 */
7
8
namespace Automattic\Jetpack;
9
10
use Automattic\Jetpack\Constants as Jetpack_Constants;
11
12
/**
13
 * Class Assets
14
 */
15
class Assets {
16
	/**
17
	 * Holds all the scripts handles that should be loaded in an async fashion.
18
	 *
19
	 * @var array
20
	 */
21
	private $async_script_handles = array();
22
	/**
23
	 * The singleton instance of this class.
24
	 *
25
	 * @var Assets
26
	 */
27
	protected static $instance;
28
29
	/**
30
	 * Constructor.
31
	 *
32
	 * Static-only class, so nothing here.
33
	 */
34
	private function __construct() {}
35
36
	/**
37
	 * Get the singleton instance of the class.
38
	 *
39
	 * @return Assets
40
	 */
41
	public static function instance() {
42
		if ( ! isset( self::$instance ) ) {
43
			self::$instance = new Assets();
44
			self::$instance->init_hooks();
45
		}
46
47
		return self::$instance;
48
	}
49
50
	/**
51
	 * Initalize the hooks as needed.
52
	 */
53
	private function init_hooks() {
54
		/*
55
		 * Load some scripts asynchronously.
56
		 */
57
		add_filter( 'script_loader_tag', array( $this, 'script_add_async' ), 10, 2 );
58
	}
59
60
	/**
61
	 * A public method for adding the async script.
62
	 *
63
	 * @param string $script_handle Script handle.
64
	 */
65
	public function add_async_script( $script_handle ) {
66
		$this->async_script_handles[] = $script_handle;
67
	}
68
69
	/**
70
	 * Add an async attribute to scripts that can be loaded asynchronously.
71
	 * https://www.w3schools.com/tags/att_script_async.asp
72
	 *
73
	 * @param string $tag    The <script> tag for the enqueued script.
74
	 * @param string $handle The script's registered handle.
75
	 */
76
	public function script_add_async( $tag, $handle ) {
77
		if ( empty( $this->async_script_handles ) ) {
78
			return $tag;
79
		}
80
81
		if ( in_array( $handle, $this->async_script_handles, true ) ) {
82
			return preg_replace( '/^<script /i', '<script async defer ', $tag );
83
		}
84
85
		return $tag;
86
	}
87
88
	/**
89
	 * Given a minified path, and a non-minified path, will return
90
	 * a minified or non-minified file URL based on whether SCRIPT_DEBUG is set and truthy.
91
	 *
92
	 * Both `$min_base` and `$non_min_base` are expected to be relative to the
93
	 * root Jetpack directory.
94
	 *
95
	 * @since 5.6.0
96
	 *
97
	 * @param string $min_path minified path.
98
	 * @param string $non_min_path non-minified path.
99
	 * @return string The URL to the file
100
	 */
101
	public static function get_file_url_for_environment( $min_path, $non_min_path ) {
102
		$path = ( Jetpack_Constants::is_defined( 'SCRIPT_DEBUG' ) && Jetpack_Constants::get_constant( 'SCRIPT_DEBUG' ) )
103
			? $non_min_path
104
			: $min_path;
105
106
		$url = plugins_url( $path, Jetpack_Constants::get_constant( 'JETPACK__PLUGIN_FILE' ) );
107
108
		/**
109
		 * Filters the URL for a file passed through the get_file_url_for_environment function.
110
		 *
111
		 * @since 8.1.0
112
		 *
113
		 * @package assets
114
		 *
115
		 * @param string $url The URL to the file.
116
		 * @param string $min_path The minified path.
117
		 * @param string $non_min_path The non-minified path.
118
		 */
119
		return apply_filters( 'jetpack_get_file_for_environment', $url, $min_path, $non_min_path );
0 ignored issues
show
Unused Code introduced by
The call to apply_filters() has too many arguments starting with $min_path.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
120
	}
121
122
	/**
123
	 * A helper function that lets you enqueue scripts in an async fashion.
124
	 *
125
	 * @param string $handle        Name of the script. Should be unique.
126
	 * @param string $min_path      Minimized script path.
127
	 * @param string $non_min_path  Full Script path.
128
	 * @param array  $deps           Array of script dependencies.
129
	 * @param bool   $ver             The script version.
130
	 * @param bool   $in_footer       Should the script be included in the footer.
131
	 */
132
	public static function enqueue_async_script( $handle, $min_path, $non_min_path, $deps = array(), $ver = false, $in_footer = true ) {
133
		$assets_instance = self::instance();
134
		$assets_instance->add_async_script( $handle );
135
		wp_enqueue_script( $handle, self::get_file_url_for_environment( $min_path, $non_min_path ), $deps, $ver, $in_footer );
136
	}
137
138
}
139