PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-admin/edit-form-blocks.php

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