private function _get_worker_appointments($worker_id, $status, $start_at)
 {
     global $appointments, $wpdb;
     $worker_sql = $status_sql = '';
     $services = appointments_get_worker_services($worker_id);
     $service_ids = !empty($services) ? array_filter(array_map('intval', wp_list_pluck($services, 'ID'))) : false;
     $worker_sql = !empty($service_ids) ? $wpdb->prepare('(worker=%d OR service IN(' . join(',', $service_ids) . '))', $worker_id) : $wpdb->prepare('worker=%d', $worker_id);
     $status = is_array($status) ? array_map('esc_sql', $status) : false;
     $status_sql = $status ? "AND status IN('" . join("','", $status) . "')" : '';
     $first = strtotime(date('Y-m-01', $start_at));
     $last = $first + date('t', $first) * 86400 - 1;
     $sql = $wpdb->prepare("SELECT * FROM {$appointments->app_table} WHERE {$worker_sql} {$status_sql} AND UNIX_TIMESTAMP(start)>%d AND UNIX_TIMESTAMP(end)<%d ORDER BY start", $first, $last);
     return $wpdb->get_results($sql);
 }
 private function resolve_service_id($worker = false)
 {
     //Try to guess the service ID related to the working hours.
     //This would be accurate only for specific providers providing a single service.
     $services = array();
     if ($worker) {
         $services = appointments_get_worker_services($worker);
     } else {
         $services = $this->_core->get_services();
     }
     foreach ($services as $key => $service) {
         if ($this->_data['service_padding'][$service->ID][self::PADDING_BEFORE] || $this->_data['service_padding'][$service->ID][self::PADDING_AFTER]) {
             return $service->ID;
         }
     }
     return false;
 }
 function test_get_worker_services()
 {
     $args = $this->factory->user->generate_args();
     $user_id = $this->factory->user->create_object($args);
     $service_id_1 = appointments_insert_service(array('name' => 'My Service 1'));
     $service_id_2 = appointments_insert_service(array('name' => 'My Service 2'));
     $args = array('ID' => $user_id, 'services_provided' => array($service_id_1, $service_id_2), 'dummy' => false);
     appointments_insert_worker($args);
     $services = appointments_get_worker_services($user_id);
     $this->assertCount(2, $services);
     $this->assertInstanceOf('Appointments_Service', $services[0]);
     $this->assertInstanceOf('Appointments_Service', $services[1]);
 }
    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;
    }