Skip to content

Commit 520cce2

Browse files
committed
Add post formats as channels in Friends adapter
1 parent 81d85a5 commit 520cce2

1 file changed

Lines changed: 66 additions & 2 deletions

File tree

includes/adapters/class-friends.php

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,62 @@ public function get_channels( $channels, $user_id ) {
136136
}
137137
}
138138

139+
// Get post formats as channels.
140+
$post_formats = $this->get_used_post_formats();
141+
142+
foreach ( $post_formats as $format => $name ) {
143+
$channels[] = array(
144+
'uid' => 'format-' . $format,
145+
'name' => $name,
146+
);
147+
}
148+
139149
return $channels;
140150
}
141151

152+
/**
153+
* Get post formats that are actually used by friend posts.
154+
*
155+
* @return array Associative array of format slug => format name.
156+
*/
157+
protected function get_used_post_formats() {
158+
$formats = array();
159+
160+
// Get all post format terms that have posts.
161+
$terms = \get_terms(
162+
array(
163+
'taxonomy' => 'post_format',
164+
'hide_empty' => true,
165+
)
166+
);
167+
168+
if ( \is_wp_error( $terms ) || empty( $terms ) ) {
169+
return $formats;
170+
}
171+
172+
$format_labels = array(
173+
'aside' => \__( 'Asides', 'microsub' ),
174+
'audio' => \__( 'Audio', 'microsub' ),
175+
'chat' => \__( 'Chats', 'microsub' ),
176+
'gallery' => \__( 'Galleries', 'microsub' ),
177+
'image' => \__( 'Images', 'microsub' ),
178+
'link' => \__( 'Links', 'microsub' ),
179+
'quote' => \__( 'Quotes', 'microsub' ),
180+
'status' => \__( 'Statuses', 'microsub' ),
181+
'video' => \__( 'Videos', 'microsub' ),
182+
);
183+
184+
foreach ( $terms as $term ) {
185+
// Term slug is like 'post-format-aside'.
186+
$format = str_replace( 'post-format-', '', $term->slug );
187+
if ( isset( $format_labels[ $format ] ) ) {
188+
$formats[ $format ] = $format_labels[ $format ];
189+
}
190+
}
191+
192+
return $formats;
193+
}
194+
142195
/**
143196
* Create a new channel.
144197
*
@@ -264,7 +317,7 @@ public function get_timeline( $result, $channel, $args ) {
264317
'order' => 'DESC',
265318
);
266319

267-
// Filter by friend list if not home channel.
320+
// Filter by friend list.
268321
if ( str_starts_with( $channel, 'list-' ) ) {
269322
$slug = substr( $channel, 5 );
270323
$term = \get_term_by( 'slug', $slug, 'friend-list' );
@@ -281,6 +334,16 @@ public function get_timeline( $result, $channel, $args ) {
281334
}
282335

283336
$query_args['author__in'] = $friend_users;
337+
} elseif ( str_starts_with( $channel, 'format-' ) ) {
338+
// Filter by post format.
339+
$format = substr( $channel, 7 );
340+
$query_args['tax_query'] = array(
341+
array(
342+
'taxonomy' => 'post_format',
343+
'field' => 'slug',
344+
'terms' => array( 'post-format-' . $format ),
345+
),
346+
);
284347
} elseif ( 'home' !== $channel ) {
285348
// Unknown channel, pass to next adapter.
286349
return $result;
@@ -370,7 +433,8 @@ public function get_following( $result, $channel, $user_id ) {
370433
* @return array|null Array of friend users or null if unknown channel.
371434
*/
372435
protected function get_friend_users_for_channel( $channel ) {
373-
if ( 'home' === $channel || 'notifications' === $channel ) {
436+
// For home, notifications, and format channels, return all friends.
437+
if ( 'home' === $channel || 'notifications' === $channel || str_starts_with( $channel, 'format-' ) ) {
374438
$query = \Friends\User_Query::all_friends_subscriptions();
375439
return $query->get_results();
376440
}

0 commit comments

Comments
 (0)