/**
 * Delete a service
 *
 * @param int $service_id
 *
 * @return bool|false|int
 */
function appointments_delete_service($service_id)
{
    global $wpdb;
    if (!appointments_get_service($service_id)) {
        return false;
    }
    $table = appointments_get_table('services');
    $service_id = absint($service_id);
    $result = $wpdb->query($wpdb->prepare("DELETE FROM {$table} WHERE ID = %d", $service_id));
    // Remove the service from all workers
    $table = appointments_get_table('workers');
    $wpdb->query($wpdb->prepare("UPDATE {$table} SET services_provided = REPLACE( services_provided,':%d:','' )", $service_id));
    if ($result) {
        appointments_delete_service_cache($service_id);
        return true;
    }
    return false;
}
function appointments_update_worker($worker_id, $args = array())
{
    global $wpdb;
    $old_worker = appointments_get_worker($worker_id);
    if (!$old_worker) {
        return false;
    }
    $fields = array('services_provided' => '%s', 'dummy' => '%s', 'price' => '%s', 'page' => '%d', 'ID' => '%d');
    $update = array();
    $update_wildcards = array();
    foreach ($fields as $field => $wildcard) {
        if (isset($args[$field])) {
            $update[$field] = $args[$field];
            $update_wildcards[] = $wildcard;
        }
    }
    if (isset($update['price'])) {
        $update['price'] = $price = preg_replace("/[^0-9,.]/", "", $update['price']);
    }
    if (isset($insert['dummy']) && $insert['dummy']) {
        $insert['dummy'] = true;
    } elseif (isset($insert['dummy']) && !$insert['dummy']) {
        $insert['dummy'] = '';
    }
    if (isset($update['services_provided'])) {
        if (!is_array($update['services_provided']) || empty($update['services_provided'])) {
            return false;
        }
        $services_provided = array();
        foreach ($update['services_provided'] as $service_id) {
            if (appointments_get_service($service_id)) {
                $services_provided[] = $service_id;
            }
        }
        if (empty($services_provided)) {
            return false;
        }
        $update['services_provided'] = ':' . implode(':', array_filter($services_provided)) . ':';
    }
    if (isset($update['ID'])) {
        $user_id = absint($update['ID']);
        if (!get_userdata($user_id)) {
            return false;
        }
        $update['ID'] = $user_id;
    }
    if (empty($update)) {
        return false;
    }
    $table = appointments_get_table('workers');
    $result = $wpdb->update($table, $update, array('ID' => $worker_id), $update_wildcards, array('%d'));
    if ($result) {
        appointments_delete_worker_cache($worker_id);
        if (isset($update['ID'])) {
            appointments_delete_worker_cache($update['ID']);
        }
        // Update working hours and exceptions if we have changed the ID
        if (isset($update['ID'])) {
            $wh_table = appointments_get_table('wh');
            $ex_table = appointments_get_table('exceptions');
            $wpdb->update($wh_table, array('worker' => $update['ID']), array('worker' => $worker_id), array('%d'), array('%d'));
            $wpdb->update($ex_table, array('worker' => $update['ID']), array('worker' => $worker_id), array('%d'), array('%d'));
        }
    }
    do_action('wpmudev_appointments_update_worker', $worker_id, $args, $old_worker);
    return (bool) $result;
}
 /**
  * @group cache
  */
 function test_services_cache()
 {
     $args = $this->factory->post->generate_args();
     $args['post_type'] = 'page';
     $page_id = $this->factory->post->create_object($args);
     $args = array('name' => 'My Service 1');
     $service_id = appointments_insert_service($args);
     $service = wp_cache_get($service_id, 'app_services');
     $this->assertFalse($service);
     // This will set the cache
     appointments_get_service($service_id);
     $service = wp_cache_get($service_id, 'app_services');
     $this->assertEquals($service->name, 'My Service 1');
     appointments_delete_service($service_id);
     $service = wp_cache_get($service_id, 'app_services');
     $this->assertFalse($service);
     $service_id = appointments_insert_service($args);
     appointments_get_service($service_id);
     appointments_update_service($service_id, array('name' => 'New name'));
     $service = wp_cache_get($service_id, 'app_services');
     $this->assertFalse($service);
     $service_id = appointments_insert_service(array('name' => 'Service 1'));
     appointments_insert_service(array('name' => 'Service 2', 'page' => $page_id));
     $services = appointments_get_services();
     $this->assertCount(4, $services);
     $cache = wp_cache_get('app_get_services');
     $this->assertCount(1, $cache);
     $services = appointments_get_services(array('page' => 8888));
     $this->assertCount(0, $services);
     $cache = wp_cache_get('app_get_services');
     $this->assertCount(1, $cache);
     $services = appointments_get_services(array('page' => $page_id));
     $this->assertCount(1, $services);
     $cache = wp_cache_get('app_get_services');
     $this->assertCount(2, $cache);
     $this->assertNotEmpty(wp_cache_get($service_id, 'app_services'));
     // If we insert another service, cache sould be cleared
     appointments_insert_service(array('name' => 'Service 3'));
     $this->assertFalse(wp_cache_get('app_get_services'));
     // If we select again, cache should be refreshed
     $services = appointments_get_services();
     $this->assertCount(5, $services);
     $cache = wp_cache_get('app_get_services');
     $this->assertCount(1, $cache);
 }
    public function process_shortcode($args = array(), $content = '')
    {
        global $wpdb, $appointments;
        $args = wp_parse_args($args, $this->_defaults_to_args());
        extract($args);
        $appointments->get_lsw();
        if (!trim($args['order_by'])) {
            $args['order_by'] = 'ID';
        }
        if ($args['worker']) {
            $services = appointments_get_worker_services($args['worker']);
            // Find first service by this worker
            $fsby = $services[0]->ID;
            if ($fsby && !@$_REQUEST['app_service_id']) {
                $_REQUEST['app_service_id'] = $fsby;
                // Set this as first service
                $appointments->get_lsw();
                // Update
            }
            // Re-sort worker services
            if (!empty($services) && !empty($args['order_by']) && 'ID' !== $args['order_by']) {
                $services = $this->_reorder_services($services, $args['order_by']);
            }
        } else {
            $services = $appointments->get_services($args['order_by']);
        }
        $services = apply_filters('app_services', $services);
        // If there are no workers do nothing
        if (!$services || empty($services)) {
            return '';
        }
        ob_start();
        ?>
		<div class="app_services">
			<div class="app_services_dropdown">
				<div class="app_services_dropdown_title" id="app_services_dropdown_title">
					<?php 
        echo $args['select'];
        ?>
				</div>
				<div class="app_services_dropdown_select">
					<select id="app_select_services" name="app_select_services" class="app_select_services">
						<?php 
        foreach ($services as $service) {
            ?>
							<option value="<?php 
            echo $service->ID;
            ?>
" <?php 
            selected($service->ID, $appointments->service);
            ?>
><?php 
            echo stripslashes($service->name);
            ?>
</option>
						<?php 
        }
        ?>
					</select>
					<input type="button" class="app_services_button" value="<?php 
        echo esc_attr($args['show']);
        ?>
">
				</div>
			</div>

			<div class="app_service_excerpts">
				<?php 
        if ($args['autorefresh']) {
            // Only display the selected service
            ?>
					<?php 
            $service = appointments_get_service($appointments->service);
            if ($service) {
                $page = apply_filters('app_service_page', $service->page, $service->ID);
                ?>
								<div class="app_service_excerpt" id="app_service_excerpt_<?php 
                echo $service->ID;
                ?>
">
									<?php 
                $service_description = '';
                switch ($args['description']) {
                    case 'none':
                        break;
                    case 'content':
                        $service_description = $appointments->get_content($page, $args['thumb_size'], $args['thumb_class'], $service->ID);
                        break;
                    default:
                        $service_description = $appointments->get_excerpt($page, $args['thumb_size'], $args['thumb_class'], $service->ID);
                        break;
                }
                echo apply_filters('app-services-service_description', $service_description, $service, $args['description']);
                ?>
								</div>
							<?php 
            }
            ?>
				<?php 
        } else {
            ?>
					<?php 
            foreach ($services as $service) {
                ?>
						<?php 
                $page = apply_filters('app_service_page', $service->page, $service->ID);
                ?>
						<div <?php 
                echo $service->ID != $appointments->service ? 'style="display:none"' : '';
                ?>
 class="app_service_excerpt" id="app_service_excerpt_<?php 
                echo $service->ID;
                ?>
">
							<?php 
                $service_description = '';
                switch ($args['description']) {
                    case 'none':
                        break;
                    case 'content':
                        $service_description = $appointments->get_content($page, $args['thumb_size'], $args['thumb_class'], $service->ID, absint($args['ajax']));
                        break;
                    default:
                        $service_description = $appointments->get_excerpt($page, $args['thumb_size'], $args['thumb_class'], $service->ID, absint($args['ajax']));
                        break;
                }
                echo apply_filters('app-services-service_description', $service_description, $service, $args['description']);
                ?>
						</div>
					<?php 
            }
            ?>
				<?php 
        }
        ?>

			</div>
		</div>
		<?php 
        $s = ob_get_clean();
        $wcalendar = isset($_GET['wcalendar']) && (int) $_GET['wcalendar'] ? (int) $_GET['wcalendar'] : false;
        // First remove these parameters and add them again to make wcalendar appear before js variable
        $href = add_query_arg(array("wcalendar" => false, "app_provider_id" => false, "app_service_id" => false));
        $href = apply_filters('app_service_href', add_query_arg(array("wcalendar" => $wcalendar, "app_service_id" => "__selected_service__"), $href));
        $href = $this->_js_esc_url($href) . '#app_services_dropdown_title';
        if (!$args['_noscript']) {
            wp_enqueue_script('app-shortcode-services', appointments_plugin_url() . 'includes/shortcodes/js/app-services.js', array('jquery'));
            $ajax_url = admin_url('admin-ajax.php');
            if (!is_ssl() && force_ssl_admin()) {
                $ajax_url = admin_url('admin-ajax.php', 'http');
            }
            $i10n = array('size' => $args['thumb_size'], 'worker' => $args['worker'], 'ajaxurl' => $ajax_url, 'thumbclass' => $args['thumb_class'], 'autorefresh' => $args['autorefresh'], 'ajax' => $args['ajax'], 'first_service_id' => (int) $appointments->get_first_service_id(), 'reload_url' => $href);
            wp_localize_script('app-shortcode-services', 'appointmentsStrings', $i10n);
        }
        return $s;
    }
