diff --git a/wp-admin/edit-form-blocks.php b/wp-admin/edit-form-blocks.php new file mode 100644 index 0000000000..e8f9d73b43 --- /dev/null +++ b/wp-admin/edit-form-blocks.php @@ -0,0 +1,360 @@ +labels->edit_item; +} + +// Flag that we're loading the block editor. +$current_screen = get_current_screen(); +$current_screen->is_block_editor( true ); + +/* + * Emoji replacement is disabled for now, until it plays nicely with React. + */ +remove_action( 'admin_print_scripts', 'print_emoji_detection_script' ); + +wp_enqueue_script( 'heartbeat' ); +wp_enqueue_script( 'wp-edit-post' ); + +$rest_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name; + +// Preload common data. +$preload_paths = array( + '/', + '/wp/v2/types?context=edit', + '/wp/v2/taxonomies?per_page=-1&context=edit', + '/wp/v2/themes?status=active', + sprintf( '/wp/v2/%s/%s?context=edit', $rest_base, $post->ID ), + sprintf( '/wp/v2/types/%s?context=edit', $post_type ), + sprintf( '/wp/v2/users/me?post_type=%s&context=edit', $post_type ), +); + +/* + * Ensure the global $post remains the same after API data is preloaded. + * Because API preloading can call the_content and other filters, plugins + * can unexpectedly modify $post. + */ +$backup_global_post = $post; + +$preload_data = array_reduce( + $preload_paths, + 'rest_preload_api_request', + array() +); + +// Restore the global $post as it was before API preloading. +$post = $backup_global_post; + +wp_add_inline_script( + 'wp-api-fetch', + sprintf( 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', wp_json_encode( $preload_data ) ), + 'after' +); + +wp_add_inline_script( + 'wp-blocks', + sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( get_block_categories( $post ) ) ), + 'after' +); + +/* + * Assign initial edits, if applicable. These are not initially assigned to the persisted post, + * but should be included in its save payload. + */ +$initial_edits = null; +$is_new_post = false; +if ( 'auto-draft' === $post->post_status ) { + $is_new_post = true; + // Override "(Auto Draft)" new post default title with empty string, or filtered value. + $initial_edits = array( + 'title' => array( + 'raw' => $post->post_title, + ), + 'content' => array( + 'raw' => $post->post_content, + ), + 'excerpt' => array( + 'raw' => $post->post_excerpt, + ), + ); +} + +// Preload server-registered block schemas. +wp_add_inline_script( + 'wp-blocks', + 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');' +); + +// Get admin url for handling meta boxes. +$meta_box_url = admin_url( 'post.php' ); +$meta_box_url = add_query_arg( + array( + 'post' => $post->ID, + 'action' => 'edit', + 'meta-box-loader' => true, + '_wpnonce' => wp_create_nonce( 'meta-box-loader' ), + ), + $meta_box_url +); +wp_localize_script( 'wp-editor', '_wpMetaBoxUrl', $meta_box_url ); + + +// Populate default code editor settings by short-circuiting wp_enqueue_code_editor. +wp_add_inline_script( + 'wp-editor', + sprintf( + 'window._wpGutenbergCodeEditorSettings = %s;', + wp_json_encode( wp_get_code_editor_settings( array( 'type' => 'text/html' ) ) ) + ) +); + +/* + * Initialize the editor. + */ + +$align_wide = get_theme_support( 'align-wide' ); +$color_palette = current( (array) get_theme_support( 'editor-color-palette' ) ); +$font_sizes = current( (array) get_theme_support( 'editor-font-sizes' ) ); + +/** + * Filters the allowed block types for the editor, defaulting to true (all + * block types supported). + * + * @since 5.0.0 + * + * @param bool|array $allowed_block_types Array of block type slugs, or + * boolean to enable/disable all. + * @param object $post The post resource data. + */ +$allowed_block_types = apply_filters( 'allowed_block_types', true, $post ); + +// Get all available templates for the post/page attributes meta-box. +// The "Default template" array element should only be added if the array is +// not empty so we do not trigger the template select element without any options +// besides the default value. +$available_templates = wp_get_theme()->get_page_templates( get_post( $post->ID ) ); +$available_templates = ! empty( $available_templates ) ? array_merge( + array( + /** This filter is documented in wp-admin/includes/meta-boxes.php */ + '' => apply_filters( 'default_page_template_title', __( 'Default template' ), 'rest-api' ), + ), + $available_templates +) : $available_templates; + +// Media settings. +$max_upload_size = wp_max_upload_size(); +if ( ! $max_upload_size ) { + $max_upload_size = 0; +} + +// Editor Styles. +$styles = array( + array( + 'css' => file_get_contents( + ABSPATH . WPINC . '/css/dist/editor/editor-styles.css' + ), + ), +); +if ( $editor_styles && current_theme_supports( 'editor-styles' ) ) { + foreach ( $editor_styles as $style ) { + if ( preg_match( '~^(https?:)?//~', $style ) ) { + $response = wp_remote_get( $style ); + if ( ! is_wp_error( $response ) ) { + $styles[] = array( + 'css' => wp_remote_retrieve_body( $response ), + ); + } + } else { + $file = get_theme_file_path( $style ); + $styles[] = array( + 'css' => file_get_contents( get_theme_file_path( $style ) ), + 'baseURL' => get_theme_file_uri( $style ), + ); + } + } +} + +// Lock settings. +$user_id = wp_check_post_lock( $post->ID ); +if ( $user_id ) { + /** This filter is documented in wp-admin/includes/post.php */ + if ( apply_filters( 'show_post_locked_dialog', true, $post, $user_id ) ) { + $locked = true; + } + + $user_details = null; + if ( $locked ) { + $user = get_userdata( $user_id ); + $user_details = array( + 'name' => $user->display_name, + ); + $avatar = get_avatar_url( $user_id, array( 'size' => 64 ) ); + } + + $lock_details = array( + 'isLocked' => $locked, + 'user' => $user_details, + ); +} else { + // Lock the post. + $active_post_lock = wp_set_post_lock( $post->ID ); + $lock_details = array( + 'isLocked' => false, + 'activePostLock' => esc_attr( implode( ':', $active_post_lock ) ), + ); +} + +/** + * Filters the body placeholder text. + * + * @since 5.0.0 + * + * @param string $text Placeholder text. Default 'Write your story'. + * @param WP_Post $post Post object. + */ +$body_placeholder = apply_filters( 'write_your_story', __( 'Write your story' ), $post ); + +$editor_settings = array( + 'alignWide' => $align_wide, + 'availableTemplates' => $available_templates, + 'allowedBlockTypes' => $allowed_block_types, + 'disableCustomColors' => get_theme_support( 'disable-custom-colors' ), + 'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ), + 'disablePostFormats' => ! current_theme_supports( 'post-formats' ), + /** This filter is documented in wp-admin/edit-form-advanced.php */ + 'titlePlaceholder' => apply_filters( 'enter_title_here', __( 'Add title' ), $post ), + 'bodyPlaceholder' => $body_placeholder, + 'isRTL' => is_rtl(), + 'autosaveInterval' => 10, + 'maxUploadFileSize' => $max_upload_size, + 'allowedMimeTypes' => get_allowed_mime_types(), + 'styles' => $styles, + 'postLock' => $lock_details, + 'postLockUtils' => array( + 'nonce' => wp_create_nonce( 'lock-post_' . $post->ID ), + 'unlockNonce' => wp_create_nonce( 'update-post_' . $post->ID ), + 'ajaxUrl' => admin_url( 'admin-ajax.php' ), + ), +); + +$autosave = wp_get_post_autosave( $post_ID ); +if ( $autosave ) { + if ( mysql2date( 'U', $autosave->post_modified_gmt, false ) > mysql2date( 'U', $post->post_modified_gmt, false ) ) { + $editor_settings['autosave'] = array( + 'editLink' => get_edit_post_link( $autosave->ID ), + ); + } else { + wp_delete_post_revision( $autosave->ID ); + } +} + +if ( false !== $color_palette ) { + $editor_settings['colors'] = $color_palette; +} + +if ( ! empty( $font_sizes ) ) { + $editor_settings['fontSizes'] = $font_sizes; +} + +if ( ! empty( $post_type_object->template ) ) { + $editor_settings['template'] = $post_type_object->template; + $editor_settings['templateLock'] = ! empty( $post_type_object->template_lock ) ? $post_type_object->template_lock : false; +} + +// If there's no template set on a new post, use the post format, instead. +if ( $is_new_post && ! isset( $editor_settings['template'] ) && 'post' === $post->post_type ) { + $post_format = get_post_format( $post ); + if ( in_array( $post_format, array( 'audio', 'gallery', 'image', 'quote', 'video' ), true ) ) { + $editor_settings['template'] = array( array( "core/$post_format" ) ); + } +} + +$init_script = <<post_type, + $post->ID, + wp_json_encode( $editor_settings ), + wp_json_encode( $initial_edits ) +); +wp_add_inline_script( 'wp-edit-post', $script ); + +/** + * Scripts + */ +wp_enqueue_media( + array( + 'post' => $post->ID, + ) +); +wp_enqueue_editor(); + +/** + * Styles + */ +wp_enqueue_style( 'wp-edit-post' ); + +/** + * Fires after block assets have been enqueued for the editing interface. + * + * Call `add_action` on any hook before 'admin_enqueue_scripts'. + * + * In the function call you supply, simply use `wp_enqueue_script` and + * `wp_enqueue_style` to add your functionality to the block editor. + * + * @since 5.0.0 + */ +do_action( 'enqueue_block_editor_assets' ); + +require_once( ABSPATH . 'wp-admin/admin-header.php' ); +?> + +
+

