| @@ -47,8 +47,9 @@ | ||
| 47 | 47 | add_filter( 'post_type_link', array( $this, 'post_type_link' ), 10, 2 ); |
| 48 | 48 | add_filter( 'friends_unread_count', array( $this, 'friends_unread_messages_count' ) ); |
| 49 | 49 | add_action( 'friends_own_site_menu_top', array( $this, 'friends_add_menu_unread_messages' ) ); |
| 50 | 50 | add_action( 'wp_ajax_friends-mark-read', array( $this, 'mark_message_read' ) ); |
| 51 | + add_action( 'wp_ajax_friends-get-message-delivery-statuses', array( $this, 'get_message_delivery_statuses' ) ); | |
| 51 | 52 | add_action( 'friends_author_header', array( $this, 'friends_author_header' ), 10, 2 ); |
| 52 | 53 | add_action( 'friends_after_header', array( $this, 'friends_display_messages' ), 10, 2 ); |
| 53 | 54 | add_action( 'friends_after_header', array( $this, 'friends_message_form' ), 11, 2 ); |
| 54 | 55 | add_filter( 'template_redirect', array( $this, 'handle_message_send' ), 10, 2 ); |
| @@ -159,9 +160,30 @@ | ||
| 159 | 160 | $post_types = array( self::CPT ); |
| 160 | 161 | return $post_types; |
| 161 | 162 | } |
| 162 | 163 | |
| 164 | + /** | |
| 165 | + * Look up a direct message by its URL. | |
| 166 | + * | |
| 167 | + * Messages live in their own post type which is not part of the frontend post | |
| 168 | + * types, so Feed::url_to_postid() doesn't consider them by default. | |
| 169 | + * | |
| 170 | + * @param string $url The URL of the message. | |
| 171 | + * | |
| 172 | + * @return int|null The post ID or null if it wasn't found. | |
| 173 | + */ | |
| 174 | + public static function url_to_postid( $url ) { | |
| 175 | + $only_messages = function () { | |
| 176 | + return array( self::CPT ); | |
| 177 | + }; | |
| 163 | 178 | |
| 179 | + add_filter( 'friends_frontend_post_types', $only_messages, 999 ); | |
| 180 | + $post_id = Feed::url_to_postid( $url ); | |
| 181 | + remove_filter( 'friends_frontend_post_types', $only_messages, 999 ); | |
| 182 | + | |
| 183 | + return $post_id; | |
| 184 | + } | |
| 185 | + | |
| 164 | 186 | public function save_incoming_message( User $friend_user, $message, $subject = '', $feed_url = null, $remote_url = null, $reply_to = null ) { |
| 165 | 187 | $post_data = array( |
| 166 | 188 | 'post_type' => self::CPT, |
| 167 | 189 | 'post_title' => $subject, |
| @@ -364,8 +386,59 @@ | ||
| 364 | 386 | array( |
| 365 | 387 | 'result' => $result, |
| 366 | 388 | ) |
| 367 | 389 | ); |
| 390 | + } | |
| 391 | + | |
| 392 | + /** | |
| 393 | + * Ajax function to fetch delivery status for outgoing direct messages. | |
| 394 | + */ | |
| 395 | + public function get_message_delivery_statuses() { | |
| 396 | + check_ajax_referer( 'friends-message-delivery-statuses' ); | |
| 397 | + | |
| 398 | + if ( ! is_user_logged_in() ) { | |
| 399 | + wp_send_json_error( | |
| 400 | + array( | |
| 401 | + 'message' => __( 'You are not authorized to view message delivery statuses.', 'friends' ), | |
| 402 | + ), | |
| 403 | + 403 | |
| 404 | + ); | |
| 405 | + } | |
| 406 | + | |
| 407 | + $post_ids = array(); | |
| 408 | + if ( isset( $_POST['post_ids'] ) && is_array( $_POST['post_ids'] ) ) { | |
| 409 | + $post_ids = array_map( 'absint', wp_unslash( $_POST['post_ids'] ) ); | |
| 410 | + } | |
| 411 | + | |
| 412 | + $post_ids = array_filter( array_unique( $post_ids ) ); | |
| 413 | + if ( empty( $post_ids ) ) { | |
| 414 | + wp_send_json_success( array( 'statuses' => array() ) ); | |
| 415 | + } | |
| 416 | + | |
| 417 | + $statuses = array(); | |
| 418 | + foreach ( $post_ids as $post_id ) { | |
| 419 | + $post = get_post( $post_id ); | |
| 420 | + if ( ! $post || self::CPT !== $post->post_type || get_current_user_id() !== intval( $post->post_author ) ) { | |
| 421 | + continue; | |
| 422 | + } | |
| 423 | + | |
| 424 | + $delivery = null; | |
| 425 | + if ( class_exists( 'Friends\Feed_Parser_ActivityPub' ) ) { | |
| 426 | + $delivery = Feed_Parser_ActivityPub::get_direct_message_delivery_status( $post ); | |
| 427 | + } | |
| 428 | + | |
| 429 | + if ( ! $delivery ) { | |
| 430 | + continue; | |
| 431 | + } | |
| 432 | + | |
| 433 | + $statuses[ $post_id ] = array( | |
| 434 | + 'status' => sanitize_html_class( $delivery['status'] ), | |
| 435 | + 'label' => $delivery['label'], | |
| 436 | + 'title' => $delivery['title'], | |
| 437 | + ); | |
| 438 | + } | |
| 439 | + | |
| 440 | + wp_send_json_success( array( 'statuses' => $statuses ) ); | |
| 368 | 441 | } |
| 369 | 442 | |
| 370 | 443 | /** |
| 371 | 444 | * Extend the author header. |