Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active September 5, 2018 15:53
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/cdb549ead93fb5cf47b3ab27876cb58e to your computer and use it in GitHub Desktop.
Save wpmudev-sls/cdb549ead93fb5cf47b3ab27876cb58e to your computer and use it in GitHub Desktop.
[Appointments] - Admin Calendar
<?php
/**
* Plugin Name: [Appointments] - Admin Calendar
* Plugin URI: https://premium.wpmudev.org/
* Description: Adds the monthly calendar in admin
* Author: Panos Lyrakis @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WPMUDEV_App_Admin_Calendar' ) ) {
class WPMUDEV_App_Admin_Calendar {
private static $_instance = null;
private static $capability = null;
public static function get_instance() {
if( is_null( self::$_instance ) ){
self::$_instance = new WPMUDEV_App_Admin_Calendar();
self::$capability = 'manage_options';
}
return self::$_instance;
}
private function __construct() {
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_filter( 'app_post_confirmation_status', array( $this, 'auto_confirm' ), 20, 5 );
add_action( 'admin_head', array( $this, 'css' ) );
}
public function auto_confirm( $status, $price, $service, $worker, $user_id ) {
if (
! isset( $_REQUEST['_admin_calendar_book'] ) || ! $_REQUEST['_admin_calendar_book'] ||
! isset( $_REQUEST['_app_autoconfirm'] ) || ! $_REQUEST['_app_autoconfirm']
)
{
return $status;
}
return 'confirmed';
}
public function admin_menu() {
add_submenu_page(
'appointments',
'Calendar',
'Calendar',
self::$capability,
'app_admin_calendar',
array( $this, 'admin_calendar' )
);
}
public function admin_calendar() {
$calendar_layout = ( isset( $_GET['layout'] ) && 'weekly' == $_GET['layout'] ) ? 'weekly' : 'monthly';
$layout_str = 'weekly';
$layout_url = add_query_arg( array( 'layout' => 'weekly' ) );
$appointments = appointments();
$href = add_query_arg( array( "wcalendar"=>false, "app_provider_id" =>false ) );
$href = apply_filters( 'app_worker_href', add_query_arg( array( "wcalendar"=>$wcalendar, "app_provider_id" => "__selected_worker__" ), $href ) );
$appointments->load_scripts_styles();
if ( 'monthly' == $calendar_layout ) {
$calendar = new App_Shortcode_Monthly_Schedule();
}
else {
$calendar = new App_Shortcode_WeeklySchedule();
$layout_url = add_query_arg( array( 'layout' => 'monthly' ) );
$layout_str = 'monthly';
}
$confirmation = new App_Shortcode_Confirmation();
$providers = new App_Shortcode_ServiceProviders();
$services = new App_Shortcode_Services();
$pagination = new App_Shortcode_Pagination();
?>
<div style="clear:both;">
<table>
<tr>
<td colspan="2">
<a class="button-primary" href="<?php echo $layout_url; ?>">
<i class="dashicons dashicons-calendar"></i>
<?php echo sprintf("Switch to <strong>%s</strong> view", $layout_str ); ?>
</a>
</td>
</tr>
<tr>
<td>
<?php echo $providers->process_shortcode(); ?>
</td>
<td>
<?php echo $services->process_shortcode(); ?>
</td>
</tr>
<tr>
<td colspan="2">
<?php echo $pagination->process_shortcode(); ?>
</td>
</tr>
<tr>
<td colspan="2">
<?php echo $calendar->process_shortcode(); ?>
</td>
</tr>
<tr>
<td colspan="2">
<?php echo $confirmation->process_shortcode(); ?>
</td>
</tr>
</table>
</div>
<script type="text/javascript">
(function($){
$(document).ready(function(){
$('.app_select_workers').change(function(){
var selected_worker=$('.app_select_workers option:selected').val();
if (typeof selected_worker=='undefined' || selected_worker==null){
selected_worker=0;
}
$('.app_worker_excerpt').hide();
$('#app_worker_excerpt_'+selected_worker).show();
<?php if ( $autorefresh ) { ?>
var redirection_url='<?php echo $href; ?>'.replace(/__selected_worker__/, selected_worker) + (!!parseInt(selected_worker, 10) ? '#app_worker_excerpt_'+selected_worker : '');
window.location.href=redirection_url;
<?php } ?>
});
$('.app_workers_button').click(function(){
var selected_worker=$('.app_select_workers option:selected').val();
var redirection_url='<?php echo $href; ?>'.replace(/__selected_worker__/, selected_worker) + (!!parseInt(selected_worker, 10) ? '#app_worker_excerpt_'+selected_worker : '');
window.location.href=redirection_url;
});
WPMUDEV_App_Post_Confirm.load();
});
var WPMUDEV_App_Post_Confirm = {
load: function(){
$( document ).ajaxComplete(function( event, xhr, settings ) {
var action = WPMUDEV_App_Post_Confirm.get_action( 'action', settings.data );
var response = JSON.parse(xhr.responseText);
if( action == 'post_confirmation' && typeof response.error === 'undefined' ){
location.reload();
}
});
},
get_action: function(name, url){
if (!url){
return '';
}
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp(name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
};
// Set custom parameter to Auto-confirm appointment
$.ajaxSetup({
data:{
_admin_calendar_book:true,
_app_autoconfirm:true
}
});
})(jQuery);
</script>
<?php
}
public function css() {
if ( 'appointments_page_app_admin_calendar' != get_current_screen()->id ) {
return;
}
?>
<style type="text/css">
.appointments-list .free {
background-color: #50C878;
color: #043927;
}
.appointments-list .busy,
.appointments-list .notpossible {
background-color: #ddd;
}
</style>
<?php
}
}
if ( ! function_exists( 'wpmudev_app_admin_calendar' ) ){
function wpmudev_app_admin_calendar() {
return WPMUDEV_App_Admin_Calendar::get_instance();
}
add_action( 'plugins_loaded', 'wpmudev_app_admin_calendar' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment