Advertisement
Guest User

How to get the `comment_post_ID`?

a guest
Jan 4th, 2022
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.57 KB | None | 0 0
  1. <?php
  2.  
  3. function checkEmailAlreadyExists( $message_id ) {
  4.     $posts_with_meta = get_posts( array(
  5.         'posts_per_page' => 1, // we only want to check if any exists, so don't need to get all of them
  6.         'post_type'      => 'faqpress-tickets',
  7.         'meta_key'       => 'ticket_id',
  8.         'meta_value'     => $message_id,
  9.         'fields'         => 'ids', // we don't need it's content, etc.
  10.     ) );
  11.  
  12.     return ( count( $posts_with_meta ) ) ? true : false;    
  13. }
  14. $total = $emails->total_msg();
  15.  
  16. for ( $j=1; $j <= $total; $j++ ) {
  17.     $mail = $emails->get( $j );
  18.     if ( !empty( $mail['header']->message_id ) && !checkEmailAlreadyExists( $mail['header']->message_id )  ) {
  19.         $post_array = array(
  20.             'post_content'  => $mail['body'],
  21.             'post_title'    => $mail['header']->subject,
  22.             'post_type'     => 'faqpress-tickets',
  23.             'post_author'   =>  ucwords(strstr($mail['header']->fromaddress, '<',true)),
  24.             'post_status'   => 'publish',
  25.             'meta_input'    => array(  
  26.                 'User'      => ucwords(strstr($mail['header']->fromaddress, '<',true)),
  27.                 'From'      => preg_replace('~[<>]~', '', strstr($mail['header']->fromaddress, '<')),
  28.                 'Email'     => $mail['header']->Date,
  29.                 'ticket_id' => $mail['header']->message_id,  
  30.             ),
  31.         );
  32.       //wp_insert_post( $post_array );
  33.     }
  34. }  
  35.  
  36. $posts = get_posts( array(
  37.     'post_type'  => 'faqpress-tickets',
  38.     'meta_key'   => 'ticket_id',
  39.     'meta_value' => $mail['header']->message_id,
  40. ) );
  41.  
  42.  
  43. if(! empty( $posts )){
  44.  for($j = 1; $j <= $total; $j++){
  45.             $mail = $emails->get($j);
  46.  
  47.     if ((!empty($mail['header']->references )) && (preg_replace('~[<]~','',strstr($mail['header']->references, '@',true))) == (preg_replace('~[<]~','',strstr($mail['header']->references, '@',true))))    
  48.                 {
  49.  
  50.                     //echo htmlentities($mail['header']->message_id);
  51.                     $comment_array = array(
  52.                         'comment_content' => $mail['body'],
  53.                         'comment_post_ID' => $posts,
  54.                     );
  55.                  //wp_insert_comment($comment_array);
  56.                 }
  57.            }
  58. }
  59.  
  60.  
  61. /***
  62.  function faqpress_author($data,$post_array){
  63.    
  64.  }
  65.  
  66.  add_filter("wp_insert_post_data", "faqpress_author", 10, 2);
  67.  
  68. $postType = "faqpress-tickets";
  69. add_filter("wp_insert_post_data" . $postType, function ($data, $postarr) {
  70.     if (0 === $postarr["ID"]) { // it's a new post
  71.         $data["post_author"] = ucwords(strstr($mail['header']->fromaddress, '<',true));
  72.     }
  73.     return $data;
  74. }, 10, 2);
  75. ***/
  76.  
  77. //Get all users and if user exists get username and email or else create new user as support customer.
  78. $user = strstr($mail['header']->fromaddress, '<',true);
  79. $email = $mail['header']->fromaddress;
  80.  
  81.  
  82. for ($j=1; $j < $total; $j++) {
  83.     $mail = $emails-> get($j);
  84.  
  85. if(email_exists($email)){
  86.     add_post_meta($post_id, 'faqpress_ticket_username', $email, True); 
  87.     add_post_meta($post_id, 'faqpress_ticket_email', $user, True);
  88.  }
  89.     else{
  90.          $userdata = array(
  91.         'user_login'    => ucwords(strstr($mail['header']->fromaddress, '<',true)),
  92.         'user_pass'     => '',
  93.         'user_nicename' => ucwords(strstr($mail['header']->fromaddress, '<',true)),  
  94.         'display_name'  => ucwords(strstr($mail['header']->fromaddress, '<',true)),
  95.         'user_email'    => strstr($mail['header']->fromaddress, '<'),
  96.         'role'          => 'support_customer',
  97.     );
  98.     wp_insert_user($userdata);
  99. }
  100. }
  101.    
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement