edit-form-blocks.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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 $editor_styles
  20. * @global array $wp_meta_boxes
  21. */
  22. global $post_type, $post_type_object, $post, $title, $editor_styles, $wp_meta_boxes;
  23. // Flag that we're loading the block editor.
  24. $current_screen = get_current_screen();
  25. $current_screen->is_block_editor( true );
  26. /*
  27. * Emoji replacement is disabled for now, until it plays nicely with React.
  28. */
  29. remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
  30. wp_enqueue_script( 'heartbeat' );
  31. wp_enqueue_script( 'wp-edit-post' );
  32. wp_enqueue_script( 'wp-format-library' );
  33. $rest_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name;
  34. // Preload common data.
  35. $preload_paths = array(
  36. '/',
  37. '/wp/v2/types?context=edit',
  38. '/wp/v2/taxonomies?per_page=-1&context=edit',
  39. '/wp/v2/themes?status=active',
  40. sprintf( '/wp/v2/%s/%s?context=edit', $rest_base, $post->ID ),
  41. sprintf( '/wp/v2/types/%s?context=edit', $post_type ),
  42. sprintf( '/wp/v2/users/me?post_type=%s&context=edit', $post_type ),
  43. array( '/wp/v2/media', 'OPTIONS' ),
  44. array( '/wp/v2/blocks', 'OPTIONS' ),
  45. sprintf( '/wp/v2/%s/%d/autosaves?context=edit', $rest_base, $post->ID ),
  46. );
  47. /**
  48. * Preload common data by specifying an array of REST API paths that will be preloaded.
  49. *
  50. * Filters the array of paths that will be preloaded.
  51. *
  52. * @since 5.0.0
  53. *
  54. * @param array $preload_paths Array of paths to preload.
  55. * @param object $post The post resource data.
  56. */
  57. $preload_paths = apply_filters( 'block_editor_preload_paths', $preload_paths, $post );
  58. /*
  59. * Ensure the global $post remains the same after API data is preloaded.
  60. * Because API preloading can call the_content and other filters, plugins
  61. * can unexpectedly modify $post.
  62. */
  63. $backup_global_post = $post;
  64. $preload_data = array_reduce(
  65. $preload_paths,
  66. 'rest_preload_api_request',
  67. array()
  68. );
  69. // Restore the global $post as it was before API preloading.
  70. $post = $backup_global_post;
  71. wp_add_inline_script(
  72. 'wp-api-fetch',
  73. sprintf( 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', wp_json_encode( $preload_data ) ),
  74. 'after'
  75. );
  76. wp_add_inline_script(
  77. 'wp-blocks',
  78. sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( get_block_categories( $post ) ) ),
  79. 'after'
  80. );
  81. /*
  82. * Assign initial edits, if applicable. These are not initially assigned to the persisted post,
  83. * but should be included in its save payload.
  84. */
  85. $initial_edits = null;
  86. $is_new_post = false;
  87. if ( 'auto-draft' === $post->post_status ) {
  88. $is_new_post = true;
  89. // Override "(Auto Draft)" new post default title with empty string, or filtered value.
  90. $initial_edits = array(
  91. 'title' => $post->post_title,
  92. 'content' => $post->post_content,
  93. 'excerpt' => $post->post_excerpt,
  94. );
  95. }
  96. // Preload server-registered block schemas.
  97. wp_add_inline_script(
  98. 'wp-blocks',
  99. 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');'
  100. );
  101. // Get admin url for handling meta boxes.
  102. $meta_box_url = admin_url( 'post.php' );
  103. $meta_box_url = add_query_arg(
  104. array(
  105. 'post' => $post->ID,
  106. 'action' => 'edit',
  107. 'meta-box-loader' => true,
  108. 'meta-box-loader-nonce' => wp_create_nonce( 'meta-box-loader' ),
  109. ),
  110. $meta_box_url
  111. );
  112. wp_localize_script( 'wp-editor', '_wpMetaBoxUrl', $meta_box_url );
  113. /*
  114. * Initialize the editor.
  115. */
  116. $align_wide = get_theme_support( 'align-wide' );
  117. $color_palette = current( (array) get_theme_support( 'editor-color-palette' ) );
  118. $font_sizes = current( (array) get_theme_support( 'editor-font-sizes' ) );
  119. /**
  120. * Filters the allowed block types for the editor, defaulting to true (all
  121. * block types supported).
  122. *
  123. * @since 5.0.0
  124. *
  125. * @param bool|array $allowed_block_types Array of block type slugs, or
  126. * boolean to enable/disable all.
  127. * @param object $post The post resource data.
  128. */
  129. $allowed_block_types = apply_filters( 'allowed_block_types', true, $post );
  130. // Get all available templates for the post/page attributes meta-box.
  131. // The "Default template" array element should only be added if the array is
  132. // not empty so we do not trigger the template select element without any options
  133. // besides the default value.
  134. $available_templates = wp_get_theme()->get_page_templates( get_post( $post->ID ) );
  135. $available_templates = ! empty( $available_templates ) ? array_merge(
  136. array(
  137. /** This filter is documented in wp-admin/includes/meta-boxes.php */
  138. '' => apply_filters( 'default_page_template_title', __( 'Default template' ), 'rest-api' ),
  139. ),
  140. $available_templates
  141. ) : $available_templates;
  142. // Media settings.
  143. $max_upload_size = wp_max_upload_size();
  144. if ( ! $max_upload_size ) {
  145. $max_upload_size = 0;
  146. }
  147. // Editor Styles.
  148. $styles = array(
  149. array(
  150. 'css' => file_get_contents(
  151. ABSPATH . WPINC . '/css/dist/editor/editor-styles.css'
  152. ),
  153. ),
  154. );
  155. /* translators: Use this to specify the CSS font family for the default font. */
  156. $locale_font_family = esc_html_x( 'Noto Serif', 'CSS Font Family for Editor Font' );
  157. $styles[] = array(
  158. 'css' => "body { font-family: '$locale_font_family' }",
  159. );
  160. if ( $editor_styles && current_theme_supports( 'editor-styles' ) ) {
  161. foreach ( $editor_styles as $style ) {
  162. if ( preg_match( '~^(https?:)?//~', $style ) ) {
  163. $response = wp_remote_get( $style );
  164. if ( ! is_wp_error( $response ) ) {
  165. $styles[] = array(
  166. 'css' => wp_remote_retrieve_body( $response ),
  167. );
  168. }
  169. } else {
  170. $file = get_theme_file_path( $style );
  171. if ( is_file( $file ) ) {
  172. $styles[] = array(
  173. 'css' => file_get_contents( $file ),
  174. 'baseURL' => get_theme_file_uri( $style ),
  175. );
  176. }
  177. }
  178. }
  179. }
  180. // Image sizes.
  181. /** This filter is documented in wp-admin/includes/media.php */
  182. $image_size_names = apply_filters(
  183. 'image_size_names_choose',
  184. array(
  185. 'thumbnail' => __( 'Thumbnail' ),
  186. 'medium' => __( 'Medium' ),
  187. 'large' => __( 'Large' ),
  188. 'full' => __( 'Full Size' ),
  189. )
  190. );
  191. $available_image_sizes = array();
  192. foreach ( $image_size_names as $image_size_slug => $image_size_name ) {
  193. $available_image_sizes[] = array(
  194. 'slug' => $image_size_slug,
  195. 'name' => $image_size_name,
  196. );
  197. }
  198. // Lock settings.
  199. $user_id = wp_check_post_lock( $post->ID );
  200. if ( $user_id ) {
  201. $locked = false;
  202. /** This filter is documented in wp-admin/includes/post.php */
  203. if ( apply_filters( 'show_post_locked_dialog', true, $post, $user_id ) ) {
  204. $locked = true;
  205. }
  206. $user_details = null;
  207. if ( $locked ) {
  208. $user = get_userdata( $user_id );
  209. $user_details = array(
  210. 'name' => $user->display_name,
  211. );
  212. $avatar = get_avatar_url( $user_id, array( 'size' => 64 ) );
  213. }
  214. $lock_details = array(
  215. 'isLocked' => $locked,
  216. 'user' => $user_details,
  217. );
  218. } else {
  219. // Lock the post.
  220. $active_post_lock = wp_set_post_lock( $post->ID );
  221. if ( $active_post_lock ) {
  222. $active_post_lock = esc_attr( implode( ':', $active_post_lock ) );
  223. }
  224. $lock_details = array(
  225. 'isLocked' => false,
  226. 'activePostLock' => $active_post_lock,
  227. );
  228. }
  229. /**
  230. * Filters the body placeholder text.
  231. *
  232. * @since 5.0.0
  233. *
  234. * @param string $text Placeholder text. Default 'Start writing or type / to choose a block'.
  235. * @param WP_Post $post Post object.
  236. */
  237. $body_placeholder = apply_filters( 'write_your_story', __( 'Start writing or type / to choose a block' ), $post );
  238. $editor_settings = array(
  239. 'alignWide' => $align_wide,
  240. 'availableTemplates' => $available_templates,
  241. 'allowedBlockTypes' => $allowed_block_types,
  242. 'disableCustomColors' => get_theme_support( 'disable-custom-colors' ),
  243. 'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ),
  244. 'disablePostFormats' => ! current_theme_supports( 'post-formats' ),
  245. /** This filter is documented in wp-admin/edit-form-advanced.php */
  246. 'titlePlaceholder' => apply_filters( 'enter_title_here', __( 'Add title' ), $post ),
  247. 'bodyPlaceholder' => $body_placeholder,
  248. 'isRTL' => is_rtl(),
  249. 'autosaveInterval' => AUTOSAVE_INTERVAL,
  250. 'maxUploadFileSize' => $max_upload_size,
  251. 'allowedMimeTypes' => get_allowed_mime_types(),
  252. 'styles' => $styles,
  253. 'imageSizes' => $available_image_sizes,
  254. 'richEditingEnabled' => user_can_richedit(),
  255. 'postLock' => $lock_details,
  256. 'postLockUtils' => array(
  257. 'nonce' => wp_create_nonce( 'lock-post_' . $post->ID ),
  258. 'unlockNonce' => wp_create_nonce( 'update-post_' . $post->ID ),
  259. 'ajaxUrl' => admin_url( 'admin-ajax.php' ),
  260. ),
  261. // Whether or not to load the 'postcustom' meta box is stored as a user meta
  262. // field so that we're not always loading its assets.
  263. 'enableCustomFields' => (bool) get_user_meta( get_current_user_id(), 'enable_custom_fields', true ),
  264. );
  265. $autosave = wp_get_post_autosave( $post_ID );
  266. if ( $autosave ) {
  267. if ( mysql2date( 'U', $autosave->post_modified_gmt, false ) > mysql2date( 'U', $post->post_modified_gmt, false ) ) {
  268. $editor_settings['autosave'] = array(
  269. 'editLink' => get_edit_post_link( $autosave->ID ),
  270. );
  271. } else {
  272. wp_delete_post_revision( $autosave->ID );
  273. }
  274. }
  275. if ( false !== $color_palette ) {
  276. $editor_settings['colors'] = $color_palette;
  277. }
  278. if ( false !== $font_sizes ) {
  279. $editor_settings['fontSizes'] = $font_sizes;
  280. }
  281. if ( ! empty( $post_type_object->template ) ) {
  282. $editor_settings['template'] = $post_type_object->template;
  283. $editor_settings['templateLock'] = ! empty( $post_type_object->template_lock ) ? $post_type_object->template_lock : false;
  284. }
  285. // If there's no template set on a new post, use the post format, instead.
  286. if ( $is_new_post && ! isset( $editor_settings['template'] ) && 'post' === $post->post_type ) {
  287. $post_format = get_post_format( $post );
  288. if ( in_array( $post_format, array( 'audio', 'gallery', 'image', 'quote', 'video' ), true ) ) {
  289. $editor_settings['template'] = array( array( "core/$post_format" ) );
  290. }
  291. }
  292. /**
  293. * Scripts
  294. */
  295. wp_enqueue_media(
  296. array(
  297. 'post' => $post->ID,
  298. )
  299. );
  300. wp_tinymce_inline_scripts();
  301. wp_enqueue_editor();
  302. /**
  303. * Styles
  304. */
  305. wp_enqueue_style( 'wp-edit-post' );
  306. wp_enqueue_style( 'wp-format-library' );
  307. /**
  308. * Fires after block assets have been enqueued for the editing interface.
  309. *
  310. * Call `add_action` on any hook before 'admin_enqueue_scripts'.
  311. *
  312. * In the function call you supply, simply use `wp_enqueue_script` and
  313. * `wp_enqueue_style` to add your functionality to the block editor.
  314. *
  315. * @since 5.0.0
  316. */
  317. do_action( 'enqueue_block_editor_assets' );
  318. // In order to duplicate classic meta box behaviour, we need to run the classic meta box actions.
  319. require_once( ABSPATH . 'wp-admin/includes/meta-boxes.php' );
  320. register_and_do_post_meta_boxes( $post );
  321. // Check if the Custom Fields meta box has been removed at some point.
  322. $core_meta_boxes = $wp_meta_boxes[ $current_screen->id ]['normal']['core'];
  323. if ( ! isset( $core_meta_boxes['postcustom'] ) || ! $core_meta_boxes['postcustom'] ) {
  324. unset( $editor_settings['enableCustomFields'] );
  325. }
  326. /**
  327. * Filters the settings to pass to the block editor.
  328. *
  329. * @since 5.0.0
  330. *
  331. * @param array $editor_settings Default editor settings.
  332. * @param WP_Post $post Post being edited.
  333. */
  334. $editor_settings = apply_filters( 'block_editor_settings', $editor_settings, $post );
  335. $init_script = <<<JS
  336. ( function() {
  337. window._wpLoadBlockEditor = new Promise( function( resolve ) {
  338. wp.domReady( function() {
  339. resolve( wp.editPost.initializeEditor( 'editor', "%s", %d, %s, %s ) );
  340. } );
  341. } );
  342. } )();
  343. JS;
  344. $script = sprintf(
  345. $init_script,
  346. $post->post_type,
  347. $post->ID,
  348. wp_json_encode( $editor_settings ),
  349. wp_json_encode( $initial_edits )
  350. );
  351. wp_add_inline_script( 'wp-edit-post', $script );
  352. require_once( ABSPATH . 'wp-admin/admin-header.php' );
  353. ?>
  354. <div class="block-editor">
  355. <h1 class="screen-reader-text hide-if-no-js"><?php echo esc_html( $title ); ?></h1>
  356. <div id="editor" class="block-editor__container hide-if-no-js"></div>
  357. <div id="metaboxes" class="hidden">
  358. <?php the_block_editor_meta_boxes(); ?>
  359. </div>
  360. <?php // JavaScript is disabled. ?>
  361. <div class="wrap hide-if-js block-editor-no-js">
  362. <h1 class="wp-heading-inline"><?php echo esc_html( $title ); ?></h1>
  363. <div class="notice notice-error notice-alt">
  364. <p>
  365. <?php
  366. $message = sprintf(
  367. /* translators: %s: A link to install the Classic Editor plugin. */
  368. __( 'The block editor requires JavaScript. Please enable JavaScript in your browser settings, or try the <a href="%s">Classic Editor plugin</a>.' ),
  369. esc_url( wp_nonce_url( self_admin_url( 'plugin-install.php?tab=favorites&user=wordpressdotorg&save=0' ), 'save_wporg_username_' . get_current_user_id() ) )
  370. );
  371. /**
  372. * Filters the message displayed in the block editor interface when JavaScript is
  373. * not enabled in the browser.
  374. *
  375. * @since 5.0.3
  376. *
  377. * @param string $message The message being displayed.
  378. * @param WP_Post $post The post being edited.
  379. */
  380. echo apply_filters( 'block_editor_no_javascript_message', $message, $post );
  381. ?>
  382. </p>
  383. </div>
  384. </div>
  385. </div>