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