← All changes
|
includes/blocks/storeengine-cart-notice/block.php
+202
-0
2.7.7
→
2.14.0
View file →
| @@ -1,0 +1,202 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace ABlocks\Blocks\StoreengineCartNotice; | |
| 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-notice'; | |
| 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-notice--info', | |
| 28 | + $this->get_notice_css( $attributes ), | |
| 29 | + $this->get_notice_css( $attributes, 'Tablet' ), | |
| 30 | + $this->get_notice_css( $attributes, 'Mobile' ), | |
| 31 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_notice_css( $attributes, $device ); } ) | |
| 32 | + ); | |
| 33 | + $css_generator->add_class_styles( | |
| 34 | + '{{WRAPPER}} .storeengine-notice--info:hover', | |
| 35 | + $this->get_notice_Hover_css( $attributes ), | |
| 36 | + $this->get_notice_Hover_css( $attributes, 'Tablet' ), | |
| 37 | + $this->get_notice_Hover_css( $attributes, 'Mobile' ), | |
| 38 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_notice_Hover_css( $attributes, $device ); } ) | |
| 39 | + ); | |
| 40 | + $css_generator->add_class_styles( | |
| 41 | + '{{WRAPPER}} .storeengine-notice .storeengine-notice--message p, | |
| 42 | + {{WRAPPER}} .storeengine-notice .storeengine-notice--message i', | |
| 43 | + $this->get_notice_massage_css( $attributes ), | |
| 44 | + $this->get_notice_massage_css( $attributes, 'Tablet' ), | |
| 45 | + $this->get_notice_massage_css( $attributes, 'Mobile' ), | |
| 46 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_notice_massage_css( $attributes, $device ); } ) | |
| 47 | + ); | |
| 48 | + | |
| 49 | + $css_generator->add_class_styles( | |
| 50 | + '{{WRAPPER}} .storeengine-notice a, | |
| 51 | + {{WRAPPER}} .storeengine-notice button', | |
| 52 | + $this->get_notice_link_css( $attributes ), | |
| 53 | + $this->get_notice_link_css( $attributes, 'Tablet' ), | |
| 54 | + $this->get_notice_link_css( $attributes, 'Mobile' ), | |
| 55 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_notice_link_css( $attributes, $device ); } ) | |
| 56 | + ); | |
| 57 | + | |
| 58 | + $css_generator->add_class_styles( | |
| 59 | + '{{WRAPPER}} .storeengine-notice a:hover, | |
| 60 | + {{WRAPPER}} .storeengine-notice button:hover', | |
| 61 | + $this->get_notice_link_hover_css( $attributes ), | |
| 62 | + $this->get_notice_link_hover_css( $attributes, 'Tablet' ), | |
| 63 | + $this->get_notice_link_hover_css( $attributes, 'Mobile' ), | |
| 64 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_notice_link_hover_css( $attributes, $device ); } ) | |
| 65 | + ); | |
| 66 | + $css_generator->add_class_styles( | |
| 67 | + '{{WRAPPER}}.ablocks-block--storeengine-cart-notice:not(.ablocks-block-container), | |
| 68 | + {{WRAPPER}}.ablocks-block--storeengine-cart-notice .ablocks-block-container', | |
| 69 | + $this->get_notice_wrapper_css( $attributes ), | |
| 70 | + $this->get_notice_wrapper_css( $attributes, 'Tablet' ), | |
| 71 | + $this->get_notice_wrapper_css( $attributes, 'Mobile' ), | |
| 72 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_notice_wrapper_css( $attributes, $device ); } ) | |
| 73 | + ); | |
| 74 | + return $css_generator->generate_css(); | |
| 75 | + } | |
| 76 | + | |
| 77 | + public function get_notice_css( $attributes, $device = '' ) { | |
| 78 | + if ( ! empty( $attributes['infoBoxPadding'] ) ) { | |
| 79 | + $cssPadding = Dimensions::get_css( $attributes['infoBoxPadding'], 'padding', $device ); | |
| 80 | + } | |
| 81 | + | |
| 82 | + return array_merge( | |
| 83 | + [ | |
| 84 | + 'color' => Color::get_css( | |
| 85 | + isset( $attributes['boxColor'] ) ? $attributes['boxColor'] : '' | |
| 86 | + ), | |
| 87 | + ], | |
| 88 | + [ | |
| 89 | + 'background' => Color::get_css( | |
| 90 | + isset( $attributes['boxBackground'] ) ? $attributes['boxBackground'] : '' | |
| 91 | + ), | |
| 92 | + ], | |
| 93 | + $cssPadding, | |
| 94 | + Border::get_css( $attributes['infoBoxBorder'], '', $device ), | |
| 95 | + BoxShadow::get_css( $attributes['infoBoxoxShadow'], '', $device ), | |
| 96 | + Range::get_css( [ | |
| 97 | + 'attributeValue' => $attributes['infoBoxWidth'], | |
| 98 | + 'attribute_object_key' => 'value', | |
| 99 | + 'isResponsive' => true, | |
| 100 | + 'defaultValue' => 100, | |
| 101 | + 'hasUnit' => true, | |
| 102 | + 'unitDefaultValue' => '%', | |
| 103 | + 'property' => 'width', | |
| 104 | + 'device' => $device, | |
| 105 | + ] ) | |
| 106 | + ); | |
| 107 | + } | |
| 108 | + | |
| 109 | + public function get_notice_Hover_css( $attributes, $device = '' ) { | |
| 110 | + return array_merge( | |
| 111 | + Border::get_hover_css( $attributes['infoBoxBorder'], '', $device ), | |
| 112 | + BoxShadow::get_hover_css( $attributes['infoBoxoxShadow'], '', $device ), | |
| 113 | + [ | |
| 114 | + 'color' => Color::get_css( | |
| 115 | + isset( $attributes['boxColorH'] ) ? $attributes['boxColorH'] : '' | |
| 116 | + ), | |
| 117 | + ], | |
| 118 | + [ | |
| 119 | + 'background' => Color::get_css( | |
| 120 | + isset( $attributes['boxBackgroundH'] ) ? $attributes['boxBackgroundH'] : '' | |
| 121 | + ), | |
| 122 | + ], | |
| 123 | + ); | |
| 124 | + } | |
| 125 | + | |
| 126 | + public function get_notice_massage_css( $attributes, $device = '' ) { | |
| 127 | + $typographyValueGlobal = ( isset( $attributes['infoTypographyGlobal'] ) ? $attributes['infoTypographyGlobal'] : '' ); | |
| 128 | + | |
| 129 | + $typography_value = isset( $attributes['infoTypography'] ) ? | |
| 130 | + Typography::get_css( $attributes['infoTypography'], '', $device, $typographyValueGlobal ) | |
| 131 | + : array(); | |
| 132 | + return $typography_value; | |
| 133 | + } | |
| 134 | + | |
| 135 | + public function get_notice_link_css( $attributes, $device = '' ) { | |
| 136 | + $typographyValueGlobal = ( isset( $attributes['linkTypographyGlobal'] ) ? $attributes['linkTypographyGlobal'] : '' ); | |
| 137 | + | |
| 138 | + $typography_value = isset( $attributes['linkTypography'] ) ? | |
| 139 | + Typography::get_css( $attributes['linkTypography'], '', $device, $typographyValueGlobal ) | |
| 140 | + : array(); | |
| 141 | + return array_merge( | |
| 142 | + [ | |
| 143 | + 'color' => Color::get_css( | |
| 144 | + isset( $attributes['linkColor'] ) ? $attributes['linkColor'] : '' | |
| 145 | + ), | |
| 146 | + ], | |
| 147 | + $typography_value, | |
| 148 | + ); | |
| 149 | + } | |
| 150 | + | |
| 151 | + public function get_notice_link_hover_css( $attributes, $device = '' ) { | |
| 152 | + return [ | |
| 153 | + 'color' => Color::get_css( | |
| 154 | + isset( $attributes['linkColorH'] ) ? $attributes['linkColorH'] : '' | |
| 155 | + ), | |
| 156 | + ]; | |
| 157 | + } | |
| 158 | + | |
| 159 | + public function get_notice_wrapper_css( $attributes, $device = '' ) { | |
| 160 | + $alignment_value = $attributes['infoboxAlignment'][ 'value' . $device ] ?? ''; | |
| 161 | + $css = []; | |
| 162 | + if ( ! empty( $alignment_value ) ) { | |
| 163 | + $css['display'] = 'flex'; | |
| 164 | + $css['width'] = '100%'; | |
| 165 | + $css['justify-content'] = $alignment_value; | |
| 166 | + } | |
| 167 | + return $css; | |
| 168 | + } | |
| 169 | + | |
| 170 | + public function render_block_content( $attributes, $content, $block_instance ) { | |
| 171 | + $attr_array = [ | |
| 172 | + 'product_id' => Helper::get_attribute_value( $attributes, 'product_id' ), | |
| 173 | + ]; | |
| 174 | + | |
| 175 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.MissingUnslash | |
| 176 | + $is_editor = ( isset( $_GET['context'] ) && 'edit' === sanitize_text_field( $_GET['context'] ) ); | |
| 177 | + | |
| 178 | + $is_custom = $attributes['isCustom']; | |
| 179 | + | |
| 180 | + if ( $is_custom ) { | |
| 181 | + if ( empty( $attr_array['product_id'] ) && isset( $block_instance->context['postId'] ) && ! empty( $block_instance->context['postId'] ) ) { | |
| 182 | + $attr_array['product_id'] = $block_instance->context['postId']; | |
| 183 | + } | |
| 184 | + } else { | |
| 185 | + if ( ! $is_editor && isset( $block_instance->context['postId'] ) && ! empty( $block_instance->context['postId'] ) ) { | |
| 186 | + $attr_array['product_id'] = $block_instance->context['postId']; | |
| 187 | + } | |
| 188 | + } | |
| 189 | + | |
| 190 | + if ( $is_editor && empty( $attr_array['product_id'] ) ) { | |
| 191 | + return '<p>Only in editor preview. Please Select a Product</p>'; | |
| 192 | + } | |
| 193 | + | |
| 194 | + if ( $is_editor ) { | |
| 195 | + $attr_array['dummy'] = true; | |
| 196 | + } | |
| 197 | + | |
| 198 | + $shortcode = '[storeengine_single_product_cart_notice ' . Helper::attr_shortcode( $attr_array ) . ']'; | |
| 199 | + echo do_shortcode( $shortcode ); | |
| 200 | + } | |
| 201 | + | |
| 202 | +} | |