/**
 * Update an appointment data
 *
 * @param $app_id
 * @param $args
 * @param bool $resend
 */
function appointments_update_appointment($app_id, $args)
{
    global $wpdb, $appointments;
    $old_appointment = appointments_get_appointment($app_id);
    if (!$old_appointment) {
        return false;
    }
    $fields = array('user' => '%d', 'email' => '%s', 'name' => '%s', 'phone' => '%s', 'address' => '%s', 'city' => '%s', 'service' => '%d', 'worker' => '%d', 'price' => '%s', 'note' => '%s', 'status' => false, 'location' => '%d', 'date' => false, 'time' => false, 'gcal_updated' => '%s', 'gcal_ID' => '%s');
    $update = array();
    $update_wildcards = array();
    foreach ($fields as $field => $wildcard) {
        if (isset($args[$field]) && $wildcard) {
            $update[$field] = $args[$field];
            $update_wildcards[] = $wildcard;
        }
    }
    if (isset($update['service'])) {
        $service = appointments_get_service($update['service']);
        if (!$service) {
            return false;
        }
    }
    if (isset($update['worker'])) {
        $worker = appointments_get_worker($update['worker']);
        if (!$worker) {
            $update['worker'] = 0;
        }
    }
    if (isset($update['price'])) {
        $update['price'] = preg_replace("/[^0-9,.]/", "", $update['price']);
    }
    if (isset($update['user'])) {
        $user = get_userdata($update['user']);
        if (!$user) {
            $update['user'] = 0;
        }
    }
    if (!empty($args['date']) && !empty($args['time'])) {
        $time = date('H:i', strtotime($args['time']));
        $datetime = strtotime(str_replace(',', '', $appointments->to_us($args['date'])) . " " . $time);
        $update['start'] = date('Y-m-d H:i:s', $datetime);
        $update_wildcards[] = '%s';
        $update['end'] = date('Y-m-d H:i:s', $datetime + $service->duration * 60);
        $update_wildcards[] = '%s';
    }
    // Change status?
    $updated_status = false;
    if (!empty($args['status'])) {
        // Yeah, maybe change status
        $updated_status = appointments_update_appointment_status($app_id, $args['status']);
    }
    if (empty($update) && empty($updated_status)) {
        return false;
    }
    $result = false;
    if (!empty($update)) {
        $table = appointments_get_table('appointments');
        $result = $wpdb->update($table, $update, array('ID' => $app_id), $update_wildcards, array('%d'));
    }
    if (!$result && !$updated_status) {
        // Nothing has changed
        return false;
    }
    $app = appointments_get_appointment($app_id);
    appointments_update_user_appointment_data($app_id);
    appointments_clear_appointment_cache($app_id);
    do_action('wpmudev_appointments_update_appointment', $app_id, $args, $old_appointment);
    return true;
}
 /**
  *	Admin pages init stuff, save settings
  *
  */
 function admin_init()
 {
     global $appointments;
     if (!session_id()) {
         @session_start();
     }
     $page = add_menu_page('Appointments', __('Appointments', 'appointments'), App_Roles::get_capability('manage_options', App_Roles::CTX_PAGE_APPOINTMENTS), 'appointments', array(&$this, 'appointment_list'), 'dashicons-clock');
     add_submenu_page('appointments', __('Transactions', 'appointments'), __('Transactions', 'appointments'), App_Roles::get_capability('manage_options', App_Roles::CTX_PAGE_TRANSACTIONS), "app_transactions", array(&$this, 'transactions'));
     add_submenu_page('appointments', __('Settings', 'appointments'), __('Settings', 'appointments'), App_Roles::get_capability('manage_options', App_Roles::CTX_PAGE_SETTINGS), "app_settings", array(&$this, 'settings'));
     add_submenu_page('appointments', __('Shortcodes', 'appointments'), __('Shortcodes', 'appointments'), App_Roles::get_capability('manage_options', App_Roles::CTX_PAGE_SHORTCODES), "app_shortcodes", array(&$this, 'shortcodes_page'));
     add_submenu_page('appointments', __('FAQ', 'appointments'), __('FAQ', 'appointments'), App_Roles::get_capability('manage_options', App_Roles::CTX_PAGE_FAQ), "app_faq", array(&$this, 'faq_page'));
     // Add datepicker to appointments page
     add_action("admin_print_scripts-{$page}", array(&$this, 'admin_scripts'));
     do_action('app-admin-admin_pages_added', $page);
     if (isset($_POST["action_app"]) && !wp_verify_nonce($_POST['app_nonce'], 'update_app_settings')) {
         add_action('admin_notices', array(&$this, 'warning'));
         return;
     }
     // Read Location, Service, Worker
     $appointments->get_lsw();
     global $wpdb;
     if (isset($_POST["action_app"]) && 'save_general' == $_POST["action_app"]) {
         $appointments->options["min_time"] = $_POST["min_time"];
         $appointments->options["additional_min_time"] = trim($_POST["additional_min_time"]);
         $appointments->options["admin_min_time"] = $_POST["admin_min_time"];
         $appointments->options["app_lower_limit"] = trim($_POST["app_lower_limit"]);
         $appointments->options["app_limit"] = trim($_POST["app_limit"]);
         $appointments->options["clear_time"] = trim($_POST["clear_time"]);
         $appointments->options["spam_time"] = trim($_POST["spam_time"]);
         $appointments->options["auto_confirm"] = $_POST["auto_confirm"];
         $appointments->options["allow_worker_wh"] = $_POST["allow_worker_wh"];
         $appointments->options["allow_worker_confirm"] = $_POST["allow_worker_confirm"];
         $appointments->options["allow_overwork"] = $_POST["allow_overwork"];
         $appointments->options["allow_overwork_break"] = $_POST["allow_overwork_break"];
         $appointments->options["dummy_assigned_to"] = !$appointments->is_dummy(@$_POST["dummy_assigned_to"]) ? @$_POST["dummy_assigned_to"] : 0;
         $appointments->options["login_required"] = $_POST["login_required"];
         $appointments->options["accept_api_logins"] = isset($_POST["accept_api_logins"]);
         $appointments->options["facebook-no_init"] = isset($_POST["facebook-no_init"]);
         $appointments->options['facebook-app_id'] = trim($_POST['facebook-app_id']);
         $appointments->options['twitter-app_id'] = trim($_POST['twitter-app_id']);
         $appointments->options['twitter-app_secret'] = trim($_POST['twitter-app_secret']);
         $appointments->options['google-client_id'] = trim($_POST['google-client_id']);
         $appointments->options["app_page_type"] = $_POST["app_page_type"];
         $appointments->options["show_legend"] = $_POST["show_legend"];
         $appointments->options["color_set"] = $_POST["color_set"];
         foreach ($appointments->get_classes() as $class => $name) {
             $appointments->options[$class . "_color"] = $_POST[$class . "_color"];
         }
         $appointments->options["ask_name"] = isset($_POST["ask_name"]);
         $appointments->options["ask_email"] = isset($_POST["ask_email"]);
         $appointments->options["ask_phone"] = isset($_POST["ask_phone"]);
         $appointments->options["ask_phone"] = isset($_POST["ask_phone"]);
         $appointments->options["ask_address"] = isset($_POST["ask_address"]);
         $appointments->options["ask_city"] = isset($_POST["ask_city"]);
         $appointments->options["ask_note"] = isset($_POST["ask_note"]);
         $appointments->options["additional_css"] = trim(stripslashes_deep($_POST["additional_css"]));
         $appointments->options["payment_required"] = $_POST["payment_required"];
         $appointments->options["percent_deposit"] = trim(str_replace('%', '', $_POST["percent_deposit"]));
         $appointments->options["fixed_deposit"] = trim(str_replace($appointments->options["currency"], '', $_POST["fixed_deposit"]));
         /*
          * Membership plugin is replaced by Membership2. Old options are
          * only saved when the depreacted Membership plugin is still active.
          */
         if (class_exists('M_Membership')) {
             $appointments->options['members_no_payment'] = isset($_POST['members_no_payment']);
             // not used??
             $appointments->options['members_discount'] = trim(str_replace('%', '', $_POST['members_discount']));
             $appointments->options['members'] = maybe_serialize(@$_POST["members"]);
         }
         $appointments->options['currency'] = $_POST['currency'];
         $appointments->options['mode'] = $_POST['mode'];
         $appointments->options['merchant_email'] = trim($_POST['merchant_email']);
         $appointments->options['return'] = $_POST['return'];
         $appointments->options['allow_free_autoconfirm'] = !empty($_POST['allow_free_autoconfirm']);
         $appointments->options["send_confirmation"] = $_POST["send_confirmation"];
         $appointments->options["send_notification"] = @$_POST["send_notification"];
         $appointments->options["confirmation_subject"] = stripslashes_deep($_POST["confirmation_subject"]);
         $appointments->options["confirmation_message"] = stripslashes_deep($_POST["confirmation_message"]);
         $appointments->options["send_reminder"] = $_POST["send_reminder"];
         $appointments->options["reminder_time"] = str_replace(" ", "", $_POST["reminder_time"]);
         $appointments->options["send_reminder_worker"] = $_POST["send_reminder_worker"];
         $appointments->options["reminder_time_worker"] = str_replace(" ", "", $_POST["reminder_time_worker"]);
         $appointments->options["reminder_subject"] = stripslashes_deep($_POST["reminder_subject"]);
         $appointments->options["reminder_message"] = stripslashes_deep($_POST["reminder_message"]);
         $appointments->options["send_removal_notification"] = $_POST["send_removal_notification"];
         $appointments->options["removal_notification_subject"] = stripslashes_deep($_POST["removal_notification_subject"]);
         $appointments->options["removal_notification_message"] = stripslashes_deep($_POST["removal_notification_message"]);
         $appointments->options["log_emails"] = $_POST["log_emails"];
         $appointments->options['use_cache'] = $_POST['use_cache'];
         $appointments->options['disable_js_check_admin'] = isset($_POST['disable_js_check_admin']);
         $appointments->options['disable_js_check_frontend'] = isset($_POST['disable_js_check_frontend']);
         $appointments->options['use_mp'] = isset($_POST['use_mp']);
         $appointments->options["app_page_type_mp"] = @$_POST["app_page_type_mp"];
         $appointments->options['allow_cancel'] = @$_POST['allow_cancel'];
         $appointments->options['cancel_page'] = @$_POST['cancel_page'];
         $appointments->options["records_per_page"] = (int) trim(@$_POST["records_per_page"]);
         $appointments->options = apply_filters('app-options-before_save', $appointments->options);
         $saved = false;
         if (update_option('appointments_options', $appointments->options)) {
             $saved = true;
             if ('yes' == $appointments->options['use_cache']) {
                 add_action('admin_notices', array(&$appointments, 'saved_cleared'));
             } else {
                 add_action('admin_notices', array(&$appointments, 'saved'));
             }
         }
         // Flush cache
         if (isset($_POST["force_flush"]) || $saved) {
             $appointments->flush_cache();
             appointments_delete_timetables_cache();
             if (isset($_POST["force_flush"])) {
                 add_action('admin_notices', array(&$appointments, 'cleared'));
             }
         }
         if (isset($_POST['make_an_appointment']) || isset($_POST['make_an_appointment_product'])) {
             $this->_create_pages();
         }
         // Redirecting when saving options
         if ($saved) {
             wp_redirect(add_query_arg('saved', 1));
             die;
         }
     }
     $result = $updated = $inserted = false;
     // Save Working Hours
     if (isset($_POST["action_app"]) && 'save_working_hours' == $_POST["action_app"]) {
         $location = (int) $_POST['location'];
         foreach (array('closed', 'open') as $stat) {
             $query = $wpdb->prepare("SELECT COUNT(*) FROM {$appointments->wh_table} WHERE location=%d AND worker=%d AND status=%s", $location, $appointments->worker, $stat);
             $count = $wpdb->get_var($query);
             if ($count > 0) {
                 $r = $wpdb->update($appointments->wh_table, array('hours' => serialize($_POST[$stat]), 'status' => $stat), array('location' => $location, 'worker' => $appointments->worker, 'status' => $stat), array('%s', '%s'), array('%d', '%d', '%s'));
                 if ($r) {
                     $result = true;
                 }
             } else {
                 $r = $wpdb->insert($appointments->wh_table, array('location' => $location, 'worker' => $appointments->worker, 'hours' => serialize($_POST[$stat]), 'status' => $stat), array('%d', '%d', '%s', '%s'));
                 if ($r) {
                     $result = true;
                 }
             }
             if ($result) {
                 add_action('admin_notices', array(&$appointments, 'saved'));
             }
             appointments_delete_work_breaks_cache($location, $appointments->worker);
             appointments_delete_timetables_cache();
         }
     }
     // Save Exceptions
     if (isset($_POST["action_app"]) && 'save_exceptions' == $_POST["action_app"]) {
         $location = (int) $_POST['location'];
         foreach (array('closed', 'open') as $stat) {
             $count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$appointments->exceptions_table} WHERE location=%d AND worker=%d AND status=%s", $location, $appointments->worker, $stat));
             if ($count > 0) {
                 $r = $wpdb->update($appointments->exceptions_table, array('days' => $this->_sort($_POST[$stat]["exceptional_days"]), 'status' => $stat), array('location' => $location, 'worker' => $appointments->worker, 'status' => $stat), array('%s', '%s'), array('%d', '%d', '%s'));
                 if ($r) {
                     $result = true;
                 }
             } else {
                 $r = $wpdb->insert($appointments->exceptions_table, array('location' => $location, 'worker' => $appointments->worker, 'days' => $this->_sort($_POST[$stat]["exceptional_days"]), 'status' => $stat), array('%d', '%d', '%s', '%s'));
                 if ($r) {
                     $result = true;
                 }
             }
             if ($result) {
                 add_action('admin_notices', array(&$appointments, 'saved'));
             }
             appointments_delete_exceptions_cache($location, $appointments->worker);
         }
     }
     // Save Services
     if (isset($_POST["action_app"]) && 'save_services' == $_POST["action_app"] && is_array($_POST["services"])) {
         do_action('app-services-before_save');
         foreach ($_POST["services"] as $ID => $service) {
             if ('' != trim($service["name"])) {
                 // Update or insert?
                 $_service = appointments_get_service($ID);
                 if ($_service) {
                     $args = array('name' => $service["name"], 'capacity' => (int) $service["capacity"], 'duration' => $service["duration"], 'price' => $service["price"], 'page' => $service["page"]);
                     $result = appointments_update_service($ID, $args);
                 } else {
                     $args = array('ID' => $ID, 'name' => $service["name"], 'capacity' => (int) $service["capacity"], 'duration' => $service["duration"], 'price' => $service["price"], 'page' => $service["page"]);
                     $result = appointments_insert_service($args);
                 }
                 do_action('app-services-service-updated', $ID);
             } else {
                 // Entering an empty name means deleting of a service
                 $r = appointments_delete_service($ID);
                 if ($r) {
                     $result = true;
                 }
             }
         }
         if ($result) {
             add_action('admin_notices', array(&$appointments, 'saved'));
         }
     }
     // Save Workers
     if (isset($_POST["action_app"]) && 'save_workers' == $_POST["action_app"] && is_array($_POST["workers"])) {
         foreach ($_POST["workers"] as $worker_id => $worker) {
             $new_worker_id = absint($worker["user"]);
             $worker_id = absint($worker_id);
             $inserted = false;
             $updated = false;
             $result = false;
             $worker_exists = appointments_get_worker($worker_id);
             if ($worker_exists) {
                 // Update
                 if ($new_worker_id != $worker_id && !empty($worker["services_provided"])) {
                     // We are trying to chage the user ID
                     $count = appointments_get_worker($new_worker_id);
                     // If the new ID already exist, do nothing
                     if (!$count) {
                         // Otherwise, change the ID
                         $args = array('ID' => $new_worker_id, 'price' => $worker["price"], 'services_provided' => $worker["services_provided"], 'dummy' => isset($worker["dummy"]), 'page' => $worker['page']);
                         $updated = appointments_update_worker($worker_id, $args);
                     }
                 } elseif ($new_worker_id == $worker_id && !empty($worker["services_provided"])) {
                     // Do not change user ID but update
                     $args = array('price' => $worker["price"], 'services_provided' => $worker["services_provided"], 'dummy' => isset($worker["dummy"]), 'page' => $worker['page']);
                     $updated = appointments_update_worker($worker_id, $args);
                 } elseif (empty($worker["services_provided"])) {
                     $r = appointments_delete_worker($worker_id);
                     if ($r) {
                         $result = true;
                     }
                 }
             } elseif (!$worker_exists && !empty($worker["services_provided"])) {
                 // Insert
                 $args = array('ID' => $worker["user"], 'price' => $worker["price"], 'services_provided' => $worker["services_provided"], 'page' => $worker["page"], 'dummy' => isset($worker["dummy"]));
                 $inserted = appointments_insert_worker($args);
                 if ($inserted) {
                     do_action('app-workers-worker-updated', $worker_id);
                 }
             }
         }
         if ($result || $updated || $inserted) {
             add_action('admin_notices', array(&$appointments, 'saved'));
         }
     }
     // Delete removed app records
     if (isset($_POST["delete_removed"]) && 'delete_removed' == $_POST["delete_removed"] && isset($_POST["app"]) && is_array($_POST["app"])) {
         $result = 0;
         foreach ($_POST["app"] as $app_id) {
             $result = $result + appointments_delete_appointment($app_id);
         }
         if ($result) {
             global $current_user;
             $userdata = get_userdata($current_user->ID);
             add_action('admin_notices', array(&$appointments, 'deleted'));
             do_action('app_deleted', $_POST["app"]);
             $appointments->log(sprintf(__('Appointment(s) with id(s):%s deleted by user:%s', 'appointments'), implode(', ', $_POST["app"]), $userdata->user_login));
         }
     }
     // Bulk status change
     if (isset($_POST["app_status_change"]) && $_POST["app_new_status"] && isset($_POST["app"]) && is_array($_POST["app"])) {
         $result = 0;
         $new_status = $_POST["app_new_status"];
         foreach ($_POST["app"] as $app_id) {
             $result = $result + (int) appointments_update_appointment_status(absint($app_id), $new_status);
         }
         if ($result) {
             $userdata = get_userdata(get_current_user_id());
             add_action('admin_notices', array(&$appointments, 'updated'));
             do_action('app_bulk_status_change', $_POST["app"]);
             $appointments->log(sprintf(__('Status of Appointment(s) with id(s):%s changed to %s by user:%s', 'appointments'), implode(', ', $_POST["app"]), $new_status, $userdata->user_login));
             if (is_object($appointments->gcal_api)) {
                 // If deleted, remove these from GCal too
                 if ('removed' == $new_status) {
                     foreach ($_POST["app"] as $app_id) {
                         $appointments->gcal_api->delete($app_id);
                         $appointments->send_removal_notification($app_id);
                     }
                 } else {
                     if (is_object($appointments->gcal_api) && $appointments->gcal_api->is_syncable_status($new_status)) {
                         foreach ($_POST["app"] as $app_id) {
                             $appointments->gcal_api->update($app_id);
                             // Also send out an email
                             if (!empty($appointments->options["send_confirmation"]) && 'yes' == $appointments->options["send_confirmation"]) {
                                 appointments_send_confirmation($app_id);
                             }
                         }
                     }
                 }
             }
         }
     }
     // Determine if we shall flush cache
     if (isset($_POST["action_app"]) && ($result || $updated || $inserted) || isset($_POST["delete_removed"]) && 'delete_removed' == $_POST["delete_removed"] || isset($_POST["app_status_change"]) && $_POST["app_new_status"]) {
         // As it means any setting is saved, lets clear cache
         $appointments->flush_cache();
     }
 }
 function test_update_appointment()
 {
     $worker_id = $this->factory->user->create_object($this->factory->user->generate_args());
     $worker_id_2 = $this->factory->user->create_object($this->factory->user->generate_args());
     $user_id = $this->factory->user->create_object($this->factory->user->generate_args());
     $user_id_2 = $this->factory->user->create_object($this->factory->user->generate_args());
     $service_args = array('name' => 'My Service', 'duration' => 90);
     $service_id = appointments_insert_service($service_args);
     $service = appointments_get_service($service_id);
     $service_args = array('name' => 'My Service 2', 'duration' => 90);
     $service_id_2 = appointments_insert_service($service_args);
     $worker_args = array('ID' => $worker_id, 'services_provided' => array($service_id));
     appointments_insert_worker($worker_args);
     $worker_args = array('ID' => $worker_id_2, 'services_provided' => array($service_id));
     appointments_insert_worker($worker_args);
     $args = array('user' => $user_id, 'email' => '*****@*****.**', 'name' => 'Tester', 'phone' => '667788', 'address' => 'An address', 'city' => 'Madrid', 'service' => $service_id, 'worker' => $worker_id, 'price' => '90', 'date' => 'December 18, 2024', 'time' => '07:30', 'note' => 'It\'s a note', 'status' => 'paid', 'location' => 5, 'gcal_updated' => '2015-12-01', 'gcal_ID' => 'test');
     $app_id = appointments_insert_appointment($args);
     // Only change status
     $args = array('status' => 'confirmed');
     $result = appointments_update_appointment($app_id, $args);
     $this->assertTrue($result);
     $app = appointments_get_appointment($app_id);
     $this->assertEquals('confirmed', $app->status);
     // Change all fields except status
     $args = array('user' => $user_id_2, 'email' => '*****@*****.**', 'name' => 'Tester2', 'phone' => '6677882', 'address' => 'An address2', 'city' => 'Madrid2', 'service' => $service_id_2, 'worker' => $worker_id_2, 'price' => '120', 'date' => 'November 18, 2024', 'time' => '10:30', 'note' => 'It\'s a note2', 'location' => 6, 'gcal_updated' => '2018-12-01', 'gcal_ID' => 'test2');
     $result = appointments_update_appointment($app_id, $args);
     $this->assertTrue($result);
     $app = appointments_get_appointment($app_id);
     $this->assertEquals($app_id, $app->ID);
     $this->assertEquals($args['user'], $app->user);
     $this->assertEquals($args['name'], $app->name);
     $this->assertEquals($args['email'], $app->email);
     $this->assertEquals($args['phone'], $app->phone);
     $this->assertEquals($args['address'], $app->address);
     $this->assertEquals($args['city'], $app->city);
     $this->assertEquals($args['worker'], $app->worker);
     $this->assertEquals($args['price'], $app->price);
     $this->assertEquals($args['service'], $app->service);
     $this->assertEquals('2024-11-18 10:30:00', $app->start);
     $this->assertEquals('2024-11-18 12:00:00', $app->end);
     $this->assertEquals($args['note'], $app->note);
     $this->assertEquals('confirmed', $app->status);
     $this->assertEquals($args['location'], $app->location);
     $this->assertEquals($args['gcal_updated'] . ' 00:00:00', $app->gcal_updated);
     $this->assertEquals($args['gcal_ID'], $app->gcal_ID);
     $this->assertNotEmpty($app->created);
     // Wrong service
     $args = array('service' => 8888);
     $result = appointments_update_appointment($app_id, $args);
     $this->assertFalse($result);
     $app = appointments_get_appointment($app_id);
     $this->assertEquals($service_id_2, $app->service);
     // Wrong worker
     $args = array('worker' => 8888);
     $result = appointments_update_appointment($app_id, $args);
     $this->assertTrue($result);
     $app = appointments_get_appointment($app_id);
     $this->assertEquals(0, $app->worker);
     // Only location
     $result = appointments_update_appointment($app_id, array('location' => 20));
     $this->assertTrue($result);
     $app = appointments_get_appointment($app_id);
     $this->assertEquals(20, $app->location);
 }
 /**
  * Get a single service with given ID
  * @param ID: Id of the service to be retrieved
  * @return object
  */
 function get_service($ID)
 {
     return appointments_get_service($ID);
 }