Completed
Push — add/pwa ( dcada3...4f210b )
by
unknown
12:13 queued 04:21
created

Jetpack_Perf_Optimize_Assets   D

Complexity

Total Complexity 90

Size/Duplication

Total Lines 454
Duplicated Lines 20.26 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
dl 92
loc 454
rs 4.8717
c 0
b 0
f 0
wmc 90
lcom 2
cbo 0

29 Methods

Rating   Name   Duplication   Size   Complexity  
A instance() 0 7 2
A disable_for_request() 0 6 1
C __construct() 0 49 8
A disable_emojis() 0 15 1
A optimize_jetpack() 0 3 1
A disable_emojis_tinymce() 0 7 2
A disable_emojis_remove_dns_prefetch() 0 10 2
A set_first_load_cookie() 3 5 2
A send_scripts_to_footer() 11 17 3
A move_styles_to_bottom_of_header() 0 4 1
A set_asset_groups() 0 8 2
A remove_external_font_scripts() 0 4 1
A remove_external_font_styles() 0 4 1
A filter_inline_scripts() 16 16 4
D print_inline_scripts() 0 33 9
B should_async_script() 19 19 6
B should_defer_script() 18 18 6
A should_remove_script() 0 3 1
A should_inline_script() 0 3 3
A filter_inline_styles() 15 15 4
B print_inline_styles() 0 21 5
A fix_css_urls() 0 13 2
A rel2abspath() 0 20 3
A should_inline_style() 0 3 3
A should_remove_style() 0 3 1
C should_inline_asset() 0 39 11
A should_remove_asset() 0 3 1
A register_inline_script() 5 11 2
A register_inline_style() 5 9 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Jetpack_Perf_Optimize_Assets often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Jetpack_Perf_Optimize_Assets, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * Optimizes page assets for unreliable networks and fast rendering, particularly with empty caches
5
 * - inline scripts and styles
6
 * - async external JS
7
 * - remove references to external fonts
8
 * - move CSS links below scripts in head (scripts after CSS blocks render until script finishes downloading)
9
 */
