← All changes
|
includes/blocks/storeengine-product-gallery/block.php
+136
-0
2.7.5
→
2.14.0
View file →
| @@ -1,0 +1,136 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace ABlocks\Blocks\StoreengineProductGallery; | |
| 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-product-gallery'; | |
| 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-gallery_wrapper .storeengine-col-12 .storeengine-placeholder, | |
| 28 | + {{WRAPPER}} .is-layout-constrained .storeengine-gallery_wrapper .storeengine-col-12 img, | |
| 29 | + {{WRAPPER}} .is-layout-constrained .storeengine-gallery_wrapper .carousel-main .flickity-slider img', | |
| 30 | + $this->get_gallary_image_css( $attributes ), | |
| 31 | + $this->get_gallary_image_css( $attributes, 'Tablet' ), | |
| 32 | + $this->get_gallary_image_css( $attributes, 'Mobile' ), | |
| 33 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_gallary_image_css( $attributes, $device ); } ) | |
| 34 | + ); | |
| 35 | + $css_generator->add_class_styles( | |
| 36 | + '{{WRAPPER}} .storeengine-gallery_wrapper .storeengine-row .storeengine-col-12', | |
| 37 | + $this->get_gallary_image_container_css( $attributes ), | |
| 38 | + $this->get_gallary_image_container_css( $attributes, 'Tablet' ), | |
| 39 | + $this->get_gallary_image_container_css( $attributes, 'Mobile' ), | |
| 40 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_gallary_image_container_css( $attributes, $device ); } ) | |
| 41 | + ); | |
| 42 | + $css_generator->add_class_styles( | |
| 43 | + '{{WRAPPER}} .storeengine-gallery_wrapper .storeengine-col-12 .storeengine-placeholder:hover', | |
| 44 | + $this->get_gallary_image_hover_css( $attributes ), | |
| 45 | + $this->get_gallary_image_hover_css( $attributes, 'Tablet' ), | |
| 46 | + $this->get_gallary_image_hover_css( $attributes, 'Mobile' ), | |
| 47 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_gallary_image_hover_css( $attributes, $device ); } ) | |
| 48 | + ); | |
| 49 | + return $css_generator->generate_css(); | |
| 50 | + } | |
| 51 | + | |
| 52 | + public function get_gallary_image_css( $attributes, $device = '' ) { | |
| 53 | + $css = []; | |
| 54 | + $css['opacity'] = $attributes['imageOpacity'] ?? ''; | |
| 55 | + if ( ! empty( $alignment_value ) ) { | |
| 56 | + $css['display'] = 'flex'; | |
| 57 | + $css['width'] = '100%'; | |
| 58 | + $css['justify-content'] = $alignment_value; | |
| 59 | + } | |
| 60 | + return array_merge( | |
| 61 | + $css, | |
| 62 | + isset( $attributes['border'] ) ? Border::get_css( $attributes['border'], '', $device ) : [], | |
| 63 | + isset( $attributes['boxShadow'] ) ? BoxShadow::get_css( $attributes['boxShadow'], $device ) : [], | |
| 64 | + Range::get_css([ | |
| 65 | + 'attributeValue' => $attributes['imageWidth'], | |
| 66 | + 'attribute_object_key' => 'value', | |
| 67 | + 'isResponsive' => true, | |
| 68 | + 'defaultValue' => 100, | |
| 69 | + 'hasUnit' => true, | |
| 70 | + 'unitDefaultValue' => '%', | |
| 71 | + 'property' => 'width', | |
| 72 | + 'device' => $device, | |
| 73 | + ]), | |
| 74 | + Range::get_css([ | |
| 75 | + 'attributeValue' => $attributes['imageHeight'], | |
| 76 | + 'attribute_object_key' => 'value', | |
| 77 | + 'isResponsive' => true, | |
| 78 | + 'defaultValue' => 100, | |
| 79 | + 'hasUnit' => true, | |
| 80 | + 'unitDefaultValue' => '%', | |
| 81 | + 'property' => 'height', | |
| 82 | + 'device' => $device, | |
| 83 | + ]), | |
| 84 | + ); | |
| 85 | + | |
| 86 | + } | |
| 87 | + | |
| 88 | + public function get_gallary_image_container_css( $attributes, $device = '' ) { | |
| 89 | + $alignment_value = $attributes['alignment'][ 'value' . $device ] ?? ''; | |
| 90 | + $css = []; | |
| 91 | + return $css; | |
| 92 | + } | |
| 93 | + | |
| 94 | + public function get_gallary_image_hover_css( $attributes, $device = '' ) { | |
| 95 | + $css = []; | |
| 96 | + $css['opacity'] = $attributes['imageOpacityH'] ?? ''; | |
| 97 | + | |
| 98 | + return array_merge( | |
| 99 | + isset( $attributes['border'] ) ? Border::get_hover_css( $attributes['border'], '', $device ) : [], | |
| 100 | + isset( $attributes['boxShadow'] ) ? BoxShadow::get_hover_css( $attributes['boxShadow'], $device ) : [], | |
| 101 | + ); | |
| 102 | + } | |
| 103 | + | |
| 104 | + public function render_block_content( $attributes, $content, $block_instance ) { | |
| 105 | + $attr_array = [ | |
| 106 | + 'product_id' => Helper::get_attribute_value( $attributes, 'product_id' ), | |
| 107 | + ]; | |
| 108 | + | |
| 109 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.MissingUnslash | |
| 110 | + $is_editor = ( isset( $_GET['context'] ) && 'edit' === sanitize_text_field( $_GET['context'] ) ); | |
| 111 | + | |
| 112 | + $is_custom = $attributes['isCustom']; | |
| 113 | + | |
| 114 | + if ( $is_custom ) { | |
| 115 | + if ( empty( $attr_array['product_id'] ) && isset( $block_instance->context['postId'] ) && ! empty( $block_instance->context['postId'] ) ) { | |
| 116 | + $attr_array['product_id'] = $block_instance->context['postId']; | |
| 117 | + } | |
| 118 | + } else { | |
| 119 | + if ( ! $is_editor && isset( $block_instance->context['postId'] ) && ! empty( $block_instance->context['postId'] ) ) { | |
| 120 | + $attr_array['product_id'] = $block_instance->context['postId']; | |
| 121 | + } | |
| 122 | + } | |
| 123 | + | |
| 124 | + if ( $is_editor && empty( $attr_array['product_id'] ) ) { | |
| 125 | + return '<p>Only in editor preview. Please Select a Product</p>'; | |
| 126 | + } | |
| 127 | + | |
| 128 | + if ( $is_editor ) { | |
| 129 | + $attr_array['dummy'] = true; | |
| 130 | + } | |
| 131 | + | |
| 132 | + $shortcode = '[storeengine_single_product_gallery ' . Helper::attr_shortcode( $attr_array ) . ']'; | |
| 133 | + echo do_shortcode( $shortcode ); | |
| 134 | + } | |
| 135 | + | |
| 136 | +} | |