File: /home/zeusxp5/chuair.org/wp-content/themes/lifttruck/inc/blocks.php
<?php
class ThemeBlocks {
/** Singtone class instance holder
*
*/
protected static $instance;
/**
* Create class instance
*/
public static function instance(){
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
public $blocks = [
];
public $dynamic_blocks = [
"advanced-search/" => "advanced_search",
"product-attributes/" => "product_attribute"
];
function __construct(){
add_action( 'init', [ $this, 'blocks_init' ] );
add_action( 'rest_api_init', [ $this, 'product_atteributes' ]);
}
function blocks_init() {
if( count($this->blocks) ) {
foreach( $this->blocks as $block_name ) {
register_block_type( get_template_directory() . 'public/blocks/'.$block_name );
}
}
if( count($this->dynamic_blocks) ) {
foreach( $this->dynamic_blocks as $block_name => $callback ) {
register_block_type( get_template_directory() . '/public/blocks/'.$block_name, array( 'render_callback' => [$this, $callback] ) );
}
}
}
function display_product_attribute_terms_dropdown($attribute_name){
$terms = get_terms( $attribute_name, array('hide_empty' => false) );
if ($terms) {
echo '<select name="' . str_replace( "pa_", "filter_", $attribute_name ) . '">';
echo '<option value="" selected disabled>'.esc_html__("Select", 'lifttruck').' '.str_replace( "_", " ", $attribute_name ).'</option>'; // Placeholder option
foreach ($terms as $term) {
echo '<option value="' . $term->slug . '">' . $term->name . '</option>';
}
echo '</select>';
} else {
echo esc_html__("No terms found for attribute","lifttruck") . ' ' . $attribute_name;
}
}
function advanced_searsh( $block_content, $block ) {
ob_start();
?>
<div >
<form class="wd-block-webdevia-advanced-searsh" role="search" method="get" id="categoryForm" action="<?php class_exists( 'WooCommerce' ) ? print get_permalink( wc_get_page_id( 'shop' ) ) : ''; ?>">
<?php
$attributes = wc_get_attribute_taxonomies();
if ( $attributes ) {
foreach ( $attributes as $attribute ) {
$this->display_product_attribute_terms_dropdown('pa_'.$attribute->attribute_name);
}
}
// echo $block;
?>
<input type="submit" class="wp-block-button__link wp-element-button" value="Search">
</form>
</div>
<?php
return ob_get_clean();
}
function product_attribute($attributes, $block) {
ob_start();
if ($attributes['showlabel'] == true) {
echo '<p>' . $attributes['productattributes'] . ':</p>';
}
$terms = wc_get_product_terms(get_the_ID(), "pa_" . $attributes['productattributes'], array('hide_empty' => false));
if ($terms) {
$count = count($terms); // Get the total number of terms
foreach ($terms as $key => $term) {
echo '<label>' . $term->name . '</label>';
// If it's not the last iteration, append a comma
if ($key < $count - 1) {
echo ', ';
}
}
}
return ob_get_clean();
}
function get_attribute_options() {
// Your logic to generate attribute options goes here
$attributes = wc_get_attribute_taxonomies();
if ( $attributes ) {
foreach ( $attributes as $attribute ) {
$options[] =$attribute->attribute_name;
}
}
return $options;
}
// Register REST API endpoint
function product_atteributes() {
register_rest_route( 'wdmp/v1', '/attribute-options', array(
'methods' => 'GET',
'callback' => [$this , 'get_attribute_options'],
'permission_callback' => '__return_true'
) );
}
}
ThemeBlocks::instance();