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 |