暫無描述

class.jetpack-gutenberg.php 36KB

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