[ 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        $editor_styles
  22   * @global array        $wp_meta_boxes
  23   */
  24  global $post_type, $post_type_object, $post, $title, $editor_styles, $wp_meta_boxes;
  25  
  26  // Flag that we're loading the block editor.
  27  $current_screen = get_current_screen();
  28  $current_screen->is_block_editor( true );
  29  
  30  /*
  31   * Emoji replacement is disabled for now, until it plays nicely with React.
  32   */
  33  remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
  34  
  35  /*
  36   * Block editor implements its own Options menu for toggling Document Panels.
  37   */
  38  add_filter( 'screen_options_show_screen', '__return_false' );
  39  
  40  wp_enqueue_script( 'heartbeat' );
  41  wp_enqueue_script( 'wp-edit-post' );
  42  
  43  $rest_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name;
  44  
  45  // Preload common data.
  46  $preload_paths = array(
  47      '/',
  48      '/wp/v2/types?context=edit',
  49      '/wp/v2/taxonomies?per_page=-1&context=edit',
  50      '/wp/v2/themes?status=active',
  51      sprintf( '/wp/v2/%s/%s?context=edit', $rest_base, $post->ID ),
  52      sprintf( '/wp/v2/types/%s?context=edit', $post_type ),
  53      sprintf( '/wp/v2/users/me?post_type=%s&context=edit', $post_type ),
  54      array( '/wp/v2/media', 'OPTIONS' ),
  55      array( '/wp/v2/blocks', 'OPTIONS' ),
  56      sprintf( '/wp/v2/%s/%d/autosaves?context=edit', $rest_base, $post->ID ),
  57  );
  58  
  59  /**
  60   * Preload common data by specifying an array of REST API paths that will be preloaded.
  61   *
  62   * Filters the array of paths that will be preloaded.
  63   *
  64   * @since 5.0.0
  65   *
  66   * @param string[] $preload_paths Array of paths to preload.
  67   * @param WP_Post  $post          Post being edited.
  68   */
  69  $preload_paths = apply_filters( 'block_editor_preload_paths', $preload_paths, $post );
  70  
  71  /*
  72   * Ensure the global $post remains the same after API data is preloaded.
  73   * Because API preloading can call the_content and other filters, plugins
  74   * can unexpectedly modify $post.
  75   */
  76  $backup_global_post = clone $post;
  77  
  78  $preload_data = array_reduce(
  79      $preload_paths,
  80      'rest_preload_api_request',
  81      array()
  82  );
  83  
  84  // Restore the global $post as it was before API preloading.
  85  $post = $backup_global_post;
  86  
  87  wp_add_inline_script(
  88      'wp-api-fetch',
  89      sprintf( 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', wp_json_encode( $preload_data ) ),
  90      'after'
  91  );
  92  
  93  wp_add_inline_script(
  94      'wp-blocks',
  95      sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( get_block_categories( $post ) ) ),
  96      'after'
  97  );
  98  
  99  /*
 100   * Assign initial edits, if applicable. These are not initially assigned to the persisted post,
 101   * but should be included in its save payload.
 102   */
 103  $initial_edits = null;
 104  $is_new_post   = false;
 105  if ( 'auto-draft' === $post->post_status ) {
 106      $is_new_post = true;
 107      // Override "(Auto Draft)" new post default title with empty string, or filtered value.
 108      $initial_edits = array(
 109          'title'   => $post->post_title,
 110          'content' => $post->post_content,
 111          'excerpt' => $post->post_excerpt,
 112      );
 113  }
 114  
 115  // Preload server-registered block schemas.
 116  wp_add_inline_script(
 117      'wp-blocks',
 118      'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');'
 119  );
 120  
 121  // Get admin url for handling meta boxes.
 122  $meta_box_url = admin_url( 'post.php' );
 123  $meta_box_url = add_query_arg(
 124      array(
 125          'post'                  => $post->ID,
 126          'action'                => 'edit',
 127          'meta-box-loader'       => true,
 128          'meta-box-loader-nonce' => wp_create_nonce( 'meta-box-loader' ),
 129      ),
 130      $meta_box_url
 131  );
 132  wp_add_inline_script(
 133      'wp-editor',
 134      sprintf( 'var _wpMetaBoxUrl = %s;', wp_json_encode( $meta_box_url ) ),
 135      'before'
 136  );
 137  
 138  
 139  /*
 140   * Initialize the editor.
 141   */
 142  
 143  $align_wide         = get_theme_support( 'align-wide' );
 144  $color_palette      = current( (array) get_theme_support( 'editor-color-palette' ) );
 145  $font_sizes         = current( (array) get_theme_support( 'editor-font-sizes' ) );
 146  $gradient_presets   = current( (array) get_theme_support( 'editor-gradient-presets' ) );
 147  $custom_line_height = get_theme_support( 'custom-line-height' );
 148  $custom_units       = get_theme_support( 'custom-units' );
 149  $custom_spacing     = get_theme_support( 'custom-spacing' );
 150  
 151  /**
 152   * Filters the allowed block types for the editor, defaulting to true (all
 153   * block types supported).
 154   *
 155   * @since 5.0.0
 156   *
 157   * @param bool|array $allowed_block_types Array of block type slugs, or
 158   *                                        boolean to enable/disable all.
 159   * @param WP_Post    $post                The post resource data.
 160   */
 161  $allowed_block_types = apply_filters( 'allowed_block_types', true, $post );
 162  
 163  /*
 164   * Get all available templates for the post/page attributes meta-box.
 165   * The "Default template" array element should only be added if the array is
 166   * not empty so we do not trigger the template select element without any options
 167   * besides the default value.
 168   */
 169  $available_templates = wp_get_theme()->get_page_templates( get_post( $post->ID ) );
 170  $available_templates = ! empty( $available_templates ) ? array_merge(
 171      array(
 172          /** This filter is documented in wp-admin/includes/meta-boxes.php */
 173          '' => apply_filters( 'default_page_template_title', __( 'Default template' ), 'rest-api' ),
 174      ),
 175      $available_templates
 176  ) : $available_templates;
 177  
 178  // Media settings.
 179  $max_upload_size = wp_max_upload_size();
 180  if ( ! $max_upload_size ) {
 181      $max_upload_size = 0;
 182  }
 183  
 184  // Editor Styles.
 185  $styles = array(
 186      array(
 187          'css' => 'body { font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif }',
 188      ),
 189  );
 190  if ( $editor_styles && current_theme_supports( 'editor-styles' ) ) {
 191      foreach ( $editor_styles as $style ) {
 192          if ( preg_match( '~^(https?:)?//~', $style ) ) {
 193              $response = wp_remote_get( $style );
 194              if ( ! is_wp_error( $response ) ) {
 195                  $styles[] = array(
 196                      'css' => wp_remote_retrieve_body( $response ),
 197                  );
 198              }
 199          } else {
 200              $file = get_theme_file_path( $style );
 201              if ( is_file( $file ) ) {
 202                  $styles[] = array(
 203                      'css'     => file_get_contents( $file ),
 204                      'baseURL' => get_theme_file_uri( $style ),
 205                  );
 206              }
 207          }
 208      }
 209  }
 210  
 211  // Image sizes.
 212  
 213  /** This filter is documented in wp-admin/includes/media.php */
 214  $image_size_names = apply_filters(
 215      'image_size_names_choose',
 216      array(
 217          'thumbnail' => __( 'Thumbnail' ),
 218          'medium'    => __( 'Medium' ),
 219          'large'     => __( 'Large' ),
 220          'full'      => __( 'Full Size' ),
 221      )
 222  );
 223  
 224  $available_image_sizes = array();
 225  foreach ( $image_size_names as $image_size_slug => $image_size_name ) {
 226      $available_image_sizes[] = array(
 227          'slug' => $image_size_slug,
 228          'name' => $image_size_name,
 229      );
 230  }
 231  
 232  $default_size       = get_option( 'image_default_size', 'large' );
 233  $image_default_size = in_array( $default_size, array_keys( $image_size_names ), true ) ? $image_default_size : 'large';
 234  
 235  $image_dimensions = array();
 236  $all_sizes        = wp_get_registered_image_subsizes();
 237  foreach ( $available_image_sizes as $size ) {
 238      $key = $size['slug'];
 239      if ( isset( $all_sizes[ $key ] ) ) {
 240          $image_dimensions[ $key ] = $all_sizes[ $key ];
 241      }
 242  }
 243  
 244  // Lock settings.
 245  $user_id = wp_check_post_lock( $post->ID );
 246  if ( $user_id ) {
 247      $locked = false;
 248  
 249      /** This filter is documented in wp-admin/includes/post.php */
 250      if ( apply_filters( 'show_post_locked_dialog', true, $post, $user_id ) ) {
 251          $locked = true;
 252      }
 253  
 254      $user_details = null;
 255      if ( $locked ) {
 256          $user         = get_userdata( $user_id );
 257          $user_details = array(
 258              'name' => $user->display_name,
 259          );
 260          $avatar       = get_avatar_url( $user_id, array( 'size' => 64 ) );
 261      }
 262  
 263      $lock_details = array(
 264          'isLocked' => $locked,
 265          'user'     => $user_details,
 266      );
 267  } else {
 268      // Lock the post.
 269      $active_post_lock = wp_set_post_lock( $post->ID );
 270      if ( $active_post_lock ) {
 271          $active_post_lock = esc_attr( implode( ':', $active_post_lock ) );
 272      }
 273  
 274      $lock_details = array(
 275          'isLocked'       => false,
 276          'activePostLock' => $active_post_lock,
 277      );
 278  }
 279  
 280  /**
 281   * Filters the body placeholder text.
 282   *
 283   * @since 5.0.0
 284   * @since 5.8.0 Changed the default placeholder text.
 285   *
 286   * @param string  $text Placeholder text. Default 'Type / to choose a block'.
 287   * @param WP_Post $post Post object.
 288   */
 289  $body_placeholder = apply_filters( 'write_your_story', __( 'Type / to choose a block' ), $post );
 290  
 291  $editor_settings = array(
 292      'alignWide'                            => $align_wide,
 293      'availableTemplates'                   => $available_templates,
 294      'allowedBlockTypes'                    => $allowed_block_types,
 295      'disableCustomColors'                  => get_theme_support( 'disable-custom-colors' ),
 296      'disableCustomFontSizes'               => get_theme_support( 'disable-custom-font-sizes' ),
 297      'disableCustomGradients'               => get_theme_support( 'disable-custom-gradients' ),
 298      'disablePostFormats'                   => ! current_theme_supports( 'post-formats' ),
 299      /** This filter is documented in wp-admin/edit-form-advanced.php */
 300      'titlePlaceholder'                     => apply_filters( 'enter_title_here', __( 'Add title' ), $post ),
 301      'bodyPlaceholder'                      => $body_placeholder,
 302      'isRTL'                                => is_rtl(),
 303      'autosaveInterval'                     => AUTOSAVE_INTERVAL,
 304      'maxUploadFileSize'                    => $max_upload_size,
 305      'allowedMimeTypes'                     => get_allowed_mime_types(),
 306      'styles'                               => $styles,
 307      'imageSizes'                           => $available_image_sizes,
 308      'imageDefaultSize'                     => $image_default_size,
 309      'imageDimensions'                      => $image_dimensions,
 310      'richEditingEnabled'                   => user_can_richedit(),
 311      'postLock'                             => $lock_details,
 312      'postLockUtils'                        => array(
 313          'nonce'       => wp_create_nonce( 'lock-post_' . $post->ID ),
 314          'unlockNonce' => wp_create_nonce( 'update-post_' . $post->ID ),
 315          'ajaxUrl'     => admin_url( 'admin-ajax.php' ),
 316      ),
 317      '__experimentalBlockPatterns'          => WP_Block_Patterns_Registry::get_instance()->get_all_registered(),
 318      '__experimentalBlockPatternCategories' => WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered(),
 319  
 320      // Whether or not to load the 'postcustom' meta box is stored as a user meta
 321      // field so that we're not always loading its assets.
 322      'enableCustomFields'                   => (bool) get_user_meta( get_current_user_id(), 'enable_custom_fields', true ),
 323      'enableCustomLineHeight'               => $custom_line_height,
 324      'enableCustomUnits'                    => $custom_units,
 325      'enableCustomSpacing'                  => $custom_spacing,
 326  );
 327  
 328  $autosave = wp_get_post_autosave( $post->ID );
 329  if ( $autosave ) {
 330      if ( mysql2date( 'U', $autosave->post_modified_gmt, false ) > mysql2date( 'U', $post->post_modified_gmt, false ) ) {
 331          $editor_settings['autosave'] = array(
 332              'editLink' => get_edit_post_link( $autosave->ID ),
 333          );
 334      } else {
 335          wp_delete_post_revision( $autosave->ID );
 336      }
 337  }
 338  
 339  if ( false !== $color_palette ) {
 340      $editor_settings['colors'] = $color_palette;
 341  }
 342  
 343  if ( false !== $font_sizes ) {
 344      $editor_settings['fontSizes'] = $font_sizes;
 345  }
 346  
 347  if ( false !== $gradient_presets ) {
 348      $editor_settings['gradients'] = $gradient_presets;
 349  }
 350  
 351  if ( ! empty( $post_type_object->template ) ) {
 352      $editor_settings['template']     = $post_type_object->template;
 353      $editor_settings['templateLock'] = ! empty( $post_type_object->template_lock ) ? $post_type_object->template_lock : false;
 354  }
 355  
 356  // If there's no template set on a new post, use the post format, instead.
 357  if ( $is_new_post && ! isset( $editor_settings['template'] ) && 'post' === $post->post_type ) {
 358      $post_format = get_post_format( $post );
 359      if ( in_array( $post_format, array( 'audio', 'gallery', 'image', 'quote', 'video' ), true ) ) {
 360          $editor_settings['template'] = array( array( "core/$post_format" ) );
 361      }
 362  }
 363  
 364  /**
 365   * Scripts
 366   */
 367  wp_enqueue_media(
 368      array(
 369          'post' => $post->ID,
 370      )
 371  );
 372  wp_tinymce_inline_scripts();
 373  wp_enqueue_editor();
 374  
 375  
 376  /**
 377   * Styles
 378   */
 379  wp_enqueue_style( 'wp-edit-post' );
 380  
 381  /**
 382   * Fires after block assets have been enqueued for the editing interface.
 383   *
 384   * Call `add_action` on any hook before 'admin_enqueue_scripts'.
 385   *
 386   * In the function call you supply, simply use `wp_enqueue_script` and
 387   * `wp_enqueue_style` to add your functionality to the block editor.
 388   *
 389   * @since 5.0.0
 390   */
 391  do_action( 'enqueue_block_editor_assets' );
 392  
 393  // In order to duplicate classic meta box behaviour, we need to run the classic meta box actions.
 394  require_once ABSPATH . 'wp-admin/includes/meta-boxes.php';
 395  register_and_do_post_meta_boxes( $post );
 396  
 397  // Check if the Custom Fields meta box has been removed at some point.
 398  $core_meta_boxes = $wp_meta_boxes[ $current_screen->id ]['normal']['core'];
 399  if ( ! isset( $core_meta_boxes['postcustom'] ) || ! $core_meta_boxes['postcustom'] ) {
 400      unset( $editor_settings['enableCustomFields'] );
 401  }
 402  
 403  /**
 404   * Filters the settings to pass to the block editor.
 405   *
 406   * @since 5.0.0
 407   *
 408   * @param array   $editor_settings Default editor settings.
 409   * @param WP_Post $post            Post being edited.
 410   */
 411  $editor_settings = apply_filters( 'block_editor_settings', $editor_settings, $post );
 412  
 413  $init_script = <<<JS
 414  ( function() {
 415      window._wpLoadBlockEditor = new Promise( function( resolve ) {
 416          wp.domReady( function() {
 417              resolve( wp.editPost.initializeEditor( 'editor', "%s", %d, %s, %s ) );
 418          } );
 419      } );
 420  } )();
 421  JS;
 422  
 423  $script = sprintf(
 424      $init_script,
 425      $post->post_type,
 426      $post->ID,
 427      wp_json_encode( $editor_settings ),
 428      wp_json_encode( $initial_edits )
 429  );
 430  wp_add_inline_script( 'wp-edit-post', $script );
 431  
 432  require_once ABSPATH . 'wp-admin/admin-header.php';
 433  ?>
 434  
 435  <div class="block-editor">
 436      <h1 class="screen-reader-text hide-if-no-js"><?php echo esc_html( $title ); ?></h1>
 437      <div id="editor" class="block-editor__container hide-if-no-js"></div>
 438      <div id="metaboxes" class="hidden">
 439          <?php the_block_editor_meta_boxes(); ?>
 440      </div>
 441  
 442      <?php // JavaScript is disabled. ?>
 443      <div class="wrap hide-if-js block-editor-no-js">
 444          <h1 class="wp-heading-inline"><?php echo esc_html( $title ); ?></h1>
 445          <div class="notice notice-error notice-alt">
 446              <p>
 447                  <?php
 448                      $message = sprintf(
 449                          /* translators: %s: A link to install the Classic Editor plugin. */
 450                          __( 'The block editor requires JavaScript. Please enable JavaScript in your browser settings, or try the <a href="%s">Classic Editor plugin</a>.' ),
 451                          esc_url( wp_nonce_url( self_admin_url( 'plugin-install.php?tab=favorites&user=wordpressdotorg&save=0' ), 'save_wporg_username_' . get_current_user_id() ) )
 452                      );
 453  
 454                      /**
 455                       * Filters the message displayed in the block editor interface when JavaScript is
 456                       * not enabled in the browser.
 457                       *
 458                       * @since 5.0.3
 459                       *
 460                       * @param string  $message The message being displayed.
 461                       * @param WP_Post $post    The post being edited.
 462                       */
 463                      echo apply_filters( 'block_editor_no_javascript_message', $message, $post );
 464                      ?>
 465              </p>
 466          </div>
 467      </div>
 468  </div>


Generated: Sun Apr 18 01:00:12 2021 Cross-referenced by PHPXref 0.7.1