PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 2.0.0
Subscriptions for WooCommerce with Stripe Recurring Payments v2.0.0
2.0.0 1.11.2 1.11.1 1.11.0 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.6 1.9.5 trunk 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 All 61 releases
subscription / includes / Admin / OnboardingAjax.php

OnboardingAjax.php in Subscriptions for WooCommerce with Stripe Recurring Payments 2.0.0, at includes/Admin/OnboardingAjax.php

88 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SpringDevs\Subscription\Admin;
4
5 /**
6 * AJAX handlers for the onboarding wizard.
7 *
8 * The wizard creates a plan + duration and connects it to a product entirely
9 * through the Plans REST API (wpsubscription/v1/plans). The one thing REST does
10 * not offer is creating a WooCommerce product, so that single step lives here;
11 * everything else — group, term, relation — is done client-side against REST.
12 *
13 * @package SpringDevs\Subscription\Admin
14 */
15 class OnboardingAjax {
16
17 /**
18 * Initialize the class.
19 */
20 public function __construct() {
21 add_action( 'wp_ajax_subscrpt_create_wizard_product', array( $this, 'create_wizard_product' ) );
22 add_action( 'wp_ajax_subscrpt_reset_wizard', array( $this, 'reset_wizard' ) );
23 }
24
25 /**
26 * Create a bare simple product (name + price) for the wizard to connect a
27 * plan to. No subscription meta is written here — connecting the plan (the
28 * REST relation) is what makes the product subscribable.
29 *
30 * @return void Sends JSON.
31 */
32 public function create_wizard_product() {
33 check_ajax_referer( 'subscrpt_onboarding_wizard', 'nonce' );
34
35 if ( ! current_user_can( 'manage_options' ) ) {
36 wp_send_json_error( array( 'message' => __( 'Permission denied.', 'subscription' ) ), 403 );
37 }
38
39 $product_name = isset( $_POST['product_name'] ) ? sanitize_text_field( wp_unslash( $_POST['product_name'] ) ) : '';
40 $product_price = isset( $_POST['product_price'] ) ? sanitize_text_field( wp_unslash( $_POST['product_price'] ) ) : '';
41
42 if ( '' === trim( $product_name ) ) {
43 wp_send_json_error( array( 'message' => __( 'Product name is required.', 'subscription' ) ) );
44 }
45
46 $product = new \WC_Product_Simple();
47 $product->set_name( $product_name );
48 if ( '' !== trim( $product_price ) ) {
49 $product->set_regular_price( wc_format_decimal( $product_price ) );
50 }
51 $product->set_status( 'publish' );
52 $product_id = $product->save();
53
54 if ( ! $product_id ) {
55 wp_send_json_error( array( 'message' => __( 'Failed to create product. Please try again.', 'subscription' ) ) );
56 }
57
58 wp_send_json_success(
59 array(
60 'product_id' => $product_id,
61 'product_name' => $product_name,
62 'product_price' => $product_price,
63 )
64 );
65 }
66
67 /**
68 * Reset wizard session (for "Add another" / "Start over" actions).
69 *
70 * @return void Sends JSON.
71 */
72 public function reset_wizard() {
73 check_ajax_referer( 'subscrpt_onboarding_wizard', 'nonce' );
74
75 if ( ! current_user_can( 'manage_options' ) ) {
76 wp_send_json_error( array( 'message' => __( 'Permission denied.', 'subscription' ) ), 403 );
77 }
78
79 if ( ! session_id() ) {
80 session_start();
81 }
82
83 unset( $_SESSION['subscrpt_onboarding_wizard'] );
84
85 wp_send_json_success();
86 }
87 }
88