Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.79 KB | None | 0 0
  1. // SAVE Metabox for admin response
  2. add_action('post_updated', 'display_jwl_wp_etickets_response_meta_box_save');
  3. function display_jwl_wp_etickets_response_meta_box_save( $post_id ){
  4.    
  5.  
  6.     if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
  7.         return;
  8.     if( !isset( $_POST['jwl_wp_etickets_editor_box_nonce_check'] ) || !wp_verify_nonce( $_POST['jwl_wp_etickets_editor_box_nonce_check'], 'jwl_wp_etickets_editor_box_nonce' ) )
  9.         return;
  10.        
  11.     global $post;
  12.     $post_type_object = get_post_type_object( $post->post_type );
  13.     if ( !current_user_can( $post_type_object->cap->edit_post, $post->ID ) ) {
  14.         return;
  15.     }
  16.  
  17.     //$values = get_post_custom( $post_id );
  18.  
  19.     $editor_id = 'jwl_wp_etickets_response_editor';
  20.     $meta_key = 'jwl_wp_etickets_response_box_select';
  21.    
  22.     $content_post = get_post($post_id);
  23.     $old_content = $content_post->post_content;
  24.        
  25.     if(isset($_POST[$editor_id]) && !empty($_POST[$editor_id])) {
  26.         if ( !wp_is_post_revision( $post_id ) ){ //IMPORTANT - Can cause infinite loop otherwise : codex - wp_update_post  (see note a few lines down)
  27.        
  28.             $new_content = '<div class="eMember_admin_div"><p class="eMember_adminname_response"><strong>Admin</strong> on <strong>'.date('F j, Y @ g:i a').'</strong> said:</p>'.$_POST[$editor_id].'</div>';
  29.            
  30.             $update_content = array(
  31.                 'ID'           => $post_id,
  32.                 'post_content' => $new_content.$old_content
  33.             );
  34.             // IMPORTANT!!!!
  35.             //*****
  36.             //*****  Apparently the 'post_updated' action likes to fire on every WP process while saving the content.
  37.             //*****  Since we are also firing on 'wp_update_post'; we are getting stuck in a loop.
  38.             //*****  To get around, unhook the function before sending the revised content with 'wp_update_post'.
  39.             //*****  This will prevent clashes between 'post_updated' and 'wp_update_post" firing at the same time.
  40.             //*****  DAMN YOU WORDPRESS!!
  41.             //*****
  42.             // Unhook this function so it doesn't loop infinitely
  43.             remove_action('post_updated', 'display_jwl_wp_etickets_response_meta_box_save');
  44.            
  45.                 // Update the post, which calls save_post again
  46.                 wp_update_post( $update_content );
  47.                 // Let's check the 'ticket state', and if queued... let's update it to 'in progress'
  48.                 $terms_types = wp_get_post_terms( $post->ID, 'jwl_wp_etickets_states');
  49.                 foreach ($terms_types as $term_type) {
  50.                     if ($term_type == 'Queued' || !empty($term_type)) {
  51.                         wp_set_post_terms( $post_id, __('In Progress','wp_etickets_lang'), 'jwl_wp_etickets_states' );
  52.                     }
  53.                 }
  54.                 // Do the same for post meta for cool admin filtering
  55.                 update_post_meta( $post_id, 'jwl_wp_etickets_states_box_select', __('In Progress','wp_etickets_lang'), __('Queued','wp_etickets_lang') );
  56.                
  57.             // Re-hook this function
  58.             add_action('post_updated', 'display_jwl_wp_etickets_response_meta_box_save');
  59.         }
  60.     }  
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement