| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class Api Settings |
| 4 |
* |
| 5 |
* @package Timetics |
| 6 |
*/ |
| 7 |
namespace Timetics\Core\Settings; |
| 8 |
|
| 9 |
defined( 'ABSPATH' ) || exit; |
| 10 |
|
| 11 |
use Timetics\Base\Api; |
| 12 |
use Timetics\Core\Admin\Hooks; |
| 13 |
use Timetics\Utils\Singleton; |
| 14 |
|
| 15 |
class Api_Settings extends Api { |
| 16 |
use Singleton; |
| 17 |
|
| 18 |
/** |
| 19 |
* Store api namespace |
| 20 |
* |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
protected $namespace = 'timetics/v1'; |
| 24 |
|
| 25 |
/** |
| 26 |
* Store rest base |
| 27 |
* |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
protected $rest_base = 'settings'; |
| 31 |
|
| 32 |
/** |
| 33 |
* Register rest route |
| 34 |
* |
| 35 |
* @return void |
| 36 |
*/ |
| 37 |
public function register_routes() { |
| 38 |
register_rest_route( |
| 39 |
$this->namespace, $this->rest_base, [ |
| 40 |
[ |
| 41 |
'methods' => \WP_REST_Server::READABLE, |
| 42 |
'callback' => [$this, 'get_settings'], |
| 43 |
'permission_callback' => function () { |
| 44 |
// The public booking app needs a small, explicitly safe |
| 45 |
// settings payload. get_settings() filters it by role. |
| 46 |
return true; |
| 47 |
}, |
| 48 |
], |
| 49 |
[ |
| 50 |
'methods' => \WP_REST_Server::EDITABLE, |
| 51 |
'callback' => [$this, 'update_settings'], |
| 52 |
'permission_callback' => function () { |
| 53 |
return current_user_can( 'manage_options' ); |
| 54 |
}, |
| 55 |
], |
| 56 |
] |
| 57 |
); |
| 58 |
|
| 59 |
register_rest_route( |
| 60 |
$this->namespace, $this->rest_base . '/business', [ |
| 61 |
[ |
| 62 |
'methods' => \WP_REST_Server::CREATABLE, |
| 63 |
'callback' => [$this, 'setup_business'], |
| 64 |
'permission_callback' => function () { |
| 65 |
return current_user_can( 'manage_options' ); |
| 66 |
}, |
| 67 |
], |
| 68 |
] |
| 69 |
); |
| 70 |
|
| 71 |
register_rest_route( |
| 72 |
$this->namespace, $this->rest_base . '/business/categories', [ |
| 73 |
[ |
| 74 |
'methods' => \WP_REST_Server::READABLE, |
| 75 |
'callback' => [$this, 'get_busyness_categories'], |
| 76 |
'permission_callback' => function () { |
| 77 |
return current_user_can( 'manage_options' ); |
| 78 |
}, |
| 79 |
], |
| 80 |
] |
| 81 |
); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Get settings |
| 86 |
* |
| 87 |
* @return JSON |
| 88 |
*/ |
| 89 |
public function get_settings() { |
| 90 |
$settings = apply_filters( 'timetics_settings', timetics_get_settings() ); |
| 91 |
|
| 92 |
// The booking and staff interfaces need non-sensitive display and scheduling |
| 93 |
// settings. Never send credentials or webhook URLs to non-administrators. |
| 94 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 95 |
$settings = $this->get_staff_safe_settings( $settings ); |
| 96 |
} |
| 97 |
|
| 98 |
$data = [ |
| 99 |
'status_code' => 200, |
| 100 |
'success' => 1, |
| 101 |
'message' => esc_html__( 'Get all settings', 'timetics' ), |
| 102 |
'data' => $settings, |
| 103 |
]; |
| 104 |
|
| 105 |
return rest_ensure_response( $settings ); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Return only settings that staff need to use the admin interface. |
| 110 |
* |
| 111 |
* This is deliberately an allow-list. New settings remain private until they |
| 112 |
* have been reviewed and explicitly added here. |
| 113 |
* |
| 114 |
* @param array $settings Plugin settings. |
| 115 |
* |
| 116 |
* @return array |
| 117 |
*/ |
| 118 |
private function get_staff_safe_settings( $settings ) { |
| 119 |
$safe_keys = [ |
| 120 |
'availability', |
| 121 |
'apple_calendar', |
| 122 |
'blocked_days', |
| 123 |
'busyness_category', |
| 124 |
'calendar_locale', |
| 125 |
'currency', |
| 126 |
'custom_fields', |
| 127 |
'default_booking_status', |
| 128 |
'guest_enabled', |
| 129 |
'guest_limit', |
| 130 |
'google_calendar', |
| 131 |
'locale_timezone', |
| 132 |
'paypal_status', |
| 133 |
'primary_color', |
| 134 |
'remainder_time', |
| 135 |
'secondary_color', |
| 136 |
'slot_interval', |
| 137 |
'stripe_status', |
| 138 |
'outlook_calendar', |
| 139 |
'wc_integration', |
| 140 |
'wc_checkout_url', |
| 141 |
'zoom_connection_type', |
| 142 |
]; |
| 143 |
|
| 144 |
return array_intersect_key( (array) $settings, array_flip( $safe_keys ) ); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Update settings |
| 149 |
* |
| 150 |
* @param WP_Rest_Request $request |
| 151 |
* |
| 152 |
* @return JSON |
| 153 |
*/ |
| 154 |
public function update_settings( $request ) { |
| 155 |
$options = json_decode( $request->get_body(), true ); |
| 156 |
|
| 157 |
/** |
| 158 |
* Added temporary for leagacy sass. It will remove in future. |
| 159 |
*/ |
| 160 |
$data = [ |
| 161 |
'status_code' => 200, |
| 162 |
'success' => 1, |
| 163 |
'message' => esc_html__( 'Settings successfully updated', 'timetics' ), |
| 164 |
'data' => timetics_get_settings(), |
| 165 |
]; |
| 166 |
|
| 167 |
// custom domain |
| 168 |
if (!empty($options['custom_domain_url']) && isset($options['custom_domain_url'])) { |
| 169 |
$custom_domain_data = [ |
| 170 |
'custom_domain_url' => $options['custom_domain_url'], |
| 171 |
'network_slug' => $options['network_slug'], |
| 172 |
]; |
| 173 |
do_action('timetics_custom_domain_data', $custom_domain_data); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
if ( !empty($options['schedule']) && $options['schedule'] && apply_filters('timetics/staff/member/availability', false)) { |
| 178 |
return rest_ensure_response( apply_filters( 'timetics/admin/staff/error_data', $data, 'availability_update' ) ); |
| 179 |
} |
| 180 |
|
| 181 |
if ( !empty($options['availability']) && $options['availability'] && apply_filters('timetics/staff/meeting/availability', false)) { |
| 182 |
return rest_ensure_response( apply_filters( 'timetics/admin/appointment/error_data', $data, 'availability_update' ) ); |
| 183 |
} |
| 184 |
|
| 185 |
if ( ! empty( $options['custom_fields'] ) && apply_filters( 'timetics/admin/settings/custom_fields', false, $options['custom_fields'] ) ) { |
| 186 |
return rest_ensure_response( apply_filters( 'timetics/admin/settings/error_data', $data, 'custom_fields', timetics_get_settings() ) ); |
| 187 |
} |
| 188 |
|
| 189 |
if ( ! empty( $options['paypal_status'] ) && $options['paypal_status'] && apply_filters( 'timetics/admin/settings/paypal', false ) ) { |
| 190 |
return rest_ensure_response( apply_filters( 'timetics/admin/settings/error_data', $data, 'paypal', timetics_get_settings() ) ); |
| 191 |
} |
| 192 |
|
| 193 |
if ( ! empty( $options['zoom_client_secret'] ) && $options['zoom_client_secret'] && apply_filters( 'timetics/admin/settings/zoom', false ) ) { |
| 194 |
return rest_ensure_response( apply_filters( 'timetics/admin/settings/error_data', $data, 'zoom', timetics_get_settings() ) ); |
| 195 |
} |
| 196 |
|
| 197 |
if ( ! empty( $options['fluentcrm_webhook'] ) && $options['fluentcrm_webhook'] && apply_filters( 'timetics/admin/settings/fluentcrm_webhook', false ) ) { |
| 198 |
return rest_ensure_response( apply_filters( 'timetics/admin/settings/error_data', $data, 'fluent_crm', timetics_get_settings() ) ); |
| 199 |
} |
| 200 |
|
| 201 |
if ( ! empty( $options['pabbly_webhook'] ) && $options['pabbly_webhook'] && apply_filters( 'timetics/admin/settings/pabbly_webhook', false ) ) { |
| 202 |
return rest_ensure_response( apply_filters( 'timetics/admin/settings/error_data', $data, 'pablly', timetics_get_settings() ) ); |
| 203 |
} |
| 204 |
|
| 205 |
if ( ! empty( $options['zapier_webhook'] ) && $options['zapier_webhook'] && apply_filters( 'timetics/admin/settings/zapier_webhook', false ) ) { |
| 206 |
return rest_ensure_response( apply_filters( 'timetics/admin/settings/error_data', $data, 'zapier', timetics_get_settings() ) ); |
| 207 |
} |
| 208 |
|
| 209 |
if (!empty($options['google_app_client_id']) && $options['google_app_client_id'] && apply_filters('timetics/admin/settings/google_calendar', false)) { |
| 210 |
return rest_ensure_response(apply_filters('timetics/admin/settings/error_data', $data, 'google-calendar', timetics_get_settings())); |
| 211 |
} |
| 212 |
|
| 213 |
if ( ! empty( $options['google_app_client_secret'] ) && $options['google_app_client_secret'] && apply_filters( 'timetics/admin/settings/google-calendar', false ) ) { |
| 214 |
return rest_ensure_response( apply_filters( 'timetics/admin/settings/error_data', $data, 'google-calendar', timetics_get_settings() ) ); |
| 215 |
} |
| 216 |
|
| 217 |
if ( ! empty( $options['outlook_calendar'] ) && $options['outlook_calendar'] && apply_filters( 'timetics/admin/settings/outlook_calendar', false ) ) { |
| 218 |
return rest_ensure_response( apply_filters( 'timetics/admin/settings/error_data', $data, 'outlook', timetics_get_settings() ) ); |
| 219 |
} |
| 220 |
|
| 221 |
if ( ! empty( $options['apple_calendar'] ) && $options['apple_calendar'] && apply_filters( 'timetics/admin/settings/apple_calendar', false ) ) { |
| 222 |
return rest_ensure_response( apply_filters( 'timetics/admin/settings/error_data', $data, 'paypal', timetics_get_settings() ) ); |
| 223 |
} |
| 224 |
|
| 225 |
if (!empty($options['twillo_message']) && $options['twillo_message'] && apply_filters('timetics/admin/settings/twillo_messaging', false)) { |
| 226 |
return rest_ensure_response(apply_filters('timetics/admin/settings/error_data', $data, 'twillo_messaging', timetics_get_settings())); |
| 227 |
} |
| 228 |
|
| 229 |
if ( $options ) { |
| 230 |
foreach ( $options as $key => $value ) { |
| 231 |
timetics_update_option( $key, $value ); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
$data['data'] = timetics_get_settings(); |
| 236 |
|
| 237 |
return rest_ensure_response( $data ); |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Business setup |
| 242 |
* |
| 243 |
* @param WP_Rest_Request $request |
| 244 |
* |
| 245 |
* @return void |
| 246 |
*/ |
| 247 |
public function setup_business( $request ) { |
| 248 |
$data = json_decode( $request->get_body(), true ); |
| 249 |
|
| 250 |
$email = ! empty( $data['email'] ) ? $data['email'] : ''; |
| 251 |
|
| 252 |
$body = array( |
| 253 |
'email' => $email, |
| 254 |
); |
| 255 |
|
| 256 |
$response_user = wp_remote_post( 'https://arraytics.com/?fluentcrm=1&route=contact&hash=0d9cd3d1-514d-4e1a-9147-09dfd5f9e997', ['body' => $body] ); |
| 257 |
|
| 258 |
$response = [ |
| 259 |
'status' => 200, |
| 260 |
'success' => 1, |
| 261 |
'message' => __( 'Successfully updated business', 'timetics' ), |
| 262 |
]; |
| 263 |
|
| 264 |
return rest_ensure_response( $response ); |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Get busyness categories |
| 269 |
* |
| 270 |
* @param WP_Rest_Request $request |
| 271 |
* |
| 272 |
* @return JSON |
| 273 |
*/ |
| 274 |
public function get_busyness_categories( $request ) { |
| 275 |
$busyness_categories = timetics_get_busyness_categories(); |
| 276 |
|
| 277 |
$response = [ |
| 278 |
'success' => 1, |
| 279 |
'status_code' => 200, |
| 280 |
'data' => $busyness_categories, |
| 281 |
]; |
| 282 |
|
| 283 |
return rest_ensure_response( $response ); |
| 284 |
} |
| 285 |
|
| 286 |
} |
| 287 |
|