| 1 |
<?php |
| 2 |
/** |
| 3 |
* This is the Direct Messages view. |
| 4 |
* |
| 5 |
* @version 1.0 |
| 6 |
* @package Friends |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! empty( $friends_args ) && is_array( $friends_args ) ) { |
| 10 |
$args = array_merge( $friends_args, $args ); |
| 11 |
} |
| 12 |
|
| 13 |
$args['title'] = __( 'Direct Messages', 'friends' ); |
| 14 |
$args['no-bottom-margin'] = true; |
| 15 |
$args['hide-mobile-search'] = true; |
| 16 |
|
| 17 |
$time_format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' ); |
| 18 |
if ( false === strpos( $time_format, ':s' ) ) { |
| 19 |
$time_format = str_replace( 'H:i', 'H:i:s', $time_format ); |
| 20 |
$time_format = str_replace( 'g:i', 'g:i:s', $time_format ); |
| 21 |
} |
| 22 |
|
| 23 |
$get_message_friend_user = function ( $message ) { |
| 24 |
$terms = wp_get_object_terms( $message->ID, Friends\Messages::TAXONOMY ); |
| 25 |
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) { |
| 26 |
$term = reset( $terms ); |
| 27 |
$user = Friends\User::get_user_by_id( absint( $term->slug ) ); |
| 28 |
if ( $user ) { |
| 29 |
return $user; |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
return Friends\User::get_post_author( $message ); |
| 34 |
}; |
| 35 |
|
| 36 |
$top_level_messages = get_posts( |
| 37 |
array( |
| 38 |
'post_type' => Friends\Messages::CPT, |
| 39 |
'post_parent' => 0, |
| 40 |
'post_status' => array( 'friends_read', 'friends_unread' ), |
| 41 |
'posts_per_page' => -1, |
| 42 |
) |
| 43 |
); |
| 44 |
|
| 45 |
$conversation_rows = array(); |
| 46 |
foreach ( $top_level_messages as $top_level_message ) { |
| 47 |
$thread_messages = get_posts( |
| 48 |
array( |
| 49 |
'post_parent' => $top_level_message->ID, |
| 50 |
'post_type' => Friends\Messages::CPT, |
| 51 |
'post_status' => array( 'friends_read', 'friends_unread' ), |
| 52 |
'order' => 'ASC', |
| 53 |
'orderby' => 'date', |
| 54 |
'posts_per_page' => -1, |
| 55 |
) |
| 56 |
); |
| 57 |
array_unshift( $thread_messages, $top_level_message ); |
| 58 |
|
| 59 |
$latest_message = $top_level_message; |
| 60 |
$latest_message_time = get_post_modified_time( 'U', true, $top_level_message ); |
| 61 |
$unread_count = 0; |
| 62 |
foreach ( $thread_messages as $thread_message ) { |
| 63 |
if ( 'friends_unread' === get_post_status( $thread_message ) ) { |
| 64 |
++$unread_count; |
| 65 |
} |
| 66 |
|
| 67 |
$message_time = get_post_modified_time( 'U', true, $thread_message ); |
| 68 |
if ( $message_time > $latest_message_time ) { |
| 69 |
$latest_message = $thread_message; |
| 70 |
$latest_message_time = $message_time; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
$friend_user = $get_message_friend_user( $top_level_message ); |
| 75 |
if ( ! $friend_user || is_wp_error( $friend_user ) || get_current_user_id() === intval( $friend_user->ID ) ) { |
| 76 |
foreach ( $thread_messages as $thread_message ) { |
| 77 |
$thread_author = Friends\User::get_post_author( $thread_message ); |
| 78 |
if ( $thread_author && ! is_wp_error( $thread_author ) && get_current_user_id() !== intval( $thread_author->ID ) ) { |
| 79 |
$friend_user = $thread_author; |
| 80 |
break; |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
$message_preview = get_the_title( $latest_message ); |
| 86 |
if ( ! $message_preview ) { |
| 87 |
$message_preview = wp_strip_all_tags( get_the_excerpt( $latest_message ) ); |
| 88 |
} |
| 89 |
|
| 90 |
$latest_delivery = null; |
| 91 |
if ( intval( $latest_message->post_author ) === get_current_user_id() && class_exists( 'Friends\Feed_Parser_ActivityPub' ) ) { |
| 92 |
$latest_delivery = Friends\Feed_Parser_ActivityPub::get_direct_message_delivery_status( $latest_message ); |
| 93 |
} |
| 94 |
|
| 95 |
$conversation_rows[] = array( |
| 96 |
'id' => $top_level_message->ID, |
| 97 |
'friend_user' => $friend_user, |
| 98 |
'messages' => $thread_messages, |
| 99 |
'latest' => $latest_message, |
| 100 |
'latest_delivery' => $latest_delivery, |
| 101 |
'latest_time' => $latest_message_time, |
| 102 |
'preview' => $message_preview, |
| 103 |
'unread_count' => $unread_count, |
| 104 |
'root_message' => $top_level_message, |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
usort( |
| 109 |
$conversation_rows, |
| 110 |
function ( $a, $b ) { |
| 111 |
return $b['latest_time'] - $a['latest_time']; |
| 112 |
} |
| 113 |
); |
| 114 |
|
| 115 |
$selected_conversation_id = isset( $_GET['conversation'] ) ? absint( $_GET['conversation'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 116 |
$selected_conversation = null; |
| 117 |
foreach ( $conversation_rows as $conversation_row ) { |
| 118 |
if ( ! $selected_conversation || $conversation_row['id'] === $selected_conversation_id ) { |
| 119 |
$selected_conversation = $conversation_row; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
Friends\Friends::template_loader()->get_template_part( 'frontend/header', null, $args ); |
| 124 |
?> |
| 125 |
<section class="friends-dm-view" aria-label="<?php esc_attr_e( 'Direct Messages', 'friends' ); ?>"> |
| 126 |
<?php if ( empty( $conversation_rows ) ) : ?> |
| 127 |
<div class="friends-dm-empty"> |
| 128 |
<h3><?php esc_html_e( 'No direct messages yet.', 'friends' ); ?></h3> |
| 129 |
<p><?php esc_html_e( 'Open a friend profile to start a conversation.', 'friends' ); ?></p> |
| 130 |
</div> |
| 131 |
<?php else : ?> |
| 132 |
<nav id="friends-dm-conversations" class="friends-dm-sidebar" aria-label="<?php esc_attr_e( 'Conversations', 'friends' ); ?>"> |
| 133 |
<?php foreach ( $conversation_rows as $conversation_row ) : ?> |
| 134 |
<?php |
| 135 |
$friend_user = $conversation_row['friend_user']; |
| 136 |
$is_selected = $selected_conversation && $conversation_row['id'] === $selected_conversation['id']; |
| 137 |
$item_url = add_query_arg( 'conversation', $conversation_row['id'], home_url( '/friends/messages/' ) ) . '#friends-dm-conversation-' . $conversation_row['id']; |
| 138 |
?> |
| 139 |
<a class="friends-dm-conversation<?php echo $is_selected ? ' is-selected' : ''; ?><?php echo $conversation_row['unread_count'] ? ' is-unread' : ''; ?>" href="<?php echo esc_url( $item_url ); ?>"> |
| 140 |
<?php if ( $friend_user && ! is_wp_error( $friend_user ) && $friend_user->get_avatar_url() ) : ?> |
| 141 |
<img class="avatar" src="<?php echo esc_url( $friend_user->get_avatar_url() ); ?>" alt="" width="40" height="40"> |
| 142 |
<?php else : ?> |
| 143 |
<?php echo get_avatar( 0, 40, '', '', array( 'class' => 'avatar' ) ); ?> |
| 144 |
<?php endif; ?> |
| 145 |
<span class="friends-dm-conversation-main"> |
| 146 |
<span class="friends-dm-conversation-name"> |
| 147 |
<?php echo esc_html( $friend_user && ! is_wp_error( $friend_user ) ? $friend_user->display_name : __( 'Unknown sender', 'friends' ) ); ?> |
| 148 |
</span> |
| 149 |
<span class="friends-dm-conversation-preview"><?php echo esc_html( $conversation_row['preview'] ); ?></span> |
| 150 |
</span> |
| 151 |
<span class="friends-dm-conversation-meta"> |
| 152 |
<time data-friends-relative-time="<?php echo esc_attr( $conversation_row['latest_time'] ); ?>" title="<?php echo esc_attr( date_i18n( $time_format, $conversation_row['latest_time'] ) ); ?>"> |
| 153 |
<?php echo esc_html( human_time_diff( $conversation_row['latest_time'] ) ); ?> |
| 154 |
</time> |
| 155 |
<?php if ( $conversation_row['latest_delivery'] ) : ?> |
| 156 |
<span class="friends-dm-delivery-status friends-dm-delivery-icon is-<?php echo esc_attr( $conversation_row['latest_delivery']['status'] ); ?>" data-delivery-message-id="<?php echo esc_attr( $conversation_row['latest']->ID ); ?>" title="<?php echo esc_attr( $conversation_row['latest_delivery']['title'] ); ?>" aria-label="<?php echo esc_attr( $conversation_row['latest_delivery']['label'] ); ?>"></span> |
| 157 |
<?php endif; ?> |
| 158 |
<?php if ( $conversation_row['unread_count'] ) : ?> |
| 159 |
<span class="friends-dm-unread-count"><?php echo esc_html( number_format_i18n( $conversation_row['unread_count'] ) ); ?></span> |
| 160 |
<?php endif; ?> |
| 161 |
</span> |
| 162 |
</a> |
| 163 |
<?php endforeach; ?> |
| 164 |
</nav> |
| 165 |
|
| 166 |
<?php |
| 167 |
$selected_friend_user = $selected_conversation['friend_user']; |
| 168 |
$selected_url = add_query_arg( 'conversation', $selected_conversation['id'], home_url( '/friends/messages/' ) ); |
| 169 |
?> |
| 170 |
<article id="friends-dm-conversation-<?php echo esc_attr( $selected_conversation['id'] ); ?>" class="friends-dm-thread" data-id="<?php echo esc_attr( $selected_conversation['id'] ); ?>" data-nonce="<?php echo esc_attr( wp_create_nonce( 'friends-mark-read' ) ); ?>" data-unread="<?php echo esc_attr( $selected_conversation['unread_count'] ? '1' : '0' ); ?>"> |
| 171 |
<header class="friends-dm-thread-header"> |
| 172 |
<a class="friends-dm-back" href="#friends-dm-conversations" aria-label="<?php esc_attr_e( 'Back to conversations', 'friends' ); ?>"> |
| 173 |
<span aria-hidden="true">←</span> |
| 174 |
</a> |
| 175 |
<?php if ( $selected_friend_user && ! is_wp_error( $selected_friend_user ) && $selected_friend_user->get_avatar_url() ) : ?> |
| 176 |
<img class="avatar" src="<?php echo esc_url( $selected_friend_user->get_avatar_url() ); ?>" alt="" width="44" height="44"> |
| 177 |
<?php else : ?> |
| 178 |
<?php echo get_avatar( 0, 44, '', '', array( 'class' => 'avatar' ) ); ?> |
| 179 |
<?php endif; ?> |
| 180 |
<div> |
| 181 |
<h3><?php echo esc_html( $selected_friend_user && ! is_wp_error( $selected_friend_user ) ? $selected_friend_user->display_name : __( 'Unknown sender', 'friends' ) ); ?></h3> |
| 182 |
<?php if ( $selected_friend_user && ! is_wp_error( $selected_friend_user ) ) : ?> |
| 183 |
<a href="<?php echo esc_url( $selected_friend_user->get_local_friends_page_url() ); ?>"><?php esc_html_e( 'View profile', 'friends' ); ?></a> |
| 184 |
<?php endif; ?> |
| 185 |
</div> |
| 186 |
</header> |
| 187 |
|
| 188 |
<div class="friends-dm-messages"> |
| 189 |
<?php $previous_message_author_key = null; ?> |
| 190 |
<?php foreach ( $selected_conversation['messages'] as $message ) : ?> |
| 191 |
<?php |
| 192 |
$message_author = Friends\User::get_post_author( $message ); |
| 193 |
$is_own_message = $message_author && ! is_wp_error( $message_author ) && get_current_user_id() === intval( $message_author->ID ); |
| 194 |
$author_name = $message_author && ! is_wp_error( $message_author ) ? $message_author->display_name : __( 'Unknown sender', 'friends' ); |
| 195 |
$post_time = get_post_modified_time( 'U', true, $message ); |
| 196 |
$author_key = $message_author && ! is_wp_error( $message_author ) ? 'user-' . $message_author->ID : 'unknown'; |
| 197 |
$is_consecutive = $author_key === $previous_message_author_key; |
| 198 |
$delivery = null; |
| 199 |
if ( $is_own_message && class_exists( 'Friends\Feed_Parser_ActivityPub' ) ) { |
| 200 |
$delivery = Friends\Feed_Parser_ActivityPub::get_direct_message_delivery_status( $message ); |
| 201 |
} |
| 202 |
?> |
| 203 |
<div class="friends-dm-message<?php echo $is_own_message ? ' is-own-message' : ''; ?><?php echo $is_consecutive ? ' is-consecutive-message' : ''; ?>" data-message-id="<?php echo esc_attr( $message->ID ); ?>"> |
| 204 |
<div class="friends-dm-message-avatar"> |
| 205 |
<?php if ( ! $is_own_message && $message_author && ! is_wp_error( $message_author ) && $message_author->get_avatar_url() ) : ?> |
| 206 |
<img class="avatar" src="<?php echo esc_url( $message_author->get_avatar_url() ); ?>" alt="" width="32" height="32"> |
| 207 |
<?php else : ?> |
| 208 |
<?php echo get_avatar( $is_own_message ? get_current_user_id() : 0, 32, '', '', array( 'class' => 'avatar' ) ); ?> |
| 209 |
<?php endif; ?> |
| 210 |
</div> |
| 211 |
<div class="friends-dm-message-body"> |
| 212 |
<div class="friends-dm-message-meta"> |
| 213 |
<strong><?php echo esc_html( $is_own_message ? __( 'You', 'friends' ) : $author_name ); ?></strong> |
| 214 |
<time data-friends-relative-time="<?php echo esc_attr( $post_time ); ?>" data-friends-relative-time-suffix="<?php echo esc_attr_x( ' ago', 'relative message timestamp suffix', 'friends' ); ?>" title="<?php echo esc_attr( date_i18n( $time_format, $post_time ) ); ?>"> |
| 215 |
<?php |
| 216 |
echo esc_html( |
| 217 |
sprintf( |
| 218 |
// translators: %s is a time span. |
| 219 |
__( '%s ago' ), // phpcs:ignore WordPress.WP.I18n.MissingArgDomain |
| 220 |
human_time_diff( $post_time ) |
| 221 |
) |
| 222 |
); |
| 223 |
?> |
| 224 |
</time> |
| 225 |
<?php if ( $delivery ) : ?> |
| 226 |
<span class="friends-dm-delivery-status is-<?php echo esc_attr( $delivery['status'] ); ?>" data-delivery-message-id="<?php echo esc_attr( $message->ID ); ?>" title="<?php echo esc_attr( $delivery['title'] ); ?>"><?php echo esc_html( $delivery['label'] ); ?></span> |
| 227 |
<?php endif; ?> |
| 228 |
</div> |
| 229 |
<div class="friends-dm-message-content" title="<?php echo esc_attr( date_i18n( $time_format, $post_time ) ); ?>"> |
| 230 |
<?php |
| 231 |
$content = make_clickable( get_the_content( null, false, $message ) ); |
| 232 |
echo wp_kses_post( apply_filters( 'the_content', $content ) ); |
| 233 |
?> |
| 234 |
</div> |
| 235 |
</div> |
| 236 |
</div> |
| 237 |
<?php $previous_message_author_key = $author_key; ?> |
| 238 |
<?php endforeach; ?> |
| 239 |
</div> |
| 240 |
|
| 241 |
<footer class="friends-dm-reply"> |
| 242 |
<?php |
| 243 |
$form_args = array( |
| 244 |
'blocks-everywhere' => false, |
| 245 |
'friend_user' => $selected_friend_user, |
| 246 |
'redirect_to' => $selected_url, |
| 247 |
'reply_to' => $selected_conversation['id'], |
| 248 |
'subject' => get_the_title( $selected_conversation['root_message'] ), |
| 249 |
); |
| 250 |
|
| 251 |
$feed_url = get_post_meta( $selected_conversation['root_message']->ID, 'friends_feed_url', true ); |
| 252 |
if ( $feed_url ) { |
| 253 |
$form_args['accounts'] = array( $feed_url => $feed_url ); |
| 254 |
$accounts = apply_filters( 'friends_message_form_accounts', array(), $selected_friend_user ); |
| 255 |
if ( isset( $accounts[ $feed_url ] ) ) { |
| 256 |
$form_args['accounts'][ $feed_url ] = $accounts[ $feed_url ]; |
| 257 |
} |
| 258 |
} else { |
| 259 |
$form_args['accounts'] = apply_filters( 'friends_message_form_accounts', array(), $selected_friend_user ); |
| 260 |
} |
| 261 |
|
| 262 |
if ( $selected_friend_user && ! is_wp_error( $selected_friend_user ) && ! empty( $form_args['accounts'] ) ) { |
| 263 |
Friends\Friends::template_loader()->get_template_part( |
| 264 |
'frontend/messages/message-form', |
| 265 |
null, |
| 266 |
array_merge( $args, $form_args ) |
| 267 |
); |
| 268 |
} |
| 269 |
?> |
| 270 |
</footer> |
| 271 |
</article> |
| 272 |
<?php endif; ?> |
| 273 |
</section> |
| 274 |
<?php |
| 275 |
Friends\Friends::template_loader()->get_template_part( 'frontend/footer', null, $args ); |
| 276 |
|