Completed
Push — iterate/calypsoify ( 2ba5fb...4d78ff )
by George
146:23 queued 138:29
created

Jetpack_Gutenberg::enqueue_block_assets()   B

Complexity

Conditions 6
Paths 11

Size

Total Lines 52

Duplication

Lines 20
Ratio 38.46 %

Importance

Changes 0
Metric Value
cc 6
nc 11
nop 0
dl 20
loc 52
rs 8.425
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
73
		Jetpack::setup_wp_i18n_locale_data();
74
75
		wp_enqueue_style( 'jetpack-blocks-view', $view_style, array(), $version );
76
	}
77
78
	/**
79
	 * Load Gutenberg editor assets
80
	 *
81
	 * @since 6.7.0
82
	 *
83
	 * @return void
84
	 */
85
	public static function enqueue_block_editor_assets() {
86
		if ( ! self::should_load_blocks() ) {
87
			return;
88
		}
89
90
		$rtl = is_rtl() ? '.rtl' : '';
91
92
		/** This filter is already documented above */
93 View Code Duplication
		if ( apply_filters( 'jetpack_gutenberg_cdn', false ) ) {
94
			$cdn_base      = 'https://s0.wp.com/wp-content/mu-plugins/jetpack/_inc/blocks';
95
			$editor_script = "$cdn_base/editor.js";
96
			$editor_style  = "$cdn_base/editor$rtl.css";
97
98
			/** This filter is already documented above */
99
			$version = apply_filters( 'jetpack_gutenberg_cdn_cache_buster', sprintf( '%s-%s', gmdate( 'd-m-Y' ), JETPACK__VERSION ) );
100
		} else {
101
			$editor_script = plugins_url( '_inc/blocks/editor.js', JETPACK__PLUGIN_FILE );
102
			$editor_style  = plugins_url( "_inc/blocks/editor$rtl.css", JETPACK__PLUGIN_FILE );
103
			$version       = Jetpack::is_development_version() && file_exists( JETPACK__PLUGIN_DIR . '_inc/blocks/editor.js' )
104
				? filemtime( JETPACK__PLUGIN_DIR . '_inc/blocks/editor.js' )
105
				: JETPACK__VERSION;
106
		}
107
108
		wp_enqueue_script(
109
			'jetpack-blocks-editor',
110
			$editor_script,
111
			array(
112
				'lodash',
113
				'wp-api-fetch',
114
				'wp-blocks',
115
				'wp-components',
116
				'wp-compose',
117
				'wp-data',
118
				'wp-date',
119
				'wp-edit-post',
120
				'wp-editor',
121
				'wp-element',
122
				'wp-hooks',
123
				'wp-i18n',
124
				'wp-keycodes',
125
				'wp-plugins',
126
				'wp-token-list',
127
				'wp-url',
128
			),
129
			$version
130
		);
131
132
		wp_localize_script(
133
			'jetpack-blocks-editor',
134
			'Jetpack_Block_Assets_Base_Url',
135
			plugins_url( '_inc/blocks/', JETPACK__PLUGIN_FILE )
136
		);
137
138
		$jp_react_page = new Jetpack_React_Page();
139
		wp_localize_script(
140
			'jetpack-blocks-editor',
141
			'Jetpack_Initial_State',
142
			$jp_react_page->get_initial_state()
143
		);
144
145
		Jetpack::setup_wp_i18n_locale_data();
146
147
		wp_enqueue_style( 'jetpack-blocks-editor', $editor_style, array(), $version );
148
	}
149
150
	/**
151
	 * Check whether conditions indicate Gutenberg blocks should be loaded
152
	 *
153
	 * Loading blocks is enabled by default and may be disabled via filter:
154
	 *   add_filter( 'jetpack_gutenberg', '__return_false' );
155
	 *
156
	 * @since 6.7.0
157
	 *
158
	 * @return bool
159
	 */
160
	public static function should_load_blocks() {
161
		if ( ! Jetpack::is_active() ) {
162
			return false;
163
		}
164
		/**
165
		 * Filter to disable Gutenberg blocks
166
		 *
167
		 * @since 6.5.0
168
		 *
169
		 * @param bool true Whether to load Gutenberg blocks
170
		 */
171
		return (bool) apply_filters( 'jetpack_gutenberg', true );
172
	}
173
}
174