Skip to content

Instantly share code, notes, and snippets.

@digamber89
Last active August 24, 2023 06:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save digamber89/97f795bb011af2f550a4922936bab4dc to your computer and use it in GitHub Desktop.
Save digamber89/97f795bb011af2f550a4922936bab4dc to your computer and use it in GitHub Desktop.
Adding Single Book via Plugin
<?php
namespace Codemanas\SiteModsTS;
use WP_Block_Template;
use WP_Query;
class BlockTemplate {
public static ?BlockTemplate $instance = null;
public static function get_instance(): ?BlockTemplate {
return is_null( self::$instance ) ? self::$instance = new self() : self::$instance;
}
protected function __construct() {
//used when saving /wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php used by block templates
add_filter( 'pre_get_block_file_template', [ $this, 'get_templates' ], 10, 3 );
//this handles the which html will be shown
add_filter( 'get_block_templates', [ $this, 'add_meetings_block_template' ], 10, 2 );
}
public function add_meetings_block_template( $query_results, $query ) {
$slugs = $query['slug__in'] ?? [];
//bail early
if ( ! is_admin() && ! empty( $slugs ) && ! in_array( 'single-book', $slugs ) ) {
return $query_results;
}
//check if the template is saved in the db first
$template_from_db = $this->get_template_from_db( $slugs );
if ( $template_from_db !== null ) {
$query_results[] = $template_from_db;
return $query_results;
}
$query_results[] = $this->single_meeting_template();
return $query_results;
}
private function get_template_from_db( $slugs ): ?WP_Block_Template {
$template = null;
//check if template is saved in DB and retrieve it from their
$args = [
'post_type' => 'wp_template',
'tax_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
array(
'taxonomy' => 'wp_theme',
'field' => 'name',
'terms' => [ 'cm_book' ],//this needs to be unique
),
),
];
if ( is_array( $slugs ) && count( $slugs ) > 0 ) {
$args['post_name__in'] = $slugs;
}
$check_templates = new WP_Query( $args );
if ( $check_templates->found_posts > 0 ) {
foreach ( $check_templates->posts as $post ) {
$template = $this->create_template_from_db( $post );
break;
}
}
return $template;
}
private function create_template_from_db( $post ): WP_Block_Template {
$terms = get_the_terms( $post, 'wp_theme' );
$theme = $terms[0]->name;
$template = new \WP_Block_Template();
$template->wp_id = $post->ID;
$template->id = $theme . '//' . $post->post_name;
$template->theme = $theme;
$template->content = $post->post_content;
$template->slug = $post->post_name;
$template->source = 'custom';
$template->type = $post->post_type;
$template->description = $post->post_excerpt;
$template->title = $post->post_title;
$template->status = $post->post_status;
$template->has_theme_file = true;
$template->is_custom = false;
$template->post_types = array(); //
$template->area = 'uncategorized';
return $template;
}
public function get_templates( $template, $id, $template_type ) {
if ( $template_type != 'wp_template' ) {
return $template;
}
$template_name_parts = explode( '//', $id );
if ( count( $template_name_parts ) < 2 ) {
return $template;
}
list( $template_id, $template_slug ) = $template_name_parts;
if ( $template_id != 'vczapi' ) {
return $template;
}
if ( $template_slug == 'single-book' ) {
return $this->single_meeting_template();
}
return $template;
}
private function single_meeting_template(): WP_Block_Template {
$template_content = '<!-- wp:template-part {"slug":"header","tagName":"header"} /-->
<!-- wp:group {"layout":{"inherit":true}} -->
<div class="wp-block-group"><!-- wp:paragraph -->
<p>This is an example page. Different from a blog post because it will stay in one place and will show up in your site navigation (in most themes). Most people start with an About page that introduces them to potential site visitors. It might say something like this:</p>
<!-- /wp:paragraph --></div>
<!-- /wp:group -->
<!-- wp:template-part {"slug":"footer","tagName":"footer"} /-->';
$modified_with_theme_template_content = '';
$blocks = parse_blocks( $template_content );
foreach ( $blocks as $block ) {
if (
'core/template-part' === $block['blockName'] &&
! isset( $block['attrs']['theme'] )
) {
$block['attrs']['theme'] = wp_get_theme()->get_stylesheet();
}
$modified_with_theme_template_content .= serialize_block( $block );
}
$template = new WP_Block_Template();
$template->type = 'wp_template';
$template->theme = 'codemanas';
$template->slug = 'single-book';//this needs to be
//id needs to be combination of $template->theme and $template->slug
$template->id = 'codemanas//single-book';
$template->title = 'Single Book';
$template->content = $modified_with_theme_template_content;
$template->description = 'Displays a single Book';
$template->source = 'plugin';
$template->origin = 'plugin';
$template->status = 'publish';
$template->has_theme_file = false;
$template->is_custom = false;
$template->author = null;
$template->post_types = [];
$template->area = 'uncategorized';
return $template;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment