[ Index ]

PHP Cross Reference of WordPress

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|array 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|array              $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|array $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      $max_upload_size = wp_max_upload_size();
 157      if ( ! $max_upload_size ) {
 158          $max_upload_size = 0;
 159      }
 160  
 161      /** This filter is documented in wp-admin/includes/media.php */
 162      $image_size_names = apply_filters(
 163          'image_size_names_choose',
 164          array(
 165              'thumbnail' => __( 'Thumbnail' ),
 166              'medium'    => __( 'Medium' ),
 167              'large'     => __( 'Large' ),
 168              'full'      => __( 'Full Size' ),
 169          )
 170      );
 171  
 172      $available_image_sizes = array();
 173      foreach ( $image_size_names as $image_size_slug => $image_size_name ) {
 174          $available_image_sizes[] = array(
 175              'slug' => $image_size_slug,
 176              'name' => $image_size_name,
 177          );
 178      }
 179  
 180      $default_size       = get_option( 'image_default_size', 'large' );
 181      $image_default_size = in_array( $default_size, array_keys( $image_size_names ), true ) ? $default_size : 'large';
 182  
 183      $image_dimensions = array();
 184      $all_sizes        = wp_get_registered_image_subsizes();
 185      foreach ( $available_image_sizes as $size ) {
 186          $key = $size['slug'];
 187          if ( isset( $all_sizes[ $key ] ) ) {
 188              $image_dimensions[ $key ] = $all_sizes[ $key ];
 189          }
 190      }
 191  
 192      // These styles are used if the "no theme styles" options is triggered or on
 193      // themes without their own editor styles.
 194      $default_editor_styles_file = ABSPATH . WPINC . '/css/dist/block-editor/default-editor-styles.css';
 195      if ( file_exists( $default_editor_styles_file ) ) {
 196          $default_editor_styles = array(
 197              array( 'css' => file_get_contents( $default_editor_styles_file ) ),
 198          );
 199      } else {
 200          $default_editor_styles = array();
 201      }
 202  
 203      $editor_settings = array(
 204          'alignWide'                        => get_theme_support( 'align-wide' ),
 205          'allowedBlockTypes'                => true,
 206          'allowedMimeTypes'                 => get_allowed_mime_types(),
 207          'defaultEditorStyles'              => $default_editor_styles,
 208          'blockCategories'                  => get_default_block_categories(),
 209          'disableCustomColors'              => get_theme_support( 'disable-custom-colors' ),
 210          'disableCustomFontSizes'           => get_theme_support( 'disable-custom-font-sizes' ),
 211          'disableCustomGradients'           => get_theme_support( 'disable-custom-gradients' ),
 212          'enableCustomLineHeight'           => get_theme_support( 'custom-line-height' ),
 213          'enableCustomSpacing'              => get_theme_support( 'custom-spacing' ),
 214          'enableCustomUnits'                => get_theme_support( 'custom-units' ),
 215          'isRTL'                            => is_rtl(),
 216          'imageDefaultSize'                 => $image_default_size,
 217          'imageDimensions'                  => $image_dimensions,
 218          'imageEditing'                     => true,
 219          'imageSizes'                       => $available_image_sizes,
 220          'maxUploadFileSize'                => $max_upload_size,
 221          // The following flag is required to enable the new Gallery block format on the mobile apps in 5.9.
 222          '__unstableGalleryWithImageBlocks' => true,
 223      );
 224  
 225      // Theme settings.
 226      $color_palette = current( (array) get_theme_support( 'editor-color-palette' ) );
 227      if ( false !== $color_palette ) {
 228          $editor_settings['colors'] = $color_palette;
 229      }
 230  
 231      $font_sizes = current( (array) get_theme_support( 'editor-font-sizes' ) );
 232      if ( false !== $font_sizes ) {
 233          $editor_settings['fontSizes'] = $font_sizes;
 234      }
 235  
 236      $gradient_presets = current( (array) get_theme_support( 'editor-gradient-presets' ) );
 237      if ( false !== $gradient_presets ) {
 238          $editor_settings['gradients'] = $gradient_presets;
 239      }
 240  
 241      return $editor_settings;
 242  }
 243  
 244  /**
 245   * Returns the block editor settings needed to use the Legacy Widget block which
 246   * is not registered by default.
 247   *
 248   * @since 5.8.0
 249   *
 250   * @return array Settings to be used with get_block_editor_settings().
 251   */
 252  function get_legacy_widget_block_editor_settings() {
 253      $editor_settings = array();
 254  
 255      /**
 256       * Filters the list of widget-type IDs that should **not** be offered by the
 257       * Legacy Widget block.
 258       *
 259       * Returning an empty array will make all widgets available.
 260       *
 261       * @since 5.8.0
 262       *
 263       * @param string[] $widgets An array of excluded widget-type IDs.
 264       */
 265      $editor_settings['widgetTypesToHideFromLegacyWidgetBlock'] = apply_filters(
 266          'widget_types_to_hide_from_legacy_widget_block',
 267          array(
 268              'pages',
 269              'calendar',
 270              'archives',
 271              'media_audio',
 272              'media_image',
 273              'media_gallery',
 274              'media_video',
 275              'search',
 276              'text',
 277              'categories',
 278              'recent-posts',
 279              'recent-comments',
 280              'rss',
 281              'tag_cloud',
 282              'custom_html',
 283              'block',
 284          )
 285      );
 286  
 287      return $editor_settings;
 288  }
 289  
 290  /**
 291   * Returns the contextualized block editor settings for a selected editor context.
 292   *
 293   * @since 5.8.0
 294   *
 295   * @param array                   $custom_settings      Custom settings to use with the given editor type.
 296   * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
 297   *
 298   * @return array The contextualized block editor settings.
 299   */
 300  function get_block_editor_settings( array $custom_settings, $block_editor_context ) {
 301      $editor_settings = array_merge(
 302          get_default_block_editor_settings(),
 303          array(
 304              'allowedBlockTypes' => get_allowed_block_types( $block_editor_context ),
 305              'blockCategories'   => get_block_categories( $block_editor_context ),
 306          ),
 307          $custom_settings
 308      );
 309  
 310      $presets = array(
 311          array(
 312              'css'            => 'variables',
 313              '__unstableType' => 'presets',
 314          ),
 315          array(
 316              'css'            => 'presets',
 317              '__unstableType' => 'presets',
 318          ),
 319      );
 320      foreach ( $presets as $preset_style ) {
 321          $actual_css = wp_get_global_stylesheet( array( $preset_style['css'] ) );
 322          if ( '' !== $actual_css ) {
 323              $preset_style['css']         = $actual_css;
 324              $editor_settings['styles'][] = $preset_style;
 325          }
 326      }
 327  
 328      if ( WP_Theme_JSON_Resolver::theme_has_support() ) {
 329          $block_classes = array(
 330              'css'            => 'styles',
 331              '__unstableType' => 'theme',
 332          );
 333          $actual_css    = wp_get_global_stylesheet( array( $block_classes['css'] ) );
 334          if ( '' !== $actual_css ) {
 335              $block_classes['css']        = $actual_css;
 336              $editor_settings['styles'][] = $block_classes;
 337          }
 338      }
 339  
 340      $editor_settings['__experimentalFeatures'] = wp_get_global_settings();
 341      // These settings may need to be updated based on data coming from theme.json sources.
 342      if ( isset( $editor_settings['__experimentalFeatures']['color']['palette'] ) ) {
 343          $colors_by_origin          = $editor_settings['__experimentalFeatures']['color']['palette'];
 344          $editor_settings['colors'] = isset( $colors_by_origin['custom'] ) ?
 345              $colors_by_origin['custom'] : (
 346                  isset( $colors_by_origin['theme'] ) ?
 347                      $colors_by_origin['theme'] :
 348                      $colors_by_origin['default']
 349              );
 350      }
 351      if ( isset( $editor_settings['__experimentalFeatures']['color']['gradients'] ) ) {
 352          $gradients_by_origin          = $editor_settings['__experimentalFeatures']['color']['gradients'];
 353          $editor_settings['gradients'] = isset( $gradients_by_origin['custom'] ) ?
 354              $gradients_by_origin['custom'] : (
 355                  isset( $gradients_by_origin['theme'] ) ?
 356                      $gradients_by_origin['theme'] :
 357                      $gradients_by_origin['default']
 358              );
 359      }
 360      if ( isset( $editor_settings['__experimentalFeatures']['typography']['fontSizes'] ) ) {
 361          $font_sizes_by_origin         = $editor_settings['__experimentalFeatures']['typography']['fontSizes'];
 362          $editor_settings['fontSizes'] = isset( $font_sizes_by_origin['custom'] ) ?
 363              $font_sizes_by_origin['custom'] : (
 364                  isset( $font_sizes_by_origin['theme'] ) ?
 365                      $font_sizes_by_origin['theme'] :
 366                      $font_sizes_by_origin['default']
 367              );
 368      }
 369      if ( isset( $editor_settings['__experimentalFeatures']['color']['custom'] ) ) {
 370          $editor_settings['disableCustomColors'] = ! $editor_settings['__experimentalFeatures']['color']['custom'];
 371          unset( $editor_settings['__experimentalFeatures']['color']['custom'] );
 372      }
 373      if ( isset( $editor_settings['__experimentalFeatures']['color']['customGradient'] ) ) {
 374          $editor_settings['disableCustomGradients'] = ! $editor_settings['__experimentalFeatures']['color']['customGradient'];
 375          unset( $editor_settings['__experimentalFeatures']['color']['customGradient'] );
 376      }
 377      if ( isset( $editor_settings['__experimentalFeatures']['typography']['customFontSize'] ) ) {
 378          $editor_settings['disableCustomFontSizes'] = ! $editor_settings['__experimentalFeatures']['typography']['customFontSize'];
 379          unset( $editor_settings['__experimentalFeatures']['typography']['customFontSize'] );
 380      }
 381      if ( isset( $editor_settings['__experimentalFeatures']['typography']['lineHeight'] ) ) {
 382          $editor_settings['enableCustomLineHeight'] = $editor_settings['__experimentalFeatures']['typography']['lineHeight'];
 383          unset( $editor_settings['__experimentalFeatures']['typography']['lineHeight'] );
 384      }
 385      if ( isset( $editor_settings['__experimentalFeatures']['spacing']['units'] ) ) {
 386          $editor_settings['enableCustomUnits'] = $editor_settings['__experimentalFeatures']['spacing']['units'];
 387          unset( $editor_settings['__experimentalFeatures']['spacing']['units'] );
 388      }
 389      if ( isset( $editor_settings['__experimentalFeatures']['spacing']['padding'] ) ) {
 390          $editor_settings['enableCustomSpacing'] = $editor_settings['__experimentalFeatures']['spacing']['padding'];
 391          unset( $editor_settings['__experimentalFeatures']['spacing']['padding'] );
 392      }
 393  
 394      /**
 395       * Filters the settings to pass to the block editor for all editor type.
 396       *
 397       * @since 5.8.0
 398       *
 399       * @param array                   $editor_settings      Default editor settings.
 400       * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
 401       */
 402      $editor_settings = apply_filters( 'block_editor_settings_all', $editor_settings, $block_editor_context );
 403  
 404      if ( ! empty( $block_editor_context->post ) ) {
 405          $post = $block_editor_context->post;
 406  
 407          /**
 408           * Filters the settings to pass to the block editor.
 409           *
 410           * @since 5.0.0
 411           * @deprecated 5.8.0 Use the {@see 'block_editor_settings_all'} filter instead.
 412           *
 413           * @param array   $editor_settings Default editor settings.
 414           * @param WP_Post $post            Post being edited.
 415           */
 416          $editor_settings = apply_filters_deprecated( 'block_editor_settings', array( $editor_settings, $post ), '5.8.0', 'block_editor_settings_all' );
 417      }
 418  
 419      return $editor_settings;
 420  }
 421  
 422  /**
 423   * Preloads common data used with the block editor by specifying an array of
 424   * REST API paths that will be preloaded for a given block editor context.
 425   *
 426   * @since 5.8.0
 427   *
 428   * @global WP_Post $post Global post object.
 429   *
 430   * @param string[]                $preload_paths        List of paths to preload.
 431   * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
 432   *
 433   * @return void
 434   */
 435  function block_editor_rest_api_preload( array $preload_paths, $block_editor_context ) {
 436      global $post;
 437  
 438      /**
 439       * Filters the array of REST API paths that will be used to preloaded common data for the block editor.
 440       *
 441       * @since 5.8.0
 442       *
 443       * @param string[]                $preload_paths        Array of paths to preload.
 444       * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
 445       */
 446      $preload_paths = apply_filters( 'block_editor_rest_api_preload_paths', $preload_paths, $block_editor_context );
 447  
 448      if ( ! empty( $block_editor_context->post ) ) {
 449          $selected_post = $block_editor_context->post;
 450  
 451          /**
 452           * Filters the array of paths that will be preloaded.
 453           *
 454           * Preload common data by specifying an array of REST API paths that will be preloaded.
 455           *
 456           * @since 5.0.0
 457           * @deprecated 5.8.0 Use the {@see 'block_editor_rest_api_preload_paths'} filter instead.
 458           *
 459           * @param string[] $preload_paths Array of paths to preload.
 460           * @param WP_Post  $selected_post Post being edited.
 461           */
 462          $preload_paths = apply_filters_deprecated( 'block_editor_preload_paths', array( $preload_paths, $selected_post ), '5.8.0', 'block_editor_rest_api_preload_paths' );
 463      }
 464  
 465      if ( empty( $preload_paths ) ) {
 466          return;
 467      }
 468  
 469      /*
 470       * Ensure the global $post remains the same after API data is preloaded.
 471       * Because API preloading can call the_content and other filters, plugins
 472       * can unexpectedly modify $post.
 473       */
 474      $backup_global_post = ! empty( $post ) ? clone $post : $post;
 475  
 476      foreach ( $preload_paths as &$path ) {
 477          if ( is_string( $path ) && ! str_starts_with( $path, '/' ) ) {
 478              $path = '/' . $path;
 479              continue;
 480          }
 481  
 482          if ( is_array( $path ) && is_string( $path[0] ) && ! str_starts_with( $path[0], '/' ) ) {
 483                  $path[0] = '/' . $path[0];
 484          }
 485      }
 486  
 487      unset( $path );
 488  
 489      $preload_data = array_reduce(
 490          $preload_paths,
 491          'rest_preload_api_request',
 492          array()
 493      );
 494  
 495      // Restore the global $post as it was before API preloading.
 496      $post = $backup_global_post;
 497  
 498      wp_add_inline_script(
 499          'wp-api-fetch',
 500          sprintf(
 501              'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );',
 502              wp_json_encode( $preload_data )
 503          ),
 504          'after'
 505      );
 506  }
 507  
 508  /**
 509   * Creates an array of theme styles to load into the block editor.
 510   *
 511   * @since 5.8.0
 512   *
 513   * @global array $editor_styles
 514   *
 515   * @return array An array of theme styles for the block editor.
 516   */
 517  function get_block_editor_theme_styles() {
 518      global $editor_styles;
 519  
 520      $styles = array();
 521  
 522      if ( $editor_styles && current_theme_supports( 'editor-styles' ) ) {
 523          foreach ( $editor_styles as $style ) {
 524              if ( preg_match( '~^(https?:)?//~', $style ) ) {
 525                  $response = wp_remote_get( $style );
 526                  if ( ! is_wp_error( $response ) ) {
 527                      $styles[] = array(
 528                          'css'            => wp_remote_retrieve_body( $response ),
 529                          '__unstableType' => 'theme',
 530                      );
 531                  }
 532              } else {
 533                  $file = get_theme_file_path( $style );
 534                  if ( is_file( $file ) ) {
 535                      $styles[] = array(
 536                          'css'            => file_get_contents( $file ),
 537                          'baseURL'        => get_theme_file_uri( $style ),
 538                          '__unstableType' => 'theme',
 539                      );
 540                  }
 541              }
 542          }
 543      }
 544  
 545      return $styles;
 546  }


Generated: Wed Jan 26 01:00:03 2022 Cross-referenced by PHPXref 0.7.1