diff options
Diffstat (limited to 'plugins/jetpack/class.jetpack-gutenberg.php')
-rw-r--r-- | plugins/jetpack/class.jetpack-gutenberg.php | 157 |
1 files changed, 116 insertions, 41 deletions
diff --git a/plugins/jetpack/class.jetpack-gutenberg.php b/plugins/jetpack/class.jetpack-gutenberg.php index 82d673e6..aa8f9e50 100644 --- a/plugins/jetpack/class.jetpack-gutenberg.php +++ b/plugins/jetpack/class.jetpack-gutenberg.php @@ -6,6 +6,9 @@ * @package Jetpack */ +use Automattic\Jetpack\Constants; +use Automattic\Jetpack\Status; + /** * Wrapper function to safely register a gutenberg block type * @@ -183,9 +186,38 @@ class Jetpack_Gutenberg { * * @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 ) { - self::$availability[ self::remove_extension_prefix( $slug ) ] = $reason; + 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, + ); } /** @@ -220,7 +252,7 @@ class Jetpack_Gutenberg { * @param boolean */ if ( apply_filters( 'jetpack_load_beta_blocks', false ) ) { - Jetpack_Constants::set_constant( 'JETPACK_BETA_BLOCKS', true ); + Constants::set_constant( 'JETPACK_BETA_BLOCKS', true ); } /** @@ -318,7 +350,7 @@ class Jetpack_Gutenberg { $preset_extensions = isset( $preset_extensions_manifest->production ) ? (array) $preset_extensions_manifest->production : array(); - if ( Jetpack_Constants::is_true( 'JETPACK_BETA_BLOCKS' ) ) { + if ( Constants::is_true( 'JETPACK_BETA_BLOCKS' ) ) { $beta_extensions = isset( $preset_extensions_manifest->beta ) ? (array) $preset_extensions_manifest->beta : array(); return array_unique( array_merge( $preset_extensions, $beta_extensions ) ); } @@ -369,8 +401,10 @@ class Jetpack_Gutenberg { ); if ( ! $is_available ) { - $reason = isset( self::$availability[ $extension ] ) ? self::$availability[ $extension ] : 'missing_module'; + $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; } } @@ -412,7 +446,7 @@ class Jetpack_Gutenberg { * @return bool */ public static function should_load() { - if ( ! Jetpack::is_active() && ! Jetpack::is_development_mode() ) { + if ( ! Jetpack::is_active() && ! ( new Status() )->is_development_mode() ) { return false; } @@ -490,14 +524,14 @@ class Jetpack_Gutenberg { // Enqueue script. $script_relative_path = self::get_blocks_directory() . $type . '/view.js'; - $script_deps_path = JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $type . '/view.deps.json'; - - $script_dependencies = file_exists( $script_deps_path ) - ? json_decode( file_get_contents( $script_deps_path ) ) - : array(); - $script_dependencies = array_merge( $script_dependencies, $dependencies, array( 'wp-polyfill' ) ); + $script_deps_path = JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $type . '/view.asset.php'; + $script_dependencies = array( 'wp-polyfill' ); + if ( file_exists( $script_deps_path ) ) { + $asset_manifest = include $script_deps_path; + $script_dependencies = $asset_manifest['dependencies']; + } - if ( self::block_has_asset( $script_relative_path ) ) { + if ( ( ! class_exists( 'Jetpack_AMP_Support' ) || ! Jetpack_AMP_Support::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 ); wp_enqueue_script( 'jetpack-block-' . $type, $view_script, $script_dependencies, $script_version, false ); @@ -546,18 +580,24 @@ class Jetpack_Gutenberg { return; } + // Required for Analytics. See _inc/lib/admin-pages/class.jetpack-admin-page.php. + if ( ! ( new Status() )->is_development_mode() && Jetpack::is_active() ) { + wp_enqueue_script( 'jp-tracks', '//stats.wp.com/w.js', array(), gmdate( 'YW' ), true ); + } + $rtl = is_rtl() ? '.rtl' : ''; - $beta = Jetpack_Constants::is_true( 'JETPACK_BETA_BLOCKS' ) ? '-beta' : ''; + $beta = Constants::is_true( 'JETPACK_BETA_BLOCKS' ) ? '-beta' : ''; $blocks_dir = self::get_blocks_directory(); $editor_script = plugins_url( "{$blocks_dir}editor{$beta}.js", JETPACK__PLUGIN_FILE ); $editor_style = plugins_url( "{$blocks_dir}editor{$beta}{$rtl}.css", JETPACK__PLUGIN_FILE ); - $editor_deps_path = JETPACK__PLUGIN_DIR . $blocks_dir . "editor{$beta}.deps.json"; - $editor_deps = file_exists( $editor_deps_path ) - ? json_decode( file_get_contents( $editor_deps_path ) ) - : array(); - $editor_deps[] = 'wp-polyfill'; + $editor_deps_path = JETPACK__PLUGIN_DIR . $blocks_dir . "editor{$beta}.asset.php"; + $editor_deps = array( 'wp-polyfill' ); + if ( file_exists( $editor_deps_path ) ) { + $asset_manifest = include $editor_deps_path; + $editor_deps = $asset_manifest['dependencies']; + } $version = Jetpack::is_development_version() && file_exists( JETPACK__PLUGIN_DIR . $blocks_dir . 'editor.js' ) ? filemtime( JETPACK__PLUGIN_DIR . $blocks_dir . 'editor.js' ) @@ -585,6 +625,18 @@ class Jetpack_Gutenberg { plugins_url( $blocks_dir . '/', JETPACK__PLUGIN_FILE ) ); + if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { + $user = wp_get_current_user(); + $user_data = array( + 'userid' => $user->ID, + 'username' => $user->user_login, + ); + $blog_id = get_current_blog_id(); + } else { + $user_data = Jetpack_Tracks_Client::get_connected_user_tracks_identity(); + $blog_id = Jetpack_Options::get_option( 'id', 0 ); + } + wp_localize_script( 'jetpack-blocks-editor', 'Jetpack_Editor_Initial_State', @@ -592,36 +644,17 @@ class Jetpack_Gutenberg { 'available_blocks' => self::get_availability(), 'jetpack' => array( 'is_active' => Jetpack::is_active() ), 'siteFragment' => $site_fragment, + 'tracksUserData' => $user_data, + 'wpcomBlogId' => $blog_id, ) ); - wp_set_script_translations( 'jetpack-blocks-editor', 'jetpack', plugins_url( 'languages/json', JETPACK__PLUGIN_FILE ) ); - - // Adding a filter late to allow every other filter to process the path, including the CDN. - add_filter( 'pre_load_script_translations', array( __CLASS__, 'filter_pre_load_script_translations' ), 1000, 3 ); + wp_set_script_translations( 'jetpack-blocks-editor', 'jetpack' ); wp_enqueue_style( 'jetpack-blocks-editor', $editor_style, array(), $version ); } /** - * A workaround for setting i18n data for WordPress client-side i18n mechanism. - * We are not yet using dotorg language packs for the editor file, so this short-circuits - * the translation loading and feeds our JSON data directly into the translation getter. - * - * @param NULL $null not used. - * @param String $file the file path that is being loaded, ignored. - * @param String $handle the script handle. - * @return NULL|String the translation data only if we're working with our handle. - */ - public static function filter_pre_load_script_translations( $null, $file, $handle ) { - if ( 'jetpack-blocks-editor' !== $handle ) { - return null; - } - - return Jetpack::get_i18n_data_json(); - } - - /** * Some blocks do not depend on a specific module, * and can consequently be loaded outside of the usual modules. * We will look for such modules in the extensions/ directory. @@ -642,4 +675,46 @@ class Jetpack_Gutenberg { } } } + + /** + * Get CSS classes for a block. + * + * @since 7.7.0 + * + * @param string $slug Block slug. + * @param array $attr Block attributes. + * @param array $extra Potential extra classes you may want to provide. + * + * @return string $classes List of CSS classes for a block. + */ + public static function block_classes( $slug = '', $attr, $extra = array() ) { + if ( empty( $slug ) ) { + return ''; + } + + // Basic block name class. + $classes = array( + 'wp-block-jetpack-' . $slug, + ); + + // Add alignment if provided. + if ( + ! empty( $attr['align'] ) + && in_array( $attr['align'], array( 'left', 'center', 'right', 'wide', 'full' ), true ) + ) { + array_push( $classes, 'align' . $attr['align'] ); + } + + // Add custom classes if provided in the block editor. + if ( ! empty( $attr['className'] ) ) { + array_push( $classes, $attr['className'] ); + } + + // Add any extra classes. + if ( is_array( $extra ) && ! empty( $extra ) ) { + $classes = array_merge( $classes, $extra ); + } + + return implode( ' ', $classes ); + } } |