Block.php
252 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCartBlocks\Blocks\ProductCollection; |
| 4 | |
| 5 | use SureCartBlocks\Blocks\BaseBlock; |
| 6 | |
| 7 | /** |
| 8 | * Product Collection block. |
| 9 | */ |
| 10 | class Block extends BaseBlock { |
| 11 | /** |
| 12 | * Keeps track of instances of this block. |
| 13 | * |
| 14 | * @var integer |
| 15 | */ |
| 16 | public static $instance; |
| 17 | |
| 18 | /** |
| 19 | * Default block template. |
| 20 | * |
| 21 | * @var array |
| 22 | */ |
| 23 | protected $default_block_template = [ |
| 24 | [ |
| 25 | 'blockName' => 'surecart/product-item-image', |
| 26 | 'attrs' => [ |
| 27 | 'sizing' => 'cover', |
| 28 | 'ratio' => '1/1.33', |
| 29 | 'style' => [ |
| 30 | 'border' => [ |
| 31 | 'radius' => '6px', |
| 32 | ], |
| 33 | 'spacing' => [ |
| 34 | 'margin' => [ |
| 35 | 'bottom' => '16px', |
| 36 | ], |
| 37 | ], |
| 38 | ], |
| 39 | ], |
| 40 | ], |
| 41 | [ |
| 42 | 'blockName' => 'surecart/product-item-title', |
| 43 | 'attrs' => [ |
| 44 | 'level' => 3, |
| 45 | 'style' => [ |
| 46 | 'typography' => [ |
| 47 | 'fontSize' => '18px', |
| 48 | ], |
| 49 | 'color' => [ |
| 50 | 'text' => '#000000', |
| 51 | ], |
| 52 | 'spacing' => [ |
| 53 | 'margin' => [ |
| 54 | 'bottom' => '8px', |
| 55 | ], |
| 56 | ], |
| 57 | ], |
| 58 | ], |
| 59 | ], |
| 60 | [ |
| 61 | 'blockName' => 'surecart/product-item-price', |
| 62 | 'attrs' => [ |
| 63 | 'style' => [ |
| 64 | 'typography' => [ |
| 65 | 'fontSize' => '16px', |
| 66 | ], |
| 67 | 'color' => [ |
| 68 | 'text' => '#000000', |
| 69 | ], |
| 70 | 'spacing' => [ |
| 71 | 'margin' => [ |
| 72 | 'bottom' => '8px', |
| 73 | ], |
| 74 | ], |
| 75 | ], |
| 76 | ], |
| 77 | ], |
| 78 | ]; |
| 79 | |
| 80 | /** |
| 81 | * Get the style for the block. |
| 82 | * |
| 83 | * @param array $attr Product List attributes. |
| 84 | * @return string |
| 85 | */ |
| 86 | public function getProductListStyle( $attr ) { |
| 87 | $style = '--sc-product-item-list-column: ' . ( $attr['columns'] ?? 3 ) . ';'; |
| 88 | $style .= '--sc-product-item-list-gap: ' . $this->getSpacingPresetCssVar( $attr['style']['spacing']['blockGap'] ?? '40px' ) . ';'; |
| 89 | $style .= '--sc-pagination-font-size: ' . ( $attr['paginationSize'] ?? '14px' ) . ';'; |
| 90 | return $style; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Get the style for the block. |
| 95 | * |
| 96 | * @param array $attr Style variables. |
| 97 | * @param string $prefix Prefix for the css variable. |
| 98 | * @return string |
| 99 | */ |
| 100 | public function getVars( $attr, $prefix ) { |
| 101 | $style = ''; |
| 102 | // padding. |
| 103 | if ( ! empty( $attr['style']['spacing']['padding'] ) ) { |
| 104 | $padding = $attr['style']['spacing']['padding']; |
| 105 | $style .= '--sc-product-' . $prefix . '-padding-top: ' . $this->getSpacingPresetCssVar( array_key_exists( 'top', $padding ) ? $padding['top'] : '0' ) . ';'; |
| 106 | $style .= '--sc-product-' . $prefix . '-padding-bottom: ' . $this->getSpacingPresetCssVar( array_key_exists( 'bottom', $padding ) ? $padding['bottom'] : '0' ) . ';'; |
| 107 | $style .= '--sc-product-' . $prefix . '-padding-left: ' . $this->getSpacingPresetCssVar( array_key_exists( 'left', $padding ) ? $padding['left'] : '0' ) . ';'; |
| 108 | $style .= '--sc-product-' . $prefix . '-padding-right: ' . $this->getSpacingPresetCssVar( array_key_exists( 'right', $padding ) ? $padding['right'] : '0' ) . ';'; |
| 109 | } |
| 110 | // margin. |
| 111 | if ( ! empty( $attr['style']['spacing']['margin'] ) ) { |
| 112 | $margin = $attr['style']['spacing']['margin']; |
| 113 | $style .= '--sc-product-' . $prefix . '-margin-top: ' . $this->getSpacingPresetCssVar( array_key_exists( 'top', $margin ) ? $margin['top'] : '0' ) . ';'; |
| 114 | $style .= '--sc-product-' . $prefix . '-margin-bottom: ' . $this->getSpacingPresetCssVar( array_key_exists( 'bottom', $margin ) ? $margin['bottom'] : '0' ) . ';'; |
| 115 | $style .= '--sc-product-' . $prefix . '-margin-left: ' . $this->getSpacingPresetCssVar( array_key_exists( 'left', $margin ) ? $margin['left'] : '0' ) . ';'; |
| 116 | $style .= '--sc-product-' . $prefix . '-margin-right: ' . $this->getSpacingPresetCssVar( array_key_exists( 'right', $margin ) ? $margin['right'] : '0' ) . ';'; |
| 117 | } |
| 118 | // aspect ratio. |
| 119 | if ( ! empty( $attr['ratio'] ) ) { |
| 120 | $style .= '--sc-product-' . $prefix . '-aspect-ratio: ' . $attr['ratio'] . ';'; |
| 121 | } |
| 122 | // border width. |
| 123 | if ( ! empty( $attr['style']['border']['width'] ) ) { |
| 124 | $style .= '--sc-product-' . $prefix . '-border-width: ' . $attr['style']['border']['width'] . ';'; |
| 125 | } |
| 126 | // border radius. |
| 127 | if ( ! empty( $attr['style']['border']['radius'] ) ) { |
| 128 | $style .= '--sc-product-' . $prefix . '-border-radius: ' . $attr['style']['border']['radius'] . ';'; |
| 129 | } |
| 130 | // font weight. |
| 131 | if ( ! empty( $attr['style']['typography']['fontWeight'] ) ) { |
| 132 | $style .= '--sc-product-' . $prefix . '-font-weight: ' . $attr['style']['typography']['fontWeight'] . ';'; |
| 133 | } |
| 134 | // font size. |
| 135 | if ( ! empty( $attr['fontSize'] ) || ! empty( $attr['style']['typography']['fontSize'] ) ) { |
| 136 | $font_size = ! empty( $attr['fontSize'] ) ? $this->getFontSizePresetCssVar( $attr['fontSize'] ) : $attr['style']['typography']['fontSize']; |
| 137 | $style .= '--sc-product-' . $prefix . '-font-size: ' . $font_size . ';'; |
| 138 | } |
| 139 | // border color. |
| 140 | if ( ! empty( $attr['borderColor'] ) || ! empty( $attr['style']['border']['color'] ) ) { |
| 141 | $border_color = ! empty( $attr['borderColor'] ) ? $this->getColorPresetCssVar( $attr['borderColor'] ) : $attr['style']['border']['color']; |
| 142 | $style .= '--sc-product-' . $prefix . '-border-color: ' . $border_color . ';'; |
| 143 | } |
| 144 | // text color. |
| 145 | if ( ! empty( $attr['textColor'] ) || ! empty( $attr['style']['color']['text'] ) ) { |
| 146 | $text_color = ! empty( $attr['textColor'] ) ? $this->getColorPresetCssVar( $attr['textColor'] ) : $attr['style']['color']['text']; |
| 147 | $style .= '--sc-product-' . $prefix . '-text-color: ' . $text_color . ';'; |
| 148 | } |
| 149 | if ( ! empty( $attr['backgroundColor'] ) || ! empty( $attr['style']['color']['background'] ) ) { |
| 150 | $background_color = ! empty( $attr['backgroundColor'] ) ? $this->getColorPresetCssVar( $attr['backgroundColor'] ) : $attr['style']['color']['background']; |
| 151 | $style .= '--sc-product-' . $prefix . '-background-color: ' . $background_color . ';'; |
| 152 | } |
| 153 | // text align. |
| 154 | if ( ! empty( $attr['align'] ) ) { |
| 155 | $style .= '--sc-product-' . $prefix . '-align: ' . $attr['align'] . ';'; |
| 156 | } |
| 157 | |
| 158 | return $style; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Get the style for the block. |
| 163 | * |
| 164 | * @param array $attr Product list & Product item attributes. |
| 165 | * @param array $item_attributes Product item attributes. |
| 166 | * |
| 167 | * @return string |
| 168 | */ |
| 169 | public function getStyle( $attr, $item_attributes ) { |
| 170 | $style = 'border-style: none !important;'; |
| 171 | $style .= $this->getProductListStyle( $attr ); |
| 172 | $style .= $this->getVars( $item_attributes, 'item' ); |
| 173 | return $style; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Render the block |
| 178 | * |
| 179 | * @param array $attributes Block attributes. |
| 180 | * @param string $content Post content. |
| 181 | * |
| 182 | * @return string |
| 183 | */ |
| 184 | public function render( $attributes, $content ) { |
| 185 | self::$instance++; |
| 186 | |
| 187 | // check for inner blocks. |
| 188 | $product_inner_blocks = $this->block->parsed_block['innerBlocks'] ?? []; |
| 189 | |
| 190 | $product_item_inner_blocks = $product_inner_blocks[0]['innerBlocks'] ?? $this->default_block_template; |
| 191 | $product_item_attributes = $product_inner_blocks[0]['attrs'] ?? $attributes; |
| 192 | |
| 193 | $layout_config = array_map( |
| 194 | function( $inner_block ) { |
| 195 | return (object) [ |
| 196 | 'blockName' => $inner_block['blockName'], |
| 197 | 'attributes' => $inner_block['attrs'], |
| 198 | ]; |
| 199 | }, |
| 200 | $product_item_inner_blocks |
| 201 | ); |
| 202 | |
| 203 | $style = ''; |
| 204 | $style .= $this->getStyle( $attributes, $product_item_attributes ); |
| 205 | |
| 206 | foreach ( $product_item_inner_blocks as $inner_blocks ) { |
| 207 | switch ( $inner_blocks['blockName'] ) { |
| 208 | case 'surecart/product-item-image': |
| 209 | $style .= $this->getVars( $inner_blocks['attrs'], 'image' ); |
| 210 | break; |
| 211 | case 'surecart/product-item-title': |
| 212 | $style .= $this->getVars( $inner_blocks['attrs'], 'title' ); |
| 213 | break; |
| 214 | case 'surecart/product-item-price': |
| 215 | $style .= $this->getVars( $inner_blocks['attrs'], 'price' ); |
| 216 | break; |
| 217 | default: |
| 218 | break; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | $query_collection_id = get_query_var( 'sc_collection_page_id' ); |
| 223 | $collection_id = $query_collection_id ? $query_collection_id : $attributes['collection_id'] ?? ''; |
| 224 | |
| 225 | $wrapper_attributes = get_block_wrapper_attributes( |
| 226 | array( |
| 227 | 'id' => 'selector-' . self::$instance, |
| 228 | 'style' => $style, |
| 229 | ) |
| 230 | ); |
| 231 | |
| 232 | \SureCart::assets()->addComponentData( |
| 233 | 'sc-product-item-list', |
| 234 | '#selector-' . self::$instance, |
| 235 | [ |
| 236 | 'layoutConfig' => $layout_config, |
| 237 | 'paginationAlignment' => $attributes['pagination_alignment'], |
| 238 | 'limit' => (int) $attributes['limit'], |
| 239 | 'paginationEnabled' => ! ! $attributes['pagination_enabled'], |
| 240 | 'ajaxPagination' => ! ! $attributes['ajax_pagination'], |
| 241 | 'paginationAutoScroll' => ! ! $attributes['pagination_auto_scroll'], |
| 242 | 'searchEnabled' => ! ! $attributes['search_enabled'], |
| 243 | 'sortEnabled' => ! ! $attributes['sort_enabled'], |
| 244 | 'collectionEnabled' => false, |
| 245 | 'collectionId' => $collection_id, |
| 246 | ] |
| 247 | ); |
| 248 | |
| 249 | return '<sc-product-item-list ' . $wrapper_attributes . '></sc-product-item-list>'; |
| 250 | } |
| 251 | } |
| 252 |