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
1 year ago
WC_REST_Square_Settings_Controller.php
4 months ago
WC_Square_REST_Base_Controller.php
2 years ago
WC_REST_Square_Settings_Controller.php
245 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 | 'enable_square_discount_codes', |
| 61 | 'debug_mode', |
| 62 | 'debug_logging_enabled', |
| 63 | 'enable_order_fulfillment_sync', |
| 64 | ); |
| 65 | |
| 66 | add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Register routes. |
| 71 | */ |
| 72 | public function register_routes() { |
| 73 | register_rest_route( |
| 74 | $this->namespace, |
| 75 | '/' . $this->rest_base, |
| 76 | array( |
| 77 | 'methods' => WP_REST_Server::READABLE, |
| 78 | 'callback' => array( $this, 'get_settings' ), |
| 79 | 'permission_callback' => array( $this, 'check_permission' ), |
| 80 | ) |
| 81 | ); |
| 82 | register_rest_route( |
| 83 | $this->namespace, |
| 84 | '/' . $this->rest_base, |
| 85 | array( |
| 86 | 'methods' => WP_REST_Server::EDITABLE, |
| 87 | 'callback' => array( $this, 'save_settings' ), |
| 88 | 'permission_callback' => array( $this, 'check_permission' ), |
| 89 | 'args' => array( |
| 90 | 'enable_sandbox' => array( |
| 91 | 'description' => __( 'Application ID for the Sandbox Application.', 'woocommerce-square' ), |
| 92 | 'type' => 'string', |
| 93 | 'sanitize_callback' => 'sanitize_text_field', |
| 94 | ), |
| 95 | 'sandbox_application_id' => array( |
| 96 | 'description' => __( 'Access Token for the Sandbox Test Account.', 'woocommerce-square' ), |
| 97 | 'type' => 'string', |
| 98 | 'sanitize_callback' => 'sanitize_text_field', |
| 99 | ), |
| 100 | 'sandbox_token' => array( |
| 101 | 'description' => __( 'Square sandbox ID.', 'woocommerce-square' ), |
| 102 | 'type' => 'string', |
| 103 | 'sanitize_callback' => 'sanitize_text_field', |
| 104 | ), |
| 105 | 'sandbox_location_id' => array( |
| 106 | 'description' => __( 'Square location ID. (Sandbox)', 'woocommerce-square' ), |
| 107 | 'type' => 'string', |
| 108 | 'sanitize_callback' => 'sanitize_text_field', |
| 109 | ), |
| 110 | 'production_location_id' => array( |
| 111 | 'description' => __( 'Square location ID.', 'woocommerce-square' ), |
| 112 | 'type' => 'string', |
| 113 | 'sanitize_callback' => 'sanitize_text_field', |
| 114 | ), |
| 115 | 'system_of_record' => array( |
| 116 | 'description' => __( 'Choose the origin for updates to synced products.', 'woocommerce-square' ), |
| 117 | 'type' => 'string', |
| 118 | 'sanitize_callback' => 'sanitize_text_field', |
| 119 | ), |
| 120 | 'enable_inventory_sync' => array( |
| 121 | 'description' => __( 'Enable to fetch inventory changes from Square.', 'woocommerce-square' ), |
| 122 | 'type' => 'string', |
| 123 | 'sanitize_callback' => 'sanitize_text_field', |
| 124 | ), |
| 125 | 'override_product_images' => array( |
| 126 | 'description' => __( 'Enable to override Product images from Square.', 'woocommerce-square' ), |
| 127 | 'type' => 'string', |
| 128 | 'sanitize_callback' => 'sanitize_text_field', |
| 129 | ), |
| 130 | 'hide_missing_products' => array( |
| 131 | 'description' => __( 'Hide synced products when not found in Square.', 'woocommerce-square' ), |
| 132 | 'type' => 'string', |
| 133 | 'sanitize_callback' => 'sanitize_text_field', |
| 134 | ), |
| 135 | 'sync_interval' => array( |
| 136 | 'description' => __( 'Frequency for how regularly WooCommerce will sync products with Square.', 'woocommerce-square' ), |
| 137 | 'type' => 'string', |
| 138 | 'sanitize_callback' => 'sanitize_text_field', |
| 139 | ), |
| 140 | 'debug_mode' => array( |
| 141 | 'description' => __( 'Type of debug mode.', 'woocommerce-square' ), |
| 142 | 'type' => 'string', |
| 143 | 'sanitize_callback' => 'sanitize_text_field', |
| 144 | ), |
| 145 | 'enable_customer_decline_messages' => array( |
| 146 | 'description' => __( 'Enable detailed decline messages to the customer during checkout when possible, rather than a generic decline message.', 'woocommerce-square' ), |
| 147 | 'type' => 'string', |
| 148 | 'sanitize_callback' => '', |
| 149 | ), |
| 150 | 'enable_square_discount_codes' => array( |
| 151 | 'description' => __( 'Enable Square discount codes. When disabled, only WooCommerce coupons are processed.', 'woocommerce-square' ), |
| 152 | 'type' => 'string', |
| 153 | 'sanitize_callback' => 'sanitize_text_field', |
| 154 | ), |
| 155 | 'enable_order_fulfillment_sync' => array( |
| 156 | 'description' => __( 'Enable bidirectional fulfillment synchronization between WooCommerce and Square orders.', 'woocommerce-square' ), |
| 157 | 'type' => 'string', |
| 158 | 'sanitize_callback' => 'sanitize_text_field', |
| 159 | ), |
| 160 | ), |
| 161 | ) |
| 162 | ); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Get the data. |
| 167 | * |
| 168 | * @return WP_REST_Response |
| 169 | */ |
| 170 | public function get_settings() { |
| 171 | // Need to reload the new settings as the settings are cached |
| 172 | // and won't refresh until the next page load. |
| 173 | wc_square()->get_settings_handler()->init_settings(); |
| 174 | |
| 175 | $square_settings = get_option( self::SQUARE_GATEWAY_SETTINGS_OPTION_NAME, array() ); |
| 176 | $filtered_settings = array_intersect_key( $square_settings, array_flip( $this->allowed_params ) ); |
| 177 | |
| 178 | // Default Discount Codes setting to enabled for existing installs that don't have the key yet. |
| 179 | if ( ! array_key_exists( 'enable_square_discount_codes', $filtered_settings ) ) { |
| 180 | $filtered_settings['enable_square_discount_codes'] = 'yes'; |
| 181 | } |
| 182 | |
| 183 | // Generate disconnection URL. |
| 184 | $action = 'wc_square_disconnect'; |
| 185 | $url = add_query_arg( 'action', $action, admin_url() ); |
| 186 | |
| 187 | // Add the connection parameters to the response. |
| 188 | $filtered_settings['is_connected'] = wc_square()->get_gateway()->get_plugin()->get_settings_handler()->is_connected(); |
| 189 | $filtered_settings['access_tokens'] = wc_square()->get_gateway()->get_plugin()->get_settings_handler()->get_access_tokens(); |
| 190 | $filtered_settings['connection_url'] = wc_square()->get_gateway()->get_plugin()->get_connection_handler()->get_connect_url( false ); |
| 191 | $filtered_settings['connection_url_wizard'] = wc_square()->get_gateway()->get_plugin()->get_connection_handler()->get_connect_url( false, array( 'from' => 'wizard' ) ); |
| 192 | $filtered_settings['connection_url_sandbox'] = wc_square()->get_gateway()->get_plugin()->get_connection_handler()->get_connect_url( true, array( 'from' => 'wizard' ) ); |
| 193 | $filtered_settings['disconnection_url'] = html_entity_decode( wp_nonce_url( $url, $action ), ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ); |
| 194 | |
| 195 | // Add locations to the response. |
| 196 | if ( wc_square()->get_settings_handler()->is_connected() ) { |
| 197 | $filtered_settings['locations'] = wc_square()->get_settings_handler()->get_locations(); |
| 198 | } |
| 199 | |
| 200 | return new WP_REST_Response( $filtered_settings ); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Update the data. |
| 205 | * |
| 206 | * @param WP_REST_Request $request Full data about the request. |
| 207 | */ |
| 208 | public function save_settings( WP_REST_Request $request ) { |
| 209 | $settings = array(); |
| 210 | $keys_to_skip = array( |
| 211 | 'is_connected', |
| 212 | 'access_tokens', |
| 213 | 'locations', |
| 214 | 'connection_url', |
| 215 | 'connection_url_wizard', |
| 216 | 'connection_url_sandbox', |
| 217 | 'disconnection_url', |
| 218 | ); |
| 219 | |
| 220 | foreach ( $this->allowed_params as $index => $key ) { |
| 221 | if ( in_array( $key, $keys_to_skip, true ) ) { |
| 222 | continue; |
| 223 | } |
| 224 | |
| 225 | $new_value = wc_clean( wp_unslash( $request->get_param( $key ) ) ); |
| 226 | $settings[ $key ] = $new_value; |
| 227 | } |
| 228 | |
| 229 | $is_sandbox = wc_clean( wp_unslash( $settings['enable_sandbox'] ) ?? '' ); |
| 230 | $sandbox_token = wc_clean( wp_unslash( $settings['sandbox_token'] ) ?? '' ); |
| 231 | |
| 232 | update_option( self::SQUARE_GATEWAY_SETTINGS_OPTION_NAME, $settings ); |
| 233 | |
| 234 | // Need to reload the new settings as the settings are cached |
| 235 | // and won't refresh until the next page load. |
| 236 | wc_square()->get_settings_handler()->init_settings(); |
| 237 | |
| 238 | if ( 'yes' === $is_sandbox && ! empty( $sandbox_token ) ) { |
| 239 | wc_square()->get_settings_handler()->update_access_token( $sandbox_token ); |
| 240 | } |
| 241 | |
| 242 | wp_send_json_success(); |
| 243 | } |
| 244 | } |
| 245 |