Нет описания

edit-form-blocks.php 10KB

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