| 1 |
<?php |
| 2 |
/** AJAX: create or update an Appointment Service. @package Booking Calendar */ |
| 3 |
if ( ! defined( 'ABSPATH' ) ) { exit; } |
| 4 |
|
| 5 |
/** |
| 6 |
* Create or update a Service from the AJAX settings inspector payload. |
| 7 |
* |
| 8 |
* @return void Terminates with a JSON success or error response. |
| 9 |
*/ |
| 10 |
function wpbc_appointment_services_ajax_save() { |
| 11 |
wpbc_appointment_services_ajax_authorize(); $provider = wpbc_appointment_services_get_data_provider(); |
| 12 |
if ( ! is_object( $provider ) || ! method_exists( $provider, 'save' ) ) { wpbc_appointment_services_send_provider_error( wpbc_appointment_services_storage_error(), __( 'Service storage is unavailable.', 'booking' ) ); } |
| 13 |
// phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 14 |
$raw_payload = isset( $_POST['service'] ) ? wp_unslash( $_POST['service'] ) : array(); |
| 15 |
$payload = wpbc_appointment_services_sanitize_payload( $raw_payload ); |
| 16 |
if ( '' === $payload['title'] ) { wp_send_json_error( array( 'message' => __( 'Enter a Service title.', 'booking' ) ), 400 ); } |
| 17 |
$result = $provider->save( $payload ); |
| 18 |
if ( is_wp_error( $result ) || empty( $result ) ) { wpbc_appointment_services_send_provider_error( $result, __( 'The Service could not be saved.', 'booking' ) ); } |
| 19 |
wp_send_json_success( array( 'service' => wpbc_appointment_services_normalize_item( $result ), 'message' => __( 'Service saved.', 'booking' ) ) ); |
| 20 |
} |
| 21 |
add_action( 'wp_ajax_WPBC_AJX_APPOINTMENT_SERVICE_SAVE', 'wpbc_appointment_services_ajax_save' ); |
| 22 |
|