PluginProbe
Booktics – Appointment Booking Calendar for Service Businesses / 1.0.0
Booktics – Appointment Booking Calendar for Service Businesses v1.0.0
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 1.0.4 1.0.5 1.0.6 1.0.7 All 27 releases
booktics / core / settings / controllers / settings-controller.php

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

217 lines 7.0 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\Payment\Payment_Method_Manager;
7 use Booktics\Services\Schedule_Manager;
8 use Booktics\Settings\Settings;
9 use WP_REST_Server;
10
11 if ( ! function_exists( 'is_plugin_active' ) ) {
12 require_once ABSPATH . 'wp-admin/includes/plugin.php';
13 }
14
15 /**
16 * Settings controller
17 *
18 * @package Booktics/Appointment
19 */
20 class Settings_Controller extends Base_Rest_Controller {
21 /**
22 * Endpoint namespace
23 *
24 * @var string
25 */
26 protected $namespace = 'booktics/v1';
27
28 /**
29 * Route name
30 *
31 * @var string
32 */
33 protected $rest_base = 'settings';
34
35 /**
36 * Register routes
37 *
38 * @return void
39 */
40 public function register_routes(): void {
41 register_rest_route(
42 $this->namespace,
43 '/' . $this->rest_base,
44 array(
45 array(
46 'methods' => WP_REST_Server::READABLE,
47 'callback' => array( $this, 'get_item' ),
48 'args' => array(),
49 'permission_callback' => '__return_true',
50 ),
51 array(
52 'methods' => WP_REST_Server::EDITABLE,
53 'callback' => array( $this, 'update_item' ),
54 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
55 'permission_callback' => array(
56 $this,
57 'get_item_permissions_check',
58 ),
59 ),
60 'schema' => array( $this, 'get_public_item_schema' ),
61 )
62 );
63
64 register_rest_route(
65 $this->namespace,
66 '/' . $this->rest_base . '/public',
67 array(
68 array(
69 'methods' => WP_REST_Server::READABLE,
70 'callback' => array( $this, 'get_public_item' ),
71 'args' => array(),
72 'permission_callback' => array(
73 $this,
74 'get_public_item_permissions_check',
75 ),
76 ),
77 )
78 );
79
80 register_rest_route(
81 $this->namespace,
82 '/' . $this->rest_base . '/stripe/disconnect',
83 array(
84 array(
85 'methods' => WP_REST_Server::EDITABLE,
86 'callback' => array(
87 $this,
88 'stripe_disconnect',
89 ),
90 'permission_callback' => array(
91 $this,
92 'stripe_disconnect_permissions_check',
93 ),
94 ),
95 )
96 );
97 }
98
99 /**
100 * Check if a given request has access to get items.
101 *
102 * @param WP_REST_Request $request Full data about the request.
103 *
104 * @return WP_Error
105 */
106 public function get_item_permissions_check( $request ): bool {
107 return current_user_can( 'manage_options' );
108 }
109
110 /**
111 * Format settings for API response.
112 *
113 * @param array $settings Settings array.
114 *
115 * @return array
116 */
117 private function format_settings_for_response( array $settings ): array {
118 $settings['reschedule_restriction'] = empty( $settings['reschedule_restriction'] ) ? (object) array() : $settings['reschedule_restriction'];
119 $settings['custom_reschedule_status'] = empty( $settings['custom_reschedule_status'] ) ? (object) array() : $settings['custom_reschedule_status'];
120 $settings['cancellation_restriction'] = empty( $settings['cancellation_restriction'] ) ? (object) array() : $settings['cancellation_restriction'];
121
122 if ( ! is_plugin_active( 'booktics-stripe-addon/booktics_stripe.php' ) ) {
123 $settings['stripe_addon_install_status'] = 0;
124 } else {
125 $settings['stripe_addon_install_status'] = 1;
126 $stripe = ( new Payment_Method_Manager() )->get_payment_method( 'stripe' );
127 $settings['stripe_connect_test_url'] = $stripe::connect_account( 'test' );
128 $settings['stripe_connect_live_url'] = $stripe::connect_account( 'live' );
129 $settings['stripe_account_id'] = $stripe::account_id();
130 }
131
132 return $settings;
133 }
134
135 /**
136 * Get a collection of items.
137 *
138 * @param WP_REST_Request $request Full data about the request.
139 *
140 * @return WP_REST_Response
141 */
142 public function get_item( $request ) {
143 $settings = Settings::get();
144 $settings = apply_filters( 'booktics_rest_settings', $settings );
145 $settings = $this->format_settings_for_response( $settings );
146
147 return $this->response( $settings, __( 'Settings fetched successfully.', 'booktics' ) );
148 }
149
150 /**
151 * Update plugin settings.
152 *
153 * @param WP_Rest_Request $request Request object.
154 *
155 * @return WP_Rest_Response
156 */
157 public function update_item( $request ) {
158 $params = $request->get_params();
159
160 if ( isset( $params['schedule'] ) || isset( $params['custom'] ) || isset( $params['holiday'] ) ) {
161 $input_data = json_decode( $request->get_body(), true );
162 $schedule_manager = Schedule_Manager::make( $input_data );
163 $schedule_manager->save();
164 unset( $params['schedule'], $params['custom'], $params['holiday'] );
165 }
166
167 Settings::update( $params );
168
169 $settings = Settings::get();
170 $settings = apply_filters( 'booktics_rest_settings', $settings );
171 $settings = $this->format_settings_for_response( $settings );
172
173 return $this->response( $settings, __( 'Settings updated successfully.', 'booktics' ) );
174 }
175
176 /**
177 * Get public settings
178 */
179 public function get_public_item( $request ): array {
180 return array();
181 }
182
183 /**
184 * Check if a given request has access to get items.
185 *
186 * @param WP_REST_Request $request Full data about the request.
187 *
188 * @return WP_Error
189 */
190 public function get_public_item_permissions_check( $request ): bool {
191 return true;
192 }
193
194 /**
195 * Check if a given request has access to get items.
196 *
197 * @param WP_REST_Request $request Full data about the request.
198 *
199 * @return WP_Error
200 */
201 public function stripe_disconnect_permissions_check( $request ): bool {
202 return current_user_can( 'manage_options' );
203 }
204
205 /**
206 * Disconnect Stripe account by clearing account ID
207 *
208 * @param $request Full data about the request.
209 */
210 public function stripe_disconnect( $request ) {
211 Settings::update( 'stripe_account_id', '' );
212 Settings::update( 'stripe_account_type', '' );
213
214 return $this->response( array(), __( 'Successfully disconnected Stripe account.', 'booktics' ) );
215 }
216 }
217