<?php /** * Handles all core messaging logic. */ class Pro_Army_Messaging { public static function get_or_create_thread( $user1_id, $user2_id ) { global $wpdb; if ( ! $user1_id || ! $user2_id || $user1_id == $user2_id ) return 0; $person1 = min($user1_id, $user2_id); $person2 = max($user1_id, $user2_id); $thread_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM {$wpdb->postmeta} pm1 INNER JOIN {$wpdb->postmeta} pm2 ON pm1.post_id = pm2.post_id WHERE (pm1.meta_key = '_participant_1' AND pm1.meta_value = %d AND pm2.meta_key = '_participant_2' AND pm2.meta_value = %d)", $person1, $person2 ) ); if ( $thread_id ) return (int) $thread_id; $new_thread_id = wp_insert_post(['post_type' => 'message_thread','post_title' => 'Conversation between user ' . $person1 . ' and ' . $person2,'post_status' => 'publish','post_author' => $user1_id]); if ( $new_thread_id && ! is_wp_error($new_thread_id) ) { update_post_meta($new_thread_id, '_participant_1', $person1); update_post_meta($new_thread_id, '_participant_2', $person2); return $new_thread_id; } return 0; } }