Untitled

mail@pastecode.io avatar
unknown
plain_text
8 months ago
1.8 kB
3
Indexable
Never
function kraftdeck_get_customer_unread_messages_count($customer_id) {
	$user_id = $customer_id;
	$currentuser = new WP_User($user_id);
	$currentuserlogin = $currentuser -> user_login;
	$messages = get_posts(
	            array( 
	                'post_type' => 'marketking_message', // only conversations
	                'post_status' => 'publish',
	                'numberposts' => -1,
	                'fields' => 'ids',
	                'meta_query'=> array(   // only the specific user's conversations
	                    'relation' => 'OR',
	                    array(
	                        'key' => 'marketking_message_user',
	                        'value' => $currentuserlogin, 
	                    ),
	                    array(
	                        'key' => 'marketking_message_message_1_author',
	                        'value' => $currentuserlogin, 
	                    )


	                )
	            )
	        );
	// check how many are unread
	$unread_msg = 0;
	foreach ($messages as $message){
	    // check that last msg is not current user
	    $nr_messages = get_post_meta ($message, 'marketking_message_messages_number', true);
	    $last_message_author = get_post_meta ($message, 'marketking_message_message_'.$nr_messages.'_author', true);
	    // chek if last read time is lower than last msg time
	    $last_read_time = get_user_meta($user_id,'marketking_message_last_read_'.$message, true);
	    if (!empty($last_read_time)){
	        $last_message_time = get_post_meta ($message, 'marketking_message_message_'.$nr_messages.'_time', true);
	        if (floatval($last_read_time) < floatval($last_message_time)){
	            $unread_msg++;
	        }
	    } else {
	        $unread_msg++;
	    }
	}

	return $unread_msg;
}
Leave a Comment