PluginProbe
Friends / 4.3.2
Friends v4.3.2
4.3.2 4.3.1 4.3.0 4.2.2 4.2.1 4.2.0 4.1.0 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.0 2.9.1 All 88 releases
friends / templates / frontend / messages.php

messages.php in Friends 4.3.2, at templates/frontend/messages.php

292 lines 14.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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_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_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 ( intval( $conversation_row['latest']->post_author ) === get_current_user_id() ) : ?>
156 <span class="friends-dm-delivery-status friends-dm-delivery-icon<?php echo $conversation_row['latest_delivery'] ? ' is-' . esc_attr( $conversation_row['latest_delivery']['status'] ) : ''; ?>" data-delivery-message-id="<?php echo esc_attr( $conversation_row['latest']->ID ); ?>"<?php echo $conversation_row['latest_delivery'] ? ' title="' . esc_attr( $conversation_row['latest_delivery']['title'] ) . '" aria-label="' . esc_attr( $conversation_row['latest_delivery']['label'] ) . '"' : ' hidden'; ?>></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">&larr;</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
190 $previous_message_author_key = null;
191 $previous_message_time = null;
192 $consecutive_message_time_threshold = 5 * MINUTE_IN_SECONDS;
193 ?>
194 <?php foreach ( $selected_conversation['messages'] as $message ) : ?>
195 <?php
196 $message_author = Friends\User::get_post_author( $message );
197 $is_own_message = $message_author && ! is_wp_error( $message_author ) && get_current_user_id() === intval( $message_author->ID );
198 $author_name = $message_author && ! is_wp_error( $message_author ) ? $message_author->display_name : __( 'Unknown sender', 'friends' );
199 $post_time = get_post_time( 'U', true, $message );
200 $author_key = $message_author && ! is_wp_error( $message_author ) ? 'user-' . $message_author->ID : 'unknown';
201 $is_consecutive = $author_key === $previous_message_author_key;
202 $is_spaced = $is_consecutive && $previous_message_time && $post_time - $previous_message_time >= $consecutive_message_time_threshold;
203 $delivery = null;
204 if ( $is_own_message && class_exists( 'Friends\Feed_Parser_ActivityPub' ) ) {
205 $delivery = Friends\Feed_Parser_ActivityPub::get_direct_message_delivery_status( $message );
206 }
207 ?>
208 <div class="friends-dm-message<?php echo $is_own_message ? ' is-own-message' : ''; ?><?php echo $is_consecutive ? ' is-consecutive-message' : ''; ?><?php echo $is_spaced ? ' is-spaced-message' : ''; ?>" data-message-id="<?php echo esc_attr( $message->ID ); ?>">
209 <div class="friends-dm-message-avatar">
210 <?php if ( ! $is_own_message && $message_author && ! is_wp_error( $message_author ) && $message_author->get_avatar_url() ) : ?>
211 <img class="avatar" src="<?php echo esc_url( $message_author->get_avatar_url() ); ?>" alt="" width="32" height="32">
212 <?php else : ?>
213 <?php echo get_avatar( $is_own_message ? get_current_user_id() : 0, 32, '', '', array( 'class' => 'avatar' ) ); ?>
214 <?php endif; ?>
215 </div>
216 <div class="friends-dm-message-body">
217 <div class="friends-dm-message-meta">
218 <strong><?php echo esc_html( $is_own_message ? __( 'You', 'friends' ) : $author_name ); ?></strong>
219 <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 ) ); ?>">
220 <?php
221 echo esc_html(
222 sprintf(
223 // translators: %s is a time span.
224 __( '%s ago' ), // phpcs:ignore WordPress.WP.I18n.MissingArgDomain
225 human_time_diff( $post_time )
226 )
227 );
228 ?>
229 </time>
230 <?php if ( $is_own_message ) : ?>
231 <span class="friends-dm-delivery-status<?php echo $delivery ? ' is-' . esc_attr( $delivery['status'] ) : ''; ?>" data-delivery-message-id="<?php echo esc_attr( $message->ID ); ?>"<?php echo $delivery ? ' title="' . esc_attr( $delivery['title'] ) . '"' : ' hidden'; ?>><?php echo $delivery ? esc_html( $delivery['label'] ) : ''; ?></span>
232 <?php endif; ?>
233 </div>
234 <div class="friends-dm-message-content" title="<?php echo esc_attr( date_i18n( $time_format, $post_time ) ); ?>">
235 <?php
236 $content = make_clickable( get_the_content( null, false, $message ) );
237 echo wp_kses_post( apply_filters( 'the_content', $content ) );
238 ?>
239 </div>
240 <?php $reactions = Friends\Reactions::get_post_reactions( $message ); ?>
241 <?php if ( ! empty( $reactions ) ) : ?>
242 <div class="friends-dm-message-reactions">
243 <?php foreach ( $reactions as $reaction ) : ?>
244 <span class="friends-dm-message-reaction" title="<?php echo esc_attr( $reaction->usernames ); ?>"><?php echo esc_html( $reaction->emoji ); ?> <?php echo esc_html( number_format_i18n( $reaction->count ) ); ?></span>
245 <?php endforeach; ?>
246 </div>
247 <?php endif; ?>
248 </div>
249 </div>
250 <?php
251 $previous_message_author_key = $author_key;
252 $previous_message_time = $post_time;
253 ?>
254 <?php endforeach; ?>
255 </div>
256
257 <footer class="friends-dm-reply">
258 <?php
259 $form_args = array(
260 'blocks-everywhere' => false,
261 'friend_user' => $selected_friend_user,
262 'redirect_to' => $selected_url,
263 'reply_to' => $selected_conversation['id'],
264 'subject' => get_the_title( $selected_conversation['root_message'] ),
265 );
266
267 $feed_url = get_post_meta( $selected_conversation['root_message']->ID, 'friends_feed_url', true );
268 if ( $feed_url ) {
269 $form_args['accounts'] = array( $feed_url => $feed_url );
270 $accounts = apply_filters( 'friends_message_form_accounts', array(), $selected_friend_user );
271 if ( isset( $accounts[ $feed_url ] ) ) {
272 $form_args['accounts'][ $feed_url ] = $accounts[ $feed_url ];
273 }
274 } else {
275 $form_args['accounts'] = apply_filters( 'friends_message_form_accounts', array(), $selected_friend_user );
276 }
277
278 if ( $selected_friend_user && ! is_wp_error( $selected_friend_user ) && ! empty( $form_args['accounts'] ) ) {
279 Friends\Friends::template_loader()->get_template_part(
280 'frontend/messages/message-form',
281 null,
282 array_merge( $args, $form_args )
283 );
284 }
285 ?>
286 </footer>
287 </article>
288 <?php endif; ?>
289 </section>
290 <?php
291 Friends\Friends::template_loader()->get_template_part( 'frontend/footer', null, $args );
292