0

Really rough time trying to wrap my head around how to fix this issue. Users can private message each other on my site. As shown in the image below, you can see that I display the message author on the left, and the message recipient on the bottom right.

The first message (at the very bottom) sent by the original message author displays the original message recipient (bottom right corner) properly as it should.

After the recipient responds to the initial message, the new reply is sent back to the original sender, who is now the "recipient" and it displays her metadata in bottom right (avatar + username).

Here is where the issue arises. Upon the original sender's receipt of the original recipient's response, the original recipient's meta data is still displaying in the bottom right. You can see this in 2nd message from the bottom (and the issue continues as long as the two users are still messaging within the same parent thread, as shown).

I have crossed out what should not be displayed with a red X. I only want the recipient of each individual message to display in the bottom right, but I think that since the code is considering the messages as a 'thread' per se, it wants to show "all recipients" of that message 'thread', instead of just showing the recipient of each new message sent.

enter image description here

Here is the code that is displaying the recipient avatar + username. I am completely at a dead end right now. Anyone have any ideas on how to solve this?

function get_table_columns() {
   $columns = array(
        'recipientname' => __('RecipientsName', 'front-end-pm'),
        'recipientimage' => __('RecipientsImage', 'front-end-pm')
        );
return apply_filters('fep_message_table_columns', $columns );
}


