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