| 1 |
<?php |
| 2 |
|
| 3 |
namespace ABlocks\Blocks\taxonomyListing; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
use ABlocks\Classes\BlockBaseAbstract; |
| 10 |
use ABlocks\Classes\CssGenerator; |
| 11 |
use ABlocks\Controls\Alignment; |
| 12 |
use ABlocks\Controls\Typography; |
| 13 |
use ABlocks\Controls\Dimensions; |
| 14 |
use ABlocks\Controls\Icon; |
| 15 |
use ABlocks\Controls\Border; |
| 16 |
use ABlocks\Controls\BoxShadow; |
| 17 |
|
| 18 |
use WP_Query; |
| 19 |
|
| 20 |
class Block extends BlockBaseAbstract { |
| 21 |
|
| 22 |
|
| 23 |
protected $block_name = 'taxonomy-listing'; |
| 24 |
|
| 25 |
public function build_css( $attributes ) { |
| 26 |
$css_generator = new CssGenerator( $attributes, $this->block_name ); |
| 27 |
|
| 28 |
$wrapper_styles = $this->get_wrapper_css( $attributes ); |
| 29 |
$css_generator->add_class_styles( |
| 30 |
'{{WRAPPER}}', |
| 31 |
$wrapper_styles, |
| 32 |
$this->get_wrapper_css( $attributes, 'Tablet' ), |
| 33 |
$this->get_wrapper_css( $attributes, 'Mobile' ) |
| 34 |
); |
| 35 |
|
| 36 |
$card_styles = $this->get_card_css( $attributes ); |
| 37 |
$css_generator->add_class_styles( |
| 38 |
'{{WRAPPER}} .ablocks-taxonomy-listing-item', |
| 39 |
$card_styles, |
| 40 |
$this->get_card_css( $attributes, 'Tablet' ), |
| 41 |
$this->get_card_css( $attributes, 'Mobile' ) |
| 42 |
); |
| 43 |
|
| 44 |
$card_hover_styles = $this->get_card_hover_css( $attributes ); |
| 45 |
$css_generator->add_class_styles( |
| 46 |
'{{WRAPPER}} .ablocks-taxonomy-listing-item:hover', |
| 47 |
$card_hover_styles, |
| 48 |
$this->get_card_hover_css( $attributes, 'Tablet' ), |
| 49 |
$this->get_card_hover_css( $attributes, 'Mobile' ) |
| 50 |
); |
| 51 |
|
| 52 |
$card_padding_styles = $this->get_card_padding_css( $attributes ); |
| 53 |
$css_generator->add_class_styles( |
| 54 |
'{{WRAPPER}} .ablocks-taxonomy-listing-item_content', |
| 55 |
$card_padding_styles, |
| 56 |
$this->get_card_padding_css( $attributes, 'Tablet' ), |
| 57 |
$this->get_card_padding_css( $attributes, 'Mobile' ) |
| 58 |
); |
| 59 |
|
| 60 |
$taxonomy_title_styles = $this->get_taxonomy_title_css( $attributes ); |
| 61 |
$css_generator->add_class_styles( |
| 62 |
'{{WRAPPER}} .ablocks-taxonomy-title', |
| 63 |
$taxonomy_title_styles, |
| 64 |
$this->get_taxonomy_title_css( $attributes, 'Tablet' ), |
| 65 |
$this->get_taxonomy_title_css( $attributes, 'Mobile' ) |
| 66 |
); |
| 67 |
$taxonomy_title_hover_styles = $this->get_taxonomy_hover_title_css( $attributes ); |
| 68 |
$css_generator->add_class_styles( |
| 69 |
'{{WRAPPER}} .ablocks-taxonomy-title:hover', |
| 70 |
$taxonomy_title_hover_styles, |
| 71 |
$this->get_taxonomy_hover_title_css( $attributes, 'Tablet' ), |
| 72 |
$this->get_taxonomy_hover_title_css( $attributes, 'Mobile' ) |
| 73 |
); |
| 74 |
// Icon style |
| 75 |
$icons_style = $this->get_icons( $attributes ); |
| 76 |
$css_generator->add_class_styles( |
| 77 |
'{{WRAPPER}} .ablocks-taxonomy-icon', |
| 78 |
$icons_style, |
| 79 |
$this->get_icons( $attributes, 'Tablet' ), |
| 80 |
$this->get_icons( $attributes, 'Mobile' ) |
| 81 |
); |
| 82 |
// Icon style |
| 83 |
$icons_svg_style = $this->get_icons_svg( $attributes ); |
| 84 |
$css_generator->add_class_styles( |
| 85 |
'{{WRAPPER}} .ablocks-taxonomy-icon svg', |
| 86 |
$icons_svg_style, |
| 87 |
$this->get_icons_svg( $attributes, 'Tablet' ), |
| 88 |
$this->get_icons_svg( $attributes, 'Mobile' ) |
| 89 |
); |
| 90 |
// Icon style |
| 91 |
$icon_svg_path_style = $this->get_icon_svg_path( $attributes ); |
| 92 |
$css_generator->add_class_styles( |
| 93 |
'{{WRAPPER}} .ablocks-taxonomy-icon svg path', |
| 94 |
$icon_svg_path_style, |
| 95 |
$this->get_icon_svg_path( $attributes, 'Tablet' ), |
| 96 |
$this->get_icon_svg_path( $attributes, 'Mobile' ) |
| 97 |
); |
| 98 |
// Post Title Start |
| 99 |
$post_title_styles = $this->get_post_title_css( $attributes ); |
| 100 |
$css_generator->add_class_styles( |
| 101 |
'{{WRAPPER}} .ablocks-taxonomy-posts a', |
| 102 |
$post_title_styles, |
| 103 |
$this->get_post_title_css( $attributes, 'Tablet' ), |
| 104 |
$this->get_post_title_css( $attributes, 'Mobile' ) |
| 105 |
); |
| 106 |
|
| 107 |
$post_hover_styles = $this->get_post_hover_title_css( $attributes ); |
| 108 |
$css_generator->add_class_styles( |
| 109 |
'{{WRAPPER}} .ablocks-taxonomy-posts a:hover', |
| 110 |
$post_hover_styles, |
| 111 |
$this->get_post_hover_title_css( $attributes, 'Tablet' ), |
| 112 |
$this->get_post_hover_title_css( $attributes, 'Mobile' ) |
| 113 |
); |
| 114 |
$post_active_styles = $this->get_post_active_title_css( $attributes ); |
| 115 |
$css_generator->add_class_styles( |
| 116 |
'{{WRAPPER}} .ablocks-taxonomy-posts a.active-post-link', |
| 117 |
$post_active_styles, |
| 118 |
$this->get_post_active_title_css( $attributes, 'Tablet' ), |
| 119 |
$this->get_post_active_title_css( $attributes, 'Mobile' ) |
| 120 |
); |
| 121 |
// Post Title End |
| 122 |
|
| 123 |
$post_count_styles = $this->get_post_title_count_css( $attributes ); |
| 124 |
$css_generator->add_class_styles( |
| 125 |
'{{WRAPPER}} .ablocks-taxonomy-title__post-count', |
| 126 |
$post_count_styles, |
| 127 |
$this->get_post_title_count_css( $attributes, 'Tablet' ), |
| 128 |
$this->get_post_title_count_css( $attributes, 'Mobile' ) |
| 129 |
); |
| 130 |
|
| 131 |
$post_count_hover_styles = $this->get_post_title_hover_count_css( $attributes ); |
| 132 |
$css_generator->add_class_styles( |
| 133 |
'{{WRAPPER}} .ablocks-taxonomy-title__post-count:hover', |
| 134 |
$post_count_hover_styles, |
| 135 |
$this->get_post_title_hover_count_css( $attributes, 'Tablet' ), |
| 136 |
$this->get_post_title_hover_count_css( $attributes, 'Mobile' ) |
| 137 |
); |
| 138 |
|
| 139 |
$button_styles = $this->get_button_css( $attributes ); |
| 140 |
$css_generator->add_class_styles( |
| 141 |
'{{WRAPPER}} .ablocks-reload-button', |
| 142 |
$button_styles, |
| 143 |
$this->get_button_css( $attributes, 'Tablet' ), |
| 144 |
$this->get_button_css( $attributes, 'Mobile' ) |
| 145 |
); |
| 146 |
|
| 147 |
$button_hover_styles = $this->get_button_hover_css( $attributes ); |
| 148 |
$css_generator->add_class_styles( |
| 149 |
'{{WRAPPER}} .ablocks-reload-button:hover', |
| 150 |
$button_hover_styles, |
| 151 |
$this->get_button_hover_css( $attributes, 'Tablet' ), |
| 152 |
$this->get_button_hover_css( $attributes, 'Mobile' ) |
| 153 |
); |
| 154 |
// Icon Style |
| 155 |
|
| 156 |
$css_generator->add_class_styles( |
| 157 |
'{{WRAPPER}} .ablocks-icon-wrap', |
| 158 |
Icon::get_wrapper_css( $attributes ), |
| 159 |
Icon::get_wrapper_css( $attributes, 'Tablet' ), |
| 160 |
Icon::get_wrapper_css( $attributes, 'Mobile' ) |
| 161 |
); |
| 162 |
|
| 163 |
$css_generator->add_class_styles( |
| 164 |
'{{WRAPPER}} .ablocks-icon-wrap img.ablocks-image-icon', |
| 165 |
Icon::get_element_image_css( $attributes ), |
| 166 |
Icon::get_element_image_css( $attributes, 'Tablet' ), |
| 167 |
Icon::get_element_image_css( $attributes, 'Mobile' ) |
| 168 |
); |
| 169 |
$css_generator->add_class_styles( |
| 170 |
'{{WRAPPER}} .ablocks-icon-wrap img.ablocks-image-icon:hover', |
| 171 |
Icon::get_element_image_hover_css( $attributes ), |
| 172 |
Icon::get_element_image_hover_css( $attributes, 'Tablet' ), |
| 173 |
Icon::get_element_image_hover_css( $attributes, 'Mobile' ), |
| 174 |
); |
| 175 |
$css_generator->add_class_styles( |
| 176 |
'{{WRAPPER}} .ablocks-icon-wrap svg.ablocks-svg-icon', |
| 177 |
Icon::get_element_css( $attributes ), |
| 178 |
Icon::get_element_css( $attributes, 'Tablet' ), |
| 179 |
Icon::get_element_css( $attributes, 'Mobile' ) |
| 180 |
); |
| 181 |
$css_generator->add_class_styles( |
| 182 |
'{{WRAPPER}} .ablocks-icon-wrap svg.ablocks-svg-icon:hover', |
| 183 |
Icon::get_element_image_hover_css( $attributes ), |
| 184 |
Icon::get_element_image_hover_css( $attributes, 'Tablet' ), |
| 185 |
Icon::get_element_image_hover_css( $attributes, 'Mobile' ) |
| 186 |
); |
| 187 |
// Layout grid |
| 188 |
|
| 189 |
if ( $attributes['taxonomyLayout'] === 'flex' ) { |
| 190 |
$main_wrapper_css = $this->get_card_wrapper( $attributes ); |
| 191 |
$css_generator->add_class_styles( |
| 192 |
'{{WRAPPER}} .ablocks-taxonomy-listing_flex', |
| 193 |
$main_wrapper_css, |
| 194 |
$this->get_card_wrapper( $attributes, 'Tablet' ), |
| 195 |
$this->get_card_wrapper( $attributes, 'Mobile' ) |
| 196 |
); |
| 197 |
} |
| 198 |
|
| 199 |
return $css_generator->generate_css(); |
| 200 |
} |
| 201 |
|
| 202 |
public function get_wrapper_css( $attributes, $device = '' ) { |
| 203 |
return isset( $attributes['alignment'] ) |
| 204 |
? Alignment::get_css( $attributes['alignment'], 'text-align', $device ) |
| 205 |
: []; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
public function get_taxonomy_title_css( $attributes, $device = '' ) { |
| 210 |
$css = []; |
| 211 |
|
| 212 |
// Color |
| 213 |
if ( ! empty( $attributes['taxonomyTitlecolor'] ) ) { |
| 214 |
$css['color'] = $attributes['taxonomyTitlecolor']; |
| 215 |
} |
| 216 |
|
| 217 |
// Background |
| 218 |
if ( ! empty( $attributes['taxonomyTitletBgColor'] ) ) { |
| 219 |
$css['background'] = $attributes['taxonomyTitletBgColor']; |
| 220 |
} |
| 221 |
|
| 222 |
// Justify content (alignment) |
| 223 |
if ( isset( $attributes['taxonomyTitlePosition'][ 'value' . $device ] ) ) { |
| 224 |
$css['justify-content'] = $attributes['taxonomyTitlePosition'][ 'value' . $device ]; |
| 225 |
} |
| 226 |
|
| 227 |
// Flex direction |
| 228 |
if ( isset( $attributes['taxonomyTitleDirection'][ 'value' . $device ] ) ) { |
| 229 |
$css['flex-direction'] = $attributes['taxonomyTitleDirection'][ 'value' . $device ]; |
| 230 |
} |
| 231 |
$typographyValueGlobal = ! empty( $attributes['taxonomyTitleTypographyGlobal'] ) ? $attributes['taxonomyTitleTypographyGlobal'] : []; |
| 232 |
return array_merge( |
| 233 |
Typography::get_css( $attributes['taxonomyTitleTypography'] ?? [], '', $device, $typographyValueGlobal ), |
| 234 |
Border::get_css( $attributes['taxonomyTitleBorder'] ?? [], '', $device ), |
| 235 |
Dimensions::get_css( $attributes['taxonomyTitlePadding'] ?? [], 'padding', $device ), |
| 236 |
$css |
| 237 |
); |
| 238 |
} |
| 239 |
|
| 240 |
public function get_taxonomy_hover_title_css( $attributes, $device = '' ) { |
| 241 |
$css = []; |
| 242 |
|
| 243 |
$taxonomy_title_border = ! empty( $attributes['taxonomyTitleBorder'] ) ? Border::get_hover_css( $attributes['taxonomyTitleBorder'], '', $device ) : array(); |
| 244 |
|
| 245 |
return array_merge( |
| 246 |
$taxonomy_title_border |
| 247 |
); |
| 248 |
} |
| 249 |
|
| 250 |
public function get_post_title_count_css( $attributes, $device = '' ) { |
| 251 |
$css = []; |
| 252 |
|
| 253 |
if ( ! empty( $attributes['postCountColor'] ) ) { |
| 254 |
$css['color'] = $attributes['postCountColor']; |
| 255 |
} |
| 256 |
if ( ! empty( $attributes['postCountBgColor'] ) ) { |
| 257 |
$css['background'] = $attributes['postCountBgColor']; |
| 258 |
} |
| 259 |
if ( ! empty( $attributes['postCountWidth'] ) ) { |
| 260 |
$css['width'] = $attributes['postCountWidth'] . 'px'; |
| 261 |
} |
| 262 |
if ( ! empty( $attributes['postCountWidth'] ) ) { |
| 263 |
$css['height'] = $attributes['postCountWidth'] . 'px'; |
| 264 |
} |
| 265 |
$typographyValueGlobal = ! empty( $attributes['postCountTypographyGlobal'] ) ? $attributes['postCountTypographyGlobal'] : []; |
| 266 |
return array_merge( |
| 267 |
Border::get_css( $attributes['postCountBorder'] ?? [], '', $device ), |
| 268 |
Dimensions::get_css( $attributes['padding'] ?? [], 'padding', $device ), |
| 269 |
Typography::get_css( $attributes['postCountTypography'] ?? [], '', $device, $typographyValueGlobal ), |
| 270 |
$css |
| 271 |
); |
| 272 |
} |
| 273 |
|
| 274 |
public function get_post_title_hover_count_css( $attributes, $device = '' ) { |
| 275 |
$post_count_hover_border_style = ! empty( $attributes['postCountBorder'] ) ? Border::get_hover_css( $attributes['postCountBorder'], '', $device ) : array(); |
| 276 |
return array_merge( |
| 277 |
$post_count_hover_border_style |
| 278 |
); |
| 279 |
} |
| 280 |
|
| 281 |
public function get_card_css( $attributes, $device = '' ) { |
| 282 |
$css = []; |
| 283 |
|
| 284 |
if ( ! empty( $attributes['cardBgColor'] ) ) { |
| 285 |
$css['background'] = $attributes['cardBgColor']; |
| 286 |
} |
| 287 |
|
| 288 |
return array_merge( |
| 289 |
Border::get_css( $attributes['cardBorder'] ?? [], '', $device ), |
| 290 |
BoxShadow::get_css( $attributes['cardBoxShadow'] ?? [], '', $device ), |
| 291 |
$css |
| 292 |
); |
| 293 |
} |
| 294 |
|
| 295 |
public function get_card_hover_css( $attributes, $device = '' ) { |
| 296 |
$css = []; |
| 297 |
$card_hover_border = ! empty( $attributes['cardBorder'] ) ? Border::get_hover_css( $attributes['cardBorder'], '', $device ) : array(); |
| 298 |
if ( ! empty( $attributes['cardBgHoverColor'] ) ) { |
| 299 |
$css['background'] = $attributes['cardBgHoverColor']; |
| 300 |
} |
| 301 |
|
| 302 |
return array_merge( |
| 303 |
$css, |
| 304 |
$card_hover_border, |
| 305 |
BoxShadow::get_hover_css( $attributes['cardBoxShadow'], '', $device ), |
| 306 |
); |
| 307 |
} |
| 308 |
|
| 309 |
public function get_card_padding_css( $attributes, $device = '' ) { |
| 310 |
return Dimensions::get_css( $attributes['cardPadding'] ?? [], 'padding', $device ); |
| 311 |
} |
| 312 |
|
| 313 |
// Taxonomy Title Start |
| 314 |
public function get_post_title_css( $attributes, $device = '' ) { |
| 315 |
$css = []; |
| 316 |
|
| 317 |
if ( ! empty( $attributes['postTitleColor'] ) ) { |
| 318 |
$css['color'] = $attributes['postTitleColor']; |
| 319 |
} |
| 320 |
|
| 321 |
if ( ! empty( $attributes['postBackgroundColor'] ) ) { |
| 322 |
$css['background'] = $attributes['postBackgroundColor']; |
| 323 |
} |
| 324 |
$typographyValueGlobal = ! empty( $attributes['postTitleTypographyGlobal'] ) ? $attributes['postTitleTypographyGlobal'] : []; |
| 325 |
|
| 326 |
$typography = Typography::get_css($attributes['postTitleTypography'] ?? [], [ |
| 327 |
'weight' => '500', |
| 328 |
], $device, $typographyValueGlobal); |
| 329 |
|
| 330 |
$postpadding = Dimensions::get_css( $attributes['postPadding'] ?? [], 'padding', $device ); |
| 331 |
|
| 332 |
$border = Border::get_css($attributes['postBorder'] ?? [], [ |
| 333 |
'unitWidth' => 'px', |
| 334 |
'unitRadius' => 'px', |
| 335 |
], $device); |
| 336 |
|
| 337 |
return array_merge( |
| 338 |
$css, |
| 339 |
$typography, |
| 340 |
$postpadding, |
| 341 |
$border |
| 342 |
); |
| 343 |
} |
| 344 |
|
| 345 |
public function get_post_hover_title_css( $attributes, $device = '' ) { |
| 346 |
$css = []; |
| 347 |
|
| 348 |
if ( ! empty( $attributes['postTitleHoverColor'] ) ) { |
| 349 |
$css['color'] = $attributes['postTitleHoverColor']; |
| 350 |
} |
| 351 |
|
| 352 |
if ( ! empty( $attributes['hoverPostBackgroundColor'] ) ) { |
| 353 |
$css['background'] = $attributes['hoverPostBackgroundColor']; |
| 354 |
} |
| 355 |
|
| 356 |
return $css; |
| 357 |
} |
| 358 |
|
| 359 |
public function get_post_active_title_css( $attributes, $device = '' ) { |
| 360 |
$css = []; |
| 361 |
|
| 362 |
if ( ! empty( $attributes['postTitleActiveColor'] ) ) { |
| 363 |
$css['color'] = $attributes['postTitleActiveColor']; |
| 364 |
} |
| 365 |
|
| 366 |
if ( ! empty( $attributes['activePostBackgroundColor'] ) ) { |
| 367 |
$css['background'] = $attributes['activePostBackgroundColor']; |
| 368 |
} |
| 369 |
|
| 370 |
$border = Border::get_css($attributes['activePostBorder'] ?? [], [ |
| 371 |
'unitWidth' => 'px', |
| 372 |
'unitRadius' => 'px', |
| 373 |
], $device); |
| 374 |
|
| 375 |
return array_merge( |
| 376 |
$css, |
| 377 |
$border |
| 378 |
); |
| 379 |
} |
| 380 |
|
| 381 |
// Post Title End |
| 382 |
public function get_button_css( $attributes, $device = '' ) { |
| 383 |
$css = []; |
| 384 |
|
| 385 |
if ( ! empty( $attributes['buttonColor'] ) ) { |
| 386 |
$css['color'] = $attributes['buttonColor']; |
| 387 |
} |
| 388 |
|
| 389 |
if ( ! empty( $attributes['buttonBgColor'] ) ) { |
| 390 |
$css['background'] = $attributes['buttonBgColor']; |
| 391 |
} |
| 392 |
|
| 393 |
// Display |
| 394 |
if ( isset( $attributes['buttonPosition'][ 'value' . $device ] ) ) { |
| 395 |
$css['display'] = $attributes['buttonPosition'][ 'value' . $device ]; |
| 396 |
} |
| 397 |
$typographyValueGlobal = ! empty( $attributes['buttonTypographyGlobal'] ) ? $attributes['buttonTypographyGlobal'] : []; |
| 398 |
|
| 399 |
return array_merge( |
| 400 |
Typography::get_css( $attributes['buttonTypography'] ?? [], '', $device, $typographyValueGlobal ), |
| 401 |
Border::get_css( $attributes['buttonBorder'] ?? [], '', $device ), |
| 402 |
Dimensions::get_css( $attributes['buttonPadding'] ?? [], 'padding', $device ), |
| 403 |
$css |
| 404 |
); |
| 405 |
} |
| 406 |
|
| 407 |
public function get_button_hover_css( $attributes, $device = '' ) { |
| 408 |
$css = []; |
| 409 |
$button_hover_border = ! empty( $attributes['buttonBorder'] ) ? Border::get_hover_css( $attributes['buttonBorder'], '', $device ) : array(); |
| 410 |
if ( ! empty( $attributes['buttonHoverColor'] ) ) { |
| 411 |
$css['color'] = $attributes['buttonHoverColor']; |
| 412 |
} |
| 413 |
if ( ! empty( $attributes['buttonHoverBgColor'] ) ) { |
| 414 |
$css['background'] = $attributes['buttonHoverBgColor']; |
| 415 |
} |
| 416 |
|
| 417 |
return array_merge( |
| 418 |
$button_hover_border, |
| 419 |
$css |
| 420 |
); |
| 421 |
} |
| 422 |
|
| 423 |
// Icon Style |
| 424 |
|
| 425 |
public function get_icons( $attributes, $device = '' ) { |
| 426 |
$css = []; |
| 427 |
|
| 428 |
if ( ! empty( $attributes['iconBgColor'] ) ) { |
| 429 |
$css['background'] = $attributes['iconBgColor']; |
| 430 |
} |
| 431 |
|
| 432 |
return array_merge( |
| 433 |
Dimensions::get_css( $attributes['iconPadding'] ?? [], 'padding', $device ), |
| 434 |
Dimensions::get_css( $attributes['iconBorderRadius'] ?? [], 'border-radius', $device ), |
| 435 |
$css |
| 436 |
); |
| 437 |
} |
| 438 |
|
| 439 |
public function get_icons_svg( $attributes, $device = '' ) { |
| 440 |
$css = []; |
| 441 |
if ( ! empty( $attributes['iconsSize'] ) ) { |
| 442 |
$css['width'] = $attributes['iconsSize'] . 'px'; |
| 443 |
} |
| 444 |
if ( ! empty( $attributes['iconsSize'] ) ) { |
| 445 |
$css['height'] = $attributes['iconsSize'] . 'px'; |
| 446 |
} |
| 447 |
|
| 448 |
return array_merge( |
| 449 |
$css |
| 450 |
); |
| 451 |
} |
| 452 |
|
| 453 |
public function get_icon_svg_path( $attributes, $device = '' ) { |
| 454 |
$css = []; |
| 455 |
if ( ! empty( $attributes['iconColor'] ) ) { |
| 456 |
$css['fill'] = $attributes['iconColor']; |
| 457 |
} |
| 458 |
|
| 459 |
return array_merge( |
| 460 |
$css |
| 461 |
); |
| 462 |
} |
| 463 |
|
| 464 |
public function get_card_wrapper( $attributes, $device = '' ) { |
| 465 |
$css = []; |
| 466 |
if ( $attributes['taxonomyLayout'] === 'flex' ) { |
| 467 |
$css['display'] = 'grid'; |
| 468 |
$css['gap'] = '1.5em'; |
| 469 |
if ( $device === 'Tablet' ) { |
| 470 |
$css['grid-template-columns'] = 'repeat(2, 1fr)'; |
| 471 |
} elseif ( $device === 'Mobile' ) { |
| 472 |
$css['grid-template-columns'] = 'repeat(1, 1fr)'; |
| 473 |
} elseif ( ! empty( $attributes['itemsPerRow'] ) ) { |
| 474 |
$css['grid-template-columns'] = 'repeat(' . $attributes['itemsPerRow'] . ', 1fr)'; |
| 475 |
} |
| 476 |
} |
| 477 |
return array_merge( |
| 478 |
$css |
| 479 |
); |
| 480 |
} |
| 481 |
|
| 482 |
public function render_callback( $attributes, $content, $block_instance ) { |
| 483 |
$content = ''; |
| 484 |
return parent::render_callback( $attributes, $content, $block_instance ); |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
// Markup Start |
| 489 |
|
| 490 |
protected function inherit_selected_terms( array $active_taxonomy, array $selected_terms, string $selected_post_type, bool $show_inherit ): array { |
| 491 |
if ( ! $show_inherit || empty( $active_taxonomy ) ) { |
| 492 |
return $selected_terms; |
| 493 |
} |
| 494 |
|
| 495 |
$queried_object = get_queried_object(); |
| 496 |
|
| 497 |
if ( is_tax() || is_post_type_archive() ) { |
| 498 |
foreach ( $active_taxonomy as $taxonomy ) { |
| 499 |
if ( isset( $queried_object->taxonomy ) && $queried_object->taxonomy === $taxonomy ) { |
| 500 |
$selected_terms[ $taxonomy ] = [ $queried_object->term_id ]; |
| 501 |
} else { |
| 502 |
$terms = get_terms([ |
| 503 |
'taxonomy' => $taxonomy, |
| 504 |
'hide_empty' => true |
| 505 |
]); |
| 506 |
if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) { |
| 507 |
$selected_terms[ $taxonomy ] = wp_list_pluck( $terms, 'term_id' ); |
| 508 |
} |
| 509 |
} |
| 510 |
} |
| 511 |
} elseif ( is_singular( $selected_post_type ) && $queried_object instanceof \WP_Post ) { |
| 512 |
foreach ( $active_taxonomy as $taxonomy ) { |
| 513 |
$terms = get_the_terms( $queried_object->ID, $taxonomy ); |
| 514 |
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) { |
| 515 |
$selected_terms[ $taxonomy ] = wp_list_pluck( $terms, 'term_id' ); |
| 516 |
} |
| 517 |
} |
| 518 |
} else { |
| 519 |
foreach ( $active_taxonomy as $taxonomy ) { |
| 520 |
$terms = get_terms([ |
| 521 |
'taxonomy' => $taxonomy, |
| 522 |
'hide_empty' => true |
| 523 |
]); |
| 524 |
if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) { |
| 525 |
$selected_terms[ $taxonomy ] = wp_list_pluck( $terms, 'term_id' ); |
| 526 |
} |
| 527 |
} |
| 528 |
}//end if |
| 529 |
|
| 530 |
return $selected_terms; |
| 531 |
} |
| 532 |
|
| 533 |
/** |
| 534 |
* Taxonomy Wise Query |
| 535 |
*/ |
| 536 |
protected function build_term_query( string $taxonomy, int $term_id, string $post_type, int $per_page, string $orderby, string $order ): \WP_Query { |
| 537 |
return new \WP_Query([ |
| 538 |
'post_type' => $post_type, |
| 539 |
'post_status' => 'publish', |
| 540 |
'posts_per_page' => $per_page, |
| 541 |
'orderby' => $orderby, |
| 542 |
'order' => $order, |
| 543 |
'tax_query' => [ |
| 544 |
[ |
| 545 |
'taxonomy' => $taxonomy, |
| 546 |
'field' => 'term_id', |
| 547 |
'terms' => $term_id, |
| 548 |
] |
| 549 |
], |
| 550 |
]); |
| 551 |
} |
| 552 |
|
| 553 |
/** |
| 554 |
* Accordion Open Use current post ID Check |
| 555 |
*/ |
| 556 |
protected function should_open_section( \WP_Query $query, int $current_post_id ): bool { |
| 557 |
if ( ! $current_post_id || ! $query->have_posts() ) { |
| 558 |
return false; |
| 559 |
} |
| 560 |
$open = false; |
| 561 |
while ( $query->have_posts() ) { |
| 562 |
$query->the_post(); |
| 563 |
if ( get_the_ID() === $current_post_id ) { |
| 564 |
$open = true; |
| 565 |
break; |
| 566 |
} |
| 567 |
} |
| 568 |
$query->rewind_posts(); |
| 569 |
wp_reset_postdata(); |
| 570 |
return $open; |
| 571 |
} |
| 572 |
|
| 573 |
/** |
| 574 |
* Post ID Use Active class |
| 575 |
*/ |
| 576 |
protected function render_posts_list( \WP_Query $query, bool $show_post, int $current_post_id ): void { |
| 577 |
if ( ! $query->have_posts() || ! $show_post ) { |
| 578 |
return; |
| 579 |
} |
| 580 |
echo '<ul class="ablocks-taxonomy-posts">'; |
| 581 |
while ( $query->have_posts() ) { |
| 582 |
$query->the_post(); |
| 583 |
$post_link = get_permalink(); |
| 584 |
$is_active = ( get_the_ID() === $current_post_id ) ? ' active-post-link' : ''; |
| 585 |
echo '<a class="ablocks-post-link' . esc_attr( $is_active ) . '" href="' . esc_url( $post_link ) . '" rel="noreferrer">' . esc_html( get_the_title() ) . '</a>'; |
| 586 |
} |
| 587 |
wp_reset_postdata(); |
| 588 |
echo '</ul>'; |
| 589 |
} |
| 590 |
|
| 591 |
/** |
| 592 |
* Reload Button |
| 593 |
*/ |
| 594 |
protected function render_reload_button( bool $enable_reload, $term_link, string $button_text ): void { |
| 595 |
if ( ! $enable_reload || is_wp_error( $term_link ) ) { |
| 596 |
return; |
| 597 |
} |
| 598 |
echo '<div class="ablocks-reload-button-wrapper">'; |
| 599 |
echo '<a href="' . esc_url( $term_link ) . '" class="ablocks-reload-button">' . esc_html( $button_text ) . '</a>'; |
| 600 |
echo '</div>'; |
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
public function render_block_content( $attributes, $content, $block_instance ) { |
| 605 |
$taxonomy_layout = $attributes['taxonomyLayout'] ?? 'list'; |
| 606 |
$page_links = $attributes['pageLinks'] ?? 'singlePagelink'; |
| 607 |
$items_per_row = $attributes['itemsPerRow'] ?? 3; |
| 608 |
$active_taxonomy = $attributes['activeTaxonomy'] ?? []; |
| 609 |
$selected_terms = $attributes['selectedTerms'] ?? []; |
| 610 |
$selected_post_type = $attributes['selectedPostType'] ?? 'post'; |
| 611 |
$number_of_posts = $attributes['numberOfPosts'] ?? 6; |
| 612 |
$post_order = $attributes['postOrder'] ?? 'desc'; |
| 613 |
$post_order_by = $attributes['postOrderBy'] ?? 'date'; |
| 614 |
$show_term_count = $attributes['showTermCount'] ?? false; |
| 615 |
$enable_reload = $attributes['enableReloadButton'] ?? false; |
| 616 |
$allow_icon = $attributes['allowIcon'] ?? false; |
| 617 |
$Button_text = $attributes['buttonText'] ?? 'View More'; |
| 618 |
$tab_view = $attributes['showTab'] ?? false; |
| 619 |
$showTabScrolling = $attributes['showTabScrolling'] ?? false; |
| 620 |
$icon_class = $attributes['iconClass'] ?? 'far fa-copy'; |
| 621 |
$icon_svg = $attributes['iconSvgPath'] ?? ''; |
| 622 |
$icon_url = $attributes['iconImageUrl'] ?? ''; |
| 623 |
$show_post_link = $attributes['showPostLink'] ?? false; |
| 624 |
$show_post = $attributes['showPost'] ?? false; |
| 625 |
$show_inherit = $attributes['showInherit'] ?? false; |
| 626 |
$show_Count_Prefix = $attributes['showTermCountPrefix'] ?? false; |
| 627 |
$showIconDffent = $attributes['showIconDffent'] ?? false; |
| 628 |
$count_prefix_text = $attributes['countPrefixText'] ?? 'Items'; |
| 629 |
|
| 630 |
$selected_terms = $this->inherit_selected_terms( $active_taxonomy, $selected_terms, $selected_post_type, $show_inherit ); |
| 631 |
|
| 632 |
ob_start(); |
| 633 |
|
| 634 |
$inline_style = ''; |
| 635 |
$use_accordion = ( $taxonomy_layout === 'list' && $tab_view ); |
| 636 |
|
| 637 |
$current_post_id = get_queried_object_id(); |
| 638 |
$current_term_id = 0; |
| 639 |
$current_taxonomy = ''; |
| 640 |
if ( is_category() || is_tag() || is_tax() ) { |
| 641 |
$current_term = get_queried_object(); |
| 642 |
if ( $current_term && ! is_wp_error( $current_term ) ) { |
| 643 |
$current_term_id = (int) $current_term->term_id; |
| 644 |
$current_taxonomy = (string) $current_term->taxonomy; |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
$showTabScrollingtrue = $showTabScrolling ? 'ablocks-taxonomy-listing-scrolling' : ''; |
| 649 |
|
| 650 |
?> |
| 651 |
<div class="ablocks-taxonomy-listing_<?php echo esc_attr( $taxonomy_layout ); ?> <?php echo esc_attr( $showTabScrollingtrue ); ?>" <?php |
| 652 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 653 |
echo $inline_style; ?>> |
| 654 |
|
| 655 |
<?php |
| 656 |
$accordion_index = 1; |
| 657 |
$accordion_name = 'ablocks-blower-group-' . ( $attributes['block_id'] ?? '' ); |
| 658 |
|
| 659 |
// Prime term caches for every listed term in one query so the per-term |
| 660 |
// get_term()/get_term_link()/get_taxonomy() calls below are served from |
| 661 |
// cache instead of hitting the DB per term. |
| 662 |
$all_term_ids = []; |
| 663 |
foreach ( $active_taxonomy as $prime_taxonomy ) { |
| 664 |
foreach ( ( $selected_terms[ $prime_taxonomy ] ?? [] ) as $prime_term_id ) { |
| 665 |
$all_term_ids[] = (int) $prime_term_id; |
| 666 |
} |
| 667 |
} |
| 668 |
if ( ! empty( $all_term_ids ) ) { |
| 669 |
_prime_term_caches( array_values( array_unique( $all_term_ids ) ) ); |
| 670 |
} |
| 671 |
|
| 672 |
foreach ( $active_taxonomy as $taxonomy ) : |
| 673 |
$term_ids = $selected_terms[ $taxonomy ] ?? []; |
| 674 |
|
| 675 |
foreach ( $term_ids as $term_id ) : |
| 676 |
$term = get_term( $term_id, $taxonomy ); |
| 677 |
|
| 678 |
if ( ! $term || is_wp_error( $term ) ) { |
| 679 |
continue; } |
| 680 |
|
| 681 |
$query = $this->build_term_query( $taxonomy, (int) $term_id, $selected_post_type, (int) $number_of_posts, $post_order_by, $post_order ); |
| 682 |
$term_public_count = (int) $query->found_posts; |
| 683 |
|
| 684 |
$input_id = 'ablocks-blower-' . $accordion_index++; |
| 685 |
$should_open = $this->should_open_section( $query, (int) $current_post_id ); |
| 686 |
if ( |
| 687 |
$use_accordion && |
| 688 |
$showTabScrolling && |
| 689 |
$current_term_id && |
| 690 |
$current_taxonomy === $taxonomy && |
| 691 |
(int) $term_id === $current_term_id |
| 692 |
) { |
| 693 |
$should_open = true; |
| 694 |
} |
| 695 |
|
| 696 |
$term_link = get_term_link( $term ); |
| 697 |
|
| 698 |
$taxonomy = $term->taxonomy; |
| 699 |
$tax_obj = get_taxonomy( $taxonomy ); |
| 700 |
$post_types = ( $tax_obj && ! empty( $tax_obj->object_type ) ) |
| 701 |
? array_values( array_unique( $tax_obj->object_type ) ) |
| 702 |
: 'any'; |
| 703 |
|
| 704 |
$post_ids = get_posts( [ |
| 705 |
'post_type' => $post_types, |
| 706 |
'fields' => 'ids', |
| 707 |
'post_status' => 'publish', |
| 708 |
'posts_per_page' => 1, |
| 709 |
'orderby' => $post_order_by, |
| 710 |
'order' => $post_order, |
| 711 |
'ignore_sticky_posts' => true, |
| 712 |
'no_found_rows' => true, |
| 713 |
'tax_query' => [ |
| 714 |
[ |
| 715 |
'taxonomy' => $taxonomy, |
| 716 |
'field' => 'term_id', |
| 717 |
'terms' => (int) $term->term_id, |
| 718 |
], |
| 719 |
], |
| 720 |
] ); |
| 721 |
|
| 722 |
$first_post_id = ! empty( $post_ids ) ? (int) $post_ids[0] : 0; |
| 723 |
$first_post_link = $first_post_id ? get_permalink( $first_post_id ) : ''; |
| 724 |
|
| 725 |
$link_url = ''; |
| 726 |
if ( $show_post_link && ! is_wp_error( $term_link ) ) { |
| 727 |
if ( $page_links === 'singlePagelink' && $first_post_link ) { |
| 728 |
$link_url = $first_post_link; |
| 729 |
} elseif ( $page_links === 'archivePagelink' ) { |
| 730 |
$link_url = $term_link; |
| 731 |
} |
| 732 |
} |
| 733 |
?> |
| 734 |
|
| 735 |
<?php if ( $use_accordion ) : ?> |
| 736 |
<div class="ablocks-taxonomy-blower ablocks-taxonomy-listing-item"> |
| 737 |
<input type="checkbox" name="<?php echo esc_attr( $accordion_name ); ?>" id="<?php echo esc_attr( $input_id ); ?>" <?php echo $should_open ? 'checked' : ''; ?>> |
| 738 |
<?php if ( $link_url ) : |
| 739 |
?><a class="ablocks-taxonomy-title__link" href="<?php echo esc_url( $link_url ); ?>"><?php endif; ?> |
| 740 |
<label for="<?php echo esc_attr( $input_id ); ?>" class="ablocks-taxonomy-blower__label ablocks-taxonomy-title"> |
| 741 |
<?php else : ?> |
| 742 |
<div class="ablocks-taxonomy-listing-item"> |
| 743 |
<?php if ( $link_url ) : |
| 744 |
?><a class="ablocks-taxonomy-title__link" href="<?php echo esc_url( $link_url ); ?>"><?php endif; ?> |
| 745 |
<h3 class="ablocks-taxonomy-title"> |
| 746 |
<?php endif; ?> |
| 747 |
|
| 748 |
<?php if ( $allow_icon && ( $showIconDffent == true ) ) : ?> |
| 749 |
<span class="ablocks-taxonomy-icon"> |
| 750 |
<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" fill="#000000" viewBox="0 0 256 256"><path d="M245,110.64A16,16,0,0,0,232,104H216V88a16,16,0,0,0-16-16H130.67L102.94,51.2a16.14,16.14,0,0,0-9.6-3.2H40A16,16,0,0,0,24,64V208h0a8,8,0,0,0,8,8H211.1a8,8,0,0,0,7.59-5.47l28.49-85.47A16.05,16.05,0,0,0,245,110.64ZM93.34,64,123.2,86.4A8,8,0,0,0,128,88h72v16H69.77a16,16,0,0,0-15.18,10.94L40,158.7V64Zm112,136H43.1l26.67-80H232Z"></path></svg> |
| 751 |
</span> |
| 752 |
<?php endif; ?> |
| 753 |
|
| 754 |
<span class="<?php echo $showIconDffent === true ? 'ablocks-taxonomy-listing_direction' : ''; ?>"> |
| 755 |
<?php if ( $allow_icon && ( $showIconDffent == false ) ) : ?> |
| 756 |
<span class="ablocks-taxonomy-icon"> |
| 757 |
<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" fill="#000000" viewBox="0 0 256 256"><path d="M245,110.64A16,16,0,0,0,232,104H216V88a16,16,0,0,0-16-16H130.67L102.94,51.2a16.14,16.14,0,0,0-9.6-3.2H40A16,16,0,0,0,24,64V208h0a8,8,0,0,0,8,8H211.1a8,8,0,0,0,7.59-5.47l28.49-85.47A16.05,16.05,0,0,0,245,110.64ZM93.34,64,123.2,86.4A8,8,0,0,0,128,88h72v16H69.77a16,16,0,0,0-15.18,10.94L40,158.7V64Zm112,136H43.1l26.67-80H232Z"></path></svg> |
| 758 |
</span> |
| 759 |
<?php endif; ?> |
| 760 |
|
| 761 |
<?php |
| 762 |
echo esc_html( $term->name ); |
| 763 |
|
| 764 |
if ( $show_term_count && ( $showIconDffent == true ) ) : |
| 765 |
?> |
| 766 |
<span class="ablocks-taxonomy-title__post-count"> |
| 767 |
|
| 768 |
<?php echo $term_public_count; ?><?php if ( $show_Count_Prefix ) : ?> |
| 769 |
<span><?php echo esc_html( $count_prefix_text ); ?></span> |
| 770 |
<?php endif; ?> |
| 771 |
</span> |
| 772 |
<?php endif; ?> |
| 773 |
</span> |
| 774 |
|
| 775 |
<?php if ( $show_term_count && ( $showIconDffent == false ) ) : ?> |
| 776 |
<span class="ablocks-taxonomy-title__post-count"> |
| 777 |
<?php echo $term_public_count; ?><?php if ( $show_Count_Prefix ) : ?> |
| 778 |
<span><?php echo esc_html( $count_prefix_text ); ?></span> |
| 779 |
<?php endif; ?> |
| 780 |
</span> |
| 781 |
<?php endif; ?> |
| 782 |
|
| 783 |
<?php |
| 784 |
echo $use_accordion ? '</label>' : '</h3>'; |
| 785 |
|
| 786 |
if ( $link_url ) { |
| 787 |
echo '</a>'; |
| 788 |
} |
| 789 |
?> |
| 790 |
<?php if ( $show_post ) : |
| 791 |
|
| 792 |
echo $use_accordion ? '<div class="ablocks-taxonomy-blower__content ablocks-taxonomy-listing-item_content">' : '<div class="ablocks-taxonomy-listing-item_content">'; ?> |
| 793 |
|
| 794 |
<?php if ( $query->have_posts() ) : ?> |
| 795 |
|
| 796 |
<?php |
| 797 |
|
| 798 |
$this->render_posts_list( $query, $show_post, (int) $current_post_id ); |
| 799 |
|
| 800 |
$this->render_reload_button( (bool) $enable_reload, $term_link, (string) $Button_text ); |
| 801 |
?> |
| 802 |
|
| 803 |
<?php else : ?> |
| 804 |
<p><?php echo esc_html__( 'No posts found.', 'ablocks' ); ?></p> |
| 805 |
<?php endif; ?> |
| 806 |
|
| 807 |
</div> |
| 808 |
<?php endif; ?> |
| 809 |
</div> |
| 810 |
<?php endforeach; ?> |
| 811 |
<?php endforeach; ?> |
| 812 |
|
| 813 |
|
| 814 |
</div> |
| 815 |
<?php |
| 816 |
return ob_get_clean(); |
| 817 |
} |
| 818 |
|
| 819 |
} |
| 820 |
|