function get_column_content($column) {
   $recipients = get_post_meta( get_the_ID(), '_recipients' );
   $name = array();
   $image = array();

  foreach( $recipients as $recipient ) {
    $name[] = fep_get_userdata( $recipient, 'display_name', 'id' );
    $image[] = get_avatar ( $recipient, 'id' );
}

switch( $column ) {

            case 'recipientname' :
            ?></a></div>
            <div class="recipentusername"><a href="<?php echo um_user_profile_url(); ?>" target='_blank'><?php _e("", 'front-end-pm'); ?> <?php echo implode( $name );
            break;

            case 'recipientimage' :
                    ?></a></div><div class="recipientavatar"> <a href="<?php echo um_user_profile_url(); ?>" target='_blank'> <?php echo implode( $image );
            break;

Here is the code that is storing _recipients metakey, which stores the user ID of all message recipients in the database.

// Insert the message into the database
$message_id = wp_insert_post( $post );

if( ! $message_id || is_wp_error( $message_id ) ) {
    return false;
}
$inserted_message = get_post( $message_id );

if( ! empty($message['message_to_id'] ) ) { //FRONT END message_to return id of participants
    if( is_array( $message['message_to_id'] ) ) {
        foreach( $message['message_to_id'] as $participant ) {
            add_post_meta( $message_id, '_participants', $participant );
            add_post_meta( $message_id, '_recipients', $participant );
        }
    } else {
        add_post_meta( $message_id, '_participants', $message['message_to_id'] );
        add_post_meta( $message_id, '_recipients', $message['message_to_id'] );
    }
}
add_post_meta( $message_id, '_participants', $inserted_message->post_author );

if( $inserted_message->post_parent ) {

    $participants = get_post_meta( $inserted_message->post_parent, '_recipients' );

    if( $participants && is_array( $participants ) )
    {
        foreach( $participants as $participant ) 
        {
            delete_post_meta( $inserted_message->post_parent, '_fep_parent_read_by_'. $participant );
            delete_user_meta( $participant, '_fep_user_message_count' );
        }
    }

            if( $recipients && is_array( $recipients ) )
    {
        foreach( $recipients as $recipient ) 
        {
            delete_post_meta( $inserted_message->post_parent, '_fep_parent_read_by_'. $recipient );
            delete_user_meta( $recipient, '_fep_user_message_count' );
        }
    }
    fep_make_read( true, $inserted_message->post_parent, $inserted_message->post_author );

} else {
    $participants = get_post_meta( $message_id, '_participants' );

    if( $participants && is_array( $participants ) )
    {
        foreach( $participants as $participant ) 
        {
            delete_user_meta( $participant, '_fep_user_message_count' );
        }
    }

    $recipients = get_post_meta( $message_id, '_recipients' );

    if( $recipients && is_array( $recipients ) )
    {
        foreach( $recipients as $recipient ) 
        {
            delete_user_meta( $recipient, '_fep_user_message_count' );
        }
    }
}

fep_make_read( true, $message_id, $inserted_message->post_author );

 do_action('fep_action_message_after_send', $message_id, $message, $inserted_message );

return $message_id;

Here is the code where message_to_id is defined

   function fep_send_message( $message = null, $override = array() )
{
if( null === $message ) {
    $message = $_POST;
}

if( ! empty($message['fep_parent_id'] ) ) {
    $message['post_status'] = fep_get_option('reply_post_status','publish');
    $message['message_title'] = __('RE:', 'front-end-pm'). ' ' . get_the_title( $message['fep_parent_id'] );
    $message['message_to_id'] = get_post_meta( $message['fep_parent_id'], '_participants' );
    $message['post_parent'] = absint( $message['fep_parent_id'] );
} else {
    $message['post_status'] = fep_get_option('parent_post_status','publish');
    $message['post_parent'] = 0;
}

$message = apply_filters('fep_filter_message_before_send', $message );

if( empty($message['message_title']) || empty($message['message_content']) ) {
    return false;
}
// Create post array
$post = array(
    'post_title'    => $message['message_title'],
    'post_content'  => $message['message_content'],
    'post_status'   => $message['post_status'],
    'post_parent'   => $message['post_parent'],
    'post_type'     => 'fep_message'
);

if( $override && is_array( $override ) ) {
    $post = wp_parse_args( $override, $post );
}

$post = apply_filters('fep_filter_message_after_override', $post );
  • $recipients and $participants are define with the same méta Key is it normal ? – Benoti Oct 19 '16 at 16:38
  • It is normal, but a bad choice of variable names :) Sorry. I just updated with restructured code to make it cleaner and easier to understand. – kosmicbird Oct 19 '16 at 17:06
  • You need to add a statement to exclude the user ID of the recipient message. What are you storing in _recipients meta ? – Benoti Oct 19 '16 at 17:13
  • The _recipients meta stores the user ID of all recipients of a message. I have updated with the code that stores this in the database. – kosmicbird Oct 19 '16 at 17:28
  • The problem is I don't want to completely exclude message recipients. But I need to somehow exclude any message recipient who is not the recipient of the current/new message. No idea how to accomplish that. :/ – kosmicbird Oct 19 '16 at 17:29
0

As I see it, you could replace some things in the switch. You need to open and close , tag in the same case rule, do this avoid a lot error.

Like this

switch( $column ) {

        case 'recipientname' :
          <div class="recipentusername"><a href="<?php echo um_user_profile_url(); ?>" target='_blank'><?php _e("", 'front-end-pm'); ?> <?php echo implode( $name );?> </a></div>
         <?php 
        break;

        case 'recipientimage' :
                ?></a></div><div class="recipientavatar"> <a href="<?php echo um_user_profile_url(); ?>" target='_blank'> <?php echo implode( $image );
        break;
}

to filter your column response you don't need to use in_array, but don't forget to have a look to this function :-)

function get_column_content($column) {
   $recipients = get_post_meta( get_the_ID(), '_recipients' );
   $name = array();
   $image = array();

  foreach( $recipients as $recipient ) {
     if($recipient != get_the_ID(){
         $name[] = fep_get_userdata( $recipient, 'display_name', 'id' );
         $image[] = get_avatar ( $recipient, 'id' );
     }
  }
}
  • I will still check that function definitely :) but arg..... I tried with the modifications and it is still not excluding the parent recipient. So frustrating! – kosmicbird Oct 19 '16 at 19:17
  • Yay!!! I was able to get it working by using your suggestion, but I had to switch != get_the_ID()){ with != get_the_author_id()){ Thank you so much for your help! =)) @Benoti you are the best, merci!! – kosmicbird Oct 19 '16 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.