← All changes
|
core/settings/controllers/settings-controller.php
+86
-21
1.0.0
→
1.0.29
View file →
| @@ -1,12 +1,18 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Booktics\Settings\Controllers; |
| 4 | 4 | |
| 5 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 6 | + exit; | |
| 7 | +} | |
| 8 | + | |
| 5 | 9 | use Booktics\Abstracts\Base_Rest_Controller; |
| 6 | -use Booktics\Payment\Payment_Method_Manager; | |
| 7 | 10 | use Booktics\Services\Schedule_Manager; |
| 8 | 11 | use Booktics\Settings\Settings; |
| 12 | +use WP_Error; | |
| 13 | +use WP_HTTP_Response; | |
| 14 | +use WP_REST_Request; | |
| 9 | 15 | use WP_REST_Server; |
| 10 | 16 | |
| 11 | 17 | if ( ! function_exists( 'is_plugin_active' ) ) { |
| 12 | 18 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| @@ -45,9 +51,9 @@ | ||
| 45 | 51 | array( |
| 46 | 52 | 'methods' => WP_REST_Server::READABLE, |
| 47 | 53 | 'callback' => array( $this, 'get_item' ), |
| 48 | 54 | 'args' => array(), |
| 49 | - 'permission_callback' => '__return_true', | |
| 55 | + 'permission_callback' => array( $this, 'get_item_permissions_check' ), | |
| 50 | 56 | ), |
| 51 | 57 | array( |
| 52 | 58 | 'methods' => WP_REST_Server::EDITABLE, |
| 53 | 59 | 'callback' => array( $this, 'update_item' ), |
| @@ -114,21 +120,15 @@ | ||
| 114 | 120 | * |
| 115 | 121 | * @return array |
| 116 | 122 | */ |
| 117 | 123 | 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']; | |
| 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; | |
| 121 | 129 | |
| 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 | - } | |
| 130 | + $settings = apply_filters( 'booktics_format_setting_response', $settings ); | |
| 131 | 131 | |
| 132 | 132 | return $settings; |
| 133 | 133 | } |
| 134 | 134 | |
| @@ -136,9 +136,9 @@ | ||
| 136 | 136 | * Get a collection of items. |
| 137 | 137 | * |
| 138 | 138 | * @param WP_REST_Request $request Full data about the request. |
| 139 | 139 | * |
| 140 | - * @return WP_REST_Response | |
| 140 | + * @return WP_HTTP_Response | |
| 141 | 141 | */ |
| 142 | 142 | public function get_item( $request ) { |
| 143 | 143 | $settings = Settings::get(); |
| 144 | 144 | $settings = apply_filters( 'booktics_rest_settings', $settings ); |
| @@ -151,9 +151,9 @@ | ||
| 151 | 151 | * Update plugin settings. |
| 152 | 152 | * |
| 153 | 153 | * @param WP_Rest_Request $request Request object. |
| 154 | 154 | * |
| 155 | - * @return WP_Rest_Response | |
| 155 | + * @return WP_HTTP_Response | |
| 156 | 156 | */ |
| 157 | 157 | public function update_item( $request ) { |
| 158 | 158 | $params = $request->get_params(); |
| 159 | 159 | |
| @@ -163,8 +163,45 @@ | ||
| 163 | 163 | $schedule_manager->save(); |
| 164 | 164 | unset( $params['schedule'], $params['custom'], $params['holiday'] ); |
| 165 | 165 | } |
| 166 | 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 | + | |
| 167 | 204 | Settings::update( $params ); |
| 168 | 205 | |
| 169 | 206 | $settings = Settings::get(); |
| 170 | 207 | $settings = apply_filters( 'booktics_rest_settings', $settings ); |
| @@ -175,10 +212,36 @@ | ||
| 175 | 212 | |
| 176 | 213 | /** |
| 177 | 214 | * Get public settings |
| 178 | 215 | */ |
| 179 | - public function get_public_item( $request ): array { | |
| 180 | - return array(); | |
| 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' ) ); | |
| 181 | 244 | } |
| 182 | 245 | |
| 183 | 246 | /** |
| 184 | 247 | * Check if a given request has access to get items. |
| @@ -184,9 +247,9 @@ | ||
| 184 | 247 | * Check if a given request has access to get items. |
| 185 | 248 | * |
| 186 | 249 | * @param WP_REST_Request $request Full data about the request. |
| 187 | 250 | * |
| 188 | - * @return WP_Error | |
| 251 | + * @return bool | |
| 189 | 252 | */ |
| 190 | 253 | public function get_public_item_permissions_check( $request ): bool { |
| 191 | 254 | return true; |
| 192 | 255 | } |
| @@ -195,9 +258,9 @@ | ||
| 195 | 258 | * Check if a given request has access to get items. |
| 196 | 259 | * |
| 197 | 260 | * @param WP_REST_Request $request Full data about the request. |
| 198 | 261 | * |
| 199 | - * @return WP_Error | |
| 262 | + * @return bool | |
| 200 | 263 | */ |
| 201 | 264 | public function stripe_disconnect_permissions_check( $request ): bool { |
| 202 | 265 | return current_user_can( 'manage_options' ); |
| 203 | 266 | } |
| @@ -204,9 +267,11 @@ | ||
| 204 | 267 | |
| 205 | 268 | /** |
| 206 | 269 | * Disconnect Stripe account by clearing account ID |
| 207 | 270 | * |
| 208 | - * @param $request Full data about the request. | |
| 271 | + * @param $request | |
| 272 | + * | |
| 273 | + * @return WP_HTTP_Response | |
| 209 | 274 | */ |
| 210 | 275 | public function stripe_disconnect( $request ) { |
| 211 | 276 | Settings::update( 'stripe_account_id', '' ); |
| 212 | 277 | Settings::update( 'stripe_account_type', '' ); |