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