PluginProbe ʕ •ᴥ•ʔ
WooCommerce Square / 4.8.0
WooCommerce Square v4.8.0
5.5.0 5.4.3 5.4.2 5.4.1 5.4.0 trunk 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.7.0 2.8.0 2.9.0 2.9.1 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.2.0 3.3.0 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.7.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 4.0.0 4.1.0 4.2.0 4.2.1 4.2.2 4.2.3 4.3.0 4.3.1 4.3.2 4.4.0 4.4.1 4.4.2 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5 4.8.6 4.8.7 4.8.8 4.9.0 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.6 4.9.7 4.9.8 4.9.9 5.0.0 5.0.1 5.1.0 5.1.1 5.1.2 5.2.0 5.3.0 5.3.1 5.3.2 5.3.3
woocommerce-square / includes / Admin / Rest / WC_REST_Square_Settings_Controller.php
woocommerce-square / includes / Admin / Rest Last commit date
WC_REST_Square_Cash_App_Settings_Controller.php 2 years ago WC_REST_Square_Credit_Card_Payment_Settings_Controller.php 2 years ago WC_REST_Square_Gift_Cards_Settings_Controller.php 2 years ago WC_REST_Square_Settings_Controller.php 2 years ago WC_Square_REST_Base_Controller.php 2 years ago
WC_REST_Square_Settings_Controller.php
231 lines
1 <?php
2 /**
3 * Class WC_REST_Square_Settings_Controller file.
4 */
5
6 namespace WooCommerce\Square\Admin\Rest;
7
8 use WP_REST_Server;
9 use WP_REST_Request;
10 use WP_REST_Response;
11
12 defined( 'ABSPATH' ) || exit;
13
14 /**
15 * Class WC_REST_Square_Settings_Controller.
16 *
17 * @since 4.7.0
18 */
19 class WC_REST_Square_Settings_Controller extends WC_Square_REST_Base_Controller {
20
21 /**
22 * Square settings option name.
23 *
24 * @var string
25 */
26 const SQUARE_GATEWAY_SETTINGS_OPTION_NAME = 'wc_square_settings';
27
28 /**
29 * Endpoint path.
30 *
31 * @var string
32 */
33 protected $rest_base = 'wc_square/settings';
34
35 /**
36 * Allowed parameters.
37 *
38 * @var array
39 */
40 private $allowed_params;
41
42 /**
43 * Constructor.
44 */
45 public function __construct() {
46 $this->allowed_params = array(
47 'enable_sandbox',
48 'sandbox_application_id',
49 'sandbox_token',
50 'sandbox_location_id',
51 'production_location_id',
52 'system_of_record',
53 'enable_inventory_sync',
54 'override_product_images',
55 'hide_missing_products',
56 'sync_interval',
57 'is_connected',
58 'locations',
59 'enable_customer_decline_messages',
60 'debug_mode',
61 'debug_logging_enabled',
62 );
63
64 add_action( 'rest_api_init', array( $this, 'register_routes' ) );
65 }
66
67 /**
68 * Register routes.
69 */
70 public function register_routes() {
71 register_rest_route(
72 $this->namespace,
73 '/' . $this->rest_base,
74 array(
75 'methods' => WP_REST_Server::READABLE,
76 'callback' => array( $this, 'get_settings' ),
77 'permission_callback' => array( $this, 'check_permission' ),
78 )
79 );
80 register_rest_route(
81 $this->namespace,
82 '/' . $this->rest_base,
83 array(
84 'methods' => WP_REST_Server::EDITABLE,
85 'callback' => array( $this, 'save_settings' ),
86 'permission_callback' => array( $this, 'check_permission' ),
87 'args' => array(
88 'enable_sandbox' => array(
89 'description' => __( 'Application ID for the Sandbox Application.', 'woocommerce-square' ),
90 'type' => 'string',
91 'sanitize_callback' => 'sanitize_text_field',
92 ),
93 'sandbox_application_id' => array(
94 'description' => __( 'Access Token for the Sandbox Test Account.', 'woocommerce-square' ),
95 'type' => 'string',
96 'sanitize_callback' => 'sanitize_text_field',
97 ),
98 'sandbox_token' => array(
99 'description' => __( 'Square sandbox ID.', 'woocommerce-square' ),
100 'type' => 'string',
101 'sanitize_callback' => 'sanitize_text_field',
102 ),
103 'debug_logging_enabled' => array(
104 'description' => __( 'Log debug messages to the WooCommerce status log.', 'woocommerce-square' ),
105 'type' => 'string',
106 'sanitize_callback' => 'sanitize_text_field',
107 ),
108 'sandbox_location_id' => array(
109 'description' => __( 'Square location ID. (Sandbox)', 'woocommerce-square' ),
110 'type' => 'string',
111 'sanitize_callback' => 'sanitize_text_field',
112 ),
113 'production_location_id' => array(
114 'description' => __( 'Square location ID.', 'woocommerce-square' ),
115 'type' => 'string',
116 'sanitize_callback' => 'sanitize_text_field',
117 ),
118 'system_of_record' => array(
119 'description' => __( 'Choose where data will be updated for synced products.', 'woocommerce-square' ),
120 'type' => 'string',
121 'sanitize_callback' => 'sanitize_text_field',
122 ),
123 'enable_inventory_sync' => array(
124 'description' => __( 'Enable to fetch inventory changes from Square.', 'woocommerce-square' ),
125 'type' => 'string',
126 'sanitize_callback' => 'sanitize_text_field',
127 ),
128 'override_product_images' => array(
129 'description' => __( 'Enable to override Product images from Square.', 'woocommerce-square' ),
130 'type' => 'string',
131 'sanitize_callback' => 'sanitize_text_field',
132 ),
133 'hide_missing_products' => array(
134 'description' => __( 'Hide synced products when not found in Square.', 'woocommerce-square' ),
135 'type' => 'string',
136 'sanitize_callback' => 'sanitize_text_field',
137 ),
138 'sync_interval' => array(
139 'description' => __( 'Frequency for how regularly WooCommerce will sync products with Square.', 'woocommerce-square' ),
140 'type' => 'string',
141 'sanitize_callback' => 'sanitize_text_field',
142 ),
143 'debug_mode' => array(
144 'description' => __( 'Type of debug mode.', 'woocommerce-square' ),
145 'type' => 'string',
146 'sanitize_callback' => 'sanitize_text_field',
147 ),
148 'enable_customer_decline_messages' => array(
149 'description' => __( 'Enable detailed decline messages to the customer during checkout when possible, rather than a generic decline message.', 'woocommerce-square' ),
150 'type' => 'string',
151 'sanitize_callback' => '',
152 ),
153 ),
154 )
155 );
156 }
157
158 /**
159 * Get the data.
160 *
161 * @return WP_REST_Response
162 */
163 public function get_settings() {
164 // Need to reload the new settings as the settings are cached
165 // and won't refresh until the next page load.
166 wc_square()->get_settings_handler()->init_settings();
167
168 $square_settings = get_option( self::SQUARE_GATEWAY_SETTINGS_OPTION_NAME, array() );
169 $filtered_settings = array_intersect_key( $square_settings, array_flip( $this->allowed_params ) );
170
171 // Generate disconnection URL.
172 $action = 'wc_square_disconnect';
173 $url = add_query_arg( 'action', $action, admin_url() );
174
175 // Add the connection parameters to the response.
176 $filtered_settings['is_connected'] = wc_square()->get_gateway()->get_plugin()->get_settings_handler()->is_connected();
177 $filtered_settings['connection_url'] = wc_square()->get_gateway()->get_plugin()->get_connection_handler()->get_connect_url( false );
178 $filtered_settings['connection_url_wizard'] = wc_square()->get_gateway()->get_plugin()->get_connection_handler()->get_connect_url( false, array( 'from' => 'wizard' ) );
179 $filtered_settings['connection_url_sandbox'] = wc_square()->get_gateway()->get_plugin()->get_connection_handler()->get_connect_url( true, array( 'from' => 'wizard' ) );
180 $filtered_settings['disconnection_url'] = html_entity_decode( wp_nonce_url( $url, $action ) );
181
182 // Add locations to the response.
183 if ( wc_square()->get_settings_handler()->is_connected() ) {
184 $filtered_settings['locations'] = wc_square()->get_settings_handler()->get_locations();
185 }
186
187 return new WP_REST_Response( $filtered_settings );
188 }
189
190 /**
191 * Update the data.
192 *
193 * @param WP_REST_Request $request Full data about the request.
194 */
195 public function save_settings( WP_REST_Request $request ) {
196 $settings = array();
197 $keys_to_skip = array(
198 'is_connected',
199 'locations',
200 'connection_url',
201 'connection_url_wizard',
202 'connection_url_sandbox',
203 'disconnection_url',
204 );
205
206 foreach ( $this->allowed_params as $index => $key ) {
207 if ( in_array( $key, $keys_to_skip, true ) ) {
208 continue;
209 }
210
211 $new_value = wc_clean( wp_unslash( $request->get_param( $key ) ) );
212 $settings[ $key ] = $new_value;
213 }
214
215 $is_sandbox = wc_clean( wp_unslash( $settings['enable_sandbox'] ) ?? '' );
216 $sandbox_token = wc_clean( wp_unslash( $settings['sandbox_token'] ) ?? '' );
217
218 update_option( self::SQUARE_GATEWAY_SETTINGS_OPTION_NAME, $settings );
219
220 // Need to reload the new settings as the settings are cached
221 // and won't refresh until the next page load.
222 wc_square()->get_settings_handler()->init_settings();
223
224 if ( 'yes' === $is_sandbox && ! empty( $sandbox_token ) ) {
225 wc_square()->get_settings_handler()->update_access_token( $sandbox_token );
226 }
227
228 wp_send_json_success();
229 }
230 }
231