Skip to content

Commit 9f06a42

Browse files
committed
Block theme support
1 parent 1cb794c commit 9f06a42

14 files changed

+549
-19
lines changed

includes/main.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,16 @@ function wzkb_knowledge( $args = array() ) {
7676
$output = '<div class="wzkb ' . esc_attr( $div_classes ) . '">';
7777

7878
// Are we trying to display a category?
79-
$category = absint( $args['category'] );
79+
$category = intval( $args['category'] );
80+
81+
// if $category = -1, then get the current term object and set the category to the term ID.
82+
if ( -1 === $category ) {
83+
$term = get_queried_object();
84+
if ( isset( $term->term_id ) ) {
85+
$category = $term->term_id;
86+
}
87+
}
88+
8089
$level = ( 0 < $category ) ? 1 : 0;
8190
$term_id = ( 0 < $category ) ? $category : 0;
8291
$nested_wrapper = ( isset( $args['nested_wrapper'] ) ) ? $args['nested_wrapper'] : true;
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
<?php
2+
/**
3+
* The template handler class.
4+
*
5+
* @package WebberZone\KnowledgeBase
6+
*/
7+
8+
namespace WebberZone\KnowledgeBase\Public;
9+
10+
/**
11+
* Class Template_Handler
12+
*
13+
* Handles template-related functionalities for the Knowledge Base plugin.
14+
*
15+
* @package WebberZone\KnowledgeBase
16+
*/
17+
class Template_Handler {
18+
/**
19+
* Constructor for the Template_Handler class.
20+
*/
21+
public function __construct() {
22+
add_filter( 'get_block_templates', array( $this, 'manage_block_templates' ), 10, 3 );
23+
24+
$template_types = array(
25+
'archive' => 'add_custom_archive_template',
26+
'index' => 'add_custom_index_template',
27+
'single' => 'add_custom_single_template',
28+
'taxonomy' => 'add_custom_taxonomy_template',
29+
'search' => 'add_custom_search_template',
30+
);
31+
32+
foreach ( $template_types as $type => $callback ) {
33+
add_filter( "{$type}_template_hierarchy", array( $this, $callback ) );
34+
}
35+
}
36+
37+
/**
38+
* Add custom template for the wz_knowledgebase custom post type and wzkb_category taxonomy.
39+
*
40+
* @param array $templates Array of found templates.
41+
* @param string $type Type of template (archive, single, taxonomy).
42+
* @param string $post_type Post type or taxonomy name.
43+
* @param string $template_name Template name to add.
44+
* @return array Updated array of found templates.
45+
*/
46+
private function add_custom_template( $templates, $type, $post_type, $template_name ) {
47+
if ( ( in_array( $type, array( 'archive', 'index', 'search' ), true ) ) ||
48+
( 'single' === $type && is_singular( $post_type ) ) ) {
49+
array_unshift( $templates, $template_name );
50+
}
51+
return $templates;
52+
}
53+
54+
/**
55+
* Add custom archive template for the wz_knowledgebase custom post type.
56+
*
57+
* @param array $templates Array of found templates.
58+
* @return array Updated array of found templates.
59+
*/
60+
public function add_custom_archive_template( $templates ) {
61+
if ( is_tax( 'wzkb_category' ) ) {
62+
return $this->add_custom_template( $templates, 'archive', 'wzkb_category', 'taxonomy-wzkb_category' );
63+
}
64+
if ( is_singular( 'wz_knowledgebase' ) ) {
65+
return $this->add_custom_template( $templates, 'single', 'wz_knowledgebase', 'single-wz_knowledgebase' );
66+
}
67+
if ( is_search() ) {
68+
return $this->add_custom_template( $templates, 'search', 'wz_knowledgebase', 'wzkb-search' );
69+
}
70+
return $this->add_custom_template( $templates, 'archive', 'wz_knowledgebase', 'archive-wz_knowledgebase' );
71+
}
72+
73+
/**
74+
* Add custom archive template for the wz_knowledgebase custom post type.
75+
*
76+
* @param array $templates Array of found templates.
77+
* @return array Updated array of found templates.
78+
*/
79+
public function add_custom_index_template( $templates ) {
80+
return $this->add_custom_archive_template( $templates );
81+
}
82+
83+
/**
84+
* Add custom single template for the wz_knowledgebase custom post type.
85+
*
86+
* @param array $templates Array of found templates.
87+
* @return array Updated array of found templates.
88+
*/
89+
public function add_custom_single_template( $templates ) {
90+
return $this->add_custom_template( $templates, 'single', 'wz_knowledgebase', 'single-wz_knowledgebase' );
91+
}
92+
93+
/**
94+
* Add custom taxonomy template for the wzkb_category taxonomy.
95+
*
96+
* @param array $templates Array of found templates.
97+
* @return array Updated array of found templates.
98+
*/
99+
public function add_custom_taxonomy_template( $templates ) {
100+
return $this->add_custom_template( $templates, 'archive', 'wzkb_category', 'taxonomy-wzkb_category' );
101+
}
102+
103+
/**
104+
* Add custom search template for the wz_knowledgebase custom post type.
105+
*
106+
* @param array $templates Array of found templates.
107+
* @return array Updated array of found templates.
108+
*/
109+
public function add_custom_search_template( $templates ) {
110+
return $this->add_custom_template( $templates, 'search', 'wz_knowledgebase', 'wzkb-search' );
111+
}
112+
113+
/**
114+
* Manage block templates for the wz_knowledgebase custom post type.
115+
*
116+
* @param array $query_result Array of found block templates.
117+
* @param array $query Arguments to retrieve templates.
118+
* @param string $template_type $template_type wp_template or wp_template_part.
119+
* @return array Updated array of found block templates.
120+
*/
121+
public function manage_block_templates( $query_result, $query, $template_type ) {
122+
if ( 'wp_template' !== $template_type ) {
123+
return $query_result;
124+
}
125+
126+
global $post;
127+
if ( ( empty( $post ) && ! is_admin() ) || ( ! empty( $post ) && 'wz_knowledgebase' !== $post->post_type ) ) {
128+
return $query_result;
129+
}
130+
131+
$theme = wp_get_theme();
132+
$block_source = 'plugin';
133+
134+
$template_name = null;
135+
136+
if ( is_singular( 'wz_knowledgebase' ) ) {
137+
$template_name = 'single-wz_knowledgebase';
138+
} elseif ( is_post_type_archive( 'wz_knowledgebase' ) ) {
139+
$template_name = is_search() ? 'wzkb-search' : 'archive-wz_knowledgebase';
140+
} elseif ( is_tax( 'wzkb_category' ) && ! is_search() ) {
141+
$template_name = 'taxonomy-wzkb_category';
142+
}
143+
144+
if ( $template_name ) {
145+
$template_file_path = $theme->get_template_directory() . '/templates/' . $template_name . '.html';
146+
if ( file_exists( $template_file_path ) ) {
147+
$block_source = 'theme';
148+
} else {
149+
$template_file_path = __DIR__ . '/templates/' . $template_name . '.html';
150+
}
151+
152+
$template_contents = self::get_template_content( $template_file_path );
153+
$template_contents = self::replace_placeholders_with_shortcodes( $template_contents );
154+
155+
$new_block = new \WP_Block_Template();
156+
$new_block->type = 'wp_template';
157+
$new_block->theme = $theme->stylesheet;
158+
$new_block->slug = $template_name;
159+
$new_block->id = 'wzkb//' . $template_name;
160+
$new_block->title = 'Knowledge Base Template - ' . $template_name;
161+
$new_block->description = '';
162+
$new_block->source = $block_source;
163+
$new_block->status = 'publish';
164+
$new_block->has_theme_file = true;
165+
$new_block->is_custom = true;
166+
$new_block->content = $template_contents;
167+
$new_block->post_types = array( 'wz_knowledgebase' );
168+
169+
$query_result[] = $new_block;
170+
}
171+
172+
return $query_result;
173+
}
174+
175+
/**
176+
* Replaces placeholders with corresponding shortcode output.
177+
*
178+
* @param string $template_contents The template content containing placeholders.
179+
* @return string The updated template with shortcodes replaced by their output.
180+
*/
181+
public static function replace_placeholders_with_shortcodes( $template_contents ) {
182+
// Regular expression to match placeholders like {{shortcode param="value"}}.
183+
$pattern = '/\{\{([a-zA-Z_]+)(.*?)\}\}/';
184+
185+
// Callback function to process each match.
186+
$callback = function ( $matches ) {
187+
$shortcode = trim( $matches[1] ); // Extract the shortcode name.
188+
$params = trim( $matches[2] ); // Extract any parameters.
189+
190+
// Construct the shortcode with the parameters.
191+
if ( ! empty( $params ) ) {
192+
$shortcode_output = '[' . $shortcode . ' ' . $params . ']';
193+
} else {
194+
$shortcode_output = '[' . $shortcode . ']';
195+
}
196+
197+
// Run the shortcode and return the output.
198+
return do_shortcode( $shortcode_output );
199+
};
200+
201+
// Run the preg_replace_callback to find and replace all placeholders.
202+
return preg_replace_callback( $pattern, $callback, $template_contents );
203+
}
204+
205+
/**
206+
* Get the content of a template file.
207+
*
208+
* @param string $template The template file to include.
209+
* @return string The content of the template file.
210+
*/
211+
public static function get_template_content( $template ) {
212+
ob_start();
213+
include $template;
214+
return ob_get_clean();
215+
}
216+
}
217+
218+
new Template_Handler();

includes/public/css/wzkb-styles-rtl.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
padding: 5px 1px;
2626
background-color: ghostwhite;
2727
margin-top: 30px;
28+
margin-bottom: 0;
2829
}
2930

3031
.wzkb h3.wzkb-section-name-level-0, .wzkb h3.wzkb-section-name-level-1 {

includes/public/css/wzkb-styles-rtl.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)