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