Skip to content

Commit

Permalink
Block theme support
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaydsouza committed Oct 20, 2024
1 parent 1cb794c commit 9f06a42
Show file tree
Hide file tree
Showing 14 changed files with 549 additions and 19 deletions.
11 changes: 10 additions & 1 deletion includes/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,16 @@ function wzkb_knowledge( $args = array() ) {
$output = '<div class="wzkb ' . esc_attr( $div_classes ) . '">';

// Are we trying to display a category?
$category = absint( $args['category'] );
$category = intval( $args['category'] );

// if $category = -1, then get the current term object and set the category to the term ID.
if ( -1 === $category ) {
$term = get_queried_object();
if ( isset( $term->term_id ) ) {
$category = $term->term_id;
}
}

$level = ( 0 < $category ) ? 1 : 0;
$term_id = ( 0 < $category ) ? $category : 0;
$nested_wrapper = ( isset( $args['nested_wrapper'] ) ) ? $args['nested_wrapper'] : true;
Expand Down
218 changes: 218 additions & 0 deletions includes/public/class-template-handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
<?php
/**
* The template handler class.
*
* @package WebberZone\KnowledgeBase
*/

namespace WebberZone\KnowledgeBase\Public;

Check failure on line 8 in includes/public/class-template-handler.php

View workflow job for this annotation

GitHub Actions / PHPCS check

PHP syntax error: syntax error, unexpected 'Public' (T_PUBLIC), expecting identifier (T_STRING)

Check failure on line 8 in includes/public/class-template-handler.php

View workflow job for this annotation

GitHub Actions / PHPCS check

PHP syntax error: syntax error, unexpected 'Public' (T_PUBLIC), expecting identifier (T_STRING)

/**
* Class Template_Handler
*
* Handles template-related functionalities for the Knowledge Base plugin.
*
* @package WebberZone\KnowledgeBase
*/
class Template_Handler {
/**
* Constructor for the Template_Handler class.
*/
public function __construct() {
add_filter( 'get_block_templates', array( $this, 'manage_block_templates' ), 10, 3 );

$template_types = array(
'archive' => 'add_custom_archive_template',
'index' => 'add_custom_index_template',
'single' => 'add_custom_single_template',
'taxonomy' => 'add_custom_taxonomy_template',
'search' => 'add_custom_search_template',
);

foreach ( $template_types as $type => $callback ) {
add_filter( "{$type}_template_hierarchy", array( $this, $callback ) );
}
}

/**
* Add custom template for the wz_knowledgebase custom post type and wzkb_category taxonomy.
*
* @param array $templates Array of found templates.
* @param string $type Type of template (archive, single, taxonomy).
* @param string $post_type Post type or taxonomy name.
* @param string $template_name Template name to add.
* @return array Updated array of found templates.
*/
private function add_custom_template( $templates, $type, $post_type, $template_name ) {
if ( ( in_array( $type, array( 'archive', 'index', 'search' ), true ) ) ||
( 'single' === $type && is_singular( $post_type ) ) ) {
array_unshift( $templates, $template_name );
}
return $templates;
}

/**
* Add custom archive template for the wz_knowledgebase custom post type.
*
* @param array $templates Array of found templates.
* @return array Updated array of found templates.
*/
public function add_custom_archive_template( $templates ) {
if ( is_tax( 'wzkb_category' ) ) {
return $this->add_custom_template( $templates, 'archive', 'wzkb_category', 'taxonomy-wzkb_category' );
}
if ( is_singular( 'wz_knowledgebase' ) ) {
return $this->add_custom_template( $templates, 'single', 'wz_knowledgebase', 'single-wz_knowledgebase' );
}
if ( is_search() ) {
return $this->add_custom_template( $templates, 'search', 'wz_knowledgebase', 'wzkb-search' );
}
return $this->add_custom_template( $templates, 'archive', 'wz_knowledgebase', 'archive-wz_knowledgebase' );
}

/**
* Add custom archive template for the wz_knowledgebase custom post type.
*
* @param array $templates Array of found templates.
* @return array Updated array of found templates.
*/
public function add_custom_index_template( $templates ) {
return $this->add_custom_archive_template( $templates );
}

/**
* Add custom single template for the wz_knowledgebase custom post type.
*
* @param array $templates Array of found templates.
* @return array Updated array of found templates.
*/
public function add_custom_single_template( $templates ) {
return $this->add_custom_template( $templates, 'single', 'wz_knowledgebase', 'single-wz_knowledgebase' );
}

/**
* Add custom taxonomy template for the wzkb_category taxonomy.
*
* @param array $templates Array of found templates.
* @return array Updated array of found templates.
*/
public function add_custom_taxonomy_template( $templates ) {
return $this->add_custom_template( $templates, 'archive', 'wzkb_category', 'taxonomy-wzkb_category' );
}

/**
* Add custom search template for the wz_knowledgebase custom post type.
*
* @param array $templates Array of found templates.
* @return array Updated array of found templates.
*/
public function add_custom_search_template( $templates ) {
return $this->add_custom_template( $templates, 'search', 'wz_knowledgebase', 'wzkb-search' );
}

/**
* Manage block templates for the wz_knowledgebase custom post type.
*
* @param array $query_result Array of found block templates.
* @param array $query Arguments to retrieve templates.
* @param string $template_type $template_type wp_template or wp_template_part.
* @return array Updated array of found block templates.
*/
public function manage_block_templates( $query_result, $query, $template_type ) {
if ( 'wp_template' !== $template_type ) {
return $query_result;
}

global $post;
if ( ( empty( $post ) && ! is_admin() ) || ( ! empty( $post ) && 'wz_knowledgebase' !== $post->post_type ) ) {
return $query_result;
}

$theme = wp_get_theme();
$block_source = 'plugin';

$template_name = null;

if ( is_singular( 'wz_knowledgebase' ) ) {
$template_name = 'single-wz_knowledgebase';
} elseif ( is_post_type_archive( 'wz_knowledgebase' ) ) {
$template_name = is_search() ? 'wzkb-search' : 'archive-wz_knowledgebase';
} elseif ( is_tax( 'wzkb_category' ) && ! is_search() ) {
$template_name = 'taxonomy-wzkb_category';
}

if ( $template_name ) {
$template_file_path = $theme->get_template_directory() . '/templates/' . $template_name . '.html';
if ( file_exists( $template_file_path ) ) {
$block_source = 'theme';
} else {
$template_file_path = __DIR__ . '/templates/' . $template_name . '.html';
}

$template_contents = self::get_template_content( $template_file_path );
$template_contents = self::replace_placeholders_with_shortcodes( $template_contents );

$new_block = new \WP_Block_Template();
$new_block->type = 'wp_template';
$new_block->theme = $theme->stylesheet;
$new_block->slug = $template_name;
$new_block->id = 'wzkb//' . $template_name;
$new_block->title = 'Knowledge Base Template - ' . $template_name;
$new_block->description = '';
$new_block->source = $block_source;
$new_block->status = 'publish';
$new_block->has_theme_file = true;
$new_block->is_custom = true;
$new_block->content = $template_contents;
$new_block->post_types = array( 'wz_knowledgebase' );

$query_result[] = $new_block;
}

return $query_result;
}

/**
* Replaces placeholders with corresponding shortcode output.
*
* @param string $template_contents The template content containing placeholders.
* @return string The updated template with shortcodes replaced by their output.
*/
public static function replace_placeholders_with_shortcodes( $template_contents ) {
// Regular expression to match placeholders like {{shortcode param="value"}}.
$pattern = '/\{\{([a-zA-Z_]+)(.*?)\}\}/';

// Callback function to process each match.
$callback = function ( $matches ) {
$shortcode = trim( $matches[1] ); // Extract the shortcode name.
$params = trim( $matches[2] ); // Extract any parameters.

// Construct the shortcode with the parameters.
if ( ! empty( $params ) ) {
$shortcode_output = '[' . $shortcode . ' ' . $params . ']';
} else {
$shortcode_output = '[' . $shortcode . ']';
}

// Run the shortcode and return the output.
return do_shortcode( $shortcode_output );
};

// Run the preg_replace_callback to find and replace all placeholders.
return preg_replace_callback( $pattern, $callback, $template_contents );
}

/**
* Get the content of a template file.
*
* @param string $template The template file to include.
* @return string The content of the template file.
*/
public static function get_template_content( $template ) {
ob_start();
include $template;
return ob_get_clean();
}
}

new Template_Handler();
1 change: 1 addition & 0 deletions includes/public/css/wzkb-styles-rtl.css
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
padding: 5px 1px;
background-color: ghostwhite;
margin-top: 30px;
margin-bottom: 0;
}

.wzkb h3.wzkb-section-name-level-0, .wzkb h3.wzkb-section-name-level-1 {
Expand Down
2 changes: 1 addition & 1 deletion includes/public/css/wzkb-styles-rtl.min.css

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

Loading

0 comments on commit 9f06a42

Please sign in to comment.