swissChili | f0cbdc3 | 2023-01-05 17:21:38 -0500 | [diff] [blame^] | 1 | <?php |
| 2 | /** |
| 3 | * Class WC_Stripe_REST_UPE_Flag_Toggle_Controller |
| 4 | */ |
| 5 | |
| 6 | defined( 'ABSPATH' ) || exit; |
| 7 | |
| 8 | /** |
| 9 | * REST controller for UPE feature flag. |
| 10 | */ |
| 11 | class WC_Stripe_REST_UPE_Flag_Toggle_Controller extends WC_Stripe_REST_Base_Controller { |
| 12 | /** |
| 13 | * Endpoint path. |
| 14 | * |
| 15 | * @var string |
| 16 | */ |
| 17 | protected $rest_base = 'wc_stripe/upe_flag_toggle'; |
| 18 | |
| 19 | /** |
| 20 | * Configure REST API routes. |
| 21 | */ |
| 22 | public function register_routes() { |
| 23 | register_rest_route( |
| 24 | $this->namespace, |
| 25 | '/' . $this->rest_base, |
| 26 | [ |
| 27 | 'methods' => WP_REST_Server::READABLE, |
| 28 | 'callback' => [ $this, 'get_flag' ], |
| 29 | 'permission_callback' => [ $this, 'check_permission' ], |
| 30 | ] |
| 31 | ); |
| 32 | register_rest_route( |
| 33 | $this->namespace, |
| 34 | '/' . $this->rest_base, |
| 35 | [ |
| 36 | 'methods' => WP_REST_Server::EDITABLE, |
| 37 | 'callback' => [ $this, 'set_flag' ], |
| 38 | 'permission_callback' => [ $this, 'check_permission' ], |
| 39 | 'args' => [ |
| 40 | 'is_upe_enabled' => [ |
| 41 | 'description' => __( 'Determines if the UPE feature flag is enabled.', 'woocommerce-gateway-stripe' ), |
| 42 | 'type' => 'boolean', |
| 43 | 'validate_callback' => 'rest_validate_request_arg', |
| 44 | ], |
| 45 | ], |
| 46 | ] |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Retrieve flag status. |
| 52 | * |
| 53 | * @return WP_REST_Response |
| 54 | */ |
| 55 | public function get_flag() { |
| 56 | return new WP_REST_Response( |
| 57 | [ |
| 58 | 'is_upe_enabled' => WC_Stripe_Feature_Flags::is_upe_checkout_enabled(), |
| 59 | ] |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Update the data. |
| 65 | * |
| 66 | * @param WP_REST_Request $request Full data about the request. |
| 67 | */ |
| 68 | public function set_flag( WP_REST_Request $request ) { |
| 69 | $is_upe_enabled = $request->get_param( 'is_upe_enabled' ); |
| 70 | |
| 71 | if ( null === $is_upe_enabled ) { |
| 72 | return new WP_REST_Response( [ 'result' => 'bad_request' ], 400 ); |
| 73 | } |
| 74 | |
| 75 | $settings = get_option( 'woocommerce_stripe_settings', [] ); |
| 76 | $settings[ WC_Stripe_Feature_Flags::UPE_CHECKOUT_FEATURE_ATTRIBUTE_NAME ] = $is_upe_enabled ? 'yes' : 'disabled'; |
| 77 | |
| 78 | update_option( 'woocommerce_stripe_settings', $settings ); |
| 79 | |
| 80 | // including the class again because otherwise it's not present. |
| 81 | if ( WC_Stripe_Inbox_Notes::are_inbox_notes_supported() ) { |
| 82 | require_once WC_STRIPE_PLUGIN_PATH . '/includes/notes/class-wc-stripe-upe-availability-note.php'; |
| 83 | WC_Stripe_UPE_Availability_Note::possibly_delete_note(); |
| 84 | |
| 85 | require_once WC_STRIPE_PLUGIN_PATH . '/includes/notes/class-wc-stripe-upe-stripelink-note.php'; |
| 86 | WC_Stripe_UPE_StripeLink_Note::possibly_delete_note(); |
| 87 | } |
| 88 | |
| 89 | return new WP_REST_Response( [ 'result' => 'success' ], 200 ); |
| 90 | } |
| 91 | } |