Block.php
50 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCartBlocks\Blocks\Product\PriceChoices; |
| 4 | |
| 5 | use SureCartBlocks\Blocks\Product\ProductBlock; |
| 6 | use SureCartBlocks\Util\BlockStyleAttributes; |
| 7 | |
| 8 | /** |
| 9 | * Product Title Block |
| 10 | */ |
| 11 | class Block extends ProductBlock { |
| 12 | |
| 13 | /** |
| 14 | * Render the block |
| 15 | * |
| 16 | * @param array $attributes Block attributes. |
| 17 | * @param string $content Post content. |
| 18 | * |
| 19 | * @return string |
| 20 | */ |
| 21 | public function render( $attributes, $content ) { |
| 22 | if ( empty( $attributes['id'] ) ) { |
| 23 | return \SureCart::block() |
| 24 | ->productPriceChoicesMigration( $attributes, $this->block ) |
| 25 | ->render(); |
| 26 | } |
| 27 | |
| 28 | $product = $this->getProductAndSetInitialState( $attributes['id'] ?? '' ); |
| 29 | if ( empty( $product->id ) ) { |
| 30 | return ''; |
| 31 | } |
| 32 | ['styles' => $styles] = BlockStyleAttributes::getClassesAndStylesFromAttributes( $attributes, [ 'margin' ] ); |
| 33 | |
| 34 | $attributes = get_block_wrapper_attributes( |
| 35 | [ |
| 36 | 'label' => esc_attr( $attributes['label'] ?? '' ), |
| 37 | 'class' => 'surecart-block product-price-choices', |
| 38 | 'product-id' => esc_attr( $product->id ), |
| 39 | 'style' => esc_attr( $this->getVars( $attributes, '--sc-choice' ) . ' --columns: ' . $attributes['columns'] ?? 2 . '; border: none; ' . $styles ), |
| 40 | 'show-price' => wp_validate_boolean( $attributes['show_price'] ) ? 'true' : 'false', |
| 41 | ] |
| 42 | ); |
| 43 | |
| 44 | return wp_sprintf( |
| 45 | '<sc-product-price-choices %1$s></sc-product-price-choices>', |
| 46 | $attributes |
| 47 | ); |
| 48 | } |
| 49 | } |
| 50 |