Completed
Push — remove/vr-gutenblock ( 84993f...ae61cd )
by Bernhard
385:01 queued 377:44
created

Jetpack_Gutenberg::enqueue_block_editor_assets()   B

Complexity

Conditions 6
Paths 11

Size

Total Lines 59

Duplication

Lines 20
Ratio 33.9 %

Importance

Changes 0
Metric Value
cc 6
nc 11
nop 0
dl 20
loc 59
rs 8.2723
c 0
b 0
f 0

How to fix   Long Method   

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
/**
4
 * General Gutenberg editor specific functionality
5
 */
6
class Jetpack_Gutenberg {
7
	/**
8
	 * Check if Gutenberg editor is available
9
	 *
10
	 * @since 6.7.0
11
	 *
12
	 * @return bool
13
	 */
14
	public static function is_gutenberg_available() {
15
		return function_exists( 'register_block_type' );
16
	}
17
18
	/**
19
	 * Load Gutenberg assets
20
	 *
21
	 * @since 6.7.0
22
	 *
23
	 * @return void
24
	 */
25
	public static function enqueue_block_assets() {
26
		if ( ! self::should_load_blocks() ) {
27
			return;
28
		}
29
30
		$rtl = is_rtl() ? '.rtl' : '';
31
32
		/**
33
		 * Filter to enable serving blocks via CDN
34
		 *
35
		 * CDN cache is busted once a day or when Jetpack version changes. To customize it:
36
		 *   add_filter( 'jetpack_gutenberg_cdn_cache_buster', function( $version ) { return time(); }, 10, 1 );
37
		 *
38
		 * @since 6.5.0
39
		 *
40
		 * @param bool false Whether to load Gutenberg blocks from CDN
41
		 */
42 View Code Duplication
		if ( apply_filters( 'jetpack_gutenberg_cdn', false ) ) {
43
			$cdn_base    = 'https://s0.wp.com/wp-content/mu-plugins/jetpack/_inc/blocks';
44
			$view_script = "$cdn_base/view.js";
45
			$view_style  = "$cdn_base/view$rtl.css";
46
47
			/**
48
			 * Filter to modify cache busting for Gutenberg block assets loaded from CDN
49
			 *
50
			 * @since 6.5.0
51
			 *
52
			 * @param string
53
			 */
54
			$version = apply_filters( 'jetpack_gutenberg_cdn_cache_buster', sprintf( '%s-%s', gmdate( 'd-m-Y' ), JETPACK__VERSION ) );
55
		} else {
56
			$view_script = plugins_url( '_inc/blocks/view.js', JETPACK__PLUGIN_FILE );
57
			$view_style  = plugins_url( "_inc/blocks/view$rtl.css", JETPACK__PLUGIN_FILE );
58
			$version     = Jetpack::is_development_version() && file_exists( JETPACK__PLUGIN_DIR . '_inc/blocks/view.js' )
59
				? filemtime( JETPACK__PLUGIN_DIR . '_inc/blocks/view.js' )
60
				: JETPACK__VERSION;
61
		}
62
63
		wp_enqueue_script(
64
			'jetpack-blocks-view',
65
			$view_script,
66
			array(
67
				'wp-element',
68
				'wp-i18n',
69
			),
70
			$version
71
		);
72
		wp_enqueue_style( 'jetpack-blocks-view', $view_style, array(), $version );
73
	}
74
75
	/**
76
	 * Load Gutenberg editor assets
77
	 *
78
	 * @since 6.7.0
79
	 *
80
	 * @return void
81
	 */
82
	public static function enqueue_block_editor_assets() {
83
		if ( ! self::should_load_blocks() ) {
84
			return;
85
		}
86
87
		$rtl = is_rtl() ? '.rtl' : '';
88
89
		/** This filter is already documented above */
90 View Code Duplication
		if ( apply_filters( 'jetpack_gutenberg_cdn', false ) ) {
91
			$cdn_base      = 'https://s0.wp.com/wp-content/mu-plugins/jetpack/_inc/blocks';
92
			$editor_script = "$cdn_base/editor.js";
93
			$editor_style  = "$cdn_base/editor$rtl.css";
94
95
			/** This filter is already documented above */
96
			$version = apply_filters( 'jetpack_gutenberg_cdn_cache_buster', sprintf( '%s-%s', gmdate( 'd-m-Y' ), JETPACK__VERSION ) );
97
		} else {
98
			$editor_script = plugins_url( '_inc/blocks/editor.js', JETPACK__PLUGIN_FILE );
99
			$editor_style  = plugins_url( "_inc/blocks/editor$rtl.css", JETPACK__PLUGIN_FILE );
100
			$version       = Jetpack::is_development_version() && file_exists( JETPACK__PLUGIN_DIR . '_inc/blocks/editor.js' )
101
				? filemtime( JETPACK__PLUGIN_DIR . '_inc/blocks/editor.js' )
102
				: JETPACK__VERSION;
103
		}
104
105
		wp_enqueue_script(
106
			'jetpack-blocks-editor',
107
			$editor_script,
108
			array(
109
				'lodash',
110
				'wp-api-fetch',
111
				'wp-blocks',
112
				'wp-components',
113
				'wp-compose',
114
				'wp-data',
115
				'wp-date',
116
				'wp-editor',
117
				'wp-element',
118
				'wp-hooks',
119
				'wp-i18n',
120
				'wp-keycodes',
121
				'wp-plugins',
122
				'wp-token-list',
123
				'wp-url',
124
			),
125
			$version
126
		);
127
128
		wp_localize_script(
129
			'jetpack-blocks-editor',
130
			'Jetpack_Block_Assets_Base_Url',
131
			plugins_url( '_inc/blocks/', JETPACK__PLUGIN_FILE )
132
		);
133
134
		wp_add_inline_script(
135
			'wp-i18n',
136
			'wp.i18n.setLocaleData( ' . Jetpack::get_i18n_data_json() . ', \'jetpack\' );'
137
		);
138
139
		wp_enqueue_style( 'jetpack-blocks-editor', $editor_style, array(), $version );
140
	}
141
142
	/**
143
	 * Check whether conditions indicate Gutenberg blocks should be loaded
144
	 *
145
	 * Loading blocks is enabled by default and may be disabled via filter:
146
	 *   add_filter( 'jetpack_gutenberg', '__return_false' );
147
	 *
148
	 * @since 6.7.0
149
	 *
150
	 * @return bool
151
	 */
152
	public static function should_load_blocks() {
153
		if ( ! Jetpack::is_active() ) {
154
			return false;
155
		}
156
157
		/**
158
		 * Filter to disable Gutenberg blocks
159
		 *
160
		 * @since 6.5.0
161
		 *
162
		 * @param bool true Whether to load Gutenberg blocks
163
		 */
164
		return (bool) apply_filters( 'jetpack_gutenberg', true );
165
	}
166
}
167