Block.php
334 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCartBlocks\Blocks\ProductItemList; |
| 4 | |
| 5 | use SureCart\Models\Collection; |
| 6 | use SureCart\Models\Product; |
| 7 | use SureCartBlocks\Blocks\BaseBlock; |
| 8 | /** |
| 9 | * ProductItemList block |
| 10 | */ |
| 11 | class Block extends BaseBlock { |
| 12 | /** |
| 13 | * Keeps track of instances of this block. |
| 14 | * |
| 15 | * @var integer |
| 16 | */ |
| 17 | public static $instance; |
| 18 | |
| 19 | /** |
| 20 | * Default block template. |
| 21 | * |
| 22 | * @var array |
| 23 | */ |
| 24 | protected $default_block_template = [ |
| 25 | [ |
| 26 | 'blockName' => 'surecart/product-item-image', |
| 27 | 'attrs' => [ |
| 28 | 'sizing' => 'cover', |
| 29 | 'ratio' => '1/1.33', |
| 30 | 'style' => [ |
| 31 | 'border' => [ |
| 32 | 'radius' => '6px', |
| 33 | ], |
| 34 | 'spacing' => [ |
| 35 | 'margin' => [ |
| 36 | 'bottom' => '16px', |
| 37 | ], |
| 38 | ], |
| 39 | ], |
| 40 | ], |
| 41 | ], |
| 42 | [ |
| 43 | 'blockName' => 'surecart/product-item-title', |
| 44 | 'attrs' => [ |
| 45 | 'level' => 3, |
| 46 | 'style' => [ |
| 47 | 'typography' => [ |
| 48 | 'fontSize' => '18px', |
| 49 | ], |
| 50 | 'color' => [ |
| 51 | 'text' => '#000000', |
| 52 | ], |
| 53 | 'spacing' => [ |
| 54 | 'margin' => [ |
| 55 | 'bottom' => '8px', |
| 56 | ], |
| 57 | ], |
| 58 | ], |
| 59 | ], |
| 60 | ], |
| 61 | [ |
| 62 | 'blockName' => 'surecart/product-item-price', |
| 63 | 'attrs' => [ |
| 64 | 'style' => [ |
| 65 | 'typography' => [ |
| 66 | 'fontSize' => '16px', |
| 67 | ], |
| 68 | 'color' => [ |
| 69 | 'text' => '#000000', |
| 70 | ], |
| 71 | 'spacing' => [ |
| 72 | 'margin' => [ |
| 73 | 'bottom' => '8px', |
| 74 | ], |
| 75 | ], |
| 76 | ], |
| 77 | ], |
| 78 | ], |
| 79 | ]; |
| 80 | |
| 81 | /** |
| 82 | * Get the style for the block. |
| 83 | * |
| 84 | * @param array $attr Product List attributes. |
| 85 | * @return string |
| 86 | */ |
| 87 | public function getProductListStyle( $attr ) { |
| 88 | $style = '--sc-product-item-list-column: ' . ( $attr['columns'] ?? 3 ) . ';'; |
| 89 | $style .= '--sc-product-item-list-gap: ' . $this->getSpacingPresetCssVar( $attr['style']['spacing']['blockGap'] ?? '40px' ) . ';'; |
| 90 | $style .= '--sc-pagination-font-size: ' . ( $attr['paginationSize'] ?? '14px' ) . ';'; |
| 91 | return $style; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Get the style for the block |
| 96 | * |
| 97 | * @param array $attr Style variables. |
| 98 | * @param string $prefix Prefix for the css variable. |
| 99 | * @return string |
| 100 | */ |
| 101 | public function getVars( $attr, $prefix ) { |
| 102 | $style = ''; |
| 103 | // padding. |
| 104 | if ( ! empty( $attr['style']['spacing']['padding'] ) ) { |
| 105 | $padding = $attr['style']['spacing']['padding']; |
| 106 | $style .= '--sc-product-' . $prefix . '-padding-top: ' . $this->getSpacingPresetCssVar( array_key_exists( 'top', $padding ) ? $padding['top'] : '0' ) . ';'; |
| 107 | $style .= '--sc-product-' . $prefix . '-padding-bottom: ' . $this->getSpacingPresetCssVar( array_key_exists( 'bottom', $padding ) ? $padding['bottom'] : '0' ) . ';'; |
| 108 | $style .= '--sc-product-' . $prefix . '-padding-left: ' . $this->getSpacingPresetCssVar( array_key_exists( 'left', $padding ) ? $padding['left'] : '0' ) . ';'; |
| 109 | $style .= '--sc-product-' . $prefix . '-padding-right: ' . $this->getSpacingPresetCssVar( array_key_exists( 'right', $padding ) ? $padding['right'] : '0' ) . ';'; |
| 110 | } |
| 111 | // margin. |
| 112 | if ( ! empty( $attr['style']['spacing']['margin'] ) ) { |
| 113 | $margin = $attr['style']['spacing']['margin']; |
| 114 | $style .= '--sc-product-' . $prefix . '-margin-top: ' . $this->getSpacingPresetCssVar( array_key_exists( 'top', $margin ) ? $margin['top'] : '0' ) . ';'; |
| 115 | $style .= '--sc-product-' . $prefix . '-margin-bottom: ' . $this->getSpacingPresetCssVar( array_key_exists( 'bottom', $margin ) ? $margin['bottom'] : '0' ) . ';'; |
| 116 | $style .= '--sc-product-' . $prefix . '-margin-left: ' . $this->getSpacingPresetCssVar( array_key_exists( 'left', $margin ) ? $margin['left'] : '0' ) . ';'; |
| 117 | $style .= '--sc-product-' . $prefix . '-margin-right: ' . $this->getSpacingPresetCssVar( array_key_exists( 'right', $margin ) ? $margin['right'] : '0' ) . ';'; |
| 118 | } |
| 119 | // aspect ratio. |
| 120 | if ( ! empty( $attr['ratio'] ) ) { |
| 121 | $style .= '--sc-product-' . $prefix . '-aspect-ratio: ' . $attr['ratio'] . ';'; |
| 122 | } |
| 123 | // border width. |
| 124 | if ( ! empty( $attr['style']['border']['width'] ) ) { |
| 125 | $style .= '--sc-product-' . $prefix . '-border-width: ' . $attr['style']['border']['width'] . ';'; |
| 126 | } |
| 127 | // border radius. |
| 128 | if ( ! empty( $attr['style']['border']['radius'] ) ) { |
| 129 | $style .= '--sc-product-' . $prefix . '-border-radius: ' . $attr['style']['border']['radius'] . ';'; |
| 130 | } |
| 131 | // font weight. |
| 132 | if ( ! empty( $attr['style']['typography']['fontWeight'] ) ) { |
| 133 | $style .= '--sc-product-' . $prefix . '-font-weight: ' . $attr['style']['typography']['fontWeight'] . ';'; |
| 134 | } |
| 135 | // font size. |
| 136 | if ( ! empty( $attr['fontSize'] ) || ! empty( $attr['style']['typography']['fontSize'] ) ) { |
| 137 | $font_size = ! empty( $attr['fontSize'] ) ? $this->getFontSizePresetCssVar( $attr['fontSize'] ) : $attr['style']['typography']['fontSize']; |
| 138 | $style .= '--sc-product-' . $prefix . '-font-size: ' . $font_size . ';'; |
| 139 | } |
| 140 | // border color. |
| 141 | if ( ! empty( $attr['borderColor'] ) || ! empty( $attr['style']['border']['color'] ) ) { |
| 142 | $border_color = ! empty( $attr['borderColor'] ) ? $this->getColorPresetCssVar( $attr['borderColor'] ) : $attr['style']['border']['color']; |
| 143 | $style .= '--sc-product-' . $prefix . '-border-color: ' . $border_color . ';'; |
| 144 | } |
| 145 | // text color. |
| 146 | if ( ! empty( $attr['textColor'] ) || ! empty( $attr['style']['color']['text'] ) ) { |
| 147 | $text_color = ! empty( $attr['textColor'] ) ? $this->getColorPresetCssVar( $attr['textColor'] ) : $attr['style']['color']['text']; |
| 148 | $style .= '--sc-product-' . $prefix . '-text-color: ' . $text_color . ';'; |
| 149 | } |
| 150 | if ( ! empty( $attr['backgroundColor'] ) || ! empty( $attr['style']['color']['background'] ) ) { |
| 151 | $background_color = ! empty( $attr['backgroundColor'] ) ? $this->getColorPresetCssVar( $attr['backgroundColor'] ) : $attr['style']['color']['background']; |
| 152 | $style .= '--sc-product-' . $prefix . '-background-color: ' . $background_color . ';'; |
| 153 | } |
| 154 | // text align. |
| 155 | if ( ! empty( $attr['align'] ) ) { |
| 156 | $style .= '--sc-product-' . $prefix . '-align: ' . $attr['align'] . ';'; |
| 157 | } |
| 158 | |
| 159 | return $style; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Get the style for the block |
| 164 | * |
| 165 | * @param array $attr Product List attributes. |
| 166 | * @param array $item_attributes Product item attributes. |
| 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 | * Get the dummy products array. |
| 178 | * |
| 179 | * @param int $limit Limit per page. |
| 180 | * @return array Dummy Products. |
| 181 | */ |
| 182 | public function getDummyProducts( $limit = 15 ) { |
| 183 | $dummy_products = array(); |
| 184 | |
| 185 | for ( $i = 1; $i <= $limit; $i++ ) { |
| 186 | $product = array( |
| 187 | 'permalink' => '#', |
| 188 | 'name' => __( 'Example Product Title', 'surecart' ), |
| 189 | 'created_at' => wp_rand( 1, 40 ), |
| 190 | 'prices' => array( |
| 191 | 'data' => array( |
| 192 | array( |
| 193 | 'amount' => 1900, |
| 194 | 'currency' => 'USD', |
| 195 | ), |
| 196 | ), |
| 197 | ), |
| 198 | ); |
| 199 | |
| 200 | $dummy_products[] = $product; |
| 201 | } |
| 202 | |
| 203 | return $dummy_products; |
| 204 | } |
| 205 | /** |
| 206 | * Render the block |
| 207 | * |
| 208 | * @param array $attributes Block attributes. |
| 209 | * @param string $content Post content. |
| 210 | * |
| 211 | * @return string |
| 212 | */ |
| 213 | public function render( $attributes, $content ) { |
| 214 | if ( isset( $attributes['sort_enabled'] ) ) { // This way we know it's the old block. |
| 215 | return \SureCart::block()->productListMigration( $attributes, $this->block )->render(); |
| 216 | } |
| 217 | |
| 218 | self::$instance = wp_unique_id( 'sc-product-item-list-' ); |
| 219 | |
| 220 | // check for inner blocks. |
| 221 | $product_inner_blocks = $this->block->parsed_block['innerBlocks'] ?? []; |
| 222 | |
| 223 | $product_item_inner_blocks = $product_inner_blocks[0]['innerBlocks'] ?? $this->default_block_template; |
| 224 | $product_item_attributes = $product_inner_blocks[0]['attrs'] ?? $attributes; |
| 225 | |
| 226 | $layout_config = array_map( |
| 227 | function ( $inner_block ) { |
| 228 | return (object) [ |
| 229 | 'blockName' => $inner_block['blockName'], |
| 230 | 'attributes' => $inner_block['attrs'], |
| 231 | ]; |
| 232 | }, |
| 233 | $product_item_inner_blocks |
| 234 | ); |
| 235 | |
| 236 | $style = ''; |
| 237 | $style .= $this->getStyle( $attributes, $product_item_attributes ); |
| 238 | |
| 239 | foreach ( $product_item_inner_blocks as $inner_blocks ) { |
| 240 | switch ( $inner_blocks['blockName'] ) { |
| 241 | case 'surecart/product-item-image': |
| 242 | $style .= $this->getVars( $inner_blocks['attrs'], 'image' ); |
| 243 | break; |
| 244 | case 'surecart/product-item-title': |
| 245 | $style .= $this->getVars( $inner_blocks['attrs'], 'title' ); |
| 246 | break; |
| 247 | case 'surecart/product-item-price': |
| 248 | $style .= $this->getVars( $inner_blocks['attrs'], 'price' ); |
| 249 | break; |
| 250 | default: |
| 251 | break; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | // backwards compat. |
| 256 | if ( empty( $attributes['type'] ) && ! empty( $attributes['ids'] ) ) { |
| 257 | $attributes['type'] = 'custom'; |
| 258 | } |
| 259 | if ( empty( $attributes['type'] ) ) { |
| 260 | $attributes['type'] = ''; |
| 261 | } |
| 262 | |
| 263 | // query posts. |
| 264 | $product_query = new \WP_Query( |
| 265 | array( |
| 266 | 'post_type' => 'sc_product', |
| 267 | 'posts_per_page' => 10, |
| 268 | ) |
| 269 | ); |
| 270 | |
| 271 | // get the product for each post. |
| 272 | $products = array_map( |
| 273 | function ( $post ) { |
| 274 | return sc_get_product( $post ); |
| 275 | }, |
| 276 | $product_query->posts ?? [] |
| 277 | ); |
| 278 | |
| 279 | \SureCart::assets()->addComponentData( |
| 280 | 'sc-product-item-list', |
| 281 | '#' . self::$instance, |
| 282 | [ |
| 283 | 'layoutConfig' => $layout_config, |
| 284 | 'paginationAlignment' => $attributes['pagination_alignment'], |
| 285 | 'limit' => $attributes['limit'], |
| 286 | 'style' => $style, |
| 287 | 'pagination' => [ |
| 288 | 'total' => $product_query->found_posts, |
| 289 | 'total_pages' => $product_query->max_num_pages, |
| 290 | ], |
| 291 | 'page' => (int) ( $_GET['product-page'] ?? 1 ), |
| 292 | 'ids' => 'custom' === $attributes['type'] ? array_values( array_filter( $attributes['ids'] ) ) : [], |
| 293 | 'paginationEnabled' => \SureCart::account()->isConnected() ? wp_validate_boolean( $attributes['pagination_enabled'] ) : false, |
| 294 | 'ajaxPagination' => wp_validate_boolean( $attributes['ajax_pagination'] ), |
| 295 | 'paginationAutoScroll' => $attributes['pagination_auto_scroll'], |
| 296 | 'searchEnabled' => \SureCart::account()->isConnected() ? wp_validate_boolean( $attributes['search_enabled'] ) : false, |
| 297 | 'sortEnabled' => \SureCart::account()->isConnected() ? wp_validate_boolean( $attributes['sort_enabled'] ) : false, |
| 298 | 'featured' => 'featured' === $attributes['type'], |
| 299 | 'products' => ! \SureCart::account()->isConnected() ? $this->getDummyProducts( $attributes['limit'] ) : $products, |
| 300 | 'collectionEnabled' => \SureCart::account()->isConnected() ? wp_validate_boolean( $attributes['collection_enabled'] ) : false, |
| 301 | 'pageTitle' => get_the_title(), |
| 302 | ] |
| 303 | ); |
| 304 | |
| 305 | return '<sc-product-item-list id="' . esc_attr( self::$instance ) . '"></sc-product-item-list>'; |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Get the query for the products. |
| 310 | * |
| 311 | * @param array $attributes Block attributes. |
| 312 | * |
| 313 | * @return array |
| 314 | */ |
| 315 | public function getQuery( $attributes ) { |
| 316 | $query = [ |
| 317 | 'expand' => [ 'prices', 'featured_product_media', 'product_medias', 'product_media.media', 'variants' ], |
| 318 | 'archived' => false, |
| 319 | 'status' => [ 'published' ], |
| 320 | 'sort' => 'cataloged_at:desc', |
| 321 | ]; |
| 322 | |
| 323 | if ( 'featured' === ( $attributes['type'] ?? '' ) ) { |
| 324 | $query['featured'] = true; |
| 325 | } |
| 326 | |
| 327 | if ( 'custom' === ( $attributes['type'] ?? '' ) ) { |
| 328 | $query['ids'] = array_values( array_filter( $attributes['ids'] ?? [] ) ); |
| 329 | } |
| 330 | |
| 331 | return $query; |
| 332 | } |
| 333 | } |
| 334 |