Created
February 16, 2017 17:01
-
-
Save panoslyrakis/59d4e91c176f9ece823289ce5d6e9cd0 to your computer and use it in GitHub Desktop.
Appointments+ Cancel Link
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Appointments+ Cancel Link | |
Description: Adds a cancel link in notifications. *Please read the instructions in plugin file before usage | |
Plugin URI: http://premium.wpmudev.org/ | |
Version: 1.0.0 | |
Author: Panos Lyrakis | |
Author URI: https://premium.wpmudev.org/profile/panoskatws | |
*/ | |
/** | |
* ========== INSTRUCTIONS ========== | |
* | |
* 1. In your wp-config file add: define( 'APP_ALLOW_CANCEL', true ); | |
* 2. Create a new page in your admin and add this shortcode: [app_cancel_page] | |
* 3. Get the page id that you add the shortcode and replace 'null' at line 45 of this file with that id | |
* 4. Use the APP_RM_LINK placeholder in your notification messages instead of CANCEL | |
*/ | |
if( ! class_exists('WPMUDEV_APP_Notification_Cancel_Link') ){ | |
class WPMUDEV_APP_Notification_Cancel_Link{ | |
private static $_instance = null; | |
public static $post_type = null; | |
var $cancel_page; | |
public static function get_instance() { | |
if ( is_null( self::$_instance ) ) { | |
self::$_instance = new WPMUDEV_APP_Notification_Cancel_Link(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() { | |
//Please replace "null" with the id of your cancelation page in following line: | |
$this->cancel_page = null; | |
if( $this->cancel_page == null ) return false; | |
add_filter( 'app_confirmation_message', array( $this, 'add_cancel_link' ), 10 , 3 ); | |
add_filter( 'app_reminder_message', array( $this, 'add_cancel_link' ), 10 , 3 ); | |
add_filter( 'query_vars', array( $this, 'query_vars' ), 10 ); | |
add_shortcode( 'app_cancel_page', array( $this, 'cancel_page_sh' ) ); | |
} | |
public function add_cancel_link( $text, $r, $app_id ){ | |
global $appointments; | |
if ( defined( "APP_ALLOW_CANCEL" ) && APP_ALLOW_CANCEL ) { | |
$app = appointments_get_appointment( $app_id ); | |
if ( $app ) | |
return str_replace( 'APP_RM_LINK', add_query_arg( array( 'app_cancel'=>1, 'app_id'=>$app_id, 'app_nonce'=>md5( $app_id . $appointments->salt . strtotime( $app->created ) ) ), get_permalink( $this->cancel_page ) ), $text); | |
else | |
return str_replace( 'APP_RM_LINK', '', $text ); | |
} | |
else | |
return str_replace( 'APP_RM_LINK', '', $text ); | |
} | |
public function cancel_page_sh( $atts ){ | |
global $appointments, $post; | |
if( ! is_object( $post ) || ! isset( $post->ID ) ){ | |
return false; | |
} | |
if ( ! defined( "APP_ALLOW_CANCEL" ) || ! APP_ALLOW_CANCEL ){ | |
return 'This action is not allowed! Please contact site administrator'; | |
} | |
if( get_query_var( 'app_cancel' ) == '' | |
|| get_query_var( 'app_cancel' ) != 1 | |
|| get_query_var( 'app_id' ) == '' | |
|| get_query_var( 'app_nonce' ) == '' | |
){ | |
return 'Cheating huh?'; | |
} | |
if( ! is_user_logged_in() ){ | |
$redirect = add_query_arg( array( | |
'app_cancel'=>1, | |
'app_id'=>get_query_var( 'app_id' ), | |
'app_nonce'=> get_query_var( 'app_nonce' ) | |
), get_permalink( $this->cancel_page ) ); | |
$login_page = wp_login_url( $redirect ); | |
return sprintf( 'You need to be logged in to cancel this Appointment. Please log in <a href="%s">here</a> ', $login_page ); | |
} | |
$app = appointments_get_appointment( get_query_var( 'app_id' ) ); | |
if( get_current_user_id() != $app->user ){ | |
return 'You are not allowed to cancel this Appointment. Please contact site administrator.'; | |
} | |
if ( appointments_update_appointment_status( $app->ID, 'removed' ) ) { | |
$appointments->log( sprintf( __('Client %s cancelled appointment with ID: %s','appointments'), $appointments->get_client_name( $app->ID ), $app->ID ) ); | |
appointments_send_cancel_notification( $app->ID ); | |
do_action('app-appointments-appointment_cancelled', $app->ID); | |
return 'Appointment has been canceled succesfully!'; | |
} | |
do_action( 'app_cancel_failed', $app->ID ); | |
} | |
public function query_vars( $vars ){ | |
$vars[] = "app_cancel"; | |
$vars[] = "app_id"; | |
$vars[] = "app_nonce"; | |
return $vars; | |
} | |
} | |
add_action( 'plugins_loaded', function(){ | |
WPMUDEV_APP_Notification_Cancel_Link::get_instance(); | |
},10 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Adds a cancel link in notifications. *Please read the instructions in plugin file before usage
/**
*/