| @@ -6,8 +6,9 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | namespace Timetics\Core\Settings; |
| 8 | 8 | |
| 9 | 9 | use Timetics\Base\Api; |
| 10 | +use Timetics\Core\Admin\Hooks; | |
| 10 | 11 | use Timetics\Utils\Singleton; |
| 11 | 12 | |
| 12 | 13 | class Api_Settings extends Api { |
| 13 | 14 | use Singleton; |
| @@ -33,24 +34,48 @@ | ||
| 33 | 34 | */ |
| 34 | 35 | public function register_routes() { |
| 35 | 36 | register_rest_route( |
| 36 | 37 | $this->namespace, $this->rest_base, [ |
| 37 | - [ | |
| 38 | - 'methods' => \WP_REST_Server::READABLE, | |
| 39 | - 'callback' => [ $this, 'get_settings' ], | |
| 40 | - 'permission_callback' => function () { | |
| 41 | - return current_user_can( 'manage_timetics' ); | |
| 42 | - }, | |
| 43 | - ], | |
| 44 | - [ | |
| 45 | - 'methods' => \WP_REST_Server::EDITABLE, | |
| 46 | - 'callback' => [ $this, 'update_settings' ], | |
| 47 | - 'permission_callback' => function () { | |
| 48 | - return current_user_can( 'manage_timetics' ); | |
| 49 | - }, | |
| 50 | - ], | |
| 51 | - ] | |
| 38 | + [ | |
| 39 | + 'methods' => \WP_REST_Server::READABLE, | |
| 40 | + 'callback' => [$this, 'get_settings'], | |
| 41 | + 'permission_callback' => function () { | |
| 42 | + return true; | |
| 43 | + }, | |
| 44 | + ], | |
| 45 | + [ | |
| 46 | + 'methods' => \WP_REST_Server::EDITABLE, | |
| 47 | + 'callback' => [$this, 'update_settings'], | |
| 48 | + 'permission_callback' => function () { | |
| 49 | + return current_user_can( 'manage_options' ); | |
| 50 | + }, | |
| 51 | + ], | |
| 52 | + ] | |
| 52 | 53 | ); |
| 54 | + | |
| 55 | + register_rest_route( | |
| 56 | + $this->namespace, $this->rest_base . '/business', [ | |
| 57 | + [ | |
| 58 | + 'methods' => \WP_REST_Server::CREATABLE, | |
| 59 | + 'callback' => [$this, 'setup_business'], | |
| 60 | + 'permission_callback' => function () { | |
| 61 | + return current_user_can( 'manage_options' ); | |
| 62 | + }, | |
| 63 | + ], | |
| 64 | + ] | |
| 65 | + ); | |
| 66 | + | |
| 67 | + register_rest_route( | |
| 68 | + $this->namespace, $this->rest_base . '/business/categories', [ | |
| 69 | + [ | |
| 70 | + 'methods' => \WP_REST_Server::READABLE, | |
| 71 | + 'callback' => [$this, 'get_busyness_categories'], | |
| 72 | + 'permission_callback' => function () { | |
| 73 | + return current_user_can( 'manage_options' ); | |
| 74 | + }, | |
| 75 | + ], | |
| 76 | + ] | |
| 77 | + ); | |
| 53 | 78 | } |
| 54 | 79 | |
| 55 | 80 | /** |
| 56 | 81 | * Get settings |
| @@ -57,15 +82,25 @@ | ||
| 57 | 82 | * |
| 58 | 83 | * @return JSON |
| 59 | 84 | */ |
| 60 | 85 | public function get_settings() { |
| 61 | - $settings = timetics_get_settings(); | |
| 62 | - | |
| 86 | + $settings = apply_filters( 'timetics_settings', timetics_get_settings() ); | |
| 87 | + $role = !current_user_can( 'manage_timetics' ); | |
| 88 | + //only run for non user capabilities | |
| 89 | + if ( $role ) { | |
| 90 | + $exclude_settings = $this->exclude_settings_for_customer(); | |
| 91 | + foreach($settings as $key => $setting){ | |
| 92 | + if( in_array( $key, $exclude_settings )){ | |
| 93 | + unset( $settings[$key] ); | |
| 94 | + } | |
| 95 | + } | |
| 96 | + } | |
| 97 | + | |
| 63 | 98 | $data = [ |
| 64 | 99 | 'status_code' => 200, |
| 65 | - 'success' => 1, | |
| 66 | - 'message' => __( 'Get all settings', 'timetics' ), | |
| 67 | - 'data' => $settings, | |
| 100 | + 'success' => 1, | |
| 101 | + 'message' => esc_html__( 'Get all settings', 'timetics' ), | |
| 102 | + 'data' => $settings, | |
| 68 | 103 | ]; |
| 69 | 104 | |
| 70 | 105 | return rest_ensure_response( $settings ); |
| 71 | 106 | } |
| @@ -79,8 +114,80 @@ | ||
| 79 | 114 | */ |
| 80 | 115 | public function update_settings( $request ) { |
| 81 | 116 | $options = json_decode( $request->get_body(), true ); |
| 82 | 117 | |
| 118 | + /** | |
| 119 | + * Added temporary for leagacy sass. It will remove in future. | |
| 120 | + */ | |
| 121 | + $data = [ | |
| 122 | + 'status_code' => 200, | |
| 123 | + 'success' => 1, | |
| 124 | + 'message' => esc_html__( 'Settings successfully updated', 'timetics' ), | |
| 125 | + 'data' => timetics_get_settings(), | |
| 126 | + ]; | |
| 127 | + | |
| 128 | + // custom domain | |
| 129 | + if (!empty($options['custom_domain_url']) && isset($options['custom_domain_url'])) { | |
| 130 | + $custom_domain_data = [ | |
| 131 | + 'custom_domain_url' => $options['custom_domain_url'], | |
| 132 | + 'network_slug' => $options['network_slug'], | |
| 133 | + ]; | |
| 134 | + do_action('custom_domain_data', $custom_domain_data); | |
| 135 | + } | |
| 136 | + | |
| 137 | + | |
| 138 | + if ( !empty($options['schedule']) && $options['schedule'] && apply_filters('timetics/staff/member/availability', false)) { | |
| 139 | + return rest_ensure_response( apply_filters( 'timetics/admin/staff/error_data', $data, 'availability_update' ) ); | |
| 140 | + } | |
| 141 | + | |
| 142 | + if ( !empty($options['availability']) && $options['availability'] && apply_filters('timetics/staff/meeting/availability', false)) { | |
| 143 | + return rest_ensure_response( apply_filters( 'timetics/admin/appointment/error_data', $data, 'availability_update' ) ); | |
| 144 | + } | |
| 145 | + | |
| 146 | + if ( ! empty( $options['custom_fields'] ) && apply_filters( 'timetics/admin/settings/custom_fields', false, $options['custom_fields'] ) ) { | |
| 147 | + return rest_ensure_response( apply_filters( 'timetics/admin/settings/error_data', $data, 'custom_fields', timetics_get_settings() ) ); | |
| 148 | + } | |
| 149 | + | |
| 150 | + if ( ! empty( $options['paypal_status'] ) && $options['paypal_status'] && apply_filters( 'timetics/admin/settings/paypal', false ) ) { | |
| 151 | + return rest_ensure_response( apply_filters( 'timetics/admin/settings/error_data', $data, 'paypal', timetics_get_settings() ) ); | |
| 152 | + } | |
| 153 | + | |
| 154 | + if ( ! empty( $options['zoom_client_secret'] ) && $options['zoom_client_secret'] && apply_filters( 'timetics/admin/settings/zoom', false ) ) { | |
| 155 | + return rest_ensure_response( apply_filters( 'timetics/admin/settings/error_data', $data, 'zoom', timetics_get_settings() ) ); | |
| 156 | + } | |
| 157 | + | |
| 158 | + if ( ! empty( $options['fluentcrm_webhook'] ) && $options['fluentcrm_webhook'] && apply_filters( 'timetics/admin/settings/fluentcrm_webhook', false ) ) { | |
| 159 | + return rest_ensure_response( apply_filters( 'timetics/admin/settings/error_data', $data, 'fluent_crm', timetics_get_settings() ) ); | |
| 160 | + } | |
| 161 | + | |
| 162 | + if ( ! empty( $options['pabbly_webhook'] ) && $options['pabbly_webhook'] && apply_filters( 'timetics/admin/settings/pabbly_webhook', false ) ) { | |
| 163 | + return rest_ensure_response( apply_filters( 'timetics/admin/settings/error_data', $data, 'pablly', timetics_get_settings() ) ); | |
| 164 | + } | |
| 165 | + | |
| 166 | + if ( ! empty( $options['zapier_webhook'] ) && $options['zapier_webhook'] && apply_filters( 'timetics/admin/settings/zapier_webhook', false ) ) { | |
| 167 | + return rest_ensure_response( apply_filters( 'timetics/admin/settings/error_data', $data, 'zapier', timetics_get_settings() ) ); | |
| 168 | + } | |
| 169 | + | |
| 170 | + if (!empty($options['google_app_client_id']) && $options['google_app_client_id'] && apply_filters('timetics/admin/settings/google_calendar', false)) { | |
| 171 | + return rest_ensure_response(apply_filters('timetics/admin/settings/error_data', $data, 'google-calendar', timetics_get_settings())); | |
| 172 | + } | |
| 173 | + | |
| 174 | + if ( ! empty( $options['google_app_client_secret'] ) && $options['google_app_client_secret'] && apply_filters( 'timetics/admin/settings/google-calendar', false ) ) { | |
| 175 | + return rest_ensure_response( apply_filters( 'timetics/admin/settings/error_data', $data, 'google-calendar', timetics_get_settings() ) ); | |
| 176 | + } | |
| 177 | + | |
| 178 | + if ( ! empty( $options['outlook_calendar'] ) && $options['outlook_calendar'] && apply_filters( 'timetics/admin/settings/outlook_calendar', false ) ) { | |
| 179 | + return rest_ensure_response( apply_filters( 'timetics/admin/settings/error_data', $data, 'outlook', timetics_get_settings() ) ); | |
| 180 | + } | |
| 181 | + | |
| 182 | + if ( ! empty( $options['apple_calendar'] ) && $options['apple_calendar'] && apply_filters( 'timetics/admin/settings/apple_calendar', false ) ) { | |
| 183 | + return rest_ensure_response( apply_filters( 'timetics/admin/settings/error_data', $data, 'paypal', timetics_get_settings() ) ); | |
| 184 | + } | |
| 185 | + | |
| 186 | + if (!empty($options['twillo_message']) && $options['twillo_message'] && apply_filters('timetics/admin/settings/twillo_messaging', false)) { | |
| 187 | + return rest_ensure_response(apply_filters('timetics/admin/settings/error_data', $data, 'twillo_messaging', timetics_get_settings())); | |
| 188 | + } | |
| 189 | + | |
| 83 | 190 | if ( $options ) { |
| 84 | 191 | foreach ( $options as $key => $value ) { |
| 85 | 192 | timetics_update_option( $key, $value ); |
| 86 | 193 | } |
| @@ -85,14 +192,115 @@ | ||
| 85 | 192 | timetics_update_option( $key, $value ); |
| 86 | 193 | } |
| 87 | 194 | } |
| 88 | 195 | |
| 89 | - $data = [ | |
| 90 | - 'status_code' => 200, | |
| 196 | + $data['data'] = timetics_get_settings(); | |
| 197 | + | |
| 198 | + return rest_ensure_response( $data ); | |
| 199 | + } | |
| 200 | + | |
| 201 | + /** | |
| 202 | + * Business setup | |
| 203 | + * | |
| 204 | + * @param WP_Rest_Request $request | |
| 205 | + * | |
| 206 | + * @return void | |
| 207 | + */ | |
| 208 | + public function setup_business( $request ) { | |
| 209 | + $data = json_decode( $request->get_body(), true ); | |
| 210 | + | |
| 211 | + $email = ! empty( $data['email'] ) ? $data['email'] : ''; | |
| 212 | + | |
| 213 | + $body = array( | |
| 214 | + 'email' => $email, | |
| 215 | + ); | |
| 216 | + | |
| 217 | + $response_user = wp_remote_post( 'https://arraytics.com/?fluentcrm=1&route=contact&hash=0d9cd3d1-514d-4e1a-9147-09dfd5f9e997', ['body' => $body] ); | |
| 218 | + | |
| 219 | + $response = [ | |
| 220 | + 'status' => 200, | |
| 91 | 221 | 'success' => 1, |
| 92 | - 'message' => __( 'Settings successfully updated', 'timetics' ), | |
| 93 | - 'data' => timetics_get_settings(), | |
| 222 | + 'message' => __( 'Successfully updated business', 'timetics' ), | |
| 94 | 223 | ]; |
| 95 | 224 | |
| 96 | - return rest_ensure_response( $data ); | |
| 225 | + return rest_ensure_response( $response ); | |
| 226 | + } | |
| 227 | + | |
| 228 | + /** | |
| 229 | + * Get busyness categories | |
| 230 | + * | |
| 231 | + * @param WP_Rest_Request $request | |
| 232 | + * | |
| 233 | + * @return JSON | |
| 234 | + */ | |
| 235 | + public function get_busyness_categories( $request ) { | |
| 236 | + $busyness_categories = timetics_get_busyness_categories(); | |
| 237 | + | |
| 238 | + $response = [ | |
| 239 | + 'success' => 1, | |
| 240 | + 'status_code' => 200, | |
| 241 | + 'data' => $busyness_categories, | |
| 242 | + ]; | |
| 243 | + | |
| 244 | + return rest_ensure_response( $response ); | |
| 245 | + } | |
| 246 | + | |
| 247 | + public function exclude_settings_for_customer() { | |
| 248 | + $allowed_keys = [ | |
| 249 | + "booking_created_customer_email_from", | |
| 250 | + "booking_created_customer_email_title", | |
| 251 | + "booking_created_customer_email_body", | |
| 252 | + "booking_created_host_email_from", | |
| 253 | + "booking_created_host_email_title", | |
| 254 | + "booking_created_host_email_body", | |
| 255 | + "booking_canceled_customer_email_from", | |
| 256 | + "booking_canceled_customer_email_title", | |
| 257 | + "booking_canceled_customer_email_body", | |
| 258 | + "booking_canceled_host_email_from", | |
| 259 | + "booking_canceled_host_email_title", | |
| 260 | + "booking_canceled_host_email_body", | |
| 261 | + "booking_rescheduled_customer_email_from", | |
| 262 | + "booking_rescheduled_customer_email_title", | |
| 263 | + "booking_rescheduled_customer_email_body", | |
| 264 | + "booking_rescheduled_host_email_from", | |
| 265 | + "booking_rescheduled_host_email_title", | |
| 266 | + "booking_rescheduled_host_email_body", | |
| 267 | + "booking_reminder_customer_email_from", | |
| 268 | + "booking_reminder_customer_email_title", | |
| 269 | + "booking_reminder_customer_email_body", | |
| 270 | + "booking_reminder_host_email_from", | |
| 271 | + "booking_reminder_host_email_title", | |
| 272 | + "booking_reminder_host_email_body", | |
| 273 | + "google_auth_redirect_uri", | |
| 274 | + "google_app_client_id", | |
| 275 | + "google_app_client_secret", | |
| 276 | + "zoom_client_id", | |
| 277 | + "zoom_client_secret", | |
| 278 | + "zoom_auth_redirect_uri", | |
| 279 | + "apple_calendar", | |
| 280 | + "outlook_calendar", | |
| 281 | + "fluentcrm_webhook", | |
| 282 | + "zapier_webhook", | |
| 283 | + "twillo_account_id", | |
| 284 | + "twillo_token", | |
| 285 | + "twillo_phone_number", | |
| 286 | + "booking_created_customer", | |
| 287 | + "booking_created_host", | |
| 288 | + "booking_canceled_customer", | |
| 289 | + "booking_canceled_host", | |
| 290 | + "booking_rescheduled_customer", | |
| 291 | + "booking_rescheduled_host", | |
| 292 | + "booking_reminder_customer", | |
| 293 | + "booking_reminder_host", | |
| 294 | + "stripe_pub_key", | |
| 295 | + "stripe_secret_key", | |
| 296 | + "paypal_client_id", | |
| 297 | + "paypal_client_secret", | |
| 298 | + "outlook_client_id", | |
| 299 | + "outlook_client_secret", | |
| 300 | + "outlook_auth_redirect_uri", | |
| 301 | + ]; | |
| 302 | + | |
| 303 | + | |
| 304 | + return $allowed_keys; | |
| 97 | 305 | } |
| 98 | 306 | } |