Skip to content

Commit f7ffec0

Browse files
authored
Block theme: Update pattern workaround to use template filters. (#686)
* Block theme: Update pattern workaround to use template filters. Fixes #662 — This removes the pattern slug filtering in favor of adding extra custom templates, and filtering to inject those into the hierarchy. This is a more core-native way to handle template overrides, so it will be less likely to break in the future. * Fix the content alignment of logged out messages * Tools: Update packages, unpin Gutenberg from composer
1 parent d895a72 commit f7ffec0

8 files changed

Lines changed: 127 additions & 61 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
],
6464
"require": {
6565
"composer/installers": "~1.0",
66-
"wpackagist-plugin/gutenberg": "17.8.1",
66+
"wpackagist-plugin/gutenberg": "*",
6767
"wpackagist-plugin/stream": "*",
6868
"wpackagist-plugin/wordpress-importer": "*",
6969
"wordpress-meta/wporg": "1",

composer.lock

Lines changed: 57 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public_html/wp-content/themes/wporg-pattern-directory-2024/inc/block-config.php

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace WordPressdotorg\Theme\Pattern_Directory_2024\Block_Config;
77

88
use WP_Block_Supports;
9+
use const WordPressdotorg\Pattern_Directory\Pattern_Post_Type\POST_TYPE;
910
use function WordPressdotorg\Theme\Pattern_Directory_2024\get_patterns_count;
1011

1112
add_action( 'init', __NAMESPACE__ . '\register_block_bindings' );
@@ -18,7 +19,8 @@
1819
add_filter( 'render_block_core/query-title', __NAMESPACE__ . '\update_archive_title', 10, 3 );
1920
add_filter( 'render_block_core/site-title', __NAMESPACE__ . '\update_site_title', 10, 3 );
2021
add_filter( 'wporg_block_site_breadcrumbs', __NAMESPACE__ . '\update_site_breadcrumbs' );
21-
add_filter( 'render_block_data', __NAMESPACE__ . '\modify_pattern_include' );
22+
add_filter( 'page_template_hierarchy', __NAMESPACE__ . '\modify_page_template' );
23+
add_filter( 'single_template_hierarchy', __NAMESPACE__ . '\modify_single_template' );
2224

2325
/**
2426
* Register block bindings.
@@ -535,37 +537,30 @@ function update_site_breadcrumbs( $breadcrumbs ) {
535537
}
536538

537539
/**
538-
* Update header template based on current query.
540+
* Switch to the "anon" templates when logged out for favorites and my patterns.
539541
*
540-
* @param array $parsed_block The block being rendered.
541-
*
542-
* @return array The updated block.
542+
* @param string[] $templates A list of template candidates, in descending order of priority.
543543
*/
544-
function modify_pattern_include( $parsed_block ) {
545-
if ( 'core/pattern' !== $parsed_block['blockName'] || empty( $parsed_block['attrs']['slug'] ) ) {
546-
return $parsed_block;
547-
}
548-
549-
if (
550-
'wporg-pattern-directory-2024/single-pattern' === $parsed_block['attrs']['slug'] &&
551-
get_current_user_id() === get_the_author_meta( 'ID' )
552-
) {
553-
$parsed_block['attrs']['slug'] = 'wporg-pattern-directory-2024/single-my-pattern';
554-
}
555-
556-
if (
557-
'wporg-pattern-directory-2024/grid-mine' === $parsed_block['attrs']['slug'] &&
558-
! get_current_user_id()
559-
) {
560-
$parsed_block['attrs']['slug'] = 'wporg-pattern-directory-2024/logged-out-patterns';
544+
function modify_page_template( $templates ) {
545+
if ( ! get_current_user_id() ) {
546+
if ( is_page( 'favorites' ) ) {
547+
array_unshift( $templates, 'page-favorites-anon.html' );
548+
} else if ( is_page( 'my-patterns' ) ) {
549+
array_unshift( $templates, 'page-my-patterns-anon.html' );
550+
}
561551
}
552+
return $templates;
553+
}
562554

563-
if (
564-
'wporg-pattern-directory-2024/grid-favorites' === $parsed_block['attrs']['slug'] &&
565-
! get_current_user_id()
566-
) {
567-
$parsed_block['attrs']['slug'] = 'wporg-pattern-directory-2024/logged-out-favorites';
555+
/**
556+
* Switch to the single-mine.html template on patterns owned by the current user.
557+
*
558+
* @param string[] $templates A list of template candidates, in descending order of priority.
559+
*/
560+
function modify_single_template( $templates ) {
561+
$pattern = get_post();
562+
if ( POST_TYPE === get_post_type() && get_current_user_id() === (int) $pattern->post_author ) {
563+
array_unshift( $templates, 'single-mine.html' );
568564
}
569-
570-
return $parsed_block;
565+
return $templates;
571566
}

public_html/wp-content/themes/wporg-pattern-directory-2024/patterns/_logged-out-favorites.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
$register_url = wp_registration_url();
1313

1414
?>
15-
<!-- wp:group {"layout":{"type":"constrained","contentSize":"30rem","justifyContent":"left"}} -->
16-
<div class="wp-block-group">
15+
<!-- wp:group {"layout":{"type":"constrained","contentSize":"30rem","justifyContent":"left"},"align":"wide"} -->
16+
<div class="wp-block-group alignwide">
1717
<!-- wp:paragraph -->
1818
<p><?php esc_html_e( 'Log in to your WordPress.org account and you&#8217;ll be able to see all your favorite patterns in one place.', 'wporg-patterns' ); ?></p>
1919
<!-- /wp:paragraph -->

public_html/wp-content/themes/wporg-pattern-directory-2024/patterns/_logged-out-patterns.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
$register_url = wp_registration_url();
1313

1414
?>
15-
<!-- wp:group {"layout":{"type":"constrained","contentSize":"30rem","justifyContent":"left"}} -->
16-
<div class="wp-block-group">
15+
<!-- wp:group {"layout":{"type":"constrained","contentSize":"30rem","justifyContent":"left"},"align":"wide"} -->
16+
<div class="wp-block-group alignwide">
1717
<!-- wp:paragraph -->
1818
<p><?php esc_html_e( 'Anyone can create and share patterns using the familiar block editor. Design helpful starting points for yourself and any WordPress site.', 'wporg-patterns' ); ?></p>
1919
<!-- /wp:paragraph -->
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!-- wp:template-part {"slug":"header","className":"has-display-contents"} /-->
2+
3+
<!-- wp:group {"tagName":"main","align":"full","layout":{"type":"constrained"}} -->
4+
<main class="wp-block-group alignfull">
5+
6+
<!-- wp:group {"align":"full","style":{"spacing":{"padding":{"left":"var:preset|spacing|edge-space","right":"var:preset|spacing|edge-space","top":"var:preset|spacing|40"}}},"layout":{"type":"constrained"}} -->
7+
<div class="wp-block-group alignfull" style="padding-top:var(--wp--preset--spacing--40);padding-right:var(--wp--preset--spacing--edge-space);padding-left:var(--wp--preset--spacing--edge-space)">
8+
<!-- wp:post-title {"align":"wide","level":1,"fontSize":"heading-3","style":{"spacing":{"margin":{"bottom":"var:preset|spacing|30"}}}} /-->
9+
10+
<!-- wp:pattern {"slug":"wporg-pattern-directory-2024/logged-out-favorites"} /-->
11+
</div>
12+
<!-- /wp:group -->
13+
</main>
14+
<!-- /wp:group -->
15+
16+
<!-- wp:template-part {"slug":"footer"} /-->
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!-- wp:template-part {"slug":"header","className":"has-display-contents"} /-->
2+
3+
<!-- wp:group {"tagName":"main","align":"full","layout":{"type":"constrained"}} -->
4+
<main class="wp-block-group alignfull">
5+
6+
<!-- wp:group {"align":"full","style":{"spacing":{"padding":{"left":"var:preset|spacing|edge-space","right":"var:preset|spacing|edge-space","top":"var:preset|spacing|40"}}},"layout":{"type":"constrained"}} -->
7+
<div class="wp-block-group alignfull" style="padding-top:var(--wp--preset--spacing--40);padding-right:var(--wp--preset--spacing--edge-space);padding-left:var(--wp--preset--spacing--edge-space)">
8+
<!-- wp:post-title {"align":"wide","level":1,"fontSize":"heading-3","style":{"spacing":{"margin":{"bottom":"var:preset|spacing|30"}}}} /-->
9+
10+
<!-- wp:pattern {"slug":"wporg-pattern-directory-2024/logged-out-patterns"} /-->
11+
</div>
12+
<!-- /wp:group -->
13+
</main>
14+
<!-- /wp:group -->
15+
16+
<!-- wp:template-part {"slug":"footer"} /-->
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!-- wp:template-part {"slug":"header","className":"has-display-contents"} /-->
2+
3+
<!-- wp:group {"tagName":"main","style":{"spacing":{"padding":{"left":"var:preset|spacing|edge-space","right":"var:preset|spacing|edge-space"}}},"align":"full","layout":{"type":"constrained"}} -->
4+
<main class="wp-block-group alignfull" style="padding-right:var(--wp--preset--spacing--edge-space);padding-left:var(--wp--preset--spacing--edge-space)">
5+
<!-- wp:pattern {"slug":"wporg-pattern-directory-2024/single-my-pattern"} /-->
6+
</main>
7+
<!-- /wp:group -->
8+
9+
<!-- wp:template-part {"slug":"footer"} /-->

0 commit comments

Comments
 (0)