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