-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better block theme compatibility #728
Comments
I've spent a few minutes playing with this. The default WP block themes use What feels like a "nice" solution is for Docs to provide its own template, perhaps one that it would recommend for installation when you install Docs or when you activate a block theme. But this poses its own problems, since we don't know what the rest of the template should look like - what the shared header/footer elements are in a given theme, etc. So I came up with the following technique, which is ugly, but less ugly than other options. It does the following:
Again, this is not terribly elegant, but it seems to work, and it seems not to have negative effects for other archives etc. Here's the magic: /**
* Provides a stub block template for the Docs directory.
*/
function bp_docs_provide_block_template_for_docs_directory( $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;
}
// If an archive-bp_doc template was found, use it.
$has_archive_bp_doc_template = false;
foreach ( $templates as $template ) {
if ( 'archive-bp_doc' === $template->slug ) {
$has_archive_bp_doc_template = true;
return $templates;
}
}
// If the top template already has a wp:post-content block, use it.
$first_template = reset( $templates );
if ( false !== strpos( $first_template->content, 'wp:post-content' ) ) {
return $templates;
}
// Copy the first template and swap out the wp:post-excerpt block for wp:post-content.
$new_template = clone $first_template;
$new_template->slug = 'archive-bp_doc';
$new_template->title = __( 'Docs Directory', 'buddypress-docs' );
$new_template->content = str_replace( 'wp:post-excerpt', 'wp:post-content', $new_template->content );
// Add the new template to the top of the list.
array_unshift( $templates, $new_template );
return $templates;
}
add_filter( 'get_block_templates', 'bp_docs_provide_block_template_for_docs_directory', 10, 2 ); @dcavins Since you've done some testing related to #738, perhaps you could run this across a couple different theme configurations to see whether you can break it. |
@imath had a good suggestion for how to handle this: I spent some time going down this road, and it seems promising, but the template hierarchy/filters in block themes are really different, and I was having trouble finding the right combination to use. I learned some odd things, though, like, with 2022, the parameter being passed into the I will continue to pursue this, also. You can see my first try in https://github.com/dcavins/buddypress-docs/tree/wider-layout-support |
Hm. When you say On a lark, I forced WP to use I'm wary of a solution - That being said, I could be looking in the wrong place, so I'm happy to be shown how the other approach could work :) |
Hi @boonebgorges & @dcavins This is an interesting ticket 😄 . I like @boonebgorges approach about replacing the serialized block. FWIW when starting working on a Full Block Theme for BP, I've found that the Block Template hierarchy is "copying" the regular Template hierarchy simply replacing the |
…lates. Our technique involves providing a "fake" template that copies the archive template and swaps out the excerpt block for a content block. See #728.
@imath Thanks for your thoughts. I've pushed up 6097e82, which contains my fix with some improved documentation. I've included it in the branch for #738 because the problems are ideally fixed together. Thinking more about this, I think that the long-term "proper" fix for a plugin like buddypress-docs is to provide a real
I think that many of these questions will have to be answered by BuddyPress in one way or another, though in the case of BP it'll be much more complicated - BP has more content types, and they're not CPTs. But it might be worth thinking through the approach holistically. Whatever the long-term approach, my proposed workaround will at least keep Docs functional on block themes in the near term. |
@boonebgorges I've been looking into Block themes for a year building one for my personal website and starting to experiment things in BuddyPress. In next major release I think, now we have BP Rewrites API in, we should make a first step towards being able to edit community area pages within the Site Editor. As you wrote, we need to build more BP Blocks (header-item, body-item, primary-nav, secondary-nav, etc...) & probably an extended version of the WordPress pattern. As far as I can remember you can't provide context inside a pattern which is pretty annoying, the BP Block Pattern will need to provide a BP Context so that's easier to output the right content according to it. Here's the steps we'll take:
For now I chose to avoid the problem you perfectly described here:
|
@boonebgorges Which block themes did you try with 6097e82? At first blush, it seems to work in 2022 and 23, but not 24, so I'm curious which you've tried. I'm stunned that they don't all behave the same way! :D Thanks! |
I tested with twentytwentytwo and twentytwentythree. It's not working with twentytwentyfour because that theme loads its query template inside of a |
…the post-template on the archive page. See #728.
Here's a case where we have to hack around a hack :) My previous technique looked at the serialized content of the archive template to identify and then replace post-excerpts. This technique doesn't work with themes that don't have I've reworked my hack so that we descend the block tree and find the most "specific" block whose rendered HTML contains excerpt markup. In the case of twentytwentytwo and twentytwentythree, this will be the This works, but: All this being said, the current approach seems to work OK for the core block themes, which is probably as much as we can hope for. For anyone running a different block theme, they can fix the problem manually: change the block template to a @dcavins and @imath What do you think of all this craziness? Is this too weird to ship? |
@boonebgorges that's a challenge ! About
In this case they should probably add a new I don't know BP Docs enough, but an easy way to have more control on the output could be to use a BP Directory page and put a BP Docs loop inside a regular template part? |
Yes, agreed. Is there a way to do this in the Editor or do you have to create it in your theme directory? Everything changes all the time :-/
When I first wrote this plugin, I wanted to take advantage of custom post types as much as possible, so I avoided using BP directories. Adding a custom BP directory was always a pain anyway - maybe it's easier now. In any case, I'd need to rewrite a bunch of the pagination and routing logic if I went this route. And it seems to me that this would mean putting more eggs into the "page.html" basket. BP's theme compatibility technique has always been a hack, albeit a very clever hack, and the advent of block themes suggests that it might be time to come up with a different kind of approach rather than doubling down on |
I've been testing I agree that the long-term solution is to generate a BP Docs Archive item. I'm confusing myself a bit, because previously while full-site editing in one of the themes, it seemed that I could customize an archive for a single post type, but I can't seem to find that functionality today (I can only customize the "all archives" view). I thought maybe we could insert our directory block into however the archive customizations are saved, but I'll have to find the per-type interface again and see how it's saved. |
@dcavins Agreed. This is better than nothing, and we can always yank it later if it poses problems. Let's ship it. I see you're working on the branch today. Let me know when you're relatively satisfied, and we'll merge back to 2.2.x. I think that this merge should address all the tickets in the milestone at once: https://github.com/boonebgorges/buddypress-docs/milestone/25 Then I'll push out a Christmas Miracle of a release. 🎄 |
Ah, that's amazing! There's a GH user with the username Anyway, yes, those items in the milestone I think are all ready to go. Do you want to manage the merge to 2.2.1, then I can run through my tests again with everything integrated into a release-ish state? Let the Christmas Miracle commence, even if the puppy we're gifting is a little goofy looking with supersized ears and stubby legs and not the thoroughbred puppy that's featured in Hallmark movies! |
Amazing. I've merged to 2.2.x. I'll spend some time running a few tests and writing up the readme. Let me know when you've finished your tests. |
For bookkeeping purposes, I'm going to put this in the 2.2.1 milestone, and we'll mark it resolved after testing. Future improvements to block-theme integration will have to go in the 'some day' milestone. |
I've also merged in the required changes for the group tab problem. Ive tested with BP 11.4, BP 12, and BP 12 with BP Classic in TwentyNineteen through Twenty-Four, and the behavior is much improved (wider in the non-block theme, at least displays in the block themes) and groups Docs screens are available to non-admins. Thanks for the updates! |
@boonebgorges I think you have to create it in your theme directory, so users should copy |
I can't imagine most users being able to do that (being comfortable with creating a child theme and then manipulating the files on the server). I wonder if we could do the Thanks for thinking about this problem, @imath! It seems we've run into something that shouldn't be this hard to do in WP. 👯 |
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.
@imath Yeah, I figured as much. So I figured out that the same problem is occurring on the Create page as well. This uncovers a limitation in the previous technique, which has to do with the way that BP's theme compatibility layer works (or doesn't work) at the |
As a side note, the |
Let's go with this and see if the sky falls. |
It's still better than it was! Even if Chicken Little might be right. |
On a default installation of WP running twentytwentytwo or twentytwentythree, the main post type directory puts the Docs-specific filters in a weird place. See screenshot:
I don't know how to work around this, but let's consider this ticket a placeholder for figuring it out.
The text was updated successfully, but these errors were encountered: