| 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 afterAPI 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 | // Get all available templates for the post/page attributes meta-box. |
| 143 | // The "Default template" array element should only be added if the array is |
| 144 | // not empty so we do not trigger the template select element without any options |
| 145 | // besides the default value. |
| 146 | $available_templates = wp_get_theme()->get_page_templates( get_post( $post->ID ) ); |
| 147 | $available_templates = ! empty( $available_templates ) ? array_merge( |
| 148 | array( |
| 149 | '' => apply_filters( 'default_page_template_title', __( 'Default template', 'gutenberg' ), 'rest-api' ), |
| 150 | ), |
| 151 | $available_templates |
| 152 | ) : $available_templates; |
| 153 | |
| 154 | // Media settings. |
| 155 | $max_upload_size = wp_max_upload_size(); |
| 156 | if ( ! $max_upload_size ) { |
| 157 | $max_upload_size = 0; |
| 158 | } |
| 159 | |
| 160 | // Editor Styles. |
| 161 | global $editor_styles; |
| 162 | $styles = array( |
| 163 | array( |
| 164 | 'css' => file_get_contents( |
| 165 | ABSPATH . WPINC . '/css/dist/editor/editor-styles.css' |
| 166 | ), |
| 167 | ), |
| 168 | ); |
| 169 | if ( $editor_styles && current_theme_supports( 'editor-styles' ) ) { |
| 170 | foreach ( $editor_styles as $style ) { |
| 171 | if ( filter_var( $style, FILTER_VALIDATE_URL ) ) { |
| 172 | $styles[] = array( |
| 173 | 'css' => file_get_contents( $style ), |
| 174 | ); |
| 175 | } else { |
| 176 | $file = get_theme_file_path( $style ); |
| 177 | $styles[] = array( |
| 178 | 'css' => file_get_contents( get_theme_file_path( $style ) ), |
| 179 | 'baseURL' => get_theme_file_uri( $style ), |
| 180 | ); |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | // Lock settings. |
| 186 | $user_id = wp_check_post_lock( $post->ID ); |
| 187 | if ( $user_id ) { |
| 188 | /** This filter is documented in wp-admin/includes/post.php */ |
| 189 | if ( apply_filters( 'show_post_locked_dialog', true, $post, $user_id ) ) { |
| 190 | $locked = true; |
| 191 | } |
| 192 | |
| 193 | $user_details = null; |
| 194 | if ( $locked ) { |
| 195 | $user = get_userdata( $user_id ); |
| 196 | $user_details = array( |
| 197 | 'name' => $user->display_name, |
| 198 | ); |
| 199 | $avatar = get_avatar( $user_id, 64 ); |
| 200 | if ( $avatar ) { |
| 201 | if ( preg_match( "|src='([^']+)'|", $avatar, $matches ) ) { |
| 202 | $user_details['avatar'] = $matches[1]; |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | $lock_details = array( |
| 208 | 'isLocked' => $locked, |
| 209 | 'user' => $user_details, |
| 210 | ); |
| 211 | } else { |
| 212 | // Lock the post. |
| 213 | $active_post_lock = wp_set_post_lock( $post->ID ); |
| 214 | $lock_details = array( |
| 215 | 'isLocked' => false, |
| 216 | 'activePostLock' => esc_attr( implode( ':', $active_post_lock ) ), |
| 217 | ); |
| 218 | } |
| 219 | |
| 220 | $editor_settings = array( |
| 221 | 'alignWide' => $align_wide || ! empty( $gutenberg_theme_support[0]['wide-images'] ), // Backcompat. Use `align-wide` outside of `gutenberg` array. |
| 222 | 'availableTemplates' => $available_templates, |
| 223 | 'allowedBlockTypes' => $allowed_block_types, |
| 224 | 'disableCustomColors' => get_theme_support( 'disable-custom-colors' ), |
| 225 | 'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ), |
| 226 | 'disablePostFormats' => ! current_theme_supports( 'post-formats' ), |
| 227 | 'titlePlaceholder' => apply_filters( 'enter_title_here', __( 'Add title', 'gutenberg' ), $post ), |
| 228 | 'bodyPlaceholder' => apply_filters( 'write_your_story', __( 'Write your story', 'gutenberg' ), $post ), |
| 229 | 'isRTL' => is_rtl(), |
| 230 | 'autosaveInterval' => 10, |
| 231 | 'maxUploadFileSize' => $max_upload_size, |
| 232 | 'allowedMimeTypes' => get_allowed_mime_types(), |
| 233 | 'styles' => $styles, |
| 234 | 'postLock' => $lock_details, |
| 235 | |
| 236 | // Ideally, we'd remove this and rely on a REST API endpoint. |
| 237 | 'postLockUtils' => array( |
| 238 | 'nonce' => wp_create_nonce( 'lock-post_' . $post->ID ), |
| 239 | 'unlockNonce' => wp_create_nonce( 'update-post_' . $post->ID ), |
| 240 | 'ajaxUrl' => admin_url( 'admin-ajax.php' ), |
| 241 | ), |
| 242 | ); |
| 243 | |
| 244 | $autosave = wp_get_post_autosave( $post_ID ); |
| 245 | if ( $autosave ) { |
| 246 | if ( mysql2date( 'U', $autosave->post_modified_gmt, false ) > mysql2date( 'U', $post->post_modified_gmt, false ) ) { |
| 247 | $editor_settings['autosave'] = array( |
| 248 | 'editLink' => add_query_arg( 'gutenberg', true, get_edit_post_link( $post_autosave->ID ) ), |
| 249 | ); |
| 250 | } else { |
| 251 | wp_delete_post_revision( $autosave->ID ); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | if ( false !== $color_palette ) { |
| 256 | $editor_settings['colors'] = $color_palette; |
| 257 | } |
| 258 | |
| 259 | if ( ! empty( $font_sizes ) ) { |
| 260 | $editor_settings['fontSizes'] = $font_sizes; |
| 261 | } |
| 262 | |
| 263 | if ( ! empty( $post_type_object->template ) ) { |
| 264 | $editor_settings['template'] = $post_type_object->template; |
| 265 | $editor_settings['templateLock'] = ! empty( $post_type_object->template_lock ) ? $post_type_object->template_lock : false; |
| 266 | } |
| 267 | |
| 268 | $init_script = <<<JS |
| 269 | ( function() { |
| 270 | window._wpLoadBlockEditor = new Promise( function( resolve ) { |
| 271 | wp.domReady( function() { |
| 272 | resolve( wp.editPost.initializeEditor( 'editor', "%s", %d, %s, %s ) ); |
| 273 | } ); |
| 274 | } ); |
| 275 | } )(); |
| 276 | JS; |
| 277 | |
| 278 | /** |
| 279 | * Filters the settings to pass to the block editor. |
| 280 | * |
| 281 | * @since 3.7.0 |
| 282 | * |
| 283 | * @param array $editor_settings Default editor settings. |
| 284 | * @param WP_Post $post Post being edited. |
| 285 | */ |
| 286 | $editor_settings = apply_filters( 'block_editor_settings', $editor_settings, $post ); |
| 287 | |
| 288 | $script = sprintf( |
| 289 | $init_script, |
| 290 | $post->post_type, |
| 291 | $post->ID, |
| 292 | wp_json_encode( $editor_settings ), |
| 293 | wp_json_encode( $initial_edits ) |
| 294 | ); |
| 295 | wp_add_inline_script( 'wp-edit-post', $script ); |
| 296 | |
| 297 | /** |
| 298 | * Scripts |
| 299 | */ |
| 300 | wp_enqueue_media( |
| 301 | array( |
| 302 | 'post' => $post->ID, |
| 303 | ) |
| 304 | ); |
| 305 | wp_enqueue_editor(); |
| 306 | |
| 307 | /** |
| 308 | * Styles |
| 309 | */ |
| 310 | wp_enqueue_style( 'wp-edit-post' ); |
| 311 | |
| 312 | $block_registry = WP_Block_Type_Registry::get_instance(); |
| 313 | foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) { |
| 314 | // Front-end styles. |
| 315 | if ( ! empty( $block_type->style ) ) { |
| 316 | wp_enqueue_style( $block_type->style ); |
| 317 | } |
| 318 | |
| 319 | // Front-end script. |
| 320 | if ( ! empty( $block_type->script ) ) { |
| 321 | wp_enqueue_script( $block_type->script ); |
| 322 | } |
| 323 | |
| 324 | // Editor styles. |
| 325 | if ( ! empty( $block_type->editor_style ) ) { |
| 326 | wp_enqueue_style( $block_type->editor_style ); |
| 327 | } |
| 328 | |
| 329 | // Editor script. |
| 330 | if ( ! empty( $block_type->editor_script ) ) { |
| 331 | wp_enqueue_script( $block_type->editor_script ); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Fires after block assets have been enqueued for the editing interface. |
| 337 | * |
| 338 | * Call `add_action` on any hook before 'admin_enqueue_scripts'. |
| 339 | * |
| 340 | * In the function call you supply, simply use `wp_enqueue_script` and |
| 341 | * `wp_enqueue_style` to add your functionality to the Gutenberg editor. |
| 342 | * |
| 343 | * @since 5.0.0 |
| 344 | */ |
| 345 | do_action( 'enqueue_block_editor_assets' ); |
| 346 | |
| 347 | require_once( ABSPATH . 'wp-admin/admin-header.php' ); |
| 348 | ?> |
| 349 | |
| 350 | <div class="gutenberg block-editor"> |
| 351 | <h1 class="screen-reader-text"><?php echo esc_html( $post_type_object->labels->edit_item ); ?></h1> |
| 352 | <div id="editor" class="block-editor__container"></div> |
| 353 | <div id="metaboxes" style="display: none;"> |
| 354 | <?php the_block_editor_metaboxes(); ?> |
| 355 | </div> |
| 356 | </div> |