Beee

pre_save post

Jan 29th, 2017
505
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.79 KB | None | 0 0
  1. function my_pre_save_post( $post_id ) {
  2.     if ( !empty( $_POST['acf']['field_57e3ed6c92ec1'] ) ) {
  3.  
  4.         // if profile name is entered
  5.  
  6.         $entered_title      = $_POST['acf']['field_57e3ed6c92ea0'];
  7.         $cleaned_title      = preg_replace( '/[^A-Za-z0-9\-\s]/', '', $entered_title ); // Removes special chars.
  8.         $post_name          = sanitize_title( $cleaned_title );
  9.         update_field( 'field_57e3ed6c92ea0', $cleaned_title, $post_id ); // update acf field
  10.  
  11.         // check if profile image has changed
  12.         $submitted_photo    = $_POST['acf']['field_57e3ed6c9321c']; // array
  13.         $stored_photo       = get_field( 'sd_profile_photo', $post_id );
  14.  
  15.         if ( $submitted_photo == $stored_photo ) {
  16.             // do nothing
  17.         } else {
  18.             $send_mail      = true;
  19.             add_post_meta( $submitted_photo, '_image_pending', true );
  20.         }
  21.         if ( $submitted_photo ) {
  22.             // link image to post
  23.             global $wpdb;
  24.             $wpdb->update(
  25.                 $wpdb->posts,
  26.                 array(
  27.                     'post_parent' => $post_id,
  28.                 ),
  29.                 array(
  30.                     'ID' => $submitted_photo
  31.                 )
  32.             );
  33.         }
  34.  
  35.         // update post status + post title (if needed)
  36.         global $wpdb;
  37.         $wpdb->update(
  38.             $wpdb->posts,
  39.             array(
  40.                 'post_status'   => 'pending',
  41.                 'post_title'    => $cleaned_title,
  42.                 'post_name'     => $post_name
  43.             ),
  44.             array(
  45.                 'ID' => $post_id
  46.             )
  47.         );
  48.  
  49.         // check if new images are added
  50.         $submitted_gallery  = $_POST['acf']['field_57e3ed6c93236']; // array
  51.         $stored_gallery     = get_field( 'sd_gallery', $post_id );
  52.         $gallery_id_array   = array();
  53.  
  54.         if ( $stored_gallery ) {
  55.             foreach ( $stored_gallery as $gallery_item ) {
  56.                 $gallery_id_array[] = $gallery_item['ID'];
  57.             }
  58.         }
  59.  
  60.         if ( $submitted_gallery ) {
  61.             foreach ( $submitted_gallery as $gallery ) {
  62.  
  63.                 // link image to post
  64.                 $wpdb->update(
  65.                     $wpdb->posts,
  66.                     array(
  67.                         'post_parent' => $post_id,
  68.                     ),
  69.                     array(
  70.                         'ID' => $gallery
  71.                     )
  72.                 );
  73.  
  74.                 if ( in_array( $gallery, $gallery_id_array ) ) {
  75.                     // if it exists in the array, do nothing
  76.                 } else {
  77.                     // if it doesn't exist, update post meta
  78.                     add_post_meta( $gallery, '_image_pending', true );
  79.                 }
  80.             }
  81.         }
  82.  
  83.         if ( $send_mail == true ) {
  84.             $from_email     = get_field( 'sd_support_email', 'option' );
  85.             if ( ! $from_email ) { $from_email = '[email protected]'; }
  86.             $to             = get_option( 'admin_email' );
  87.             $subject        = 'Ad waiting approval';
  88.             $message        = 'An add is submitted and is <a href="#">awaiting approval</a>.';
  89.             $headers[]      = 'From: Site <' . $from_email . '>';
  90.             $headers[]      = 'Content-Type: text/html; charset=UTF-8';
  91.             wp_mail( $to, $subject, $message, $headers );
  92.         }
  93.  
  94.         clean_post_cache( $post_id );
  95.  
  96.         // Trigger all transition hooks in wordpress (in case other functions hook into those to do things.
  97.         $old_status         = $post->post_status;
  98.         $post->post_status  = 'pending';
  99.         wp_transition_post_status( 'publish', $old_status, $post );
  100.  
  101.     }
  102.     // return the same ID
  103.     return $post_id;
  104. }
  105. add_filter( 'acf/pre_save_post' , 'my_pre_save_post', 10, 1 );
Advertisement
Add Comment
Please, Sign In to add comment