| 1 |
<?php |
| 2 |
|
| 3 |
defined( 'ABSPATH' ) || die(); |
| 4 |
|
| 5 |
use Sellkit\Global_Checkout\Checkout; |
| 6 |
|
| 7 |
/** |
| 8 |
* Sellkit global checkout. |
| 9 |
* |
| 10 |
* @since 1.7.4 |
| 11 |
*/ |
| 12 |
class Sellkit_Global_Checkout { |
| 13 |
/** |
| 14 |
* Class construct. |
| 15 |
* |
| 16 |
* @since 1.7.4 |
| 17 |
*/ |
| 18 |
public function __construct() { |
| 19 |
add_action( 'wp_ajax_sellkit_global_checkout_id', [ $this, 'get_global_checkout_funnel_id' ] ); |
| 20 |
add_action( 'wp_ajax_sellkit_global_checkout_toggle_status', [ $this, 'change_status' ] ); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Get global checkout id. |
| 25 |
* |
| 26 |
* @since 1.7.4 |
| 27 |
*/ |
| 28 |
public function get_global_checkout_funnel_id() { |
| 29 |
check_ajax_referer( 'sellkit', 'nonce' ); |
| 30 |
|
| 31 |
$global_checkout_id = get_option( Checkout::SELLKIT_GLOBAL_CHECKOUT_OPTION, 0 ); |
| 32 |
|
| 33 |
// Post not exists. |
| 34 |
if ( false === get_post_status( $global_checkout_id ) ) { |
| 35 |
wp_send_json_success( 0 ); |
| 36 |
} |
| 37 |
|
| 38 |
wp_send_json_success( $global_checkout_id ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Set global checkout funnel post status (activate / deactivate). |
| 43 |
* |
| 44 |
* Expects POST `status` as the target status: `publish` or `draft`. |
| 45 |
* |
| 46 |
* @since 1.7.4 |
| 47 |
*/ |
| 48 |
public function change_status() { |
| 49 |
check_ajax_referer( 'sellkit', 'nonce' ); |
| 50 |
|
| 51 |
$target_status = isset( $_POST['status'] ) ? sanitize_text_field( wp_unslash( $_POST['status'] ) ) : ''; |
| 52 |
$post_id = isset( $_POST['id'] ) ? absint( $_POST['id'] ) : 0; |
| 53 |
|
| 54 |
if ( ! in_array( $target_status, [ 'publish', 'draft' ], true ) || ! $post_id ) { |
| 55 |
wp_send_json_error( [ 'message' => __( 'Invalid request.', 'sellkit' ) ] ); |
| 56 |
} |
| 57 |
|
| 58 |
$post = get_post( $post_id ); |
| 59 |
|
| 60 |
if ( ! $post || 'sellkit-funnels' !== $post->post_type ) { |
| 61 |
wp_send_json_error( [ 'message' => __( 'Invalid funnel.', 'sellkit' ) ] ); |
| 62 |
} |
| 63 |
|
| 64 |
$global_checkout_id = (int) get_option( Checkout::SELLKIT_GLOBAL_CHECKOUT_OPTION, 0 ); |
| 65 |
|
| 66 |
if ( $global_checkout_id !== $post_id ) { |
| 67 |
wp_send_json_error( [ 'message' => __( 'Not the Global Checkout funnel.', 'sellkit' ) ] ); |
| 68 |
} |
| 69 |
|
| 70 |
$updated = wp_update_post( |
| 71 |
[ |
| 72 |
'ID' => $post_id, |
| 73 |
'post_status' => $target_status, |
| 74 |
], |
| 75 |
true |
| 76 |
); |
| 77 |
|
| 78 |
if ( is_wp_error( $updated ) || ! $updated ) { |
| 79 |
wp_send_json_error( [ 'message' => __( 'Could not update status.', 'sellkit' ) ] ); |
| 80 |
} |
| 81 |
|
| 82 |
$saved_status = get_post_status( $post_id ); |
| 83 |
|
| 84 |
wp_send_json_success( $saved_status ? $saved_status : $target_status ); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
new Sellkit_Global_Checkout(); |
| 89 |
|