edit-form-blocks.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. /**
  3. * The block editor page.
  4. *
  5. * @since 5.0.0
  6. *
  7. * @package WordPress
  8. * @subpackage Administration
  9. */
  10. // Don't load directly.
  11. if ( ! defined( 'ABSPATH' ) ) {
  12. die( '-1' );
  13. }
  14. /**
  15. * @global string $post_type
  16. * @global WP_Post_Type $post_type_object
  17. * @global WP_Post $post Global post object.
  18. * @global string $title
  19. * @global array $wp_meta_boxes
  20. */
  21. global $post_type, $post_type_object, $post, $title, $wp_meta_boxes;
  22. $block_editor_context = new WP_Block_Editor_Context( array( 'post' => $post ) );
  23. // Flag that we're loading the block editor.
  24. $current_screen = get_current_screen();
  25. $current_screen->is_block_editor( true );
  26. // Default to is-fullscreen-mode to avoid jumps in the UI.
  27. add_filter(
  28. 'admin_body_class',
  29. static function( $classes ) {
  30. return "$classes is-fullscreen-mode";
  31. }
  32. );
  33. /*
  34. * Emoji replacement is disabled for now, until it plays nicely with React.
  35. */
  36. remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
  37. /*
  38. * Block editor implements its own Options menu for toggling Document Panels.
  39. */
  40. add_filter( 'screen_options_show_screen', '__return_false' );
  41. wp_enqueue_script( 'heartbeat' );
  42. wp_enqueue_script( 'wp-edit-post' );
  43. $rest_path = rest_get_route_for_post( $post );
  44. // Preload common data.
  45. $preload_paths = array(
  46. '/wp/v2/types?context=view',
  47. '/wp/v2/taxonomies?context=view',
  48. add_query_arg(
  49. array(
  50. 'context' => 'edit',
  51. 'per_page' => -1,
  52. ),
  53. rest_get_route_for_post_type_items( 'wp_block' )
  54. ),
  55. add_query_arg( 'context', 'edit', $rest_path ),
  56. sprintf( '/wp/v2/types/%s?context=edit', $post_type ),
  57. '/wp/v2/users/me',
  58. array( rest_get_route_for_post_type_items( 'attachment' ), 'OPTIONS' ),
  59. array( rest_get_route_for_post_type_items( 'page' ), 'OPTIONS' ),
  60. array( rest_get_route_for_post_type_items( 'wp_block' ), 'OPTIONS' ),
  61. array( rest_get_route_for_post_type_items( 'wp_template' ), 'OPTIONS' ),
  62. sprintf( '%s/autosaves?context=edit', $rest_path ),
  63. '/wp/v2/settings',
  64. array( '/wp/v2/settings', 'OPTIONS' ),
  65. );
  66. block_editor_rest_api_preload( $preload_paths, $block_editor_context );
  67. wp_add_inline_script(
  68. 'wp-blocks',
  69. sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( get_block_categories( $post ) ) ),
  70. 'after'
  71. );
  72. /*
  73. * Assign initial edits, if applicable. These are not initially assigned to the persisted post,
  74. * but should be included in its save payload.
  75. */
  76. $initial_edits = array();
  77. $is_new_post = false;
  78. if ( 'auto-draft' === $post->post_status ) {
  79. $is_new_post = true;
  80. // Override "(Auto Draft)" new post default title with empty string, or filtered value.
  81. if ( post_type_supports( $post->post_type, 'title' ) ) {
  82. $initial_edits['title'] = $post->post_title;
  83. }
  84. if ( post_type_supports( $post->post_type, 'editor' ) ) {
  85. $initial_edits['content'] = $post->post_content;
  86. }
  87. if ( post_type_supports( $post->post_type, 'excerpt' ) ) {
  88. $initial_edits['excerpt'] = $post->post_excerpt;
  89. }
  90. }
  91. // Preload server-registered block schemas.
  92. wp_add_inline_script(
  93. 'wp-blocks',
  94. 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');'
  95. );
  96. // Get admin url for handling meta boxes.
  97. $meta_box_url = admin_url( 'post.php' );
  98. $meta_box_url = add_query_arg(
  99. array(
  100. 'post' => $post->ID,
  101. 'action' => 'edit',
  102. 'meta-box-loader' => true,
  103. 'meta-box-loader-nonce' => wp_create_nonce( 'meta-box-loader' ),
  104. ),
  105. $meta_box_url
  106. );
  107. wp_add_inline_script(
  108. 'wp-editor',
  109. sprintf( 'var _wpMetaBoxUrl = %s;', wp_json_encode( $meta_box_url ) ),
  110. 'before'
  111. );
  112. /*
  113. * Get all available templates for the post/page attributes meta-box.
  114. * The "Default template" array element should only be added if the array is
  115. * not empty so we do not trigger the template select element without any options
  116. * besides the default value.
  117. */
  118. $available_templates = wp_get_theme()->get_page_templates( get_post( $post->ID ) );
  119. $available_templates = ! empty( $available_templates ) ? array_replace(
  120. array(
  121. /** This filter is documented in wp-admin/includes/meta-boxes.php */
  122. '' => apply_filters( 'default_page_template_title', __( 'Default template' ), 'rest-api' ),
  123. ),
  124. $available_templates
  125. ) : $available_templates;
  126. // Lock settings.
  127. $user_id = wp_check_post_lock( $post->ID );
  128. if ( $user_id ) {
  129. $locked = false;
  130. /** This filter is documented in wp-admin/includes/post.php */
  131. if ( apply_filters( 'show_post_locked_dialog', true, $post, $user_id ) ) {
  132. $locked = true;
  133. }
  134. $user_details = null;
  135. if ( $locked ) {
  136. $user = get_userdata( $user_id );
  137. $user_details = array(
  138. 'avatar' => get_avatar_url( $user_id, array( 'size' => 128 ) ),
  139. 'name' => $user->display_name,
  140. );
  141. }
  142. $lock_details = array(
  143. 'isLocked' => $locked,
  144. 'user' => $user_details,
  145. );
  146. } else {
  147. // Lock the post.
  148. $active_post_lock = wp_set_post_lock( $post->ID );
  149. if ( $active_post_lock ) {
  150. $active_post_lock = esc_attr( implode( ':', $active_post_lock ) );
  151. }
  152. $lock_details = array(
  153. 'isLocked' => false,
  154. 'activePostLock' => $active_post_lock,
  155. );
  156. }
  157. /**
  158. * Filters the body placeholder text.
  159. *
  160. * @since 5.0.0
  161. * @since 5.8.0 Changed the default placeholder text.
  162. *
  163. * @param string $text Placeholder text. Default 'Type / to choose a block'.
  164. * @param WP_Post $post Post object.
  165. */
  166. $body_placeholder = apply_filters( 'write_your_story', __( 'Type / to choose a block' ), $post );
  167. $editor_settings = array(
  168. 'availableTemplates' => $available_templates,
  169. 'disablePostFormats' => ! current_theme_supports( 'post-formats' ),
  170. /** This filter is documented in wp-admin/edit-form-advanced.php */
  171. 'titlePlaceholder' => apply_filters( 'enter_title_here', __( 'Add title' ), $post ),
  172. 'bodyPlaceholder' => $body_placeholder,
  173. 'autosaveInterval' => AUTOSAVE_INTERVAL,
  174. 'richEditingEnabled' => user_can_richedit(),
  175. 'postLock' => $lock_details,
  176. 'postLockUtils' => array(
  177. 'nonce' => wp_create_nonce( 'lock-post_' . $post->ID ),
  178. 'unlockNonce' => wp_create_nonce( 'update-post_' . $post->ID ),
  179. 'ajaxUrl' => admin_url( 'admin-ajax.php' ),
  180. ),
  181. 'supportsLayout' => WP_Theme_JSON_Resolver::theme_has_support(),
  182. 'supportsTemplateMode' => current_theme_supports( 'block-templates' ),
  183. // Whether or not to load the 'postcustom' meta box is stored as a user meta
  184. // field so that we're not always loading its assets.
  185. 'enableCustomFields' => (bool) get_user_meta( get_current_user_id(), 'enable_custom_fields', true ),
  186. );
  187. // Add additional back-compat patterns registered by `current_screen` et al.
  188. $editor_settings['__experimentalAdditionalBlockPatterns'] = WP_Block_Patterns_Registry::get_instance()->get_all_registered( true );
  189. $editor_settings['__experimentalAdditionalBlockPatternCategories'] = WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered( true );
  190. $autosave = wp_get_post_autosave( $post->ID );
  191. if ( $autosave ) {
  192. if ( mysql2date( 'U', $autosave->post_modified_gmt, false ) > mysql2date( 'U', $post->post_modified_gmt, false ) ) {
  193. $editor_settings['autosave'] = array(
  194. 'editLink' => get_edit_post_link( $autosave->ID ),
  195. );
  196. } else {
  197. wp_delete_post_revision( $autosave->ID );
  198. }
  199. }
  200. if ( ! empty( $post_type_object->template ) ) {
  201. $editor_settings['template'] = $post_type_object->template;
  202. $editor_settings['templateLock'] = ! empty( $post_type_object->template_lock ) ? $post_type_object->template_lock : false;
  203. }
  204. // If there's no template set on a new post, use the post format, instead.
  205. if ( $is_new_post && ! isset( $editor_settings['template'] ) && 'post' === $post->post_type ) {
  206. $post_format = get_post_format( $post );
  207. if ( in_array( $post_format, array( 'audio', 'gallery', 'image', 'quote', 'video' ), true ) ) {
  208. $editor_settings['template'] = array( array( "core/$post_format" ) );
  209. }
  210. }
  211. if ( wp_is_block_theme() && $editor_settings['supportsTemplateMode'] ) {
  212. $editor_settings['defaultTemplatePartAreas'] = get_allowed_block_template_part_areas();
  213. }
  214. /**
  215. * Scripts
  216. */
  217. wp_enqueue_media(
  218. array(
  219. 'post' => $post->ID,
  220. )
  221. );
  222. wp_tinymce_inline_scripts();
  223. wp_enqueue_editor();
  224. /**
  225. * Styles
  226. */
  227. wp_enqueue_style( 'wp-edit-post' );
  228. /**
  229. * Fires after block assets have been enqueued for the editing interface.
  230. *
  231. * Call `add_action` on any hook before 'admin_enqueue_scripts'.
  232. *
  233. * In the function call you supply, simply use `wp_enqueue_script` and
  234. * `wp_enqueue_style` to add your functionality to the block editor.
  235. *
  236. * @since 5.0.0
  237. */
  238. do_action( 'enqueue_block_editor_assets' );
  239. // In order to duplicate classic meta box behaviour, we need to run the classic meta box actions.
  240. require_once ABSPATH . 'wp-admin/includes/meta-boxes.php';
  241. register_and_do_post_meta_boxes( $post );
  242. // Check if the Custom Fields meta box has been removed at some point.
  243. $core_meta_boxes = $wp_meta_boxes[ $current_screen->id ]['normal']['core'];
  244. if ( ! isset( $core_meta_boxes['postcustom'] ) || ! $core_meta_boxes['postcustom'] ) {
  245. unset( $editor_settings['enableCustomFields'] );
  246. }
  247. $editor_settings = get_block_editor_settings( $editor_settings, $block_editor_context );
  248. $init_script = <<<JS
  249. ( function() {
  250. window._wpLoadBlockEditor = new Promise( function( resolve ) {
  251. wp.domReady( function() {
  252. resolve( wp.editPost.initializeEditor( 'editor', "%s", %d, %s, %s ) );
  253. } );
  254. } );
  255. } )();
  256. JS;
  257. $script = sprintf(
  258. $init_script,
  259. $post->post_type,
  260. $post->ID,
  261. wp_json_encode( $editor_settings ),
  262. wp_json_encode( $initial_edits )
  263. );
  264. wp_add_inline_script( 'wp-edit-post', $script );
  265. if ( (int) get_option( 'page_for_posts' ) === $post->ID ) {
  266. add_action( 'admin_enqueue_scripts', '_wp_block_editor_posts_page_notice' );
  267. }
  268. require_once ABSPATH . 'wp-admin/admin-header.php';
  269. ?>
  270. <div class="block-editor">
  271. <h1 class="screen-reader-text hide-if-no-js"><?php echo esc_html( $title ); ?></h1>
  272. <div id="editor" class="block-editor__container hide-if-no-js"></div>
  273. <div id="metaboxes" class="hidden">
  274. <?php the_block_editor_meta_boxes(); ?>
  275. </div>
  276. <?php // JavaScript is disabled. ?>
  277. <div class="wrap hide-if-js block-editor-no-js">
  278. <h1 class="wp-heading-inline"><?php echo esc_html( $title ); ?></h1>
  279. <div class="notice notice-error notice-alt">
  280. <p>
  281. <?php
  282. $message = sprintf(
  283. /* translators: %s: A link to install the Classic Editor plugin. */
  284. __( 'The block editor requires JavaScript. Please enable JavaScript in your browser settings, or try the <a href="%s">Classic Editor plugin</a>.' ),
  285. esc_url( wp_nonce_url( self_admin_url( 'plugin-install.php?tab=favorites&user=wordpressdotorg&save=0' ), 'save_wporg_username_' . get_current_user_id() ) )
  286. );
  287. /**
  288. * Filters the message displayed in the block editor interface when JavaScript is
  289. * not enabled in the browser.
  290. *
  291. * @since 5.0.3
  292. *
  293. * @param string $message The message being displayed.
  294. * @param WP_Post $post The post being edited.
  295. */
  296. echo apply_filters( 'block_editor_no_javascript_message', $message, $post );
  297. ?>
  298. </p>
  299. </div>
  300. </div>
  301. </div>