Advertisement
julien731

Perform Action on Ticket Status Change

Jun 30th, 2016
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.81 KB | None | 0 0
  1. <?php
  2. add_filter( 'wp_insert_post_data', 'wpas_custom_status_change_notification', 10, 2 );
  3. /**
  4.  * Check if the ticket status has changed and, if so, maybe send an e-mail notification if the new status required one.
  5.  *
  6.  * @since  3.0.0
  7.  *
  8.  * @param  array $data    Post data
  9.  * @param  array $postarr Original post data
  10.  *
  11.  * @return array          Modified post data for insertion
  12.  */
  13. function wpas_custom_status_change_notification( $data, $postarr ) {
  14.  
  15.     // Make sure we only work on tickets and not other post types
  16.     if ( ! isset( $data['post_type'] ) || 'ticket' !== $data['post_type'] ) {
  17.         return $data;
  18.     }
  19.  
  20.     // We're not interested in tickets being trashed
  21.     if ( 'trash' === $data['post_status'] ) {
  22.         return $data;
  23.     }
  24.  
  25.     // Not interested in auto-drafts either
  26.     if ( 'auto-draft' === $data['post_status'] ) {
  27.         return $data;
  28.     }
  29.  
  30.     global $current_user;
  31.  
  32.     // Make sure the current user has the capability to edit a ticket
  33.     if ( ! user_can( $current_user->ID, 'edit_ticket' ) ) {
  34.         return $data;
  35.     }
  36.  
  37.     // Get the registered custom status
  38.     $status = wpas_get_post_status();
  39.  
  40.     // Make sure the new status is a registered one
  41.     if ( array_key_exists( $_POST['post_status_override'], $status ) ) {
  42.  
  43.         // We only want to send the notification if the status has changed and if a new reply is being posted
  44.         if ( $postarr['original_post_status'] !== $_POST['post_status_override'] && isset( $_POST['wpas_post_parent'] ) ) {
  45.            
  46.             switch ( $_POST['post_status_override'] ) {
  47.  
  48.                 case 'status1':
  49.                     // Do something when the new status is "status1"
  50.                     break;
  51.  
  52.                 case 'status2':
  53.                     // Do something when the new status is "status2"
  54.                     break;
  55.  
  56.             }
  57.  
  58.             // If you want to perform the same action whatever the new status then remove the entire switch() block and add your action here
  59.  
  60.         }
  61.     }
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement