$post['id'], 'title' => $post['title'], 'content' => $post['message_rendered'], 'raw' => $can_change ? $post['message'] : null, 'author' => $authors[ $post['user_id'] ] ?? self::deleted_author(), 'space' => array( 'id' => $space['id'], 'title' => $space['title'], 'slug' => $space['slug'], 'icon' => (string) ( $space['settings']['icon'] ?? '' ), 'color' => (string) ( $space['settings']['color'] ?? '' ), ), 'media' => $media[ $post['id'] ] ?? array(), 'preview' => $post['meta']['preview'] ?? null, 'topics' => $terms[ $post['id'] ]['topics'] ?? array(), 'hashtags' => $terms[ $post['id'] ]['hashtags'] ?? array(), 'status' => $post['status'], 'is_pinned' => $post['is_pinned'], 'likes_count' => $post['reactions_count'], 'comments_count' => $post['comments_count'], 'is_edited' => ! empty( $post['meta']['edited_by'] ), 'url' => Portal::link( 'posts/' . $post['id'] ), 'created_at' => Time::iso( $post['created_at'] ), 'updated_at' => Time::iso( $post['updated_at'] ), 'viewer' => array( 'liked' => in_array( $post['id'], $liked, true ), 'bookmarked' => in_array( $post['id'], $bookmarked, true ), 'can_edit' => $can_change, 'can_delete' => $can_change, 'can_pin' => SpaceAccess::can_moderate( $space, $viewer_id ), 'can_comment' => PostAccess::can_comment( $space, $viewer_id ), 'can_report' => self::can_report( $post['user_id'], $viewer_id ), ), ); } return $items; } /** * Formats one post. * * @param array $post Post row. * @param int $viewer_id Viewer ID. * @return array */ public static function post( array $post, $viewer_id ) { $items = self::posts( array( $post ), $viewer_id ); return $items ? $items[0] : array(); } /** * Formats comments of one post. * * @param array[] $comments Comment rows. * @param array $space Space of the post. * @param int $viewer_id Viewer ID. * @return array[] */ public static function comments( array $comments, array $space, $viewer_id ) { if ( ! $comments ) { return array(); } $liked = Reactions::of_user( 'comment', wp_list_pluck( $comments, 'id' ), $viewer_id, 'like' ); $authors = self::authors( wp_list_pluck( $comments, 'user_id' ) ); $can_add = PostAccess::can_comment( $space, $viewer_id ); $items = array(); $by_id = array_column( $comments, null, 'id' ); foreach ( $comments as $comment ) { $can_change = PostAccess::can_change( $space, $comment['user_id'], $viewer_id ); $depth = 1; $cursor = $comment; // Walk up inside the list; ask the database for a parent outside it. while ( $cursor['parent_id'] && $depth < 100 ) { if ( isset( $by_id[ $cursor['parent_id'] ] ) && $cursor['parent_id'] !== $comment['id'] ) { $cursor = $by_id[ $cursor['parent_id'] ]; ++$depth; continue; } $outside = Comments::find( $cursor['parent_id'] ); $depth += $outside ? Comments::depth( $outside ) : 0; break; } $items[] = array( 'id' => $comment['id'], 'post_id' => $comment['post_id'], 'parent_id' => $comment['parent_id'], 'depth' => $depth, 'content' => $comment['message_rendered'], 'raw' => $can_change ? $comment['message'] : null, 'author' => $authors[ $comment['user_id'] ] ?? self::deleted_author(), 'status' => $comment['status'], 'likes_count' => $comment['reactions_count'], 'is_edited' => $comment['updated_at'] !== $comment['created_at'], 'created_at' => Time::iso( $comment['created_at'] ), 'viewer' => array( 'liked' => in_array( $comment['id'], $liked, true ), 'can_edit' => $can_change, 'can_delete' => $can_change, 'can_reply' => $can_add, 'can_report' => self::can_report( $comment['user_id'], $viewer_id ), ), ); } return $items; } /** * Whether the viewer may report something the author wrote. * * @param int $author_id Author. * @param int $viewer_id Viewer. * @return bool */ public static function can_report( $author_id, $viewer_id ) { return $viewer_id > 0 && (int) $author_id !== (int) $viewer_id && ! SpaceAccess::is_restricted( $viewer_id ); } /** * Formats authors by ID. * * @param int[] $user_ids User IDs. * @return array */ public static function authors( array $user_ids ) { $user_ids = array_values( array_unique( array_map( 'intval', $user_ids ) ) ); cache_users( $user_ids ); $users = array(); foreach ( $user_ids as $user_id ) { $user = get_userdata( $user_id ); if ( $user ) { $users[] = $user; } } $authors = array(); foreach ( MemberDirectory::format_many( $users ) as $member ) { $authors[ $member['id'] ] = $member; } return $authors; } /** * Placeholder for content whose author no longer exists. * * @return array */ public static function deleted_author() { return array( 'id' => 0, 'name' => __( 'Former member', 'spacebay' ), 'slug' => '', 'avatar' => get_avatar_url( 0, array( 'size' => 128 ) ), 'headline' => '', 'is_manager' => false, 'joined_at' => null, 'level' => '', 'level_rank' => 0, ); } }