Repository navigation
Expand file tree
/
Copy pathclass-friends.php
More file actions
783 lines (648 loc) · 18.4 KB
/
Copy pathclass-friends.php
File metadata and controls
783 lines (648 loc) · 18.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
<?php
/**
* Friends Plugin Adapter for Microsub.
*
* @package Microsub
*/
namespace Microsub\Adapters;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use Microsub\Adapter;
/**
* Friends Adapter
*
* Provides Microsub functionality using the Friends plugin as backend.
*
* @see https://github.com/akirk/friends
*/
class Friends extends Adapter {
/**
* Adapter identifier.
*
* @var string
*/
protected $id = 'friends';
/**
* Adapter name.
*
* @var string
*/
protected $name = 'Friends';
/**
* Check if the Friends plugin is available.
*
* @return bool
*/
public static function is_available() {
return \defined( 'FRIENDS_VERSION' );
}
/**
* Get the Friends plugin instance.
*
* @return \Friends\Friends|null
*/
protected function get_friends() {
if ( ! self::is_available() ) {
return null;
}
return \Friends\Friends::get_instance();
}
/**
* Check if this adapter can handle following the given URL.
*
* Friends plugin can handle most feed types (RSS, Atom, ActivityPub, mf2).
*
* @param string $url The URL to check.
* @return bool True if this adapter can handle the URL.
*/
public function can_handle_url( $url ) {
if ( ! self::is_available() ) {
return false;
}
$friends = $this->get_friends();
if ( ! $friends || ! isset( $friends->feed ) ) {
return false;
}
// Try to discover feeds from the URL.
$discovered = $friends->feed->discover_available_feeds( $url );
return ! \is_wp_error( $discovered ) && ! empty( $discovered );
}
/**
* Check if this adapter owns/manages the given feed URL.
*
* @param string $url The feed URL to check.
* @return bool True if this adapter owns the feed.
*/
public function owns_feed( $url ) {
if ( ! self::is_available() ) {
return false;
}
$feed = $this->get_feed_by_url( $url );
return ! empty( $feed );
}
/**
* Find a user feed by URL.
*
* @param string $url The feed URL to find.
* @return \Friends\User_Feed|null The feed or null if not found.
*/
protected function get_feed_by_url( $url ) {
$friend_users = \Friends\User_Query::all_associated_users();
foreach ( $friend_users->get_results() as $friend_user ) {
if ( ! $friend_user instanceof \Friends\User ) {
continue;
}
$user_feeds = $friend_user->get_active_feeds();
foreach ( $user_feeds as $user_feed ) {
if ( $user_feed->get_url() === $url ) {
return $user_feed;
}
}
}
return null;
}
/**
* Get list of channels.
*
* Maps Friends' friend lists (taxonomy terms) to Microsub channels.
*
* @param array $channels Current channels array from other adapters.
* @param int $user_id The user ID.
* @return array Array of channels with 'uid' and 'name'.
*/
public function get_channels( $channels, $user_id ) {
if ( ! self::is_available() ) {
return $channels;
}
// Notifications channel.
$channels[] = array(
'uid' => 'notifications',
'name' => \__( 'Notifications', 'microsub' ),
'unread' => 0,
);
// Home channel (all posts).
$channels[] = array(
'uid' => 'home',
'name' => \__( 'Home', 'microsub' ),
);
// Get friend tags as channels.
$friend_tags = \get_terms(
array(
'taxonomy' => 'friend_tag',
'hide_empty' => false,
)
);
if ( ! \is_wp_error( $friend_tags ) && ! empty( $friend_tags ) ) {
foreach ( $friend_tags as $tag ) {
$channels[] = array(
'uid' => 'tag-' . $tag->slug,
'name' => $tag->name,
);
}
}
// Get post formats as channels.
$post_formats = $this->get_post_formats();
foreach ( $post_formats as $format => $name ) {
$channels[] = array(
'uid' => 'friends-' . $format,
'name' => $name,
);
}
return $channels;
}
/**
* Get post formats supported by the current theme.
*
* @return array Associative array of format slug => format name.
*/
protected function get_post_formats() {
// Standard is always available.
$formats = array( 'standard' => \__( 'Standard', 'microsub' ) );
// Get formats supported by the theme.
$theme_formats = \get_theme_support( 'post-formats' );
if ( empty( $theme_formats ) || empty( $theme_formats[0] ) ) {
return $formats;
}
$all_formats = \get_post_format_strings();
foreach ( $theme_formats[0] as $format ) {
if ( isset( $all_formats[ $format ] ) ) {
$formats[ $format ] = $all_formats[ $format ];
}
}
return $formats;
}
/**
* Create a new channel.
*
* Creates a new friend list term.
*
* @param array|null $result Current result or null.
* @param string $name Channel name.
* @param int $user_id The user ID.
* @return array|null Channel data on success.
*/
public function create_channel( $result, $name, $user_id ) {
if ( ! self::is_available() ) {
return $result;
}
$term = \wp_insert_term( $name, 'friend_tag' );
if ( \is_wp_error( $term ) ) {
return $result;
}
$term_data = \get_term( $term['term_id'], 'friend_tag' );
return array(
'uid' => 'tag-' . $term_data->slug,
'name' => $term_data->name,
);
}
/**
* Update a channel.
*
* @param array|null $result Current result or null.
* @param string $uid Channel UID.
* @param string $name New channel name.
* @param int $user_id The user ID.
* @return array|null Updated channel data on success.
*/
public function update_channel( $result, $uid, $name, $user_id ) {
if ( ! self::is_available() ) {
return $result;
}
// Only handle list channels.
if ( ! str_starts_with( $uid, 'tag-' ) ) {
return $result;
}
$slug = substr( $uid, 4 );
$term = \get_term_by( 'slug', $slug, 'friend_tag' );
if ( ! $term ) {
return $result;
}
$updated = \wp_update_term( $term->term_id, 'friend_tag', array( 'name' => $name ) );
if ( \is_wp_error( $updated ) ) {
return $result;
}
return array(
'uid' => $uid,
'name' => $name,
);
}
/**
* Delete a channel.
*
* @param bool|null $result Current result or null.
* @param string $uid Channel UID.
* @param int $user_id The user ID.
* @return bool|null True on success.
*/
public function delete_channel( $result, $uid, $user_id ) {
if ( ! self::is_available() ) {
return $result;
}
// Only handle list channels.
if ( ! str_starts_with( $uid, 'tag-' ) ) {
return $result;
}
$slug = substr( $uid, 4 );
$term = \get_term_by( 'slug', $slug, 'friend_tag' );
if ( ! $term ) {
return $result;
}
$deleted = \wp_delete_term( $term->term_id, 'friend_tag' );
return ! \is_wp_error( $deleted ) && $deleted;
}
/**
* Get timeline entries for a channel.
*
* @param array $result Current result with 'items' from other adapters.
* @param string $channel Channel UID.
* @param array $args Query arguments (after, before, limit).
* @param int $user_id The user ID the timeline is requested for.
* @return array Timeline data with 'items' and optional 'paging'.
*/
public function get_timeline( $result, $channel, $args, $user_id = 0 ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
if ( ! self::is_available() ) {
return $result;
}
// Notifications channel returns empty for now.
if ( 'notifications' === $channel ) {
return $result;
}
$limit = isset( $args['limit'] ) ? \absint( $args['limit'] ) : 20;
$query_args = array(
'post_type' => \Friends\Friends::CPT,
'post_status' => array( 'publish', 'private' ),
'posts_per_page' => $limit,
'orderby' => 'date',
'order' => 'DESC',
);
// Filter by friend list.
if ( str_starts_with( $channel, 'tag-' ) ) {
$slug = substr( $channel, 4 );
$term = \get_term_by( 'slug', $slug, 'friend_tag' );
if ( ! $term ) {
return $result;
}
// Get friend users in this list.
$friend_users = \get_objects_in_term( $term->term_id, 'friend_tag' );
if ( empty( $friend_users ) ) {
return $result;
}
$query_args['author__in'] = $friend_users;
} elseif ( str_starts_with( $channel, 'friends-' ) ) {
// Filter by post format.
$format = substr( $channel, 8 );
if ( 'standard' === $format ) {
// Standard = posts without any post format.
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
$query_args['tax_query'] = array(
array(
'taxonomy' => 'post_format',
'operator' => 'NOT EXISTS',
),
);
} else {
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
$query_args['tax_query'] = array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-' . $format ),
),
);
}
} elseif ( 'home' !== $channel ) {
// Unknown channel, pass to next adapter.
return $result;
}//end if
// Handle cursor-based pagination.
if ( ! empty( $args['after'] ) ) {
$query_args['date_query'] = array(
array(
'before' => $args['after'],
),
);
}
if ( ! empty( $args['before'] ) ) {
$query_args['date_query'] = array(
array(
'after' => $args['before'],
),
);
$query_args['order'] = 'ASC';
}
$query = new \WP_Query( $query_args );
foreach ( $query->posts as $post ) {
$result['items'][] = $this->post_to_jf2( $post );
}
return $result;
}
/**
* Get list of followed feeds in a channel.
*
* @param array $result Current result from other adapters.
* @param string $channel Channel UID.
* @param int $user_id The user ID.
* @return array Array of feed objects with 'type' and 'url'.
*/
public function get_following( $result, $channel, $user_id ) {
if ( ! self::is_available() ) {
return $result;
}
// Get friend users, optionally filtered by list.
$friend_users = $this->get_friend_users_for_channel( $channel );
if ( null === $friend_users ) {
return $result;
// Unknown channel.
}
foreach ( $friend_users as $friend_user ) {
if ( ! $friend_user instanceof \Friends\User ) {
continue;
}
$user_feeds = $friend_user->get_active_feeds();
foreach ( $user_feeds as $user_feed ) {
$feed_url = $user_feed->get_url();
$feed = array(
'type' => 'feed',
'url' => $feed_url,
'_id' => \md5( $feed_url ),
);
$name = $friend_user->display_name;
if ( $name ) {
$feed['name'] = $name;
}
$photo = \get_avatar_url( $friend_user->ID );
if ( $photo ) {
$feed['photo'] = $photo;
}
$result[] = $feed;
}//end foreach
}//end foreach
return $result;
}
/**
* Get friend users for a channel.
*
* @param string $channel Channel UID.
* @return array|null Array of friend users or null if unknown channel.
*/
protected function get_friend_users_for_channel( $channel ) {
// For home, notifications, and format channels, return all friends.
if ( 'home' === $channel || 'notifications' === $channel || str_starts_with( $channel, 'friends-' ) ) {
$query = \Friends\User_Query::all_associated_users();
return $query->get_results();
}
if ( str_starts_with( $channel, 'tag-' ) ) {
$slug = substr( $channel, 4 );
$term = \get_term_by( 'slug', $slug, 'friend_tag' );
if ( ! $term ) {
return array();
}
$user_ids = \get_objects_in_term( $term->term_id, 'friend_tag' );
if ( empty( $user_ids ) ) {
return array();
}
$users = array();
foreach ( $user_ids as $user_id ) {
$user = new \Friends\User( $user_id );
if ( $user->has_cap( 'friend' ) || $user->has_cap( 'subscription' ) ) {
$users[] = $user;
}
}
return $users;
}//end if
return null;
// Unknown channel.
}
/**
* Follow a URL.
*
* @param array|null $result Current result or null.
* @param string $channel Channel UID.
* @param string $url URL to follow.
* @param int $user_id The user ID.
* @return array|null Feed data on success, null to pass to next adapter.
*/
public function follow( $result, $channel, $url, $user_id ) {
// Respect the "first adapter that can handle the URL wins" contract.
if ( null !== $result ) {
return $result;
}
if ( ! self::is_available() ) {
return $result;
}
$friends = $this->get_friends();
if ( ! $friends || ! isset( $friends->feed ) ) {
return $result;
}
// Discover subscribable feeds at the URL.
$feeds = $friends->feed->discover_available_feeds( $url );
if ( \is_wp_error( $feeds ) || empty( $feeds ) ) {
return $result;
// Pass to next adapter.
}
// Derive a user login and display name from the discovered feeds.
$user_login = \Friends\User::get_user_login_from_feeds( $feeds );
$display_name = \Friends\User::get_display_name_from_feeds( $feeds );
if ( empty( $user_login ) ) {
$user_login = \sanitize_user( (string) \wp_parse_url( $url, \PHP_URL_HOST ), true );
}
if ( empty( $user_login ) ) {
return $result;
}
// Pull an avatar and description from the discovered feeds, if available.
$avatar = null;
$description = null;
foreach ( $feeds as $feed_details ) {
if ( ! $avatar && ! empty( $feed_details['avatar'] ) ) {
$avatar = $feed_details['avatar'];
}
if ( ! $description && ! empty( $feed_details['description'] ) ) {
$description = $feed_details['description'];
}
}
// Create (or fetch) the subscription user.
$friend_user = \Friends\User::create( $user_login, 'subscription', $url, $display_name, $avatar, $description );
if ( \is_wp_error( $friend_user ) ) {
return $result;
}
// Subscribe to the discovered feeds (autoselected, or any supported parser).
$subscribed = false;
foreach ( $feeds as $feed_url => $feed_details ) {
$autoselect = ! empty( $feed_details['autoselect'] );
$unsupported = isset( $feed_details['parser'] ) && 'unsupported' === $feed_details['parser'];
if ( ! $autoselect && $unsupported ) {
continue;
}
$subscribed_feed = $friend_user->subscribe( $feed_url, $feed_details );
if ( ! \is_wp_error( $subscribed_feed ) ) {
$subscribed = true;
}
}
if ( ! $subscribed ) {
return $result;
}
// If subscribing to a specific list channel, add to that list.
if ( str_starts_with( $channel, 'tag-' ) ) {
$slug = substr( $channel, 4 );
$term = \get_term_by( 'slug', $slug, 'friend_tag' );
if ( $term ) {
\wp_set_object_terms( $friend_user->ID, $term->term_id, 'friend_tag', true );
}
}
return array(
'type' => 'feed',
'url' => $url,
);
}
/**
* Unfollow a URL.
*
* @param bool|null $result Current result or null.
* @param string $channel Channel UID.
* @param string $url URL to unfollow.
* @param int $user_id The user ID.
* @return bool|null True on success, false on failure, null to pass to next adapter.
*/
public function unfollow( $result, $channel, $url, $user_id ) {
if ( ! self::is_available() ) {
return $result;
}
// Find the feed by URL.
$feed = $this->get_feed_by_url( $url );
if ( ! $feed ) {
return $result;
// Pass to next adapter.
}
$friend_user = $feed->get_friend_user();
if ( ! $friend_user ) {
return false;
}
// Check if user has other feeds.
$user_feeds = $friend_user->get_active_feeds();
if ( \count( $user_feeds ) <= 1 ) {
// Delete the user if this is the only feed.
require_once ABSPATH . 'wp-admin/includes/user.php';
\wp_delete_user( $friend_user->ID );
} else {
// Just deactivate this feed.
$feed->update_metadata( 'active', false );
}
return true;
}
/**
* Search for feeds.
*
* @param array|null $result Current result or null.
* @param string $query Search query.
* @param int $user_id The user ID.
* @return array|null Search results.
*/
public function search( $result, $query, $user_id ) {
if ( ! self::is_available() ) {
return $result;
}
$friends = $this->get_friends();
if ( ! $friends || ! isset( $friends->feed ) ) {
return $result;
}
// Use Friends feed discovery.
$discovered = $friends->feed->discover_available_feeds( $query );
if ( \is_wp_error( $discovered ) || empty( $discovered ) ) {
// Nothing to add; preserve results from other adapters.
return $result;
}
// Preserve results aggregated by earlier adapters.
$results = ( \is_array( $result ) && isset( $result['results'] ) ) ? $result['results'] : array();
foreach ( $discovered as $feed_url => $feed_data ) {
$item = array(
'type' => 'feed',
'url' => $feed_url,
);
if ( ! empty( $feed_data['title'] ) ) {
$item['name'] = $feed_data['title'];
}
$results[] = $item;
}
return array( 'results' => $results );
}
/**
* Preview a URL.
*
* @param array|null $result Current result or null.
* @param string $url URL to preview.
* @param int $user_id The user ID.
* @return array|null Preview data.
*/
public function preview( $result, $url, $user_id ) {
if ( ! self::is_available() ) {
return $result;
}
$friends = $this->get_friends();
if ( ! $friends || ! isset( $friends->feed ) ) {
return $result;
}
// Try to fetch and parse the feed.
$discovered = $friends->feed->discover_available_feeds( $url );
if ( \is_wp_error( $discovered ) || empty( $discovered ) ) {
return $result;
}
// Get first feed.
$feed_url = \array_key_first( $discovered );
$feed_data = $discovered[ $feed_url ];
$items = array();
if ( ! empty( $feed_data['entries'] ) ) {
foreach ( \array_slice( $feed_data['entries'], 0, 10 ) as $entry ) {
$items[] = $this->to_jf2( $entry );
}
}
return array( 'items' => $items );
}
/**
* Convert a WordPress post to jf2 format.
*
* @param \WP_Post $post The post object.
* @return array jf2 formatted entry.
*/
protected function post_to_jf2( $post ) {
$jf2 = array(
'type' => 'entry',
'_id' => (string) $post->ID,
'published' => \get_the_date( 'c', $post ),
);
// URL.
$permalink = \get_post_meta( $post->ID, 'permalink', true );
if ( $permalink ) {
$jf2['url'] = $permalink;
}
// Title.
$title = \get_the_title( $post );
if ( $title && $title !== $permalink ) {
$jf2['name'] = $title;
}
// Content.
$content = \get_the_content( null, false, $post );
if ( $content ) {
$jf2['content'] = array(
'html' => $content,
'text' => \wp_strip_all_tags( $content ),
);
}
// Author from post author (Friends user).
$author = \get_userdata( $post->post_author );
if ( $author ) {
$jf2['author'] = array(
'type' => 'card',
'name' => $author->display_name,
'url' => $author->user_url,
'photo' => \get_avatar_url( $author->ID ),
);
}
// Post format.
$post_format = \get_post_format( $post );
if ( $post_format ) {
$jf2['post-type'] = $post_format;
}
return $jf2;
}
}