[ 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 * 113 * @return bool|string[] Array of block type slugs, or boolean to enable/disable all. 114 */ 115 function get_allowed_block_types( $block_editor_context ) { 116 $allowed_block_types = true; 117 118 /** 119 * Filters the allowed block types for all editor types. 120 * 121 * @since 5.8.0 122 * 123 * @param bool|string[] $allowed_block_types Array of block type slugs, or boolean to enable/disable all. 124 * Default true (all registered block types supported). 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 129 if ( ! empty( $block_editor_context->post ) ) { 130 $post = $block_editor_context->post; 131 132 /** 133 * Filters the allowed block types for the editor. 134 * 135 * @since 5.0.0 136 * @deprecated 5.8.0 Use the {@see 'allowed_block_types_all'} filter instead. 137 * 138 * @param bool|string[] $allowed_block_types Array of block type slugs, or boolean to enable/disable all. 139 * Default true (all registered block types supported) 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 158 // wp_max_upload_size() can be expensive, so only call it when relevant for the current user. 159 $max_upload_size = 0; 160 if ( current_user_can( 'upload_files' ) ) { 161 $max_upload_size = wp_max_upload_size(); 162 if ( ! $max_upload_size ) { 163 $max_upload_size = 0; 164 } 165 } 166 167 /** This filter is documented in wp-admin/includes/media.php */ 168 $image_size_names = apply_filters( 169 'image_size_names_choose', 170 array( 171 'thumbnail' => __( 'Thumbnail' ), 172 'medium' => __( 'Medium' ), 173 'large' => __( 'Large' ), 174 'full' => __( 'Full Size' ), 175 ) 176 ); 177 178 $available_image_sizes = array(); 179 foreach ( $image_size_names as $image_size_slug => $image_size_name ) { 180 $available_image_sizes[] = array( 181 'slug' => $image_size_slug, 182 'name' => $image_size_name, 183 ); 184 } 185 186 $default_size = get_option( 'image_default_size', 'large' ); 187 $image_default_size = in_array( $default_size, array_keys( $image_size_names ), true ) ? $default_size : 'large'; 188 189 $image_dimensions = array(); 190 $all_sizes = wp_get_registered_image_subsizes(); 191 foreach ( $available_image_sizes as $size ) { 192 $key = $size['slug']; 193 if ( isset( $all_sizes[ $key ] ) ) { 194 $image_dimensions[ $key ] = $all_sizes[ $key ]; 195 } 196 } 197 198 // These styles are used if the "no theme styles" options is triggered or on 199 // themes without their own editor styles. 200 $default_editor_styles_file = ABSPATH . WPINC . '/css/dist/block-editor/default-editor-styles.css'; 201 202 static $default_editor_styles_file_contents = false; 203 if ( ! $default_editor_styles_file_contents && file_exists( $default_editor_styles_file ) ) { 204 $default_editor_styles_file_contents = file_get_contents( $default_editor_styles_file ); 205 } 206 207 $default_editor_styles = array(); 208 if ( $default_editor_styles_file_contents ) { 209 $default_editor_styles = array( 210 array( 'css' => $default_editor_styles_file_contents ), 211 ); 212 } 213 214 $editor_settings = array( 215 'alignWide' => get_theme_support( 'align-wide' ), 216 'allowedBlockTypes' => true, 217 'allowedMimeTypes' => get_allowed_mime_types(), 218 'defaultEditorStyles' => $default_editor_styles, 219 'blockCategories' => get_default_block_categories(), 220 'isRTL' => is_rtl(), 221 'imageDefaultSize' => $image_default_size, 222 'imageDimensions' => $image_dimensions, 223 'imageEditing' => true, 224 'imageSizes' => $available_image_sizes, 225 'maxUploadFileSize' => $max_upload_size, 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 do_action( 'enqueue_block_assets' ); 341 remove_filter( 'should_load_block_editor_scripts_and_styles', '__return_false' ); 342 343 $block_registry = WP_Block_Type_Registry::get_instance(); 344 345 /* 346 * Additionally, do enqueue `editorStyle` assets for all blocks, which 347 * contains editor-only styling for blocks (editor content). 348 */ 349 foreach ( $block_registry->get_all_registered() as $block_type ) { 350 if ( isset( $block_type->editor_style_handles ) && is_array( $block_type->editor_style_handles ) ) { 351 foreach ( $block_type->editor_style_handles as $style_handle ) { 352 wp_enqueue_style( $style_handle ); 353 } 354 } 355 } 356 357 /** 358 * Remove the deprecated `print_emoji_styles` handler. 359 * It avoids breaking style generation with a deprecation message. 360 */ 361 $has_emoji_styles = has_action( 'wp_print_styles', 'print_emoji_styles' ); 362 if ( $has_emoji_styles ) { 363 remove_action( 'wp_print_styles', 'print_emoji_styles' ); 364 } 365 366 ob_start(); 367 wp_print_styles(); 368 wp_print_font_faces(); 369 $styles = ob_get_clean(); 370 371 if ( $has_emoji_styles ) { 372 add_action( 'wp_print_styles', 'print_emoji_styles' ); 373 } 374 375 ob_start(); 376 wp_print_head_scripts(); 377 wp_print_footer_scripts(); 378 $scripts = ob_get_clean(); 379 380 // Restore the original instances. 381 $wp_styles = $current_wp_styles; 382 $wp_scripts = $current_wp_scripts; 383 384 return array( 385 'styles' => $styles, 386 'scripts' => $scripts, 387 ); 388 } 389 390 /** 391 * Finds the first occurrence of a specific block in an array of blocks. 392 * 393 * @since 6.3.0 394 * 395 * @param array $blocks Array of blocks. 396 * @param string $block_name Name of the block to find. 397 * @return array Found block, or empty array if none found. 398 */ 399 function wp_get_first_block( $blocks, $block_name ) { 400 foreach ( $blocks as $block ) { 401 if ( $block_name === $block['blockName'] ) { 402 return $block; 403 } 404 if ( ! empty( $block['innerBlocks'] ) ) { 405 $found_block = wp_get_first_block( $block['innerBlocks'], $block_name ); 406 407 if ( ! empty( $found_block ) ) { 408 return $found_block; 409 } 410 } 411 } 412 413 return array(); 414 } 415 416 /** 417 * Retrieves Post Content block attributes from the current post template. 418 * 419 * @since 6.3.0 420 * @since 6.4.0 Return null if there is no post content block. 421 * @access private 422 * 423 * @global int $post_ID 424 * 425 * @return array|null Post Content block attributes array or null if Post Content block doesn't exist. 426 */ 427 function wp_get_post_content_block_attributes() { 428 global $post_ID; 429 430 $is_block_theme = wp_is_block_theme(); 431 432 if ( ! $is_block_theme || ! $post_ID ) { 433 return null; 434 } 435 436 $template_slug = get_page_template_slug( $post_ID ); 437 438 if ( ! $template_slug ) { 439 $post_slug = 'singular'; 440 $page_slug = 'singular'; 441 $template_types = get_block_templates(); 442 443 foreach ( $template_types as $template_type ) { 444 if ( 'page' === $template_type->slug ) { 445 $page_slug = 'page'; 446 } 447 if ( 'single' === $template_type->slug ) { 448 $post_slug = 'single'; 449 } 450 } 451 452 $what_post_type = get_post_type( $post_ID ); 453 switch ( $what_post_type ) { 454 case 'page': 455 $template_slug = $page_slug; 456 break; 457 default: 458 $template_slug = $post_slug; 459 break; 460 } 461 } 462 463 $current_template = get_block_templates( array( 'slug__in' => array( $template_slug ) ) ); 464 465 if ( ! empty( $current_template ) ) { 466 $template_blocks = parse_blocks( $current_template[0]->content ); 467 $post_content_block = wp_get_first_block( $template_blocks, 'core/post-content' ); 468 469 if ( isset( $post_content_block['attrs'] ) ) { 470 return $post_content_block['attrs']; 471 } 472 } 473 474 return null; 475 } 476 477 /** 478 * Returns the contextualized block editor settings for a selected editor context. 479 * 480 * @since 5.8.0 481 * 482 * @param array $custom_settings Custom settings to use with the given editor type. 483 * @param WP_Block_Editor_Context $block_editor_context The current block editor context. 484 * 485 * @return array The contextualized block editor settings. 486 */ 487 function get_block_editor_settings( array $custom_settings, $block_editor_context ) { 488 $editor_settings = array_merge( 489 get_default_block_editor_settings(), 490 array( 491 'allowedBlockTypes' => get_allowed_block_types( $block_editor_context ), 492 'blockCategories' => get_block_categories( $block_editor_context ), 493 ), 494 $custom_settings 495 ); 496 497 $global_styles = array(); 498 $presets = array( 499 array( 500 'css' => 'variables', 501 '__unstableType' => 'presets', 502 'isGlobalStyles' => true, 503 ), 504 array( 505 'css' => 'presets', 506 '__unstableType' => 'presets', 507 'isGlobalStyles' => true, 508 ), 509 ); 510 foreach ( $presets as $preset_style ) { 511 $actual_css = wp_get_global_stylesheet( array( $preset_style['css'] ) ); 512 if ( '' !== $actual_css ) { 513 $preset_style['css'] = $actual_css; 514 $global_styles[] = $preset_style; 515 } 516 } 517 518 if ( wp_theme_has_theme_json() ) { 519 $block_classes = array( 520 'css' => 'styles', 521 '__unstableType' => 'theme', 522 'isGlobalStyles' => true, 523 ); 524 $actual_css = wp_get_global_stylesheet( array( $block_classes['css'] ) ); 525 if ( '' !== $actual_css ) { 526 $block_classes['css'] = $actual_css; 527 $global_styles[] = $block_classes; 528 } 529 530 /* 531 * Add the custom CSS as a separate stylesheet so any invalid CSS 532 * entered by users does not break other global styles. 533 */ 534 $global_styles[] = array( 535 'css' => wp_get_global_styles_custom_css(), 536 '__unstableType' => 'user', 537 'isGlobalStyles' => true, 538 ); 539 } else { 540 // If there is no `theme.json` file, ensure base layout styles are still available. 541 $block_classes = array( 542 'css' => 'base-layout-styles', 543 '__unstableType' => 'base-layout', 544 'isGlobalStyles' => true, 545 ); 546 $actual_css = wp_get_global_stylesheet( array( $block_classes['css'] ) ); 547 if ( '' !== $actual_css ) { 548 $block_classes['css'] = $actual_css; 549 $global_styles[] = $block_classes; 550 } 551 } 552 553 $editor_settings['styles'] = array_merge( $global_styles, get_block_editor_theme_styles() ); 554 555 $editor_settings['__experimentalFeatures'] = wp_get_global_settings(); 556 // These settings may need to be updated based on data coming from theme.json sources. 557 if ( isset( $editor_settings['__experimentalFeatures']['color']['palette'] ) ) { 558 $colors_by_origin = $editor_settings['__experimentalFeatures']['color']['palette']; 559 $editor_settings['colors'] = isset( $colors_by_origin['custom'] ) ? 560 $colors_by_origin['custom'] : ( 561 isset( $colors_by_origin['theme'] ) ? 562 $colors_by_origin['theme'] : 563 $colors_by_origin['default'] 564 ); 565 } 566 if ( isset( $editor_settings['__experimentalFeatures']['color']['gradients'] ) ) { 567 $gradients_by_origin = $editor_settings['__experimentalFeatures']['color']['gradients']; 568 $editor_settings['gradients'] = isset( $gradients_by_origin['custom'] ) ? 569 $gradients_by_origin['custom'] : ( 570 isset( $gradients_by_origin['theme'] ) ? 571 $gradients_by_origin['theme'] : 572 $gradients_by_origin['default'] 573 ); 574 } 575 if ( isset( $editor_settings['__experimentalFeatures']['typography']['fontSizes'] ) ) { 576 $font_sizes_by_origin = $editor_settings['__experimentalFeatures']['typography']['fontSizes']; 577 $editor_settings['fontSizes'] = isset( $font_sizes_by_origin['custom'] ) ? 578 $font_sizes_by_origin['custom'] : ( 579 isset( $font_sizes_by_origin['theme'] ) ? 580 $font_sizes_by_origin['theme'] : 581 $font_sizes_by_origin['default'] 582 ); 583 } 584 if ( isset( $editor_settings['__experimentalFeatures']['color']['custom'] ) ) { 585 $editor_settings['disableCustomColors'] = ! $editor_settings['__experimentalFeatures']['color']['custom']; 586 unset( $editor_settings['__experimentalFeatures']['color']['custom'] ); 587 } 588 if ( isset( $editor_settings['__experimentalFeatures']['color']['customGradient'] ) ) { 589 $editor_settings['disableCustomGradients'] = ! $editor_settings['__experimentalFeatures']['color']['customGradient']; 590 unset( $editor_settings['__experimentalFeatures']['color']['customGradient'] ); 591 } 592 if ( isset( $editor_settings['__experimentalFeatures']['typography']['customFontSize'] ) ) { 593 $editor_settings['disableCustomFontSizes'] = ! $editor_settings['__experimentalFeatures']['typography']['customFontSize']; 594 unset( $editor_settings['__experimentalFeatures']['typography']['customFontSize'] ); 595 } 596 if ( isset( $editor_settings['__experimentalFeatures']['typography']['lineHeight'] ) ) { 597 $editor_settings['enableCustomLineHeight'] = $editor_settings['__experimentalFeatures']['typography']['lineHeight']; 598 unset( $editor_settings['__experimentalFeatures']['typography']['lineHeight'] ); 599 } 600 if ( isset( $editor_settings['__experimentalFeatures']['spacing']['units'] ) ) { 601 $editor_settings['enableCustomUnits'] = $editor_settings['__experimentalFeatures']['spacing']['units']; 602 unset( $editor_settings['__experimentalFeatures']['spacing']['units'] ); 603 } 604 if ( isset( $editor_settings['__experimentalFeatures']['spacing']['padding'] ) ) { 605 $editor_settings['enableCustomSpacing'] = $editor_settings['__experimentalFeatures']['spacing']['padding']; 606 unset( $editor_settings['__experimentalFeatures']['spacing']['padding'] ); 607 } 608 if ( isset( $editor_settings['__experimentalFeatures']['spacing']['customSpacingSize'] ) ) { 609 $editor_settings['disableCustomSpacingSizes'] = ! $editor_settings['__experimentalFeatures']['spacing']['customSpacingSize']; 610 unset( $editor_settings['__experimentalFeatures']['spacing']['customSpacingSize'] ); 611 } 612 613 if ( isset( $editor_settings['__experimentalFeatures']['spacing']['spacingSizes'] ) ) { 614 $spacing_sizes_by_origin = $editor_settings['__experimentalFeatures']['spacing']['spacingSizes']; 615 $editor_settings['spacingSizes'] = isset( $spacing_sizes_by_origin['custom'] ) ? 616 $spacing_sizes_by_origin['custom'] : ( 617 isset( $spacing_sizes_by_origin['theme'] ) ? 618 $spacing_sizes_by_origin['theme'] : 619 $spacing_sizes_by_origin['default'] 620 ); 621 } 622 623 $editor_settings['__unstableResolvedAssets'] = _wp_get_iframed_editor_assets(); 624 $editor_settings['__unstableIsBlockBasedTheme'] = wp_is_block_theme(); 625 $editor_settings['localAutosaveInterval'] = 15; 626 $editor_settings['disableLayoutStyles'] = current_theme_supports( 'disable-layout-styles' ); 627 $editor_settings['__experimentalDiscussionSettings'] = array( 628 'commentOrder' => get_option( 'comment_order' ), 629 'commentsPerPage' => get_option( 'comments_per_page' ), 630 'defaultCommentsPage' => get_option( 'default_comments_page' ), 631 'pageComments' => get_option( 'page_comments' ), 632 'threadComments' => get_option( 'thread_comments' ), 633 'threadCommentsDepth' => get_option( 'thread_comments_depth' ), 634 'defaultCommentStatus' => get_option( 'default_comment_status' ), 635 'avatarURL' => get_avatar_url( 636 '', 637 array( 638 'size' => 96, 639 'force_default' => true, 640 'default' => get_option( 'avatar_default' ), 641 ) 642 ), 643 ); 644 645 $post_content_block_attributes = wp_get_post_content_block_attributes(); 646 647 if ( isset( $post_content_block_attributes ) ) { 648 $editor_settings['postContentAttributes'] = $post_content_block_attributes; 649 } 650 651 /** 652 * Filters the settings to pass to the block editor for all editor type. 653 * 654 * @since 5.8.0 655 * 656 * @param array $editor_settings Default editor settings. 657 * @param WP_Block_Editor_Context $block_editor_context The current block editor context. 658 */ 659 $editor_settings = apply_filters( 'block_editor_settings_all', $editor_settings, $block_editor_context ); 660 661 if ( ! empty( $block_editor_context->post ) ) { 662 $post = $block_editor_context->post; 663 664 /** 665 * Filters the settings to pass to the block editor. 666 * 667 * @since 5.0.0 668 * @deprecated 5.8.0 Use the {@see 'block_editor_settings_all'} filter instead. 669 * 670 * @param array $editor_settings Default editor settings. 671 * @param WP_Post $post Post being edited. 672 */ 673 $editor_settings = apply_filters_deprecated( 'block_editor_settings', array( $editor_settings, $post ), '5.8.0', 'block_editor_settings_all' ); 674 } 675 676 return $editor_settings; 677 } 678 679 /** 680 * Preloads common data used with the block editor by specifying an array of 681 * REST API paths that will be preloaded for a given block editor context. 682 * 683 * @since 5.8.0 684 * 685 * @global WP_Post $post Global post object. 686 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts. 687 * @global WP_Styles $wp_styles The WP_Styles object for printing styles. 688 * 689 * @param (string|string[])[] $preload_paths List of paths to preload. 690 * @param WP_Block_Editor_Context $block_editor_context The current block editor context. 691 */ 692 function block_editor_rest_api_preload( array $preload_paths, $block_editor_context ) { 693 global $post, $wp_scripts, $wp_styles; 694 695 /** 696 * Filters the array of REST API paths that will be used to preloaded common data for the block editor. 697 * 698 * @since 5.8.0 699 * 700 * @param (string|string[])[] $preload_paths Array of paths to preload. 701 * @param WP_Block_Editor_Context $block_editor_context The current block editor context. 702 */ 703 $preload_paths = apply_filters( 'block_editor_rest_api_preload_paths', $preload_paths, $block_editor_context ); 704 705 if ( ! empty( $block_editor_context->post ) ) { 706 $selected_post = $block_editor_context->post; 707 708 /** 709 * Filters the array of paths that will be preloaded. 710 * 711 * Preload common data by specifying an array of REST API paths that will be preloaded. 712 * 713 * @since 5.0.0 714 * @deprecated 5.8.0 Use the {@see 'block_editor_rest_api_preload_paths'} filter instead. 715 * 716 * @param (string|string[])[] $preload_paths Array of paths to preload. 717 * @param WP_Post $selected_post Post being edited. 718 */ 719 $preload_paths = apply_filters_deprecated( 'block_editor_preload_paths', array( $preload_paths, $selected_post ), '5.8.0', 'block_editor_rest_api_preload_paths' ); 720 } 721 722 if ( empty( $preload_paths ) ) { 723 return; 724 } 725 726 /* 727 * Ensure the global $post, $wp_scripts, and $wp_styles remain the same after 728 * API data is preloaded. 729 * Because API preloading can call the_content and other filters, plugins 730 * can unexpectedly modify the global $post or enqueue assets which are not 731 * intended for the block editor. 732 */ 733 $backup_global_post = ! empty( $post ) ? clone $post : $post; 734 $backup_wp_scripts = ! empty( $wp_scripts ) ? clone $wp_scripts : $wp_scripts; 735 $backup_wp_styles = ! empty( $wp_styles ) ? clone $wp_styles : $wp_styles; 736 737 foreach ( $preload_paths as &$path ) { 738 if ( is_string( $path ) && ! str_starts_with( $path, '/' ) ) { 739 $path = '/' . $path; 740 continue; 741 } 742 743 if ( is_array( $path ) && is_string( $path[0] ) && ! str_starts_with( $path[0], '/' ) ) { 744 $path[0] = '/' . $path[0]; 745 } 746 } 747 748 unset( $path ); 749 750 $preload_data = array_reduce( 751 $preload_paths, 752 'rest_preload_api_request', 753 array() 754 ); 755 756 // Restore the global $post, $wp_scripts, and $wp_styles as they were before API preloading. 757 $post = $backup_global_post; 758 $wp_scripts = $backup_wp_scripts; 759 $wp_styles = $backup_wp_styles; 760 761 wp_add_inline_script( 762 'wp-api-fetch', 763 sprintf( 764 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', 765 wp_json_encode( $preload_data ) 766 ), 767 'after' 768 ); 769 } 770 771 /** 772 * Creates an array of theme styles to load into the block editor. 773 * 774 * @since 5.8.0 775 * 776 * @global array $editor_styles 777 * 778 * @return array An array of theme styles for the block editor. 779 */ 780 function get_block_editor_theme_styles() { 781 global $editor_styles; 782 783 $styles = array(); 784 785 if ( $editor_styles && current_theme_supports( 'editor-styles' ) ) { 786 foreach ( $editor_styles as $style ) { 787 if ( preg_match( '~^(https?:)?//~', $style ) ) { 788 $response = wp_remote_get( $style ); 789 if ( ! is_wp_error( $response ) ) { 790 $styles[] = array( 791 'css' => wp_remote_retrieve_body( $response ), 792 '__unstableType' => 'theme', 793 'isGlobalStyles' => false, 794 ); 795 } 796 } else { 797 $file = get_theme_file_path( $style ); 798 if ( is_file( $file ) ) { 799 $styles[] = array( 800 'css' => file_get_contents( $file ), 801 'baseURL' => get_theme_file_uri( $style ), 802 '__unstableType' => 'theme', 803 'isGlobalStyles' => false, 804 ); 805 } 806 } 807 } 808 } 809 810 return $styles; 811 } 812 813 /** 814 * Returns the classic theme supports settings for block editor. 815 * 816 * @since 6.2.0 817 * 818 * @return array The classic theme supports settings. 819 */ 820 function get_classic_theme_supports_block_editor_settings() { 821 $theme_settings = array( 822 'disableCustomColors' => get_theme_support( 'disable-custom-colors' ), 823 'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ), 824 'disableCustomGradients' => get_theme_support( 'disable-custom-gradients' ), 825 'disableLayoutStyles' => get_theme_support( 'disable-layout-styles' ), 826 'enableCustomLineHeight' => get_theme_support( 'custom-line-height' ), 827 'enableCustomSpacing' => get_theme_support( 'custom-spacing' ), 828 'enableCustomUnits' => get_theme_support( 'custom-units' ), 829 ); 830 831 // Theme settings. 832 $color_palette = current( (array) get_theme_support( 'editor-color-palette' ) ); 833 if ( false !== $color_palette ) { 834 $theme_settings['colors'] = $color_palette; 835 } 836 837 $font_sizes = current( (array) get_theme_support( 'editor-font-sizes' ) ); 838 if ( false !== $font_sizes ) { 839 $theme_settings['fontSizes'] = $font_sizes; 840 } 841 842 $gradient_presets = current( (array) get_theme_support( 'editor-gradient-presets' ) ); 843 if ( false !== $gradient_presets ) { 844 $theme_settings['gradients'] = $gradient_presets; 845 } 846 847 return $theme_settings; 848 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Wed Apr 24 08:20:01 2024 | Cross-referenced by PHPXref |