=' ); } elseif ( ! empty( $version_requirements['wp'] ) ) { $version_available = version_compare( $wp_version, $version_requirements['wp'], '>=' ); } if ( ! $version_available ) { self::set_extension_unavailable( $slug, 'incorrect_gutenberg_version', array( 'required_feature' => $slug, 'required_version' => $version_requirements, 'current_version' => array( 'wp' => $wp_version, 'gutenberg' => defined( 'GUTENBERG_VERSION' ) ? GUTENBERG_VERSION : null, ), ) ); } return $version_available; } /** * Prepend the 'jetpack/' prefix to a block name * * @param string $block_name The block name. * * @return string The prefixed block name. */ private static function prepend_block_prefix( $block_name ) { return 'jetpack/' . $block_name; } /** * Remove the 'jetpack/' or jetpack-' prefix from an extension name * * @param string $extension_name The extension name. * * @return string The unprefixed extension name. */ public static function remove_extension_prefix( $extension_name ) { if ( 0 === strpos( $extension_name, 'jetpack/' ) || 0 === strpos( $extension_name, 'jetpack-' ) ) { return substr( $extension_name, strlen( 'jetpack/' ) ); } return $extension_name; } /** * Whether two arrays share at least one item * * @param array $a An array. * @param array $b Another array. * * @return boolean True if $a and $b share at least one item */ protected static function share_items( $a, $b ) { return count( array_intersect( $a, $b ) ) > 0; } /** * Register a plugin * * @deprecated 7.1.0 Use Jetpack_Gutenberg::set_extension_available() instead * * @param string $slug Slug of the plugin. */ public static function register_plugin( $slug ) { _deprecated_function( __METHOD__, '7.1', 'Jetpack_Gutenberg::set_extension_available' ); self::set_extension_available( $slug ); } /** * Set a (non-block) extension as available * * @param string $slug Slug of the extension. */ public static function set_extension_available( $slug ) { self::$availability[ self::remove_extension_prefix( $slug ) ] = true; } /** * Set the reason why an extension (block or plugin) is unavailable * * @param string $slug Slug of the extension. * @param string $reason A string representation of why the extension is unavailable. * @param array $details A free-form array containing more information on why the extension is unavailable. */ public static function set_extension_unavailable( $slug, $reason, $details = array() ) { if ( // Extensions that require a plan may be eligible for upgrades. 'missing_plan' === $reason && ( /** * Filter 'jetpack_block_editor_enable_upgrade_nudge' with `true` to enable or `false` * to disable paid feature upgrade nudges in the block editor. * * When this is changed to default to `true`, you should also update `modules/memberships/class-jetpack-memberships.php` * See https://github.com/Automattic/jetpack/pull/13394#pullrequestreview-293063378 * * @since 7.7.0 * * @param boolean */ ! apply_filters( 'jetpack_block_editor_enable_upgrade_nudge', false ) /** This filter is documented in _inc/lib/admin-pages/class.jetpack-react-page.php */ || ! apply_filters( 'jetpack_show_promotions', true ) ) ) { // The block editor may apply an upgrade nudge if `missing_plan` is the reason. // Add a descriptive suffix to disable behavior but provide informative reason. $reason .= '__nudge_disabled'; } self::$availability[ self::remove_extension_prefix( $slug ) ] = array( 'reason' => $reason, 'details' => $details, ); } /** * Set the reason why an extension (block or plugin) is unavailable * * @deprecated 7.1.0 Use set_extension_unavailable() instead * * @param string $slug Slug of the extension. * @param string $reason A string representation of why the extension is unavailable. */ public static function set_extension_unavailability_reason( $slug, $reason ) { _deprecated_function( __METHOD__, '7.1', 'Jetpack_Gutenberg::set_extension_unavailable' ); self::set_extension_unavailable( $slug, $reason ); } /** * Set up a list of allowed block editor extensions * * @return void */ public static function init() { if ( ! self::should_load() ) { return; } /** * Alternative to `JETPACK_BETA_BLOCKS`, set to `true` to load Beta Blocks. * * @since 6.9.0 * * @param boolean */ if ( apply_filters( 'jetpack_load_beta_blocks', false ) ) { Constants::set_constant( 'JETPACK_BETA_BLOCKS', true ); } /** * Alternative to `JETPACK_EXPERIMENTAL_BLOCKS`, set to `true` to load Experimental Blocks. * * @since 8.4.0 * * @param boolean */ if ( apply_filters( 'jetpack_load_experimental_blocks', false ) ) { Constants::set_constant( 'JETPACK_EXPERIMENTAL_BLOCKS', true ); } /** * Filter the list of block editor extensions that are available through Jetpack. * * @since 7.0.0 * * @param array */ self::$extensions = apply_filters( 'jetpack_set_available_extensions', self::get_available_extensions() ); /** * Filter the list of block editor plugins that are available through Jetpack. * * @deprecated 7.0.0 Use jetpack_set_available_extensions instead * * @since 6.8.0 * * @param array */ self::$extensions = apply_filters( 'jetpack_set_available_blocks', self::$extensions ); /** * Filter the list of block editor plugins that are available through Jetpack. * * @deprecated 7.0.0 Use jetpack_set_available_extensions instead * * @since 6.9.0 * * @param array */ self::$extensions = apply_filters( 'jetpack_set_available_plugins', self::$extensions ); } /** * Resets the class to its original state * * Used in unit tests * * @return void */ public static function reset() { self::$extensions = array(); self::$availability = array(); self::$cached_availability = null; } /** * Return the Gutenberg extensions (blocks and plugins) directory * * @return string The Gutenberg extensions directory */ public static function get_blocks_directory() { /** * Filter to select Gutenberg blocks directory * * @since 6.9.0 * * @param string default: '_inc/blocks/' */ return apply_filters( 'jetpack_blocks_directory', '_inc/blocks/' ); } /** * Checks for a given .json file in the blocks folder. * * @param string $preset The name of the .json file to look for. * * @return bool True if the file is found. */ public static function preset_exists( $preset ) { return file_exists( JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $preset . '.json' ); } /** * Decodes JSON loaded from a preset file in the blocks folder * * @param string $preset The name of the .json file to load. * * @return mixed Returns an object if the file is present, or false if a valid .json file is not present. */ public static function get_preset( $preset ) { return json_decode( // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents file_get_contents( JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $preset . '.json' ) ); } /** * Returns a list of Jetpack Gutenberg extensions (blocks and plugins), based on index.json * * @return array A list of blocks: eg [ 'publicize', 'markdown' ] */ public static function get_jetpack_gutenberg_extensions_allowed_list() { $preset_extensions_manifest = self::preset_exists( 'index' ) ? self::get_preset( 'index' ) : (object) array(); $blocks_variation = self::blocks_variation(); return self::get_extensions_preset_for_variation( $preset_extensions_manifest, $blocks_variation ); } /** * Returns a list of Jetpack Gutenberg extensions (blocks and plugins), based on index.json * * @deprecated 8.7.0 Use get_jetpack_gutenberg_extensions_allowed_list() * * @return array A list of blocks: eg [ 'publicize', 'markdown' ] */ public static function get_jetpack_gutenberg_extensions_whitelist() { _deprecated_function( __FUNCTION__, 'jetpack-8.7.0', 'Jetpack_Gutenberg::get_jetpack_gutenberg_extensions_allowed_list' ); return self::get_jetpack_gutenberg_extensions_allowed_list(); } /** * Returns a diff from a combined list of allowed extensions and extensions determined to be excluded * * @param array $allowed_extensions An array of allowed extensions. * * @return array A list of blocks: eg array( 'publicize', 'markdown' ) */ public static function get_available_extensions( $allowed_extensions = null ) { $exclusions = get_option( 'jetpack_excluded_extensions', array() ); $allowed_extensions = is_null( $allowed_extensions ) ? self::get_jetpack_gutenberg_extensions_allowed_list() : $allowed_extensions; return array_diff( $allowed_extensions, $exclusions ); } /** * Return true if the extension has been registered and there's nothing in the availablilty array. * * @param string $extension The name of the extension. * * @return bool whether the extension has been registered and there's nothing in the availablilty array. */ public static function is_registered_and_no_entry_in_availability( $extension ) { return self::is_registered( 'jetpack/' . $extension ) && ! isset( self::$availability[ $extension ] ); } /** * Return true if the extension has a true entry in the availablilty array. * * @param string $extension The name of the extension. * * @return bool whether the extension has a true entry in the availablilty array. */ public static function is_available( $extension ) { return isset( self::$availability[ $extension ] ) && true === self::$availability[ $extension ]; } /** * Get the availability of each block / plugin, or return the cached availability * if it has already been calculated. Avoids re-registering extensions when not * necessary. * * @return array A list of block and plugins and their availability status. */ public static function get_cached_availability() { if ( null === self::$cached_availability ) { self::$cached_availability = self::get_availability(); } return self::$cached_availability; } /** * Get availability of each block / plugin. * * @return array A list of block and plugins and their availablity status */ public static function get_availability() { /** * Fires before Gutenberg extensions availability is computed. * * In the function call you supply, use `Blocks::jetpack_register_block()` to set a block as available. * Alternatively, use `Jetpack_Gutenberg::set_extension_available()` (for a non-block plugin), and * `Jetpack_Gutenberg::set_extension_unavailable()` (if the block or plugin should not be registered * but marked as unavailable). * * @since 7.0.0 */ do_action( 'jetpack_register_gutenberg_extensions' ); $available_extensions = array(); foreach ( self::$extensions as $extension ) { $is_available = self::is_registered_and_no_entry_in_availability( $extension ) || self::is_available( $extension ); $available_extensions[ $extension ] = array( 'available' => $is_available, ); if ( ! $is_available ) { $reason = isset( self::$availability[ $extension ] ) ? self::$availability[ $extension ]['reason'] : 'missing_module'; $details = isset( self::$availability[ $extension ] ) ? self::$availability[ $extension ]['details'] : array(); $available_extensions[ $extension ]['unavailable_reason'] = $reason; $available_extensions[ $extension ]['details'] = $details; } } return $available_extensions; } /** * Check if an extension/block is already registered * * @since 7.2 * * @param string $slug Name of extension/block to check. * * @return bool */ public static function is_registered( $slug ) { return WP_Block_Type_Registry::get_instance()->is_registered( $slug ); } /** * Check if Gutenberg editor is available * * @since 6.7.0 * * @return bool */ public static function is_gutenberg_available() { return true; } /** * Check whether conditions indicate Gutenberg Extensions (blocks and plugins) should be loaded * * Loading blocks and plugins is enabled by default and may be disabled via filter: * add_filter( 'jetpack_gutenberg', '__return_false' ); * * @since 6.9.0 * * @return bool */ public static function should_load() { if ( ! Jetpack::is_connection_ready() && ! ( new Status() )->is_offline_mode() ) { return false; } /** * Filter to disable Gutenberg blocks * * @since 6.5.0 * * @param bool true Whether to load Gutenberg blocks */ return (bool) apply_filters( 'jetpack_gutenberg', true ); } /** * Only enqueue block assets when needed. * * @param string $type Slug of the block. * @param array $script_dependencies Script dependencies. Will be merged with automatically * detected script dependencies from the webpack build. * * @return void */ public static function load_assets_as_required( $type, $script_dependencies = array() ) { if ( is_admin() ) { // A block's view assets will not be required in wp-admin. return; } $type = sanitize_title_with_dashes( $type ); self::load_styles_as_required( $type ); self::load_scripts_as_required( $type, $script_dependencies ); } /** * Only enqueue block sytles when needed. * * @param string $type Slug of the block. * * @since 7.2.0 * * @return void */ public static function load_styles_as_required( $type ) { if ( is_admin() ) { // A block's view assets will not be required in wp-admin. return; } // Enqueue styles. $style_relative_path = self::get_blocks_directory() . $type . '/view' . ( is_rtl() ? '.rtl' : '' ) . '.css'; if ( self::block_has_asset( $style_relative_path ) ) { $style_version = self::get_asset_version( $style_relative_path ); $view_style = plugins_url( $style_relative_path, JETPACK__PLUGIN_FILE ); // If this is a customizer preview, render the style directly to the preview after autosave. // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( is_customize_preview() && ! empty( $_GET['customize_autosaved'] ) ) { // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet echo ''; } else { wp_enqueue_style( 'jetpack-block-' . $type, $view_style, array(), $style_version ); } } } /** * Only enqueue block scripts when needed. * * @param string $type Slug of the block. * @param array $script_dependencies Script dependencies. Will be merged with automatically * detected script dependencies from the webpack build. * * @since 7.2.0 * * @return void */ public static function load_scripts_as_required( $type, $script_dependencies = array() ) { if ( is_admin() ) { // A block's view assets will not be required in wp-admin. return; } // Enqueue script. $script_relative_path = self::get_blocks_directory() . $type . '/view.js'; $script_deps_path = JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $type . '/view.asset.php'; $script_dependencies[] = 'wp-polyfill'; if ( file_exists( $script_deps_path ) ) { $asset_manifest = include $script_deps_path; $script_dependencies = array_unique( array_merge( $script_dependencies, $asset_manifest['dependencies'] ) ); } if ( ! Blocks::is_amp_request() && self::block_has_asset( $script_relative_path ) ) { $script_version = self::get_asset_version( $script_relative_path ); $view_script = plugins_url( $script_relative_path, JETPACK__PLUGIN_FILE ); // Enqueue dependencies. wp_enqueue_script( 'jetpack-block-' . $type, $view_script, $script_dependencies, $script_version, false ); // If this is a customizer preview, enqueue the dependencies and render the script directly to the preview after autosave. // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( is_customize_preview() && ! empty( $_GET['customize_autosaved'] ) ) { // The Map block is dependent on wp-element, and it doesn't appear to to be possible to load // this dynamically into the customizer iframe currently. if ( 'map' === $type ) { echo '