PluginProbe
Booktics – Appointment Booking Calendar for Service Businesses / 1.0.24
Booktics – Appointment Booking Calendar for Service Businesses v1.0.24
1.0.29 1.0.28 1.0.27 1.0.26 1.0.25 1.0.24 1.0.23 1.0.22 1.0.21 1.0.20 1.0.19 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.2 1.0.3 All 31 releases
booktics / core / settings / controllers / settings-controller.php

settings-controller.php in Booktics – Appointment Booking Calendar for Service Businesses 1.0.24, at core/settings/controllers/settings-controller.php

254 lines 8.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Booktics\Settings\Controllers;
4
5 use Booktics\Abstracts\Base_Rest_Controller;
6 use Booktics\Services\Schedule_Manager;
7 use Booktics\Settings\Settings;
8 use WP_Error;
9 use WP_HTTP_Response;
10 use WP_REST_Request;
11 use WP_REST_Server;
12
13 if ( ! function_exists( 'is_plugin_active' ) ) {
14 require_once ABSPATH . 'wp-admin/includes/plugin.php';
15 }
16
17 /**
18 * Settings controller
19 *
20 * @package Booktics/Appointment
21 */
22 class Settings_Controller extends Base_Rest_Controller {
23 /**
24 * Endpoint namespace
25 *
26 * @var string
27 */
28 protected $namespace = 'booktics/v1';
29
30 /**
31 * Route name
32 *
33 * @var string
34 */
35 protected $rest_base = 'settings';
36
37 /**
38 * Register routes
39 *
40 * @return void
41 */
42 public function register_routes(): void {
43 register_rest_route(
44 $this->namespace,
45 '/' . $this->rest_base,
46 array(
47 array(
48 'methods' => WP_REST_Server::READABLE,
49 'callback' => array( $this, 'get_item' ),
50 'args' => array(),
51 'permission_callback' => array( $this, 'get_item_permissions_check' ),
52 ),
53 array(
54 'methods' => WP_REST_Server::EDITABLE,
55 'callback' => array( $this, 'update_item' ),
56 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
57 'permission_callback' => array(
58 $this,
59 'get_item_permissions_check',
60 ),
61 ),
62 'schema' => array( $this, 'get_public_item_schema' ),
63 )
64 );
65
66 register_rest_route(
67 $this->namespace,
68 '/' . $this->rest_base . '/public',
69 array(
70 array(
71 'methods' => WP_REST_Server::READABLE,
72 'callback' => array( $this, 'get_public_item' ),
73 'args' => array(),
74 'permission_callback' => array(
75 $this,
76 'get_public_item_permissions_check',
77 ),
78 ),
79 )
80 );
81
82 register_rest_route(
83 $this->namespace,
84 '/' . $this->rest_base . '/stripe/disconnect',
85 array(
86 array(
87 'methods' => WP_REST_Server::EDITABLE,
88 'callback' => array(
89 $this,
90 'stripe_disconnect',
91 ),
92 'permission_callback' => array(
93 $this,
94 'stripe_disconnect_permissions_check',
95 ),
96 ),
97 )
98 );
99 }
100
101 /**
102 * Check if a given request has access to get items.
103 *
104 * @param WP_REST_Request $request Full data about the request.
105 *
106 * @return WP_Error
107 */
108 public function get_item_permissions_check( $request ): bool {
109 return current_user_can( 'manage_options' );
110 }
111
112 /**
113 * Format settings for API response.
114 *
115 * @param array $settings Settings array.
116 *
117 * @return array
118 */
119 private function format_settings_for_response( array $settings ): array {
120 $settings['reschedule_restriction'] = empty( $settings['reschedule_restriction'] ) ? (object) array() : $settings['reschedule_restriction'];
121 $settings['custom_reschedule_status'] = empty( $settings['custom_reschedule_status'] ) ? (object) array() : $settings['custom_reschedule_status'];
122 $settings['cancellation_restriction'] = empty( $settings['cancellation_restriction'] ) ? (object) array() : $settings['cancellation_restriction'];
123 $settings['stripe_addon_install_status'] = 0;
124 $settings['woocommerce_install_status'] = 0;
125
126 $settings = apply_filters( 'booktics_format_setting_response', $settings );
127
128 return $settings;
129 }
130
131 /**
132 * Get a collection of items.
133 *
134 * @param WP_REST_Request $request Full data about the request.
135 *
136 * @return WP_HTTP_Response
137 */
138 public function get_item( $request ) {
139 $settings = Settings::get();
140 $settings = apply_filters( 'booktics_rest_settings', $settings );
141 $settings = $this->format_settings_for_response( $settings );
142
143 return $this->response( $settings, __( 'Settings fetched successfully.', 'booktics' ) );
144 }
145
146 /**
147 * Update plugin settings.
148 *
149 * @param WP_Rest_Request $request Request object.
150 *
151 * @return WP_HTTP_Response
152 */
153 public function update_item( $request ) {
154 $params = $request->get_params();
155
156 if ( isset( $params['schedule'] ) || isset( $params['custom'] ) || isset( $params['holiday'] ) ) {
157 $input_data = json_decode( $request->get_body(), true );
158 $schedule_manager = Schedule_Manager::make( $input_data );
159 $schedule_manager->save();
160 unset( $params['schedule'], $params['custom'], $params['holiday'] );
161 }
162
163 // Enforce an integer minute value for the reservation hold timeout.
164 if ( isset( $params['pending_reservation_timeout'] ) ) {
165 $minutes = absint( $params['pending_reservation_timeout'] );
166 $params['pending_reservation_timeout'] = $minutes > 0 ? $minutes : 10;
167 }
168
169 // Only the two known destinations are accepted for the post-checkout page.
170 if ( isset( $params['wc_thankyou_page'] ) ) {
171 $params['wc_thankyou_page'] = in_array( $params['wc_thankyou_page'], array( 'woocommerce', 'booktics' ), true )
172 ? $params['wc_thankyou_page']
173 : 'woocommerce';
174 }
175
176 Settings::update( $params );
177
178 $settings = Settings::get();
179 $settings = apply_filters( 'booktics_rest_settings', $settings );
180 $settings = $this->format_settings_for_response( $settings );
181
182 return $this->response( $settings, __( 'Settings updated successfully.', 'booktics' ) );
183 }
184
185 /**
186 * Get public settings
187 */
188 public function get_public_item( $request ) {
189 $all = Settings::get();
190 $allowed = [
191 'primary_color',
192 'secondary_color',
193 'currency',
194 'currency_symbol_position',
195 'currency_decimals',
196 'currency_price_separator',
197 'enable_stripe_payment',
198 'enable_paypal_payment',
199 'enable_local_payment',
200 'enable_woocommerce_payment',
201 'wc_show_billing_fields',
202 'wc_thankyou_page',
203 'stripe_publishable_key',
204 'show_end_time',
205 'calendar_locale',
206 'allow_customer_rescheduling',
207 'allow_customer_cancellation',
208 'reschedule_restriction',
209 'cancellation_restriction',
210 ];
211
212 $public = array_intersect_key( $all, array_flip( $allowed ) );
213 $public = apply_filters( 'booktics_rest_public_settings', $public );
214
215 return $this->response( $public, __( 'Public settings fetched successfully.', 'booktics' ) );
216 }
217
218 /**
219 * Check if a given request has access to get items.
220 *
221 * @param WP_REST_Request $request Full data about the request.
222 *
223 * @return bool
224 */
225 public function get_public_item_permissions_check( $request ): bool {
226 return true;
227 }
228
229 /**
230 * Check if a given request has access to get items.
231 *
232 * @param WP_REST_Request $request Full data about the request.
233 *
234 * @return bool
235 */
236 public function stripe_disconnect_permissions_check( $request ): bool {
237 return current_user_can( 'manage_options' );
238 }
239
240 /**
241 * Disconnect Stripe account by clearing account ID
242 *
243 * @param $request
244 *
245 * @return WP_HTTP_Response
246 */
247 public function stripe_disconnect( $request ) {
248 Settings::update( 'stripe_account_id', '' );
249 Settings::update( 'stripe_account_type', '' );
250
251 return $this->response( array(), __( 'Successfully disconnected Stripe account.', 'booktics' ) );
252 }
253 }
254