jetpack
Last commit date
3rd-party
7 years ago
_inc
6 years ago
bin
7 years ago
css
7 years ago
images
6 years ago
json-endpoints
7 years ago
languages
7 years ago
modules
5 years ago
sal
7 years ago
scss
7 years ago
sync
7 years ago
views
7 years ago
.svnignore
12 years ago
CODE-OF-CONDUCT.md
9 years ago
changelog.txt
7 years ago
class.frame-nonce-preview.php
9 years ago
class.jetpack-admin.php
7 years ago
class.jetpack-affiliate.php
7 years ago
class.jetpack-autoupdate.php
8 years ago
class.jetpack-bbpress-json-api-compat.php
9 years ago
class.jetpack-cli.php
7 years ago
class.jetpack-client-server.php
8 years ago
class.jetpack-client.php
7 years ago
class.jetpack-connection-banner.php
7 years ago
class.jetpack-constants.php
8 years ago
class.jetpack-data.php
7 years ago
class.jetpack-debugger.php
7 years ago
class.jetpack-error.php
10 years ago
class.jetpack-gutenberg.php
7 years ago
class.jetpack-heartbeat.php
7 years ago
class.jetpack-idc.php
8 years ago
class.jetpack-ixr-client.php
10 years ago
class.jetpack-jitm.php
7 years ago
class.jetpack-modules-list-table.php
8 years ago
class.jetpack-network-sites-list-table.php
9 years ago
class.jetpack-network.php
7 years ago
class.jetpack-options.php
7 years ago
class.jetpack-post-images.php
7 years ago
class.jetpack-signature.php
7 years ago
class.jetpack-tracks.php
8 years ago
class.jetpack-twitter-cards.php
7 years ago
class.jetpack-user-agent.php
8 years ago
class.jetpack-xmlrpc-server.php
7 years ago
class.jetpack.php
7 years ago
class.json-api-endpoints.php
7 years ago
class.json-api.php
7 years ago
class.photon.php
7 years ago
composer.json
7 years ago
functions.compat.php
7 years ago
functions.gallery.php
8 years ago
functions.global.php
7 years ago
functions.opengraph.php
7 years ago
functions.photon.php
8 years ago
jetpack.php
5 years ago
json-api-config.php
10 years ago
json-endpoints.php
7 years ago
locales.php
7 years ago
readme.txt
5 years ago
require-lib.php
7 years ago
uninstall.php
8 years ago
wpml-config.xml
10 years ago
class.jetpack-gutenberg.php
512 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Handles server-side registration and use of all blocks and plugins available in Jetpack for the block editor, aka Gutenberg. |
| 4 | * Works in tandem with client-side block registration via `index.json` |
| 5 | * |
| 6 | * @package Jetpack |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Helper function to register a Jetpack Gutenberg block |
| 11 | * |
| 12 | * @param string $slug Slug of the block. |
| 13 | * @param array $args Arguments that are passed into register_block_type. |
| 14 | * |
| 15 | * @see register_block_type |
| 16 | * |
| 17 | * @since 6.7.0 |
| 18 | * |
| 19 | * @return void |
| 20 | */ |
| 21 | function jetpack_register_block( $slug, $args = array() ) { |
| 22 | Jetpack_Gutenberg::register_block( $slug, $args ); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Helper function to register a Jetpack Gutenberg plugin |
| 27 | * |
| 28 | * @param string $slug Slug of the plugin. |
| 29 | * |
| 30 | * @since 6.9.0 |
| 31 | * |
| 32 | * @return void |
| 33 | */ |
| 34 | function jetpack_register_plugin( $slug ) { |
| 35 | Jetpack_Gutenberg::register_plugin( $slug ); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Set the reason why an extension (block or plugin) is unavailable |
| 40 | * |
| 41 | * @param string $slug Slug of the block. |
| 42 | * @param string $reason A string representation of why the extension is unavailable. |
| 43 | * |
| 44 | * @since 7.0.0 |
| 45 | * |
| 46 | * @return void |
| 47 | */ |
| 48 | function jetpack_set_extension_unavailability_reason( $slug, $reason ) { |
| 49 | Jetpack_Gutenberg::set_extension_unavailability_reason( $slug, $reason ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * General Gutenberg editor specific functionality |
| 54 | */ |
| 55 | class Jetpack_Gutenberg { |
| 56 | |
| 57 | /** |
| 58 | * Only these extensions can be registered. Used to control availability of beta blocks. |
| 59 | * |
| 60 | * @var array Extensions whitelist |
| 61 | */ |
| 62 | private static $extensions = array(); |
| 63 | |
| 64 | /** |
| 65 | * Keeps track of the reasons why a given extension is unavailable. |
| 66 | * |
| 67 | * @var array Extensions availability information |
| 68 | */ |
| 69 | private static $availability = array(); |
| 70 | |
| 71 | /** |
| 72 | * Since there is no `register_plugin()` counterpart to `register_block_type()` in Gutenberg, |
| 73 | * we have to keep track of plugin registration ourselves |
| 74 | * |
| 75 | * @var array Plugin registry |
| 76 | */ |
| 77 | private static $registered_plugins = array(); |
| 78 | |
| 79 | /** |
| 80 | * Prepend the 'jetpack/' prefix to a block name |
| 81 | * |
| 82 | * @param string $block_name The block name. |
| 83 | * |
| 84 | * @return string The prefixed block name. |
| 85 | */ |
| 86 | private static function prepend_block_prefix( $block_name ) { |
| 87 | return 'jetpack/' . $block_name; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Whether two arrays share at least one item |
| 92 | * |
| 93 | * @param array $a An array. |
| 94 | * @param array $b Another array. |
| 95 | * |
| 96 | * @return boolean True if $a and $b share at least one item |
| 97 | */ |
| 98 | protected static function share_items( $a, $b ) { |
| 99 | return count( array_intersect( $a, $b ) ) > 0; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Register a block |
| 104 | * |
| 105 | * If the block isn't whitelisted, set its unavailability reason instead. |
| 106 | * |
| 107 | * @param string $slug Slug of the block. |
| 108 | * @param array $args Arguments that are passed into register_block_type(). |
| 109 | */ |
| 110 | public static function register_block( $slug, $args ) { |
| 111 | $prefixed_extensions = array_map( array( __CLASS__, 'prepend_block_prefix' ), self::$extensions ); |
| 112 | |
| 113 | // Register the block if it's whitelisted, or if it's a child block, and one of its parents is whitelisted. |
| 114 | if ( in_array( $slug, self::$extensions, true ) || ( isset( $args['parent'] ) && self::share_items( $args['parent'], $prefixed_extensions ) ) ) { |
| 115 | register_block_type( 'jetpack/' . $slug, $args ); |
| 116 | } elseif ( ! isset( $args['parent'] ) ) { |
| 117 | // Don't set availability information for child blocks -- we infer it from their parents. |
| 118 | self::set_extension_unavailability_reason( $slug, 'not_whitelisted' ); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Register a plugin |
| 124 | * |
| 125 | * If the plugin isn't whitelisted, set its unavailability reason instead. |
| 126 | * |
| 127 | * @param string $slug Slug of the plugin. |
| 128 | */ |
| 129 | public static function register_plugin( $slug ) { |
| 130 | if ( in_array( $slug, self::$extensions, true ) ) { |
| 131 | self::$registered_plugins[] = 'jetpack-' . $slug; |
| 132 | } else { |
| 133 | self::set_extension_unavailability_reason( $slug, 'not_whitelisted' ); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Register a block |
| 139 | * |
| 140 | * @deprecated 7.0.0 Use register_block() instead |
| 141 | * |
| 142 | * @param string $slug Slug of the block. |
| 143 | * @param array $args Arguments that are passed into the register_block_type. |
| 144 | * @param array $availability array containing if a block is available and the reason when it is not. |
| 145 | */ |
| 146 | public static function register( $slug, $args, $availability ) { |
| 147 | if ( isset( $availability['available'] ) && ! $availability['available'] ) { |
| 148 | self::set_extension_unavailability_reason( $slug, $availability['unavailable_reason'] ); |
| 149 | } else { |
| 150 | self::register_block( $slug, $args ); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Set the reason why an extension (block or plugin) is unavailable |
| 156 | * |
| 157 | * @param string $slug Slug of the extension. |
| 158 | * @param string $reason A string representation of why the extension is unavailable. |
| 159 | */ |
| 160 | public static function set_extension_unavailability_reason( $slug, $reason ) { |
| 161 | self::$availability[ $slug ] = $reason; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Set up a whitelist of allowed block editor extensions |
| 166 | * |
| 167 | * @return void |
| 168 | */ |
| 169 | public static function init() { |
| 170 | if ( ! self::is_gutenberg_available() ) { |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | if ( ! self::should_load() ) { |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Alternative to `JETPACK_BETA_BLOCKS`, set to `true` to load Beta Blocks. |
| 180 | * |
| 181 | * @since 6.9.0 |
| 182 | * |
| 183 | * @param boolean |
| 184 | */ |
| 185 | if ( apply_filters( 'jetpack_load_beta_blocks', false ) ) { |
| 186 | Jetpack_Constants::set_constant( 'JETPACK_BETA_BLOCKS', true ); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Filter the whitelist of block editor extensions that are available through Jetpack. |
| 191 | * |
| 192 | * @since 7.0.0 |
| 193 | * |
| 194 | * @param array |
| 195 | */ |
| 196 | self::$extensions = apply_filters( 'jetpack_set_available_extensions', self::get_jetpack_gutenberg_extensions_whitelist() ); |
| 197 | |
| 198 | /** |
| 199 | * Filter the whitelist of block editor plugins that are available through Jetpack. |
| 200 | * |
| 201 | * @deprecated 7.0.0 Use jetpack_set_available_extensions instead |
| 202 | * |
| 203 | * @since 6.8.0 |
| 204 | * |
| 205 | * @param array |
| 206 | */ |
| 207 | self::$extensions = apply_filters( 'jetpack_set_available_blocks', self::$extensions ); |
| 208 | |
| 209 | /** |
| 210 | * Filter the whitelist of block editor plugins that are available through Jetpack. |
| 211 | * |
| 212 | * @deprecated 7.0.0 Use jetpack_set_available_extensions instead |
| 213 | * |
| 214 | * @since 6.9.0 |
| 215 | * |
| 216 | * @param array |
| 217 | */ |
| 218 | self::$extensions = apply_filters( 'jetpack_set_available_plugins', self::$extensions ); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Resets the class to its original state |
| 223 | * |
| 224 | * Used in unit tests |
| 225 | * |
| 226 | * @return void |
| 227 | */ |
| 228 | public static function reset() { |
| 229 | self::$extensions = array(); |
| 230 | self::$availability = array(); |
| 231 | self::$registered_plugins = array(); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Return the Gutenberg extensions (blocks and plugins) directory |
| 236 | * |
| 237 | * @return string The Gutenberg extensions directory |
| 238 | */ |
| 239 | public static function get_blocks_directory() { |
| 240 | /** |
| 241 | * Filter to select Gutenberg blocks directory |
| 242 | * |
| 243 | * @since 6.9.0 |
| 244 | * |
| 245 | * @param string default: '_inc/blocks/' |
| 246 | */ |
| 247 | return apply_filters( 'jetpack_blocks_directory', '_inc/blocks/' ); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Checks for a given .json file in the blocks folder. |
| 252 | * |
| 253 | * @param string $preset The name of the .json file to look for. |
| 254 | * |
| 255 | * @return bool True if the file is found. |
| 256 | */ |
| 257 | public static function preset_exists( $preset ) { |
| 258 | return file_exists( JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $preset . '.json' ); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Decodes JSON loaded from a preset file in the blocks folder |
| 263 | * |
| 264 | * @param string $preset The name of the .json file to load. |
| 265 | * |
| 266 | * @return mixed Returns an object if the file is present, or false if a valid .json file is not present. |
| 267 | */ |
| 268 | public static function get_preset( $preset ) { |
| 269 | return json_decode( file_get_contents( JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $preset . '.json' ) ); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Returns a whitelist of Jetpack Gutenberg extensions (blocks and plugins), based on index.json |
| 274 | * |
| 275 | * @return array A list of blocks: eg [ 'publicize', 'markdown' ] |
| 276 | */ |
| 277 | public static function get_jetpack_gutenberg_extensions_whitelist() { |
| 278 | $preset_extensions_manifest = self::preset_exists( 'index' ) ? self::get_preset( 'index' ) : (object) array(); |
| 279 | |
| 280 | $preset_extensions = isset( $preset_extensions_manifest->production ) ? (array) $preset_extensions_manifest->production : array(); |
| 281 | |
| 282 | if ( Jetpack_Constants::is_true( 'JETPACK_BETA_BLOCKS' ) ) { |
| 283 | $beta_extensions = isset( $preset_extensions_manifest->beta ) ? (array) $preset_extensions_manifest->beta : array(); |
| 284 | return array_unique( array_merge( $preset_extensions, $beta_extensions ) ); |
| 285 | } |
| 286 | |
| 287 | return $preset_extensions; |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Get availability of each block / plugin. |
| 292 | * |
| 293 | * @return array A list of block and plugins and their availablity status |
| 294 | */ |
| 295 | public static function get_availability() { |
| 296 | |
| 297 | /** |
| 298 | * Fires before Gutenberg extensions availability is computed. |
| 299 | * |
| 300 | * In the function call you supply, use `jetpack_register_block()` and `jetpack_register_plugin()`, respectively. |
| 301 | * Alternatively, use `jetpack_set_extension_unavailability_reason()` if the block or plugin should not be registered but |
| 302 | * marked as unavailable. |
| 303 | * |
| 304 | * @since 7.0.0 |
| 305 | */ |
| 306 | do_action( 'jetpack_register_gutenberg_extensions' ); |
| 307 | |
| 308 | $available_extensions = array(); |
| 309 | |
| 310 | foreach ( self::$extensions as $extension ) { |
| 311 | $is_available = WP_Block_Type_Registry::get_instance()->is_registered( 'jetpack/' . $extension ) || |
| 312 | in_array( 'jetpack-' . $extension, self::$registered_plugins, true ); |
| 313 | |
| 314 | $available_extensions[ $extension ] = array( |
| 315 | 'available' => $is_available, |
| 316 | ); |
| 317 | |
| 318 | if ( ! $is_available ) { |
| 319 | $reason = isset( self::$availability[ $extension ] ) ? self::$availability[ $extension ] : 'missing_module'; |
| 320 | $available_extensions[ $extension ]['unavailable_reason'] = $reason; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | $unwhitelisted = array_fill_keys( |
| 325 | array_diff( array_keys( self::$availability ), self::$extensions ), |
| 326 | array( |
| 327 | 'available' => false, |
| 328 | 'unavailable_reason' => 'not_whitelisted', |
| 329 | ) |
| 330 | ); |
| 331 | |
| 332 | return array_merge( $available_extensions, $unwhitelisted ); |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Check if Gutenberg editor is available |
| 337 | * |
| 338 | * @since 6.7.0 |
| 339 | * |
| 340 | * @return bool |
| 341 | */ |
| 342 | public static function is_gutenberg_available() { |
| 343 | return function_exists( 'register_block_type' ); |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * Check whether conditions indicate Gutenberg Extensions (blocks and plugins) should be loaded |
| 348 | * |
| 349 | * Loading blocks and plugins is enabled by default and may be disabled via filter: |
| 350 | * add_filter( 'jetpack_gutenberg', '__return_false' ); |
| 351 | * |
| 352 | * @since 6.9.0 |
| 353 | * |
| 354 | * @return bool |
| 355 | */ |
| 356 | public static function should_load() { |
| 357 | if ( ! Jetpack::is_active() && ! Jetpack::is_development_mode() ) { |
| 358 | return false; |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Filter to disable Gutenberg blocks |
| 363 | * |
| 364 | * @since 6.5.0 |
| 365 | * |
| 366 | * @param bool true Whether to load Gutenberg blocks |
| 367 | */ |
| 368 | return (bool) apply_filters( 'jetpack_gutenberg', true ); |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Only enqueue block assets when needed. |
| 373 | * |
| 374 | * @param string $type slug of the block. |
| 375 | * @param array $script_dependencies An array of view-side Javascript dependencies to be enqueued. |
| 376 | * |
| 377 | * @return void |
| 378 | */ |
| 379 | public static function load_assets_as_required( $type, $script_dependencies = array() ) { |
| 380 | if ( is_admin() ) { |
| 381 | // A block's view assets will not be required in wp-admin. |
| 382 | return; |
| 383 | } |
| 384 | |
| 385 | $type = sanitize_title_with_dashes( $type ); |
| 386 | // Enqueue styles. |
| 387 | $style_relative_path = self::get_blocks_directory() . $type . '/view' . ( is_rtl() ? '.rtl' : '' ) . '.css'; |
| 388 | if ( self::block_has_asset( $style_relative_path ) ) { |
| 389 | $style_version = self::get_asset_version( $style_relative_path ); |
| 390 | $view_style = plugins_url( $style_relative_path, JETPACK__PLUGIN_FILE ); |
| 391 | wp_enqueue_style( 'jetpack-block-' . $type, $view_style, array(), $style_version ); |
| 392 | } |
| 393 | |
| 394 | // Enqueue script. |
| 395 | $script_relative_path = self::get_blocks_directory() . $type . '/view.js'; |
| 396 | if ( self::block_has_asset( $script_relative_path ) ) { |
| 397 | $script_version = self::get_asset_version( $script_relative_path ); |
| 398 | $view_script = plugins_url( $script_relative_path, JETPACK__PLUGIN_FILE ); |
| 399 | wp_enqueue_script( 'jetpack-block-' . $type, $view_script, $script_dependencies, $script_version, false ); |
| 400 | } |
| 401 | |
| 402 | wp_localize_script( |
| 403 | 'jetpack-block-' . $type, |
| 404 | 'Jetpack_Block_Assets_Base_Url', |
| 405 | plugins_url( self::get_blocks_directory(), JETPACK__PLUGIN_FILE ) |
| 406 | ); |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * Check if an asset exists for a block. |
| 411 | * |
| 412 | * @param string $file Path of the file we are looking for. |
| 413 | * |
| 414 | * @return bool $block_has_asset Does the file exist. |
| 415 | */ |
| 416 | public static function block_has_asset( $file ) { |
| 417 | return file_exists( JETPACK__PLUGIN_DIR . $file ); |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * Get the version number to use when loading the file. Allows us to bypass cache when developing. |
| 422 | * |
| 423 | * @param string $file Path of the file we are looking for. |
| 424 | * |
| 425 | * @return string $script_version Version number. |
| 426 | */ |
| 427 | public static function get_asset_version( $file ) { |
| 428 | return Jetpack::is_development_version() && self::block_has_asset( $file ) |
| 429 | ? filemtime( JETPACK__PLUGIN_DIR . $file ) |
| 430 | : JETPACK__VERSION; |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * Load Gutenberg editor assets |
| 435 | * |
| 436 | * @since 6.7.0 |
| 437 | * |
| 438 | * @return void |
| 439 | */ |
| 440 | public static function enqueue_block_editor_assets() { |
| 441 | if ( ! self::should_load() ) { |
| 442 | return; |
| 443 | } |
| 444 | |
| 445 | $rtl = is_rtl() ? '.rtl' : ''; |
| 446 | $beta = Jetpack_Constants::is_true( 'JETPACK_BETA_BLOCKS' ) ? '-beta' : ''; |
| 447 | $blocks_dir = self::get_blocks_directory(); |
| 448 | |
| 449 | $editor_script = plugins_url( "{$blocks_dir}editor{$beta}.js", JETPACK__PLUGIN_FILE ); |
| 450 | $editor_style = plugins_url( "{$blocks_dir}editor{$beta}{$rtl}.css", JETPACK__PLUGIN_FILE ); |
| 451 | |
| 452 | $version = Jetpack::is_development_version() && file_exists( JETPACK__PLUGIN_DIR . $blocks_dir . 'editor.js' ) |
| 453 | ? filemtime( JETPACK__PLUGIN_DIR . $blocks_dir . 'editor.js' ) |
| 454 | : JETPACK__VERSION; |
| 455 | |
| 456 | if ( method_exists( 'Jetpack', 'build_raw_urls' ) ) { |
| 457 | $site_fragment = Jetpack::build_raw_urls( home_url() ); |
| 458 | } elseif ( class_exists( 'WPCOM_Masterbar' ) && method_exists( 'WPCOM_Masterbar', 'get_calypso_site_slug' ) ) { |
| 459 | $site_fragment = WPCOM_Masterbar::get_calypso_site_slug( get_current_blog_id() ); |
| 460 | } else { |
| 461 | $site_fragment = ''; |
| 462 | } |
| 463 | |
| 464 | wp_enqueue_script( |
| 465 | 'jetpack-blocks-editor', |
| 466 | $editor_script, |
| 467 | array( |
| 468 | 'lodash', |
| 469 | 'wp-api-fetch', |
| 470 | 'wp-blob', |
| 471 | 'wp-blocks', |
| 472 | 'wp-components', |
| 473 | 'wp-compose', |
| 474 | 'wp-data', |
| 475 | 'wp-date', |
| 476 | 'wp-edit-post', |
| 477 | 'wp-editor', |
| 478 | 'wp-element', |
| 479 | 'wp-hooks', |
| 480 | 'wp-i18n', |
| 481 | 'wp-keycodes', |
| 482 | 'wp-plugins', |
| 483 | 'wp-rich-text', |
| 484 | 'wp-token-list', |
| 485 | 'wp-url', |
| 486 | ), |
| 487 | $version, |
| 488 | false |
| 489 | ); |
| 490 | |
| 491 | wp_localize_script( |
| 492 | 'jetpack-blocks-editor', |
| 493 | 'Jetpack_Block_Assets_Base_Url', |
| 494 | plugins_url( $blocks_dir . '/', JETPACK__PLUGIN_FILE ) |
| 495 | ); |
| 496 | |
| 497 | wp_localize_script( |
| 498 | 'jetpack-blocks-editor', |
| 499 | 'Jetpack_Editor_Initial_State', |
| 500 | array( |
| 501 | 'available_blocks' => self::get_availability(), |
| 502 | 'jetpack' => array( 'is_active' => Jetpack::is_active() ), |
| 503 | 'siteFragment' => $site_fragment, |
| 504 | ) |
| 505 | ); |
| 506 | |
| 507 | Jetpack::setup_wp_i18n_locale_data(); |
| 508 | |
| 509 | wp_enqueue_style( 'jetpack-blocks-editor', $editor_style, array(), $version ); |
| 510 | } |
| 511 | } |
| 512 |