| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace SureCart\Controllers\Web; |
| 6 |
|
| 7 |
/** |
| 8 |
* Handles Checkout related routes. |
| 9 |
*/ |
| 10 |
class CheckoutFormsController { |
| 11 |
|
| 12 |
/** |
| 13 |
* Change the mode of the checkout form. |
| 14 |
*/ |
| 15 |
public function changeMode() { |
| 16 |
$form_post_id = get_query_var( 'sc_checkout_change_mode' ); |
| 17 |
$checkout_post_id = get_query_var( 'sc_checkout_post' ); |
| 18 |
$form_post = get_post( $form_post_id ); |
| 19 |
$checkout_page_post = get_post( $checkout_post_id ); |
| 20 |
|
| 21 |
if ( empty( $form_post ) || empty( $checkout_page_post ) ) { |
| 22 |
wp_die( esc_html__( 'Invalid request.', 'surecart' ) ); |
| 23 |
} |
| 24 |
|
| 25 |
$form_block = \SureCart::post()->getFormBlock( $checkout_page_post ); |
| 26 |
if ( ! $form_block ) { |
| 27 |
wp_die( esc_html__( 'There is no checkout form block on this page.', 'surecart' ) ); |
| 28 |
} |
| 29 |
|
| 30 |
// Get the mode. |
| 31 |
$mode = $form_block['attrs']['mode'] ?? 'live'; |
| 32 |
|
| 33 |
// Change the mode. |
| 34 |
$form_block['attrs']['mode'] = 'test' === $mode ? 'live' : 'test'; |
| 35 |
|
| 36 |
// Update the post. |
| 37 |
wp_update_post( |
| 38 |
array( |
| 39 |
'ID' => $form_post_id, |
| 40 |
'post_content' => serialize_blocks( [ $form_block ] ), |
| 41 |
) |
| 42 |
); |
| 43 |
|
| 44 |
// Redirect to the checkout page. |
| 45 |
return \SureCart::redirect()->to( get_permalink( $checkout_post_id ) ); |
| 46 |
} |
| 47 |
} |
| 48 |
|