| @@ -52,70 +52,63 @@ | ||
| 52 | 52 | 'video', |
| 53 | 53 | 'embed', |
| 54 | 54 | ), |
| 55 | 55 | 'block_names' => array( |
| 56 | - 'archives.php' => 'core/archives', | |
| 57 | - 'block.php' => 'core/block', | |
| 58 | - 'calendar.php' => 'core/calendar', | |
| 59 | - 'categories.php' => 'core/categories', | |
| 60 | - 'comment-author-avatar.php' => 'core/comment-author-avatar', | |
| 61 | - 'comment-author-name.php' => 'core/comment-author-name', | |
| 62 | - 'comment-content.php' => 'core/comment-content', | |
| 63 | - 'comment-date.php' => 'core/comment-date', | |
| 64 | - 'comment-edit-link.php' => 'core/comment-edit-link', | |
| 65 | - 'comment-reply-link.php' => 'core/comment-reply-link', | |
| 66 | - 'comment-template.php' => 'core/comment-template', | |
| 67 | - 'comments-pagination.php' => 'core/comments-pagination', | |
| 68 | - 'comments-pagination-next.php' => 'core/comments-pagination-next', | |
| 69 | - 'comments-pagination-numbers.php' => 'core/comments-pagination-numbers', | |
| 70 | - 'comments-pagination-previous.php' => 'core/comments-pagination-previous', | |
| 71 | - 'file.php' => 'core/file', | |
| 72 | - 'home-link.php' => 'core/home-link', | |
| 73 | - 'image.php' => 'core/image', | |
| 74 | - 'gallery.php' => 'core/gallery', | |
| 75 | - 'latest-comments.php' => 'core/latest-comments', | |
| 76 | - 'latest-posts.php' => 'core/latest-posts', | |
| 77 | - 'loginout.php' => 'core/loginout', | |
| 78 | - 'navigation.php' => 'core/navigation', | |
| 79 | - 'navigation-area.php' => 'core/navigation-area', | |
| 80 | - 'navigation-link.php' => 'core/navigation-link', | |
| 81 | - 'navigation-submenu.php' => 'core/navigation-submenu', | |
| 82 | - 'page-list.php' => 'core/page-list', | |
| 83 | - 'pattern.php' => 'core/pattern', | |
| 84 | - 'post-author.php' => 'core/post-author', | |
| 85 | - 'post-author-name.php' => 'core/post-author-name', | |
| 86 | - 'post-author-biography.php' => 'core/post-author-biography', | |
| 87 | - 'post-comment.php' => 'core/post-comment', | |
| 88 | - 'post-comments.php' => 'core/post-comments', | |
| 89 | - 'post-comments-count.php' => 'core/post-comments-count', | |
| 90 | - 'post-comments-form.php' => 'core/post-comments-form', | |
| 91 | - 'post-comments-link.php' => 'core/post-comments-link', | |
| 92 | - 'post-content.php' => 'core/post-content', | |
| 93 | - 'post-date.php' => 'core/post-date', | |
| 94 | - 'post-excerpt.php' => 'core/post-excerpt', | |
| 95 | - 'post-featured-image.php' => 'core/post-featured-image', | |
| 96 | - 'post-navigation-link.php' => 'core/post-navigation-link', | |
| 97 | - 'post-terms.php' => 'core/post-terms', | |
| 98 | - 'post-title.php' => 'core/post-title', | |
| 99 | - 'query.php' => 'core/query', | |
| 100 | - 'post-template.php' => 'core/post-template', | |
| 101 | - 'query-pagination.php' => 'core/query-pagination', | |
| 102 | - 'query-pagination-next.php' => 'core/query-pagination-next', | |
| 103 | - 'query-pagination-numbers.php' => 'core/query-pagination-numbers', | |
| 104 | - 'query-pagination-previous.php' => 'core/query-pagination-previous', | |
| 105 | - 'query-title.php' => 'core/query-title', | |
| 106 | - 'read-more.php' => 'core/read-more', | |
| 107 | - 'rss.php' => 'core/rss', | |
| 108 | - 'search.php' => 'core/search', | |
| 109 | - 'shortcode.php' => 'core/shortcode', | |
| 110 | - 'social-link.php' => 'core/social-link', | |
| 111 | - 'site-logo.php' => 'core/site-logo', | |
| 112 | - 'site-tagline.php' => 'core/site-tagline', | |
| 113 | - 'site-title.php' => 'core/site-title', | |
| 114 | - // 'table-of-contents.php' => 'core/table-of-contents', | |
| 115 | - 'tag-cloud.php' => 'core/tag-cloud', | |
| 116 | - 'template-part.php' => 'core/template-part', | |
| 117 | - 'term-description.php' => 'core/term-description', | |
| 56 | + 'archives.php' => 'core/archives', | |
| 57 | + 'block.php' => 'core/block', | |
| 58 | + 'calendar.php' => 'core/calendar', | |
| 59 | + 'categories.php' => 'core/categories', | |
| 60 | + 'comment-author-avatar.php' => 'core/comment-author-avatar', | |
| 61 | + 'comment-author-name.php' => 'core/comment-author-name', | |
| 62 | + 'comment-content.php' => 'core/comment-content', | |
| 63 | + 'comment-date.php' => 'core/comment-date', | |
| 64 | + 'comment-edit-link.php' => 'core/comment-edit-link', | |
| 65 | + 'comment-reply-link.php' => 'core/comment-reply-link', | |
| 66 | + 'comment-template.php' => 'core/comment-template', | |
| 67 | + 'file.php' => 'core/file', | |
| 68 | + 'home-link.php' => 'core/home-link', | |
| 69 | + 'image.php' => 'core/image', | |
| 70 | + 'gallery.php' => 'core/gallery', | |
| 71 | + 'latest-comments.php' => 'core/latest-comments', | |
| 72 | + 'latest-posts.php' => 'core/latest-posts', | |
| 73 | + 'loginout.php' => 'core/loginout', | |
| 74 | + 'navigation.php' => 'core/navigation', | |
| 75 | + 'navigation-area.php' => 'core/navigation-area', | |
| 76 | + 'navigation-link.php' => 'core/navigation-link', | |
| 77 | + 'navigation-submenu.php' => 'core/navigation-submenu', | |
| 78 | + 'page-list.php' => 'core/page-list', | |
| 79 | + 'pattern.php' => 'core/pattern', | |
| 80 | + 'post-author.php' => 'core/post-author', | |
| 81 | + 'post-comment.php' => 'core/post-comment', | |
| 82 | + 'post-comments.php' => 'core/post-comments', | |
| 83 | + 'post-comments-count.php' => 'core/post-comments-count', | |
| 84 | + 'post-comments-form.php' => 'core/post-comments-form', | |
| 85 | + 'post-comments-link.php' => 'core/post-comments-link', | |
| 86 | + 'post-content.php' => 'core/post-content', | |
| 87 | + 'post-date.php' => 'core/post-date', | |
| 88 | + 'post-excerpt.php' => 'core/post-excerpt', | |
| 89 | + 'post-featured-image.php' => 'core/post-featured-image', | |
| 90 | + 'post-navigation-link.php' => 'core/post-navigation-link', | |
| 91 | + 'post-terms.php' => 'core/post-terms', | |
| 92 | + 'post-title.php' => 'core/post-title', | |
| 93 | + 'query.php' => 'core/query', | |
| 94 | + 'post-template.php' => 'core/post-template', | |
| 95 | + 'query-pagination.php' => 'core/query-pagination', | |
| 96 | + 'query-pagination-next.php' => 'core/query-pagination-next', | |
| 97 | + 'query-pagination-numbers.php' => 'core/query-pagination-numbers', | |
| 98 | + 'query-pagination-previous.php' => 'core/query-pagination-previous', | |
| 99 | + 'query-title.php' => 'core/query-title', | |
| 100 | + 'rss.php' => 'core/rss', | |
| 101 | + 'search.php' => 'core/search', | |
| 102 | + 'shortcode.php' => 'core/shortcode', | |
| 103 | + 'social-link.php' => 'core/social-link', | |
| 104 | + 'site-logo.php' => 'core/site-logo', | |
| 105 | + 'site-tagline.php' => 'core/site-tagline', | |
| 106 | + 'site-title.php' => 'core/site-title', | |
| 107 | + // 'table-of-contents.php' => 'core/table-of-contents', | |
| 108 | + 'tag-cloud.php' => 'core/tag-cloud', | |
| 109 | + 'template-part.php' => 'core/template-part', | |
| 110 | + 'term-description.php' => 'core/term-description', | |
| 118 | 111 | ), |
| 119 | 112 | ), |
| 120 | 113 | __DIR__ . '/../build/edit-widgets/blocks/' => array( |
| 121 | 114 | 'block_folders' => array( |
| @@ -273,8 +266,97 @@ | ||
| 273 | 266 | wp_register_style( "wp-block-{$block_name}-editor", false ); |
| 274 | 267 | } |
| 275 | 268 | } |
| 276 | 269 | |
| 270 | +/** | |
| 271 | + * Change the way styles get loaded depending on their size. | |
| 272 | + * | |
| 273 | + * Optimizes performance and sustainability of styles by inlining smaller stylesheets. | |
| 274 | + * | |
| 275 | + * @todo Remove this function when the minimum supported version is WordPress 5.8. | |
| 276 | + * | |
| 277 | + * @return void | |
| 278 | + */ | |
| 279 | +function gutenberg_maybe_inline_styles() { | |
| 280 | + | |
| 281 | + // Early exit if the "wp_maybe_inline_styles" function exists. | |
| 282 | + if ( function_exists( 'wp_maybe_inline_styles' ) ) { | |
| 283 | + return; | |
| 284 | + } | |
| 285 | + | |
| 286 | + $total_inline_limit = 20000; | |
| 287 | + /** | |
| 288 | + * The maximum size of inlined styles in bytes. | |
| 289 | + * | |
| 290 | + * @param int $total_inline_limit The file-size threshold, in bytes. Defaults to 20000. | |
| 291 | + * @return int The file-size threshold, in bytes. | |
| 292 | + */ | |
| 293 | + $total_inline_limit = apply_filters( 'styles_inline_size_limit', $total_inline_limit ); | |
| 294 | + | |
| 295 | + global $wp_styles; | |
| 296 | + $styles = array(); | |
| 297 | + | |
| 298 | + // Build an array of styles that have a path defined. | |
| 299 | + foreach ( $wp_styles->queue as $handle ) { | |
| 300 | + if ( wp_styles()->get_data( $handle, 'path' ) && file_exists( $wp_styles->registered[ $handle ]->extra['path'] ) ) { | |
| 301 | + $styles[] = array( | |
| 302 | + 'handle' => $handle, | |
| 303 | + 'src' => $wp_styles->registered[ $handle ]->src, | |
| 304 | + 'path' => $wp_styles->registered[ $handle ]->extra['path'], | |
| 305 | + 'size' => filesize( $wp_styles->registered[ $handle ]->extra['path'] ), | |
| 306 | + ); | |
| 307 | + } | |
| 308 | + } | |
| 309 | + | |
| 310 | + if ( ! empty( $styles ) ) { | |
| 311 | + // Reorder styles array based on size. | |
| 312 | + usort( | |
| 313 | + $styles, | |
| 314 | + function( $a, $b ) { | |
| 315 | + return ( $a['size'] <= $b['size'] ) ? -1 : 1; | |
| 316 | + } | |
| 317 | + ); | |
| 318 | + | |
| 319 | + /** | |
| 320 | + * The total inlined size. | |
| 321 | + * | |
| 322 | + * On each iteration of the loop, if a style gets added inline the value of this var increases | |
| 323 | + * to reflect the total size of inlined styles. | |
| 324 | + */ | |
| 325 | + $total_inline_size = 0; | |
| 326 | + | |
| 327 | + // Loop styles. | |
| 328 | + foreach ( $styles as $style ) { | |
| 329 | + | |
| 330 | + // Size check. Since styles are ordered by size, we can break the loop. | |
| 331 | + if ( $total_inline_size + $style['size'] > $total_inline_limit ) { | |
| 332 | + break; | |
| 333 | + } | |
| 334 | + | |
| 335 | + // Get the styles if we don't already have them. | |
| 336 | + $style['css'] = file_get_contents( $style['path'] ); | |
| 337 | + | |
| 338 | + // Check if the style contains relative URLs that need to be modified. | |
| 339 | + // URLs relative to the stylesheet's path should be converted to relative to the site's root. | |
| 340 | + $style['css'] = _wp_normalize_relative_css_links( $style['css'], $style['src'] ); | |
| 341 | + | |
| 342 | + // Set `src` to `false` and add styles inline. | |
| 343 | + $wp_styles->registered[ $style['handle'] ]->src = false; | |
| 344 | + if ( empty( $wp_styles->registered[ $style['handle'] ]->extra['after'] ) ) { | |
| 345 | + $wp_styles->registered[ $style['handle'] ]->extra['after'] = array(); | |
| 346 | + } | |
| 347 | + array_unshift( $wp_styles->registered[ $style['handle'] ]->extra['after'], $style['css'] ); | |
| 348 | + | |
| 349 | + // Add the styles size to the $total_inline_size var. | |
| 350 | + $total_inline_size += (int) $style['size']; | |
| 351 | + } | |
| 352 | + } | |
| 353 | +} | |
| 354 | +// Run for styles enqueued in <head>. | |
| 355 | +add_action( 'wp_head', 'gutenberg_maybe_inline_styles', 1 ); | |
| 356 | +// Run for late-loaded styles in the footer. | |
| 357 | +add_action( 'wp_footer', 'gutenberg_maybe_inline_styles', 1 ); | |
| 358 | + | |
| 277 | 359 | if ( ! function_exists( '_wp_normalize_relative_css_links' ) ) { |
| 278 | 360 | /** |
| 279 | 361 | * Make URLs relative to the WordPress installation. |
| 280 | 362 | * |
| @@ -396,8 +478,42 @@ | ||
| 396 | 478 | |
| 397 | 479 | add_action( 'init', 'gutenberg_register_legacy_social_link_blocks' ); |
| 398 | 480 | |
| 399 | 481 | /** |
| 482 | + * Filters the default block categories array to add a new one for themes. | |
| 483 | + * | |
| 484 | + * This can be removed when plugin support requires WordPress 5.8.0+. | |
| 485 | + * | |
| 486 | + * @see https://core.trac.wordpress.org/ticket/52883 | |
| 487 | + * | |
| 488 | + * @param array[] $categories The list of default block categories. | |
| 489 | + * | |
| 490 | + * @return array[] Filtered block categories. | |
| 491 | + */ | |
| 492 | +function gutenberg_register_theme_block_category( $categories ) { | |
| 493 | + foreach ( $categories as $category ) { | |
| 494 | + // Skip when the category is already set in WordPress core. | |
| 495 | + if ( | |
| 496 | + isset( $category['slug'] ) && | |
| 497 | + 'theme' === $category['slug'] | |
| 498 | + ) { | |
| 499 | + return $categories; | |
| 500 | + } | |
| 501 | + } | |
| 502 | + | |
| 503 | + $categories[] = array( | |
| 504 | + 'slug' => 'theme', | |
| 505 | + 'title' => _x( 'Theme', 'block category', 'gutenberg' ), | |
| 506 | + 'icon' => null, | |
| 507 | + ); | |
| 508 | + return $categories; | |
| 509 | +} | |
| 510 | +// This can be removed when plugin support requires WordPress 5.8.0+. | |
| 511 | +if ( ! function_exists( 'get_default_block_categories' ) ) { | |
| 512 | + add_filter( 'block_categories', 'gutenberg_register_theme_block_category' ); | |
| 513 | +} | |
| 514 | + | |
| 515 | +/** | |
| 400 | 516 | * Checks whether the current block type supports the feature requested. |
| 401 | 517 | * |
| 402 | 518 | * @param WP_Block_Type $block_type Block type to check for support. |
| 403 | 519 | * @param array $feature Path of the feature to check support for. |
| @@ -483,11 +599,12 @@ | ||
| 483 | 599 | * |
| 484 | 600 | * @param string $content When the callback is used for the render_block filter, |
| 485 | 601 | * the content needs to be returned so the function parameter |
| 486 | 602 | * is to ensure the content exists. |
| 487 | - * @return string Block content. | |
| 603 | + * | |
| 604 | + * @return string | |
| 488 | 605 | */ |
| 489 | - $callback = static function( $content ) use ( $args ) { | |
| 606 | + $callback = function( $content ) use ( $args ) { | |
| 490 | 607 | // Register the stylesheet. |
| 491 | 608 | if ( ! empty( $args['src'] ) ) { |
| 492 | 609 | wp_register_style( $args['handle'], $args['src'], $args['deps'], $args['ver'], $args['media'] ); |
| 493 | 610 | } |
| @@ -500,9 +617,9 @@ | ||
| 500 | 617 | $rtl_file_path = str_replace( '.css', '-rtl.css', $args['path'] ); |
| 501 | 618 | |
| 502 | 619 | // Add RTL stylesheet. |
| 503 | 620 | if ( file_exists( $rtl_file_path ) ) { |
| 504 | - wp_style_add_data( $args['handle'], 'rtl', 'replace' ); | |
| 621 | + wp_style_add_data( $args['hanle'], 'rtl', 'replace' ); | |
| 505 | 622 | |
| 506 | 623 | if ( is_rtl() ) { |
| 507 | 624 | wp_style_add_data( $args['handle'], 'path', $rtl_file_path ); |
| 508 | 625 | } |
| @@ -516,44 +633,12 @@ | ||
| 516 | 633 | }; |
| 517 | 634 | |
| 518 | 635 | $hook = did_action( 'wp_enqueue_scripts' ) ? 'wp_footer' : 'wp_enqueue_scripts'; |
| 519 | 636 | if ( wp_should_load_separate_core_block_assets() ) { |
| 520 | - /** | |
| 521 | - * Callback function to register and enqueue styles. | |
| 522 | - * | |
| 523 | - * @param string $content The block content. | |
| 524 | - * @param array $block The full block, including name and attributes. | |
| 525 | - * @return string Block content. | |
| 526 | - */ | |
| 527 | - $callback_separate = static function( $content, $block ) use ( $block_name, $callback ) { | |
| 528 | - if ( ! empty( $block['blockName'] ) && $block_name === $block['blockName'] ) { | |
| 529 | - return $callback( $content ); | |
| 530 | - } | |
| 531 | - return $content; | |
| 532 | - }; | |
| 533 | - | |
| 534 | - /* | |
| 535 | - * The filter's callback here is an anonymous function because | |
| 536 | - * using a named function in this case is not possible. | |
| 537 | - * | |
| 538 | - * The function cannot be unhooked, however, users are still able | |
| 539 | - * to dequeue the stylesheets registered/enqueued by the callback | |
| 540 | - * which is why in this case, using an anonymous function | |
| 541 | - * was deemed acceptable. | |
| 542 | - */ | |
| 543 | - add_filter( 'render_block', $callback_separate, 10, 2 ); | |
| 544 | - return; | |
| 637 | + $hook = "render_block_$block_name"; | |
| 545 | 638 | } |
| 546 | 639 | |
| 547 | - /* | |
| 548 | - * The filter's callback here is an anonymous function because | |
| 549 | - * using a named function in this case is not possible. | |
| 550 | - * | |
| 551 | - * The function cannot be unhooked, however, users are still able | |
| 552 | - * to dequeue the stylesheets registered/enqueued by the callback | |
| 553 | - * which is why in this case, using an anonymous function | |
| 554 | - * was deemed acceptable. | |
| 555 | - */ | |
| 640 | + // Enqueue assets in the frontend. | |
| 556 | 641 | add_filter( $hook, $callback ); |
| 557 | 642 | |
| 558 | 643 | // Enqueue assets in the editor. |
| 559 | 644 | add_action( 'enqueue_block_assets', $callback ); |