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_Gift_Cards_Settings_Controller.php
140 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class WC_REST_Square_Gift_Cards_Settings_Controller file. |
| 4 | */ |
| 5 | |
| 6 | namespace WooCommerce\Square\Admin\Rest; |
| 7 | |
| 8 | use WooCommerce\Square\Gateway\Gift_Card; |
| 9 | use WP_REST_Server; |
| 10 | use WP_REST_Request; |
| 11 | use WP_REST_Response; |
| 12 | |
| 13 | defined( 'ABSPATH' ) || exit; |
| 14 | |
| 15 | /** |
| 16 | * Class WC_REST_Square_Gift_Cards_Settings_Controller. |
| 17 | * |
| 18 | * @since 4.7.0 |
| 19 | */ |
| 20 | class WC_REST_Square_Gift_Cards_Settings_Controller extends WC_Square_REST_Base_Controller { |
| 21 | |
| 22 | /** |
| 23 | * Endpoint path. |
| 24 | * |
| 25 | * @var string |
| 26 | */ |
| 27 | protected $rest_base = 'wc_square/gift_cards_settings'; |
| 28 | |
| 29 | /** |
| 30 | * Allowed parameters. |
| 31 | * |
| 32 | * @var array |
| 33 | */ |
| 34 | private $allowed_params; |
| 35 | |
| 36 | /** |
| 37 | * Constructor. |
| 38 | */ |
| 39 | public function __construct() { |
| 40 | $this->allowed_params = array( |
| 41 | 'enabled', |
| 42 | 'title', |
| 43 | 'description', |
| 44 | 'is_default_placeholder', |
| 45 | 'placeholder_id', |
| 46 | ); |
| 47 | |
| 48 | add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Register routes. |
| 53 | */ |
| 54 | public function register_routes() { |
| 55 | register_rest_route( |
| 56 | $this->namespace, |
| 57 | '/' . $this->rest_base, |
| 58 | array( |
| 59 | 'methods' => WP_REST_Server::READABLE, |
| 60 | 'callback' => array( $this, 'get_settings' ), |
| 61 | 'permission_callback' => array( $this, 'check_permission' ), |
| 62 | ) |
| 63 | ); |
| 64 | register_rest_route( |
| 65 | $this->namespace, |
| 66 | '/' . $this->rest_base, |
| 67 | array( |
| 68 | 'methods' => WP_REST_Server::EDITABLE, |
| 69 | 'callback' => array( $this, 'save_settings' ), |
| 70 | 'permission_callback' => array( $this, 'check_permission' ), |
| 71 | 'args' => array( |
| 72 | 'enabled' => array( |
| 73 | 'description' => __( 'Enable Square payment gateway.', 'woocommerce-square' ), |
| 74 | 'type' => 'string', |
| 75 | 'sanitize_callback' => '', |
| 76 | ), |
| 77 | 'title' => array( |
| 78 | 'description' => __( 'Square payment gateway title.', 'woocommerce-square' ), |
| 79 | 'type' => 'string', |
| 80 | 'sanitize_callback' => '', |
| 81 | ), |
| 82 | 'description' => array( |
| 83 | 'description' => __( 'Square payment gateway description.', 'woocommerce-square' ), |
| 84 | 'type' => 'string', |
| 85 | 'sanitize_callback' => '', |
| 86 | ), |
| 87 | 'is_default_placeholder' => array( |
| 88 | 'description' => __( 'Indicates if a Gift card product should use the default placeholder provided by the plugin.', 'woocommerce-square' ), |
| 89 | 'type' => 'string', |
| 90 | 'sanitize_callback' => '', |
| 91 | ), |
| 92 | 'placeholder_id' => array( |
| 93 | 'description' => __( 'ID of the placeholder media.', 'woocommerce-square' ), |
| 94 | 'type' => 'integer', |
| 95 | 'sanitize_callback' => '', |
| 96 | ), |
| 97 | ), |
| 98 | ) |
| 99 | ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Get the data. |
| 104 | * |
| 105 | * @return WP_REST_Response |
| 106 | */ |
| 107 | public function get_settings() { |
| 108 | $square_settings = get_option( Gift_Card::SQUARE_PAYMENT_SETTINGS_OPTION_NAME, array() ); |
| 109 | $filtered_settings = array_intersect_key( $square_settings, array_flip( $this->allowed_params ) ); |
| 110 | |
| 111 | return new WP_REST_Response( $filtered_settings ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Update the data. |
| 116 | * |
| 117 | * @param WP_REST_Request $request Full data about the request. |
| 118 | */ |
| 119 | public function save_settings( WP_REST_Request $request ) { |
| 120 | $settings = array(); |
| 121 | |
| 122 | foreach ( $this->allowed_params as $index => $key ) { |
| 123 | $new_value = wc_clean( wp_unslash( $request->get_param( $key ) ) ); |
| 124 | |
| 125 | $settings[ $key ] = $new_value; |
| 126 | } |
| 127 | |
| 128 | update_option( Gift_Card::SQUARE_PAYMENT_SETTINGS_OPTION_NAME, $settings ); |
| 129 | |
| 130 | /** |
| 131 | * Action triggered when the Gift card payment settings are updated. |
| 132 | * |
| 133 | * @since 4.8.1 |
| 134 | */ |
| 135 | do_action( 'wc_square_' . Gift_Card::SQUARE_PAYMENT_SETTINGS_OPTION_NAME . '_settings_updated', $settings ); |
| 136 | |
| 137 | wp_send_json_success(); |
| 138 | } |
| 139 | } |
| 140 |