Block.php
148 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCartBlocks\Blocks\Form; |
| 4 | |
| 5 | use SureCart\Models\ManualPaymentMethod; |
| 6 | use SureCart\Models\Processor; |
| 7 | use SureCartBlocks\Blocks\BaseBlock; |
| 8 | |
| 9 | /** |
| 10 | * Checkout block |
| 11 | */ |
| 12 | class Block extends BaseBlock { |
| 13 | /** |
| 14 | * Get the style for the block |
| 15 | * |
| 16 | * @param array $attributes Block attributes. |
| 17 | * @return string |
| 18 | */ |
| 19 | public function getStyle( $attributes ) { |
| 20 | $style = 'text-align: left;'; |
| 21 | $style .= '--sc-form-row-spacing: ' . ( $attributes['gap'] ?? '25' ) . ';'; |
| 22 | if ( ! empty( $attributes['color'] ) ) { |
| 23 | $style .= '--sc-color-primary-500: ' . sanitize_hex_color( $attributes['color'] ) . ';'; |
| 24 | $style .= '--sc-focus-ring-color-primary: ' . sanitize_hex_color( $attributes['color'] ) . ';'; |
| 25 | $style .= '--sc-input-border-color-focus: ' . sanitize_hex_color( $attributes['color'] ) . ';'; |
| 26 | } |
| 27 | return $style; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Render the block |
| 32 | * |
| 33 | * @param array $attributes Block attributes. |
| 34 | * @param string $content Post content. |
| 35 | * |
| 36 | * @return string |
| 37 | */ |
| 38 | public function render( $attributes, $content ) { |
| 39 | global $sc_form_id; |
| 40 | $post = get_post( $sc_form_id ); |
| 41 | $user = wp_get_current_user(); |
| 42 | |
| 43 | $processors = Processor::get(); |
| 44 | if ( is_wp_error( $processors ) ) { |
| 45 | $processors = []; |
| 46 | } |
| 47 | |
| 48 | // set the initial state. |
| 49 | sc_initial_state( |
| 50 | array_filter( |
| 51 | [ |
| 52 | 'checkout' => [ |
| 53 | 'formId' => $attributes['form_id'] ?? $sc_form_id, |
| 54 | 'mode' => apply_filters( 'surecart/payments/mode', $attributes['mode'] ?? 'live' ), |
| 55 | 'product' => $attributes['product'] ?? [], |
| 56 | 'currencyCode' => $attributes['currency'] ?? \SureCart::account()->currency, |
| 57 | 'groupId' => 'sc-checkout-' . ( $attributes['form_id'] ?? $sc_form_id ), |
| 58 | 'abandonedCheckoutEnabled' => ! is_admin(), |
| 59 | 'taxProtocol' => \SureCart::account()->tax_protocol, |
| 60 | 'isCheckoutPage' => true, |
| 61 | 'validateStock' => ! is_admin(), |
| 62 | ], |
| 63 | 'processors' => [ |
| 64 | 'processors' => array_values( |
| 65 | array_filter( |
| 66 | $processors ?? [], |
| 67 | function( $processor ) { |
| 68 | return $processor->approved && $processor->enabled; |
| 69 | } |
| 70 | ) |
| 71 | ), |
| 72 | 'manualPaymentMethods' => (array) ManualPaymentMethod::where( [ 'archived' => false ] )->get() ?? [], |
| 73 | 'config' => [ |
| 74 | 'stripe' => [ |
| 75 | 'paymentElement' => (bool) get_option( 'sc_stripe_payment_element', true ), |
| 76 | ], |
| 77 | ], |
| 78 | ], |
| 79 | 'user' => [ |
| 80 | 'loggedIn' => is_user_logged_in(), |
| 81 | 'email' => $user->user_email, |
| 82 | 'name' => $user->display_name, |
| 83 | ], |
| 84 | 'form' => array_filter( |
| 85 | [ |
| 86 | 'text' => array_filter( |
| 87 | [ |
| 88 | 'loading' => array_filter( $attributes['loading_text'] ?? [] ), |
| 89 | 'success' => array_filter( $attributes['success_text'] ?? [] ), |
| 90 | ] |
| 91 | ), |
| 92 | ] |
| 93 | ), |
| 94 | ] |
| 95 | ) |
| 96 | ); |
| 97 | |
| 98 | if ( ! empty( $attributes['prices'] ) ) { |
| 99 | $line_items = $this->convertPricesToLineItems( $attributes['prices'] ); |
| 100 | sc_initial_state( |
| 101 | [ |
| 102 | 'checkout' => [ |
| 103 | 'initialLineItems' => sc_initial_line_items( $line_items ), |
| 104 | ], |
| 105 | ] |
| 106 | ); |
| 107 | } |
| 108 | |
| 109 | return \SureCart::blocks()->render( |
| 110 | 'blocks/form', |
| 111 | [ |
| 112 | 'align' => $attributes['align'] ?? '', |
| 113 | 'modified' => $post->post_modified_gmt ?? '', |
| 114 | 'honeypot_enabled' => (bool) get_option( 'surecart_honeypot_enabled', true ), |
| 115 | 'classes' => $this->getClasses( $attributes ), |
| 116 | 'style' => $this->getStyle( $attributes ), |
| 117 | 'content' => $content, |
| 118 | 'id' => 'sc-checkout-' . ( $attributes['form_id'] ?? $sc_form_id ), |
| 119 | 'success_url' => ! empty( $attributes['success_url'] ) ? $attributes['success_url'] : \SureCart::pages()->url( 'order-confirmation' ), |
| 120 | ] |
| 121 | ); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Convert price blocks to line items |
| 126 | * |
| 127 | * @param array $prices Array of prices. |
| 128 | * |
| 129 | * @return array Array of line items. |
| 130 | */ |
| 131 | public function convertPricesToLineItems( $prices ) { |
| 132 | return array_values( |
| 133 | array_map( |
| 134 | function( $price ) { |
| 135 | return array_filter( |
| 136 | [ |
| 137 | 'price' => $price['id'], |
| 138 | 'variant' => $price['variant_id'] ?? null, |
| 139 | 'quantity' => $price['quantity'] ?? 1, |
| 140 | ] |
| 141 | ); |
| 142 | }, |
| 143 | $prices |
| 144 | ) |
| 145 | ); |
| 146 | } |
| 147 | } |
| 148 |