暫無描述

block-editor.php 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. <?php
  2. /**
  3. * Block Editor API.
  4. *
  5. * @package WordPress
  6. * @subpackage Editor
  7. * @since 5.8.0
  8. */
  9. /**
  10. * Returns the list of default categories for block types.
  11. *
  12. * @since 5.8.0
  13. *
  14. * @return array[] Array of categories for block types.
  15. */
  16. function get_default_block_categories() {
  17. return array(
  18. array(
  19. 'slug' => 'text',
  20. 'title' => _x( 'Text', 'block category' ),
  21. 'icon' => null,
  22. ),
  23. array(
  24. 'slug' => 'media',
  25. 'title' => _x( 'Media', 'block category' ),
  26. 'icon' => null,
  27. ),
  28. array(
  29. 'slug' => 'design',
  30. 'title' => _x( 'Design', 'block category' ),
  31. 'icon' => null,
  32. ),
  33. array(
  34. 'slug' => 'widgets',
  35. 'title' => _x( 'Widgets', 'block category' ),
  36. 'icon' => null,
  37. ),
  38. array(
  39. 'slug' => 'theme',
  40. 'title' => _x( 'Theme', 'block category' ),
  41. 'icon' => null,
  42. ),
  43. array(
  44. 'slug' => 'embed',
  45. 'title' => _x( 'Embeds', 'block category' ),
  46. 'icon' => null,
  47. ),
  48. array(
  49. 'slug' => 'reusable',
  50. 'title' => _x( 'Reusable Blocks', 'block category' ),
  51. 'icon' => null,
  52. ),
  53. );
  54. }
  55. /**
  56. * Returns all the categories for block types that will be shown in the block editor.
  57. *
  58. * @since 5.0.0
  59. * @since 5.8.0 It is possible to pass the block editor context as param.
  60. *
  61. * @param WP_Post|WP_Block_Editor_Context $post_or_block_editor_context The current post object or
  62. * the block editor context.
  63. *
  64. * @return array[] Array of categories for block types.
  65. */
  66. function get_block_categories( $post_or_block_editor_context ) {
  67. $block_categories = get_default_block_categories();
  68. $block_editor_context = $post_or_block_editor_context instanceof WP_Post ?
  69. new WP_Block_Editor_Context(
  70. array(
  71. 'post' => $post_or_block_editor_context,
  72. )
  73. ) : $post_or_block_editor_context;
  74. /**
  75. * Filters the default array of categories for block types.
  76. *
  77. * @since 5.8.0
  78. *
  79. * @param array[] $block_categories Array of categories for block types.
  80. * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
  81. */
  82. $block_categories = apply_filters( 'block_categories_all', $block_categories, $block_editor_context );
  83. if ( ! empty( $block_editor_context->post ) ) {
  84. $post = $block_editor_context->post;
  85. /**
  86. * Filters the default array of categories for block types.
  87. *
  88. * @since 5.0.0
  89. * @deprecated 5.8.0 Use the {@see 'block_categories_all'} filter instead.
  90. *
  91. * @param array[] $block_categories Array of categories for block types.
  92. * @param WP_Post $post Post being loaded.
  93. */
  94. $block_categories = apply_filters_deprecated( 'block_categories', array( $block_categories, $post ), '5.8.0', 'block_categories_all' );
  95. }
  96. return $block_categories;
  97. }
  98. /**
  99. * Gets the list of allowed block types to use in the block editor.
  100. *
  101. * @since 5.8.0
  102. *
  103. * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
  104. *
  105. * @return bool|string[] Array of block type slugs, or boolean to enable/disable all.
  106. */
  107. function get_allowed_block_types( $block_editor_context ) {
  108. $allowed_block_types = true;
  109. /**
  110. * Filters the allowed block types for all editor types.
  111. *
  112. * @since 5.8.0
  113. *
  114. * @param bool|string[] $allowed_block_types Array of block type slugs, or boolean to enable/disable all.
  115. * Default true (all registered block types supported).
  116. * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
  117. */
  118. $allowed_block_types = apply_filters( 'allowed_block_types_all', $allowed_block_types, $block_editor_context );
  119. if ( ! empty( $block_editor_context->post ) ) {
  120. $post = $block_editor_context->post;
  121. /**
  122. * Filters the allowed block types for the editor.
  123. *
  124. * @since 5.0.0
  125. * @deprecated 5.8.0 Use the {@see 'allowed_block_types_all'} filter instead.
  126. *
  127. * @param bool|string[] $allowed_block_types Array of block type slugs, or boolean to enable/disable all.
  128. * Default true (all registered block types supported)
  129. * @param WP_Post $post The post resource data.
  130. */
  131. $allowed_block_types = apply_filters_deprecated( 'allowed_block_types', array( $allowed_block_types, $post ), '5.8.0', 'allowed_block_types_all' );
  132. }
  133. return $allowed_block_types;
  134. }
  135. /**
  136. * Returns the default block editor settings.
  137. *
  138. * @since 5.8.0
  139. *
  140. * @return array The default block editor settings.
  141. */
  142. function get_default_block_editor_settings() {
  143. // Media settings.
  144. $max_upload_size = wp_max_upload_size();
  145. if ( ! $max_upload_size ) {
  146. $max_upload_size = 0;
  147. }
  148. /** This filter is documented in wp-admin/includes/media.php */
  149. $image_size_names = apply_filters(
  150. 'image_size_names_choose',
  151. array(
  152. 'thumbnail' => __( 'Thumbnail' ),
  153. 'medium' => __( 'Medium' ),
  154. 'large' => __( 'Large' ),
  155. 'full' => __( 'Full Size' ),
  156. )
  157. );
  158. $available_image_sizes = array();
  159. foreach ( $image_size_names as $image_size_slug => $image_size_name ) {
  160. $available_image_sizes[] = array(
  161. 'slug' => $image_size_slug,
  162. 'name' => $image_size_name,
  163. );
  164. }
  165. $default_size = get_option( 'image_default_size', 'large' );
  166. $image_default_size = in_array( $default_size, array_keys( $image_size_names ), true ) ? $default_size : 'large';
  167. $image_dimensions = array();
  168. $all_sizes = wp_get_registered_image_subsizes();
  169. foreach ( $available_image_sizes as $size ) {
  170. $key = $size['slug'];
  171. if ( isset( $all_sizes[ $key ] ) ) {
  172. $image_dimensions[ $key ] = $all_sizes[ $key ];
  173. }
  174. }
  175. // These styles are used if the "no theme styles" options is triggered or on
  176. // themes without their own editor styles.
  177. $default_editor_styles_file = ABSPATH . WPINC . '/css/dist/block-editor/default-editor-styles.css';
  178. if ( file_exists( $default_editor_styles_file ) ) {
  179. $default_editor_styles = array(
  180. array( 'css' => file_get_contents( $default_editor_styles_file ) ),
  181. );
  182. } else {
  183. $default_editor_styles = array();
  184. }
  185. $editor_settings = array(
  186. 'alignWide' => get_theme_support( 'align-wide' ),
  187. 'allowedBlockTypes' => true,
  188. 'allowedMimeTypes' => get_allowed_mime_types(),
  189. 'defaultEditorStyles' => $default_editor_styles,
  190. 'blockCategories' => get_default_block_categories(),
  191. 'disableCustomColors' => get_theme_support( 'disable-custom-colors' ),
  192. 'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ),
  193. 'disableCustomGradients' => get_theme_support( 'disable-custom-gradients' ),
  194. 'enableCustomLineHeight' => get_theme_support( 'custom-line-height' ),
  195. 'enableCustomSpacing' => get_theme_support( 'custom-spacing' ),
  196. 'enableCustomUnits' => get_theme_support( 'custom-units' ),
  197. 'isRTL' => is_rtl(),
  198. 'imageDefaultSize' => $image_default_size,
  199. 'imageDimensions' => $image_dimensions,
  200. 'imageEditing' => true,
  201. 'imageSizes' => $available_image_sizes,
  202. 'maxUploadFileSize' => $max_upload_size,
  203. // The following flag is required to enable the new Gallery block format on the mobile apps in 5.9.
  204. '__unstableGalleryWithImageBlocks' => true,
  205. );
  206. // Theme settings.
  207. $color_palette = current( (array) get_theme_support( 'editor-color-palette' ) );
  208. if ( false !== $color_palette ) {
  209. $editor_settings['colors'] = $color_palette;
  210. }
  211. $font_sizes = current( (array) get_theme_support( 'editor-font-sizes' ) );
  212. if ( false !== $font_sizes ) {
  213. $editor_settings['fontSizes'] = $font_sizes;
  214. }
  215. $gradient_presets = current( (array) get_theme_support( 'editor-gradient-presets' ) );
  216. if ( false !== $gradient_presets ) {
  217. $editor_settings['gradients'] = $gradient_presets;
  218. }
  219. return $editor_settings;
  220. }
  221. /**
  222. * Returns the block editor settings needed to use the Legacy Widget block which
  223. * is not registered by default.
  224. *
  225. * @since 5.8.0
  226. *
  227. * @return array Settings to be used with get_block_editor_settings().
  228. */
  229. function get_legacy_widget_block_editor_settings() {
  230. $editor_settings = array();
  231. /**
  232. * Filters the list of widget-type IDs that should **not** be offered by the
  233. * Legacy Widget block.
  234. *
  235. * Returning an empty array will make all widgets available.
  236. *
  237. * @since 5.8.0
  238. *
  239. * @param string[] $widgets An array of excluded widget-type IDs.
  240. */
  241. $editor_settings['widgetTypesToHideFromLegacyWidgetBlock'] = apply_filters(
  242. 'widget_types_to_hide_from_legacy_widget_block',
  243. array(
  244. 'pages',
  245. 'calendar',
  246. 'archives',
  247. 'media_audio',
  248. 'media_image',
  249. 'media_gallery',
  250. 'media_video',
  251. 'search',
  252. 'text',
  253. 'categories',
  254. 'recent-posts',
  255. 'recent-comments',
  256. 'rss',
  257. 'tag_cloud',
  258. 'custom_html',
  259. 'block',
  260. )
  261. );
  262. return $editor_settings;
  263. }
  264. /**
  265. * Collect the block editor assets that need to be loaded into the editor's iframe.
  266. *
  267. * @since 6.0.0
  268. * @access private
  269. *
  270. * @global string $pagenow The filename of the current screen.
  271. *
  272. * @return array {
  273. * The block editor assets.
  274. *
  275. * @type string|false $styles String containing the HTML for styles.
  276. * @type string|false $scripts String containing the HTML for scripts.
  277. * }
  278. */
  279. function _wp_get_iframed_editor_assets() {
  280. global $pagenow;
  281. $script_handles = array();
  282. $style_handles = array(
  283. 'wp-block-editor',
  284. 'wp-block-library',
  285. 'wp-edit-blocks',
  286. );
  287. if ( current_theme_supports( 'wp-block-styles' ) ) {
  288. $style_handles[] = 'wp-block-library-theme';
  289. }
  290. if ( 'widgets.php' === $pagenow || 'customize.php' === $pagenow ) {
  291. $style_handles[] = 'wp-widgets';
  292. $style_handles[] = 'wp-edit-widgets';
  293. }
  294. $block_registry = WP_Block_Type_Registry::get_instance();
  295. foreach ( $block_registry->get_all_registered() as $block_type ) {
  296. if ( ! empty( $block_type->style ) ) {
  297. $style_handles[] = $block_type->style;
  298. }
  299. if ( ! empty( $block_type->editor_style ) ) {
  300. $style_handles[] = $block_type->editor_style;
  301. }
  302. if ( ! empty( $block_type->script ) ) {
  303. $script_handles[] = $block_type->script;
  304. }
  305. }
  306. $style_handles = array_unique( $style_handles );
  307. $done = wp_styles()->done;
  308. ob_start();
  309. // We do not need reset styles for the iframed editor.
  310. wp_styles()->done = array( 'wp-reset-editor-styles' );
  311. wp_styles()->do_items( $style_handles );
  312. wp_styles()->done = $done;
  313. $styles = ob_get_clean();
  314. $script_handles = array_unique( $script_handles );
  315. $done = wp_scripts()->done;
  316. ob_start();
  317. wp_scripts()->done = array();
  318. wp_scripts()->do_items( $script_handles );
  319. wp_scripts()->done = $done;
  320. $scripts = ob_get_clean();
  321. return array(
  322. 'styles' => $styles,
  323. 'scripts' => $scripts,
  324. );
  325. }
  326. /**
  327. * Returns the contextualized block editor settings for a selected editor context.
  328. *
  329. * @since 5.8.0
  330. *
  331. * @param array $custom_settings Custom settings to use with the given editor type.
  332. * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
  333. *
  334. * @return array The contextualized block editor settings.
  335. */
  336. function get_block_editor_settings( array $custom_settings, $block_editor_context ) {
  337. $editor_settings = array_merge(
  338. get_default_block_editor_settings(),
  339. array(
  340. 'allowedBlockTypes' => get_allowed_block_types( $block_editor_context ),
  341. 'blockCategories' => get_block_categories( $block_editor_context ),
  342. ),
  343. $custom_settings
  344. );
  345. $global_styles = array();
  346. $presets = array(
  347. array(
  348. 'css' => 'variables',
  349. '__unstableType' => 'presets',
  350. 'isGlobalStyles' => true,
  351. ),
  352. array(
  353. 'css' => 'presets',
  354. '__unstableType' => 'presets',
  355. 'isGlobalStyles' => true,
  356. ),
  357. );
  358. foreach ( $presets as $preset_style ) {
  359. $actual_css = wp_get_global_stylesheet( array( $preset_style['css'] ) );
  360. if ( '' !== $actual_css ) {
  361. $preset_style['css'] = $actual_css;
  362. $global_styles[] = $preset_style;
  363. }
  364. }
  365. if ( WP_Theme_JSON_Resolver::theme_has_support() ) {
  366. $block_classes = array(
  367. 'css' => 'styles',
  368. '__unstableType' => 'theme',
  369. 'isGlobalStyles' => true,
  370. );
  371. $actual_css = wp_get_global_stylesheet( array( $block_classes['css'] ) );
  372. if ( '' !== $actual_css ) {
  373. $block_classes['css'] = $actual_css;
  374. $global_styles[] = $block_classes;
  375. }
  376. }
  377. $editor_settings['styles'] = array_merge( $global_styles, get_block_editor_theme_styles() );
  378. $editor_settings['__experimentalFeatures'] = wp_get_global_settings();
  379. // These settings may need to be updated based on data coming from theme.json sources.
  380. if ( isset( $editor_settings['__experimentalFeatures']['color']['palette'] ) ) {
  381. $colors_by_origin = $editor_settings['__experimentalFeatures']['color']['palette'];
  382. $editor_settings['colors'] = isset( $colors_by_origin['custom'] ) ?
  383. $colors_by_origin['custom'] : (
  384. isset( $colors_by_origin['theme'] ) ?
  385. $colors_by_origin['theme'] :
  386. $colors_by_origin['default']
  387. );
  388. }
  389. if ( isset( $editor_settings['__experimentalFeatures']['color']['gradients'] ) ) {
  390. $gradients_by_origin = $editor_settings['__experimentalFeatures']['color']['gradients'];
  391. $editor_settings['gradients'] = isset( $gradients_by_origin['custom'] ) ?
  392. $gradients_by_origin['custom'] : (
  393. isset( $gradients_by_origin['theme'] ) ?
  394. $gradients_by_origin['theme'] :
  395. $gradients_by_origin['default']
  396. );
  397. }
  398. if ( isset( $editor_settings['__experimentalFeatures']['typography']['fontSizes'] ) ) {
  399. $font_sizes_by_origin = $editor_settings['__experimentalFeatures']['typography']['fontSizes'];
  400. $editor_settings['fontSizes'] = isset( $font_sizes_by_origin['custom'] ) ?
  401. $font_sizes_by_origin['custom'] : (
  402. isset( $font_sizes_by_origin['theme'] ) ?
  403. $font_sizes_by_origin['theme'] :
  404. $font_sizes_by_origin['default']
  405. );
  406. }
  407. if ( isset( $editor_settings['__experimentalFeatures']['color']['custom'] ) ) {
  408. $editor_settings['disableCustomColors'] = ! $editor_settings['__experimentalFeatures']['color']['custom'];
  409. unset( $editor_settings['__experimentalFeatures']['color']['custom'] );
  410. }
  411. if ( isset( $editor_settings['__experimentalFeatures']['color']['customGradient'] ) ) {
  412. $editor_settings['disableCustomGradients'] = ! $editor_settings['__experimentalFeatures']['color']['customGradient'];
  413. unset( $editor_settings['__experimentalFeatures']['color']['customGradient'] );
  414. }
  415. if ( isset( $editor_settings['__experimentalFeatures']['typography']['customFontSize'] ) ) {
  416. $editor_settings['disableCustomFontSizes'] = ! $editor_settings['__experimentalFeatures']['typography']['customFontSize'];
  417. unset( $editor_settings['__experimentalFeatures']['typography']['customFontSize'] );
  418. }
  419. if ( isset( $editor_settings['__experimentalFeatures']['typography']['lineHeight'] ) ) {
  420. $editor_settings['enableCustomLineHeight'] = $editor_settings['__experimentalFeatures']['typography']['lineHeight'];
  421. unset( $editor_settings['__experimentalFeatures']['typography']['lineHeight'] );
  422. }
  423. if ( isset( $editor_settings['__experimentalFeatures']['spacing']['units'] ) ) {
  424. $editor_settings['enableCustomUnits'] = $editor_settings['__experimentalFeatures']['spacing']['units'];
  425. unset( $editor_settings['__experimentalFeatures']['spacing']['units'] );
  426. }
  427. if ( isset( $editor_settings['__experimentalFeatures']['spacing']['padding'] ) ) {
  428. $editor_settings['enableCustomSpacing'] = $editor_settings['__experimentalFeatures']['spacing']['padding'];
  429. unset( $editor_settings['__experimentalFeatures']['spacing']['padding'] );
  430. }
  431. $editor_settings['__unstableResolvedAssets'] = _wp_get_iframed_editor_assets();
  432. $editor_settings['localAutosaveInterval'] = 15;
  433. $editor_settings['__experimentalDiscussionSettings'] = array(
  434. 'commentOrder' => get_option( 'comment_order' ),
  435. 'commentsPerPage' => get_option( 'comments_per_page' ),
  436. 'defaultCommentsPage' => get_option( 'default_comments_page' ),
  437. 'pageComments' => get_option( 'page_comments' ),
  438. 'threadComments' => get_option( 'thread_comments' ),
  439. 'threadCommentsDepth' => get_option( 'thread_comments_depth' ),
  440. 'defaultCommentStatus' => get_option( 'default_comment_status' ),
  441. 'avatarURL' => get_avatar_url(
  442. '',
  443. array(
  444. 'size' => 96,
  445. 'force_default' => true,
  446. 'default' => get_option( 'avatar_default' ),
  447. )
  448. ),
  449. );
  450. /**
  451. * Filters the settings to pass to the block editor for all editor type.
  452. *
  453. * @since 5.8.0
  454. *
  455. * @param array $editor_settings Default editor settings.
  456. * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
  457. */
  458. $editor_settings = apply_filters( 'block_editor_settings_all', $editor_settings, $block_editor_context );
  459. if ( ! empty( $block_editor_context->post ) ) {
  460. $post = $block_editor_context->post;
  461. /**
  462. * Filters the settings to pass to the block editor.
  463. *
  464. * @since 5.0.0
  465. * @deprecated 5.8.0 Use the {@see 'block_editor_settings_all'} filter instead.
  466. *
  467. * @param array $editor_settings Default editor settings.
  468. * @param WP_Post $post Post being edited.
  469. */
  470. $editor_settings = apply_filters_deprecated( 'block_editor_settings', array( $editor_settings, $post ), '5.8.0', 'block_editor_settings_all' );
  471. }
  472. return $editor_settings;
  473. }
  474. /**
  475. * Preloads common data used with the block editor by specifying an array of
  476. * REST API paths that will be preloaded for a given block editor context.
  477. *
  478. * @since 5.8.0
  479. *
  480. * @global WP_Post $post Global post object.
  481. * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
  482. * @global WP_Styles $wp_styles The WP_Styles object for printing styles.
  483. *
  484. * @param string[] $preload_paths List of paths to preload.
  485. * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
  486. */
  487. function block_editor_rest_api_preload( array $preload_paths, $block_editor_context ) {
  488. global $post, $wp_scripts, $wp_styles;
  489. /**
  490. * Filters the array of REST API paths that will be used to preloaded common data for the block editor.
  491. *
  492. * @since 5.8.0
  493. *
  494. * @param string[] $preload_paths Array of paths to preload.
  495. * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
  496. */
  497. $preload_paths = apply_filters( 'block_editor_rest_api_preload_paths', $preload_paths, $block_editor_context );
  498. if ( ! empty( $block_editor_context->post ) ) {
  499. $selected_post = $block_editor_context->post;
  500. /**
  501. * Filters the array of paths that will be preloaded.
  502. *
  503. * Preload common data by specifying an array of REST API paths that will be preloaded.
  504. *
  505. * @since 5.0.0
  506. * @deprecated 5.8.0 Use the {@see 'block_editor_rest_api_preload_paths'} filter instead.
  507. *
  508. * @param string[] $preload_paths Array of paths to preload.
  509. * @param WP_Post $selected_post Post being edited.
  510. */
  511. $preload_paths = apply_filters_deprecated( 'block_editor_preload_paths', array( $preload_paths, $selected_post ), '5.8.0', 'block_editor_rest_api_preload_paths' );
  512. }
  513. if ( empty( $preload_paths ) ) {
  514. return;
  515. }
  516. /*
  517. * Ensure the global $post, $wp_scripts, and $wp_styles remain the same after
  518. * API data is preloaded.
  519. * Because API preloading can call the_content and other filters, plugins
  520. * can unexpectedly modify the global $post or enqueue assets which are not
  521. * intended for the block editor.
  522. */
  523. $backup_global_post = ! empty( $post ) ? clone $post : $post;
  524. $backup_wp_scripts = ! empty( $wp_scripts ) ? clone $wp_scripts : $wp_scripts;
  525. $backup_wp_styles = ! empty( $wp_styles ) ? clone $wp_styles : $wp_styles;
  526. foreach ( $preload_paths as &$path ) {
  527. if ( is_string( $path ) && ! str_starts_with( $path, '/' ) ) {
  528. $path = '/' . $path;
  529. continue;
  530. }
  531. if ( is_array( $path ) && is_string( $path[0] ) && ! str_starts_with( $path[0], '/' ) ) {
  532. $path[0] = '/' . $path[0];
  533. }
  534. }
  535. unset( $path );
  536. $preload_data = array_reduce(
  537. $preload_paths,
  538. 'rest_preload_api_request',
  539. array()
  540. );
  541. // Restore the global $post, $wp_scripts, and $wp_styles as they were before API preloading.
  542. $post = $backup_global_post;
  543. $wp_scripts = $backup_wp_scripts;
  544. $wp_styles = $backup_wp_styles;
  545. wp_add_inline_script(
  546. 'wp-api-fetch',
  547. sprintf(
  548. 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );',
  549. wp_json_encode( $preload_data )
  550. ),
  551. 'after'
  552. );
  553. }
  554. /**
  555. * Creates an array of theme styles to load into the block editor.
  556. *
  557. * @since 5.8.0
  558. *
  559. * @global array $editor_styles
  560. *
  561. * @return array An array of theme styles for the block editor.
  562. */
  563. function get_block_editor_theme_styles() {
  564. global $editor_styles;
  565. $styles = array();
  566. if ( $editor_styles && current_theme_supports( 'editor-styles' ) ) {
  567. foreach ( $editor_styles as $style ) {
  568. if ( preg_match( '~^(https?:)?//~', $style ) ) {
  569. $response = wp_remote_get( $style );
  570. if ( ! is_wp_error( $response ) ) {
  571. $styles[] = array(
  572. 'css' => wp_remote_retrieve_body( $response ),
  573. '__unstableType' => 'theme',
  574. 'isGlobalStyles' => false,
  575. );
  576. }
  577. } else {
  578. $file = get_theme_file_path( $style );
  579. if ( is_file( $file ) ) {
  580. $styles[] = array(
  581. 'css' => file_get_contents( $file ),
  582. 'baseURL' => get_theme_file_uri( $style ),
  583. '__unstableType' => 'theme',
  584. 'isGlobalStyles' => false,
  585. );
  586. }
  587. }
  588. }
  589. }
  590. return $styles;
  591. }