10
11
class Jetpack_Perf_Optimize_Assets {
12
	private static $__instance = null;
13
	private $remove_remote_fonts = false;
14
	private $inline_scripts_and_styles = false;
15
	private $async_scripts = false;
16
	private $defer_scripts = false;
17
	const INLINE_ASSET_MAX_SIZE_BYTES = 50 * 1024; // 50kb
18
19
	/**
20
	 * Singleton implementation
21
	 *
22
	 * @return object
23
	 */
24
	public static function instance() {
25
		if ( ! is_a( self::$__instance, 'Jetpack_Perf_Optimize_Assets' ) ) {
26
			self::$__instance = new Jetpack_Perf_Optimize_Assets();
27
		}
28
29
		return self::$__instance;
30
	}
31
32
	public function disable_for_request() {
33
		$this->remove_remote_fonts = false;
34
		$this->inline_scripts_and_styles = false;
35
		$this->async_scripts = false;
36
		$this->defer_scripts = false;
37
	}
38
39
	/**
40
	 * TODO: detect if this is worth doing for wp-admin?
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
41
	 */
42
43
	/**
44
	 * Registers actions
45
	 */
46
	private function __construct() {
47
		$this->is_first_load             = ! isset( $_COOKIE['jetpack_perf_loaded'] );
0 ignored issues
show
Bug introduced by
The property is_first_load does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
48
		$this->remove_remote_fonts       = get_option( 'perf_remove_remote_fonts', true );
49
		$this->inline_always             = get_option( 'perf_inline_on_every_request', false );
0 ignored issues
show
Bug introduced by
The property inline_always does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
50
		$this->inline_scripts_and_styles = get_option( 'perf_inline_scripts_and_styles', true ) && ( $this->is_first_load || $this->inline_always );
51
		$this->async_scripts             = get_option( 'perf_async_scripts', true );
52
		$this->defer_scripts             = get_option( 'perf_defer_scripts', true );
53
		$this->move_scripts_to_footer    = true;
0 ignored issues
show
Bug introduced by
The property move_scripts_to_footer does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
54
		$this->move_scripts_above_css_in_header = true;
0 ignored issues
show
Bug introduced by
The property move_scripts_above_css_in_header does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
55
		$this->remove_core_emojis        = true;
0 ignored issues
show
Bug introduced by
The property remove_core_emojis does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
56
		$this->prevent_jetpack_implode_css = true;
0 ignored issues
show
Bug introduced by
The property prevent_jetpack_implode_css does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
57
58
		if ( $this->remove_remote_fonts ) {
59
			add_filter( 'jetpack_perf_remove_script', array( $this, 'remove_external_font_scripts' ), 10, 3 );
60
			add_filter( 'jetpack_perf_remove_style', array( $this, 'remove_external_font_styles' ), 10, 3 );
61
		}
62
63
		if ( $this->move_scripts_to_footer ) {
64
			add_filter( 'jetpack_perf_asset_group', array( $this, 'set_asset_groups' ), 10, 2 );
65
		}
66
67
		if ( $this->move_scripts_above_css_in_header ) {
68
			add_action( 'init', array( $this, 'move_styles_to_bottom_of_header' ), PHP_INT_MAX );
69
		}
70
71
		if ( $this->prevent_jetpack_implode_css ) {
72
			add_filter( 'jetpack_implode_frontend_css', '__return_false' );
73
		}
74
75
		add_action( 'wp_enqueue_scripts', array( $this, 'send_scripts_to_footer' ), PHP_INT_MAX );
76
		add_filter( 'script_loader_src', array( $this, 'filter_inline_scripts' ), -100, 2 );
77
		add_filter( 'script_loader_tag', array( $this, 'print_inline_scripts' ), -100, 3 );
78
		add_filter( 'style_loader_src', array( $this, 'filter_inline_styles' ), -100, 2 );
79
		add_filter( 'style_loader_tag', array( $this, 'print_inline_styles' ), -100, 4 );
80
81
		add_action( 'init', array( $this, 'set_first_load_cookie' ) );
82
83
		/**
84
		 * Feature, theme and plugin-specific hacks
85
		 */
86
87
		// remove emoji detection - TODO a setting for this
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
88
		if ( $this->remove_core_emojis ) {
89
			add_action( 'init', array( $this, 'disable_emojis' ) );
90
		}
91
92
		// inline/defer/async stuff for Jetpack
93
		add_action( 'init', array( $this, 'optimize_jetpack' ) );
94
	}
95
96
	/** Disabling Emojis **/
97
	// improves page load performance
98
99
	function disable_emojis() {
100
		remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
101
		remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
102
		remove_action( 'embed_head', 'print_emoji_detection_script', 7 );
103
104
		remove_action( 'wp_print_styles', 'print_emoji_styles' );
105
		remove_action( 'admin_print_styles', 'print_emoji_styles' );
106
107
		remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
108
		remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
109
		remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
110
111
		add_filter( 'tiny_mce_plugins', array( $this, 'disable_emojis_tinymce' ) );
112
		add_filter( 'wp_resource_hints', array( $this, 'disable_emojis_remove_dns_prefetch' ), 10, 2 );
113
	}
114
115
	function optimize_jetpack() {
116
117
	}
118
119
	/**
120
	 * Filter function used to remove the tinymce emoji plugin.
121
	 *
122
	 * @param array $plugins
123
	 * @return array Difference betwen the two arrays
124
	 */
125
	function disable_emojis_tinymce( $plugins ) {
126
		if ( is_array( $plugins ) ) {
127
			return array_diff( $plugins, array( 'wpemoji' ) );
128
		} else {
129
			return array();
130
		}
131
	}
132
133
	/**
134
	 * Remove emoji CDN hostname from DNS prefetching hints.
135
	 *
136
	 * @param array $urls URLs to print for resource hints.
137
	 * @param string $relation_type The relation type the URLs are printed for.
138
	 * @return array Difference betwen the two arrays.
139
	 */
140
	function disable_emojis_remove_dns_prefetch( $urls, $relation_type ) {
141
		if ( 'dns-prefetch' == $relation_type ) {
142
			/** This filter is documented in wp-includes/formatting.php */
143
			$emoji_svg_url = apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/2/svg/' );
144
145
			$urls = array_diff( $urls, array( $emoji_svg_url ) );
146
		}
147
148
		return $urls;
149
	}
150
151
	// by default we only inline scripts+styles on first page load for a given user
152
	function set_first_load_cookie() {
153 View Code Duplication
		if ( ! isset( $_COOKIE['jetpack_perf_loaded'] ) ) {
154
			setcookie( 'jetpack_perf_loaded', '1', time() + YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );
155
		}
156
	}
157
158
	// this code essentially sets the default asset location to the footer rather than the head
159
	function send_scripts_to_footer() {
160
		global $wp_scripts;
161
162
		// fetch all deps for head
163
		$wp_scripts->all_deps( $wp_scripts->queue, true, 1 );
164 View Code Duplication
		foreach( $wp_scripts->to_do as $handle ) {
165
			$registration = $wp_scripts->registered[$handle];
166
			if ( $registration->args !== NULL ) {
167
				// skip, this asset has an explicit location
168
				continue;
169
			}
170
171
			$asset_group = apply_filters( 'jetpack_perf_asset_group', 1, $handle );
172
			$registration->args = $asset_group;
173
			$wp_scripts->groups[$handle] = $asset_group;
174
		}
175
	}
176
177
	// scripts that run after CSS <link>s in the header block waiting for the CSS to load
178
	// so we move styles as late as possible in the wp_head action to maximise the chance
179
	// of non-blocking rendering
180
	function move_styles_to_bottom_of_header() {
181
		remove_action( 'wp_head', 'wp_print_styles', 8 );
182
		add_action( 'wp_head', 'wp_print_styles', 999 );
183
	}
184
185
	function set_asset_groups( $group, $handle ) {
186
		// force jquery into header, everything else can go in footer unless filtered elsewhere
187
		if ( in_array( $handle, array( 'jquery-core', 'jquery-migrate', 'jquery' ) ) ) {
188
			return 0;
189
		}
190
191
		return $group;
192
	}
193
194
	/** FILTERS **/
195
	public function remove_external_font_scripts( $should_remove, $handle, $asset_url ) {
196
		$font_script_url = 'http://use.typekit.com/';
197
		return strncmp( $asset_url, $font_script_url, strlen( $font_script_url ) ) === 0;
198
	}
199
200
	public function remove_external_font_styles( $should_remove, $handle, $asset_url ) {
201
		$font_url = 'https://fonts.googleapis.com';
202
		return strncmp( $asset_url, $font_url, strlen( $font_url ) ) === 0;
203
	}
204
205
	/** SCRIPTS **/
206 View Code Duplication
	public function filter_inline_scripts( $src, $handle ) {
207
		global $wp_scripts;
208
209
		if ( is_admin() || ! isset( $wp_scripts->registered[$handle] ) ) {
210
			return $src;
211
		}
212
213
		$script = $wp_scripts->registered[$handle];
214
215
		// reset src to empty - can't return empty string though because then it skips rendering the tag
216
		if ( $this->should_inline_script( $script ) ) {
217
			return '#';
218
		}
219
220
		return $src;
221
	}
222
223
	public function print_inline_scripts( $tag, $handle, $src ) {
224
		global $wp_scripts;
225
226
		if ( is_admin() || ! isset( $wp_scripts->registered[$handle] ) ) {
227
			return $tag;
228
		}
229
230
		$script = $wp_scripts->registered[$handle];
231
232
		if ( $this->should_remove_script( $script ) ) {
233
			return '';
234
		}
235
236
		if ( $this->should_inline_script( $script ) ) {
237
			$label = '<!-- ' . $script->src . ' -->';
238
			// base64-encoding a script into the src URL only makes sense if we intend to async or defer it
239
			if ( $this->should_defer_script( $script ) ) {
240
				$tag = $label . '<script defer type="text/javascript" src="data:text/javascript;base64,' . base64_encode( file_get_contents( $script->extra['jetpack-inline-file'] ) ) . '"></script>';
241
			} elseif ( $this->should_async_script( $script ) ) {
242
				$tag = $label . '<script async type="text/javascript" src="data:text/javascript;base64,' . base64_encode( file_get_contents( $script->extra['jetpack-inline-file'] ) ) . '"></script>';
243
			} else {
244
				$tag = $label . '<script type="text/javascript">' . file_get_contents( $script->extra['jetpack-inline-file'] ) . '</script>';
245
			}
246
		} else {
247
			if ( $this->should_defer_script( $script ) ) {
248
				$tag = preg_replace( '/<script /', '<script defer ', $tag );
249
			} elseif ( $this->should_async_script( $script ) ) {
250
				$tag = preg_replace( '/<script /', '<script async ', $tag );
251
			}
252
		}
253
254
		return $tag;
255
	}
256
257 View Code Duplication
	private function should_async_script( $script ) {
258
		global $wp_scripts;
259
260
		// explicitly in the header (scripts aren't affected much by async)
261
		$should_async_script = $script->args === 0;
262
263
		// only make scripts async if nothing depends on them
264
		foreach ( $wp_scripts->to_do as $other_script_handle ) {
265
			$other_script = $wp_scripts->registered[ $other_script_handle ];
266
			if ( in_array( $script->handle, $other_script->deps ) ) {
267
				$should_async_script = false;
268
				break;
269
			}
270
		}
271
272
		// you can override this logic by setting jetpack-async
273
		$should_async_script = $should_async_script || ( isset( $script->extra['jetpack-async'] ) && $script->extra['jetpack-async'] );
274
		return $this->async_scripts && apply_filters( 'jetpack_perf_async_script', $should_async_script, $script->handle, $script->src );
275
	}
276
277 View Code Duplication
	private function should_defer_script( $script ) {
278
		global $wp_scripts;
279
280
		// if it's explicitly not in the footer, or we have Jetpack Defer set, and has no dependencies
281
		$should_defer_script = $script->args === 0;
282
283
		// only make scripts deferred if nothing depends on them
284
		foreach ( $wp_scripts->to_do as $other_script_handle ) {
285
			$other_script = $wp_scripts->registered[ $other_script_handle ];
286
			if ( in_array( $script->handle, $other_script->deps ) ) {
287
				$should_defer_script = false;
288
				break;
289
			}
290
		}
291
292
		$should_defer_script = $should_defer_script || ( isset( $script->extra['jetpack-defer'] ) && $script->extra['jetpack-defer'] );
293
		return $this->defer_scripts && apply_filters( 'jetpack_perf_defer_script', $should_defer_script, $script->handle, $script->src );
294
	}
295
296
	private function should_remove_script( $script ) {
297
		return $this->should_remove_asset( 'jetpack_perf_remove_script', $script );
298
	}
299
300
	private function should_inline_script( $script ) {
301
		return ( $this->inline_scripts_and_styles || $this->inline_always ) && $this->should_inline_asset( 'jetpack_perf_inline_script', $script );
302
	}
303
304
	/** STYLES **/
305 View Code Duplication
	public function filter_inline_styles( $src, $handle ) {
306
		global $wp_scripts;
307
308
		if ( is_admin() || ! isset( $wp_scripts->registered[$handle] ) ) {
309
			return $src;
310
		}
311
312
		$style = $wp_scripts->registered[$handle];
313
314
		if ( $this->should_inline_style( $style ) ) {
315
			return '#';
316
		}
317
318
		return $src;
319
	}
320
321
	public function print_inline_styles( $tag, $handle, $href, $media ) {
322
		global $wp_styles;
323
324
		if ( is_admin() || ! isset( $wp_styles->registered[$handle] ) ) {
325
			return $tag;
326
		}
327
328
		$style = $wp_styles->registered[$handle];
329
330
		if ( $this->should_inline_style( $style ) ) {
331
			$label = '<!-- ' . $style->src . ' -->';
332
			$css = $this->fix_css_urls( file_get_contents( $style->extra['jetpack-inline-file'] ), $style->src );
333
			return "$label<style type='text/css' media='$media'>$css</style>";
334
		}
335
336
		if ( $this->should_remove_style( $style ) ) {
337
			return '';
338
		}
339
340
		return $tag;
341
	}
342
343
	public function fix_css_urls( $css, $css_url ) {
344
		$base = trailingslashit( dirname( $css_url ) );
345
		$base = str_replace( site_url(), '', $base );
346
347
		// reject absolute site_url
348
		if ( 'http' === substr( $base, 0, 4 ) ) {
349
			return $css;
350
		}
351
		return preg_replace_callback( '/url[\s]*\([\s]*["\']?[\s]*(?!https?:\/\/)(?!data:)(?!#)([^\)"\']*)["\']?\)/i', function( $matches ) use ( $base ) {
352
			// TODO: embed data-encoded file, for files smaller than certain size?
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
353
			return 'url('.$this->rel2abspath( $matches[1], $base ).')';
354
		}, $css );
355
	}
356
357
	// see: http://stackoverflow.com/questions/4444475/transfrom-relative-path-into-absolute-url-using-php
358
	private function rel2abspath( $rel, $path) {
359
		/* remove non-directory element from path */
360
		$path = preg_replace( '#/[^/]*$#', '', $path );
361
362
		/* destroy path if relative url points to root */
363
		if( $rel[0] == '/' )
364
			$path = '';
365
366
		/* dirty absolute URL */
367
		$abs = '';
368
369
		$abs .= $path . '/' . $rel;
370
371
		/* replace '//' or '/./' or '/foo/../' with '/' */
372
		$re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
373
		for( $n=1; $n>0; $abs = preg_replace( $re, '/', $abs, -1, $n ) ) {}
0 ignored issues
show
Unused Code introduced by
This for loop is empty and can be removed.

This check looks for for loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

Consider removing the loop.

Loading history...
374
375
		/* absolute path is ready! */
376
		return $abs;
377
	}
378
379
	private function should_inline_style( $style ) {
380
		return ( $this->inline_scripts_and_styles || $this->inline_always ) && $this->should_inline_asset( 'jetpack_perf_inline_style', $style );
381
	}
382
383
	private function should_remove_style( $style ) {
384
		return $this->should_remove_asset( 'jetpack_perf_remove_style', $style );
385
	}
386
387
	/** shared code **/
388
389
	private function should_inline_asset( $filter, $dependency ) {
390
		// inline anything local, with a src starting with /, or starting with site_url
391
		$site_url = site_url();
392
393
		$is_local_url = ( strncmp( $dependency->src, '/', 1 ) === 0 && strncmp( $dependency->src, '//', 2 ) !== 0 )
394
			|| strpos( $dependency->src, $site_url ) === 0;
395
396
		if ( $is_local_url && ! isset( $dependency->extra['jetpack-inline'] ) ) {
397
			$dependency->extra['jetpack-inline'] = true;
398
399
			$path = untrailingslashit( ABSPATH ) . str_replace( $site_url, '', $dependency->src );
400
401
			if ( ! file_exists( $path ) ) {
402
				$path = str_replace('/', DIRECTORY_SEPARATOR, str_replace( $site_url, '', $dependency->src ));
403
404
				$prefix = explode( DIRECTORY_SEPARATOR, untrailingslashit( WP_CONTENT_DIR ) );
405
				$prefix = array_slice( $prefix, 0, array_search( $path[1], $prefix ) - 1 );
406
407
				$path = implode( DIRECTORY_SEPARATOR, $prefix ) . $path;
408
			}
409
410
			$dependency->extra['jetpack-inline-file'] = $path;
411
		}
412
413
		// early exit if the file doesn't exist or is too large
414
		if ( ! isset( $dependency->extra['jetpack-inline-file'] )
415
			||
416
			! file_exists( $dependency->extra['jetpack-inline-file'] ) ) {
417
				return false;
418
		}
419
420
		// only inline if we don't have a conditional
421
		$should_inline = ! isset( $dependency->extra['conditional'] )
422
			&& isset( $dependency->extra['jetpack-inline'] )
423
			&& $dependency->extra['jetpack-inline']
424
			&& filesize( $dependency->extra['jetpack-inline-file'] ) < self::INLINE_ASSET_MAX_SIZE_BYTES;
425
426
		return apply_filters( $filter, $should_inline, $dependency->handle, $dependency->src );
427
	}
428
429
	private function should_remove_asset( $filter, $dependency ) {
430
		return apply_filters( $filter, false, $dependency->handle, $dependency->src );
431
	}
432
433
	/**
434
	 * if inline assets are enabled, renders inline
435
	 * TODO: enable this just for certain paths/patterns/filetypes
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
436
	 * This is actually currently unused
437
	 */
438
	 public function register_inline_script( $handle, $file, $plugin_file, $deps = false, $ver = false, $in_footer = false ) {
439
		$registered = wp_register_script( $handle, plugins_url( $file, $plugin_file ), $deps, $ver, $in_footer );
440
441 View Code Duplication
		if ( $registered ) {
442
			$file_full_path = dirname( $plugin_file ) . '/' . $file;
443
			wp_script_add_data( $handle, 'jetpack-inline', true );
444
			wp_script_add_data( $handle, 'jetpack-inline-file', $file_full_path );
445
		}
446
447
		return $registered;
448
	}
449
450
	/**
451
	 * if inline assets are enabled, renders inline
452
	 * TODO: enable this just for certain paths/patterns/filetypes
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
453
	 * This is actually currently unused
454
	 */
455
	public function register_inline_style( $handle, $file, $plugin_file, $deps = array(), $ver = false, $media = 'all' ) {
456
		$registered = wp_register_style( $handle, plugins_url( $file, $plugin_file ), $deps, $ver, $media );
457
458 View Code Duplication
		if ( $registered ) {
459
			$file_full_path = dirname( $plugin_file ) . '/' . $file;
460
			wp_style_add_data( $handle, 'jetpack-inline', true );
461
			wp_style_add_data( $handle, 'jetpack-inline-file', $file_full_path );
462
		}
463
	}
464
}
465