[ Index ] |
PHP Cross Reference of WordPress |
[Summary view] [Print] [Text view]
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 $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 * Collect the block editor assets that need to be loaded into the editor's iframe. 292 * 293 * @since 6.0.0 294 * @access private 295 * 296 * @global string $pagenow The filename of the current screen. 297 * 298 * @return array { 299 * The block editor assets. 300 * 301 * @type string|false $styles String containing the HTML for styles. 302 * @type string|false $scripts String containing the HTML for scripts. 303 * } 304 */ 305 function _wp_get_iframed_editor_assets() { 306 global $pagenow; 307 308 $script_handles = array(); 309 $style_handles = array( 310 'wp-block-editor', 311 'wp-block-library', 312 'wp-block-library-theme', 313 'wp-edit-blocks', 314 ); 315 316 if ( 'widgets.php' === $pagenow || 'customize.php' === $pagenow ) { 317 $style_handles[] = 'wp-widgets'; 318 $style_handles[] = 'wp-edit-widgets'; 319 } 320 321 $block_registry = WP_Block_Type_Registry::get_instance(); 322 323 foreach ( $block_registry->get_all_registered() as $block_type ) { 324 if ( ! empty( $block_type->style ) ) { 325 $style_handles[] = $block_type->style; 326 } 327 328 if ( ! empty( $block_type->editor_style ) ) { 329 $style_handles[] = $block_type->editor_style; 330 } 331 332 if ( ! empty( $block_type->script ) ) { 333 $script_handles[] = $block_type->script; 334 } 335 } 336 337 $style_handles = array_unique( $style_handles ); 338 $done = wp_styles()->done; 339 340 ob_start(); 341 342 // We do not need reset styles for the iframed editor. 343 wp_styles()->done = array( 'wp-reset-editor-styles' ); 344 wp_styles()->do_items( $style_handles ); 345 wp_styles()->done = $done; 346 347 $styles = ob_get_clean(); 348 349 $script_handles = array_unique( $script_handles ); 350 $done = wp_scripts()->done; 351 352 ob_start(); 353 354 wp_scripts()->done = array(); 355 wp_scripts()->do_items( $script_handles ); 356 wp_scripts()->done = $done; 357 358 $scripts = ob_get_clean(); 359 360 return array( 361 'styles' => $styles, 362 'scripts' => $scripts, 363 ); 364 } 365 366 /** 367 * Returns the contextualized block editor settings for a selected editor context. 368 * 369 * @since 5.8.0 370 * 371 * @param array $custom_settings Custom settings to use with the given editor type. 372 * @param WP_Block_Editor_Context $block_editor_context The current block editor context. 373 * 374 * @return array The contextualized block editor settings. 375 */ 376 function get_block_editor_settings( array $custom_settings, $block_editor_context ) { 377 $editor_settings = array_merge( 378 get_default_block_editor_settings(), 379 array( 380 'allowedBlockTypes' => get_allowed_block_types( $block_editor_context ), 381 'blockCategories' => get_block_categories( $block_editor_context ), 382 ), 383 $custom_settings 384 ); 385 386 $global_styles = array(); 387 $presets = array( 388 array( 389 'css' => 'variables', 390 '__unstableType' => 'presets', 391 'isGlobalStyles' => true, 392 ), 393 array( 394 'css' => 'presets', 395 '__unstableType' => 'presets', 396 'isGlobalStyles' => true, 397 ), 398 ); 399 foreach ( $presets as $preset_style ) { 400 $actual_css = wp_get_global_stylesheet( array( $preset_style['css'] ) ); 401 if ( '' !== $actual_css ) { 402 $preset_style['css'] = $actual_css; 403 $global_styles[] = $preset_style; 404 } 405 } 406 407 if ( WP_Theme_JSON_Resolver::theme_has_support() ) { 408 $block_classes = array( 409 'css' => 'styles', 410 '__unstableType' => 'theme', 411 'isGlobalStyles' => true, 412 ); 413 $actual_css = wp_get_global_stylesheet( array( $block_classes['css'] ) ); 414 if ( '' !== $actual_css ) { 415 $block_classes['css'] = $actual_css; 416 $global_styles[] = $block_classes; 417 } 418 } 419 420 $editor_settings['styles'] = array_merge( $global_styles, get_block_editor_theme_styles() ); 421 422 $editor_settings['__experimentalFeatures'] = wp_get_global_settings(); 423 // These settings may need to be updated based on data coming from theme.json sources. 424 if ( isset( $editor_settings['__experimentalFeatures']['color']['palette'] ) ) { 425 $colors_by_origin = $editor_settings['__experimentalFeatures']['color']['palette']; 426 $editor_settings['colors'] = isset( $colors_by_origin['custom'] ) ? 427 $colors_by_origin['custom'] : ( 428 isset( $colors_by_origin['theme'] ) ? 429 $colors_by_origin['theme'] : 430 $colors_by_origin['default'] 431 ); 432 } 433 if ( isset( $editor_settings['__experimentalFeatures']['color']['gradients'] ) ) { 434 $gradients_by_origin = $editor_settings['__experimentalFeatures']['color']['gradients']; 435 $editor_settings['gradients'] = isset( $gradients_by_origin['custom'] ) ? 436 $gradients_by_origin['custom'] : ( 437 isset( $gradients_by_origin['theme'] ) ? 438 $gradients_by_origin['theme'] : 439 $gradients_by_origin['default'] 440 ); 441 } 442 if ( isset( $editor_settings['__experimentalFeatures']['typography']['fontSizes'] ) ) { 443 $font_sizes_by_origin = $editor_settings['__experimentalFeatures']['typography']['fontSizes']; 444 $editor_settings['fontSizes'] = isset( $font_sizes_by_origin['custom'] ) ? 445 $font_sizes_by_origin['custom'] : ( 446 isset( $font_sizes_by_origin['theme'] ) ? 447 $font_sizes_by_origin['theme'] : 448 $font_sizes_by_origin['default'] 449 ); 450 } 451 if ( isset( $editor_settings['__experimentalFeatures']['color']['custom'] ) ) { 452 $editor_settings['disableCustomColors'] = ! $editor_settings['__experimentalFeatures']['color']['custom']; 453 unset( $editor_settings['__experimentalFeatures']['color']['custom'] ); 454 } 455 if ( isset( $editor_settings['__experimentalFeatures']['color']['customGradient'] ) ) { 456 $editor_settings['disableCustomGradients'] = ! $editor_settings['__experimentalFeatures']['color']['customGradient']; 457 unset( $editor_settings['__experimentalFeatures']['color']['customGradient'] ); 458 } 459 if ( isset( $editor_settings['__experimentalFeatures']['typography']['customFontSize'] ) ) { 460 $editor_settings['disableCustomFontSizes'] = ! $editor_settings['__experimentalFeatures']['typography']['customFontSize']; 461 unset( $editor_settings['__experimentalFeatures']['typography']['customFontSize'] ); 462 } 463 if ( isset( $editor_settings['__experimentalFeatures']['typography']['lineHeight'] ) ) { 464 $editor_settings['enableCustomLineHeight'] = $editor_settings['__experimentalFeatures']['typography']['lineHeight']; 465 unset( $editor_settings['__experimentalFeatures']['typography']['lineHeight'] ); 466 } 467 if ( isset( $editor_settings['__experimentalFeatures']['spacing']['units'] ) ) { 468 $editor_settings['enableCustomUnits'] = $editor_settings['__experimentalFeatures']['spacing']['units']; 469 unset( $editor_settings['__experimentalFeatures']['spacing']['units'] ); 470 } 471 if ( isset( $editor_settings['__experimentalFeatures']['spacing']['padding'] ) ) { 472 $editor_settings['enableCustomSpacing'] = $editor_settings['__experimentalFeatures']['spacing']['padding']; 473 unset( $editor_settings['__experimentalFeatures']['spacing']['padding'] ); 474 } 475 476 $editor_settings['__unstableResolvedAssets'] = _wp_get_iframed_editor_assets(); 477 $editor_settings['localAutosaveInterval'] = 15; 478 $editor_settings['__experimentalDiscussionSettings'] = array( 479 'commentOrder' => get_option( 'comment_order' ), 480 'commentsPerPage' => get_option( 'comments_per_page' ), 481 'defaultCommentsPage' => get_option( 'default_comments_page' ), 482 'pageComments' => get_option( 'page_comments' ), 483 'threadComments' => get_option( 'thread_comments' ), 484 'threadCommentsDepth' => get_option( 'thread_comments_depth' ), 485 'defaultCommentStatus' => get_option( 'default_comment_status' ), 486 'avatarURL' => get_avatar_url( 487 '', 488 array( 489 'size' => 96, 490 'force_default' => true, 491 'default' => get_option( 'avatar_default' ), 492 ) 493 ), 494 ); 495 496 /** 497 * Filters the settings to pass to the block editor for all editor type. 498 * 499 * @since 5.8.0 500 * 501 * @param array $editor_settings Default editor settings. 502 * @param WP_Block_Editor_Context $block_editor_context The current block editor context. 503 */ 504 $editor_settings = apply_filters( 'block_editor_settings_all', $editor_settings, $block_editor_context ); 505 506 if ( ! empty( $block_editor_context->post ) ) { 507 $post = $block_editor_context->post; 508 509 /** 510 * Filters the settings to pass to the block editor. 511 * 512 * @since 5.0.0 513 * @deprecated 5.8.0 Use the {@see 'block_editor_settings_all'} filter instead. 514 * 515 * @param array $editor_settings Default editor settings. 516 * @param WP_Post $post Post being edited. 517 */ 518 $editor_settings = apply_filters_deprecated( 'block_editor_settings', array( $editor_settings, $post ), '5.8.0', 'block_editor_settings_all' ); 519 } 520 521 return $editor_settings; 522 } 523 524 /** 525 * Preloads common data used with the block editor by specifying an array of 526 * REST API paths that will be preloaded for a given block editor context. 527 * 528 * @since 5.8.0 529 * 530 * @global WP_Post $post Global post object. 531 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts. 532 * @global WP_Styles $wp_styles The WP_Styles object for printing styles. 533 * 534 * @param string[] $preload_paths List of paths to preload. 535 * @param WP_Block_Editor_Context $block_editor_context The current block editor context. 536 */ 537 function block_editor_rest_api_preload( array $preload_paths, $block_editor_context ) { 538 global $post, $wp_scripts, $wp_styles; 539 540 /** 541 * Filters the array of REST API paths that will be used to preloaded common data for the block editor. 542 * 543 * @since 5.8.0 544 * 545 * @param string[] $preload_paths Array of paths to preload. 546 * @param WP_Block_Editor_Context $block_editor_context The current block editor context. 547 */ 548 $preload_paths = apply_filters( 'block_editor_rest_api_preload_paths', $preload_paths, $block_editor_context ); 549 550 if ( ! empty( $block_editor_context->post ) ) { 551 $selected_post = $block_editor_context->post; 552 553 /** 554 * Filters the array of paths that will be preloaded. 555 * 556 * Preload common data by specifying an array of REST API paths that will be preloaded. 557 * 558 * @since 5.0.0 559 * @deprecated 5.8.0 Use the {@see 'block_editor_rest_api_preload_paths'} filter instead. 560 * 561 * @param string[] $preload_paths Array of paths to preload. 562 * @param WP_Post $selected_post Post being edited. 563 */ 564 $preload_paths = apply_filters_deprecated( 'block_editor_preload_paths', array( $preload_paths, $selected_post ), '5.8.0', 'block_editor_rest_api_preload_paths' ); 565 } 566 567 if ( empty( $preload_paths ) ) { 568 return; 569 } 570 571 /* 572 * Ensure the global $post, $wp_scripts, and $wp_styles remain the same after 573 * API data is preloaded. 574 * Because API preloading can call the_content and other filters, plugins 575 * can unexpectedly modify the global $post or enqueue assets which are not 576 * intended for the block editor. 577 */ 578 $backup_global_post = ! empty( $post ) ? clone $post : $post; 579 $backup_wp_scripts = ! empty( $wp_scripts ) ? clone $wp_scripts : $wp_scripts; 580 $backup_wp_styles = ! empty( $wp_styles ) ? clone $wp_styles : $wp_styles; 581 582 foreach ( $preload_paths as &$path ) { 583 if ( is_string( $path ) && ! str_starts_with( $path, '/' ) ) { 584 $path = '/' . $path; 585 continue; 586 } 587 588 if ( is_array( $path ) && is_string( $path[0] ) && ! str_starts_with( $path[0], '/' ) ) { 589 $path[0] = '/' . $path[0]; 590 } 591 } 592 593 unset( $path ); 594 595 $preload_data = array_reduce( 596 $preload_paths, 597 'rest_preload_api_request', 598 array() 599 ); 600 601 // Restore the global $post, $wp_scripts, and $wp_styles as they were before API preloading. 602 $post = $backup_global_post; 603 $wp_scripts = $backup_wp_scripts; 604 $wp_styles = $backup_wp_styles; 605 606 wp_add_inline_script( 607 'wp-api-fetch', 608 sprintf( 609 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', 610 wp_json_encode( $preload_data ) 611 ), 612 'after' 613 ); 614 } 615 616 /** 617 * Creates an array of theme styles to load into the block editor. 618 * 619 * @since 5.8.0 620 * 621 * @global array $editor_styles 622 * 623 * @return array An array of theme styles for the block editor. 624 */ 625 function get_block_editor_theme_styles() { 626 global $editor_styles; 627 628 $styles = array(); 629 630 if ( $editor_styles && current_theme_supports( 'editor-styles' ) ) { 631 foreach ( $editor_styles as $style ) { 632 if ( preg_match( '~^(https?:)?//~', $style ) ) { 633 $response = wp_remote_get( $style ); 634 if ( ! is_wp_error( $response ) ) { 635 $styles[] = array( 636 'css' => wp_remote_retrieve_body( $response ), 637 '__unstableType' => 'theme', 638 'isGlobalStyles' => false, 639 ); 640 } 641 } else { 642 $file = get_theme_file_path( $style ); 643 if ( is_file( $file ) ) { 644 $styles[] = array( 645 'css' => file_get_contents( $file ), 646 'baseURL' => get_theme_file_uri( $style ), 647 '__unstableType' => 'theme', 648 'isGlobalStyles' => false, 649 ); 650 } 651 } 652 } 653 } 654 655 return $styles; 656 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Dec 22 01:00:02 2024 | Cross-referenced by PHPXref 0.7.1 |