Skip to content

Commit

Permalink
Block theme: Update pattern workaround to use template filters. (#686)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ryelle authored May 6, 2024
1 parent d895a72 commit f7ffec0
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 61 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
],
"require": {
"composer/installers": "~1.0",
"wpackagist-plugin/gutenberg": "17.8.1",
"wpackagist-plugin/gutenberg": "*",
"wpackagist-plugin/stream": "*",
"wpackagist-plugin/wordpress-importer": "*",
"wordpress-meta/wporg": "1",
Expand Down
84 changes: 57 additions & 27 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace WordPressdotorg\Theme\Pattern_Directory_2024\Block_Config;

use WP_Block_Supports;
use const WordPressdotorg\Pattern_Directory\Pattern_Post_Type\POST_TYPE;
use function WordPressdotorg\Theme\Pattern_Directory_2024\get_patterns_count;

add_action( 'init', __NAMESPACE__ . '\register_block_bindings' );
Expand All @@ -18,7 +19,8 @@
add_filter( 'render_block_core/query-title', __NAMESPACE__ . '\update_archive_title', 10, 3 );
add_filter( 'render_block_core/site-title', __NAMESPACE__ . '\update_site_title', 10, 3 );
add_filter( 'wporg_block_site_breadcrumbs', __NAMESPACE__ . '\update_site_breadcrumbs' );
add_filter( 'render_block_data', __NAMESPACE__ . '\modify_pattern_include' );
add_filter( 'page_template_hierarchy', __NAMESPACE__ . '\modify_page_template' );
add_filter( 'single_template_hierarchy', __NAMESPACE__ . '\modify_single_template' );

/**
* Register block bindings.
Expand Down Expand Up @@ -535,37 +537,30 @@ function update_site_breadcrumbs( $breadcrumbs ) {
}

/**
* Update header template based on current query.
* Switch to the "anon" templates when logged out for favorites and my patterns.
*
* @param array $parsed_block The block being rendered.
*
* @return array The updated block.
* @param string[] $templates A list of template candidates, in descending order of priority.
*/
function modify_pattern_include( $parsed_block ) {
if ( 'core/pattern' !== $parsed_block['blockName'] || empty( $parsed_block['attrs']['slug'] ) ) {
return $parsed_block;
}

if (
'wporg-pattern-directory-2024/single-pattern' === $parsed_block['attrs']['slug'] &&
get_current_user_id() === get_the_author_meta( 'ID' )
) {
$parsed_block['attrs']['slug'] = 'wporg-pattern-directory-2024/single-my-pattern';
}

if (
'wporg-pattern-directory-2024/grid-mine' === $parsed_block['attrs']['slug'] &&
! get_current_user_id()
) {
$parsed_block['attrs']['slug'] = 'wporg-pattern-directory-2024/logged-out-patterns';
function modify_page_template( $templates ) {
if ( ! get_current_user_id() ) {
if ( is_page( 'favorites' ) ) {
array_unshift( $templates, 'page-favorites-anon.html' );
} else if ( is_page( 'my-patterns' ) ) {
array_unshift( $templates, 'page-my-patterns-anon.html' );
}
}
return $templates;
}

if (
'wporg-pattern-directory-2024/grid-favorites' === $parsed_block['attrs']['slug'] &&
! get_current_user_id()
) {
$parsed_block['attrs']['slug'] = 'wporg-pattern-directory-2024/logged-out-favorites';
/**
* Switch to the single-mine.html template on patterns owned by the current user.
*
* @param string[] $templates A list of template candidates, in descending order of priority.
*/
function modify_single_template( $templates ) {
$pattern = get_post();
if ( POST_TYPE === get_post_type() && get_current_user_id() === (int) $pattern->post_author ) {
array_unshift( $templates, 'single-mine.html' );
}

return $parsed_block;
return $templates;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
$register_url = wp_registration_url();

?>
<!-- wp:group {"layout":{"type":"constrained","contentSize":"30rem","justifyContent":"left"}} -->
<div class="wp-block-group">
<!-- wp:group {"layout":{"type":"constrained","contentSize":"30rem","justifyContent":"left"},"align":"wide"} -->
<div class="wp-block-group alignwide">
<!-- wp:paragraph -->
<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>
<!-- /wp:paragraph -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
$register_url = wp_registration_url();

?>
<!-- wp:group {"layout":{"type":"constrained","contentSize":"30rem","justifyContent":"left"}} -->
<div class="wp-block-group">
<!-- wp:group {"layout":{"type":"constrained","contentSize":"30rem","justifyContent":"left"},"align":"wide"} -->
<div class="wp-block-group alignwide">
<!-- wp:paragraph -->
<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>
<!-- /wp:paragraph -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!-- wp:template-part {"slug":"header","className":"has-display-contents"} /-->

<!-- wp:group {"tagName":"main","align":"full","layout":{"type":"constrained"}} -->
<main class="wp-block-group alignfull">

<!-- 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"}} -->
<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)">
<!-- wp:post-title {"align":"wide","level":1,"fontSize":"heading-3","style":{"spacing":{"margin":{"bottom":"var:preset|spacing|30"}}}} /-->

<!-- wp:pattern {"slug":"wporg-pattern-directory-2024/logged-out-favorites"} /-->
</div>
<!-- /wp:group -->
</main>
<!-- /wp:group -->

<!-- wp:template-part {"slug":"footer"} /-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!-- wp:template-part {"slug":"header","className":"has-display-contents"} /-->

<!-- wp:group {"tagName":"main","align":"full","layout":{"type":"constrained"}} -->
<main class="wp-block-group alignfull">

<!-- 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"}} -->
<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)">
<!-- wp:post-title {"align":"wide","level":1,"fontSize":"heading-3","style":{"spacing":{"margin":{"bottom":"var:preset|spacing|30"}}}} /-->

<!-- wp:pattern {"slug":"wporg-pattern-directory-2024/logged-out-patterns"} /-->
</div>
<!-- /wp:group -->
</main>
<!-- /wp:group -->

<!-- wp:template-part {"slug":"footer"} /-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!-- wp:template-part {"slug":"header","className":"has-display-contents"} /-->

<!-- wp:group {"tagName":"main","style":{"spacing":{"padding":{"left":"var:preset|spacing|edge-space","right":"var:preset|spacing|edge-space"}}},"align":"full","layout":{"type":"constrained"}} -->
<main class="wp-block-group alignfull" style="padding-right:var(--wp--preset--spacing--edge-space);padding-left:var(--wp--preset--spacing--edge-space)">
<!-- wp:pattern {"slug":"wporg-pattern-directory-2024/single-my-pattern"} /-->
</main>
<!-- /wp:group -->

<!-- wp:template-part {"slug":"footer"} /-->

0 comments on commit f7ffec0

Please sign in to comment.