Completed
Push — add/changelog-890 ( a96ab5...bdfacc )
by Jeremy
10:34 queued 02:15
created

Blocks::is_gutenberg_version_available()   B

Complexity

Conditions 9
Paths 8

Size

Total Lines 43

Duplication

Lines 43
Ratio 100 %

Importance

Changes 0
Metric Value
cc 9
nc 8
nop 2
dl 43
loc 43
rs 7.6764
c 0
b 0
f 0
1
<?php
2
/** Blocks package.
3
 *
4
 * @since 9.0.0
5
 *
6
 * This package lifts elements from Jetpack's Jetpack_Gutenberg class.
7
 * It is now an standalone package reusable outside Jetpack.
8
 *
9
 * @package automattic/jetpack-blocks
10
 */
11
12
namespace Automattic\Jetpack;
13
14
use Jetpack_Gutenberg;
15
16
/**
17
 * Register and manage blocks within a plugin. Used to manage block registration, enqueues, and more.
18
 *
19
 * @since 9.0.0
20
 */
21
class Blocks {
22
	/**
23
	 * Wrapper function to safely register a Gutenberg block type
24
	 *
25
	 * @see register_block_type
26
	 * @see Automattic\Jetpack\Blocks::is_gutenberg_version_available
27
	 *
28
	 * @since 9.0.0
29
	 *
30
	 * @param string $slug Slug of the block.
31
	 * @param array  $args {
32
	 *     Arguments that are passed into register_block_type.
33
	 *     See register_block_type for full list of arguments.
34
	 *     Can also include 2 extra arguments not currently supported by register_block_type.
35
	 *
36
	 *     @type array $version_requirements Array containing required Gutenberg version and, if known, the WordPress version that was released with this minimum version.
37
	 *     @type bool  $plan_check           Should we check for a specific plan before registering the block.
38
	 * }
39
	 *
40
	 * @return WP_Block_Type|false The registered block type on success, or false on failure.
41
	 */
42
	public static function jetpack_register_block( $slug, $args = array() ) {
43 View Code Duplication
		if ( 0 !== strpos( $slug, 'jetpack/' ) && ! strpos( $slug, '/' ) ) {
44
			_doing_it_wrong( 'jetpack_register_block', 'Prefix the block with jetpack/ ', 'Jetpack 9.0.0' );
45
			$slug = 'jetpack/' . $slug;
46
		}
47
48 View Code Duplication
		if (
49
			isset( $args['version_requirements'] )
50
			&& ! self::is_gutenberg_version_available( $args['version_requirements'], $slug )
51
		) {
52
			return false;
53
		}
54
55
		// Checking whether block is registered to ensure it isn't registered twice.
56
		if ( self::is_registered( $slug ) ) {
57
			return false;
58
		}
59
60
		$feature_name = self::remove_extension_prefix( $slug );
61
62
		// This is only useful in Jetpack.
63
		if ( class_exists( Jetpack_Gutenberg::class ) ) {
64
			// If the block is dynamic, and a Jetpack block, wrap the render_callback to check availability.
65
			if ( ! empty( $args['plan_check'] ) ) {
66
				if ( isset( $args['render_callback'] ) ) {
67
					$args['render_callback'] = Jetpack_Gutenberg::get_render_callback_with_availability_check( $feature_name, $args['render_callback'] );
68
				}
69
				$method_name = 'set_availability_for_plan';
70
			} else {
71
				$method_name = 'set_extension_available';
72
			}
73
74
			add_action(
75
				'jetpack_register_gutenberg_extensions',
76
				function() use ( $feature_name, $method_name ) {
77
					call_user_func( array( 'Jetpack_Gutenberg', $method_name ), $feature_name );
78
				}
79
			);
80
		}
81
		return register_block_type( $slug, $args );
82
	}
83
84
	/**
85
	 * Check if an extension/block is already registered
86
	 *
87
	 * @since 9.0.0
88
	 *
89
	 * @param string $slug Name of extension/block to check.
90
	 *
91
	 * @return bool
92
	 */
93
	public static function is_registered( $slug ) {
94
		return \WP_Block_Type_Registry::get_instance()->is_registered( $slug );
95
	}
96
97
	/**
98
	 * Remove the 'jetpack/' or jetpack-' prefix from an extension name
99
	 *
100
	 * @since 9.0.0
101
	 *
102
	 * @param string $extension_name The extension name.
103
	 *
104
	 * @return string The unprefixed extension name.
105
	 */
106
	public static function remove_extension_prefix( $extension_name ) {
107 View Code Duplication
		if ( 0 === strpos( $extension_name, 'jetpack/' ) || 0 === strpos( $extension_name, 'jetpack-' ) ) {
108
			return substr( $extension_name, strlen( 'jetpack/' ) );
109
		}
110
		return $extension_name;
111
	}
112
113
	/**
114
	 * Check to see if a minimum version of Gutenberg is available. Because a Gutenberg version is not available in
115
	 * php if the Gutenberg plugin is not installed, if we know which minimum WP release has the required version we can
116
	 * optionally fall back to that.
117
	 *
118
	 * @since 9.0.0
119
	 *
120
	 * @param array  $version_requirements {
121
	 *     An array containing the required Gutenberg version and, if known, the WordPress version that was released with this minimum version.
122
	 *
123
	 *     @type string $gutenberg Gutenberg version.
124
	 *     @type string $wp        Optional. WordPress version.
125
	 * }
126
	 * @param string $slug The slug of the block or plugin that has the Gutenberg version requirement.
127
	 *
128
	 * @return boolean True if the version of Gutenberg required by the block or plugin is available.
129
	 */
130 View Code Duplication
	public static function is_gutenberg_version_available( $version_requirements, $slug ) {
131
		global $wp_version;
132
133
		// Bail if we don't at least have the Gutenberg version requirement, the WP version is optional.
134
		if ( empty( $version_requirements['gutenberg'] ) ) {
135
			return false;
136
		}
137
138
		// If running a local dev build of Gutenberg plugin GUTENBERG_DEVELOPMENT_MODE is set so assume correct version.
139
		if ( defined( 'GUTENBERG_DEVELOPMENT_MODE' ) && GUTENBERG_DEVELOPMENT_MODE ) {
140
			return true;
141
		}
142
143
		$version_available = false;
144
145
		// If running a production build of the Gutenberg plugin then GUTENBERG_VERSION is set, otherwise if WP version
146
		// with required version of Gutenberg is known check that.
147
		if ( defined( 'GUTENBERG_VERSION' ) ) {
148
			$version_available = version_compare( GUTENBERG_VERSION, $version_requirements['gutenberg'], '>=' );
149
		} elseif ( ! empty( $version_requirements['wp'] ) ) {
150
			$version_available = version_compare( $wp_version, $version_requirements['wp'], '>=' );
151
		}
152
153
		if (
154
			! $version_available
155
			&& class_exists( Jetpack_Gutenberg::class ) // This is only useful in Jetpack.
156
		) {
157
			Jetpack_Gutenberg::set_extension_unavailable(
158
				$slug,
159
				'incorrect_gutenberg_version',
160
				array(
161
					'required_feature' => $slug,
162
					'required_version' => $version_requirements,
163
					'current_version'  => array(
164
						'wp'        => $wp_version,
165
						'gutenberg' => defined( 'GUTENBERG_VERSION' ) ? GUTENBERG_VERSION : null,
166
					),
167
				)
168
			);
169
		}
170
171
		return $version_available;
172
	}
173
174
	/**
175
	 * Get CSS classes for a block.
176
	 *
177
	 * @since 9.0.0
178
	 *
179
	 * @param string $slug  Block slug.
180
	 * @param array  $attr  Block attributes.
181
	 * @param array  $extra Potential extra classes you may want to provide.
182
	 *
183
	 * @return string $classes List of CSS classes for a block.
184
	 */
185
	public static function classes( $slug, $attr, $extra = array() ) {
186
		if ( empty( $slug ) ) {
187
			return '';
188
		}
189
190
		// Basic block name class.
191
		$classes = array(
192
			'wp-block-jetpack-' . $slug,
193
		);
194
195
		// Add alignment if provided.
196
		if (
197
			! empty( $attr['align'] )
198
			&& in_array( $attr['align'], array( 'left', 'center', 'right', 'wide', 'full' ), true )
199
		) {
200
			$classes[] = 'align' . $attr['align'];
201
		}
202
203
		// Add custom classes if provided in the block editor.
204
		if ( ! empty( $attr['className'] ) ) {
205
			$classes[] = $attr['className'];
206
		}
207
208
		// Add any extra classes.
209
		if ( is_array( $extra ) && ! empty( $extra ) ) {
210
			$classes = array_merge( $classes, array_filter( $extra ) );
211
		}
212
213
		return implode( ' ', $classes );
214
	}
215
216
	/**
217
	 * Does the page return AMP content.
218
	 *
219
	 * @since 9.0.0
220
	 *
221
	 * @return bool $is_amp_request Are we on an AMP view.
222
	 */
223
	public static function is_amp_request() {
224
		$is_amp_request = ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() );
225
226
		/** This filter is documented in 3rd-party/class.jetpack-amp-support.php */
227
		return apply_filters( 'jetpack_is_amp_request', $is_amp_request );
228
	}
229
}
230