widgets-form-blocks.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * The block-based widgets editor, for use in widgets.php.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. // Don't load directly.
  9. if ( ! defined( 'ABSPATH' ) ) {
  10. die( '-1' );
  11. }
  12. // Flag that we're loading the block editor.
  13. $current_screen = get_current_screen();
  14. $current_screen->is_block_editor( true );
  15. $block_editor_context = new WP_Block_Editor_Context( array( 'name' => 'core/edit-widgets' ) );
  16. $preload_paths = array(
  17. array( rest_get_route_for_post_type_items( 'attachment' ), 'OPTIONS' ),
  18. '/wp/v2/widget-types?context=edit&per_page=-1',
  19. '/wp/v2/sidebars?context=edit&per_page=-1',
  20. '/wp/v2/widgets?context=edit&per_page=-1&_embed=about',
  21. );
  22. block_editor_rest_api_preload( $preload_paths, $block_editor_context );
  23. $editor_settings = get_block_editor_settings(
  24. array_merge( get_legacy_widget_block_editor_settings(), array( 'styles' => get_block_editor_theme_styles() ) ),
  25. $block_editor_context
  26. );
  27. // The widgets editor does not support the Block Directory, so don't load any of
  28. // its assets. This also prevents 'wp-editor' from being enqueued which we
  29. // cannot load in the widgets screen because many widget scripts rely on `wp.editor`.
  30. remove_action( 'enqueue_block_editor_assets', 'wp_enqueue_editor_block_directory_assets' );
  31. wp_add_inline_script(
  32. 'wp-edit-widgets',
  33. sprintf(
  34. 'wp.domReady( function() {
  35. wp.editWidgets.initialize( "widgets-editor", %s );
  36. } );',
  37. wp_json_encode( $editor_settings )
  38. )
  39. );
  40. // Preload server-registered block schemas.
  41. wp_add_inline_script(
  42. 'wp-blocks',
  43. 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');'
  44. );
  45. wp_add_inline_script(
  46. 'wp-blocks',
  47. sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( get_block_categories( $block_editor_context ) ) ),
  48. 'after'
  49. );
  50. wp_enqueue_script( 'wp-edit-widgets' );
  51. wp_enqueue_script( 'admin-widgets' );
  52. wp_enqueue_style( 'wp-edit-widgets' );
  53. /** This action is documented in wp-admin/edit-form-blocks.php */
  54. do_action( 'enqueue_block_editor_assets' );
  55. /** This action is documented in wp-admin/widgets-form.php */
  56. do_action( 'sidebar_admin_setup' );
  57. require_once ABSPATH . 'wp-admin/admin-header.php';
  58. /** This action is documented in wp-admin/widgets-form.php */
  59. do_action( 'widgets_admin_page' );
  60. ?>
  61. <div id="widgets-editor" class="blocks-widgets-container"></div>
  62. <?php
  63. /** This action is documented in wp-admin/widgets-form.php */
  64. do_action( 'sidebar_admin_page' );
  65. require_once ABSPATH . 'wp-admin/admin-footer.php';