Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.8 kB
9
Indexable
Never
function get_messages_number(){
	// get and display messages
    $currentuser = new WP_User(get_current_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);
        if ($last_message_author !== $currentuserlogin){
            // 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;
}