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