Block.php
118 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCartBlocks\Blocks\PriceSelector; |
| 4 | |
| 5 | use SureCartBlocks\Blocks\BaseBlock; |
| 6 | |
| 7 | /** |
| 8 | * Checkout block |
| 9 | */ |
| 10 | class Block extends BaseBlock { |
| 11 | /** |
| 12 | * Render the block |
| 13 | * |
| 14 | * @param array $attributes Block attributes. |
| 15 | * @param string $content Post content. |
| 16 | * |
| 17 | * @return string |
| 18 | */ |
| 19 | public function render( $attributes, $content ) { |
| 20 | sc_initial_state( |
| 21 | [ |
| 22 | 'checkout' => [ |
| 23 | 'initialLineItems' => sc_initial_line_items( $this->getInitialLineItems() ), |
| 24 | ], |
| 25 | ] |
| 26 | ); |
| 27 | |
| 28 | return '<sc-price-choices |
| 29 | class="wp-block-surecart-price-choice ' . esc_attr( $this->getClasses( $attributes ) || '' ) . '" |
| 30 | label="' . esc_attr( $attributes['label'] ?? '' ) . '" |
| 31 | type="' . esc_attr( $attributes['type'] ?? 'radio' ) . '" |
| 32 | columns="' . intval( $attributes['columns'] ?? 1 ) . '" |
| 33 | >' . |
| 34 | $this->getRemovedPriceChoicesWrapper( $content ) |
| 35 | . '</sc-price-choices>'; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Get the initial line items. |
| 40 | * |
| 41 | * @return array |
| 42 | */ |
| 43 | public function getInitialLineItems() { |
| 44 | // get choice blocks. |
| 45 | $choices = $this->getInnerPriceChoices(); |
| 46 | |
| 47 | // are any checked by default? |
| 48 | $checked = array_filter( |
| 49 | $choices, |
| 50 | function( $block ) { |
| 51 | return ! empty( $block['attrs']['checked'] ); |
| 52 | } |
| 53 | ); |
| 54 | |
| 55 | // there are no checked, so use the first one. |
| 56 | if ( empty( $checked ) ) { |
| 57 | $checked = [ $choices[0] ] ?? []; |
| 58 | } |
| 59 | |
| 60 | // get the line items. |
| 61 | return $this->convertPriceBlocksToLineItems( $checked ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Get the inner price choice blocks. |
| 66 | * |
| 67 | * @return array |
| 68 | */ |
| 69 | public function getInnerPriceChoices() { |
| 70 | return array_filter( |
| 71 | $this->block->parsed_block['innerBlocks'], |
| 72 | function( $block ) { |
| 73 | return 'surecart/price-choice' === $block['blockName'] && ! empty( $block['attrs']['price_id'] ); |
| 74 | } |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Convert price blocks to line items |
| 80 | * |
| 81 | * @param array $blocks Array of parsed blocks. |
| 82 | * |
| 83 | * @return array Array of line items. |
| 84 | */ |
| 85 | public function convertPriceBlocksToLineItems( $blocks ) { |
| 86 | return array_values( |
| 87 | array_map( |
| 88 | function( $block ) { |
| 89 | return [ |
| 90 | 'price' => $block['attrs']['price_id'], |
| 91 | 'quantity' => $block['attrs']['quantity'] ?? 1, |
| 92 | ]; |
| 93 | }, |
| 94 | $blocks |
| 95 | ) |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Remove price choice wrapper and return the html. |
| 101 | * |
| 102 | * @param string $content Block content. |
| 103 | * |
| 104 | * @return string |
| 105 | */ |
| 106 | public function getRemovedPriceChoicesWrapper( $content ): string { |
| 107 | if(empty($content)){ |
| 108 | return ''; |
| 109 | } |
| 110 | |
| 111 | $price_choices_tag = trim(str_replace('</sc-price-choices>', '', strip_tags($content, '<sc-price-choices>'))); |
| 112 | $content = str_replace($price_choices_tag, '', $content); |
| 113 | $content = str_replace('</sc-price-choices>', '', $content); |
| 114 | |
| 115 | return filter_block_content( $content, 'post' ); |
| 116 | } |
| 117 | } |
| 118 |