1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* WooCommerce API Settings |
4
|
|
|
* |
5
|
|
|
* @author WooThemes |
6
|
|
|
* @category Admin |
7
|
|
|
* @package WooCommerce/Admin |
8
|
|
|
* @version 2.4.0 |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
12
|
|
|
exit; // Exit if accessed directly |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
if ( ! class_exists( 'WC_Settings_Rest_API' ) ) : |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* WC_Settings_Rest_API. |
19
|
|
|
*/ |
20
|
|
|
class WC_Settings_Rest_API extends WC_Settings_Page { |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Constructor. |
24
|
|
|
*/ |
25
|
|
View Code Duplication |
public function __construct() { |
|
|
|
|
26
|
|
|
$this->id = 'api'; |
27
|
|
|
$this->label = __( 'API', 'woocommerce' ); |
28
|
|
|
|
29
|
|
|
add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_settings_page' ), 20 ); |
30
|
|
|
add_action( 'woocommerce_settings_' . $this->id, array( $this, 'output' ) ); |
31
|
|
|
add_action( 'woocommerce_sections_' . $this->id, array( $this, 'output_sections' ) ); |
32
|
|
|
add_action( 'woocommerce_settings_form_method_tab_' . $this->id, array( $this, 'form_method' ) ); |
33
|
|
|
add_action( 'woocommerce_settings_save_' . $this->id, array( $this, 'save' ) ); |
34
|
|
|
|
35
|
|
|
$this->notices(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get sections. |
40
|
|
|
* |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
|
|
public function get_sections() { |
44
|
|
|
$sections = array( |
45
|
|
|
'' => __( 'Settings', 'woocommerce' ), |
46
|
|
|
'keys' => __( 'Keys/Apps', 'woocommerce' ), |
47
|
|
|
'webhooks' => __( 'Webhooks', 'woocommerce' ) |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
return apply_filters( 'woocommerce_get_sections_' . $this->id, $sections ); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get settings array. |
55
|
|
|
* |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
|
|
public function get_settings() { |
59
|
|
|
$settings = apply_filters( 'woocommerce_settings_rest_api', array( |
60
|
|
|
array( |
61
|
|
|
'title' => __( 'General Options', 'woocommerce' ), |
62
|
|
|
'type' => 'title', |
63
|
|
|
'desc' => '', |
64
|
|
|
'id' => 'general_options' |
65
|
|
|
), |
66
|
|
|
|
67
|
|
|
array( |
68
|
|
|
'title' => __( 'API', 'woocommerce' ), |
69
|
|
|
'desc' => __( 'Enable the REST API', 'woocommerce' ), |
70
|
|
|
'id' => 'woocommerce_api_enabled', |
71
|
|
|
'type' => 'checkbox', |
72
|
|
|
'default' => 'yes', |
73
|
|
|
), |
74
|
|
|
|
75
|
|
|
array( |
76
|
|
|
'type' => 'sectionend', |
77
|
|
|
'id' => 'general_options' |
78
|
|
|
), |
79
|
|
|
) ); |
80
|
|
|
|
81
|
|
|
return apply_filters( 'woocommerce_get_settings_' . $this->id, $settings ); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Form method. |
86
|
|
|
* |
87
|
|
|
* @param string $method |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function form_method( $method ) { |
|
|
|
|
92
|
|
|
global $current_section; |
93
|
|
|
|
94
|
|
View Code Duplication |
if ( 'webhooks' == $current_section ) { |
|
|
|
|
95
|
|
|
if ( isset( $_GET['edit-webhook'] ) ) { |
96
|
|
|
$webhook_id = absint( $_GET['edit-webhook'] ); |
97
|
|
|
$webhook = new WC_Webhook( $webhook_id ); |
98
|
|
|
|
99
|
|
|
if ( 'trash' != $webhook->post_data->post_status ) { |
100
|
|
|
return 'post'; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return 'get'; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if ( 'keys' == $current_section ) { |
108
|
|
|
if ( isset( $_GET['create-key'] ) || isset( $_GET['edit-key'] ) ) { |
109
|
|
|
return 'post'; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return 'get'; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return 'post'; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Notices. |
120
|
|
|
*/ |
121
|
|
|
private function notices() { |
122
|
|
|
if ( isset( $_GET['section'] ) && 'webhooks' == $_GET['section'] ) { |
123
|
|
|
WC_Admin_Webhooks::notices(); |
124
|
|
|
} |
125
|
|
|
if ( isset( $_GET['section'] ) && 'keys' == $_GET['section'] ) { |
126
|
|
|
WC_Admin_API_Keys::notices(); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Output the settings. |
132
|
|
|
*/ |
133
|
|
|
public function output() { |
134
|
|
|
global $current_section; |
135
|
|
|
|
136
|
|
|
if ( 'webhooks' == $current_section ) { |
137
|
|
|
WC_Admin_Webhooks::page_output(); |
138
|
|
|
} else if ( 'keys' == $current_section ) { |
139
|
|
|
WC_Admin_API_Keys::page_output(); |
140
|
|
|
} else { |
141
|
|
|
$settings = $this->get_settings( $current_section ); |
|
|
|
|
142
|
|
|
WC_Admin_Settings::output_fields( $settings ); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Save settings. |
148
|
|
|
*/ |
149
|
|
|
public function save() { |
150
|
|
|
global $current_section; |
151
|
|
|
|
152
|
|
|
if ( apply_filters( 'woocommerce_rest_api_valid_to_save', ! in_array( $current_section, array( 'keys', 'webhooks' ) ) ) ) { |
153
|
|
|
$settings = $this->get_settings(); |
154
|
|
|
WC_Admin_Settings::save_fields( $settings ); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
endif; |
160
|
|
|
|
161
|
|
|
return new WC_Settings_Rest_API(); |
162
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.