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_Cash_App_Settings_Controller.php
156 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class WC_REST_Square_Cash_App_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_Cash_App_Settings_Controller. |
| 16 | * |
| 17 | * @since 4.7.0 |
| 18 | */ |
| 19 | class WC_REST_Square_Cash_App_Settings_Controller extends WC_Square_REST_Base_Controller { |
| 20 | |
| 21 | /** |
| 22 | * Square settings option name. |
| 23 | * |
| 24 | * @var string |
| 25 | */ |
| 26 | const SQUARE_CASH_APP_SETTINGS_OPTION_NAME = 'woocommerce_square_cash_app_pay_settings'; |
| 27 | |
| 28 | /** |
| 29 | * Endpoint path. |
| 30 | * |
| 31 | * @var string |
| 32 | */ |
| 33 | protected $rest_base = 'wc_square/cash_app_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 | 'enabled', |
| 48 | 'title', |
| 49 | 'description', |
| 50 | 'transaction_type', |
| 51 | 'button_theme', |
| 52 | 'charge_virtual_orders', |
| 53 | 'enable_paid_capture', |
| 54 | 'button_shape', |
| 55 | ); |
| 56 | |
| 57 | add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Register routes. |
| 62 | */ |
| 63 | public function register_routes() { |
| 64 | register_rest_route( |
| 65 | $this->namespace, |
| 66 | '/' . $this->rest_base, |
| 67 | array( |
| 68 | 'methods' => WP_REST_Server::READABLE, |
| 69 | 'callback' => array( $this, 'get_settings' ), |
| 70 | 'permission_callback' => array( $this, 'check_permission' ), |
| 71 | ) |
| 72 | ); |
| 73 | register_rest_route( |
| 74 | $this->namespace, |
| 75 | '/' . $this->rest_base, |
| 76 | array( |
| 77 | 'methods' => WP_REST_Server::EDITABLE, |
| 78 | 'callback' => array( $this, 'save_settings' ), |
| 79 | 'permission_callback' => array( $this, 'check_permission' ), |
| 80 | 'args' => array( |
| 81 | 'enabled' => array( |
| 82 | 'description' => __( 'Enable Square payment gateway.', 'woocommerce-square' ), |
| 83 | 'type' => 'string', |
| 84 | 'sanitize_callback' => '', |
| 85 | ), |
| 86 | 'title' => array( |
| 87 | 'description' => __( 'Square payment gateway title.', 'woocommerce-square' ), |
| 88 | 'type' => 'string', |
| 89 | 'sanitize_callback' => '', |
| 90 | ), |
| 91 | 'description' => array( |
| 92 | 'description' => __( 'Square payment gateway description.', 'woocommerce-square' ), |
| 93 | 'type' => 'string', |
| 94 | 'sanitize_callback' => '', |
| 95 | ), |
| 96 | 'transaction_type' => array( |
| 97 | 'description' => __( 'The transaction type.', 'woocommerce-square' ), |
| 98 | 'type' => 'string', |
| 99 | 'sanitize_callback' => '', |
| 100 | ), |
| 101 | 'charge_virtual_orders' => array( |
| 102 | 'description' => __( 'If the order contains exclusively virtual items, enable this to immediately charge, rather than authorize, the transaction.', 'woocommerce-square' ), |
| 103 | 'type' => 'string', |
| 104 | 'sanitize_callback' => '', |
| 105 | ), |
| 106 | 'enable_paid_capture' => array( |
| 107 | 'description' => __( 'Automatically capture orders when they are changed to Processing or Completed.', 'woocommerce-square' ), |
| 108 | 'type' => 'string', |
| 109 | 'sanitize_callback' => '', |
| 110 | ), |
| 111 | 'button_theme' => array( |
| 112 | 'description' => __( 'Button Theme.', 'woocommerce-square' ), |
| 113 | 'type' => 'string', |
| 114 | 'sanitize_callback' => '', |
| 115 | ), |
| 116 | 'button_shape' => array( |
| 117 | 'description' => __( 'Button Shape.', 'woocommerce-square' ), |
| 118 | 'type' => 'string', |
| 119 | 'sanitize_callback' => '', |
| 120 | ), |
| 121 | ), |
| 122 | ) |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Get the data. |
| 128 | * |
| 129 | * @return WP_REST_Response |
| 130 | */ |
| 131 | public function get_settings() { |
| 132 | $square_settings = get_option( self::SQUARE_CASH_APP_SETTINGS_OPTION_NAME, array() ); |
| 133 | $filtered_settings = array_intersect_key( $square_settings, array_flip( $this->allowed_params ) ); |
| 134 | |
| 135 | return new WP_REST_Response( $filtered_settings ); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Update the data. |
| 140 | * |
| 141 | * @param WP_REST_Request $request Full data about the request. |
| 142 | */ |
| 143 | public function save_settings( WP_REST_Request $request ) { |
| 144 | $settings = array(); |
| 145 | |
| 146 | foreach ( $this->allowed_params as $index => $key ) { |
| 147 | $new_value = wc_clean( wp_unslash( $request->get_param( $key ) ) ); |
| 148 | |
| 149 | $settings[ $key ] = $new_value; |
| 150 | } |
| 151 | |
| 152 | update_option( self::SQUARE_CASH_APP_SETTINGS_OPTION_NAME, $settings ); |
| 153 | wp_send_json_success(); |
| 154 | } |
| 155 | } |
| 156 |