Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Created December 24, 2017 15:19
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/298049f09347f41996c3e25b232977dc to your computer and use it in GitHub Desktop.
Save wpmudev-sls/298049f09347f41996c3e25b232977dc to your computer and use it in GitHub Desktop.
[Appointments] - Shows last scheduled user name for unavailable appointment schedule segments.
<?php
/*
Plugin Name: Show Last User in Calendar
Description: Shows last scheduled user name for unavailable appointment schedule segments.
Plugin URI: http://premium.wpmudev.org/project/appointments-plus/
Version: 1.0
AddonType: Schedule
Author: Panos @ WPMU DEV
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WPMUDEV_App_Last_Scheduled_User' ) ) {
class WPMUDEV_App_Last_Scheduled_User {
private static $_instance = null;
public static function get_instance() {
if( is_null( self::$_instance ) ){
self::$_instance = new WPMUDEV_App_Last_Scheduled_User();
}
return self::$_instance;
}
private function __construct() {
if( ! function_exists( 'appointments_delete_timetables_cache' ) ){
return;
}
appointments_delete_timetables_cache();
add_filter( 'app-schedule_cell-title', array( $this, 'process_cell_title' ), 10, 5 );
add_action( 'wp_footer', array( $this, 'footer_scripts' ), 10 );
add_action( 'wp_head', array( $this, 'head_styles' ), 10 );
}
public function process_cell_title ($title, $is_busy, $start, $end, $schedule_key) {
if ( ! $is_busy || ! is_user_logged_in() || ! current_user_can( 'manage_options' ) ){
return $title;
}
$customers = $this->_get_appointments_by_interval($start, $end, $schedule_key);
if (empty($customers)) return $title;
$names = join("\n", array_unique(wp_list_pluck($customers, 'name')));
return $names;
}
private function _get_appointments_by_interval ($start, $end, $schedule_key) {
$apps = $this->_get_appointments_for_scheduled_interval($schedule_key, $start);
if (!$apps) return false;
$ret = array();
$period = new App_Period($start, $end);
foreach ($apps as $app) {
if ($period->contains($app->start, $app->end)) $ret[] = $app;
}
return $ret;
}
private function _get_appointments_for_scheduled_interval( $schedule_key, $start ) {
$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;
}
$start = date( "Y-m-d H:i:s", $start );
$args['date_query'] = array(
array(
'field' => 'start',
'compare' => '=',
'value' => $start
)
);
$args['order'] = 'DESC';
$args['per_page'] = 1;
$apps = appointments_get_appointments( $args );
$res = array();
foreach ( $apps as $app ) {
if ( strtotime( $app->start ) > $interval_start && strtotime( $app->end ) < $interval_end ) {
$res[] = $app;
}
}
return $res;
}
public function footer_scripts(){
global $post;
if ( ! has_shortcode( $post->post_content, 'app_monthly_schedule' ) ) {
return;
}
?>
<script type="text/javascript">
(function($){
$(document).ready(function(){
$( '.app_timetable_cell.busy' ).each(function(){
const el = $(this);
let title = el.attr( 'title' );
if( ! app_isDate( title ) ){
el.append( '<div class="app_user">' + title + '</div>' );
}
});
});
})(jQuery);
let app_isDate = function(date) {
return (new Date(date) !== "Invalid Date") && !isNaN(new Date(date));
}
</script>
<?php
}
public function head_styles(){
global $post;
if ( ! has_shortcode( $post->post_content, 'app_monthly_schedule' ) ) {
return;
}
?>
<style type="text/css">
.app_timetable_cell{
min-height: 45px;
}
.app_timetable_cell .app_user{
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
</style>
<?php
}
}
add_action( 'plugins_loaded', function(){
$GLOBALS['WPMUDEV_App_Last_Scheduled_User'] = WPMUDEV_App_Last_Scheduled_User::get_instance();
}, 10 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment