[ 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      if ( ! empty( $block_editor_context->post ) ) {
  87          $post = $block_editor_context->post;
  88  
  89          /**
  90           * Filters the default array of categories for block types.
  91           *
  92           * @since 5.0.0
  93           * @deprecated 5.8.0 The hook transitioned to support also screens that don't contain the $post instance.
  94           *
  95           * @param array[] $block_categories Array of categories for block types.
  96           * @param WP_Post $post             Post being loaded.
  97           */
  98          $block_categories = apply_filters_deprecated( 'block_categories', array( $block_categories, $post ), '5.8.0', 'block_categories_all' );
  99      }
 100  
 101      return $block_categories;
 102  }
 103  
 104  /**
 105   * Gets the list of allowed block types to use in the block editor.
 106   *
 107   * @since 5.8.0
 108   *
 109   * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
 110   *
 111   * @return bool|array Array of block type slugs, or boolean to enable/disable all.
 112   */
 113  function get_allowed_block_types( $block_editor_context ) {
 114      $allowed_block_types = true;
 115  
 116      /**
 117       * Filters the allowed block types for all editor types, defaulting to `true`
 118       * (all registered block types supported).
 119       *
 120       *
 121       * @since 5.8.0
 122       *
 123       * @param bool|array              $allowed_block_types  Array of block type slugs, or
 124       *                                                      boolean to enable/disable all.
 125       * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
 126       */
 127      $allowed_block_types = apply_filters( 'allowed_block_types_all', $allowed_block_types, $block_editor_context );
 128      if ( ! empty( $block_editor_context->post ) ) {
 129          $post = $block_editor_context->post;
 130  
 131          /**
 132           * Filters the allowed block types for the editor, defaulting to true (all
 133           * block types supported).
 134           *
 135           * @since 5.0.0
 136           * @deprecated 5.8.0 The hook transitioned to support also screens that don't contain $post instance.
 137           *
 138           * @param bool|array $allowed_block_types Array of block type slugs, or
 139           *                                        boolean to enable/disable all.
 140           * @param WP_Post    $post                The post resource data.
 141           */
 142          $allowed_block_types = apply_filters_deprecated( 'allowed_block_types', array( $allowed_block_types, $post ), '5.8.0', 'allowed_block_types_all' );
 143      }
 144  
 145      return $allowed_block_types;
 146  }
 147  
 148  /**
 149   * Returns the default block editor settings.
 150   *
 151   * @since 5.8.0
 152   *
 153   * @return array The default block editor settings.
 154   */
 155  function get_default_block_editor_settings() {
 156      // Media settings.
 157      $max_upload_size = wp_max_upload_size();
 158      if ( ! $max_upload_size ) {
 159          $max_upload_size = 0;
 160      }
 161  
 162      /** This filter is documented in wp-admin/includes/media.php */
 163      $image_size_names = apply_filters(
 164          'image_size_names_choose',
 165          array(
 166              'thumbnail' => __( 'Thumbnail' ),
 167              'medium'    => __( 'Medium' ),
 168              'large'     => __( 'Large' ),
 169              'full'      => __( 'Full Size' ),
 170          )
 171      );
 172  
 173      $available_image_sizes = array();
 174      foreach ( $image_size_names as $image_size_slug => $image_size_name ) {
 175          $available_image_sizes[] = array(
 176              'slug' => $image_size_slug,
 177              'name' => $image_size_name,
 178          );
 179      }
 180  
 181      $default_size       = get_option( 'image_default_size', 'large' );
 182      $image_default_size = in_array( $default_size, array_keys( $image_size_names ), true ) ? $default_size : 'large';
 183  
 184      $image_dimensions = array();
 185      $all_sizes        = wp_get_registered_image_subsizes();
 186      foreach ( $available_image_sizes as $size ) {
 187          $key = $size['slug'];
 188          if ( isset( $all_sizes[ $key ] ) ) {
 189              $image_dimensions[ $key ] = $all_sizes[ $key ];
 190          }
 191      }
 192  
 193      $editor_settings = array(
 194          'alignWide'              => get_theme_support( 'align-wide' ),
 195          'allowedBlockTypes'      => true,
 196          'allowedMimeTypes'       => get_allowed_mime_types(),
 197          'blockCategories'        => get_default_block_categories(),
 198          'disableCustomColors'    => get_theme_support( 'disable-custom-colors' ),
 199          'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ),
 200          'disableCustomGradients' => get_theme_support( 'disable-custom-gradients' ),
 201          'enableCustomLineHeight' => get_theme_support( 'custom-line-height' ),
 202          'enableCustomSpacing'    => get_theme_support( 'custom-spacing' ),
 203          'enableCustomUnits'      => get_theme_support( 'custom-units' ),
 204          'isRTL'                  => is_rtl(),
 205          'imageDefaultSize'       => $image_default_size,
 206          'imageDimensions'        => $image_dimensions,
 207          'imageEditing'           => true,
 208          'imageSizes'             => $available_image_sizes,
 209          'maxUploadFileSize'      => $max_upload_size,
 210      );
 211  
 212      // Theme settings.
 213      $color_palette = current( (array) get_theme_support( 'editor-color-palette' ) );
 214      if ( false !== $color_palette ) {
 215          $editor_settings['colors'] = $color_palette;
 216      }
 217  
 218      $font_sizes = current( (array) get_theme_support( 'editor-font-sizes' ) );
 219      if ( false !== $font_sizes ) {
 220          $editor_settings['fontSizes'] = $font_sizes;
 221      }
 222  
 223      $gradient_presets = current( (array) get_theme_support( 'editor-gradient-presets' ) );
 224      if ( false !== $gradient_presets ) {
 225          $editor_settings['gradients'] = $gradient_presets;
 226      }
 227  
 228      return $editor_settings;
 229  }
 230  
 231  /**
 232   * Returns the block editor settings needed to use the Legacy Widget block which
 233   * is not registered by default.
 234   *
 235   * @since 5.8.0
 236   *
 237   * @return array Settings to be used with get_block_editor_settings().
 238   */
 239  function get_legacy_widget_block_editor_settings() {
 240      $editor_settings = array();
 241  
 242      /**
 243       * Filters the list of widget-type IDs that should **not** be offered by the
 244       * Legacy Widget block.
 245       *
 246       * Returning an empty array will make all widgets available.
 247       *
 248       * @since 5.8.0
 249       *
 250       * @param array $widgets An array of excluded widget-type IDs.
 251       */
 252      $editor_settings['widgetTypesToHideFromLegacyWidgetBlock'] = apply_filters(
 253          'widget_types_to_hide_from_legacy_widget_block',
 254          array(
 255              'pages',
 256              'calendar',
 257              'archives',
 258              'media_audio',
 259              'media_image',
 260              'media_gallery',
 261              'media_video',
 262              'search',
 263              'text',
 264              'categories',
 265              'recent-posts',
 266              'recent-comments',
 267              'rss',
 268              'tag_cloud',
 269              'custom_html',
 270              'block',
 271          )
 272      );
 273  
 274      return $editor_settings;
 275  }
 276  
 277  /**
 278   * Returns the contextualized block editor settings settings for a selected editor context.
 279   *
 280   * @since 5.8.0
 281   *
 282   * @param array                   $custom_settings      Custom settings to use with the given editor type.
 283   * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
 284   *
 285   * @return array The contextualized block editor settings.
 286   */
 287  function get_block_editor_settings( array $custom_settings, $block_editor_context ) {
 288      $editor_settings = array_merge(
 289          get_default_block_editor_settings(),
 290          array(
 291              'allowedBlockTypes' => get_allowed_block_types( $block_editor_context ),
 292              'blockCategories'   => get_block_categories( $block_editor_context ),
 293          ),
 294          $custom_settings
 295      );
 296  
 297      $theme_json = WP_Theme_JSON_Resolver::get_merged_data( $editor_settings );
 298  
 299      if ( WP_Theme_JSON_Resolver::theme_has_support() ) {
 300          $editor_settings['styles'][] = array(
 301              'css'            => $theme_json->get_stylesheet( 'block_styles' ),
 302              '__unstableType' => 'globalStyles',
 303          );
 304          $editor_settings['styles'][] = array(
 305              'css'                     => $theme_json->get_stylesheet( 'css_variables' ),
 306              '__experimentalNoWrapper' => true,
 307              '__unstableType'          => 'globalStyles',
 308          );
 309      }
 310  
 311      $editor_settings['__experimentalFeatures'] = $theme_json->get_settings();
 312      // These settings may need to be updated based on data coming from theme.json sources.
 313      if ( isset( $settings['__experimentalFeatures']['color']['palette'] ) ) {
 314          $colors_by_origin   = $settings['__experimentalFeatures']['color']['palette'];
 315          $settings['colors'] = isset( $colors_by_origin['user'] ) ?
 316              $colors_by_origin['user'] : (
 317                  isset( $colors_by_origin['theme'] ) ?
 318                      $colors_by_origin['theme'] :
 319                      $colors_by_origin['core']
 320              );
 321      }
 322      if ( isset( $settings['__experimentalFeatures']['color']['gradients'] ) ) {
 323          $gradients_by_origin   = $settings['__experimentalFeatures']['color']['gradients'];
 324          $settings['gradients'] = isset( $gradients_by_origin['user'] ) ?
 325              $gradients_by_origin['user'] : (
 326                  isset( $gradients_by_origin['theme'] ) ?
 327                      $gradients_by_origin['theme'] :
 328                      $gradients_by_origin['core']
 329              );
 330      }
 331      if ( isset( $settings['__experimentalFeatures']['typography']['fontSizes'] ) ) {
 332          $font_sizes_by_origin  = $settings['__experimentalFeatures']['typography']['fontSizes'];
 333          $settings['fontSizes'] = isset( $font_sizes_by_origin['user'] ) ?
 334              $font_sizes_by_origin['user'] : (
 335                  isset( $font_sizes_by_origin['theme'] ) ?
 336                      $font_sizes_by_origin['theme'] :
 337                      $font_sizes_by_origin['core']
 338              );
 339      }
 340      if ( isset( $editor_settings['__experimentalFeatures']['color']['custom'] ) ) {
 341          $editor_settings['disableCustomColors'] = ! $editor_settings['__experimentalFeatures']['color']['custom'];
 342          unset( $editor_settings['__experimentalFeatures']['color']['custom'] );
 343      }
 344      if ( isset( $editor_settings['__experimentalFeatures']['color']['customGradient'] ) ) {
 345          $editor_settings['disableCustomGradients'] = ! $editor_settings['__experimentalFeatures']['color']['customGradient'];
 346          unset( $editor_settings['__experimentalFeatures']['color']['customGradient'] );
 347      }
 348      if ( isset( $editor_settings['__experimentalFeatures']['typography']['customFontSize'] ) ) {
 349          $editor_settings['disableCustomFontSizes'] = ! $editor_settings['__experimentalFeatures']['typography']['customFontSize'];
 350          unset( $editor_settings['__experimentalFeatures']['typography']['customFontSize'] );
 351      }
 352      if ( isset( $editor_settings['__experimentalFeatures']['typography']['customLineHeight'] ) ) {
 353          $editor_settings['enableCustomLineHeight'] = $editor_settings['__experimentalFeatures']['typography']['customLineHeight'];
 354          unset( $editor_settings['__experimentalFeatures']['typography']['customLineHeight'] );
 355      }
 356      if ( isset( $editor_settings['__experimentalFeatures']['spacing']['units'] ) ) {
 357          if ( ! is_array( $editor_settings['__experimentalFeatures']['spacing']['units'] ) ) {
 358              $editor_settings['enableCustomUnits'] = false;
 359          } else {
 360              $editor_settings['enableCustomUnits'] = count( $editor_settings['__experimentalFeatures']['spacing']['units'] ) > 0;
 361          }
 362          unset( $editor_settings['__experimentalFeatures']['spacing']['units'] );
 363      }
 364      if ( isset( $editor_settings['__experimentalFeatures']['spacing']['customPadding'] ) ) {
 365          $editor_settings['enableCustomSpacing'] = $editor_settings['__experimentalFeatures']['spacing']['customPadding'];
 366          unset( $editor_settings['__experimentalFeatures']['spacing']['customPadding'] );
 367      }
 368  
 369      /**
 370       * Filters the settings to pass to the block editor for all editor type.
 371       *
 372       * @since 5.8.0
 373       *
 374       * @param array                   $editor_settings      Default editor settings.
 375       * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
 376       */
 377      $editor_settings = apply_filters( 'block_editor_settings_all', $editor_settings, $block_editor_context );
 378      if ( ! empty( $block_editor_context->post ) ) {
 379          $post = $block_editor_context->post;
 380  
 381          /**
 382           * Filters the settings to pass to the block editor.
 383           *
 384           * @since 5.0.0
 385           * @deprecated 5.8.0 The hook transitioned to support also screens that don't contain $post instance.
 386           *
 387           * @param array   $editor_settings Default editor settings.
 388           * @param WP_Post $post            Post being edited.
 389           */
 390          $editor_settings = apply_filters_deprecated( 'block_editor_settings', array( $editor_settings, $post ), '5.8.0', 'block_editor_settings_all' );
 391      }
 392  
 393      return $editor_settings;
 394  }
 395  
 396  /**
 397   * Preloads common data used with the block editor by specifying an array of
 398   * REST API paths that will be preloaded for a given block editor context.
 399   *
 400   * @since 5.8.0
 401   *
 402   * @global WP_Post $post Global post object.
 403   *
 404   * @param array                   $preload_paths        List of paths to preload.
 405   * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
 406   *
 407   * @return void
 408   */
 409  function block_editor_rest_api_preload( array $preload_paths, $block_editor_context ) {
 410      global $post;
 411  
 412      /**
 413       * Filters the array of REST API paths that will be used to preloaded common data
 414       * to use with the block editor.
 415       *
 416       * @since 5.8.0
 417       *
 418       * @param string[] $preload_paths Array of paths to preload.
 419       */
 420      $preload_paths = apply_filters( 'block_editor_rest_api_preload_paths', $preload_paths, $block_editor_context );
 421      if ( ! empty( $block_editor_context->post ) ) {
 422          $selected_post = $block_editor_context->post;
 423  
 424          /**
 425           * Preload common data by specifying an array of REST API paths that will be preloaded.
 426           *
 427           * Filters the array of paths that will be preloaded.
 428           *
 429           * @since 5.0.0
 430           * @deprecated 5.8.0 The hook transitioned to support also screens that don't contain $post instance.
 431           *
 432           * @param string[] $preload_paths Array of paths to preload.
 433           * @param WP_Post  $selected_post Post being edited.
 434           */
 435          $preload_paths = apply_filters_deprecated( 'block_editor_preload_paths', array( $preload_paths, $selected_post ), '5.8.0', 'block_editor_rest_api_preload_paths' );
 436      }
 437  
 438      if ( empty( $preload_paths ) ) {
 439          return;
 440      }
 441  
 442      /*
 443       * Ensure the global $post remains the same after API data is preloaded.
 444       * Because API preloading can call the_content and other filters, plugins
 445       * can unexpectedly modify $post.
 446       */
 447      $backup_global_post = ! empty( $post ) ? clone $post : $post;
 448  
 449      $preload_data = array_reduce(
 450          $preload_paths,
 451          'rest_preload_api_request',
 452          array()
 453      );
 454  
 455      // Restore the global $post as it was before API preloading.
 456      $post = $backup_global_post;
 457  
 458      wp_add_inline_script(
 459          'wp-api-fetch',
 460          sprintf(
 461              'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );',
 462              wp_json_encode( $preload_data )
 463          ),
 464          'after'
 465      );
 466  }


Generated: Fri Jun 18 01:00:07 2021 Cross-referenced by PHPXref 0.7.1