class-wc-rest-connect-account-settings-controller.php
1.93 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
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( class_exists( 'WC_REST_Connect_Account_Settings_Controller' ) ) {
return;
}
class WC_REST_Connect_Account_Settings_Controller extends WC_REST_Connect_Base_Controller {
protected $rest_base = 'connect/account/settings';
/*
* @var WC_Connect_Payment_Methods_Store
*/
protected $payment_methods_store;
/**
* @var WC_Connect_Account_Settings
*/
protected $account_settings;
public function __construct( WC_Connect_API_Client $api_client, WC_Connect_Service_Settings_Store $settings_store, WC_Connect_Logger $logger, WC_Connect_Payment_Methods_Store $payment_methods_store ) {
parent::__construct( $api_client, $settings_store, $logger );
$this->payment_methods_store = $payment_methods_store;
$this->account_settings = new WC_Connect_Account_Settings(
$settings_store,
$payment_methods_store
);
}
public function get() {
return new WP_REST_Response(
array_merge(
array( 'success' => true ),
$this->account_settings->get()
),
200
);
}
public function post( $request ) {
$settings = $request->get_json_params();
if ( ! $this->settings_store->can_user_manage_payment_methods() ) {
// Ignore the user-provided payment method ID if they don't have permission to change it
$old_settings = $this->settings_store->get_account_settings();
$settings['selected_payment_method_id'] = $old_settings['selected_payment_method_id'];
}
$result = $this->settings_store->update_account_settings( $settings );
if ( is_wp_error( $result ) ) {
$error = new WP_Error(
'save_failed',
sprintf(
__( 'Unable to update settings. %s', 'woocommerce-services' ),
$result->get_error_message()
),
array_merge(
array( 'status' => 400 ),
$result->get_error_data()
)
);
$this->logger->log( $error, __CLASS__ );
return $error;
}
return new WP_REST_Response( array( 'success' => true ), 200 );
}
}