[ Index ]

PHP Cross Reference of WordPress

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
  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      sprintf( '%s/autosaves?context=edit', $rest_path ),
  72      '/wp/v2/settings',
  73  );
  74  
  75  block_editor_rest_api_preload( $preload_paths, $block_editor_context );
  76  
  77  wp_add_inline_script(
  78      'wp-blocks',
  79      sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( get_block_categories( $post ) ) ),
  80      'after'
  81  );
  82  
  83  /*
  84   * Assign initial edits, if applicable. These are not initially assigned to the persisted post,
  85   * but should be included in its save payload.
  86   */
  87  $initial_edits = array();
  88  $is_new_post   = false;
  89  if ( 'auto-draft' === $post->post_status ) {
  90      $is_new_post = true;
  91      // Override "(Auto Draft)" new post default title with empty string, or filtered value.
  92      if ( post_type_supports( $post->post_type, 'title' ) ) {
  93          $initial_edits['title'] = $post->post_title;
  94      }
  95  
  96      if ( post_type_supports( $post->post_type, 'editor' ) ) {
  97          $initial_edits['content'] = $post->post_content;
  98      }
  99  
 100      if ( post_type_supports( $post->post_type, 'excerpt' ) ) {
 101          $initial_edits['excerpt'] = $post->post_excerpt;
 102      }
 103  }
 104  
 105  // Preload server-registered block schemas.
 106  wp_add_inline_script(
 107      'wp-blocks',
 108      'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');'
 109  );
 110  
 111  // Get admin url for handling meta boxes.
 112  $meta_box_url = admin_url( 'post.php' );
 113  $meta_box_url = add_query_arg(
 114      array(
 115          'post'                  => $post->ID,
 116          'action'                => 'edit',
 117          'meta-box-loader'       => true,
 118          'meta-box-loader-nonce' => wp_create_nonce( 'meta-box-loader' ),
 119      ),
 120      $meta_box_url
 121  );
 122  wp_add_inline_script(
 123      'wp-editor',
 124      sprintf( 'var _wpMetaBoxUrl = %s;', wp_json_encode( $meta_box_url ) ),
 125      'before'
 126  );
 127  
 128  /*
 129   * Get all available templates for the post/page attributes meta-box.
 130   * The "Default template" array element should only be added if the array is
 131   * not empty so we do not trigger the template select element without any options
 132   * besides the default value.
 133   */
 134  $available_templates = wp_get_theme()->get_page_templates( get_post( $post->ID ) );
 135  $available_templates = ! empty( $available_templates ) ? array_replace(
 136      array(
 137          /** This filter is documented in wp-admin/includes/meta-boxes.php */
 138          '' => apply_filters( 'default_page_template_title', __( 'Default template' ), 'rest-api' ),
 139      ),
 140      $available_templates
 141  ) : $available_templates;
 142  
 143  // Lock settings.
 144  $user_id = wp_check_post_lock( $post->ID );
 145  if ( $user_id ) {
 146      $locked = false;
 147  
 148      /** This filter is documented in wp-admin/includes/post.php */
 149      if ( apply_filters( 'show_post_locked_dialog', true, $post, $user_id ) ) {
 150          $locked = true;
 151      }
 152  
 153      $user_details = null;
 154      if ( $locked ) {
 155          $user         = get_userdata( $user_id );
 156          $user_details = array(
 157              'avatar' => get_avatar_url( $user_id, array( 'size' => 128 ) ),
 158              'name'   => $user->display_name,
 159          );
 160      }
 161  
 162      $lock_details = array(
 163          'isLocked' => $locked,
 164          'user'     => $user_details,
 165      );
 166  } else {
 167      // Lock the post.
 168      $active_post_lock = wp_set_post_lock( $post->ID );
 169      if ( $active_post_lock ) {
 170          $active_post_lock = esc_attr( implode( ':', $active_post_lock ) );
 171      }
 172  
 173      $lock_details = array(
 174          'isLocked'       => false,
 175          'activePostLock' => $active_post_lock,
 176      );
 177  }
 178  
 179  /**
 180   * Filters the body placeholder text.
 181   *
 182   * @since 5.0.0
 183   * @since 5.8.0 Changed the default placeholder text.
 184   *
 185   * @param string  $text Placeholder text. Default 'Type / to choose a block'.
 186   * @param WP_Post $post Post object.
 187   */
 188  $body_placeholder = apply_filters( 'write_your_story', __( 'Type / to choose a block' ), $post );
 189  
 190  $editor_settings = array(
 191      'availableTemplates'   => $available_templates,
 192      'disablePostFormats'   => ! current_theme_supports( 'post-formats' ),
 193      /** This filter is documented in wp-admin/edit-form-advanced.php */
 194      'titlePlaceholder'     => apply_filters( 'enter_title_here', __( 'Add title' ), $post ),
 195      'bodyPlaceholder'      => $body_placeholder,
 196      'autosaveInterval'     => AUTOSAVE_INTERVAL,
 197      'richEditingEnabled'   => user_can_richedit(),
 198      'postLock'             => $lock_details,
 199      'postLockUtils'        => array(
 200          'nonce'       => wp_create_nonce( 'lock-post_' . $post->ID ),
 201          'unlockNonce' => wp_create_nonce( 'update-post_' . $post->ID ),
 202          'ajaxUrl'     => admin_url( 'admin-ajax.php' ),
 203      ),
 204      'supportsLayout'       => WP_Theme_JSON_Resolver::theme_has_support(),
 205      'supportsTemplateMode' => current_theme_supports( 'block-templates' ),
 206  
 207      // Whether or not to load the 'postcustom' meta box is stored as a user meta
 208      // field so that we're not always loading its assets.
 209      'enableCustomFields'   => (bool) get_user_meta( get_current_user_id(), 'enable_custom_fields', true ),
 210  );
 211  
 212  $autosave = wp_get_post_autosave( $post->ID );
 213  if ( $autosave ) {
 214      if ( mysql2date( 'U', $autosave->post_modified_gmt, false ) > mysql2date( 'U', $post->post_modified_gmt, false ) ) {
 215          $editor_settings['autosave'] = array(
 216              'editLink' => get_edit_post_link( $autosave->ID ),
 217          );
 218      } else {
 219          wp_delete_post_revision( $autosave->ID );
 220      }
 221  }
 222  
 223  if ( ! empty( $post_type_object->template ) ) {
 224      $editor_settings['template']     = $post_type_object->template;
 225      $editor_settings['templateLock'] = ! empty( $post_type_object->template_lock ) ? $post_type_object->template_lock : false;
 226  }
 227  
 228  // If there's no template set on a new post, use the post format, instead.
 229  if ( $is_new_post && ! isset( $editor_settings['template'] ) && 'post' === $post->post_type ) {
 230      $post_format = get_post_format( $post );
 231      if ( in_array( $post_format, array( 'audio', 'gallery', 'image', 'quote', 'video' ), true ) ) {
 232          $editor_settings['template'] = array( array( "core/$post_format" ) );
 233      }
 234  }
 235  
 236  if ( wp_is_block_theme() && $editor_settings['supportsTemplateMode'] ) {
 237      $editor_settings['defaultTemplatePartAreas'] = get_allowed_block_template_part_areas();
 238  }
 239  
 240  /**
 241   * Scripts
 242   */
 243  wp_enqueue_media(
 244      array(
 245          'post' => $post->ID,
 246      )
 247  );
 248  wp_tinymce_inline_scripts();
 249  wp_enqueue_editor();
 250  
 251  /**
 252   * Styles
 253   */
 254  wp_enqueue_style( 'wp-edit-post' );
 255  
 256  /**
 257   * Fires after block assets have been enqueued for the editing interface.
 258   *
 259   * Call `add_action` on any hook before 'admin_enqueue_scripts'.
 260   *
 261   * In the function call you supply, simply use `wp_enqueue_script` and
 262   * `wp_enqueue_style` to add your functionality to the block editor.
 263   *
 264   * @since 5.0.0
 265   */
 266  do_action( 'enqueue_block_editor_assets' );
 267  
 268  // In order to duplicate classic meta box behaviour, we need to run the classic meta box actions.
 269  require_once ABSPATH . 'wp-admin/includes/meta-boxes.php';
 270  register_and_do_post_meta_boxes( $post );
 271  
 272  // Check if the Custom Fields meta box has been removed at some point.
 273  $core_meta_boxes = $wp_meta_boxes[ $current_screen->id ]['normal']['core'];
 274  if ( ! isset( $core_meta_boxes['postcustom'] ) || ! $core_meta_boxes['postcustom'] ) {
 275      unset( $editor_settings['enableCustomFields'] );
 276  }
 277  
 278  $editor_settings = get_block_editor_settings( $editor_settings, $block_editor_context );
 279  
 280  $init_script = <<<JS
 281  ( function() {
 282      window._wpLoadBlockEditor = new Promise( function( resolve ) {
 283          wp.domReady( function() {
 284              resolve( wp.editPost.initializeEditor( 'editor', "%s", %d, %s, %s ) );
 285          } );
 286      } );
 287  } )();
 288  JS;
 289  
 290  $script = sprintf(
 291      $init_script,
 292      $post->post_type,
 293      $post->ID,
 294      wp_json_encode( $editor_settings ),
 295      wp_json_encode( $initial_edits )
 296  );
 297  wp_add_inline_script( 'wp-edit-post', $script );
 298  
 299  if ( (int) get_option( 'page_for_posts' ) === $post->ID ) {
 300      add_action( 'admin_enqueue_scripts', '_wp_block_editor_posts_page_notice' );
 301  }
 302  
 303  require_once ABSPATH . 'wp-admin/admin-header.php';
 304  ?>
 305  
 306  <div class="block-editor">
 307      <h1 class="screen-reader-text hide-if-no-js"><?php echo esc_html( $title ); ?></h1>
 308      <div id="editor" class="block-editor__container hide-if-no-js"></div>
 309      <div id="metaboxes" class="hidden">
 310          <?php the_block_editor_meta_boxes(); ?>
 311      </div>
 312  
 313      <?php // JavaScript is disabled. ?>
 314      <div class="wrap hide-if-js block-editor-no-js">
 315          <h1 class="wp-heading-inline"><?php echo esc_html( $title ); ?></h1>
 316          <div class="notice notice-error notice-alt">
 317              <p>
 318                  <?php
 319                      $message = sprintf(
 320                          /* translators: %s: A link to install the Classic Editor plugin. */
 321                          __( 'The block editor requires JavaScript. Please enable JavaScript in your browser settings, or try the <a href="%s">Classic Editor plugin</a>.' ),
 322                          esc_url( wp_nonce_url( self_admin_url( 'plugin-install.php?tab=favorites&user=wordpressdotorg&save=0' ), 'save_wporg_username_' . get_current_user_id() ) )
 323                      );
 324  
 325                      /**
 326                       * Filters the message displayed in the block editor interface when JavaScript is
 327                       * not enabled in the browser.
 328                       *
 329                       * @since 5.0.3
 330                       *
 331                       * @param string  $message The message being displayed.
 332                       * @param WP_Post $post    The post being edited.
 333                       */
 334                      echo apply_filters( 'block_editor_no_javascript_message', $message, $post );
 335                      ?>
 336              </p>
 337          </div>
 338      </div>
 339  </div>


Generated: Mon May 16 01:00:02 2022 Cross-referenced by PHPXref 0.7.1