Skip to content

Instantly share code, notes, and snippets.

@panoslyrakis
Last active June 10, 2017 12:22
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save panoslyrakis/825e2d6c6c191b9c5427a4b9462868a8 to your computer and use it in GitHub Desktop.
Adds an admin menu for providers to manage their appointments
<?php
/*
Plugin Name: Appointments Providers admin menu
Plugin URI: https://premium.wpmudev.org/
Description: Adds an admin menu for providers to manage and export their appointments. Allowed only to edit their appointments and set their selves as providers for new appointments.
Author: Panos Lyrakis @ WPMUDEV
Author URI: https://premium.wpmudev.org/
License: GPLv2 or later
*/
if( ! class_exists( 'App_Provider_Admin_Menu' ) ){
class App_Provider_Admin_Menu{
private static $_instance = null;
private static $_user_id = null;
public static function get_instance() {
if( is_null( self::$_instance ) ){
self::$_instance = new App_Provider_Admin_Menu();
}
return self::$_instance;
}
private function __construct(){
$this->maybe_allow_app_list();
}
public function maybe_allow_app_list(){
if( ! is_admin() || current_user_can( 'manage_options' ) ){
return;
}
self::$_user_id = get_current_user_id();
if( ! appointments_is_worker( self::$_user_id ) ){
return;
}
add_filter( 'app-capabilities-requested_capability', array( $this, 'provider_cap' ) ,10, 2 );
add_action( 'wp_loaded', array( $this, 'set_workerlist_page' ), 10 );
add_filter( 'app-export-appointment', array( $this, 'fliter_app_export' ), 10, 2 );
remove_all_actions('wp_ajax_inline_edit');
add_action( 'wp_ajax_inline_edit', array( $this, 'inline_edit' ), 10 );
//add_filter( 'app-appointment-inline_edit-result', array( $this, 'inline_edit_page' ), 10, 3 );
}
public function provider_cap( $capability, $context ){
if( ! is_admin() ){
return;
}
if( $context == 'page_appointments' ){
$capability = 'edit_published_posts';
}
return $capability;
}
public function set_workerlist_page(){
if( ! is_admin() || ! appointments_is_worker( self::$_user_id ) || current_user_can( 'manage_options' ) ){
return;
}
if( isset( $_GET['page'] ) && $_GET['page'] == 'appointments' && ( !isset( $_GET['app_provider_id'] ) || $_GET['app_provider_id'] != self::$_user_id ) ){
$full_url = $this->full_url( $_SERVER );
wp_redirect( $full_url . '&app_provider_id=' . self::$_user_id );
}
return;
}
public function fliter_app_export( $app, $raw ){
//$app->worker and $app->get_worker_id() they both return the worker's full name at this point instead of id
if( $this->get_provider_id( $app->ID ) != self::$_user_id ){
return array();
}
return $app;
}
public function inline_edit(){
$appointments = appointments();
check_ajax_referer( 'app-add-new', 'nonce' );
$app_id = absint( $_POST["app_id"] );
$app = false;
if ( $app_id ) {
$app = appointments_get_appointment( $app_id );
}
if ( $app ) {
$start_date_timestamp = date( "Y-m-d", $app->get_start_timestamp() );
$end_datetime = date_i18n( $appointments->datetime_format, strtotime( $app->end ) );
// Is this a registered user?
if ( $app->user ) {
$fields = array( 'name', 'email', 'phone', 'address', 'city' );
// Fill up the fields
foreach ( $fields as $field ) {
$value = get_user_meta( $app->user, 'app_' . $field, true );
if ( $value ) {
$app->$field = $value;
}
}
}
} else {
$app = array(
'ID' => 0,
'user' => 0,
'worker' => 0,
'location' => 0,
'service' => appointments_get_services_min_id(),
'price' => $appointments->get_price()
);
$_REQUEST['app_service_id'] = $app['service'];
$_REQUEST['app_provider_id'] = 0;
$app = new Appointments_Appointment( $app );
// Set start date as now + 60 minutes.
$current_time = current_time( 'timestamp' );
$start_date_timestamp = date( "Y-m-d", $current_time + 60 * $appointments->get_min_time() );
$end_datetime = '';
}
$column_length = isset( $_POST['col_len'] ) && is_numeric( $_POST['col_len'] ) ? absint( $_POST['col_len'] ) : 6;
$columns = isset( $_POST['columns'] ) ? absint( $_POST['columns'] ) : $column_length;
$dropdown_users = wp_dropdown_users( array(
'show_option_all' => __( 'Not registered user', 'appointments' ),
'show' => 'user_login',
'echo' => 0,
'selected' => $app->user,
'name' => 'user'
) );
$current_provider = appointments_get_worker( self::$_user_id );
$workers = array( $current_provider );
$user_fields = array( 'name', 'email', 'phone', 'address', 'city' );
$options = appointments_get_options();
// Check if an admin min time (time base) is set. @since 1.0.2
$min_time = $options["admin_min_time"] ? $options["admin_min_time"] : $appointments->get_min_time();
$min_secs = 60 * apply_filters( 'app_admin_min_time', $min_time );
$services = $current_provider->get_services(); //appointments_get_services();
ob_start();
include( appointments_plugin_dir() . 'admin/views/inline-edit.php' );
wp_send_json( array( 'result' => ob_get_clean() ) );
}
/*
* Some Helpers
*/
public function get_provider_id( $app_id ){
if( ! is_numeric( $app_id ) ){
if( $app_id instanceof Appointments_Appointment ){
$app_id = $app_id->ID;
}
else{
return false;
}
}
$app = appointments_get_appointment( $app_id );
return $app->worker;
}
public function full_url( $s, $use_forwarded_host = false ) {
return $this->url_origin( $s, $use_forwarded_host ) . $s['REQUEST_URI'];
}
public function url_origin( $s, $use_forwarded_host = false ) {
$ssl = ( ! empty( $s['HTTPS'] ) && $s['HTTPS'] == 'on' );
$sp = strtolower( $s['SERVER_PROTOCOL'] );
$protocol = substr( $sp, 0, strpos( $sp, '/' ) ) . ( ( $ssl ) ? 's' : '' );
$port = $s['SERVER_PORT'];
$port = ( ( ! $ssl && $port=='80' ) || ( $ssl && $port=='443' ) ) ? '' : ':'.$port;
$host = ( $use_forwarded_host && isset( $s['HTTP_X_FORWARDED_HOST'] ) ) ? $s['HTTP_X_FORWARDED_HOST'] : ( isset( $s['HTTP_HOST'] ) ? $s['HTTP_HOST'] : null );
$host = isset( $host ) ? $host : $s['SERVER_NAME'] . $port;
return $protocol . '://' . $host;
}
}
add_action( 'plugins_loaded', function(){
$GLOBALS['App_Provider_Admin_Menu'] = App_Provider_Admin_Menu::get_instance();
}, 10 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment