| 1 | <?php |
| 2 | /** |
| 3 | * The Block Editor page. |
| 4 | * |
| 5 | * @since 5.0.0 |
| 6 | * |
| 7 | * @package WordPress |
| 8 | * @subpackage Administration |
| 9 | */ |
| 10 | |
| 11 | // don't load directly |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | die( '-1' ); |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * @global string $post_type |
| 18 | * @global WP_Post_Type $post_type_object |
| 19 | * @global WP_Post $post |
| 20 | * @global string $title |
| 21 | * @global array $editor_styles |
| 22 | */ |
| 23 | global $post_type, $post_type_object, $post, $title, $editor_styles; |
| 24 | |
| 25 | if ( ! empty( $post_type_object ) ) { |
| 26 | $title = $post_type_object->labels->edit_item; |
| 27 | } |
| 28 | |
| 29 | // Flag that we're loading the block editor. |
| 30 | $current_screen = get_current_screen(); |
| 31 | $current_screen->is_block_editor( true ); |
| 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 | wp_enqueue_script( 'heartbeat' ); |
| 39 | wp_enqueue_script( 'wp-edit-post' ); |
| 40 | |
| 41 | $rest_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name; |
| 42 | |
| 43 | // Preload common data. |
| 44 | $preload_paths = array( |
| 45 | '/', |
| 46 | '/wp/v2/types?context=edit', |
| 47 | '/wp/v2/taxonomies?per_page=-1&context=edit', |
| 48 | '/wp/v2/themes?status=active', |
| 49 | sprintf( '/wp/v2/%s/%s?context=edit', $rest_base, $post->ID ), |
| 50 | sprintf( '/wp/v2/types/%s?context=edit', $post_type ), |
| 51 | sprintf( '/wp/v2/users/me?post_type=%s&context=edit', $post_type ), |
| 52 | ); |
| 53 | |
| 54 | /* |
| 55 | * Ensure the global $post remains the same after API data is preloaded. |
| 56 | * Because API preloading can call the_content and other filters, plugins |
| 57 | * can unexpectedly modify $post. |
| 58 | */ |
| 59 | $backup_global_post = $post; |
| 60 | |
| 61 | $preload_data = array_reduce( |
| 62 | $preload_paths, |
| 63 | 'rest_preload_api_request', |
| 64 | array() |
| 65 | ); |
| 66 | |
| 67 | // Restore the global $post as it was before API preloading. |
| 68 | $post = $backup_global_post; |
| 69 | |
| 70 | wp_add_inline_script( |
| 71 | 'wp-api-fetch', |
| 72 | sprintf( 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', wp_json_encode( $preload_data ) ), |
| 73 | 'after' |
| 74 | ); |
| 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 | /* |
| 83 | * Assign initial edits, if applicable. These are not initially assigned to the persisted post, |
| 84 | * but should be included in its save payload. |
| 85 | */ |
| 86 | $initial_edits = null; |
| 87 | $is_new_post = false; |
| 88 | if ( 'auto-draft' === $post->post_status ) { |
| 89 | $is_new_post = true; |
| 90 | // Override "(Auto Draft)" new post default title with empty string, or filtered value. |
| 91 | $initial_edits = array( |
| 92 | 'title' => array( |
| 93 | 'raw' => $post->post_title, |
| 94 | ), |
| 95 | 'content' => array( |
| 96 | 'raw' => $post->post_content, |
| 97 | ), |
| 98 | 'excerpt' => array( |
| 99 | 'raw' => $post->post_excerpt, |
| 100 | ), |
| 101 | ); |
| 102 | } |
| 103 | |
| 104 | // Prepare Jed locale data. |
| 105 | $locale_data = wp_get_jed_locale_data( 'default' ); |
| 106 | wp_add_inline_script( |
| 107 | 'wp-i18n', |
| 108 | 'wp.i18n.setLocaleData( ' . wp_json_encode( $locale_data ) . ' );' |
| 109 | ); |
| 110 | |
| 111 | // Preload server-registered block schemas. |
| 112 | wp_add_inline_script( |
| 113 | 'wp-blocks', |
| 114 | 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');' |
| 115 | ); |
| 116 | |
| 117 | // Get admin url for handling meta boxes. |
| 118 | $meta_box_url = admin_url( 'post.php' ); |
| 119 | $meta_box_url = add_query_arg( |
| 120 | array( |
| 121 | 'post' => $post->ID, |
| 122 | 'action' => 'edit', |
| 123 | 'meta-box-loader' => true, |
| 124 | '_wpnonce' => wp_create_nonce( 'meta-box-loader' ), |
| 125 | ), |
| 126 | $meta_box_url |
| 127 | ); |
| 128 | wp_localize_script( 'wp-editor', '_wpMetaBoxUrl', $meta_box_url ); |
| 129 | |
| 130 | |
| 131 | // Populate default code editor settings by short-circuiting wp_enqueue_code_editor. |
| 132 | wp_add_inline_script( |
| 133 | 'wp-editor', |
| 134 | sprintf( |
| 135 | 'window._wpGutenbergCodeEditorSettings = %s;', |
| 136 | wp_json_encode( wp_get_code_editor_settings( array( 'type' => 'text/html' ) ) ) |
| 137 | ) |
| 138 | ); |
| 139 | |
| 140 | /* |
| 141 | * Initialize the editor. |
| 142 | */ |
| 143 | |
| 144 | $align_wide = get_theme_support( 'align-wide' ); |
| 145 | $color_palette = current( (array) get_theme_support( 'editor-color-palette' ) ); |
| 146 | $font_sizes = current( (array) get_theme_support( 'editor-font-sizes' ) ); |
| 147 | |
| 148 | /** |
| 149 | * Filters the allowed block types for the editor, defaulting to true (all |
| 150 | * block types supported). |
| 151 | * |
| 152 | * @since 5.0.0 |
| 153 | * |
| 154 | * @param bool|array $allowed_block_types Array of block type slugs, or |
| 155 | * boolean to enable/disable all. |
| 156 | * @param object $post The post resource data. |
| 157 | */ |
| 158 | $allowed_block_types = apply_filters( 'allowed_block_types', true, $post ); |
| 159 | |
| 160 | // Get all available templates for the post/page attributes meta-box. |
| 161 | // The "Default template" array element should only be added if the array is |
| 162 | // not empty so we do not trigger the template select element without any options |
| 163 | // besides the default value. |
| 164 | $available_templates = wp_get_theme()->get_page_templates( get_post( $post->ID ) ); |
| 165 | $available_templates = ! empty( $available_templates ) ? array_merge( |
| 166 | array( |
| 167 | /** This filter is documented in wp-admin/includes/meta-boxes.php */ |
| 168 | '' => apply_filters( 'default_page_template_title', __( 'Default template' ), 'rest-api' ), |
| 169 | ), |
| 170 | $available_templates |
| 171 | ) : $available_templates; |
| 172 | |
| 173 | // Media settings. |
| 174 | $max_upload_size = wp_max_upload_size(); |
| 175 | if ( ! $max_upload_size ) { |
| 176 | $max_upload_size = 0; |
| 177 | } |
| 178 | |
| 179 | // Editor Styles. |
| 180 | $styles = array( |
| 181 | array( |
| 182 | 'css' => file_get_contents( |
| 183 | ABSPATH . WPINC . '/css/dist/editor/editor-styles.css' |
| 184 | ), |
| 185 | ), |
| 186 | ); |
| 187 | if ( $editor_styles && current_theme_supports( 'editor-styles' ) ) { |
| 188 | foreach ( $editor_styles as $style ) { |
| 189 | if ( preg_match( '~^(https?:)?//~', $style ) ) { |
| 190 | $styles[] = array( |
| 191 | 'css' => file_get_contents( $style ), |
| 192 | ); |
| 193 | } else { |
| 194 | $file = get_theme_file_path( $style ); |
| 195 | $styles[] = array( |
| 196 | 'css' => file_get_contents( get_theme_file_path( $style ) ), |
| 197 | 'baseURL' => get_theme_file_uri( $style ), |
| 198 | ); |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | // Lock settings. |
| 204 | $user_id = wp_check_post_lock( $post->ID ); |
| 205 | if ( $user_id ) { |
| 206 | /** This filter is documented in wp-admin/includes/post.php */ |
| 207 | if ( apply_filters( 'show_post_locked_dialog', true, $post, $user_id ) ) { |
| 208 | $locked = true; |
| 209 | } |
| 210 | |
| 211 | $user_details = null; |
| 212 | if ( $locked ) { |
| 213 | $user = get_userdata( $user_id ); |
| 214 | $user_details = array( |
| 215 | 'name' => $user->display_name, |
| 216 | ); |
| 217 | $avatar = get_avatar_url( $user_id, array( 'size' => 64 ) ); |
| 218 | } |
| 219 | |
| 220 | $lock_details = array( |
| 221 | 'isLocked' => $locked, |
| 222 | 'user' => $user_details, |
| 223 | ); |
| 224 | } else { |
| 225 | // Lock the post. |
| 226 | $active_post_lock = wp_set_post_lock( $post->ID ); |
| 227 | $lock_details = array( |
| 228 | 'isLocked' => false, |
| 229 | 'activePostLock' => esc_attr( implode( ':', $active_post_lock ) ), |
| 230 | ); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Filters the body placeholder text. |
| 235 | * |
| 236 | * @since 5.0.0 |
| 237 | * |
| 238 | * @param string $text Placeholder text. Default 'Write your story'. |
| 239 | * @param WP_Post $post Post object. |
| 240 | */ |
| 241 | $body_placeholder = apply_filters( 'write_your_story', __( 'Write your story' ), $post ); |
| 242 | |
| 243 | $editor_settings = array( |
| 244 | 'alignWide' => $align_wide, |
| 245 | 'availableTemplates' => $available_templates, |
| 246 | 'allowedBlockTypes' => $allowed_block_types, |
| 247 | 'disableCustomColors' => get_theme_support( 'disable-custom-colors' ), |
| 248 | 'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ), |
| 249 | 'disablePostFormats' => ! current_theme_supports( 'post-formats' ), |
| 250 | /** This filter is documented in wp-admin/edit-form-advanced.php */ |
| 251 | 'titlePlaceholder' => apply_filters( 'enter_title_here', __( 'Add title' ), $post ), |
| 252 | 'bodyPlaceholder' => $body_placeholder, |
| 253 | 'isRTL' => is_rtl(), |
| 254 | 'autosaveInterval' => 10, |
| 255 | 'maxUploadFileSize' => $max_upload_size, |
| 256 | 'allowedMimeTypes' => get_allowed_mime_types(), |
| 257 | 'styles' => $styles, |
| 258 | 'postLock' => $lock_details, |
| 259 | 'postLockUtils' => array( |
| 260 | 'nonce' => wp_create_nonce( 'lock-post_' . $post->ID ), |
| 261 | 'unlockNonce' => wp_create_nonce( 'update-post_' . $post->ID ), |
| 262 | 'ajaxUrl' => admin_url( 'admin-ajax.php' ), |
| 263 | ), |
| 264 | ); |
| 265 | |
| 266 | $autosave = wp_get_post_autosave( $post_ID ); |
| 267 | if ( $autosave ) { |
| 268 | if ( mysql2date( 'U', $autosave->post_modified_gmt, false ) > mysql2date( 'U', $post->post_modified_gmt, false ) ) { |
| 269 | $editor_settings['autosave'] = array( |
| 270 | 'editLink' => get_edit_post_link( $autosave->ID ), |
| 271 | ); |
| 272 | } else { |
| 273 | wp_delete_post_revision( $autosave->ID ); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | if ( false !== $color_palette ) { |
| 278 | $editor_settings['colors'] = $color_palette; |
| 279 | } |
| 280 | |
| 281 | if ( ! empty( $font_sizes ) ) { |
| 282 | $editor_settings['fontSizes'] = $font_sizes; |
| 283 | } |
| 284 | |
| 285 | if ( ! empty( $post_type_object->template ) ) { |
| 286 | $editor_settings['template'] = $post_type_object->template; |
| 287 | $editor_settings['templateLock'] = ! empty( $post_type_object->template_lock ) ? $post_type_object->template_lock : false; |
| 288 | } |
| 289 | |
| 290 | // If there's no template set on a new post, use the post format, instead. |
| 291 | if ( $is_new_post && ! isset( $editor_settings['template'] ) && 'post' === $post->post_type ) { |
| 292 | $post_format = get_post_format( $post ); |
| 293 | if ( in_array( $post_format, array( 'audio', 'gallery', 'image', 'quote', 'video' ), true ) ) { |
| 294 | $editor_settings['template'] = array( array( "core/$post_format" ) ); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | $init_script = <<<JS |
| 299 | ( function() { |
| 300 | window._wpLoadBlockEditor = new Promise( function( resolve ) { |
| 301 | wp.domReady( function() { |
| 302 | resolve( wp.editPost.initializeEditor( 'editor', "%s", %d, %s, %s ) ); |
| 303 | } ); |
| 304 | } ); |
| 305 | } )(); |
| 306 | JS; |
| 307 | |
| 308 | |
| 309 | /** |
| 310 | * Filters the settings to pass to the block editor. |
| 311 | * |
| 312 | * @since 5.0.0 |
| 313 | * |
| 314 | * @param array $editor_settings Default editor settings. |
| 315 | * @param WP_Post $post Post being edited. |
| 316 | */ |
| 317 | $editor_settings = apply_filters( 'block_editor_settings', $editor_settings, $post ); |
| 318 | |
| 319 | $script = sprintf( |
| 320 | $init_script, |
| 321 | $post->post_type, |
| 322 | $post->ID, |
| 323 | wp_json_encode( $editor_settings ), |
| 324 | wp_json_encode( $initial_edits ) |
| 325 | ); |
| 326 | wp_add_inline_script( 'wp-edit-post', $script ); |
| 327 | |
| 328 | /** |
| 329 | * Scripts |
| 330 | */ |
| 331 | wp_enqueue_media( |
| 332 | array( |
| 333 | 'post' => $post->ID, |
| 334 | ) |
| 335 | ); |
| 336 | wp_enqueue_editor(); |
| 337 | |
| 338 | /** |
| 339 | * Styles |
| 340 | */ |
| 341 | wp_enqueue_style( 'wp-edit-post' ); |
| 342 | |
| 343 | $block_registry = WP_Block_Type_Registry::get_instance(); |
| 344 | foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) { |
| 345 | // Front-end styles. |
| 346 | if ( ! empty( $block_type->style ) ) { |
| 347 | wp_enqueue_style( $block_type->style ); |
| 348 | } |
| 349 | |
| 350 | // Front-end script. |
| 351 | if ( ! empty( $block_type->script ) ) { |
| 352 | wp_enqueue_script( $block_type->script ); |
| 353 | } |
| 354 | |
| 355 | // Editor styles. |
| 356 | if ( ! empty( $block_type->editor_style ) ) { |
| 357 | wp_enqueue_style( $block_type->editor_style ); |
| 358 | } |
| 359 | |
| 360 | // Editor script. |
| 361 | if ( ! empty( $block_type->editor_script ) ) { |
| 362 | wp_enqueue_script( $block_type->editor_script ); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Fires after block assets have been enqueued for the editing interface. |
| 368 | * |
| 369 | * Call `add_action` on any hook before 'admin_enqueue_scripts'. |
| 370 | * |
| 371 | * In the function call you supply, simply use `wp_enqueue_script` and |
| 372 | * `wp_enqueue_style` to add your functionality to the block editor. |
| 373 | * |
| 374 | * @since 5.0.0 |
| 375 | */ |
| 376 | do_action( 'enqueue_block_editor_assets' ); |
| 377 | |
| 378 | require_once( ABSPATH . 'wp-admin/admin-header.php' ); |
| 379 | ?> |
| 380 | |
| 381 | <div class="block-editor"> |
| 382 | <h1 class="screen-reader-text"><?php echo esc_html( $post_type_object->labels->edit_item ); ?></h1> |
| 383 | <div id="editor" class="block-editor__container"></div> |
| 384 | <div id="metaboxes" class="hidden"> |
| 385 | <?php the_block_editor_meta_boxes(); ?> |
| 386 | </div> |
| 387 | </div> |