[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-admin/ -> edit-form-blocks.php (source)

   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        Global post type.
  18   * @global WP_Post_Type $post_type_object Global post type object.
  19   * @global WP_Post      $post             Global post object.
  20   * @global string       $title            The title of the current screen.
  21   * @global array        $wp_meta_boxes    Global meta box state.
  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  $active_theme = get_stylesheet();
  55  
  56  // Preload common data.
  57  $preload_paths = array(
  58      '/wp/v2/types?context=view',
  59      '/wp/v2/taxonomies?context=view',
  60      add_query_arg( 'context', 'edit', $rest_path ),
  61      sprintf( '/wp/v2/types/%s?context=edit', $post_type ),
  62      '/wp/v2/users/me',
  63      array( rest_get_route_for_post_type_items( 'attachment' ), 'OPTIONS' ),
  64      array( rest_get_route_for_post_type_items( 'page' ), 'OPTIONS' ),
  65      array( rest_get_route_for_post_type_items( 'wp_block' ), 'OPTIONS' ),
  66      array( rest_get_route_for_post_type_items( 'wp_template' ), 'OPTIONS' ),
  67      sprintf( '%s/autosaves?context=edit', $rest_path ),
  68      '/wp/v2/settings',
  69      array( '/wp/v2/settings', 'OPTIONS' ),
  70      '/wp/v2/global-styles/themes/' . $active_theme . '?context=view',
  71      '/wp/v2/global-styles/themes/' . $active_theme . '/variations?context=view',
  72      '/wp/v2/themes?context=edit&status=active',
  73      array( '/wp/v2/global-styles/' . WP_Theme_JSON_Resolver::get_user_global_styles_post_id(), 'OPTIONS' ),
  74      '/wp/v2/global-styles/' . WP_Theme_JSON_Resolver::get_user_global_styles_post_id() . '?context=edit',
  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  // Preload server-registered block bindings sources.
 114  $registered_sources = get_all_registered_block_bindings_sources();
 115  if ( ! empty( $registered_sources ) ) {
 116      $filtered_sources = array();
 117      foreach ( $registered_sources as $source ) {
 118          $filtered_sources[] = array(
 119              'name'        => $source->name,
 120              'label'       => $source->label,
 121              'usesContext' => $source->uses_context,
 122          );
 123      }
 124      $script = sprintf( 'for ( const source of %s ) { wp.blocks.registerBlockBindingsSource( source ); }', wp_json_encode( $filtered_sources ) );
 125      wp_add_inline_script(
 126          'wp-blocks',
 127          $script
 128      );
 129  }
 130  
 131  // Get admin url for handling meta boxes.
 132  $meta_box_url = admin_url( 'post.php' );
 133  $meta_box_url = add_query_arg(
 134      array(
 135          'post'                  => $post->ID,
 136          'action'                => 'edit',
 137          'meta-box-loader'       => true,
 138          'meta-box-loader-nonce' => wp_create_nonce( 'meta-box-loader' ),
 139      ),
 140      $meta_box_url
 141  );
 142  wp_add_inline_script(
 143      'wp-editor',
 144      sprintf( 'var _wpMetaBoxUrl = %s;', wp_json_encode( $meta_box_url ) ),
 145      'before'
 146  );
 147  
 148  // Set Heartbeat interval to 10 seconds, used to refresh post locks.
 149  wp_add_inline_script(
 150      'heartbeat',
 151      'jQuery( function() {
 152          wp.heartbeat.interval( 10 );
 153      } );',
 154      'after'
 155  );
 156  
 157  /*
 158   * Get all available templates for the post/page attributes meta-box.
 159   * The "Default template" array element should only be added if the array is
 160   * not empty so we do not trigger the template select element without any options
 161   * besides the default value.
 162   */
 163  $available_templates = wp_get_theme()->get_page_templates( get_post( $post->ID ) );
 164  $available_templates = ! empty( $available_templates ) ? array_replace(
 165      array(
 166          /** This filter is documented in wp-admin/includes/meta-boxes.php */
 167          '' => apply_filters( 'default_page_template_title', __( 'Default template' ), 'rest-api' ),
 168      ),
 169      $available_templates
 170  ) : $available_templates;
 171  
 172  // Lock settings.
 173  $user_id = wp_check_post_lock( $post->ID );
 174  if ( $user_id ) {
 175      $locked = false;
 176  
 177      /** This filter is documented in wp-admin/includes/post.php */
 178      if ( apply_filters( 'show_post_locked_dialog', true, $post, $user_id ) ) {
 179          $locked = true;
 180      }
 181  
 182      $user_details = null;
 183      if ( $locked ) {
 184          $user         = get_userdata( $user_id );
 185          $user_details = array(
 186              'name' => $user->display_name,
 187          );
 188  
 189          if ( get_option( 'show_avatars' ) ) {
 190              $user_details['avatar'] = get_avatar_url( $user_id, array( 'size' => 128 ) );
 191          }
 192      }
 193  
 194      $lock_details = array(
 195          'isLocked' => $locked,
 196          'user'     => $user_details,
 197      );
 198  } else {
 199      // Lock the post.
 200      $active_post_lock = wp_set_post_lock( $post->ID );
 201      if ( $active_post_lock ) {
 202          $active_post_lock = esc_attr( implode( ':', $active_post_lock ) );
 203      }
 204  
 205      $lock_details = array(
 206          'isLocked'       => false,
 207          'activePostLock' => $active_post_lock,
 208      );
 209  }
 210  
 211  /**
 212   * Filters the body placeholder text.
 213   *
 214   * @since 5.0.0
 215   * @since 5.8.0 Changed the default placeholder text.
 216   *
 217   * @param string  $text Placeholder text. Default 'Type / to choose a block'.
 218   * @param WP_Post $post Post object.
 219   */
 220  $body_placeholder = apply_filters( 'write_your_story', __( 'Type / to choose a block' ), $post );
 221  
 222  $editor_settings = array(
 223      'availableTemplates'   => $available_templates,
 224      'disablePostFormats'   => ! current_theme_supports( 'post-formats' ),
 225      /** This filter is documented in wp-admin/edit-form-advanced.php */
 226      'titlePlaceholder'     => apply_filters( 'enter_title_here', __( 'Add title' ), $post ),
 227      'bodyPlaceholder'      => $body_placeholder,
 228      'autosaveInterval'     => AUTOSAVE_INTERVAL,
 229      'richEditingEnabled'   => user_can_richedit(),
 230      'postLock'             => $lock_details,
 231      'postLockUtils'        => array(
 232          'nonce'       => wp_create_nonce( 'lock-post_' . $post->ID ),
 233          'unlockNonce' => wp_create_nonce( 'update-post_' . $post->ID ),
 234          'ajaxUrl'     => admin_url( 'admin-ajax.php' ),
 235      ),
 236      'supportsLayout'       => wp_theme_has_theme_json(),
 237      'supportsTemplateMode' => current_theme_supports( 'block-templates' ),
 238  
 239      // Whether or not to load the 'postcustom' meta box is stored as a user meta
 240      // field so that we're not always loading its assets.
 241      'enableCustomFields'   => (bool) get_user_meta( get_current_user_id(), 'enable_custom_fields', true ),
 242  );
 243  
 244  // Add additional back-compat patterns registered by `current_screen` et al.
 245  $editor_settings['__experimentalAdditionalBlockPatterns']          = WP_Block_Patterns_Registry::get_instance()->get_all_registered( true );
 246  $editor_settings['__experimentalAdditionalBlockPatternCategories'] = WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered( true );
 247  
 248  $autosave = wp_get_post_autosave( $post->ID );
 249  if ( $autosave ) {
 250      if ( mysql2date( 'U', $autosave->post_modified_gmt, false ) > mysql2date( 'U', $post->post_modified_gmt, false ) ) {
 251          $editor_settings['autosave'] = array(
 252              'editLink' => get_edit_post_link( $autosave->ID ),
 253          );
 254      } else {
 255          wp_delete_post_revision( $autosave->ID );
 256      }
 257  }
 258  
 259  if ( ! empty( $post_type_object->template ) ) {
 260      $editor_settings['template']     = $post_type_object->template;
 261      $editor_settings['templateLock'] = ! empty( $post_type_object->template_lock ) ? $post_type_object->template_lock : false;
 262  }
 263  
 264  // If there's no template set on a new post, use the post format, instead.
 265  if ( $is_new_post && ! isset( $editor_settings['template'] ) && 'post' === $post->post_type ) {
 266      $post_format = get_post_format( $post );
 267      if ( in_array( $post_format, array( 'audio', 'gallery', 'image', 'quote', 'video' ), true ) ) {
 268          $editor_settings['template'] = array( array( "core/$post_format" ) );
 269      }
 270  }
 271  
 272  if ( wp_is_block_theme() && $editor_settings['supportsTemplateMode'] ) {
 273      $editor_settings['defaultTemplatePartAreas'] = get_allowed_block_template_part_areas();
 274  }
 275  
 276  /**
 277   * Scripts
 278   */
 279  wp_enqueue_media(
 280      array(
 281          'post' => $post->ID,
 282      )
 283  );
 284  wp_tinymce_inline_scripts();
 285  wp_enqueue_editor();
 286  
 287  /**
 288   * Styles
 289   */
 290  wp_enqueue_style( 'wp-edit-post' );
 291  
 292  /**
 293   * Fires after block assets have been enqueued for the editing interface.
 294   *
 295   * Call `add_action` on any hook before 'admin_enqueue_scripts'.
 296   *
 297   * In the function call you supply, simply use `wp_enqueue_script` and
 298   * `wp_enqueue_style` to add your functionality to the block editor.
 299   *
 300   * @since 5.0.0
 301   */
 302  do_action( 'enqueue_block_editor_assets' );
 303  
 304  // In order to duplicate classic meta box behavior, we need to run the classic meta box actions.
 305  require_once  ABSPATH . 'wp-admin/includes/meta-boxes.php';
 306  register_and_do_post_meta_boxes( $post );
 307  
 308  // Check if the Custom Fields meta box has been removed at some point.
 309  $core_meta_boxes = $wp_meta_boxes[ $current_screen->id ]['normal']['core'];
 310  if ( ! isset( $core_meta_boxes['postcustom'] ) || ! $core_meta_boxes['postcustom'] ) {
 311      unset( $editor_settings['enableCustomFields'] );
 312  }
 313  
 314  $editor_settings = get_block_editor_settings( $editor_settings, $block_editor_context );
 315  
 316  $init_script = <<<JS
 317  ( function() {
 318      window._wpLoadBlockEditor = new Promise( function( resolve ) {
 319          wp.domReady( function() {
 320              resolve( wp.editPost.initializeEditor( 'editor', "%s", %d, %s, %s ) );
 321          } );
 322      } );
 323  } )();
 324  JS;
 325  
 326  $script = sprintf(
 327      $init_script,
 328      $post->post_type,
 329      $post->ID,
 330      wp_json_encode( $editor_settings ),
 331      wp_json_encode( $initial_edits )
 332  );
 333  wp_add_inline_script( 'wp-edit-post', $script );
 334  
 335  if ( (int) get_option( 'page_for_posts' ) === $post->ID ) {
 336      add_action( 'admin_enqueue_scripts', '_wp_block_editor_posts_page_notice' );
 337  }
 338  
 339  require_once  ABSPATH . 'wp-admin/admin-header.php';
 340  ?>
 341  
 342  <div class="block-editor">
 343      <h1 class="screen-reader-text hide-if-no-js"><?php echo esc_html( $title ); ?></h1>
 344      <div id="editor" class="block-editor__container hide-if-no-js"></div>
 345      <div id="metaboxes" class="hidden">
 346          <?php the_block_editor_meta_boxes(); ?>
 347      </div>
 348  
 349      <?php // JavaScript is disabled. ?>
 350      <div class="wrap hide-if-js block-editor-no-js">
 351          <h1 class="wp-heading-inline"><?php echo esc_html( $title ); ?></h1>
 352          <?php
 353          if ( file_exists( WP_PLUGIN_DIR . '/classic-editor/classic-editor.php' ) ) {
 354              // If Classic Editor is already installed, provide a link to activate the plugin.
 355              $installed           = true;
 356              $plugin_activate_url = wp_nonce_url( 'plugins.php?action=activate&amp;plugin=classic-editor/classic-editor.php', 'activate-plugin_classic-editor/classic-editor.php' );
 357              $message             = sprintf(
 358                  /* translators: %s: Link to activate the Classic Editor plugin. */
 359                  __( 'The block editor requires JavaScript. Please enable JavaScript in your browser settings, or activate the <a href="%s">Classic Editor plugin</a>.' ),
 360                  esc_url( $plugin_activate_url )
 361              );
 362          } else {
 363              // If Classic Editor is not installed, provide a link to install it.
 364              $installed          = false;
 365              $plugin_install_url = wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin=classic-editor' ), 'install-plugin_classic-editor' );
 366              $message            = sprintf(
 367                  /* translators: %s: Link to install the Classic Editor plugin. */
 368                  __( 'The block editor requires JavaScript. Please enable JavaScript in your browser settings, or install the <a href="%s">Classic Editor plugin</a>.' ),
 369                  esc_url( $plugin_install_url )
 370              );
 371          }
 372  
 373          /**
 374           * Filters the message displayed in the block editor interface when JavaScript is
 375           * not enabled in the browser.
 376           *
 377           * @since 5.0.3
 378           * @since 6.4.0 Added `$installed` parameter.
 379           *
 380           * @param string  $message   The message being displayed.
 381           * @param WP_Post $post      The post being edited.
 382           * @param bool    $installed Whether the classic editor is installed.
 383           */
 384          $message = apply_filters( 'block_editor_no_javascript_message', $message, $post, $installed );
 385          wp_admin_notice(
 386              $message,
 387              array(
 388                  'type' => 'error',
 389              )
 390          );
 391          ?>
 392      </div>
 393  </div>


Generated : Sat Dec 21 08:20:01 2024 Cross-referenced by PHPXref