PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.9.0
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.9.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 1.1.2 All 80 releases
ablocks / includes / blocks / storeengine-cart-button / block.php

block.php in aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder 2.9.0, at includes/blocks/storeengine-cart-button/block.php

352 lines 13.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ABlocks\Blocks\StoreengineCartButton;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit;
7 }
8
9 use ABlocks\Classes\BlockBaseAbstract;
10 use ABlocks\Classes\CssGenerator;
11 use ABlocks\Helper;
12 use ABlocks\Controls\Typography;
13 use ABlocks\Controls\Background;
14 use ABlocks\Controls\Border;
15 use ABlocks\Controls\Dimensions;
16 use ABlocks\Controls\Range;
17 use ABlocks\Controls\BoxShadow;
18 use ABlocks\Controls\Color;
19
20 class Block extends BlockBaseAbstract {
21 protected $block_name = 'storeengine-cart-button';
22
23 public function build_css( $attributes ) {
24 $css_generator = new CssGenerator( $attributes, $this->block_name );
25
26 $css_generator->add_class_styles(
27 '{{WRAPPER}} .storeengine-btn--add-to-cart,
28 {{WRAPPER}} .storeengine-btn--add-to-cart-replacement,
29 {{WRAPPER}} .storeengine-btn--direct-checkout,
30 {{WRAPPER}} .storeengine-btn--view-options',
31 $this->get_button_css( $attributes ),
32 $this->get_button_css( $attributes, 'Tablet' ),
33 $this->get_button_css( $attributes, 'Mobile' )
34 );
35 $css_generator->add_class_styles(
36 '{{WRAPPER}} .storeengine-btn--add-to-cart:hover,
37 {{WRAPPER}} .storeengine-btn--add-to-cart-replacement:hover,
38 {{WRAPPER}} .storeengine-btn--direct-checkout:hover,
39 {{WRAPPER}} .storeengine-btn--view-options:hover',
40 $this->get_button_hover_css( $attributes, '' ),
41 $this->get_button_hover_css( $attributes, 'Tablet' ),
42 $this->get_button_hover_css( $attributes, 'Mobile' )
43 );
44 $css_generator->add_class_styles(
45 '{{WRAPPER}} .storeengine-single-product-quantity-wrap',
46 $this->get_button_alignment_css( $attributes, '' ),
47 $this->get_button_alignment_css( $attributes, 'Tablet' ),
48 $this->get_button_alignment_css( $attributes, 'Mobile' )
49 );
50 $css_generator->add_class_styles(
51 '{{WRAPPER}} .storeengine-add-to-cart-shortcode .storeengine-price.amount',
52 $this->get_price_css( $attributes, '' ),
53 $this->get_price_css( $attributes, 'Tablet' ),
54 $this->get_price_css( $attributes, 'Mobile' )
55 );
56 $css_generator->add_class_styles(
57 '{{WRAPPER}} .storeengine-add-to-cart-shortcode .storeengine-price.amount:hover',
58 $this->get_price_hover_css( $attributes, '' ),
59 $this->get_price_hover_css( $attributes, 'Tablet' ),
60 $this->get_price_hover_css( $attributes, 'Mobile' )
61 );
62 $css_generator->add_class_styles(
63 '{{WRAPPER}} .storeengine-single-product-price-name,
64 {{WRAPPER}} .storeengine-loop-product-price-summery .storeengine-loop-product-price-label',
65 $this->get_price_name_css( $attributes, '' ),
66 $this->get_price_name_css( $attributes, 'Tablet' ),
67 $this->get_price_name_css( $attributes, 'Mobile' )
68 );
69 $css_generator->add_class_styles(
70 '{{WRAPPER}} .storeengine-single-product-price-name:hover,
71 {{WRAPPER}} .storeengine-loop-product-price-summery .storeengine-loop-product-price-label:hover',
72 $this->get_price_name_hover_css( $attributes, '' ),
73 $this->get_price_name_hover_css( $attributes, 'Tablet' ),
74 $this->get_price_name_hover_css( $attributes, 'Mobile' )
75 );
76 $css_generator->add_class_styles(
77 '{{WRAPPER}} .storeengine-single__amount .storeengine-dropdown,
78 {{WRAPPER}} .storeengine-single__amount .storeengine-product__multi-prices,
79 {{WRAPPER}} .storeengine-dropdown.open .storeengine-dropdown-content .storeengine-product__multi-price,
80 {{WRAPPER}} .storeengine-dropdown.open .storeengine-dropdown-content .storeengine-product__multi-price label,
81 {{WRAPPER}} .storeengine-single-product-prices',
82 $this->get_box_css( $attributes, '' ),
83 $this->get_box_css( $attributes, 'Tablet' ),
84 $this->get_box_css( $attributes, 'Mobile' )
85 );
86 $css_generator->add_class_styles(
87 '{{WRAPPER}} .storeengine-single__amount .storeengine-dropdown:hover,
88 {{WRAPPER}} .storeengine-single__amount .storeengine-product__multi-prices:hover,
89 {{WRAPPER}} .storeengine-dropdown.open .storeengine-dropdown-content .storeengine-product__multi-price:hover,
90 {{WRAPPER}} .storeengine-dropdown.open .storeengine-dropdown-content .storeengine-product__multi-price label:hover,
91 {{WRAPPER}} .storeengine-single-product-prices:hover',
92 $this->get_box_hover_css( $attributes, '' ),
93 $this->get_box_hover_css( $attributes, 'Tablet' ),
94 $this->get_box_hover_css( $attributes, 'Mobile' )
95 );
96 $css_generator->add_class_styles(
97 '{{WRAPPER}} .storeengine-single-product-price-summery .storeengine-single-product-price-label input[type=radio]',
98 $this->get_radio_css( $attributes, '' ),
99 $this->get_radio_css( $attributes, 'Tablet' ),
100 $this->get_radio_css( $attributes, 'Mobile' )
101 );
102 $css_generator->add_class_styles(
103 '{{WRAPPER}} .storeengine-single__amount',
104 $this->get_alignment_css( $attributes, '' ),
105 $this->get_alignment_css( $attributes, 'Tablet' ),
106 $this->get_alignment_css( $attributes, 'Mobile' )
107 );
108 $css_generator->add_class_styles(
109 '{{WRAPPER}} .storeengine-add-to-cart-shortcode',
110 $this->get_gap_css( $attributes, '' ),
111 $this->get_gap_css( $attributes, 'Tablet' ),
112 $this->get_gap_css( $attributes, 'Mobile' )
113 );
114 $css_generator->add_class_styles(
115 '{{WRAPPER}} .storeengine-single-product-prices .storeengine-single-product-price,
116 {{WRAPPER}} .storeengine-product__multi-prices .storeengine-dropdown__toggle,
117 {{WRAPPER}} .storeengine-single__amount .storeengine-product__multi-prices',
118 $this->get_price_box_css( $attributes, '' ),
119 $this->get_price_box_css( $attributes, 'Tablet' ),
120 $this->get_price_box_css( $attributes, 'Mobile' )
121 );
122
123 return $css_generator->generate_css();
124 }
125
126 public function get_button_css( $attributes, $device = '' ) {
127 $css = [];
128 $cssPadding = [];
129 $alignment_css = [];
130 $typographyValueGlobal = ( isset( $attributes['btn_typographyGlobal'] ) ? $attributes['btn_typographyGlobal'] : '' );
131 $typography_value = isset( $attributes['btn_typography'] ) ? $attributes['btn_typography'] : [];
132
133 if ( ! empty( $attributes['padding'] ) ) {
134 $cssPadding = Dimensions::get_css( $attributes['padding'], 'padding', $device );
135 }
136
137 if ( ! empty( $attributes['buttonTextAlign'][ 'value' . $device ] ) ) {
138 $alignment_css['justify-content'] = $attributes['buttonTextAlign'][ 'value' . $device ];
139 }
140
141 return array_merge(
142 [ 'color' => Color::get_css( isset( $attributes['button_color'] ) ? $attributes['button_color'] : '' ) ],
143 [ 'background' => Color::get_css( isset( $attributes['button_bg'] ) ? $attributes['button_bg'] : '' ) ],
144 $cssPadding,
145 Typography::get_css( $typography_value, '', $device, $typographyValueGlobal ),
146 $alignment_css,
147 Border::get_css( $attributes['buttonBorder'], '', $device ),
148 BoxShadow::get_css( $attributes['boxShadow'], '', $device ),
149 Range::get_css( [
150 'attributeValue' => $attributes['button_width'],
151 'attribute_object_key' => 'value',
152 'isResponsive' => true,
153 'defaultValue' => 100,
154 'hasUnit' => true,
155 'unitDefaultValue' => '%',
156 'property' => 'width',
157 'device' => $device,
158 ] )
159 );
160 }
161
162 public function get_button_hover_css( $attributes, $device = '' ) {
163
164 return array_merge(
165 [ 'color' => Color::get_css( isset( $attributes['button_color_hover'] ) ? $attributes['button_color_hover'] : '' ) ],
166 [ 'background' => Color::get_css( isset( $attributes['button_bg_hover'] ) ? $attributes['button_bg_hover'] : '' ) ],
167 Border::get_hover_css( $attributes['buttonBorder'] ?? [], '', $device ),
168 BoxShadow::get_hover_css( $attributes['boxShadow'], '', $device ),
169 );
170 }
171
172 public function get_button_alignment_css( $attributes, $device = '' ) {
173 $alignment_css = [];
174
175 if ( ! empty( $attributes['buttonAlign'][ 'value' . $device ] ) ) {
176 $alignment_css['justify-content'] = $attributes['buttonAlign'][ 'value' . $device ];
177 }
178
179 return $alignment_css;
180 }
181
182 public function get_price_css( $attributes, $device = '' ) {
183 $typographyValueGlobal = ( isset( $attributes['priceTypographyGlobal'] ) ? $attributes['priceTypographyGlobal'] : '' );
184 $typography_value = isset( $attributes['priceTypography'] ) ? $attributes['priceTypography'] : [];
185 return array_merge(
186 $typography_value,
187 Typography::get_css( $typography_value, '', $device, $typographyValueGlobal ),
188 [
189 'color' => Color::get_css(
190 isset( $attributes['priceColor'] ) ? $attributes['priceColor'] : ''
191 ),
192 ]
193 );
194 }
195
196 public function get_price_hover_css( $attributes, $device = '' ) {
197 return [
198 'color' => Color::get_css(
199 isset( $attributes['priceColorH'] ) ? $attributes['priceColorH'] : ''
200 ),
201 ];
202 }
203
204 public function get_price_name_css( $attributes, $device = '' ) {
205 $typographyValueGlobal = ( isset( $attributes['priceNameTypographyGlobal'] ) ? $attributes['priceNameTypographyGlobal'] : '' );
206 $typography_value = isset( $attributes['priceNameTypography'] ) ? $attributes['priceNameTypography'] : [];
207
208 return array_merge(
209 $typography_value,
210 Typography::get_css( $typography_value, '', $device, $typographyValueGlobal ),
211 [
212 'color' => Color::get_css(
213 isset( $attributes['priceNameColor'] ) ? $attributes['priceNameColor'] : ''
214 ),
215 ]
216 );
217 }
218
219 public function get_price_name_hover_css( $attributes, $device = '' ) {
220 return [
221 'color' => Color::get_css(
222 isset( $attributes['priceNameColorH'] ) ? $attributes['priceNameColorH'] : ''
223 ),
224 ];
225 }
226
227 public function get_box_css( $attributes, $device = '' ) {
228 return array_merge(
229 [
230 'background' => Color::get_css(
231 isset( $attributes['boxBackground'] ) ? $attributes['boxBackground'] : ''),
232 ],
233 Range::get_css( [
234 'attributeValue' => $attributes['boxWidth'],
235 'attribute_object_key' => 'value',
236 'isResponsive' => true,
237 'defaultValue' => 100,
238 'hasUnit' => true,
239 'unitDefaultValue' => '%',
240 'property' => 'width',
241 'device' => $device,
242 ] )
243 );
244 }
245
246 public function get_radio_css( $attributes, $device = '' ) {
247 return array_merge(
248 Range::get_css([
249 'attributeValue' => $attributes['radioWidth'],
250 'attribute_object_key' => 'value',
251 'isResponsive' => true,
252 'defaultValue' => 15,
253 'hasUnit' => true,
254 'unitDefaultValue' => 'px',
255 'property' => 'width',
256 'device' => $device,
257 ]),
258 Range::get_css([
259 'attributeValue' => $attributes['radioHeight'],
260 'attribute_object_key' => 'value',
261 'isResponsive' => true,
262 'defaultValue' => 15,
263 'hasUnit' => true,
264 'unitDefaultValue' => 'px',
265 'property' => 'height',
266 'device' => $device,
267 ])
268 );
269 }
270
271 public function get_alignment_css( $attributes, $device = '' ) {
272 $alignment_css = [];
273 $css['display'] = 'flex';
274 if ( ! empty( $attributes['priceAlign'][ 'value' . $device ] ) ) {
275 $alignment_css['justify-content'] = $attributes['priceAlign'][ 'value' . $device ];
276 }
277
278 return array_merge(
279 $alignment_css,
280 $css,
281 );
282 }
283
284
285 public function get_box_hover_css( $attributes, $device = '' ) {
286 return [
287 'background' => Color::get_css(
288 isset( $attributes['boxBackgroundH'] ) ? $attributes['boxBackgroundH'] : ''),
289 ];
290 }
291
292 public function get_gap_css( $attributes, $device = '' ) {
293 $css_flex['display'] = 'flex';
294 $css_direction['flex-direction'] = 'column';
295
296 return array_merge(
297 $css_direction,
298 $css_flex,
299 Range::get_css([
300 'attributeValue' => $attributes['elementGap'],
301 'attribute_object_key' => 'value',
302 'isResponsive' => true,
303 'defaultValue' => 12,
304 'hasUnit' => true,
305 'unitDefaultValue' => 'px',
306 'property' => 'gap',
307 'device' => $device,
308 ]),
309 );
310 }
311
312 public function get_price_box_css( $attributes, $device = '' ) {
313 if ( ! empty( $attributes['boxPadding'] ) ) {
314 $css_padding = Dimensions::get_css( $attributes['boxPadding'], 'padding', $device );
315 }
316
317 return array_merge(
318 $css_padding,
319 Border::get_css( $attributes['boxBorder'], '', $device ),
320 );
321
322 }
323
324 public function render_block_content( $attributes, $content, $block_instance ) {
325 $attr_array = [
326 'label' => Helper::get_attribute_value( $attributes, 'label' ),
327 'product_id' => Helper::get_attribute_value( $attributes, 'product_id' ),
328 'price_id' => Helper::get_attribute_value( $attributes, 'price_id' ),
329 'variation_id' => Helper::get_attribute_value( $attributes, 'variation_id' ),
330 'direct_checkout' => Helper::get_attribute_value( $attributes, 'direct_checkout' ) ? 'true' : 'false',
331 'quantity' => Helper::get_attribute_value( $attributes, 'quantity' ),
332 'show_quantity' => Helper::get_attribute_value( $attributes, 'show_quantity' ),
333 'disabled' => Helper::get_attribute_value( $attributes, 'disabled' ),
334 'price_display' => Helper::get_attribute_value( $attributes, 'price_display' ),
335 'button_display' => Helper::get_attribute_value( $attributes, 'button_display' ),
336 ];
337
338 if ( isset( $block_instance->context['postId'] ) && ! empty( $block_instance->context['postId'] ) && empty( $attr_array['product_id'] ) ) {
339 $attr_array['product_id'] = $block_instance->context['postId'];
340 }
341
342 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
343 if ( isset( $_GET['context'] ) && 'edit' === sanitize_text_field( $_GET['context'] ) ) {
344 $attr_array['dummy'] = true;
345 }
346
347 $shortcode = '[storeengine_add_to_cart ' . Helper::attr_shortcode( $attr_array ) . ']';
348 echo do_shortcode( $shortcode );
349 }
350
351 }
352