[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> block-editor.php (source)

   1  <?php
   2  /**
   3   * Block Editor API.
   4   *
   5   * @package WordPress
   6   * @subpackage Editor
   7   * @since 5.8.0
   8   */
   9  
  10  /**
  11   * Returns the list of default categories for block types.
  12   *
  13   * @since 5.8.0
  14   * @since 6.3.0 Reusable Blocks renamed to Patterns.
  15   *
  16   * @return array[] Array of categories for block types.
  17   */
  18  function get_default_block_categories() {
  19      return array(
  20          array(
  21              'slug'  => 'text',
  22              'title' => _x( 'Text', 'block category' ),
  23              'icon'  => null,
  24          ),
  25          array(
  26              'slug'  => 'media',
  27              'title' => _x( 'Media', 'block category' ),
  28              'icon'  => null,
  29          ),
  30          array(
  31              'slug'  => 'design',
  32              'title' => _x( 'Design', 'block category' ),
  33              'icon'  => null,
  34          ),
  35          array(
  36              'slug'  => 'widgets',
  37              'title' => _x( 'Widgets', 'block category' ),
  38              'icon'  => null,
  39          ),
  40          array(
  41              'slug'  => 'theme',
  42              'title' => _x( 'Theme', 'block category' ),
  43              'icon'  => null,
  44          ),
  45          array(
  46              'slug'  => 'embed',
  47              'title' => _x( 'Embeds', 'block category' ),
  48              'icon'  => null,
  49          ),
  50          array(
  51              'slug'  => 'reusable',
  52              'title' => _x( 'Patterns', 'block category' ),
  53              'icon'  => null,
  54          ),
  55      );
  56  }
  57  
  58  /**
  59   * Returns all the categories for block types that will be shown in the block editor.
  60   *
  61   * @since 5.0.0
  62   * @since 5.8.0 It is possible to pass the block editor context as param.
  63   *
  64   * @param WP_Post|WP_Block_Editor_Context $post_or_block_editor_context The current post object or
  65   *                                                                      the block editor context.
  66   *
  67   * @return array[] Array of categories for block types.
  68   */
  69  function get_block_categories( $post_or_block_editor_context ) {
  70      $block_categories     = get_default_block_categories();
  71      $block_editor_context = $post_or_block_editor_context instanceof WP_Post ?
  72          new WP_Block_Editor_Context(
  73              array(
  74                  'post' => $post_or_block_editor_context,
  75              )
  76          ) : $post_or_block_editor_context;
  77  
  78      /**
  79       * Filters the default array of categories for block types.
  80       *
  81       * @since 5.8.0
  82       *
  83       * @param array[]                 $block_categories     Array of categories for block types.
  84       * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
  85       */
  86      $block_categories = apply_filters( 'block_categories_all', $block_categories, $block_editor_context );
  87  
  88      if ( ! empty( $block_editor_context->post ) ) {
  89          $post = $block_editor_context->post;
  90  
  91          /**
  92           * Filters the default array of categories for block types.
  93           *
  94           * @since 5.0.0
  95           * @deprecated 5.8.0 Use the {@see 'block_categories_all'} filter instead.
  96           *
  97           * @param array[] $block_categories Array of categories for block types.
  98           * @param WP_Post $post             Post being loaded.
  99           */
 100          $block_categories = apply_filters_deprecated( 'block_categories', array( $block_categories, $post ), '5.8.0', 'block_categories_all' );
 101      }
 102  
 103      return $block_categories;
 104  }
 105  
 106  /**
 107   * Gets the list of allowed block types to use in the block editor.
 108   *
 109   * @since 5.8.0
 110   *
 111   * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
 112   * @return bool|string[] Array of block type slugs, or boolean to enable/disable all.
 113   */
 114  function get_allowed_block_types( $block_editor_context ) {
 115      $allowed_block_types = true;
 116  
 117      /**
 118       * Filters the allowed block types for all editor types.
 119       *
 120       * @since 5.8.0
 121       *
 122       * @param bool|string[]           $allowed_block_types  Array of block type slugs, or boolean to enable/disable all.
 123       *                                                      Default true (all registered block types supported).
 124       * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
 125       */
 126      $allowed_block_types = apply_filters( 'allowed_block_types_all', $allowed_block_types, $block_editor_context );
 127  
 128      if ( ! empty( $block_editor_context->post ) ) {
 129          $post = $block_editor_context->post;
 130  
 131          /**
 132           * Filters the allowed block types for the editor.
 133           *
 134           * @since 5.0.0
 135           * @deprecated 5.8.0 Use the {@see 'allowed_block_types_all'} filter instead.
 136           *
 137           * @param bool|string[] $allowed_block_types Array of block type slugs, or boolean to enable/disable all.
 138           *                                           Default true (all registered block types supported)
 139           * @param WP_Post       $post                The post resource data.
 140           */
 141          $allowed_block_types = apply_filters_deprecated( 'allowed_block_types', array( $allowed_block_types, $post ), '5.8.0', 'allowed_block_types_all' );
 142      }
 143  
 144      return $allowed_block_types;
 145  }
 146  
 147  /**
 148   * Returns the default block editor settings.
 149   *
 150   * @since 5.8.0
 151   *
 152   * @return array The default block editor settings.
 153   */
 154  function get_default_block_editor_settings() {
 155      // Media settings.
 156  
 157      // wp_max_upload_size() can be expensive, so only call it when relevant for the current user.
 158      $max_upload_size = 0;
 159      if ( current_user_can( 'upload_files' ) ) {
 160          $max_upload_size = wp_max_upload_size();
 161          if ( ! $max_upload_size ) {
 162              $max_upload_size = 0;
 163          }
 164      }
 165  
 166      /** This filter is documented in wp-admin/includes/media.php */
 167      $image_size_names = apply_filters(
 168          'image_size_names_choose',
 169          array(
 170              'thumbnail' => __( 'Thumbnail' ),
 171              'medium'    => __( 'Medium' ),
 172              'large'     => __( 'Large' ),
 173              'full'      => __( 'Full Size' ),
 174          )
 175      );
 176  
 177      $available_image_sizes = array();
 178      foreach ( $image_size_names as $image_size_slug => $image_size_name ) {
 179          $available_image_sizes[] = array(
 180              'slug' => $image_size_slug,
 181              'name' => $image_size_name,
 182          );
 183      }
 184  
 185      $default_size       = get_option( 'image_default_size', 'large' );
 186      $image_default_size = isset( $image_size_names[ $default_size ] ) ? $default_size : 'large';
 187  
 188      $image_dimensions = array();
 189      $all_sizes        = wp_get_registered_image_subsizes();
 190      foreach ( $available_image_sizes as $size ) {
 191          $key = $size['slug'];
 192          if ( isset( $all_sizes[ $key ] ) ) {
 193              $image_dimensions[ $key ] = $all_sizes[ $key ];
 194          }
 195      }
 196  
 197      /*
 198       * These styles are used when the "Use theme styles" preference is turned off
 199       * and in the Customizer widgets editor.
 200       *
 201       * They were previously read from `css/dist/block-editor/default-editor-styles.css`,
 202       * which the block editor package no longer ships.
 203       */
 204      $default_editor_styles = array(
 205          array(
 206              'css' => 'body{font-family:-apple-system,system-ui,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif;font-size:18px;line-height:1.5;--wp--style--block-gap:2em}p{line-height:1.8}',
 207          ),
 208      );
 209  
 210      $editor_settings = array(
 211          'alignWide'                        => get_theme_support( 'align-wide' ),
 212          'allowedBlockTypes'                => true,
 213          'allowedMimeTypes'                 => get_allowed_mime_types(),
 214          /*
 215           * Attaching media to a post moves its attachment page URL, and the old URL then returns a 404.
 216           * Only attach on save when attachment pages are disabled, since they then redirect to the file.
 217           */
 218          'autoAttachMediaEnabled'           => ! get_option( 'wp_attachment_pages_enabled' ),
 219          'defaultEditorStyles'              => $default_editor_styles,
 220          'blockCategories'                  => get_default_block_categories(),
 221          'isRTL'                            => is_rtl(),
 222          'imageDefaultSize'                 => $image_default_size,
 223          'imageDimensions'                  => $image_dimensions,
 224          'imageEditing'                     => true,
 225          'imageSizes'                       => $available_image_sizes,
 226          'maxUploadFileSize'                => $max_upload_size,
 227          '__experimentalDashboardLink'      => admin_url( '/' ),
 228          // The following flag is required to enable the new Gallery block format on the mobile apps in 5.9.
 229          '__unstableGalleryWithImageBlocks' => true,
 230      );
 231  
 232      $theme_settings = get_classic_theme_supports_block_editor_settings();
 233      foreach ( $theme_settings as $key => $value ) {
 234          $editor_settings[ $key ] = $value;
 235      }
 236  
 237      return $editor_settings;
 238  }
 239  
 240  /**
 241   * Returns the block editor settings needed to use the Legacy Widget block which
 242   * is not registered by default.
 243   *
 244   * @since 5.8.0
 245   *
 246   * @return array Settings to be used with get_block_editor_settings().
 247   */
 248  function get_legacy_widget_block_editor_settings() {
 249      $editor_settings = array();
 250  
 251      /**
 252       * Filters the list of widget-type IDs that should **not** be offered by the
 253       * Legacy Widget block.
 254       *
 255       * Returning an empty array will make all widgets available.
 256       *
 257       * @since 5.8.0
 258       *
 259       * @param string[] $widgets An array of excluded widget-type IDs.
 260       */
 261      $editor_settings['widgetTypesToHideFromLegacyWidgetBlock'] = apply_filters(
 262          'widget_types_to_hide_from_legacy_widget_block',
 263          array(
 264              'pages',
 265              'calendar',
 266              'archives',
 267              'media_audio',
 268              'media_image',
 269              'media_gallery',
 270              'media_video',
 271              'search',
 272              'text',
 273              'categories',
 274              'recent-posts',
 275              'recent-comments',
 276              'rss',
 277              'tag_cloud',
 278              'custom_html',
 279              'block',
 280          )
 281      );
 282  
 283      return $editor_settings;
 284  }
 285  
 286  /**
 287   * Collect the block editor assets that need to be loaded into the editor's iframe.
 288   *
 289   * @since 6.0.0
 290   * @access private
 291   *
 292   * @global WP_Styles  $wp_styles  The WP_Styles current instance.
 293   * @global WP_Scripts $wp_scripts The WP_Scripts current instance.
 294   *
 295   * @return array {
 296   *     The block editor assets.
 297   *
 298   *     @type string|false $styles  String containing the HTML for styles.
 299   *     @type string|false $scripts String containing the HTML for scripts.
 300   * }
 301   */
 302  function _wp_get_iframed_editor_assets() {
 303      global $wp_styles, $wp_scripts;
 304  
 305      // Keep track of the styles and scripts instance to restore later.
 306      $current_wp_styles  = $wp_styles;
 307      $current_wp_scripts = $wp_scripts;
 308  
 309      // Create new instances to collect the assets.
 310      $wp_styles  = new WP_Styles();
 311      $wp_scripts = new WP_Scripts();
 312  
 313      /*
 314       * Register all currently registered styles and scripts. The actions that
 315       * follow enqueue assets, but don't necessarily register them.
 316       */
 317      $wp_styles->registered  = $current_wp_styles->registered;
 318      $wp_scripts->registered = $current_wp_scripts->registered;
 319  
 320      /*
 321       * We generally do not need reset styles for the iframed editor.
 322       * However, if it's a classic theme, margins will be added to every block,
 323       * which is reset specifically for list items, so classic themes rely on
 324       * these reset styles.
 325       */
 326      $wp_styles->done =
 327          wp_theme_has_theme_json() ? array( 'wp-reset-editor-styles' ) : array();
 328  
 329      wp_enqueue_script( 'wp-polyfill' );
 330      // Enqueue the `editorStyle` handles for all core block, and dependencies.
 331      wp_enqueue_style( 'wp-edit-blocks' );
 332  
 333      if ( current_theme_supports( 'wp-block-styles' ) ) {
 334          wp_enqueue_style( 'wp-block-library-theme' );
 335      }
 336  
 337      /*
 338       * We don't want to load EDITOR scripts in the iframe, only enqueue
 339       * front-end assets for the content.
 340       */
 341      add_filter( 'should_load_block_editor_scripts_and_styles', '__return_false' );
 342      /** This action is documented in wp-includes/script-loader.php */
 343      do_action( 'enqueue_block_assets' );
 344      remove_filter( 'should_load_block_editor_scripts_and_styles', '__return_false' );
 345  
 346      $block_registry = WP_Block_Type_Registry::get_instance();
 347  
 348      /*
 349       * Additionally, do enqueue `editorStyle` assets for all blocks, which
 350       * contains editor-only styling for blocks (editor content).
 351       */
 352      foreach ( $block_registry->get_all_registered() as $block_type ) {
 353          if ( isset( $block_type->editor_style_handles ) && is_array( $block_type->editor_style_handles ) ) {
 354              foreach ( $block_type->editor_style_handles as $style_handle ) {
 355                  wp_enqueue_style( $style_handle );
 356              }
 357          }
 358      }
 359  
 360      /**
 361       * Remove the deprecated `print_emoji_styles` handler.
 362       * It avoids breaking style generation with a deprecation message.
 363       */
 364      $has_emoji_styles = has_action( 'wp_print_styles', 'print_emoji_styles' );
 365      if ( $has_emoji_styles ) {
 366          remove_action( 'wp_print_styles', 'print_emoji_styles' );
 367      }
 368  
 369      ob_start();
 370      wp_print_styles();
 371      wp_print_font_faces();
 372      wp_print_font_faces_from_style_variations();
 373      $styles = ob_get_clean();
 374  
 375      if ( $has_emoji_styles ) {
 376          add_action( 'wp_print_styles', 'print_emoji_styles' );
 377      }
 378  
 379      ob_start();
 380      wp_print_head_scripts();
 381      wp_print_footer_scripts();
 382      $scripts = ob_get_clean();
 383  
 384      // Restore the original instances.
 385      $wp_styles  = $current_wp_styles;
 386      $wp_scripts = $current_wp_scripts;
 387  
 388      return array(
 389          'styles'  => $styles,
 390          'scripts' => $scripts,
 391      );
 392  }
 393  
 394  /**
 395   * Finds the first occurrence of a specific block in an array of blocks.
 396   *
 397   * @since 6.3.0
 398   *
 399   * @param array  $blocks     Array of blocks.
 400   * @param string $block_name Name of the block to find.
 401   * @return array Found block, or empty array if none found.
 402   */
 403  function wp_get_first_block( $blocks, $block_name ) {
 404      foreach ( $blocks as $block ) {
 405          if ( $block_name === $block['blockName'] ) {
 406              return $block;
 407          }
 408          if ( ! empty( $block['innerBlocks'] ) ) {
 409              $found_block = wp_get_first_block( $block['innerBlocks'], $block_name );
 410  
 411              if ( ! empty( $found_block ) ) {
 412                  return $found_block;
 413              }
 414          }
 415      }
 416  
 417      return array();
 418  }
 419  
 420  /**
 421   * Retrieves Post Content block attributes from the current post template.
 422   *
 423   * @since 6.3.0
 424   * @since 6.4.0 Return null if there is no post content block.
 425   * @access private
 426   *
 427   * @global int $post_ID
 428   *
 429   * @return array|null Post Content block attributes array or null if Post Content block doesn't exist.
 430   */
 431  function wp_get_post_content_block_attributes() {
 432      global $post_ID;
 433  
 434      $is_block_theme = wp_is_block_theme();
 435  
 436      if ( ! $is_block_theme || ! $post_ID ) {
 437          return null;
 438      }
 439  
 440      $template_slug = get_page_template_slug( $post_ID );
 441  
 442      if ( ! $template_slug ) {
 443          $post_slug      = 'singular';
 444          $page_slug      = 'singular';
 445          $template_types = get_block_templates();
 446  
 447          foreach ( $template_types as $template_type ) {
 448              if ( 'page' === $template_type->slug ) {
 449                  $page_slug = 'page';
 450              }
 451              if ( 'single' === $template_type->slug ) {
 452                  $post_slug = 'single';
 453              }
 454          }
 455  
 456          $what_post_type = get_post_type( $post_ID );
 457          switch ( $what_post_type ) {
 458              case 'page':
 459                  $template_slug = $page_slug;
 460                  break;
 461              default:
 462                  $template_slug = $post_slug;
 463                  break;
 464          }
 465      }
 466  
 467      $current_template = get_block_templates( array( 'slug__in' => array( $template_slug ) ) );
 468  
 469      if ( ! empty( $current_template ) ) {
 470          $template_blocks    = parse_blocks( $current_template[0]->content );
 471          $post_content_block = wp_get_first_block( $template_blocks, 'core/post-content' );
 472  
 473          if ( isset( $post_content_block['attrs'] ) ) {
 474              return $post_content_block['attrs'];
 475          }
 476      }
 477  
 478      return null;
 479  }
 480  
 481  /**
 482   * Returns the contextualized block editor settings for a selected editor context.
 483   *
 484   * @since 5.8.0
 485   *
 486   * @param array                   $custom_settings      Custom settings to use with the given editor type.
 487   * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
 488   * @return array The contextualized block editor settings.
 489   */
 490  function get_block_editor_settings( array $custom_settings, $block_editor_context ) {
 491      $editor_settings = array_merge(
 492          get_default_block_editor_settings(),
 493          array(
 494              'allowedBlockTypes' => get_allowed_block_types( $block_editor_context ),
 495              'blockCategories'   => get_block_categories( $block_editor_context ),
 496          ),
 497          $custom_settings
 498      );
 499  
 500      $editor_settings['__experimentalBlockBindingsSupportedAttributes'] = array();
 501      foreach ( array_keys( WP_Block_Type_Registry::get_instance()->get_all_registered() ) as $block_type ) {
 502          $supported_block_attributes = get_block_bindings_supported_attributes( $block_type );
 503          if ( ! empty( $supported_block_attributes ) ) {
 504              $editor_settings['__experimentalBlockBindingsSupportedAttributes'][ $block_type ] = $supported_block_attributes;
 505          }
 506      }
 507  
 508      $global_styles = array();
 509      $presets       = array(
 510          array(
 511              'css'            => 'variables',
 512              '__unstableType' => 'presets',
 513              'isGlobalStyles' => true,
 514          ),
 515          array(
 516              'css'            => 'presets',
 517              '__unstableType' => 'presets',
 518              'isGlobalStyles' => true,
 519          ),
 520      );
 521      foreach ( $presets as $preset_style ) {
 522          $actual_css = wp_get_global_stylesheet( array( $preset_style['css'] ) );
 523          if ( '' !== $actual_css ) {
 524              $preset_style['css'] = $actual_css;
 525              $global_styles[]     = $preset_style;
 526          }
 527      }
 528  
 529      $block_classes = array(
 530          'css'            => 'styles',
 531          '__unstableType' => 'theme',
 532          'isGlobalStyles' => true,
 533      );
 534      $actual_css    = wp_get_global_stylesheet( array( $block_classes['css'] ) );
 535      if ( '' !== $actual_css ) {
 536          $block_classes['css'] = $actual_css;
 537          $global_styles[]      = $block_classes;
 538      }
 539  
 540      // Get any additional css from the customizer and add it before global styles custom CSS.
 541      $global_styles[] = array(
 542          'css'            => wp_get_custom_css(),
 543          '__unstableType' => 'user',
 544          'isGlobalStyles' => false,
 545      );
 546  
 547      /*
 548       * Add the custom CSS as a separate stylesheet so any invalid CSS
 549       * entered by users does not break other global styles.
 550       */
 551      $global_styles[] = array(
 552          'css'            => wp_get_global_stylesheet( array( 'custom-css' ) ),
 553          '__unstableType' => 'user',
 554          'isGlobalStyles' => true,
 555      );
 556  
 557      $editor_settings['styles'] = array_merge( $global_styles, get_block_editor_theme_styles() );
 558  
 559      $editor_settings['__experimentalFeatures'] = wp_get_global_settings();
 560      // These settings may need to be updated based on data coming from theme.json sources.
 561      if ( isset( $editor_settings['__experimentalFeatures']['color']['palette'] ) ) {
 562          $colors_by_origin          = $editor_settings['__experimentalFeatures']['color']['palette'];
 563          $editor_settings['colors'] = $colors_by_origin['custom'] ?? $colors_by_origin['theme'] ?? $colors_by_origin['default'];
 564      }
 565      if ( isset( $editor_settings['__experimentalFeatures']['color']['gradients'] ) ) {
 566          $gradients_by_origin          = $editor_settings['__experimentalFeatures']['color']['gradients'];
 567          $editor_settings['gradients'] = $gradients_by_origin['custom'] ?? $gradients_by_origin['theme'] ?? $gradients_by_origin['default'];
 568      }
 569      if ( isset( $editor_settings['__experimentalFeatures']['typography']['fontSizes'] ) ) {
 570          $font_sizes_by_origin         = $editor_settings['__experimentalFeatures']['typography']['fontSizes'];
 571          $editor_settings['fontSizes'] = $font_sizes_by_origin['custom'] ?? $font_sizes_by_origin['theme'] ?? $font_sizes_by_origin['default'];
 572      }
 573      if ( isset( $editor_settings['__experimentalFeatures']['color']['custom'] ) ) {
 574          $editor_settings['disableCustomColors'] = ! $editor_settings['__experimentalFeatures']['color']['custom'];
 575          unset( $editor_settings['__experimentalFeatures']['color']['custom'] );
 576      }
 577      if ( isset( $editor_settings['__experimentalFeatures']['color']['customGradient'] ) ) {
 578          $editor_settings['disableCustomGradients'] = ! $editor_settings['__experimentalFeatures']['color']['customGradient'];
 579          unset( $editor_settings['__experimentalFeatures']['color']['customGradient'] );
 580      }
 581      if ( isset( $editor_settings['__experimentalFeatures']['typography']['customFontSize'] ) ) {
 582          $editor_settings['disableCustomFontSizes'] = ! $editor_settings['__experimentalFeatures']['typography']['customFontSize'];
 583          unset( $editor_settings['__experimentalFeatures']['typography']['customFontSize'] );
 584      }
 585      if ( isset( $editor_settings['__experimentalFeatures']['typography']['lineHeight'] ) ) {
 586          $editor_settings['enableCustomLineHeight'] = $editor_settings['__experimentalFeatures']['typography']['lineHeight'];
 587          unset( $editor_settings['__experimentalFeatures']['typography']['lineHeight'] );
 588      }
 589      if ( isset( $editor_settings['__experimentalFeatures']['spacing']['units'] ) ) {
 590          $editor_settings['enableCustomUnits'] = $editor_settings['__experimentalFeatures']['spacing']['units'];
 591          unset( $editor_settings['__experimentalFeatures']['spacing']['units'] );
 592      }
 593      if ( isset( $editor_settings['__experimentalFeatures']['spacing']['padding'] ) ) {
 594          $editor_settings['enableCustomSpacing'] = $editor_settings['__experimentalFeatures']['spacing']['padding'];
 595          unset( $editor_settings['__experimentalFeatures']['spacing']['padding'] );
 596      }
 597      if ( isset( $editor_settings['__experimentalFeatures']['spacing']['customSpacingSize'] ) ) {
 598          $editor_settings['disableCustomSpacingSizes'] = ! $editor_settings['__experimentalFeatures']['spacing']['customSpacingSize'];
 599          unset( $editor_settings['__experimentalFeatures']['spacing']['customSpacingSize'] );
 600      }
 601  
 602      if ( isset( $editor_settings['__experimentalFeatures']['spacing']['spacingSizes'] ) ) {
 603          $spacing_sizes_by_origin         = $editor_settings['__experimentalFeatures']['spacing']['spacingSizes'];
 604          $editor_settings['spacingSizes'] = $spacing_sizes_by_origin['custom'] ?? $spacing_sizes_by_origin['theme'] ?? $spacing_sizes_by_origin['default'];
 605      }
 606  
 607      $editor_settings['__unstableResolvedAssets']         = _wp_get_iframed_editor_assets();
 608      $editor_settings['__unstableIsBlockBasedTheme']      = wp_is_block_theme();
 609      $editor_settings['localAutosaveInterval']            = 15;
 610      $editor_settings['disableLayoutStyles']              = current_theme_supports( 'disable-layout-styles' );
 611      $editor_settings['__experimentalDiscussionSettings'] = array(
 612          'commentOrder'         => get_option( 'comment_order' ),
 613          'commentsPerPage'      => get_option( 'comments_per_page' ),
 614          'defaultCommentsPage'  => get_option( 'default_comments_page' ),
 615          'pageComments'         => get_option( 'page_comments' ),
 616          'threadComments'       => get_option( 'thread_comments' ),
 617          'threadCommentsDepth'  => get_option( 'thread_comments_depth' ),
 618          'defaultCommentStatus' => get_option( 'default_comment_status' ),
 619          'avatarURL'            => get_avatar_url(
 620              '',
 621              array(
 622                  'size'          => 96,
 623                  'force_default' => true,
 624                  'default'       => get_option( 'avatar_default' ),
 625              )
 626          ),
 627      );
 628  
 629      $post_content_block_attributes = wp_get_post_content_block_attributes();
 630  
 631      if ( isset( $post_content_block_attributes ) ) {
 632          $editor_settings['postContentAttributes'] = $post_content_block_attributes;
 633      }
 634  
 635      $editor_settings['canUpdateBlockBindings'] = current_user_can( 'edit_block_binding', $block_editor_context );
 636  
 637      /**
 638       * Filters the settings to pass to the block editor for all editor type.
 639       *
 640       * @since 5.8.0
 641       *
 642       * @param array                   $editor_settings      Default editor settings.
 643       * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
 644       */
 645      $editor_settings = apply_filters( 'block_editor_settings_all', $editor_settings, $block_editor_context );
 646  
 647      if ( ! empty( $block_editor_context->post ) ) {
 648          $post = $block_editor_context->post;
 649  
 650          /**
 651           * Filters the settings to pass to the block editor.
 652           *
 653           * @since 5.0.0
 654           * @deprecated 5.8.0 Use the {@see 'block_editor_settings_all'} filter instead.
 655           *
 656           * @param array   $editor_settings Default editor settings.
 657           * @param WP_Post $post            Post being edited.
 658           */
 659          $editor_settings = apply_filters_deprecated( 'block_editor_settings', array( $editor_settings, $post ), '5.8.0', 'block_editor_settings_all' );
 660      }
 661  
 662      $editor_settings['canEditCSS'] = current_user_can( 'edit_css' );
 663  
 664      return $editor_settings;
 665  }
 666  
 667  /**
 668   * Preloads common data used with the block editor by specifying an array of
 669   * REST API paths that will be preloaded for a given block editor context.
 670   *
 671   * @since 5.8.0
 672   *
 673   * @global WP_Post           $post              Global post object.
 674   * @global WP_Scripts        $wp_scripts        The WP_Scripts object for printing scripts.
 675   * @global WP_Styles         $wp_styles         The WP_Styles object for printing styles.
 676   * @global WP_Script_Modules $wp_script_modules The WP_Script_Modules object for printing script modules.
 677   *
 678   * @param (string|string[])[]     $preload_paths        List of paths to preload.
 679   * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
 680   */
 681  function block_editor_rest_api_preload( array $preload_paths, $block_editor_context ) {
 682      global $post, $wp_scripts, $wp_styles, $wp_script_modules;
 683  
 684      /**
 685       * Filters the array of REST API paths that will be used to preloaded common data for the block editor.
 686       *
 687       * @since 5.8.0
 688       *
 689       * @param (string|string[])[]     $preload_paths        Array of paths to preload.
 690       * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
 691       */
 692      $preload_paths = apply_filters( 'block_editor_rest_api_preload_paths', $preload_paths, $block_editor_context );
 693  
 694      if ( ! empty( $block_editor_context->post ) ) {
 695          $selected_post = $block_editor_context->post;
 696  
 697          /**
 698           * Filters the array of paths that will be preloaded.
 699           *
 700           * Preload common data by specifying an array of REST API paths that will be preloaded.
 701           *
 702           * @since 5.0.0
 703           * @deprecated 5.8.0 Use the {@see 'block_editor_rest_api_preload_paths'} filter instead.
 704           *
 705           * @param (string|string[])[] $preload_paths Array of paths to preload.
 706           * @param WP_Post             $selected_post Post being edited.
 707           */
 708          $preload_paths = apply_filters_deprecated( 'block_editor_preload_paths', array( $preload_paths, $selected_post ), '5.8.0', 'block_editor_rest_api_preload_paths' );
 709      }
 710  
 711      if ( empty( $preload_paths ) ) {
 712          return;
 713      }
 714  
 715      /*
 716       * Ensure the globals $post, $wp_scripts, $wp_styles, and $wp_script_modules
 717       * remain the same after API data is preloaded.
 718       * Because API preloading can call the_content and other filters, plugins
 719       * can unexpectedly modify the global $post or enqueue assets which are not
 720       * intended for the block editor.
 721       *
 722       * Copies are swapped in for the duration of the preload and the original
 723       * instances are restored afterwards, so that hooks and other references
 724       * bound to those instances remain valid.
 725       */
 726      $original_post              = $post;
 727      $original_wp_scripts        = $wp_scripts;
 728      $original_wp_styles         = $wp_styles;
 729      $original_wp_script_modules = $wp_script_modules;
 730  
 731      $post              = ! empty( $post ) ? clone $post : $post;
 732      $wp_scripts        = ! empty( $wp_scripts ) ? clone $wp_scripts : $wp_scripts;
 733      $wp_styles         = ! empty( $wp_styles ) ? clone $wp_styles : $wp_styles;
 734      $wp_script_modules = ! empty( $wp_script_modules ) ? clone $wp_script_modules : $wp_script_modules;
 735  
 736      foreach ( $preload_paths as &$path ) {
 737          if ( is_string( $path ) && ! str_starts_with( $path, '/' ) ) {
 738              $path = '/' . $path;
 739              continue;
 740          }
 741  
 742          if ( is_array( $path ) && is_string( $path[0] ) && ! str_starts_with( $path[0], '/' ) ) {
 743              $path[0] = '/' . $path[0];
 744          }
 745      }
 746  
 747      unset( $path );
 748  
 749      $preload_data = array_reduce(
 750          $preload_paths,
 751          'rest_preload_api_request',
 752          array()
 753      );
 754  
 755      // Restore the original $post, $wp_scripts, $wp_styles, and $wp_script_modules instances.
 756      $post = $original_post;
 757      if ( ! empty( $post ) ) {
 758          setup_postdata( $post );
 759      }
 760      $wp_scripts        = $original_wp_scripts;
 761      $wp_styles         = $original_wp_styles;
 762      $wp_script_modules = $original_wp_script_modules;
 763  
 764      wp_add_inline_script(
 765          'wp-api-fetch',
 766          sprintf(
 767              'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );',
 768              wp_json_encode( $preload_data, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES )
 769          ),
 770          'after'
 771      );
 772  }
 773  
 774  /**
 775   * Creates an array of theme styles to load into the block editor.
 776   *
 777   * @since 5.8.0
 778   *
 779   * @global array $editor_styles
 780   *
 781   * @return array An array of theme styles for the block editor.
 782   */
 783  function get_block_editor_theme_styles() {
 784      global $editor_styles;
 785  
 786      $styles = array();
 787  
 788      if ( $editor_styles && current_theme_supports( 'editor-styles' ) ) {
 789          foreach ( $editor_styles as $style ) {
 790              if ( preg_match( '~^(https?:)?//~', $style ) ) {
 791                  $response = wp_remote_get( $style );
 792                  if ( ! is_wp_error( $response ) ) {
 793                      $styles[] = array(
 794                          'css'            => wp_remote_retrieve_body( $response ),
 795                          '__unstableType' => 'theme',
 796                          'isGlobalStyles' => false,
 797                      );
 798                  }
 799              } else {
 800                  $file = get_theme_file_path( $style );
 801                  if ( is_file( $file ) ) {
 802                      $styles[] = array(
 803                          'css'            => file_get_contents( $file ),
 804                          'baseURL'        => get_theme_file_uri( $style ),
 805                          '__unstableType' => 'theme',
 806                          'isGlobalStyles' => false,
 807                      );
 808                  }
 809              }
 810          }
 811      }
 812  
 813      return $styles;
 814  }
 815  
 816  /**
 817   * Returns the classic theme supports settings for block editor.
 818   *
 819   * @since 6.2.0
 820   * @since 6.6.0 Add support for 'editor-spacing-sizes' theme support.
 821   *
 822   * @return array The classic theme supports settings.
 823   */
 824  function get_classic_theme_supports_block_editor_settings() {
 825      $theme_settings = array(
 826          'disableCustomColors'    => get_theme_support( 'disable-custom-colors' ),
 827          'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ),
 828          'disableCustomGradients' => get_theme_support( 'disable-custom-gradients' ),
 829          'disableLayoutStyles'    => get_theme_support( 'disable-layout-styles' ),
 830          'enableCustomLineHeight' => get_theme_support( 'custom-line-height' ),
 831          'enableCustomSpacing'    => get_theme_support( 'custom-spacing' ),
 832          'enableCustomUnits'      => get_theme_support( 'custom-units' ),
 833      );
 834  
 835      // Theme settings.
 836      $color_palette = current( (array) get_theme_support( 'editor-color-palette' ) );
 837      if ( false !== $color_palette ) {
 838          $theme_settings['colors'] = $color_palette;
 839      }
 840  
 841      $font_sizes = current( (array) get_theme_support( 'editor-font-sizes' ) );
 842      if ( false !== $font_sizes ) {
 843          $theme_settings['fontSizes'] = $font_sizes;
 844      }
 845  
 846      $gradient_presets = current( (array) get_theme_support( 'editor-gradient-presets' ) );
 847      if ( false !== $gradient_presets ) {
 848          $theme_settings['gradients'] = $gradient_presets;
 849      }
 850  
 851      $spacing_sizes = current( (array) get_theme_support( 'editor-spacing-sizes' ) );
 852      if ( false !== $spacing_sizes ) {
 853          $theme_settings['spacingSizes'] = $spacing_sizes;
 854      }
 855  
 856      return $theme_settings;
 857  }
 858  
 859  /**
 860   * Initialize site preview.
 861   *
 862   * This function sets IFRAME_REQUEST to true if the site preview parameter is set.
 863   *
 864   * @since 6.8.0
 865   */
 866  function wp_initialize_site_preview_hooks() {
 867      if (
 868          ! defined( 'IFRAME_REQUEST' ) &&
 869          isset( $_GET['wp_site_preview'] ) &&
 870          1 === (int) $_GET['wp_site_preview'] &&
 871          current_user_can( 'edit_theme_options' )
 872      ) {
 873          define( 'IFRAME_REQUEST', true );
 874      }
 875  }


Generated : Wed Dec 2 08:20:02 2026 Cross-referenced by PHPXref