| @@ -1,0 +1,196 @@ | ||
| 1 | +<?php | |
| 2 | +namespace ABlocks\Blocks\LoopTemplate; | |
| 3 | + | |
| 4 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 5 | + exit; | |
| 6 | +} | |
| 7 | + | |
| 8 | +use ABlocks\Classes\BlockBaseAbstract; | |
| 9 | +use ABlocks\Classes\CssGenerator; | |
| 10 | +use ABlocks\Controls\Alignment; | |
| 11 | +use ABlocks\Controls\Range; | |
| 12 | +use ABlocks\Controls\Border; | |
| 13 | +use ABlocks\Controls\Dimensions; | |
| 14 | +use ABlocks\Controls\Color; | |
| 15 | + | |
| 16 | +class Block extends BlockBaseAbstract { | |
| 17 | + protected $parent_block_name = 'loop-builder'; | |
| 18 | + protected $block_name = 'loop-template'; | |
| 19 | + protected $is_skip_inner_block = true; | |
| 20 | + | |
| 21 | + public function build_css( $attributes ) { | |
| 22 | + | |
| 23 | + // Generate CSS start | |
| 24 | + $css_generator = new CssGenerator( $attributes, $this->block_name ); | |
| 25 | + // Generate CSS using the loop template styles | |
| 26 | + $css_generator->add_class_styles( | |
| 27 | + '{{WRAPPER}} .wp-block-ablocks-loop-template', | |
| 28 | + $this->loop_template_wrapper( $attributes ), | |
| 29 | + $this->loop_template_wrapper( $attributes, 'Tablet' ), | |
| 30 | + $this->loop_template_wrapper( $attributes, 'Mobile' ) | |
| 31 | + ); | |
| 32 | + $css_generator->add_class_styles( | |
| 33 | + '{{WRAPPER}} .ablocks-loop-template-item', | |
| 34 | + $this->template_card_style_css( $attributes ), | |
| 35 | + $this->template_card_style_css( $attributes, 'Tablet' ), | |
| 36 | + $this->template_card_style_css( $attributes, 'Mobile' ) | |
| 37 | + ); | |
| 38 | + $css_generator->add_class_styles( | |
| 39 | + '{{WRAPPER}} .ablocks-loop-template-item:hover', | |
| 40 | + $this->template_card_style_hover_css( $attributes ), | |
| 41 | + $this->template_card_style_hover_css( $attributes, 'Tablet' ), | |
| 42 | + $this->template_card_style_hover_css( $attributes, 'Mobile' ) | |
| 43 | + ); | |
| 44 | + | |
| 45 | + return $css_generator->generate_css(); | |
| 46 | + } | |
| 47 | + private function loop_template_wrapper( $attributes, $device = '' ) { | |
| 48 | + $css = []; | |
| 49 | + // Temporary approch instead of attribute migration. Might remove it in future | |
| 50 | + if ( ! empty( $attributes['gridColumns'] ) && (int) $attributes['gridColumns'] !== 2 ) { | |
| 51 | + $attributes['templateGridColumns'][ 'value' . $device ] = $attributes['gridColumns']; | |
| 52 | + $attributes['gridColumns'] = ''; | |
| 53 | + } | |
| 54 | + // Temporary approch instead of attribute migration. Might remove it in future | |
| 55 | + | |
| 56 | + if ( ! empty( $attributes['gridStyle'] ) && $attributes['gridStyle'] === 'grid' ) { | |
| 57 | + $css['display'] = 'grid'; | |
| 58 | + $columnCSS = Range::get_css([ | |
| 59 | + 'attributeValue' => $attributes['templateGridColumns'], | |
| 60 | + 'attribute_object_key' => 'value', | |
| 61 | + 'isResponsive' => true, | |
| 62 | + 'hasUnit' => false, | |
| 63 | + 'defaultValue' => 2, | |
| 64 | + 'defaultValueTablet' => 2, | |
| 65 | + 'defaultValueMobile' => 1, | |
| 66 | + 'unitDefaultValue' => '', | |
| 67 | + 'property' => 'grid-template-columns', | |
| 68 | + 'device' => $device, | |
| 69 | + ]); | |
| 70 | + if ( ! empty( $columnCSS ) ) { | |
| 71 | + $css['grid-template-columns'] = 'repeat(' . $columnCSS['grid-template-columns'] . ', 1fr)'; | |
| 72 | + }; | |
| 73 | + }; | |
| 74 | + | |
| 75 | + return array_merge( | |
| 76 | + $css, | |
| 77 | + Range::get_css([ | |
| 78 | + 'attributeValue' => $attributes['itemGap'], | |
| 79 | + 'attribute_object_key' => 'value', | |
| 80 | + 'isResponsive' => true, | |
| 81 | + 'hasUnit' => false, | |
| 82 | + 'unitDefaultValue' => 'px', | |
| 83 | + 'defaultValue' => 10, | |
| 84 | + 'property' => 'gap', | |
| 85 | + 'device' => $device, | |
| 86 | + ]) | |
| 87 | + ); | |
| 88 | + } | |
| 89 | + | |
| 90 | + public function render_block_content( $attributes, $content, $block ) { | |
| 91 | + $page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page'; | |
| 92 | + $enhanced_pagination = isset( $block->context['enhancedPagination'] ) && $block->context['enhancedPagination']; | |
| 93 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 94 | + $page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ]; | |
| 95 | + | |
| 96 | + // Use global query if needed. | |
| 97 | + $use_global_query = ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ); | |
| 98 | + // Disable use of global query if this is an AJAX request. | |
| 99 | + if ( wp_doing_ajax() ) { | |
| 100 | + $use_global_query = false; | |
| 101 | + } | |
| 102 | + | |
| 103 | + if ( $use_global_query ) { | |
| 104 | + global $wp_query; | |
| 105 | + | |
| 106 | + if ( in_the_loop() ) { | |
| 107 | + $query = clone $wp_query; | |
| 108 | + $query->rewind_posts(); | |
| 109 | + } else { | |
| 110 | + $query = $wp_query; | |
| 111 | + } | |
| 112 | + } else { | |
| 113 | + $query_args = build_query_vars_from_query_block( $block, $page ); | |
| 114 | + $query_args = isset( $attributes['query'] ) ? array_merge( $query_args, $attributes['query'] ) : $query_args; | |
| 115 | + $query = new \WP_Query( $query_args ); | |
| 116 | + } | |
| 117 | + | |
| 118 | + if ( ! $query->have_posts() ) { | |
| 119 | + return ''; | |
| 120 | + } | |
| 121 | + | |
| 122 | + if ( block_core_post_template_uses_featured_image( $block->inner_blocks ) ) { | |
| 123 | + update_post_thumbnail_cache( $query ); | |
| 124 | + } | |
| 125 | + | |
| 126 | + $classnames = ''; | |
| 127 | + if ( isset( $block->context['displayLayout'] ) && isset( $block->context['query'] ) ) { | |
| 128 | + if ( isset( $block->context['displayLayout']['type'] ) && 'flex' === $block->context['displayLayout']['type'] ) { | |
| 129 | + $classnames = "is-flex-container columns-{$block->context['displayLayout']['columns']}"; | |
| 130 | + } | |
| 131 | + } | |
| 132 | + if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) { | |
| 133 | + $classnames .= ' has-link-color'; | |
| 134 | + } | |
| 135 | + | |
| 136 | + // Ensure backwards compatibility by flagging the number of columns via classname when using grid layout. | |
| 137 | + if ( isset( $attributes['layout']['type'] ) && 'grid' === $attributes['layout']['type'] && ! empty( $attributes['layout']['columnCount'] ) ) { | |
| 138 | + $classnames .= ' ' . sanitize_title( 'columns-' . $attributes['layout']['columnCount'] ); | |
| 139 | + } | |
| 140 | + | |
| 141 | + $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => trim( $classnames ) ) ); | |
| 142 | + | |
| 143 | + $content = ''; | |
| 144 | + while ( $query->have_posts() ) { | |
| 145 | + $query->the_post(); | |
| 146 | + | |
| 147 | + $block_instance = $block->parsed_block; | |
| 148 | + | |
| 149 | + $block_instance['blockName'] = 'core/null'; | |
| 150 | + | |
| 151 | + $post_id = get_the_ID(); | |
| 152 | + $post_type = get_post_type(); | |
| 153 | + $filter_block_context = static function ( $context ) use ( $post_id, $post_type ) { | |
| 154 | + $context['postType'] = $post_type; | |
| 155 | + $context['postId'] = $post_id; | |
| 156 | + return $context; | |
| 157 | + }; | |
| 158 | + | |
| 159 | + add_filter( 'render_block_context', $filter_block_context, 1 ); | |
| 160 | + | |
| 161 | + $block_content = ( new \WP_Block( $block_instance ) )->render( array( 'dynamic' => false ) ); | |
| 162 | + remove_filter( 'render_block_context', $filter_block_context, 1 ); | |
| 163 | + | |
| 164 | + $post_classes = implode( ' ', get_post_class( 'ablocks-loop-template-item' ) ); | |
| 165 | + | |
| 166 | + $inner_block_directives = $enhanced_pagination ? ' data-wp-key="post-template-item-' . $post_id . '"' : ''; | |
| 167 | + | |
| 168 | + $content .= '<div' . $inner_block_directives . ' class="' . esc_attr( $post_classes ) . '">' . $block_content . '</div>'; | |
| 169 | + }//end while | |
| 170 | + | |
| 171 | + wp_reset_postdata(); | |
| 172 | + | |
| 173 | + return sprintf( | |
| 174 | + '<div %1$s>%2$s</div>', | |
| 175 | + $wrapper_attributes, | |
| 176 | + $content | |
| 177 | + ); | |
| 178 | + } | |
| 179 | + | |
| 180 | + public function template_card_style_css( $attributes, $device = '' ) { | |
| 181 | + $templte_padding_css = ! empty( $attributes['padding'] ) ? Dimensions::get_css( $attributes['padding'], 'padding', $device ) : array(); | |
| 182 | + return array_merge( | |
| 183 | + [ 'background' => Color::get_css( isset( $attributes['bgColor'] ) ? $attributes['bgColor'] : '' ) ], | |
| 184 | + $templte_padding_css, | |
| 185 | + ! empty( $attributes['border'] ) ? Border::get_css( $attributes['border'], '', $device ) : [], | |
| 186 | + ); | |
| 187 | + } | |
| 188 | + public function template_card_style_hover_css( $attributes, $device = '' ) { | |
| 189 | + $css = []; | |
| 190 | + return array_merge( | |
| 191 | + $css, | |
| 192 | + Border::get_hover_css( $attributes['border'], '', $device ), | |
| 193 | + ); | |
| 194 | + } | |
| 195 | + | |
| 196 | +} | |