| 1 |
<?php |
| 2 |
namespace ABlocks\Blocks\LoopLoadMore; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
use ABlocks\Classes\BlockBaseAbstract; |
| 9 |
use ABlocks\Classes\CssGeneratorV2; |
| 10 |
use ABlocks\Controls\Typography; |
| 11 |
use ABlocks\Controls\Border; |
| 12 |
use ABlocks\Controls\Dimensions; |
| 13 |
use ABlocks\Controls\Range; |
| 14 |
use ABlocks\Controls\Alignment; |
| 15 |
use ABlocks\Controls\BoxShadow; |
| 16 |
use ABlocks\Controls\Color; |
| 17 |
|
| 18 |
class Block extends BlockBaseAbstract { |
| 19 |
protected $parent_block_name = 'loop-builder'; |
| 20 |
protected $block_name = 'loop-load-more'; |
| 21 |
|
| 22 |
public function build_css( $attributes ) { |
| 23 |
|
| 24 |
// Generate CSS start |
| 25 |
$css_generator = new CssGeneratorV2( $attributes, $this->block_name ); |
| 26 |
// button style |
| 27 |
$css_generator->add_class_styles( |
| 28 |
'{{WRAPPER}}', |
| 29 |
$this->loop_load_More_Wrapper( $attributes ), |
| 30 |
$this->loop_load_More_Wrapper( $attributes, 'Tablet' ), |
| 31 |
$this->loop_load_More_Wrapper( $attributes, 'Mobile' ), |
| 32 |
); |
| 33 |
$css_generator->add_class_styles( |
| 34 |
'{{WRAPPER}} .ablocks-loop-load-more__text', |
| 35 |
$this->loop_load_More_button( $attributes ), |
| 36 |
$this->loop_load_More_button( $attributes, 'Tablet' ), |
| 37 |
$this->loop_load_More_button( $attributes, 'Mobile' ) |
| 38 |
); |
| 39 |
$css_generator->add_class_styles( |
| 40 |
'{{WRAPPER}} .ablocks-loop-load-more__text:hover', |
| 41 |
$this->loop_load_More_button_hover( $attributes ), |
| 42 |
$this->loop_load_More_button_hover( $attributes, 'Tablet' ), |
| 43 |
$this->loop_load_More_button_hover( $attributes, 'Mobile' ) |
| 44 |
); |
| 45 |
return $css_generator->generate_css(); |
| 46 |
} |
| 47 |
public function render_block_content( $attributes, $content, $block_instance ) { |
| 48 |
global $post; |
| 49 |
|
| 50 |
$current_post_id = isset( $post->ID ) ? intval( $post->ID ) : 0; |
| 51 |
$load_more_text = isset( $attributes['loadMoreButtonText'] ) ? esc_html( $attributes['loadMoreButtonText'] ) : ''; |
| 52 |
$no_more_items_text = isset( $attributes['noMoreItemsText'] ) ? esc_attr( $attributes['noMoreItemsText'] ) : ''; |
| 53 |
|
| 54 |
if ( ! empty( $attributes['taxonomy'] ) && $attributes['taxonomy'] !== 'inherit' ) { |
| 55 |
$taxonomy = $attributes['taxonomy']; |
| 56 |
} else { |
| 57 |
// Try to get the current queried taxonomy |
| 58 |
$queried_object = get_queried_object(); |
| 59 |
if ( ! empty( $queried_object ) && ! empty( $queried_object->taxonomy ) ) { |
| 60 |
$taxonomy = $queried_object->taxonomy; |
| 61 |
} else { |
| 62 |
$taxonomy = 'category'; // fallback |
| 63 |
} |
| 64 |
} |
| 65 |
$taxonomy = esc_attr( $taxonomy ); |
| 66 |
|
| 67 |
// Determine archive info |
| 68 |
$is_archive = is_archive() ? 'true' : 'false'; |
| 69 |
$archive_post_type = is_post_type_archive() ? get_query_var( 'post_type' ) : get_post_type(); |
| 70 |
if ( is_array( $archive_post_type ) ) { |
| 71 |
$archive_post_type = reset( $archive_post_type ); |
| 72 |
} |
| 73 |
$archive_post_type = esc_attr( $archive_post_type ); |
| 74 |
|
| 75 |
$term_id = null; |
| 76 |
if ( is_tax() || is_category() || is_tag() ) { |
| 77 |
$term = get_queried_object(); |
| 78 |
|
| 79 |
if ( $term instanceof \WP_Term ) { |
| 80 |
$term_id = $term->term_id; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
echo '<span |
| 85 |
class="ablocks-loop-load-more__text" |
| 86 |
data-post-id="' . esc_attr( $current_post_id ) . '" |
| 87 |
data-term-id="' . esc_attr( $term_id ) . '" |
| 88 |
data-no-item-text="' . esc_attr( $no_more_items_text ) . '" |
| 89 |
data-more-button-text="' . esc_attr( $load_more_text ) . '" |
| 90 |
data-taxonomy="' . esc_attr( $taxonomy ) . '" |
| 91 |
data-is-archive="' . esc_attr( $is_archive ) . '" |
| 92 |
data-archive-post-type="' . esc_attr( $archive_post_type ) . '" |
| 93 |
>' . esc_attr( $load_more_text ) . '</span>'; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
private function loop_load_More_Wrapper( $attributes, $device = '' ) { |
| 98 |
$css = []; |
| 99 |
if ( isset( $attributes['moreButtonAlignment'] ) ) { |
| 100 |
$css['justify-content'] = $attributes['moreButtonAlignment']; |
| 101 |
} |
| 102 |
|
| 103 |
return array_merge( |
| 104 |
$css, |
| 105 |
Range::get_css([ |
| 106 |
'attributeValue' => $attributes['loadMoreButtonGap'] ?? null, |
| 107 |
'attribute_object_key' => 'value', |
| 108 |
'isResponsive' => true, |
| 109 |
'hasUnit' => false, |
| 110 |
'unitDefaultValue' => 'px', |
| 111 |
'defaultValue' => '', |
| 112 |
'property' => 'margin-top', |
| 113 |
'device' => $device, |
| 114 |
]) |
| 115 |
); |
| 116 |
} |
| 117 |
private function loop_load_More_button( $attributes, $device = '' ) { |
| 118 |
$typographyGlobal = isset( $attributes['moreButtonTypographyGlobal'] ) ? $attributes['moreButtonTypographyGlobal'] : array(); |
| 119 |
|
| 120 |
return array_merge( |
| 121 |
[ 'color' => Color::get_css( isset( $attributes['loadMoreButtonTextColor'] ) ? $attributes['loadMoreButtonTextColor'] : '' ) ], |
| 122 |
[ 'background' => Color::get_css( isset( $attributes['loadMoreButtonBackground'] ) ? $attributes['loadMoreButtonBackground'] : '' ) ], |
| 123 |
Border::get_css( $attributes['moreButtonBorder'], '', $device ), |
| 124 |
BoxShadow::get_css( $attributes['moreButtonboxShadow'], '', $device ), |
| 125 |
Typography::get_css( $attributes['moreButtonTypography'], '', $device, $typographyGlobal ), |
| 126 |
Dimensions::get_css( $attributes['moreButtonPadding'], 'padding', $device ), |
| 127 |
); |
| 128 |
} |
| 129 |
private function loop_load_More_button_hover( $attributes, $device = '' ) { |
| 130 |
$css = []; |
| 131 |
if ( ! empty( $attributes['loadMoreButtonTransition'] ) ) { |
| 132 |
$css['transition-duration'] = $attributes['loadMoreButtonTransition'] . 's'; |
| 133 |
} |
| 134 |
return array_merge( |
| 135 |
[ 'color' => Color::get_css( isset( $attributes['loadMoreButtonTextColorH'] ) ? $attributes['loadMoreButtonTextColorH'] : '' ) ], |
| 136 |
[ 'background' => Color::get_css( isset( $attributes['loadMoreButtonBackgroundH'] ) ? $attributes['loadMoreButtonBackgroundH'] : '' ) ], |
| 137 |
$css, |
| 138 |
Border::get_hover_css( $attributes['moreButtonBorder'], '', $device ), |
| 139 |
BoxShadow::get_hover_css( $attributes['moreButtonboxShadow'], '', $device ) |
| 140 |
); |
| 141 |
} |
| 142 |
} |
| 143 |
|