class.jetpack-gutenberg.php
| 1 | <?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 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 | use Automattic\Jetpack\Blocks; |
| 10 | use Automattic\Jetpack\Constants; |
| 11 | use Automattic\Jetpack\Status; |
| 12 | |
| 13 | /** |
| 14 | * Wrapper function to safely register a gutenberg block type |
| 15 | * |
| 16 | * @deprecated 9.1.0 Use Automattic\\Jetpack\\Blocks::jetpack_register_block instead |
| 17 | * |
| 18 | * @see register_block_type |
| 19 | * |
| 20 | * @since 6.7.0 |
| 21 | * |
| 22 | * @param string $slug Slug of the block. |
| 23 | * @param array $args Arguments that are passed into register_block_type. |
| 24 | * |
| 25 | * @return WP_Block_Type|false The registered block type on success, or false on failure. |
| 26 | */ |
| 27 | function jetpack_register_block( $slug, $args = array() ) { |
| 28 | _deprecated_function( __METHOD__, '9.1.0', 'Automattic\\Jetpack\\Blocks::jetpack_register_block' ); |
| 29 | return Blocks::jetpack_register_block( $slug, $args ); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Helper function to register a Jetpack Gutenberg plugin |
| 34 | * |
| 35 | * @deprecated 7.1.0 Use Jetpack_Gutenberg::set_extension_available() instead |
| 36 | * |
| 37 | * @param string $slug Slug of the plugin. |
| 38 | * |
| 39 | * @since 6.9.0 |
| 40 | * |
| 41 | * @return void |
| 42 | */ |
| 43 | function jetpack_register_plugin( $slug ) { |
| 44 | _deprecated_function( __FUNCTION__, '7.1', 'Jetpack_Gutenberg::set_extension_available' ); |
| 45 | |
| 46 | Jetpack_Gutenberg::register_plugin( $slug ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Set the reason why an extension (block or plugin) is unavailable |
| 51 | * |
| 52 | * @deprecated 7.1.0 Use Jetpack_Gutenberg::set_extension_unavailable() instead |
| 53 | * |
| 54 | * @param string $slug Slug of the block. |
| 55 | * @param string $reason A string representation of why the extension is unavailable. |
| 56 | * |
| 57 | * @since 7.0.0 |
| 58 | * |
| 59 | * @return void |
| 60 | */ |
| 61 | function jetpack_set_extension_unavailability_reason( $slug, $reason ) { |
| 62 | _deprecated_function( __FUNCTION__, '7.1', 'Jetpack_Gutenberg::set_extension_unavailable' ); |
| 63 | |
| 64 | Jetpack_Gutenberg::set_extension_unavailability_reason( $slug, $reason ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * General Gutenberg editor specific functionality |
| 69 | */ |
| 70 | class Jetpack_Gutenberg { |
| 71 | |
| 72 | /** |
| 73 | * Only these extensions can be registered. Used to control availability of beta blocks. |
| 74 | * |
| 75 | * @var array Extensions allowed list. |
| 76 | */ |
| 77 | private static $extensions = array(); |
| 78 | |
| 79 | /** |
| 80 | * Keeps track of the reasons why a given extension is unavailable. |
| 81 | * |
| 82 | * @var array Extensions availability information |
| 83 | */ |
| 84 | private static $availability = array(); |
| 85 | |
| 86 | /** |
| 87 | * Check to see if a minimum version of Gutenberg is available. Because a Gutenberg version is not available in |
| 88 | * php if the Gutenberg plugin is not installed, if we know which minimum WP release has the required version we can |
| 89 | * optionally fall back to that. |
| 90 | * |
| 91 | * @param array $version_requirements An array containing the required Gutenberg version and, if known, the WordPress version that was released with this minimum version. |
| 92 | * @param string $slug The slug of the block or plugin that has the gutenberg version requirement. |
| 93 | * |
| 94 | * @since 8.3.0 |
| 95 | * |
| 96 | * @return boolean True if the version of gutenberg required by the block or plugin is available. |
| 97 | */ |
| 98 | public static function is_gutenberg_version_available( $version_requirements, $slug ) { |
| 99 | global $wp_version; |
| 100 | |
| 101 | // Bail if we don't at least have the gutenberg version requirement, the WP version is optional. |
| 102 | if ( empty( $version_requirements['gutenberg'] ) ) { |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | // If running a local dev build of gutenberg plugin GUTENBERG_DEVELOPMENT_MODE is set so assume correct version. |
| 107 | if ( defined( 'GUTENBERG_DEVELOPMENT_MODE' ) && GUTENBERG_DEVELOPMENT_MODE ) { |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | $version_available = false; |
| 112 | |
| 113 | // If running a production build of the gutenberg plugin then GUTENBERG_VERSION is set, otherwise if WP version |
| 114 | // with required version of Gutenberg is known check that. |
| 115 | if ( defined( 'GUTENBERG_VERSION' ) ) { |
| 116 | $version_available = version_compare( GUTENBERG_VERSION, $version_requirements['gutenberg'], '>=' ); |
| 117 | } elseif ( ! empty( $version_requirements['wp'] ) ) { |
| 118 | $version_available = version_compare( $wp_version, $version_requirements['wp'], '>=' ); |
| 119 | } |
| 120 | |
| 121 | if ( ! $version_available ) { |
| 122 | self::set_extension_unavailable( |
| 123 | $slug, |
| 124 | 'incorrect_gutenberg_version', |
| 125 | array( |
| 126 | 'required_feature' => $slug, |
| 127 | 'required_version' => $version_requirements, |
| 128 | 'current_version' => array( |
| 129 | 'wp' => $wp_version, |
| 130 | 'gutenberg' => defined( 'GUTENBERG_VERSION' ) ? GUTENBERG_VERSION : null, |
| 131 | ), |
| 132 | ) |
| 133 | ); |
| 134 | } |
| 135 | |
| 136 | return $version_available; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Prepend the 'jetpack/' prefix to a block name |
| 141 | * |
| 142 | * @param string $block_name The block name. |
| 143 | * |
| 144 | * @return string The prefixed block name. |
| 145 | */ |
| 146 | private static function prepend_block_prefix( $block_name ) { |
| 147 | return 'jetpack/' . $block_name; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Remove the 'jetpack/' or jetpack-' prefix from an extension name |
| 152 | * |
| 153 | * @param string $extension_name The extension name. |
| 154 | * |
| 155 | * @return string The unprefixed extension name. |
| 156 | */ |
| 157 | public static function remove_extension_prefix( $extension_name ) { |
| 158 | if ( 0 === strpos( $extension_name, 'jetpack/' ) || 0 === strpos( $extension_name, 'jetpack-' ) ) { |
| 159 | return substr( $extension_name, strlen( 'jetpack/' ) ); |
| 160 | } |
| 161 | return $extension_name; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Whether two arrays share at least one item |
| 166 | * |
| 167 | * @param array $a An array. |
| 168 | * @param array $b Another array. |
| 169 | * |
| 170 | * @return boolean True if $a and $b share at least one item |
| 171 | */ |
| 172 | protected static function share_items( $a, $b ) { |
| 173 | return count( array_intersect( $a, $b ) ) > 0; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Register a plugin |
| 178 | * |
| 179 | * @deprecated 7.1.0 Use Jetpack_Gutenberg::set_extension_available() instead |
| 180 | * |
| 181 | * @param string $slug Slug of the plugin. |
| 182 | */ |
| 183 | public static function register_plugin( $slug ) { |
| 184 | _deprecated_function( __METHOD__, '7.1', 'Jetpack_Gutenberg::set_extension_available' ); |
| 185 | |
| 186 | self::set_extension_available( $slug ); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Set a (non-block) extension as available |
| 191 | * |
| 192 | * @param string $slug Slug of the extension. |
| 193 | */ |
| 194 | public static function set_extension_available( $slug ) { |
| 195 | self::$availability[ self::remove_extension_prefix( $slug ) ] = true; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Set the reason why an extension (block or plugin) is unavailable |
| 200 | * |
| 201 | * @param string $slug Slug of the extension. |
| 202 | * @param string $reason A string representation of why the extension is unavailable. |
| 203 | * @param array $details A free-form array containing more information on why the extension is unavailable. |
| 204 | */ |
| 205 | public static function set_extension_unavailable( $slug, $reason, $details = array() ) { |
| 206 | if ( |
| 207 | // Extensions that require a plan may be eligible for upgrades. |
| 208 | 'missing_plan' === $reason |
| 209 | && ( |
| 210 | /** |
| 211 | * Filter 'jetpack_block_editor_enable_upgrade_nudge' with `true` to enable or `false` |
| 212 | * to disable paid feature upgrade nudges in the block editor. |
| 213 | * |
| 214 | * When this is changed to default to `true`, you should also update `modules/memberships/class-jetpack-memberships.php` |
| 215 | * See https://github.com/Automattic/jetpack/pull/13394#pullrequestreview-293063378 |
| 216 | * |
| 217 | * @since 7.7.0 |
| 218 | * |
| 219 | * @param boolean |
| 220 | */ |
| 221 | ! apply_filters( 'jetpack_block_editor_enable_upgrade_nudge', false ) |
| 222 | /** This filter is documented in _inc/lib/admin-pages/class.jetpack-react-page.php */ |
| 223 | || ! apply_filters( 'jetpack_show_promotions', true ) |
| 224 | ) |
| 225 | ) { |
| 226 | // The block editor may apply an upgrade nudge if `missing_plan` is the reason. |
| 227 | // Add a descriptive suffix to disable behavior but provide informative reason. |
| 228 | $reason .= '__nudge_disabled'; |
| 229 | } |
| 230 | |
| 231 | self::$availability[ self::remove_extension_prefix( $slug ) ] = array( |
| 232 | 'reason' => $reason, |
| 233 | 'details' => $details, |
| 234 | ); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Set the reason why an extension (block or plugin) is unavailable |
| 239 | * |
| 240 | * @deprecated 7.1.0 Use set_extension_unavailable() instead |
| 241 | * |
| 242 | * @param string $slug Slug of the extension. |
| 243 | * @param string $reason A string representation of why the extension is unavailable. |
| 244 | */ |
| 245 | public static function set_extension_unavailability_reason( $slug, $reason ) { |
| 246 | _deprecated_function( __METHOD__, '7.1', 'Jetpack_Gutenberg::set_extension_unavailable' ); |
| 247 | |
| 248 | self::set_extension_unavailable( $slug, $reason ); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Set up a list of allowed block editor extensions |
| 253 | * |
| 254 | * @return void |
| 255 | */ |
| 256 | public static function init() { |
| 257 | if ( ! self::should_load() ) { |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Alternative to `JETPACK_BETA_BLOCKS`, set to `true` to load Beta Blocks. |
| 263 | * |
| 264 | * @since 6.9.0 |
| 265 | * |
| 266 | * @param boolean |
| 267 | */ |
| 268 | if ( apply_filters( 'jetpack_load_beta_blocks', false ) ) { |
| 269 | Constants::set_constant( 'JETPACK_BETA_BLOCKS', true ); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Alternative to `JETPACK_EXPERIMENTAL_BLOCKS`, set to `true` to load Experimental Blocks. |
| 274 | * |
| 275 | * @since 8.4.0 |
| 276 | * |
| 277 | * @param boolean |
| 278 | */ |
| 279 | if ( apply_filters( 'jetpack_load_experimental_blocks', false ) ) { |
| 280 | Constants::set_constant( 'JETPACK_EXPERIMENTAL_BLOCKS', true ); |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Filter the list of block editor extensions that are available through Jetpack. |
| 285 | * |
| 286 | * @since 7.0.0 |
| 287 | * |
| 288 | * @param array |
| 289 | */ |
| 290 | self::$extensions = apply_filters( 'jetpack_set_available_extensions', self::get_available_extensions() ); |
| 291 | |
| 292 | /** |
| 293 | * Filter the list of block editor plugins that are available through Jetpack. |
| 294 | * |
| 295 | * @deprecated 7.0.0 Use jetpack_set_available_extensions instead |
| 296 | * |
| 297 | * @since 6.8.0 |
| 298 | * |
| 299 | * @param array |
| 300 | */ |
| 301 | self::$extensions = apply_filters( 'jetpack_set_available_blocks', self::$extensions ); |
| 302 | |
| 303 | /** |
| 304 | * Filter the list of block editor plugins that are available through Jetpack. |
| 305 | * |
| 306 | * @deprecated 7.0.0 Use jetpack_set_available_extensions instead |
| 307 | * |
| 308 | * @since 6.9.0 |
| 309 | * |
| 310 | * @param array |
| 311 | */ |
| 312 | self::$extensions = apply_filters( 'jetpack_set_available_plugins', self::$extensions ); |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Resets the class to its original state |
| 317 | * |
| 318 | * Used in unit tests |
| 319 | * |
| 320 | * @return void |
| 321 | */ |
| 322 | public static function reset() { |
| 323 | self::$extensions = array(); |
| 324 | self::$availability = array(); |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Return the Gutenberg extensions (blocks and plugins) directory |
| 329 | * |
| 330 | * @return string The Gutenberg extensions directory |
| 331 | */ |
| 332 | public static function get_blocks_directory() { |
| 333 | /** |
| 334 | * Filter to select Gutenberg blocks directory |
| 335 | * |
| 336 | * @since 6.9.0 |
| 337 | * |
| 338 | * @param string default: '_inc/blocks/' |
| 339 | */ |
| 340 | return apply_filters( 'jetpack_blocks_directory', '_inc/blocks/' ); |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * Checks for a given .json file in the blocks folder. |
| 345 | * |
| 346 | * @param string $preset The name of the .json file to look for. |
| 347 | * |
| 348 | * @return bool True if the file is found. |
| 349 | */ |
| 350 | public static function preset_exists( $preset ) { |
| 351 | return file_exists( JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $preset . '.json' ); |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Decodes JSON loaded from a preset file in the blocks folder |
| 356 | * |
| 357 | * @param string $preset The name of the .json file to load. |
| 358 | * |
| 359 | * @return mixed Returns an object if the file is present, or false if a valid .json file is not present. |
| 360 | */ |
| 361 | public static function get_preset( $preset ) { |
| 362 | return json_decode( |
| 363 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 364 | file_get_contents( JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $preset . '.json' ) |
| 365 | ); |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Returns a list of Jetpack Gutenberg extensions (blocks and plugins), based on index.json |
| 370 | * |
| 371 | * @return array A list of blocks: eg [ 'publicize', 'markdown' ] |
| 372 | */ |
| 373 | public static function get_jetpack_gutenberg_extensions_allowed_list() { |
| 374 | $preset_extensions_manifest = self::preset_exists( 'index' ) |
| 375 | ? self::get_preset( 'index' ) |
| 376 | : (object) array(); |
| 377 | $blocks_variation = self::blocks_variation(); |
| 378 | |
| 379 | return self::get_extensions_preset_for_variation( $preset_extensions_manifest, $blocks_variation ); |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Returns a list of Jetpack Gutenberg extensions (blocks and plugins), based on index.json |
| 384 | * |
| 385 | * @deprecated 8.7.0 Use get_jetpack_gutenberg_extensions_allowed_list() |
| 386 | * |
| 387 | * @return array A list of blocks: eg [ 'publicize', 'markdown' ] |
| 388 | */ |
| 389 | public static function get_jetpack_gutenberg_extensions_whitelist() { |
| 390 | _deprecated_function( __FUNCTION__, 'jetpack-8.7.0', 'Jetpack_Gutenberg::get_jetpack_gutenberg_extensions_allowed_list' ); |
| 391 | return self::get_jetpack_gutenberg_extensions_allowed_list(); |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Returns a diff from a combined list of allowed extensions and extensions determined to be excluded |
| 396 | * |
| 397 | * @param array $allowed_extensions An array of allowed extensions. |
| 398 | * |
| 399 | * @return array A list of blocks: eg array( 'publicize', 'markdown' ) |
| 400 | */ |
| 401 | public static function get_available_extensions( $allowed_extensions = null ) { |
| 402 | $exclusions = get_option( 'jetpack_excluded_extensions', array() ); |
| 403 | $allowed_extensions = is_null( $allowed_extensions ) ? self::get_jetpack_gutenberg_extensions_allowed_list() : $allowed_extensions; |
| 404 | |
| 405 | return array_diff( $allowed_extensions, $exclusions ); |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * Return true if the extension has been registered and there's nothing in the availablilty array. |
| 410 | * |
| 411 | * @param string $extension The name of the extension. |
| 412 | * |
| 413 | * @return bool whether the extension has been registered and there's nothing in the availablilty array. |
| 414 | */ |
| 415 | public static function is_registered_and_no_entry_in_availability( $extension ) { |
| 416 | return self::is_registered( 'jetpack/' . $extension ) && ! isset( self::$availability[ $extension ] ); |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Return true if the extension has a true entry in the availablilty array. |
| 421 | * |
| 422 | * @param string $extension The name of the extension. |
| 423 | * |
| 424 | * @return bool whether the extension has a true entry in the availablilty array. |
| 425 | */ |
| 426 | public static function is_available( $extension ) { |
| 427 | return isset( self::$availability[ $extension ] ) && true === self::$availability[ $extension ]; |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Get availability of each block / plugin. |
| 432 | * |
| 433 | * @return array A list of block and plugins and their availablity status |
| 434 | */ |
| 435 | public static function get_availability() { |
| 436 | /** |
| 437 | * Fires before Gutenberg extensions availability is computed. |
| 438 | * |
| 439 | * In the function call you supply, use `Blocks::jetpack_register_block()` to set a block as available. |
| 440 | * Alternatively, use `Jetpack_Gutenberg::set_extension_available()` (for a non-block plugin), and |
| 441 | * `Jetpack_Gutenberg::set_extension_unavailable()` (if the block or plugin should not be registered |
| 442 | * but marked as unavailable). |
| 443 | * |
| 444 | * @since 7.0.0 |
| 445 | */ |
| 446 | do_action( 'jetpack_register_gutenberg_extensions' ); |
| 447 | |
| 448 | $available_extensions = array(); |
| 449 | |
| 450 | foreach ( self::$extensions as $extension ) { |
| 451 | $is_available = self::is_registered_and_no_entry_in_availability( $extension ) || self::is_available( $extension ); |
| 452 | $available_extensions[ $extension ] = array( |
| 453 | 'available' => $is_available, |
| 454 | ); |
| 455 | |
| 456 | if ( ! $is_available ) { |
| 457 | $reason = isset( self::$availability[ $extension ] ) ? self::$availability[ $extension ]['reason'] : 'missing_module'; |
| 458 | $details = isset( self::$availability[ $extension ] ) ? self::$availability[ $extension ]['details'] : array(); |
| 459 | $available_extensions[ $extension ]['unavailable_reason'] = $reason; |
| 460 | $available_extensions[ $extension ]['details'] = $details; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | return $available_extensions; |
| 465 | } |
| 466 | |
| 467 | /** |
| 468 | * Check if an extension/block is already registered |
| 469 | * |
| 470 | * @since 7.2 |
| 471 | * |
| 472 | * @param string $slug Name of extension/block to check. |
| 473 | * |
| 474 | * @return bool |
| 475 | */ |
| 476 | public static function is_registered( $slug ) { |
| 477 | return WP_Block_Type_Registry::get_instance()->is_registered( $slug ); |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * Check if Gutenberg editor is available |
| 482 | * |
| 483 | * @since 6.7.0 |
| 484 | * |
| 485 | * @return bool |
| 486 | */ |
| 487 | public static function is_gutenberg_available() { |
| 488 | return true; |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * Check whether conditions indicate Gutenberg Extensions (blocks and plugins) should be loaded |
| 493 | * |
| 494 | * Loading blocks and plugins is enabled by default and may be disabled via filter: |
| 495 | * add_filter( 'jetpack_gutenberg', '__return_false' ); |
| 496 | * |
| 497 | * @since 6.9.0 |
| 498 | * |
| 499 | * @return bool |
| 500 | */ |
| 501 | public static function should_load() { |
| 502 | if ( ! Jetpack::is_active() && ! ( new Status() )->is_offline_mode() ) { |
| 503 | return false; |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * Filter to disable Gutenberg blocks |
| 508 | * |
| 509 | * @since 6.5.0 |
| 510 | * |
| 511 | * @param bool true Whether to load Gutenberg blocks |
| 512 | */ |
| 513 | return (bool) apply_filters( 'jetpack_gutenberg', true ); |
| 514 | } |
| 515 | |
| 516 | /** |
| 517 | * Only enqueue block assets when needed. |
| 518 | * |
| 519 | * @param string $type Slug of the block. |
| 520 | * @param array $script_dependencies Script dependencies. Will be merged with automatically |
| 521 | * detected script dependencies from the webpack build. |
| 522 | * |
| 523 | * @return void |
| 524 | */ |
| 525 | public static function load_assets_as_required( $type, $script_dependencies = array() ) { |
| 526 | if ( is_admin() ) { |
| 527 | // A block's view assets will not be required in wp-admin. |
| 528 | return; |
| 529 | } |
| 530 | |
| 531 | $type = sanitize_title_with_dashes( $type ); |
| 532 | self::load_styles_as_required( $type ); |
| 533 | self::load_scripts_as_required( $type, $script_dependencies ); |
| 534 | } |
| 535 | |
| 536 | /** |
| 537 | * Only enqueue block sytles when needed. |
| 538 | * |
| 539 | * @param string $type Slug of the block. |
| 540 | * |
| 541 | * @since 7.2.0 |
| 542 | * |
| 543 | * @return void |
| 544 | */ |
| 545 | public static function load_styles_as_required( $type ) { |
| 546 | if ( is_admin() ) { |
| 547 | // A block's view assets will not be required in wp-admin. |
| 548 | return; |
| 549 | } |
| 550 | |
| 551 | // Enqueue styles. |
| 552 | $style_relative_path = self::get_blocks_directory() . $type . '/view' . ( is_rtl() ? '.rtl' : '' ) . '.css'; |
| 553 | if ( self::block_has_asset( $style_relative_path ) ) { |
| 554 | $style_version = self::get_asset_version( $style_relative_path ); |
| 555 | $view_style = plugins_url( $style_relative_path, JETPACK__PLUGIN_FILE ); |
| 556 | wp_enqueue_style( 'jetpack-block-' . $type, $view_style, array(), $style_version ); |
| 557 | } |
| 558 | |
| 559 | } |
| 560 | |
| 561 | /** |
| 562 | * Only enqueue block scripts when needed. |
| 563 | * |
| 564 | * @param string $type Slug of the block. |
| 565 | * @param array $script_dependencies Script dependencies. Will be merged with automatically |
| 566 | * detected script dependencies from the webpack build. |
| 567 | * |
| 568 | * @since 7.2.0 |
| 569 | * |
| 570 | * @return void |
| 571 | */ |
| 572 | public static function load_scripts_as_required( $type, $script_dependencies = array() ) { |
| 573 | if ( is_admin() ) { |
| 574 | // A block's view assets will not be required in wp-admin. |
| 575 | return; |
| 576 | } |
| 577 | |
| 578 | // Enqueue script. |
| 579 | $script_relative_path = self::get_blocks_directory() . $type . '/view.js'; |
| 580 | $script_deps_path = JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $type . '/view.asset.php'; |
| 581 | $script_dependencies[] = 'wp-polyfill'; |
| 582 | if ( file_exists( $script_deps_path ) ) { |
| 583 | $asset_manifest = include $script_deps_path; |
| 584 | $script_dependencies = array_unique( array_merge( $script_dependencies, $asset_manifest['dependencies'] ) ); |
| 585 | } |
| 586 | |
| 587 | if ( ! Blocks::is_amp_request() && self::block_has_asset( $script_relative_path ) ) { |
| 588 | $script_version = self::get_asset_version( $script_relative_path ); |
| 589 | $view_script = plugins_url( $script_relative_path, JETPACK__PLUGIN_FILE ); |
| 590 | wp_enqueue_script( 'jetpack-block-' . $type, $view_script, $script_dependencies, $script_version, false ); |
| 591 | } |
| 592 | |
| 593 | wp_localize_script( |
| 594 | 'jetpack-block-' . $type, |
| 595 | 'Jetpack_Block_Assets_Base_Url', |
| 596 | array( |
| 597 | 'url' => plugins_url( self::get_blocks_directory(), JETPACK__PLUGIN_FILE ), |
| 598 | ) |
| 599 | ); |
| 600 | } |
| 601 | |
| 602 | /** |
| 603 | * Check if an asset exists for a block. |
| 604 | * |
| 605 | * @param string $file Path of the file we are looking for. |
| 606 | * |
| 607 | * @return bool $block_has_asset Does the file exist. |
| 608 | */ |
| 609 | public static function block_has_asset( $file ) { |
| 610 | return file_exists( JETPACK__PLUGIN_DIR . $file ); |
| 611 | } |
| 612 | |
| 613 | /** |
| 614 | * Get the version number to use when loading the file. Allows us to bypass cache when developing. |
| 615 | * |
| 616 | * @param string $file Path of the file we are looking for. |
| 617 | * |
| 618 | * @return string $script_version Version number. |
| 619 | */ |
| 620 | public static function get_asset_version( $file ) { |
| 621 | return Jetpack::is_development_version() && self::block_has_asset( $file ) |
| 622 | ? filemtime( JETPACK__PLUGIN_DIR . $file ) |
| 623 | : JETPACK__VERSION; |
| 624 | } |
| 625 | |
| 626 | /** |
| 627 | * Load Gutenberg editor assets |
| 628 | * |
| 629 | * @since 6.7.0 |
| 630 | * |
| 631 | * @return void |
| 632 | */ |
| 633 | public static function enqueue_block_editor_assets() { |
| 634 | if ( ! self::should_load() ) { |
| 635 | return; |
| 636 | } |
| 637 | |
| 638 | $status = new Status(); |
| 639 | |
| 640 | // Required for Analytics. See _inc/lib/admin-pages/class.jetpack-admin-page.php. |
| 641 | if ( ! $status->is_offline_mode() && Jetpack::is_active() ) { |
| 642 | wp_enqueue_script( 'jp-tracks', '//stats.wp.com/w.js', array(), gmdate( 'YW' ), true ); |
| 643 | } |
| 644 | |
| 645 | $rtl = is_rtl() ? '.rtl' : ''; |
| 646 | $blocks_dir = self::get_blocks_directory(); |
| 647 | $blocks_variation = self::blocks_variation(); |
| 648 | |
| 649 | if ( 'production' !== $blocks_variation ) { |
| 650 | $blocks_env = '-' . esc_attr( $blocks_variation ); |
| 651 | } else { |
| 652 | $blocks_env = ''; |
| 653 | } |
| 654 | |
| 655 | $editor_script = plugins_url( "{$blocks_dir}editor{$blocks_env}.js", JETPACK__PLUGIN_FILE ); |
| 656 | $editor_style = plugins_url( "{$blocks_dir}editor{$blocks_env}{$rtl}.css", JETPACK__PLUGIN_FILE ); |
| 657 | |
| 658 | $editor_deps_path = JETPACK__PLUGIN_DIR . $blocks_dir . "editor{$blocks_env}.asset.php"; |
| 659 | $editor_deps = array( 'wp-polyfill' ); |
| 660 | if ( file_exists( $editor_deps_path ) ) { |
| 661 | $asset_manifest = include $editor_deps_path; |
| 662 | $editor_deps = $asset_manifest['dependencies']; |
| 663 | } |
| 664 | |
| 665 | $version = Jetpack::is_development_version() && file_exists( JETPACK__PLUGIN_DIR . $blocks_dir . 'editor.js' ) |
| 666 | ? filemtime( JETPACK__PLUGIN_DIR . $blocks_dir . 'editor.js' ) |
| 667 | : JETPACK__VERSION; |
| 668 | |
| 669 | wp_enqueue_script( |
| 670 | 'jetpack-blocks-editor', |
| 671 | $editor_script, |
| 672 | $editor_deps, |
| 673 | $version, |
| 674 | false |
| 675 | ); |
| 676 | |
| 677 | wp_localize_script( |
| 678 | 'jetpack-blocks-editor', |
| 679 | 'Jetpack_Block_Assets_Base_Url', |
| 680 | array( |
| 681 | 'url' => plugins_url( $blocks_dir . '/', JETPACK__PLUGIN_FILE ), |
| 682 | ) |
| 683 | ); |
| 684 | |
| 685 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 686 | $user = wp_get_current_user(); |
| 687 | $user_data = array( |
| 688 | 'userid' => $user->ID, |
| 689 | 'username' => $user->user_login, |
| 690 | ); |
| 691 | $blog_id = get_current_blog_id(); |
| 692 | $is_current_user_connected = true; |
| 693 | } else { |
| 694 | $user_data = Jetpack_Tracks_Client::get_connected_user_tracks_identity(); |
| 695 | $blog_id = Jetpack_Options::get_option( 'id', 0 ); |
| 696 | $is_current_user_connected = Jetpack::is_user_connected(); |
| 697 | } |
| 698 | |
| 699 | wp_localize_script( |
| 700 | 'jetpack-blocks-editor', |
| 701 | 'Jetpack_Editor_Initial_State', |
| 702 | array( |
| 703 | 'available_blocks' => self::get_availability(), |
| 704 | 'jetpack' => array( |
| 705 | 'is_active' => Jetpack::is_active(), |
| 706 | 'is_current_user_connected' => $is_current_user_connected, |
| 707 | /** This filter is documented in class.jetpack-gutenberg.php */ |
| 708 | 'enable_upgrade_nudge' => apply_filters( 'jetpack_block_editor_enable_upgrade_nudge', false ), |
| 709 | ), |
| 710 | 'siteFragment' => $status->get_site_suffix(), |
| 711 | 'adminUrl' => esc_url( admin_url() ), |
| 712 | 'tracksUserData' => $user_data, |
| 713 | 'wpcomBlogId' => $blog_id, |
| 714 | 'allowedMimeTypes' => wp_get_mime_types(), |
| 715 | ) |
| 716 | ); |
| 717 | |
| 718 | wp_set_script_translations( 'jetpack-blocks-editor', 'jetpack' ); |
| 719 | |
| 720 | wp_enqueue_style( 'jetpack-blocks-editor', $editor_style, array(), $version ); |
| 721 | } |
| 722 | |
| 723 | /** |
| 724 | * Some blocks do not depend on a specific module, |
| 725 | * and can consequently be loaded outside of the usual modules. |
| 726 | * We will look for such modules in the extensions/ directory. |
| 727 | * |
| 728 | * @since 7.1.0 |
| 729 | */ |
| 730 | public static function load_independent_blocks() { |
| 731 | if ( self::should_load() ) { |
| 732 | /** |
| 733 | * Look for files that match our list of available Jetpack Gutenberg extensions (blocks and plugins). |
| 734 | * If available, load them. |
| 735 | */ |
| 736 | foreach ( self::$extensions as $extension ) { |
| 737 | $extension_file_glob = glob( JETPACK__PLUGIN_DIR . 'extensions/*/' . $extension . '/' . $extension . '.php' ); |
| 738 | if ( ! empty( $extension_file_glob ) ) { |
| 739 | include_once $extension_file_glob[0]; |
| 740 | } |
| 741 | } |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | /** |
| 746 | * Loads PHP components of extended-blocks. |
| 747 | * |
| 748 | * @since 8.9.0 |
| 749 | */ |
| 750 | public static function load_extended_blocks() { |
| 751 | if ( self::should_load() ) { |
| 752 | $extended_blocks = glob( JETPACK__PLUGIN_DIR . 'extensions/extended-blocks/*' ); |
| 753 | |
| 754 | foreach ( $extended_blocks as $block ) { |
| 755 | $name = basename( $block ); |
| 756 | $path = JETPACK__PLUGIN_DIR . 'extensions/extended-blocks/' . $name . '/' . $name . '.php'; |
| 757 | |
| 758 | if ( file_exists( $path ) ) { |
| 759 | include_once $path; |
| 760 | } |
| 761 | } |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | /** |
| 766 | * Get CSS classes for a block. |
| 767 | * |
| 768 | * @since 7.7.0 |
| 769 | * |
| 770 | * @param string $slug Block slug. |
| 771 | * @param array $attr Block attributes. |
| 772 | * @param array $extra Potential extra classes you may want to provide. |
| 773 | * |
| 774 | * @return string $classes List of CSS classes for a block. |
| 775 | */ |
| 776 | public static function block_classes( $slug, $attr, $extra = array() ) { |
| 777 | _deprecated_function( __METHOD__, '9.0.0', 'Automattic\\Jetpack\\Blocks::classes' ); |
| 778 | return Blocks::classes( $slug, $attr, $extra ); |
| 779 | } |
| 780 | |
| 781 | /** |
| 782 | * Determine whether a site should use the default set of blocks, or a custom set. |
| 783 | * Possible variations are currently beta, experimental, and production. |
| 784 | * |
| 785 | * @since 8.1.0 |
| 786 | * |
| 787 | * @return string $block_varation production|beta|experimental |
| 788 | */ |
| 789 | public static function blocks_variation() { |
| 790 | // Default to production blocks. |
| 791 | $block_varation = 'production'; |
| 792 | |
| 793 | if ( Constants::is_true( 'JETPACK_BETA_BLOCKS' ) ) { |
| 794 | $block_varation = 'beta'; |
| 795 | } |
| 796 | |
| 797 | /* |
| 798 | * Switch to experimental blocks if you use the JETPACK_EXPERIMENTAL_BLOCKS constant. |
| 799 | */ |
| 800 | if ( Constants::is_true( 'JETPACK_EXPERIMENTAL_BLOCKS' ) ) { |
| 801 | $block_varation = 'experimental'; |
| 802 | } |
| 803 | |
| 804 | /** |
| 805 | * Allow customizing the variation of blocks in use on a site. |
| 806 | * |
| 807 | * @since 8.1.0 |
| 808 | * |
| 809 | * @param string $block_variation Can be beta, experimental, and production. Defaults to production. |
| 810 | */ |
| 811 | return apply_filters( 'jetpack_blocks_variation', $block_varation ); |
| 812 | } |
| 813 | |
| 814 | /** |
| 815 | * Get a list of extensions available for the variation you chose. |
| 816 | * |
| 817 | * @since 8.1.0 |
| 818 | * |
| 819 | * @param obj $preset_extensions_manifest List of extensions available in Jetpack. |
| 820 | * @param string $blocks_variation Subset of blocks. production|beta|experimental. |
| 821 | * |
| 822 | * @return array $preset_extensions Array of extensions for that variation |
| 823 | */ |
| 824 | public static function get_extensions_preset_for_variation( $preset_extensions_manifest, $blocks_variation ) { |
| 825 | $preset_extensions = isset( $preset_extensions_manifest->{ $blocks_variation } ) |
| 826 | ? (array) $preset_extensions_manifest->{ $blocks_variation } |
| 827 | : array(); |
| 828 | |
| 829 | /* |
| 830 | * Experimental and Beta blocks need the production blocks as well. |
| 831 | */ |
| 832 | if ( |
| 833 | 'experimental' === $blocks_variation |
| 834 | || 'beta' === $blocks_variation |
| 835 | ) { |
| 836 | $production_extensions = isset( $preset_extensions_manifest->production ) |
| 837 | ? (array) $preset_extensions_manifest->production |
| 838 | : array(); |
| 839 | |
| 840 | $preset_extensions = array_unique( array_merge( $preset_extensions, $production_extensions ) ); |
| 841 | } |
| 842 | |
| 843 | /* |
| 844 | * Beta blocks need the experimental blocks as well. |
| 845 | * |
| 846 | * If you've chosen to see Beta blocks, |
| 847 | * we want to make all blocks available to you: |
| 848 | * - Production |
| 849 | * - Experimental |
| 850 | * - Beta |
| 851 | */ |
| 852 | if ( 'beta' === $blocks_variation ) { |
| 853 | $production_extensions = isset( $preset_extensions_manifest->experimental ) |
| 854 | ? (array) $preset_extensions_manifest->experimental |
| 855 | : array(); |
| 856 | |
| 857 | $preset_extensions = array_unique( array_merge( $preset_extensions, $production_extensions ) ); |
| 858 | } |
| 859 | |
| 860 | return $preset_extensions; |
| 861 | } |
| 862 | |
| 863 | /** |
| 864 | * Validate a URL used in a SSR block. |
| 865 | * |
| 866 | * @since 8.3.0 |
| 867 | * |
| 868 | * @param string $url URL saved as an attribute in block. |
| 869 | * @param array $allowed Array of allowed hosts for that block, or regexes to check against. |
| 870 | * @param bool $is_regex Array of regexes matching the URL that could be used in block. |
| 871 | * |
| 872 | * @return bool|string |
| 873 | */ |
| 874 | public static function validate_block_embed_url( $url, $allowed = array(), $is_regex = false ) { |
| 875 | if ( |
| 876 | empty( $url ) |
| 877 | || ! is_array( $allowed ) |
| 878 | || empty( $allowed ) |
| 879 | ) { |
| 880 | return false; |
| 881 | } |
| 882 | |
| 883 | $url_components = wp_parse_url( $url ); |
| 884 | |
| 885 | // Bail early if we cannot find a host. |
| 886 | if ( empty( $url_components['host'] ) ) { |
| 887 | return false; |
| 888 | } |
| 889 | |
| 890 | // Normalize URL. |
| 891 | $url = sprintf( |
| 892 | '%s://%s%s%s', |
| 893 | isset( $url_components['scheme'] ) ? $url_components['scheme'] : 'https', |
| 894 | $url_components['host'], |
| 895 | isset( $url_components['path'] ) ? $url_components['path'] : '/', |
| 896 | isset( $url_components['query'] ) ? '?' . $url_components['query'] : '' |
| 897 | ); |
| 898 | |
| 899 | if ( ! empty( $url_components['fragment'] ) ) { |
| 900 | $url = $url . '#' . rawurlencode( $url_components['fragment'] ); |
| 901 | } |
| 902 | |
| 903 | /* |
| 904 | * If we're using an allowed list of hosts, |
| 905 | * check if the URL belongs to one of the domains allowed for that block. |
| 906 | */ |
| 907 | if ( |
| 908 | false === $is_regex |
| 909 | && in_array( $url_components['host'], $allowed, true ) |
| 910 | ) { |
| 911 | return $url; |
| 912 | } |
| 913 | |
| 914 | /* |
| 915 | * If we are using an array of regexes to check against, |
| 916 | * loop through that. |
| 917 | */ |
| 918 | if ( true === $is_regex ) { |
| 919 | foreach ( $allowed as $regex ) { |
| 920 | if ( 1 === preg_match( $regex, $url ) ) { |
| 921 | return $url; |
| 922 | } |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | return false; |
| 927 | } |
| 928 | |
| 929 | /** |
| 930 | * Output an UpgradeNudge Component on the frontend of a site. |
| 931 | * |
| 932 | * @since 8.4.0 |
| 933 | * |
| 934 | * @param string $plan The plan that users need to purchase to make the block work. |
| 935 | * |
| 936 | * @return string |
| 937 | */ |
| 938 | public static function upgrade_nudge( $plan ) { |
| 939 | if ( |
| 940 | ! current_user_can( 'manage_options' ) |
| 941 | /** This filter is documented in class.jetpack-gutenberg.php */ |
| 942 | || ! apply_filters( 'jetpack_block_editor_enable_upgrade_nudge', false ) |
| 943 | /** This filter is documented in _inc/lib/admin-pages/class.jetpack-react-page.php */ |
| 944 | || ! apply_filters( 'jetpack_show_promotions', true ) |
| 945 | || is_feed() |
| 946 | ) { |
| 947 | return; |
| 948 | } |
| 949 | |
| 950 | jetpack_require_lib( 'components' ); |
| 951 | return Jetpack_Components::render_upgrade_nudge( |
| 952 | array( |
| 953 | 'plan' => $plan, |
| 954 | ) |
| 955 | ); |
| 956 | } |
| 957 | |
| 958 | /** |
| 959 | * Output a notice within a block. |
| 960 | * |
| 961 | * @since 8.6.0 |
| 962 | * |
| 963 | * @param string $message Notice we want to output. |
| 964 | * @param string $status Status of the notice. Can be one of success, info, warning, error. info by default. |
| 965 | * @param string $classes List of CSS classes. |
| 966 | * |
| 967 | * @return string |
| 968 | */ |
| 969 | public static function notice( $message, $status = 'info', $classes = '' ) { |
| 970 | if ( |
| 971 | empty( $message ) |
| 972 | || ! in_array( $status, array( 'success', 'info', 'warning', 'error' ), true ) |
| 973 | ) { |
| 974 | return ''; |
| 975 | } |
| 976 | |
| 977 | $color = ''; |
| 978 | switch ( $status ) { |
| 979 | case 'success': |
| 980 | $color = '#46b450'; |
| 981 | break; |
| 982 | case 'warning': |
| 983 | $color = '#ffb900'; |
| 984 | break; |
| 985 | case 'error': |
| 986 | $color = '#dc3232'; |
| 987 | break; |
| 988 | case 'info': |
| 989 | default: |
| 990 | $color = '#00a0d2'; |
| 991 | break; |
| 992 | } |
| 993 | |
| 994 | return sprintf( |
| 995 | '<div class="jetpack-block__notice %1$s %3$s" style="border-left:5px solid %4$s;padding:1em;background-color:#f8f9f9;">%2$s</div>', |
| 996 | esc_attr( $status ), |
| 997 | wp_kses( |
| 998 | $message, |
| 999 | array( |
| 1000 | 'br' => array(), |
| 1001 | 'p' => array(), |
| 1002 | ) |
| 1003 | ), |
| 1004 | esc_attr( $classes ), |
| 1005 | sanitize_hex_color( $color ) |
| 1006 | ); |
| 1007 | } |
| 1008 | |
| 1009 | /** |
| 1010 | * Set the availability of the block as the editor |
| 1011 | * is loaded. |
| 1012 | * |
| 1013 | * @param string $slug Slug of the block. |
| 1014 | */ |
| 1015 | public static function set_availability_for_plan( $slug ) { |
| 1016 | $is_available = true; |
| 1017 | $plan = ''; |
| 1018 | $slug = self::remove_extension_prefix( $slug ); |
| 1019 | |
| 1020 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 1021 | if ( ! class_exists( 'Store_Product_List' ) ) { |
| 1022 | require WP_CONTENT_DIR . '/admin-plugins/wpcom-billing/store-product-list.php'; |
| 1023 | } |
| 1024 | $features_data = Store_Product_List::get_site_specific_features_data(); |
| 1025 | $is_available = in_array( $slug, $features_data['active'], true ); |
| 1026 | if ( ! empty( $features_data['available'][ $slug ] ) ) { |
| 1027 | $plan = $features_data['available'][ $slug ][0]; |
| 1028 | } |
| 1029 | } elseif ( ! jetpack_is_atomic_site() ) { |
| 1030 | /* |
| 1031 | * If it's Atomic then assume all features are available |
| 1032 | * otherwise check against the Jetpack plan. |
| 1033 | */ |
| 1034 | $is_available = Jetpack_Plan::supports( $slug ); |
| 1035 | $plan = Jetpack_Plan::get_minimum_plan_for_feature( $slug ); |
| 1036 | } |
| 1037 | if ( $is_available ) { |
| 1038 | self::set_extension_available( $slug ); |
| 1039 | } else { |
| 1040 | self::set_extension_unavailable( |
| 1041 | $slug, |
| 1042 | 'missing_plan', |
| 1043 | array( |
| 1044 | 'required_feature' => $slug, |
| 1045 | 'required_plan' => $plan, |
| 1046 | ) |
| 1047 | ); |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | /** |
| 1052 | * Wraps the suplied render_callback in a function to check |
| 1053 | * the availability of the block before rendering it. |
| 1054 | * |
| 1055 | * @param string $slug The block slug, used to check for availability. |
| 1056 | * @param callable $render_callback The render_callback that will be called if the block is available. |
| 1057 | */ |
| 1058 | public static function get_render_callback_with_availability_check( $slug, $render_callback ) { |
| 1059 | return function ( $prepared_attributes, $block_content ) use ( $render_callback, $slug ) { |
| 1060 | $availability = self::get_availability(); |
| 1061 | $bare_slug = self::remove_extension_prefix( $slug ); |
| 1062 | if ( isset( $availability[ $bare_slug ] ) && $availability[ $bare_slug ]['available'] ) { |
| 1063 | return call_user_func( $render_callback, $prepared_attributes, $block_content ); |
| 1064 | } |
| 1065 | |
| 1066 | return null; |
| 1067 | }; |
| 1068 | } |
| 1069 | } |
| 1070 |