Advertisement
eventsmanager

Custom Email Status

May 30th, 2022 (edited)
1,770
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.77 KB | None | 0 0
  1. <?php
  2. /**
  3. Adds a custom email status, in this example we add the email status 20 and 21. This will integrate with default and custom emails (including gateways), you need to define the default email template below, custom emails can be defined when editing events/gateways.
  4.  
  5. For installation instructions, see here - http://wp-events-plugin.com/tutorials/how-to-safely-add-php-code-to-wordpress/
  6.  
  7. We recommend you copy this whole snippet into a new PHP file and upload it to the mu-folders, rather than including it into your functions.php. Alternatively, include it within functions.php as a separate file in your theme.
  8.  
  9. NOTE : This snippet assumes the booking_status field is int(2), as of Events Manager 6.0. Change this in your DB table if necessary or use status 9 (we'll be using lower single digit statuses over time). Also, this will need EM Pro version 2.7.1 or higher to work with custom emails.
  10. */
  11.  
  12. class EM_My_Custom_Booking_Status {
  13.    
  14.     /**
  15.      * Add your custom statuses here, one ore more as per the template below, you can add custom statuses in the bookings table filters as per comments inside array
  16.      * @var array[]
  17.      */
  18.     public static $custom_statuses = array(
  19.         20 => array(
  20.                 'label' => 'Some Status',
  21.                 'bookings-table-statuses' => array(
  22.                     'needs-attention', // this adds status 20 bookings to the 'needs attention' filter option in bookings table
  23.                     'custom-status', // this is a custom filter option defined below in $custom_bookings_table_statuses
  24.                 ),
  25.                 //Generic set of status and default emails that are used if no custom mail defined, you could make this more elaborate accounting for admin/user emails and gateways. Note that for translations, you'd need to define this in init()
  26.                 'emails' => array(
  27.                     'admin' => array('subject'=>'Some Custom Status Email', 'message'=> 'User has booked this: #_BOOKINGSUMMARY'),
  28.                     'user' => array('subject' => '#_EVENT - Custom Status Email', 'message' => 'Your booking now has a custom status: #_BOOKINGSUMMARY'),
  29.                 ),
  30.             ),
  31.         21 => array(
  32.                 'label' => 'Some Other  Status',
  33.                 'bookings-table-statuses' => array( 'custom-status' ),
  34.                 'emails' => array(
  35.                     'admin' => array('subject'=>'Some Other Custom Status Email', 'message'=> 'User has booked this: #_BOOKINGSUMMARY'),
  36.                     'user' => array('subject' => '#_EVENT - Custom Status Email', 'message' => 'Your booking now has a custom status: #_BOOKINGSUMMARY'),
  37.                 )
  38.             ),
  39.     );
  40.    
  41.     /**
  42.      * You can add custom statuses here that appear in the bookings table admin filters
  43.      * @var array[]
  44.      */
  45.     public static $custom_bookings_table_statuses = array(
  46.         'custom-status' => array('label'=> 'Custom Statuses'),
  47.     );
  48.    
  49.     public static function init(){
  50.         add_action('em_booking', 'EM_My_Custom_Booking_Status::em_booking', 100, 1);
  51.         add_action('em_bookings_table','EM_My_Custom_Booking_Status::em_bookings_table',100,1);
  52.         // default email, get in there fast so it can be overridden
  53.         add_filter('em_booking_email_messages', 'EM_My_Custom_Booking_Status::em_booking_email_messages', 1, 2);
  54.         // custom emails - here we're adding it to all types, admin and gateways, you can be more specific by checking the extra 2 args, $type and $gateway in the function
  55.         add_filter('em_custom_emails_admin_default_emails_subgroup', 'EM_My_Custom_Booking_Status::custom_emails', 10, 1);
  56.         // custom emails - here we're adding it to all types, admin and gateways, you can be more specific by checking the extra 3 args, $multiple_bookings, $type and $gateway in the function
  57.         add_filter('em_custom_emails_admin_default_email_values', 'EM_My_Custom_Booking_Status::em_custom_emails_admin_default_email_values', 10, 4);
  58.     }
  59.    
  60.     public static function em_booking( $EM_Booking ){
  61.         foreach( static::$custom_statuses as $status => $data ){
  62.             $EM_Booking->status_array[$status] = $data['label'];
  63.         }
  64.     }
  65.    
  66.     public static function em_bookings_table( $EM_Bookings_Table ){
  67.         foreach( static::$custom_statuses as $status => $data ) {
  68.             if( !empty($data['bookings-table-statuses']) ){
  69.                 foreach( $data['bookings-table-statuses'] as $bt_status ){
  70.                     if( empty($EM_Bookings_Table->statuses[$bt_status]) && !empty(static::$custom_bookings_table_statuses[$bt_status]) ){
  71.                         $EM_Bookings_Table->statuses[$bt_status] = static::$custom_bookings_table_statuses[$bt_status];
  72.                     }
  73.                     $EM_Bookings_Table->statuses[$bt_status]['search'][] = $status;
  74.                 }
  75.             }
  76.         }
  77.     }
  78.    
  79.     public static function em_booking_email_messages( $msg, $EM_Booking ){
  80.         if( array_key_exists($EM_Booking->booking_status, static::$custom_statuses) ){
  81.             if( !empty(static::$custom_statuses[$EM_Booking->booking_status]['emails']) ){
  82.                 $email = static::$custom_statuses[$EM_Booking->booking_status]['emails'];
  83.                 $msg = array(
  84.                     'admin' => array(
  85.                         'subject' => $email['admin']['subject'],
  86.                         'body' => $email['admin']['message'],
  87.                     ),
  88.                     'user' => array(
  89.                         'subject' => $email['user']['subject'],
  90.                         'body' => $email['user']['message'],
  91.                     )
  92.                 );
  93.             }
  94.         }
  95.         return $msg;
  96.     }
  97.    
  98.     public static function custom_emails( $emails ){
  99.         foreach( static::$custom_statuses as $status => $data ){
  100.             $emails[$status] = array('title'=> $data['label']);
  101.         }
  102.         return $emails;
  103.     }
  104.    
  105.     public static function em_custom_emails_admin_default_email_values( $emails, $multiple_bookings, $type, $EM_Gateway = null ){
  106.         foreach( static::$custom_statuses as $status => $data ){
  107.             if( !empty(static::$custom_statuses[$status]['emails'][$type]) ) {
  108.                 $emails[$status] = static::$custom_statuses[$status]['emails'][$type];
  109.                 if( !isset(static::$custom_statuses[$status]['emails'][$type]['status']) ){
  110.                     $emails[$status]['status'] = 0;
  111.                 }
  112.             }else{
  113.                 $emails[$status] = array('subject'=>'', 'message'=> '', 'status' => 0);
  114.             }
  115.         }
  116.         return $emails;
  117.     }
  118. }
  119. EM_My_Custom_Booking_Status::init();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement