Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Created September 2, 2019 13:38
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 wpmudev-sls/4f1e3cac80c57f02471e3f97287bc4a9 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/4f1e3cac80c57f02471e3f97287bc4a9 to your computer and use it in GitHub Desktop.
[Appointment+] - Show Scheduled Users Extended. Extend existing functionality of Show Scheduled Users to show users names on time slots
<?php
/**
* Plugin Name: [Appointment+] - Show Scheduled Users Extended
* Plugin URI: https://premium.wpmudev.org/
* Description: Extend existing functionality of Show Scheduled Users to show users names on time slots
* Author: Alessandro Kaounas @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WPMUDEV_Appointments_Scheduled_Users' ) ) {
class WPMUDEV_Appointments_Scheduled_Users {
private static $_instance = null;
public static function get_instance() {
if( is_null( self::$_instance ) ){
self::$_instance = new WPMUDEV_Appointments_Scheduled_Users();
}
return self::$_instance;
}
private function __construct() {
add_filter( 'app-schedule_cell-title', array( $this, 'appointments_scheduled_users' ), 20, 5 );
}
function appointments_scheduled_users($title, $is_busy, $start, $end, $schedule_key){
// This has to be checked out
// if (!$is_busy) return $title;
$customers = $this->_get_appointments_by_interval($start, $end, $schedule_key);
if (empty($customers)) return $title;
$names = join(" - ", array_unique(wp_list_pluck($customers, 'name')));
return $names;
}
private function _get_appointments_by_interval ($start, $end, $schedule_key) {
$apps = wp_cache_get('app-show_users-' . $schedule_key);
if (!$apps) $apps = $this->_get_appointments_for_scheduled_interval($schedule_key);
if (!$apps) return false;
$ret = array();
$period = new App_Period($start, $end);
foreach ($apps as $app) {
//if (mysql2date('U',$app->start) >= $start && mysql2date('U',$app->end) <= $end) $ret[] = $app;
if ($period->contains($app->start, $app->end)) $ret[] = $app;
}
return $ret;
}
private function _get_appointments_for_scheduled_interval( $schedule_key ) {
$data = explode( 'x', $schedule_key );
if ( count( $data ) != 2 ) {
$interval_start = current_time( 'timestamp' );
$interval_end = strtotime( 'next month', $interval_start );
} else {
$interval_start = $data[0];
$interval_end = $data[1];
}
$appointments = appointments();
$args = array(
'status' => array( 'pending', 'paid', 'confirmed', 'reserved' )
);
if ( $appointments->service ) {
$args['service'] = $appointments->service;
}
if ( $appointments->worker ) {
$args['worker'] = $appointments->service;
}
$apps = appointments_get_appointments( $args );
$res = array();
foreach ( $apps as $app ) {
if ( strtotime( $app->start ) > $interval_start && strtotime( $app->end ) < $interval_end ) {
$res[] = $app;
}
}
wp_cache_set( 'app-show_users-' . $schedule_key, $res );
return $res;
}
}
if ( ! function_exists( 'wpmudev_appointments_scheduled_users' ) ) {
function wpmudev_appointments_scheduled_users() {
return WPMUDEV_Appointments_Scheduled_Users::get_instance();
};
add_action( 'plugins_loaded', 'wpmudev_appointments_scheduled_users', 99 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment