Completed
Push — master ( 506120...ae76ed )
by Jon
524:15 queued 472:59
created

Jetpack_Gutenberg::should_load_blocks()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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