Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
69.23% covered (warning)
69.23%
54 / 78
55.56% covered (warning)
55.56%
5 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
Unavailable_Blocks
69.23% covered (warning)
69.23%
54 / 78
55.56% covered (warning)
55.56%
5 / 9
89.97
0.00% covered (danger)
0.00%
0 / 1
 init
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 enqueue_site_wide_notice
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
20
 get_editor_data
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
7
 get_site_wide_reason
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
6
 describe_inactive_modules
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
4
 get_inactive_module_details
84.62% covered (warning)
84.62%
11 / 13
0.00% covered (danger)
0.00%
0 / 1
8.23
 get_shipped_blocks
75.00% covered (warning)
75.00%
6 / 8
0.00% covered (danger)
0.00%
0 / 1
5.39
 get_blocks_module_url
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
5
 is_forced_off
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Explains, in the block editor, why a saved Jetpack block is unavailable.
4 *
5 * @package automattic/jetpack
6 */
7
8namespace Automattic\Jetpack\Plugin;
9
10use Automattic\Jetpack\Assets;
11use Automattic\Jetpack\Constants;
12use Automattic\Jetpack\Modules;
13use Automattic\Jetpack\My_Jetpack\Initializer;
14use Automattic\Jetpack\Status;
15use Automattic\Jetpack\Status\Host;
16use Jetpack;
17use Jetpack_Gutenberg;
18use Jetpack_Modules_Overrides;
19
20/**
21 * Works out why Jetpack is not registering its blocks, so the editor can say so
22 * instead of showing core's generic "doesn't include support" warning.
23 */
24class Unavailable_Blocks {
25
26    const SCRIPT_HANDLE = 'jetpack-unavailable-blocks-notice';
27
28    /**
29     * Blocks that stay registered in the editor while the Blocks module is inactive.
30     */
31    const INDEPENDENT_BLOCKS = array( 'jetpack/contact-form' );
32
33    /**
34     * Top-level blocks that packages register, which the plugin's block manifest does not list.
35     */
36    const PACKAGE_BLOCKS = array(
37        'jetpack/contact-form',
38        'jetpack/podcast-episode',
39        'jetpack/paypal-payment-buttons',
40        'videopress/video',
41        'videopress/all-playlists',
42        'videopress/latest-videos-playlist',
43        'videopress/playlist',
44    );
45
46    /**
47     * Manifest blocks that a plan or site setting can also keep unregistered.
48     */
49    const CONDITIONAL_BLOCKS = array( 'jetpack/cookie-consent', 'jetpack/voice-to-content', 'jetpack/wordads' );
50
51    /**
52     * Hook into the block editor.
53     *
54     * @since 16.4
55     */
56    public static function init() {
57        add_action( 'enqueue_block_editor_assets', array( __CLASS__, 'enqueue_site_wide_notice' ) );
58    }
59
60    /**
61     * Load the standalone notice when no Jetpack block is registered at all.
62     *
63     * Otherwise Jetpack's own editor bundle is on the page and carries the same notice.
64     *
65     * @since 16.4
66     */
67    public static function enqueue_site_wide_notice() {
68        if ( ! wp_should_load_block_editor_scripts_and_styles() ) {
69            return;
70        }
71
72        $data = self::get_editor_data();
73        if ( null === $data || null === $data['reason'] ) {
74            return;
75        }
76
77        Assets::register_script(
78            self::SCRIPT_HANDLE,
79            Jetpack_Gutenberg::get_blocks_directory() . 'unavailable-blocks-notice.js',
80            JETPACK__PLUGIN_FILE,
81            array(
82                'textdomain' => 'jetpack',
83                'enqueue'    => true,
84            )
85        );
86        wp_add_inline_script(
87            self::SCRIPT_HANDLE,
88            'var Jetpack_Unavailable_Blocks = ' . wp_json_encode( $data, JSON_HEX_TAG | JSON_HEX_AMP ) . ';',
89            'before'
90        );
91    }
92
93    /**
94     * Get what the standalone notice needs when no Jetpack block from the Blocks module is registered.
95     *
96     * @since 16.4
97     *
98     * @return array|null Null when there is nothing site-wide to explain.
99     */
100    public static function get_editor_data() {
101        // The mobile apps fetch editor assets over REST, where wp-admin links are no use.
102        if ( Constants::is_true( 'REST_REQUEST' ) ) {
103            return null;
104        }
105
106        $reason = self::get_site_wide_reason();
107        if ( null === $reason ) {
108            return null;
109        }
110
111        $is_disconnected = 'not_connected' === $reason;
112        $features        = array();
113
114        // The form registers without the Blocks module, so Forms itself can be the cause.
115        if ( ! ( new Modules() )->is_active( 'contact-form' ) ) {
116            $forms = self::get_inactive_module_details( 'contact-form' );
117            if ( $forms ) {
118                $features['jetpack/contact-form'] = $forms;
119            }
120        }
121
122        return array(
123            'reason'           => $reason,
124            'canFix'           => current_user_can( $is_disconnected ? 'jetpack_connect' : 'jetpack_manage_modules' ),
125            'fixUrl'           => $is_disconnected ? admin_url( 'admin.php?page=jetpack' ) : self::get_blocks_module_url(),
126            'features'         => $features,
127            'canManageModules' => current_user_can( 'jetpack_manage_modules' ),
128            'modulesUrl'       => admin_url( 'admin.php?page=jetpack_modules' ),
129            'independent'      => self::INDEPENDENT_BLOCKS,
130            'shipped'          => self::get_shipped_blocks(),
131        );
132    }
133
134    /**
135     * Get the reason no Jetpack block from the Blocks module is registered, if there is one.
136     *
137     * @since 16.4
138     *
139     * @return string|null `not_connected`, `blocks_module`, `disabled`, or null.
140     */
141    public static function get_site_wide_reason() {
142        if ( ! Jetpack::is_connection_ready() && ! ( new Status() )->is_offline_mode() ) {
143            return 'not_connected';
144        }
145
146        // Not Jetpack_Gutenberg::should_load(): a filter returning true cannot load an inactive module.
147        if ( ! ( new Modules() )->is_active( 'blocks' ) ) {
148            return self::is_forced_off( 'blocks' ) ? 'disabled' : 'blocks_module';
149        }
150
151        /** This filter is documented in class.jetpack-gutenberg.php */
152        if ( ! apply_filters( 'jetpack_gutenberg', true ) ) {
153            return 'disabled';
154        }
155
156        return null;
157    }
158
159    /**
160     * Add each inactive module's name and forced state to blocks that reported one when they bailed out.
161     *
162     * @since 16.4
163     *
164     * @param array $availability Block availability, as from Jetpack_Gutenberg::get_availability().
165     * @return array The same availability, with module details filled in or dropped.
166     */
167    public static function describe_inactive_modules( $availability ) {
168        foreach ( (array) $availability as $slug => $entry ) {
169            if ( isset( $entry['details']['module'] ) && is_string( $entry['details']['module'] ) ) {
170                $availability[ $slug ]['details'] = self::get_inactive_module_details( $entry['details']['module'] );
171            }
172        }
173
174        return $availability;
175    }
176
177    /**
178     * Describe an inactive module for the editor's explanation of a block that needs it.
179     *
180     * @since 16.4
181     *
182     * @param string $module Module slug.
183     * @return array Module slug, name, and whether it is forced off; empty when the editor should not name it.
184     */
185    public static function get_inactive_module_details( $module ) {
186        if ( ( new Host() )->is_wpcom_simple() ) {
187            return array();
188        }
189
190        $info = ( new Modules() )->get( $module );
191        if ( ! is_array( $info ) ) {
192            return array();
193        }
194
195        // Offline mode keeps connection-dependent modules off, so their toggle would not help.
196        if ( ! empty( $info['requires_connection'] ) && ( new Status() )->is_offline_mode() ) {
197            return array();
198        }
199
200        // Modules::get() returns the untranslated file header.
201        $i18n = function_exists( 'jetpack_get_module_i18n' ) ? jetpack_get_module_i18n( $module ) : array();
202
203        return array(
204            'module'      => $module,
205            'module_name' => ! empty( $i18n['name'] ) ? $i18n['name'] : ( ! empty( $info['name'] ) ? $info['name'] : $module ),
206            'forced'      => self::is_forced_off( $module ),
207        );
208    }
209
210    /**
211     * Get the top-level blocks this site would register with nothing turned off.
212     *
213     * @since 16.4
214     *
215     * @return string[] Block names.
216     */
217    public static function get_shipped_blocks() {
218        $manifest_file = JETPACK__PLUGIN_DIR . '_inc/blocks/blocks-manifest.php';
219        $manifest      = file_exists( $manifest_file ) ? require $manifest_file : array();
220        $extensions    = Jetpack_Gutenberg::get_available_extensions();
221        $blocks        = self::PACKAGE_BLOCKS;
222
223        foreach ( (array) $manifest as $extension => $metadata ) {
224            if ( in_array( $extension, $extensions, true ) && ! empty( $metadata['name'] ) ) {
225                $blocks[] = $metadata['name'];
226            }
227        }
228
229        return array_values( array_diff( $blocks, self::CONDITIONAL_BLOCKS ) );
230    }
231
232    /**
233     * Where an admin turns the Blocks module back on.
234     *
235     * @return string
236     */
237    private static function get_blocks_module_url() {
238        if ( ! method_exists( Initializer::class, 'is_settings_consolidation_enabled' ) || ! Initializer::is_settings_consolidation_enabled() ) {
239            return admin_url( 'admin.php?page=jetpack#/writing' );
240        }
241
242        // Features searches translated module names, so an English term would miss the row in other locales.
243        $i18n = function_exists( 'jetpack_get_module_i18n' ) ? jetpack_get_module_i18n( 'blocks' ) : array();
244
245        return admin_url( 'admin.php?page=my-jetpack#/features?search=' . rawurlencode( ! empty( $i18n['name'] ) ? $i18n['name'] : 'blocks' ) );
246    }
247
248    /**
249     * Whether a filter or the host's feature policy keeps a module inactive.
250     *
251     * @param string $module Module slug.
252     * @return bool
253     */
254    private static function is_forced_off( $module ) {
255        return 'inactive' === Jetpack_Modules_Overrides::instance()->get_module_override( $module );
256    }
257}