[ 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   *
  15   * @return array[] Array of categories for block types.
  16   */
  17  function get_default_block_categories() {
  18      return array(
  19          array(
  20              'slug'  => 'text',
  21              'title' => _x( 'Text', 'block category' ),
  22              'icon'  => null,
  23          ),
  24          array(
  25              'slug'  => 'media',
  26              'title' => _x( 'Media', 'block category' ),
  27              'icon'  => null,
  28          ),
  29          array(
  30              'slug'  => 'design',
  31              'title' => _x( 'Design', 'block category' ),
  32              'icon'  => null,
  33          ),
  34          array(
  35              'slug'  => 'widgets',
  36              'title' => _x( 'Widgets', 'block category' ),
  37              'icon'  => null,
  38          ),
  39          array(
  40              'slug'  => 'theme',
  41              'title' => _x( 'Theme', 'block category' ),
  42              'icon'  => null,
  43          ),
  44          array(
  45              'slug'  => 'embed',
  46              'title' => _x( 'Embeds', 'block category' ),
  47              'icon'  => null,
  48          ),
  49          array(
  50              'slug'  => 'reusable',
  51              'title' => _x( 'Reusable Blocks', 'block category' ),
  52              'icon'  => null,
  53          ),
  54      );
  55  }
  56  
  57  /**
  58   * Returns all the categories for block types that will be shown in the block editor.
  59   *
  60   * @since 5.0.0
  61   * @since 5.8.0 It is possible to pass the block editor context as param.
  62   *
  63   * @param WP_Post|WP_Block_Editor_Context $post_or_block_editor_context The current post object or
  64   *                                                                      the block editor context.
  65   *
  66   * @return array[] Array of categories for block types.
  67   */
  68  function get_block_categories( $post_or_block_editor_context ) {
  69      $block_categories     = get_default_block_categories();
  70      $block_editor_context = $post_or_block_editor_context instanceof WP_Post ?
  71          new WP_Block_Editor_Context(
  72              array(
  73                  'post' => $post_or_block_editor_context,
  74              )
  75          ) : $post_or_block_editor_context;
  76  
  77      /**
  78       * Filters the default array of categories for block types.
  79       *
  80       * @since 5.8.0
  81       *
  82       * @param array[]                 $block_categories     Array of categories for block types.
  83       * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
  84       */
  85      $block_categories = apply_filters( 'block_categories_all', $block_categories, $block_editor_context );
  86  
  87      if ( ! empty( $block_editor_context->post ) ) {
  88          $post = $block_editor_context->post;
  89  
  90          /**
  91           * Filters the default array of categories for block types.
  92           *
  93           * @since 5.0.0
  94           * @deprecated 5.8.0 Use the {@see 'block_categories_all'} filter instead.
  95           *
  96           * @param array[] $block_categories Array of categories for block types.
  97           * @param WP_Post $post             Post being loaded.
  98           */
  99          $block_categories = apply_filters_deprecated( 'block_categories', array( $block_categories, $post ), '5.8.0', 'block_categories_all' );
 100      }
 101  
 102      return $block_categories;
 103  }
 104  
 105  /**
 106   * Gets the list of allowed block types to use in the block editor.
 107   *
 108   * @since 5.8.0
 109   *
 110   * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
 111   *
 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 = in_array( $default_size, array_keys( $image_size_names ), true ) ? $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      // These styles are used if the "no theme styles" options is triggered or on
 198      // themes without their own editor styles.
 199      $default_editor_styles_file = ABSPATH . WPINC . '/css/dist/block-editor/default-editor-styles.css';
 200  
 201      static $default_editor_styles_file_contents = false;
 202      if ( ! $default_editor_styles_file_contents && file_exists( $default_editor_styles_file ) ) {
 203          $default_editor_styles_file_contents = file_get_contents( $default_editor_styles_file );
 204      }
 205  
 206      $default_editor_styles = array();
 207      if ( $default_editor_styles_file_contents ) {
 208          $default_editor_styles = array(
 209              array( 'css' => $default_editor_styles_file_contents ),
 210          );
 211      }
 212  
 213      $editor_settings = array(
 214          'alignWide'                        => get_theme_support( 'align-wide' ),
 215          'allowedBlockTypes'                => true,
 216          'allowedMimeTypes'                 => get_allowed_mime_types(),
 217          'defaultEditorStyles'              => $default_editor_styles,
 218          'blockCategories'                  => get_default_block_categories(),
 219          'isRTL'                            => is_rtl(),
 220          'imageDefaultSize'                 => $image_default_size,
 221          'imageDimensions'                  => $image_dimensions,
 222          'imageEditing'                     => true,
 223          'imageSizes'                       => $available_image_sizes,
 224          'maxUploadFileSize'                => $max_upload_size,
 225          // The following flag is required to enable the new Gallery block format on the mobile apps in 5.9.
 226          '__unstableGalleryWithImageBlocks' => true,
 227      );
 228  
 229      $theme_settings = get_classic_theme_supports_block_editor_settings();
 230      foreach ( $theme_settings as $key => $value ) {
 231          $editor_settings[ $key ] = $value;
 232      }
 233  
 234      return $editor_settings;
 235  }
 236  
 237  /**
 238   * Returns the block editor settings needed to use the Legacy Widget block which
 239   * is not registered by default.
 240   *
 241   * @since 5.8.0
 242   *
 243   * @return array Settings to be used with get_block_editor_settings().
 244   */
 245  function get_legacy_widget_block_editor_settings() {
 246      $editor_settings = array();
 247  
 248      /**
 249       * Filters the list of widget-type IDs that should **not** be offered by the
 250       * Legacy Widget block.
 251       *
 252       * Returning an empty array will make all widgets available.
 253       *
 254       * @since 5.8.0
 255       *
 256       * @param string[] $widgets An array of excluded widget-type IDs.
 257       */
 258      $editor_settings['widgetTypesToHideFromLegacyWidgetBlock'] = apply_filters(
 259          'widget_types_to_hide_from_legacy_widget_block',
 260          array(
 261              'pages',
 262              'calendar',
 263              'archives',
 264              'media_audio',
 265              'media_image',
 266              'media_gallery',
 267              'media_video',
 268              'search',
 269              'text',
 270              'categories',
 271              'recent-posts',
 272              'recent-comments',
 273              'rss',
 274              'tag_cloud',
 275              'custom_html',
 276              'block',
 277          )
 278      );
 279  
 280      return $editor_settings;
 281  }
 282  
 283  /**
 284   * Collect the block editor assets that need to be loaded into the editor's iframe.
 285   *
 286   * @since 6.0.0
 287   * @access private
 288   *
 289   * @global string $pagenow The filename of the current screen.
 290   *
 291   * @return array {
 292   *     The block editor assets.
 293   *
 294   *     @type string|false $styles  String containing the HTML for styles.
 295   *     @type string|false $scripts String containing the HTML for scripts.
 296   * }
 297   */
 298  function _wp_get_iframed_editor_assets() {
 299      global $pagenow;
 300  
 301      $script_handles = array();
 302      $style_handles  = array(
 303          'wp-block-editor',
 304          'wp-block-library',
 305          'wp-edit-blocks',
 306      );
 307  
 308      if ( current_theme_supports( 'wp-block-styles' ) ) {
 309          $style_handles[] = 'wp-block-library-theme';
 310      }
 311  
 312      if ( 'widgets.php' === $pagenow || 'customize.php' === $pagenow ) {
 313          $style_handles[] = 'wp-widgets';
 314          $style_handles[] = 'wp-edit-widgets';
 315      }
 316  
 317      $block_registry = WP_Block_Type_Registry::get_instance();
 318  
 319      foreach ( $block_registry->get_all_registered() as $block_type ) {
 320          $style_handles = array_merge(
 321              $style_handles,
 322              $block_type->style_handles,
 323              $block_type->editor_style_handles
 324          );
 325  
 326          $script_handles = array_merge(
 327              $script_handles,
 328              $block_type->script_handles
 329          );
 330      }
 331  
 332      $style_handles = array_unique( $style_handles );
 333      $done          = wp_styles()->done;
 334  
 335      ob_start();
 336  
 337      // We do not need reset styles for the iframed editor.
 338      wp_styles()->done = array( 'wp-reset-editor-styles' );
 339      wp_styles()->do_items( $style_handles );
 340      wp_styles()->done = $done;
 341  
 342      $styles = ob_get_clean();
 343  
 344      $script_handles = array_unique( $script_handles );
 345      $done           = wp_scripts()->done;
 346  
 347      ob_start();
 348  
 349      wp_scripts()->done = array();
 350      wp_scripts()->do_items( $script_handles );
 351      wp_scripts()->done = $done;
 352  
 353      $scripts = ob_get_clean();
 354  
 355      return array(
 356          'styles'  => $styles,
 357          'scripts' => $scripts,
 358      );
 359  }
 360  
 361  /**
 362   * Returns the contextualized block editor settings for a selected editor context.
 363   *
 364   * @since 5.8.0
 365   *
 366   * @param array                   $custom_settings      Custom settings to use with the given editor type.
 367   * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
 368   *
 369   * @return array The contextualized block editor settings.
 370   */
 371  function get_block_editor_settings( array $custom_settings, $block_editor_context ) {
 372      $editor_settings = array_merge(
 373          get_default_block_editor_settings(),
 374          array(
 375              'allowedBlockTypes' => get_allowed_block_types( $block_editor_context ),
 376              'blockCategories'   => get_block_categories( $block_editor_context ),
 377          ),
 378          $custom_settings
 379      );
 380  
 381      $global_styles = array();
 382      $presets       = array(
 383          array(
 384              'css'            => 'variables',
 385              '__unstableType' => 'presets',
 386              'isGlobalStyles' => true,
 387          ),
 388          array(
 389              'css'            => 'presets',
 390              '__unstableType' => 'presets',
 391              'isGlobalStyles' => true,
 392          ),
 393      );
 394      foreach ( $presets as $preset_style ) {
 395          $actual_css = wp_get_global_stylesheet( array( $preset_style['css'] ) );
 396          if ( '' !== $actual_css ) {
 397              $preset_style['css'] = $actual_css;
 398              $global_styles[]     = $preset_style;
 399          }
 400      }
 401  
 402      if ( wp_theme_has_theme_json() ) {
 403          $block_classes = array(
 404              'css'            => 'styles',
 405              '__unstableType' => 'theme',
 406              'isGlobalStyles' => true,
 407          );
 408          $actual_css    = wp_get_global_stylesheet( array( $block_classes['css'] ) );
 409          if ( '' !== $actual_css ) {
 410              $block_classes['css'] = $actual_css;
 411              $global_styles[]      = $block_classes;
 412          }
 413      } else {
 414          // If there is no `theme.json` file, ensure base layout styles are still available.
 415          $block_classes = array(
 416              'css'            => 'base-layout-styles',
 417              '__unstableType' => 'base-layout',
 418              'isGlobalStyles' => true,
 419          );
 420          $actual_css    = wp_get_global_stylesheet( array( $block_classes['css'] ) );
 421          if ( '' !== $actual_css ) {
 422              $block_classes['css'] = $actual_css;
 423              $global_styles[]      = $block_classes;
 424          }
 425      }
 426  
 427      $editor_settings['styles'] = array_merge( $global_styles, get_block_editor_theme_styles() );
 428  
 429      $editor_settings['__experimentalFeatures'] = wp_get_global_settings();
 430      // These settings may need to be updated based on data coming from theme.json sources.
 431      if ( isset( $editor_settings['__experimentalFeatures']['color']['palette'] ) ) {
 432          $colors_by_origin          = $editor_settings['__experimentalFeatures']['color']['palette'];
 433          $editor_settings['colors'] = isset( $colors_by_origin['custom'] ) ?
 434              $colors_by_origin['custom'] : (
 435                  isset( $colors_by_origin['theme'] ) ?
 436                      $colors_by_origin['theme'] :
 437                      $colors_by_origin['default']
 438              );
 439      }
 440      if ( isset( $editor_settings['__experimentalFeatures']['color']['gradients'] ) ) {
 441          $gradients_by_origin          = $editor_settings['__experimentalFeatures']['color']['gradients'];
 442          $editor_settings['gradients'] = isset( $gradients_by_origin['custom'] ) ?
 443              $gradients_by_origin['custom'] : (
 444                  isset( $gradients_by_origin['theme'] ) ?
 445                      $gradients_by_origin['theme'] :
 446                      $gradients_by_origin['default']
 447              );
 448      }
 449      if ( isset( $editor_settings['__experimentalFeatures']['typography']['fontSizes'] ) ) {
 450          $font_sizes_by_origin         = $editor_settings['__experimentalFeatures']['typography']['fontSizes'];
 451          $editor_settings['fontSizes'] = isset( $font_sizes_by_origin['custom'] ) ?
 452              $font_sizes_by_origin['custom'] : (
 453                  isset( $font_sizes_by_origin['theme'] ) ?
 454                      $font_sizes_by_origin['theme'] :
 455                      $font_sizes_by_origin['default']
 456              );
 457      }
 458      if ( isset( $editor_settings['__experimentalFeatures']['color']['custom'] ) ) {
 459          $editor_settings['disableCustomColors'] = ! $editor_settings['__experimentalFeatures']['color']['custom'];
 460          unset( $editor_settings['__experimentalFeatures']['color']['custom'] );
 461      }
 462      if ( isset( $editor_settings['__experimentalFeatures']['color']['customGradient'] ) ) {
 463          $editor_settings['disableCustomGradients'] = ! $editor_settings['__experimentalFeatures']['color']['customGradient'];
 464          unset( $editor_settings['__experimentalFeatures']['color']['customGradient'] );
 465      }
 466      if ( isset( $editor_settings['__experimentalFeatures']['typography']['customFontSize'] ) ) {
 467          $editor_settings['disableCustomFontSizes'] = ! $editor_settings['__experimentalFeatures']['typography']['customFontSize'];
 468          unset( $editor_settings['__experimentalFeatures']['typography']['customFontSize'] );
 469      }
 470      if ( isset( $editor_settings['__experimentalFeatures']['typography']['lineHeight'] ) ) {
 471          $editor_settings['enableCustomLineHeight'] = $editor_settings['__experimentalFeatures']['typography']['lineHeight'];
 472          unset( $editor_settings['__experimentalFeatures']['typography']['lineHeight'] );
 473      }
 474      if ( isset( $editor_settings['__experimentalFeatures']['spacing']['units'] ) ) {
 475          $editor_settings['enableCustomUnits'] = $editor_settings['__experimentalFeatures']['spacing']['units'];
 476          unset( $editor_settings['__experimentalFeatures']['spacing']['units'] );
 477      }
 478      if ( isset( $editor_settings['__experimentalFeatures']['spacing']['padding'] ) ) {
 479          $editor_settings['enableCustomSpacing'] = $editor_settings['__experimentalFeatures']['spacing']['padding'];
 480          unset( $editor_settings['__experimentalFeatures']['spacing']['padding'] );
 481      }
 482      if ( isset( $editor_settings['__experimentalFeatures']['spacing']['customSpacingSize'] ) ) {
 483          $editor_settings['disableCustomSpacingSizes'] = ! $editor_settings['__experimentalFeatures']['spacing']['customSpacingSize'];
 484          unset( $editor_settings['__experimentalFeatures']['spacing']['customSpacingSize'] );
 485      }
 486  
 487      if ( isset( $editor_settings['__experimentalFeatures']['spacing']['spacingSizes'] ) ) {
 488          $spacing_sizes_by_origin         = $editor_settings['__experimentalFeatures']['spacing']['spacingSizes'];
 489          $editor_settings['spacingSizes'] = isset( $spacing_sizes_by_origin['custom'] ) ?
 490              $spacing_sizes_by_origin['custom'] : (
 491                  isset( $spacing_sizes_by_origin['theme'] ) ?
 492                      $spacing_sizes_by_origin['theme'] :
 493                      $spacing_sizes_by_origin['default']
 494              );
 495      }
 496  
 497      $editor_settings['__unstableResolvedAssets']         = _wp_get_iframed_editor_assets();
 498      $editor_settings['__unstableIsBlockBasedTheme']      = wp_is_block_theme();
 499      $editor_settings['localAutosaveInterval']            = 15;
 500      $editor_settings['disableLayoutStyles']              = current_theme_supports( 'disable-layout-styles' );
 501      $editor_settings['__experimentalDiscussionSettings'] = array(
 502          'commentOrder'         => get_option( 'comment_order' ),
 503          'commentsPerPage'      => get_option( 'comments_per_page' ),
 504          'defaultCommentsPage'  => get_option( 'default_comments_page' ),
 505          'pageComments'         => get_option( 'page_comments' ),
 506          'threadComments'       => get_option( 'thread_comments' ),
 507          'threadCommentsDepth'  => get_option( 'thread_comments_depth' ),
 508          'defaultCommentStatus' => get_option( 'default_comment_status' ),
 509          'avatarURL'            => get_avatar_url(
 510              '',
 511              array(
 512                  'size'          => 96,
 513                  'force_default' => true,
 514                  'default'       => get_option( 'avatar_default' ),
 515              )
 516          ),
 517      );
 518  
 519      /**
 520       * Filters the settings to pass to the block editor for all editor type.
 521       *
 522       * @since 5.8.0
 523       *
 524       * @param array                   $editor_settings      Default editor settings.
 525       * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
 526       */
 527      $editor_settings = apply_filters( 'block_editor_settings_all', $editor_settings, $block_editor_context );
 528  
 529      if ( ! empty( $block_editor_context->post ) ) {
 530          $post = $block_editor_context->post;
 531  
 532          /**
 533           * Filters the settings to pass to the block editor.
 534           *
 535           * @since 5.0.0
 536           * @deprecated 5.8.0 Use the {@see 'block_editor_settings_all'} filter instead.
 537           *
 538           * @param array   $editor_settings Default editor settings.
 539           * @param WP_Post $post            Post being edited.
 540           */
 541          $editor_settings = apply_filters_deprecated( 'block_editor_settings', array( $editor_settings, $post ), '5.8.0', 'block_editor_settings_all' );
 542      }
 543  
 544      return $editor_settings;
 545  }
 546  
 547  /**
 548   * Preloads common data used with the block editor by specifying an array of
 549   * REST API paths that will be preloaded for a given block editor context.
 550   *
 551   * @since 5.8.0
 552   *
 553   * @global WP_Post    $post       Global post object.
 554   * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
 555   * @global WP_Styles  $wp_styles  The WP_Styles object for printing styles.
 556   *
 557   * @param (string|string[])[]     $preload_paths        List of paths to preload.
 558   * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
 559   */
 560  function block_editor_rest_api_preload( array $preload_paths, $block_editor_context ) {
 561      global $post, $wp_scripts, $wp_styles;
 562  
 563      /**
 564       * Filters the array of REST API paths that will be used to preloaded common data for the block editor.
 565       *
 566       * @since 5.8.0
 567       *
 568       * @param (string|string[])[]     $preload_paths        Array of paths to preload.
 569       * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
 570       */
 571      $preload_paths = apply_filters( 'block_editor_rest_api_preload_paths', $preload_paths, $block_editor_context );
 572  
 573      if ( ! empty( $block_editor_context->post ) ) {
 574          $selected_post = $block_editor_context->post;
 575  
 576          /**
 577           * Filters the array of paths that will be preloaded.
 578           *
 579           * Preload common data by specifying an array of REST API paths that will be preloaded.
 580           *
 581           * @since 5.0.0
 582           * @deprecated 5.8.0 Use the {@see 'block_editor_rest_api_preload_paths'} filter instead.
 583           *
 584           * @param (string|string[])[] $preload_paths Array of paths to preload.
 585           * @param WP_Post             $selected_post Post being edited.
 586           */
 587          $preload_paths = apply_filters_deprecated( 'block_editor_preload_paths', array( $preload_paths, $selected_post ), '5.8.0', 'block_editor_rest_api_preload_paths' );
 588      }
 589  
 590      if ( empty( $preload_paths ) ) {
 591          return;
 592      }
 593  
 594      /*
 595       * Ensure the global $post, $wp_scripts, and $wp_styles remain the same after
 596       * API data is preloaded.
 597       * Because API preloading can call the_content and other filters, plugins
 598       * can unexpectedly modify the global $post or enqueue assets which are not
 599       * intended for the block editor.
 600       */
 601      $backup_global_post = ! empty( $post ) ? clone $post : $post;
 602      $backup_wp_scripts  = ! empty( $wp_scripts ) ? clone $wp_scripts : $wp_scripts;
 603      $backup_wp_styles   = ! empty( $wp_styles ) ? clone $wp_styles : $wp_styles;
 604  
 605      foreach ( $preload_paths as &$path ) {
 606          if ( is_string( $path ) && ! str_starts_with( $path, '/' ) ) {
 607              $path = '/' . $path;
 608              continue;
 609          }
 610  
 611          if ( is_array( $path ) && is_string( $path[0] ) && ! str_starts_with( $path[0], '/' ) ) {
 612              $path[0] = '/' . $path[0];
 613          }
 614      }
 615  
 616      unset( $path );
 617  
 618      $preload_data = array_reduce(
 619          $preload_paths,
 620          'rest_preload_api_request',
 621          array()
 622      );
 623  
 624      // Restore the global $post, $wp_scripts, and $wp_styles as they were before API preloading.
 625      $post       = $backup_global_post;
 626      $wp_scripts = $backup_wp_scripts;
 627      $wp_styles  = $backup_wp_styles;
 628  
 629      wp_add_inline_script(
 630          'wp-api-fetch',
 631          sprintf(
 632              'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );',
 633              wp_json_encode( $preload_data )
 634          ),
 635          'after'
 636      );
 637  }
 638  
 639  /**
 640   * Creates an array of theme styles to load into the block editor.
 641   *
 642   * @since 5.8.0
 643   *
 644   * @global array $editor_styles
 645   *
 646   * @return array An array of theme styles for the block editor.
 647   */
 648  function get_block_editor_theme_styles() {
 649      global $editor_styles;
 650  
 651      $styles = array();
 652  
 653      if ( $editor_styles && current_theme_supports( 'editor-styles' ) ) {
 654          foreach ( $editor_styles as $style ) {
 655              if ( preg_match( '~^(https?:)?//~', $style ) ) {
 656                  $response = wp_remote_get( $style );
 657                  if ( ! is_wp_error( $response ) ) {
 658                      $styles[] = array(
 659                          'css'            => wp_remote_retrieve_body( $response ),
 660                          '__unstableType' => 'theme',
 661                          'isGlobalStyles' => false,
 662                      );
 663                  }
 664              } else {
 665                  $file = get_theme_file_path( $style );
 666                  if ( is_file( $file ) ) {
 667                      $styles[] = array(
 668                          'css'            => file_get_contents( $file ),
 669                          'baseURL'        => get_theme_file_uri( $style ),
 670                          '__unstableType' => 'theme',
 671                          'isGlobalStyles' => false,
 672                      );
 673                  }
 674              }
 675          }
 676      }
 677  
 678      return $styles;
 679  }
 680  
 681  /**
 682   * Returns the classic theme supports settings for block editor.
 683   *
 684   * @since 6.2.0
 685   *
 686   * @return array The classic theme supports settings.
 687   */
 688  function get_classic_theme_supports_block_editor_settings() {
 689      $theme_settings = array(
 690          'disableCustomColors'    => get_theme_support( 'disable-custom-colors' ),
 691          'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ),
 692          'disableCustomGradients' => get_theme_support( 'disable-custom-gradients' ),
 693          'disableLayoutStyles'    => get_theme_support( 'disable-layout-styles' ),
 694          'enableCustomLineHeight' => get_theme_support( 'custom-line-height' ),
 695          'enableCustomSpacing'    => get_theme_support( 'custom-spacing' ),
 696          'enableCustomUnits'      => get_theme_support( 'custom-units' ),
 697      );
 698  
 699      // Theme settings.
 700      $color_palette = current( (array) get_theme_support( 'editor-color-palette' ) );
 701      if ( false !== $color_palette ) {
 702          $theme_settings['colors'] = $color_palette;
 703      }
 704  
 705      $font_sizes = current( (array) get_theme_support( 'editor-font-sizes' ) );
 706      if ( false !== $font_sizes ) {
 707          $theme_settings['fontSizes'] = $font_sizes;
 708      }
 709  
 710      $gradient_presets = current( (array) get_theme_support( 'editor-gradient-presets' ) );
 711      if ( false !== $gradient_presets ) {
 712          $theme_settings['gradients'] = $gradient_presets;
 713      }
 714  
 715      return $theme_settings;
 716  }


Generated : Mon Jan 30 08:20:01 2023 Cross-referenced by PHPXref