Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save panoslyrakis/9fe14a89b6315a0d2a10f3060a2d2d82 to your computer and use it in GitHub Desktop.
Save panoslyrakis/9fe14a89b6315a0d2a10f3060a2d2d82 to your computer and use it in GitHub Desktop.
It accepts argument "date" and returns appointments for that date.The date arg should be in "Y-m-d" format. Used with Appointments+ plugin
<?php
/*
Description: Copy file in mu-plugins folder. To be used with Appointments+ plugin. It accepts argument "date" and returns appointments for that date.The date arg should be in "Y-m-d" format.
*/
if( ! function_exists( 'appointments_get_appointments_for_day' ) ){
function appointments_get_appointments_for_day( $args = array() ) {
if( ! class_exists( 'Appointments' ) ){
return;
}
global $wpdb;
$table = appointments_get_table( 'appointments' );
$defaults = array(
'worker' => false,
'service' => false,
'location' => false,
'user' => false,
'date_query' => array(),
'app_id' => array(),
'status' => false,
'per_page' => -1,
'page' => 1,
's' => false,
'orderby' => 'ID',
'order' => 'ASC',
'count' => false, // Will return only the number of rows found
'date' => date( 'Y-m-d' )
);
$args = wp_parse_args( $args, $defaults );
$cache_key = md5( maybe_serialize( $args ) );
$cached_queries = wp_cache_get( 'app_get_appointments' );
if ( ! is_array( $cached_queries ) ) {
$cached_queries = array();
}
if ( isset( $cached_queries[ $cache_key ] ) ) {
$results = $cached_queries[ $cache_key ];
}
else {
$where = array();
if ( false !== $args['worker'] && $worker = appointments_get_worker( $args['worker'] ) ) {
$where[] = $wpdb->prepare( "worker = %d", $worker->ID );
}
if ( false !== $args['location'] ) {
$where[] = $wpdb->prepare( "location = %d", $args['location'] );
}
if ( false !== $args['user'] ) {
$where[] = $wpdb->prepare( "user = %d", $args['user'] );
}
if ( false !== $args['service'] && ! is_array( $args['service'] ) ) {
// Only one service, let's make it an array
$args['service'] = array( $args['service'] );
}
if ( ! empty( $args['service'] ) && is_array( $args['service'] ) ) {
$where_services = array();
foreach ( $args['service'] as $service_id ) {
$service = appointments_get_service( $service_id );
if ( ! $service ) {
continue;
}
$where_services[] = absint( $service_id );
}
if ( ! empty( $where_services ) ) {
$where[] = 'service IN (' . implode( ',', $where_services ) . ')';
}
}
if ( ! empty( $args['date_query'] ) && is_array( $args['date_query'] ) ) {
$date_query_where = array();
$date_queries = $args['date_query'];
// Set the date queries conditions
$allowed_conditions = array( 'AND', 'OR' );
if ( ! isset( $args['date_query']['condition'] ) ) {
$condition = 'AND';
}
else {
$condition = strtoupper( $args['date_query']['condition'] );
}
if ( ! in_array( $condition, $allowed_conditions ) ) {
$condition = 'AND';
}
// Parse every Date query
foreach ( $date_queries as $key => $date_query ) {
if ( 'condition' === $key ) {
continue;
}
$date_query = _appointments_parse_date_query( $date_query );
if ( $date_query ) {
$date_query_where[] = $wpdb->prepare( $date_query['field'] . $date_query['compare'] . "%s", $date_query['value'] );
}
}
if ( $date_query_where ) {
$where[] = '(' . implode( " " . $condition . " ", $date_query_where ) . ')';
}
}
if ( ! empty( $args['app_id'] ) && is_array( $args['app_id'] ) ) {
$args['app_id'] = array_map( 'absint', $args['app_id'] );
$where[] = 'ID IN ( ' . implode( ',', $args['app_id'] ) . ' )';
}
if ( $args['status'] ) {
$statuses = array();
if ( is_array( $args['status'] ) ) {
foreach ( $args['status'] as $status ) {
if ( array_key_exists( $status, appointments_get_statuses() ) ) {
$statuses[] = $status;
}
}
}
elseif ( is_string( $args['status'] ) && array_key_exists( $args['status'], appointments_get_statuses() ) ) {
$statuses = array( $args['status'] );
}
$where[] = 'status IN ("' . implode( '","', $statuses ) . '")';
}
$first = date( 'Y-m-d 00:00:00', strtotime( $args['date'] ) );
$first_timestamp = strtotime( $first );
$last_timestamp = strtotime('+1 day', $first_timestamp);
$last = date( 'Y-m-d H:i:s', $last_timestamp );
$where[] = $wpdb->prepare( "start >= %s", $first );
$where[] = $wpdb->prepare( "end < %s", $last );
if ( false !== $args['s'] ) {
$args['s'] = trim( $args['s'] );
if ( $args['s'] ) {
// Search by user name
$where[] = $wpdb->prepare(
"( name LIKE %s OR email LIKE %s OR user IN ( SELECT ID FROM $wpdb->users WHERE user_login LIKE %s OR user_nicename LIKE %s OR display_name LIKE %s OR user_email LIKE %s ) )",
'%' . $args['s'] . '%',
'%' . $args['s'] . '%',
'%' . $args['s'] . '%',
'%' . $args['s'] . '%',
'%' . $args['s'] . '%',
'%' . $args['s'] . '%'
);
}
}
if ( ! empty( $where ) ) {
$where = "WHERE " . implode( " AND ", $where );
}
else {
$where = '';
}
$allowed_orderby = array( 'ID', 'created', 'user', 'name', 'email', 'location',
'service', 'worker', 'price', 'status', 'start', 'end' );
$allowed_order = array( 'DESC', 'ASC', '' );
$order_query = '';
$args['order'] = strtoupper( $args['order'] );
if ( in_array( $args['orderby'], $allowed_orderby ) && in_array( $args['order'], $allowed_order ) ) {
$orderby = $args['orderby'];
$order = $args['order'];
$order_query = "ORDER BY $orderby $order";
}
$limit = '';
if ( $args['per_page'] > 0 ) {
$limit = $wpdb->prepare( "LIMIT %d, %d", intval( ( $args['page'] - 1 ) * $args['per_page'] ), intval( $args['per_page'] ) );
}
$found_rows = '';
if ( $args['count'] ) {
$found_rows = 'SQL_CALC_FOUND_ROWS';
}
$query = "SELECT $found_rows * FROM $table $where $order_query $limit";
$results = $wpdb->get_results( $query );
if ( $args['count'] ) {
$results = $wpdb->get_var( "SELECT FOUND_ROWS()" );
}
if ( $results && ! $args['count'] ) {
$cached_queries[ $cache_key ] = $results;
wp_cache_set( 'app_get_appointments', $cached_queries );
}
}
if ( ! $args['count'] ) {
$apps = array();
foreach ( $results as $row ) {
wp_cache_add( $row->ID, $row, 'app_appointments' );
$apps[] = new Appointments_Appointment( $row );
}
return $apps;
}
return $results;
}
}
?>
@panoslyrakis
Copy link
Author

panoslyrakis commented Jan 26, 2017

Can be called like:

$args = array(
	'per_page' 		=> 50,
	'page' 			=> 1,
	'status' 		=> array(
						'confirmed',
						'paid'
					),
	'orderby' 		=> 'start',
	'order' 		=> 'ASC',
	'date'			=> '2017-01-28'
);

$apps = appointments_get_appointments_for_day( $args );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment