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