| @@ -1,0 +1,144 @@ | ||
| 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 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->loop_load_More_button( $attributes, $device ); } ) | |
| 39 | + ); | |
| 40 | + $css_generator->add_class_styles( | |
| 41 | + '{{WRAPPER}} .ablocks-loop-load-more__text:hover', | |
| 42 | + $this->loop_load_More_button_hover( $attributes ), | |
| 43 | + $this->loop_load_More_button_hover( $attributes, 'Tablet' ), | |
| 44 | + $this->loop_load_More_button_hover( $attributes, 'Mobile' ), | |
| 45 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->loop_load_More_button_hover( $attributes, $device ); } ) | |
| 46 | + ); | |
| 47 | + return $css_generator->generate_css(); | |
| 48 | + } | |
| 49 | + public function render_block_content( $attributes, $content, $block_instance ) { | |
| 50 | + global $post; | |
| 51 | + | |
| 52 | + $current_post_id = isset( $post->ID ) ? intval( $post->ID ) : 0; | |
| 53 | + $load_more_text = isset( $attributes['loadMoreButtonText'] ) ? esc_html( $attributes['loadMoreButtonText'] ) : ''; | |
| 54 | + $no_more_items_text = isset( $attributes['noMoreItemsText'] ) ? esc_attr( $attributes['noMoreItemsText'] ) : ''; | |
| 55 | + | |
| 56 | + if ( ! empty( $attributes['taxonomy'] ) && $attributes['taxonomy'] !== 'inherit' ) { | |
| 57 | + $taxonomy = $attributes['taxonomy']; | |
| 58 | + } else { | |
| 59 | + // Try to get the current queried taxonomy | |
| 60 | + $queried_object = get_queried_object(); | |
| 61 | + if ( ! empty( $queried_object ) && ! empty( $queried_object->taxonomy ) ) { | |
| 62 | + $taxonomy = $queried_object->taxonomy; | |
| 63 | + } else { | |
| 64 | + $taxonomy = 'category'; // fallback | |
| 65 | + } | |
| 66 | + } | |
| 67 | + $taxonomy = esc_attr( $taxonomy ); | |
| 68 | + | |
| 69 | + // Determine archive info | |
| 70 | + $is_archive = is_archive() ? 'true' : 'false'; | |
| 71 | + $archive_post_type = is_post_type_archive() ? get_query_var( 'post_type' ) : get_post_type(); | |
| 72 | + if ( is_array( $archive_post_type ) ) { | |
| 73 | + $archive_post_type = reset( $archive_post_type ); | |
| 74 | + } | |
| 75 | + $archive_post_type = esc_attr( $archive_post_type ); | |
| 76 | + | |
| 77 | + $term_id = null; | |
| 78 | + if ( is_tax() || is_category() || is_tag() ) { | |
| 79 | + $term = get_queried_object(); | |
| 80 | + | |
| 81 | + if ( $term instanceof \WP_Term ) { | |
| 82 | + $term_id = $term->term_id; | |
| 83 | + } | |
| 84 | + } | |
| 85 | + | |
| 86 | + echo '<span | |
| 87 | + class="ablocks-loop-load-more__text" | |
| 88 | + data-post-id="' . esc_attr( $current_post_id ) . '" | |
| 89 | + data-term-id="' . esc_attr( $term_id ) . '" | |
| 90 | + data-no-item-text="' . esc_attr( $no_more_items_text ) . '" | |
| 91 | + data-more-button-text="' . esc_attr( $load_more_text ) . '" | |
| 92 | + data-taxonomy="' . esc_attr( $taxonomy ) . '" | |
| 93 | + data-is-archive="' . esc_attr( $is_archive ) . '" | |
| 94 | + data-archive-post-type="' . esc_attr( $archive_post_type ) . '" | |
| 95 | + >' . esc_attr( $load_more_text ) . '</span>'; | |
| 96 | + } | |
| 97 | + | |
| 98 | + | |
| 99 | + private function loop_load_More_Wrapper( $attributes, $device = '' ) { | |
| 100 | + $css = []; | |
| 101 | + if ( isset( $attributes['moreButtonAlignment'] ) ) { | |
| 102 | + $css['justify-content'] = $attributes['moreButtonAlignment']; | |
| 103 | + } | |
| 104 | + | |
| 105 | + return array_merge( | |
| 106 | + $css, | |
| 107 | + Range::get_css([ | |
| 108 | + 'attributeValue' => $attributes['loadMoreButtonGap'] ?? null, | |
| 109 | + 'attribute_object_key' => 'value', | |
| 110 | + 'isResponsive' => true, | |
| 111 | + 'hasUnit' => false, | |
| 112 | + 'unitDefaultValue' => 'px', | |
| 113 | + 'defaultValue' => '', | |
| 114 | + 'property' => 'margin-top', | |
| 115 | + 'device' => $device, | |
| 116 | + ]) | |
| 117 | + ); | |
| 118 | + } | |
| 119 | + private function loop_load_More_button( $attributes, $device = '' ) { | |
| 120 | + $typographyGlobal = isset( $attributes['moreButtonTypographyGlobal'] ) ? $attributes['moreButtonTypographyGlobal'] : array(); | |
| 121 | + | |
| 122 | + return array_merge( | |
| 123 | + [ 'color' => Color::get_css( isset( $attributes['loadMoreButtonTextColor'] ) ? $attributes['loadMoreButtonTextColor'] : '' ) ], | |
| 124 | + [ 'background' => Color::get_css( isset( $attributes['loadMoreButtonBackground'] ) ? $attributes['loadMoreButtonBackground'] : '' ) ], | |
| 125 | + Border::get_css( $attributes['moreButtonBorder'], '', $device ), | |
| 126 | + BoxShadow::get_css( $attributes['moreButtonboxShadow'], '', $device ), | |
| 127 | + Typography::get_css( $attributes['moreButtonTypography'], '', $device, $typographyGlobal ), | |
| 128 | + Dimensions::get_css( $attributes['moreButtonPadding'], 'padding', $device ), | |
| 129 | + ); | |
| 130 | + } | |
| 131 | + private function loop_load_More_button_hover( $attributes, $device = '' ) { | |
| 132 | + $css = []; | |
| 133 | + if ( ! empty( $attributes['loadMoreButtonTransition'] ) ) { | |
| 134 | + $css['transition-duration'] = $attributes['loadMoreButtonTransition'] . 's'; | |
| 135 | + } | |
| 136 | + return array_merge( | |
| 137 | + [ 'color' => Color::get_css( isset( $attributes['loadMoreButtonTextColorH'] ) ? $attributes['loadMoreButtonTextColorH'] : '' ) ], | |
| 138 | + [ 'background' => Color::get_css( isset( $attributes['loadMoreButtonBackgroundH'] ) ? $attributes['loadMoreButtonBackgroundH'] : '' ) ], | |
| 139 | + $css, | |
| 140 | + Border::get_hover_css( $attributes['moreButtonBorder'], '', $device ), | |
| 141 | + BoxShadow::get_hover_css( $attributes['moreButtonboxShadow'], '', $device ) | |
| 142 | + ); | |
| 143 | + } | |
| 144 | +} | |