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


Generated: Mon Oct 26 01:00:02 2020 Cross-referenced by PHPXref 0.7.1