labels->edit_item ); ?>

+
+ +
diff --git a/wp-admin/post-new.php b/wp-admin/post-new.php index abd0baf33a..153cba2dc4 100644 --- a/wp-admin/post-new.php +++ b/wp-admin/post-new.php @@ -68,8 +68,12 @@ $post_ID = $post->ID; /** This filter is documented in wp-admin/post.php */ if ( apply_filters( 'replace_editor', false, $post ) !== true ) { - wp_enqueue_script( 'autosave' ); - include( ABSPATH . 'wp-admin/edit-form-advanced.php' ); + if ( use_block_editor_for_post( $post ) ) { + include( ABSPATH . 'wp-admin/edit-form-blocks.php' ); + } else { + wp_enqueue_script( 'autosave' ); + include( ABSPATH . 'wp-admin/edit-form-advanced.php' ); + } } include( ABSPATH . 'wp-admin/admin-footer.php' ); diff --git a/wp-admin/post.php b/wp-admin/post.php index 052ce8e448..3a899cb4f3 100644 --- a/wp-admin/post.php +++ b/wp-admin/post.php @@ -16,9 +16,7 @@ $submenu_file = 'edit.php'; wp_reset_vars( array( 'action' ) ); -if ( isset( $_GET['post'] ) && isset( $_POST['post_ID'] ) && (int) $_GET['post'] !== (int) $_POST['post_ID'] ) { - wp_die( __( 'A post ID mismatch has been detected.' ), __( 'Sorry, you are not allowed to edit this item.' ), 400 ); -} elseif ( isset( $_GET['post'] ) ) { +if ( isset( $_GET['post'] ) ) { $post_id = $post_ID = (int) $_GET['post']; } elseif ( isset( $_POST['post_ID'] ) ) { $post_id = $post_ID = (int) $_POST['post_ID']; @@ -42,10 +40,6 @@ if ( $post ) { $post_type_object = get_post_type_object( $post_type ); } -if ( isset( $_POST['post_type'] ) && $post && $post_type !== $_POST['post_type'] ) { - wp_die( __( 'A post type mismatch has been detected.' ), __( 'Sorry, you are not allowed to edit this item.' ), 400 ); -} - if ( isset( $_POST['deletepost'] ) ) { $action = 'delete'; } elseif ( isset( $_POST['wp-preview'] ) && 'dopreview' == $_POST['wp-preview'] ) { @@ -54,8 +48,8 @@ if ( isset( $_POST['deletepost'] ) ) { $sendback = wp_get_referer(); if ( ! $sendback || - strpos( $sendback, 'post.php' ) !== false || - strpos( $sendback, 'post-new.php' ) !== false ) { + strpos( $sendback, 'post.php' ) !== false || + strpos( $sendback, 'post-new.php' ) !== false ) { if ( 'attachment' == $post_type ) { $sendback = admin_url( 'upload.php' ); } else { @@ -172,6 +166,11 @@ switch ( $action ) { break; } + if ( use_block_editor_for_post( $post ) ) { + include( ABSPATH . 'wp-admin/edit-form-blocks.php' ); + break; + } + if ( ! wp_check_post_lock( $post->ID ) ) { $active_post_lock = wp_set_post_lock( $post->ID ); diff --git a/wp-includes/version.php b/wp-includes/version.php index 2ec6a31b90..819a25f3cf 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.1-alpha-44164'; +$wp_version = '5.1-alpha-44165'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.