PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.14.0
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.14.0
2.14.0 2.13.0 2.13.1 2.12.0 2.11.1 2.11.0 2.10.0 2.9.0 2.7.4 2.7.5 2.7.6 2.7.7 2.8.0 2.8.1 2.9.1 trunk 1.0 1.0-beta1 1.0-beta2 1.0-beta3 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 All 81 releases
ablocks / includes / blocks / loop-filter / block.php

block.php in aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder 2.14.0, at includes/blocks/loop-filter/block.php

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