@@ -76,7 +76,7 @@ public function can_handle_url( $url ) {
7676 }
7777
7878 // Try to discover feeds from the URL.
79- $ discovered = $ friends ->feed ->discover_feeds ( $ url );
79+ $ discovered = $ friends ->feed ->discover_available_feeds ( $ url );
8080
8181 return ! \is_wp_error ( $ discovered ) && ! empty ( $ discovered );
8282 }
@@ -103,7 +103,7 @@ public function owns_feed( $url ) {
103103 * @return \Friends\User_Feed|null The feed or null if not found.
104104 */
105105 protected function get_feed_by_url ( $ url ) {
106- $ friend_users = \Friends \User_Query::all_friends_subscriptions ();
106+ $ friend_users = \Friends \User_Query::all_associated_users ();
107107
108108 foreach ( $ friend_users ->get_results () as $ friend_user ) {
109109 if ( ! $ friend_user instanceof \Friends \User ) {
@@ -309,9 +309,10 @@ public function delete_channel( $result, $uid, $user_id ) {
309309 * @param array $result Current result with 'items' from other adapters.
310310 * @param string $channel Channel UID.
311311 * @param array $args Query arguments (after, before, limit).
312+ * @param int $user_id The user ID the timeline is requested for.
312313 * @return array Timeline data with 'items' and optional 'paging'.
313314 */
314- public function get_timeline ( $ result , $ channel , $ args ) {
315+ public function get_timeline ( $ result , $ channel , $ args, $ user_id = 0 ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
315316 if ( ! self ::is_available () ) {
316317 return $ result ;
317318 }
@@ -466,7 +467,7 @@ public function get_following( $result, $channel, $user_id ) {
466467 protected function get_friend_users_for_channel ( $ channel ) {
467468 // For home, notifications, and format channels, return all friends.
468469 if ( 'home ' === $ channel || 'notifications ' === $ channel || str_starts_with ( $ channel , 'friends- ' ) ) {
469- $ query = \Friends \User_Query::all_friends_subscriptions ();
470+ $ query = \Friends \User_Query::all_associated_users ();
470471 return $ query ->get_results ();
471472 }
472473
@@ -508,23 +509,80 @@ protected function get_friend_users_for_channel( $channel ) {
508509 * @return array|null Feed data on success, null to pass to next adapter.
509510 */
510511 public function follow ( $ result , $ channel , $ url , $ user_id ) {
512+ // Respect the "first adapter that can handle the URL wins" contract.
513+ if ( null !== $ result ) {
514+ return $ result ;
515+ }
516+
511517 if ( ! self ::is_available () ) {
512518 return $ result ;
513519 }
514520
515- // Check if we can handle this URL.
516- if ( ! $ this ->can_handle_url ( $ url ) ) {
521+ $ friends = $ this ->get_friends ();
522+ if ( ! $ friends || ! isset ( $ friends ->feed ) ) {
523+ return $ result ;
524+ }
525+
526+ // Discover subscribable feeds at the URL.
527+ $ feeds = $ friends ->feed ->discover_available_feeds ( $ url );
528+
529+ if ( \is_wp_error ( $ feeds ) || empty ( $ feeds ) ) {
517530 return $ result ;
518531 // Pass to next adapter.
519532 }
520533
521- // Use Friends plugin to subscribe to the URL.
522- $ friend_user = \Friends \Subscription::subscribe ( $ url );
534+ // Derive a user login and display name from the discovered feeds.
535+ $ user_login = \Friends \User::get_user_login_from_feeds ( $ feeds );
536+ $ display_name = \Friends \User::get_display_name_from_feeds ( $ feeds );
537+
538+ if ( empty ( $ user_login ) ) {
539+ $ user_login = \sanitize_user ( (string ) \wp_parse_url ( $ url , \PHP_URL_HOST ), true );
540+ }
541+
542+ if ( empty ( $ user_login ) ) {
543+ return $ result ;
544+ }
545+
546+ // Pull an avatar and description from the discovered feeds, if available.
547+ $ avatar = null ;
548+ $ description = null ;
549+ foreach ( $ feeds as $ feed_details ) {
550+ if ( ! $ avatar && ! empty ( $ feed_details ['avatar ' ] ) ) {
551+ $ avatar = $ feed_details ['avatar ' ];
552+ }
553+ if ( ! $ description && ! empty ( $ feed_details ['description ' ] ) ) {
554+ $ description = $ feed_details ['description ' ];
555+ }
556+ }
557+
558+ // Create (or fetch) the subscription user.
559+ $ friend_user = \Friends \User::create ( $ user_login , 'subscription ' , $ url , $ display_name , $ avatar , $ description );
523560
524561 if ( \is_wp_error ( $ friend_user ) ) {
525562 return $ result ;
526563 }
527564
565+ // Subscribe to the discovered feeds (autoselected, or any supported parser).
566+ $ subscribed = false ;
567+ foreach ( $ feeds as $ feed_url => $ feed_details ) {
568+ $ autoselect = ! empty ( $ feed_details ['autoselect ' ] );
569+ $ unsupported = isset ( $ feed_details ['parser ' ] ) && 'unsupported ' === $ feed_details ['parser ' ];
570+
571+ if ( ! $ autoselect && $ unsupported ) {
572+ continue ;
573+ }
574+
575+ $ subscribed_feed = $ friend_user ->subscribe ( $ feed_url , $ feed_details );
576+
577+ if ( ! \is_wp_error ( $ subscribed_feed ) ) {
578+ $ subscribed = true ;
579+ }
580+ }
581+
582+ if ( ! $ subscribed ) {
583+ return $ result ;
584+ }
585+
528586 // If subscribing to a specific list channel, add to that list.
529587 if ( str_starts_with ( $ channel , 'tag- ' ) ) {
530588 $ slug = substr ( $ channel , 4 );
@@ -603,13 +661,15 @@ public function search( $result, $query, $user_id ) {
603661 }
604662
605663 // Use Friends feed discovery.
606- $ discovered = $ friends ->feed ->discover_feeds ( $ query );
664+ $ discovered = $ friends ->feed ->discover_available_feeds ( $ query );
607665
608666 if ( \is_wp_error ( $ discovered ) || empty ( $ discovered ) ) {
609- return array ( 'results ' => array () );
667+ // Nothing to add; preserve results from other adapters.
668+ return $ result ;
610669 }
611670
612- $ results = array ();
671+ // Preserve results aggregated by earlier adapters.
672+ $ results = ( \is_array ( $ result ) && isset ( $ result ['results ' ] ) ) ? $ result ['results ' ] : array ();
613673
614674 foreach ( $ discovered as $ feed_url => $ feed_data ) {
615675 $ item = array (
@@ -646,7 +706,7 @@ public function preview( $result, $url, $user_id ) {
646706 }
647707
648708 // Try to fetch and parse the feed.
649- $ discovered = $ friends ->feed ->discover_feeds ( $ url );
709+ $ discovered = $ friends ->feed ->discover_available_feeds ( $ url );
650710
651711 if ( \is_wp_error ( $ discovered ) || empty ( $ discovered ) ) {
652712 return $ result ;
0 commit comments