2023-04-21 14:56:22 +02:00
< ? php
namespace Activitypub\Collection ;
use WP_Error ;
2023-04-26 17:23:28 +02:00
use Exception ;
2023-06-15 11:47:50 +02:00
use WP_Query ;
2023-04-28 11:23:40 +02:00
use Activitypub\Http ;
2023-04-21 14:56:22 +02:00
use Activitypub\Webfinger ;
use Activitypub\Model\Activity ;
2023-04-24 20:46:51 +02:00
use Activitypub\Model\Follower ;
2023-04-21 14:56:22 +02:00
2023-05-02 14:39:25 +02:00
use function Activitypub\is_tombstone ;
2023-04-27 09:57:50 +02:00
use function Activitypub\get_remote_metadata_by_actor ;
2023-04-21 14:56:22 +02:00
/**
* ActivityPub Followers Collection
*
2023-06-12 18:38:15 +02:00
* @ author Matt Wiebe
2023-04-21 14:56:22 +02:00
* @ author Matthias Pfefferle
*/
class Followers {
2023-06-15 11:47:50 +02:00
const POST_TYPE = 'activitypub_follower' ;
2023-05-10 09:04:33 +02:00
const CACHE_KEY_INBOXES = 'follower_inboxes_%s' ;
2023-04-21 14:56:22 +02:00
/**
* Register WordPress hooks / actions and register Taxonomy
*
* @ return void
*/
public static function init () {
2023-06-12 18:38:15 +02:00
// register "followers" post_type
self :: register_post_type ();
2023-04-21 14:56:22 +02:00
\add_action ( 'activitypub_inbox_follow' , array ( self :: class , 'handle_follow_request' ), 10 , 2 );
\add_action ( 'activitypub_inbox_undo' , array ( self :: class , 'handle_undo_request' ), 10 , 2 );
2023-04-24 20:46:51 +02:00
\add_action ( 'activitypub_followers_post_follow' , array ( self :: class , 'send_follow_response' ), 10 , 4 );
2023-04-21 14:56:22 +02:00
}
/**
* Register the " Followers " Taxonomy
*
* @ return void
*/
2023-06-12 18:38:15 +02:00
private static function register_post_type () {
register_post_type (
self :: POST_TYPE ,
array (
'labels' => array (
'name' => _x ( 'Followers' , 'post_type plural name' , 'activitypub' ),
'singular_name' => _x ( 'Follower' , 'post_type single name' , 'activitypub' ),
),
2023-06-15 11:47:50 +02:00
'public' => true ,
2023-06-12 18:38:15 +02:00
'hierarchical' => false ,
'rewrite' => false ,
'query_var' => false ,
'delete_with_user' => false ,
'can_export' => true ,
'supports' => array (),
)
2023-04-21 14:56:22 +02:00
);
2023-06-12 18:38:15 +02:00
register_post_meta (
self :: POST_TYPE ,
2023-04-21 14:56:22 +02:00
'name' ,
array (
'type' => 'string' ,
'single' => true ,
2023-04-26 17:23:28 +02:00
'sanitize_callback' => function ( $value ) {
return sanitize_user ( $value );
},
2023-04-21 14:56:22 +02:00
)
);
2023-06-12 18:38:15 +02:00
register_post_meta (
self :: POST_TYPE ,
2023-04-21 14:56:22 +02:00
'username' ,
array (
'type' => 'string' ,
'single' => true ,
2023-04-26 17:23:28 +02:00
'sanitize_callback' => function ( $value ) {
return sanitize_user ( $value , true );
},
2023-04-21 14:56:22 +02:00
)
);
2023-06-12 18:38:15 +02:00
register_post_meta (
self :: POST_TYPE ,
2023-04-21 14:56:22 +02:00
'avatar' ,
array (
'type' => 'string' ,
'single' => true ,
2023-06-12 18:38:15 +02:00
'sanitize_callback' => array ( self :: class , 'sanitize_url' ),
)
);
2023-04-26 17:23:28 +02:00
2023-06-12 18:38:15 +02:00
register_post_meta (
self :: POST_TYPE ,
'url' ,
array (
'type' => 'string' ,
'single' => false ,
'sanitize_callback' => array ( self :: class , 'sanitize_url' ),
2023-04-21 14:56:22 +02:00
)
);
2023-06-12 18:38:15 +02:00
register_post_meta (
self :: POST_TYPE ,
2023-04-24 20:46:51 +02:00
'inbox' ,
2023-04-21 14:56:22 +02:00
array (
'type' => 'string' ,
'single' => true ,
2023-06-12 18:38:15 +02:00
'sanitize_callback' => array ( self :: class , 'sanitize_url' ),
2023-04-26 17:23:28 +02:00
)
);
2023-06-12 18:38:15 +02:00
register_post_meta (
self :: POST_TYPE ,
2023-04-26 17:23:28 +02:00
'shared_inbox' ,
array (
'type' => 'string' ,
'single' => true ,
2023-06-12 18:38:15 +02:00
'sanitize_callback' => array ( self :: class , 'sanitize_url' ),
2023-04-21 14:56:22 +02:00
)
);
2023-06-12 18:38:15 +02:00
register_post_meta (
self :: POST_TYPE ,
2023-04-24 20:46:51 +02:00
'updated_at' ,
2023-04-21 14:56:22 +02:00
array (
'type' => 'string' ,
'single' => true ,
2023-04-26 17:23:28 +02:00
'sanitize_callback' => function ( $value ) {
if ( ! is_numeric ( $value ) && ( int ) $value !== $value ) {
2023-05-10 18:52:13 +02:00
$value = \time ();
2023-04-26 17:23:28 +02:00
}
return $value ;
},
2023-04-21 14:56:22 +02:00
)
);
2023-06-12 18:38:15 +02:00
register_post_meta (
self :: POST_TYPE ,
2023-05-02 14:39:25 +02:00
'errors' ,
array (
'type' => 'string' ,
'single' => false ,
'sanitize_callback' => function ( $value ) {
if ( ! is_string ( $value ) ) {
throw new Exception ( 'Error message is no valid string' );
}
return esc_sql ( $value );
},
)
);
2023-06-12 18:38:15 +02:00
do_action ( 'activitypub_after_register_post_type' );
}
public static function sanitize_url ( $value ) {
if ( filter_var ( $value , FILTER_VALIDATE_URL ) === false ) {
return null ;
}
return esc_url_raw ( $value );
2023-04-21 14:56:22 +02:00
}
/**
* Handle the " Follow " Request
*
* @ param array $object The JSON " Follow " Activity
2023-04-25 09:09:07 +02:00
* @ param int $user_id The ID of the ID of the WordPress User
*
2023-04-21 14:56:22 +02:00
* @ return void
*/
public static function handle_follow_request ( $object , $user_id ) {
// save follower
2023-04-24 20:46:51 +02:00
$follower = self :: add_follower ( $user_id , $object [ 'actor' ] );
do_action ( 'activitypub_followers_post_follow' , $object [ 'actor' ], $object , $user_id , $follower );
2023-04-21 14:56:22 +02:00
}
/**
2023-05-09 10:08:51 +02:00
* Handle " Unfollow " requests
2023-04-21 14:56:22 +02:00
*
2023-04-25 09:09:07 +02:00
* @ param array $object The JSON " Undo " Activity
* @ param int $user_id The ID of the ID of the WordPress User
2023-04-21 14:56:22 +02:00
*/
public static function handle_undo_request ( $object , $user_id ) {
if (
isset ( $object [ 'object' ] ) &&
isset ( $object [ 'object' ][ 'type' ] ) &&
'Follow' === $object [ 'object' ][ 'type' ]
) {
self :: remove_follower ( $user_id , $object [ 'actor' ] );
}
}
/**
2023-05-09 10:08:51 +02:00
* Add new Follower
2023-04-21 14:56:22 +02:00
*
2023-04-25 09:09:07 +02:00
* @ param int $user_id The ID of the WordPress User
2023-04-21 15:57:49 +02:00
* @ param string $actor The Actor URL
2023-04-25 09:09:07 +02:00
*
2023-04-24 20:46:51 +02:00
* @ return array | WP_Error The Follower ( WP_Term array ) or an WP_Error
2023-04-21 14:56:22 +02:00
*/
public static function add_follower ( $user_id , $actor ) {
2023-04-27 09:57:50 +02:00
$meta = get_remote_metadata_by_actor ( $actor );
2023-05-03 14:50:16 +02:00
if ( empty ( $meta ) || ! is_array ( $meta ) || is_wp_error ( $meta ) ) {
return $meta ;
2023-04-27 09:57:50 +02:00
}
2023-06-15 11:47:50 +02:00
$follower = new Follower ( $actor );
2023-05-03 14:54:34 +02:00
$follower -> from_meta ( $meta );
2023-04-24 20:46:51 +02:00
$follower -> upsert ();
2023-04-21 14:56:22 +02:00
2023-06-16 11:40:26 +02:00
update_post_meta ( $follower -> get_id (), 'user_id' , $user_id , $user_id );
2023-06-15 11:47:50 +02:00
wp_cache_delete ( sprintf ( self :: CACHE_KEY_INBOXES , $user_id ), 'activitypub' );
return $follower ;
2023-04-21 14:56:22 +02:00
}
2023-04-24 20:46:51 +02:00
/**
* Remove a Follower
*
2023-04-26 17:24:27 +02:00
* @ param int $user_id The ID of the WordPress User
* @ param string $actor The Actor URL
2023-04-25 09:09:07 +02:00
*
* @ return bool | WP_Error True on success , false or WP_Error on failure .
2023-04-24 20:46:51 +02:00
*/
2023-04-21 16:25:15 +02:00
public static function remove_follower ( $user_id , $actor ) {
2023-05-10 09:04:33 +02:00
wp_cache_delete ( sprintf ( self :: CACHE_KEY_INBOXES , $user_id ), 'activitypub' );
2023-06-15 11:47:50 +02:00
2023-06-16 16:46:49 +02:00
$follower = self :: get_follower ( $user_id , $actor );
if ( ! $follower ) {
return false ;
}
return delete_post_meta ( $follower -> get_id (), 'user_id' , $user_id );
2023-04-21 16:25:15 +02:00
}
2023-04-21 15:57:49 +02:00
/**
2023-06-15 11:47:50 +02:00
* Get a Follower
2023-04-24 20:46:51 +02:00
*
2023-06-15 12:13:30 +02:00
* @ param int $user_id The ID of the WordPress User
* @ param string $actor The Actor URL
2023-04-25 09:09:07 +02:00
*
2023-04-26 17:22:44 +02:00
* @ return \Activitypub\Model\Follower The Follower object
2023-04-24 20:46:51 +02:00
*/
2023-06-15 12:13:30 +02:00
public static function get_follower ( $user_id , $actor ) {
2023-06-15 11:47:50 +02:00
global $wpdb ;
$post_id = $wpdb -> get_var (
$wpdb -> prepare (
2023-06-16 16:46:49 +02:00
" SELECT p.ID FROM $wpdb->posts p INNER JOIN $wpdb->postmeta pm ON p.ID = pm.post_id WHERE p.post_type = %s AND pm.meta_key = 'user_id' AND pm.meta_value = %d AND p.guid = %s " ,
array (
esc_sql ( self :: POST_TYPE ),
esc_sql ( $user_id ),
esc_sql ( $actor ),
)
2023-06-15 11:47:50 +02:00
)
);
if ( $post_id ) {
$post = get_post ( $post_id );
return new Follower ( $post );
}
return null ;
2023-04-24 20:46:51 +02:00
}
/**
* Send Accept response
2023-04-21 15:57:49 +02:00
*
2023-04-24 20:46:51 +02:00
* @ param string $actor The Actor URL
* @ param array $object The Activity object
2023-04-25 09:09:07 +02:00
* @ param int $user_id The ID of the WordPress User
2023-04-24 20:46:51 +02:00
* @ param Activitypub\Model\Follower $follower The Follower object
2023-04-25 09:09:07 +02:00
*
2023-04-21 15:57:49 +02:00
* @ return void
*/
2023-04-24 20:46:51 +02:00
public static function send_follow_response ( $actor , $object , $user_id , $follower ) {
2023-05-03 14:50:16 +02:00
if ( is_wp_error ( $follower ) ) {
// it is not even possible to send a "Reject" because
// we can not get the Remote-Inbox
return ;
}
2023-04-24 20:46:51 +02:00
2023-04-28 11:23:40 +02:00
if ( isset ( $object [ 'user_id' ] ) ) {
unset ( $object [ 'user_id' ] );
unset ( $object [ '@context' ] );
}
2023-04-21 14:56:22 +02:00
// get inbox
2023-04-24 20:46:51 +02:00
$inbox = $follower -> get_inbox ();
2023-04-21 14:56:22 +02:00
// send "Accept" activity
$activity = new Activity ( 'Accept' );
$activity -> set_object ( $object );
$activity -> set_actor ( \get_author_posts_url ( $user_id ) );
2023-04-24 20:46:51 +02:00
$activity -> set_to ( $actor );
$activity -> set_id ( \get_author_posts_url ( $user_id ) . '#follow-' . \preg_replace ( '~^https?://~' , '' , $actor ) );
2023-04-21 14:56:22 +02:00
$activity = $activity -> to_simple_json ();
2023-04-28 11:23:40 +02:00
$response = Http :: post ( $inbox , $activity , $user_id );
2023-04-21 14:56:22 +02:00
}
2023-04-21 15:57:49 +02:00
/**
* Get the Followers of a given user
*
2023-04-25 09:09:07 +02:00
* @ param int $user_id The ID of the WordPress User
* @ param string $output The output format , supported ARRAY_N , OBJECT and ACTIVITYPUB_OBJECT
* @ param int $number Limts the result
* @ param int $offset Offset
*
* @ return array The Term list of Followers , the format depends on $output
2023-04-21 15:57:49 +02:00
*/
2023-05-11 09:46:26 +02:00
public static function get_followers ( $user_id , $number = null , $offset = null , $args = array () ) {
2023-05-02 14:39:25 +02:00
$defaults = array (
2023-06-15 11:47:50 +02:00
'post_type' => self :: POST_TYPE ,
'posts_per_page' => $number ,
'offset' => $offset ,
'orderby' => 'ID' ,
'order' => 'DESC' ,
'meta_query' => array (
array (
'key' => 'user_id' ,
'value' => $user_id ,
),
),
2023-04-21 14:56:22 +02:00
);
2023-05-02 14:39:25 +02:00
$args = wp_parse_args ( $args , $defaults );
2023-06-12 18:38:15 +02:00
$query = new WP_Query ( $args );
2023-04-24 20:46:51 +02:00
$items = array ();
2023-06-12 18:38:15 +02:00
foreach ( $query -> get_posts () as $post ) {
$items [] = new Follower ( $post ); // phpcs:ignore
2023-04-24 20:46:51 +02:00
}
2023-05-11 09:46:26 +02:00
return $items ;
2023-04-21 14:56:22 +02:00
}
2023-05-22 10:58:13 +02:00
/**
* Get all Followers
*
2023-06-16 11:40:26 +02:00
* @ param array $args The WP_Query arguments .
2023-05-22 10:58:13 +02:00
*
* @ return array The Term list of Followers .
*/
2023-06-12 18:38:15 +02:00
public static function get_all_followers ( $user_id = null ) {
$args = array (
2023-06-16 11:40:26 +02:00
'meta_query' => array (),
2023-05-22 10:58:13 +02:00
);
2023-06-12 18:38:15 +02:00
return self :: get_followers ( $user_id , null , null , $args );
2023-05-22 10:58:13 +02:00
}
2023-04-21 15:57:49 +02:00
/**
* Count the total number of followers
*
2023-04-25 09:09:07 +02:00
* @ param int $user_id The ID of the WordPress User
*
* @ return int The number of Followers
2023-04-21 15:57:49 +02:00
*/
2023-06-16 11:40:26 +02:00
public static function count_followers ( $user_id ) {
2023-06-12 18:38:15 +02:00
// todo: rethink this. Don't we already get a total_posts count out of WP_Query?
// in the absence of that: caching.
2023-06-16 11:40:26 +02:00
return count ( self :: get_followers ( $user_id ) );
2023-04-21 14:56:22 +02:00
}
2023-04-25 09:09:07 +02:00
/**
* Returns all Inboxes fo a Users Followers
*
* @ param int $user_id The ID of the WordPress User
*
* @ return array The list of Inboxes
*/
2023-04-24 20:46:51 +02:00
public static function get_inboxes ( $user_id ) {
2023-05-09 10:28:23 +02:00
$cache_key = sprintf ( self :: CACHE_KEY_INBOXES , $user_id );
2023-05-10 09:04:33 +02:00
$inboxes = wp_cache_get ( $cache_key , 'activitypub' );
2023-05-09 10:28:23 +02:00
if ( $inboxes ) {
return $inboxes ;
}
2023-04-25 09:09:07 +02:00
// get all Followers of a ID of the WordPress User
2023-06-15 11:53:07 +02:00
$posts = new WP_Query (
2023-04-24 20:46:51 +02:00
array (
2023-06-15 11:47:50 +02:00
'post_type' => self :: POST_TYPE ,
2023-04-24 20:46:51 +02:00
'fields' => 'ids' ,
2023-05-02 14:39:25 +02:00
'meta_query' => array (
array (
'key' => 'inbox' ,
'compare' => 'EXISTS' ,
),
2023-06-15 11:53:07 +02:00
array (
'key' => 'user_id' ,
'value' => $user_id ,
),
2023-05-02 14:39:25 +02:00
),
2023-04-24 20:46:51 +02:00
)
);
2023-06-15 11:47:50 +02:00
$posts = $posts -> get_posts ();
2023-04-24 20:46:51 +02:00
2023-06-15 11:47:50 +02:00
if ( ! $posts ) {
2023-04-25 09:31:28 +02:00
return array ();
}
2023-04-24 20:46:51 +02:00
global $wpdb ;
$results = $wpdb -> get_col (
$wpdb -> prepare (
2023-06-16 11:40:26 +02:00
" SELECT DISTINCT meta_value FROM { $wpdb -> postmeta }
WHERE post_id IN ( " . implode( ', ', array_fill( 0, count( $posts ), '%d' ) ) . " )
2023-04-24 20:46:51 +02:00
AND meta_key = 'shared_inbox'
AND meta_value IS NOT NULL " ,
2023-06-16 11:40:26 +02:00
$posts
2023-04-24 20:46:51 +02:00
)
);
2023-05-09 10:28:23 +02:00
$inboxes = array_filter ( $results );
2023-05-10 09:04:33 +02:00
wp_cache_set ( $cache_key , $inboxes , 'activitypub' );
2023-05-09 10:28:23 +02:00
return $inboxes ;
2023-04-24 20:46:51 +02:00
}
2023-05-10 14:18:56 +02:00
/**
2023-05-10 14:55:10 +02:00
* Get all Followers that have not been updated for a given time
2023-05-10 14:18:56 +02:00
*
2023-05-10 14:55:10 +02:00
* @ param enum $output The output format , supported ARRAY_N , OBJECT and ACTIVITYPUB_OBJECT .
* @ param int $number Limits the result .
* @ param int $older_than The time in seconds .
*
* @ return mixed The Term list of Followers , the format depends on $output .
2023-05-10 14:18:56 +02:00
*/
2023-05-11 09:46:26 +02:00
public static function get_outdated_followers ( $number = 50 , $older_than = 604800 ) {
2023-05-10 14:18:56 +02:00
$args = array (
2023-06-15 12:17:48 +02:00
'post_type' => self :: POST_TYPE ,
'posts_per_page' => $number ,
'orderby' => 'modified' ,
'order' => 'DESC' ,
2023-06-16 11:40:26 +02:00
'post_status' => 'any' , // 'any' includes 'trash
'date_query' => array (
2023-05-10 14:18:56 +02:00
array (
2023-06-15 12:17:48 +02:00
'column' => 'post_modified_gmt' ,
2023-06-16 11:40:26 +02:00
'before' => gmdate ( 'Y-m-d' , \time () - $older_than ),
2023-05-10 14:18:56 +02:00
),
),
);
2023-06-15 12:17:48 +02:00
$posts = new WP_Query ( $args );
2023-05-10 14:18:56 +02:00
$items = array ();
2023-06-15 12:17:48 +02:00
foreach ( $posts -> get_posts () as $follower ) {
2023-06-12 18:38:15 +02:00
$items [] = new Follower ( $follower ); // phpcs:ignore
2023-05-10 14:18:56 +02:00
}
2023-05-11 09:46:26 +02:00
return $items ;
2023-05-10 14:18:56 +02:00
}
2023-05-10 14:55:10 +02:00
/**
* Get all Followers that had errors
*
* @ param enum $output The output format , supported ARRAY_N , OBJECT and ACTIVITYPUB_OBJECT
* @ param integer $number The number of Followers to return .
*
* @ return mixed The Term list of Followers , the format depends on $output .
*/
2023-05-11 09:46:26 +02:00
public static function get_faulty_followers ( $number = 10 ) {
2023-05-10 14:18:56 +02:00
$args = array (
2023-06-15 11:47:50 +02:00
'post_type' => self :: POST_TYPE ,
'posts_per_page' => $number ,
'meta_query' => array (
2023-05-10 14:18:56 +02:00
array (
2023-06-15 12:17:48 +02:00
'key' => 'errors' ,
'compare' => 'EXISTS' ,
2023-05-10 14:18:56 +02:00
),
),
);
2023-06-15 12:17:48 +02:00
$posts = new WP_Query ( $args );
2023-05-10 14:18:56 +02:00
$items = array ();
2023-06-15 12:17:48 +02:00
foreach ( $posts -> get_posts () as $follower ) {
2023-06-12 18:38:15 +02:00
$items [] = new Follower ( $follower ); // phpcs:ignore
2023-05-10 14:18:56 +02:00
}
2023-05-11 09:46:26 +02:00
return $items ;
2023-05-10 14:18:56 +02:00
}
2023-04-21 14:56:22 +02:00
}