| 1 |
<?php |
| 2 |
namespace ABlocks\Blocks\LoopFilter; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
use ABlocks\Classes\BlockBaseAbstract; |
| 9 |
use ABlocks\Classes\CssGeneratorV2; |
| 10 |
use ABlocks\Controls\Range; |
| 11 |
use ABlocks\Controls\Alignment; |
| 12 |
use ABlocks\Controls\Border; |
| 13 |
use ABlocks\Controls\Dimensions; |
| 14 |
use ABlocks\Controls\Typography; |
| 15 |
use ABlocks\Controls\Color; |
| 16 |
|
| 17 |
class Block extends BlockBaseAbstract { |
| 18 |
protected $parent_block_name = 'loop-builder'; |
| 19 |
protected $block_name = 'loop-filter'; |
| 20 |
|
| 21 |
public function build_css( $attributes ) { |
| 22 |
|
| 23 |
$css_generator = new CssGeneratorV2( $attributes, $this->block_name ); |
| 24 |
|
| 25 |
$css_generator->add_class_styles( |
| 26 |
'{{WRAPPER}}', |
| 27 |
$this->filter_button_alignment( $attributes ), |
| 28 |
$this->filter_button_alignment( $attributes, 'Tablet' ), |
| 29 |
$this->filter_button_alignment( $attributes, 'Mobile' ) |
| 30 |
); |
| 31 |
$css_generator->add_class_styles( |
| 32 |
'{{WRAPPER}} .ablocks-loop-term-filter', |
| 33 |
$this->get_button_style( $attributes ), |
| 34 |
$this->get_button_style( $attributes, 'Tablet' ), |
| 35 |
$this->get_button_style( $attributes, 'Mobile' ) |
| 36 |
); |
| 37 |
$css_generator->add_class_styles( |
| 38 |
'{{WRAPPER}} .ablocks-loop-term-filter:hover', |
| 39 |
$this->get_button_style_hover( $attributes ), |
| 40 |
$this->get_button_style_hover( $attributes, 'Tablet' ), |
| 41 |
$this->get_button_style_hover( $attributes, 'Mobile' ) |
| 42 |
); |
| 43 |
$css_generator->add_class_styles( |
| 44 |
'{{WRAPPER}} .ablocks-loop-term-filter--active', |
| 45 |
$this->get_active_button_style( $attributes ), |
| 46 |
$this->get_active_button_style( $attributes, 'Tablet' ), |
| 47 |
$this->get_active_button_style( $attributes, 'Mobile' ) |
| 48 |
); |
| 49 |
$css_generator->add_class_styles( |
| 50 |
'{{WRAPPER}} .ablocks-loop-term-filter--active:hover', |
| 51 |
$this->get_active_button_style_hover( $attributes ), |
| 52 |
$this->get_active_button_style_hover( $attributes, 'Tablet' ), |
| 53 |
$this->get_active_button_style_hover( $attributes, 'Mobile' ) |
| 54 |
); |
| 55 |
return $css_generator->generate_css(); |
| 56 |
} |
| 57 |
|
| 58 |
public function render_block_content( $attributes, $content, $block_instance ) { |
| 59 |
global $post; |
| 60 |
|
| 61 |
$current_post_id = isset( $post->ID ) ? $post->ID : 0; |
| 62 |
$active_term_id = ! empty( $attributes['active_term_id'] ) ? $attributes['active_term_id'] : '*'; |
| 63 |
if ( ! empty( $attributes['taxonomy'] ) && $attributes['taxonomy'] !== 'inherit' ) { |
| 64 |
$taxonomy = $attributes['taxonomy']; |
| 65 |
} else { |
| 66 |
// Try to get the current queried taxonomy |
| 67 |
$queried_object = get_queried_object(); |
| 68 |
if ( ! empty( $queried_object ) && ! empty( $queried_object->taxonomy ) ) { |
| 69 |
$taxonomy = $queried_object->taxonomy; |
| 70 |
} else { |
| 71 |
$taxonomy = 'category'; // fallback |
| 72 |
} |
| 73 |
} |
| 74 |
// Determine archive state and post type (null if not archive) |
| 75 |
$is_archive = is_archive() ? 'true' : 'false'; |
| 76 |
$archive_post_type = is_post_type_archive() ? get_query_var( 'post_type' ) : get_post_type(); |
| 77 |
if ( is_array( $archive_post_type ) ) { |
| 78 |
$archive_post_type = reset( $archive_post_type ); |
| 79 |
} |
| 80 |
$archive_post_type = esc_attr( $archive_post_type ); |
| 81 |
|
| 82 |
$taxonomy_term_items = isset( $attributes['taxonomy_term_items'] ) && is_array( $attributes['taxonomy_term_items'] ) ? $attributes['taxonomy_term_items'] : []; |
| 83 |
|
| 84 |
if ( count( $taxonomy_term_items ) ) { |
| 85 |
foreach ( $taxonomy_term_items as $term_item ) { |
| 86 |
$term_value = isset( $term_item['value'] ) ? $term_item['value'] : ''; |
| 87 |
$term_label = isset( $term_item['label'] ) ? $term_item['label'] : ''; |
| 88 |
|
| 89 |
$is_active = (string) $active_term_id === (string) $term_value ? ' ablocks-loop-term-filter--active' : ''; |
| 90 |
|
| 91 |
echo '<span class="ablocks-loop-term-filter' . esc_attr( $is_active ) . '" ' |
| 92 |
. 'data-post-id="' . esc_attr( $current_post_id ) . '" ' |
| 93 |
. 'data-term-id="' . esc_attr( $term_value ) . '" ' |
| 94 |
. 'data-taxonomy="' . esc_attr( $taxonomy ) . '" ' |
| 95 |
. 'data-is-archive="' . esc_attr( $is_archive ) . '" ' |
| 96 |
. 'data-archive-post-type="' . esc_attr( $archive_post_type ) . '" ' |
| 97 |
. 'type="button">' |
| 98 |
. esc_html( $term_label ) |
| 99 |
. '</span>'; |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
public function filter_button_alignment( $attributes, $device = '' ) { |
| 106 |
$css = []; |
| 107 |
|
| 108 |
$css = array_merge( |
| 109 |
$css, |
| 110 |
Range::get_css([ |
| 111 |
'attributeValue' => $attributes['FilterBtnGap'] ?? null, |
| 112 |
'attribute_object_key' => 'value', |
| 113 |
'isResponsive' => true, |
| 114 |
'hasUnit' => true, |
| 115 |
'defaultValue' => 10, |
| 116 |
'unitDefaultValue' => 'px', |
| 117 |
'property' => 'gap', |
| 118 |
'device' => $device, |
| 119 |
]) |
| 120 |
); |
| 121 |
$css['display'] = 'flex'; |
| 122 |
$css['width'] = '100%'; |
| 123 |
$css['flex-wrap'] = 'wrap'; |
| 124 |
if ( ! empty( $attributes['buttonAlignment'] ) ) { |
| 125 |
$css['flex-direction'] = $attributes['buttonAlignment']; |
| 126 |
} |
| 127 |
|
| 128 |
$alignment_css = isset( $attributes['filterBtnAlignment'] ) |
| 129 |
? Alignment::get_css( $attributes['filterBtnAlignment'], 'align-items', $device ) |
| 130 |
: []; |
| 131 |
$css = array_merge( $css, $alignment_css ); |
| 132 |
|
| 133 |
$row_alignment_css = isset( $attributes['rowAlignBtn'] ) |
| 134 |
? Alignment::get_css( $attributes['rowAlignBtn'], 'justify-content', $device ) |
| 135 |
: []; |
| 136 |
$css = array_merge( $css, $row_alignment_css ); |
| 137 |
|
| 138 |
return $css; |
| 139 |
} |
| 140 |
|
| 141 |
public function get_button_style( $attributes, $device = '' ) { |
| 142 |
$normal_button_border = ! empty( $attributes['filterBtnBorder'] ) ? Border::get_css( $attributes['filterBtnBorder'], '', $device ) : array(); |
| 143 |
$typographyGlobal = ! empty( $attributes['typographyGlobal'] ) ? $attributes['typographyGlobal'] : array(); |
| 144 |
$css = array_merge( |
| 145 |
[ 'color' => Color::get_css( isset( $attributes['filterBtnTextColor'] ) ? $attributes['filterBtnTextColor'] : '' ) ], |
| 146 |
[ 'background' => Color::get_css( isset( $attributes['filterBtnBgColor'] ) ? $attributes['filterBtnBgColor'] : '' ) ], |
| 147 |
$normal_button_border, |
| 148 |
Typography::get_css( isset( $attributes['typography'] ) ? $attributes['typography'] : array(), '', $device, $typographyGlobal ), |
| 149 |
Dimensions::get_css( isset( $attributes['filterButtonPadding'] ) ? $attributes['filterButtonPadding'] : [], 'padding', $device ), |
| 150 |
Dimensions::get_css( isset( $attributes['filterButtonMargin'] ) ? $attributes['filterButtonMargin'] : [], 'margin', $device ), |
| 151 |
); |
| 152 |
return $css; |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
public function get_button_style_hover( $attributes, $device = '' ) { |
| 157 |
return array_merge( |
| 158 |
isset( $attributes['filterBtnBorder'] ) ? Border::get_hover_css( $attributes['filterBtnBorder'], '', $device ) : [], |
| 159 |
); |
| 160 |
} |
| 161 |
public function get_active_button_style( $attributes, $device = '' ) { |
| 162 |
$button_border_css = ! empty( $attributes['activeBtnBorder'] ) |
| 163 |
? Border::get_css( $attributes['activeBtnBorder'], '', $device ) |
| 164 |
: array(); |
| 165 |
$typographyGlobal = ! empty( $attributes['typographyGlobal'] ) ? $attributes['typographyGlobal'] : array(); |
| 166 |
$css = array_merge( |
| 167 |
[ 'color' => Color::get_css( isset( $attributes['activeBtnTextColor'] ) ? $attributes['activeBtnTextColor'] : '' ) ], |
| 168 |
[ 'background' => Color::get_css( isset( $attributes['activeBtnBgColor'] ) ? $attributes['activeBtnBgColor'] : '' ) ], |
| 169 |
$button_border_css, |
| 170 |
Typography::get_css( isset( $attributes['typography'] ) ? $attributes['typography'] : array(), '', $device, $typographyGlobal ), |
| 171 |
); |
| 172 |
return $css; |
| 173 |
} |
| 174 |
|
| 175 |
public function get_active_button_style_hover( $attributes, $device = '' ) { |
| 176 |
return array_merge( |
| 177 |
isset( $attributes['activeBtnBorder'] ) ? Border::get_hover_css( $attributes['activeBtnBorder'], '', $device ) : [], |
| 178 |
); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
|