Skip to content

Commit

Permalink
Block theme support for Create page.
Browse files Browse the repository at this point in the history
The technique previously introduced to support the Docs directory
in block themes worked because the `bp_doc` archive query successfully
matched at least one item. It's important that the query match at least
one item, otherwise the search for the `post-excerpt` string will never
succeed, since the query block will render to show a `query-no-results`
element instead. This limitation in the technique meant that the Create
page would not work in block themes, since the `post_type=bp_doc&create=1`
query would never match any items.

BuddyPress's theme compatibility layer intends to work around the
404/no-results issue by using `bp_theme_compat_reset_post()`. This
function resets WP's global query with a "dummy" post object, which
in traditional themes tricks the theme into thinking that the query
matches a result. The technique doesn't work properly in block themes,
because of load order: In a block theme using the `core/query` block
and the 'inherit' attribute, the query is accessed at the
`template_include` hook. This causes race conditions with BP's
"dependency" system that loads theme compatibility at 'bp_template_include'.

The workaround is to run the theme-compatibility reset immediately
before running the template content through `render_block()` in the Docs
theme compatibility layer. This ensures that the dummy post is set
at the time that WP renders the `core/query` block, avoiding "no results".

See #728.
  • Loading branch information
boonebgorges committed Dec 20, 2023
1 parent 3562224 commit 46ea36f
Showing 1 changed file with 47 additions and 30 deletions.
77 changes: 47 additions & 30 deletions includes/theme-bridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,7 @@ function query_templates( $templates ) {
* @since 1.3
*/
public function directory_dummy_post() {
bp_theme_compat_reset_post( array(
'ID' => 0,
'post_title' => bp_docs_get_docs_directory_title(),
'post_author' => 0,
'post_date' => 0,
'post_content' => '',
'post_type' => bp_docs_get_post_type_name(),
'post_status' => 'publish',
'is_archive' => true,
'comment_status' => 'closed'
) );
bp_docs_theme_compat_reset_post( 'directory' );
}

/**
Expand Down Expand Up @@ -286,17 +276,7 @@ public function single_content() {
* @since 1.3
*/
public function create_dummy_post() {
bp_theme_compat_reset_post( array(
'ID' => 0,
'post_title' => __( 'Create a Doc', 'buddypress-docs' ),
'post_author' => get_current_user_id(),
'post_date' => 0,
'post_content' => '',
'post_type' => bp_docs_get_post_type_name(),
'post_status' => 'publish',
'is_archive' => true,
'comment_status' => 'closed'
) );
bp_docs_theme_compat_reset_post( 'create' );
}

/**
Expand All @@ -310,6 +290,36 @@ public function create_content() {
}
new BP_Docs_Theme_Compat();

/**
* Resets the global $post object to a dummy post.
*
* @since 2.2.1
*
* @param string $type 'create' or 'directory'.
* @return void
*/
function bp_docs_theme_compat_reset_post( $type ) {
$post_args = [
'ID' => 0,
'post_date' => 0,
'post_content' => '',
'post_type' => bp_docs_get_post_type_name(),
'post_status' => 'publish',
'is_archive' => true,
'comment_status' => 'closed'
];

if ( 'create' === $type ) {
$post_args['post_title'] = __( 'Create a Doc', 'buddypress-docs' );
$post_args['post_author'] = get_current_user_id();
} elseif ( 'directory' === $type ) {
$post_args['post_title'] = bp_docs_get_docs_directory_title();
$post_args['post_author'] = 0;
}

bp_theme_compat_reset_post( $post_args );
}

/**
* Wrapper function for bp_is_theme_compat_active()
*
Expand All @@ -329,13 +339,14 @@ function bp_docs_is_theme_compat_active() {
}

/**
* Provides a stub block template for the Docs directory.
* Provides a stub block template for the Docs directory and create pages.
*
* On block themes, the Docs directory is powered by the archive.html template.
* In many block themes, including WP default themes, archive.html shows only
* an excerpt from the posts, rather than the full content. This breaks our
* theme compatibility technique, which requires that the untruncated content
* returned by the 'the_content' filter be displayed on the CPT archive.
* On block themes, the Docs directory and create pages are powered by the
* archive.html template. In many block themes, including WP default themes,
* archive.html shows only an excerpt from the posts, rather than the full
* content. This breaks our theme compatibility technique, which requires
* that the untruncated content returned by the 'the_content' filter be
* displayed on the CPT archive.
*
* As a workaround, and to provide maximal compatibility with the rest of the
* theme, we detect whether the archive template shows only excerpts. If so,
Expand All @@ -348,7 +359,7 @@ function bp_docs_is_theme_compat_active() {
* @param array $query Query arguments.
* @return array
*/
function bp_docs_provide_block_template_for_docs_directory( $templates, $query ) {
function bp_docs_provide_block_template_for_docs_content( $templates, $query ) {
// We are only concerned with the Docs directory, ie the bp_doc archive.
if ( empty( $query['slug__in'] ) || ! in_array( 'archive-bp_doc', $query['slug__in'], true ) ) {
return $templates;
Expand All @@ -363,6 +374,12 @@ function bp_docs_provide_block_template_for_docs_directory( $templates, $query )
}
}

if ( bp_docs_is_doc_create() ) {
bp_docs_theme_compat_reset_post( 'create' );
} else {
bp_docs_theme_compat_reset_post( 'directory' );
}

// Render the top template.
$rendered = do_blocks( $templates[0]->content );

Expand Down Expand Up @@ -413,7 +430,7 @@ function bp_docs_provide_block_template_for_docs_directory( $templates, $query )

return $templates;
}
add_filter( 'get_block_templates', 'bp_docs_provide_block_template_for_docs_directory', 10, 2 );
add_filter( 'get_block_templates', 'bp_docs_provide_block_template_for_docs_content', 10, 2 );

/**
* Finds the most deeply nested block whose rendered content contains a post excerpt block.
Expand Down

0 comments on commit 46ea36f

Please sign in to comment.