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-04-21 14:56:22 +02:00
|
|
|
use WP_Term_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
|
|
|
|
*
|
|
|
|
* @author Matthias Pfefferle
|
|
|
|
*/
|
|
|
|
class Followers {
|
|
|
|
const TAXONOMY = 'activitypub-followers';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register WordPress hooks/actions and register Taxonomy
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function init() {
|
|
|
|
// register "followers" taxonomy
|
|
|
|
self::register_taxonomy();
|
|
|
|
|
|
|
|
\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
|
|
|
|
*/
|
|
|
|
public static function register_taxonomy() {
|
|
|
|
$args = array(
|
|
|
|
'labels' => array(
|
|
|
|
'name' => _x( 'Followers', 'taxonomy general name', 'activitypub' ),
|
|
|
|
'singular_name' => _x( 'Followers', 'taxonomy singular name', 'activitypub' ),
|
|
|
|
'menu_name' => __( 'Followers', 'activitypub' ),
|
|
|
|
),
|
|
|
|
'hierarchical' => false,
|
|
|
|
'show_ui' => false,
|
|
|
|
'show_in_menu' => false,
|
|
|
|
'show_in_nav_menus' => false,
|
|
|
|
'show_admin_column' => false,
|
|
|
|
'query_var' => false,
|
|
|
|
'rewrite' => false,
|
|
|
|
'public' => false,
|
|
|
|
'capabilities' => array(
|
|
|
|
'edit_terms' => null,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
register_taxonomy( self::TAXONOMY, 'user', $args );
|
|
|
|
register_taxonomy_for_object_type( self::TAXONOMY, 'user' );
|
|
|
|
|
|
|
|
register_term_meta(
|
|
|
|
self::TAXONOMY,
|
|
|
|
'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
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
register_term_meta(
|
|
|
|
self::TAXONOMY,
|
|
|
|
'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
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
register_term_meta(
|
|
|
|
self::TAXONOMY,
|
|
|
|
'avatar',
|
|
|
|
array(
|
|
|
|
'type' => 'string',
|
|
|
|
'single' => true,
|
2023-04-26 17:23:28 +02:00
|
|
|
'sanitize_callback' => function( $value ) {
|
|
|
|
if ( filter_var( $value, FILTER_VALIDATE_URL ) === false ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return esc_url_raw( $value );
|
|
|
|
},
|
2023-04-21 14:56:22 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
register_term_meta(
|
|
|
|
self::TAXONOMY,
|
2023-04-24 20:46:51 +02:00
|
|
|
'inbox',
|
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 ( filter_var( $value, FILTER_VALIDATE_URL ) === false ) {
|
|
|
|
throw new Exception( '"inbox" has to be a valid URL' );
|
|
|
|
}
|
|
|
|
|
|
|
|
return esc_url_raw( $value );
|
|
|
|
},
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
register_term_meta(
|
|
|
|
self::TAXONOMY,
|
|
|
|
'shared_inbox',
|
|
|
|
array(
|
|
|
|
'type' => 'string',
|
|
|
|
'single' => true,
|
|
|
|
'sanitize_callback' => function( $value ) {
|
|
|
|
if ( filter_var( $value, FILTER_VALIDATE_URL ) === false ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return esc_url_raw( $value );
|
|
|
|
},
|
2023-04-21 14:56:22 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
register_term_meta(
|
|
|
|
self::TAXONOMY,
|
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 ) {
|
|
|
|
$value = strtotime( 'now' );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
},
|
2023-04-21 14:56:22 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2023-05-02 14:39:25 +02:00
|
|
|
register_term_meta(
|
|
|
|
self::TAXONOMY,
|
|
|
|
'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-04-21 14:56:22 +02:00
|
|
|
do_action( 'activitypub_after_register_taxonomy' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles "Unfollow" requests
|
|
|
|
*
|
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-04-21 15:57:49 +02:00
|
|
|
* Add a 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-02 14:39:25 +02:00
|
|
|
$follower = new Follower( $actor );
|
|
|
|
|
|
|
|
if ( is_tombstone( $meta ) ) {
|
|
|
|
return;
|
|
|
|
} if ( empty( $meta ) || ! is_array( $meta ) || is_wp_error( $meta ) ) {
|
|
|
|
$follower->set_error( $meta );
|
|
|
|
} else {
|
|
|
|
$follower->from_meta( $meta );
|
2023-04-27 09:57:50 +02:00
|
|
|
}
|
|
|
|
|
2023-04-24 20:46:51 +02:00
|
|
|
$follower->upsert();
|
2023-04-21 14:56:22 +02:00
|
|
|
|
2023-04-24 20:46:51 +02:00
|
|
|
$result = wp_set_object_terms( $user_id, $follower->get_actor(), self::TAXONOMY, true );
|
2023-04-21 14:56:22 +02:00
|
|
|
|
2023-04-24 20:46:51 +02:00
|
|
|
if ( is_wp_error( $result ) ) {
|
|
|
|
return $result;
|
|
|
|
} else {
|
|
|
|
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-04-24 20:46:51 +02:00
|
|
|
return wp_remove_object_terms( $user_id, $actor, self::TAXONOMY );
|
2023-04-21 16:25:15 +02:00
|
|
|
}
|
|
|
|
|
2023-04-21 15:57:49 +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
|
|
|
*
|
2023-04-26 17:22:44 +02:00
|
|
|
* @return \Activitypub\Model\Follower The Follower object
|
2023-04-24 20:46:51 +02:00
|
|
|
*/
|
2023-04-26 17:22:44 +02:00
|
|
|
public static function get_follower( $user_id, $actor ) {
|
|
|
|
$terms = new WP_Term_Query(
|
|
|
|
array(
|
|
|
|
'name' => $actor,
|
|
|
|
'taxonomy' => self::TAXONOMY,
|
|
|
|
'hide_empty' => false,
|
|
|
|
'object_ids' => $user_id,
|
|
|
|
'number' => 1,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$term = $terms->get_terms();
|
|
|
|
|
|
|
|
if ( is_array( $term ) && ! empty( $term ) ) {
|
|
|
|
$term = reset( $term );
|
|
|
|
return new Follower( $term->name );
|
|
|
|
}
|
2023-04-24 20:46:51 +02:00
|
|
|
|
2023-04-26 17:22:44 +02:00
|
|
|
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 ) {
|
|
|
|
//if ( is_wp_error( $follower ) ) {
|
|
|
|
// @todo send error message
|
|
|
|
//}
|
|
|
|
|
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-02 14:39:25 +02:00
|
|
|
public static function get_followers( $user_id, $output = ARRAY_N, $number = null, $offset = null, $hide_errors = false, $args = array() ) {
|
|
|
|
$defaults = array(
|
|
|
|
'taxonomy' => self::TAXONOMY,
|
|
|
|
'hide_empty' => false,
|
|
|
|
'object_ids' => $user_id,
|
|
|
|
'number' => $number,
|
|
|
|
'offset' => $offset,
|
|
|
|
'orderby' => 'id',
|
|
|
|
'order' => 'ASC',
|
2023-04-21 14:56:22 +02:00
|
|
|
);
|
|
|
|
|
2023-05-02 14:39:25 +02:00
|
|
|
if ( true === $hide_errors ) {
|
|
|
|
$defaults['meta_query'] = array(
|
|
|
|
array(
|
|
|
|
'key' => 'errors',
|
|
|
|
'compare' => 'NOT EXISTS',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$args = wp_parse_args( $args, $defaults );
|
|
|
|
$terms = new WP_Term_Query( $args );
|
|
|
|
|
2023-04-24 20:46:51 +02:00
|
|
|
$items = array();
|
|
|
|
|
|
|
|
// change output format
|
|
|
|
switch ( $output ) {
|
|
|
|
case ACTIVITYPUB_OBJECT:
|
|
|
|
foreach ( $terms->get_terms() as $follower ) {
|
|
|
|
$items[] = new Follower( $follower->name ); // phpcs:ignore
|
|
|
|
}
|
|
|
|
return $items;
|
|
|
|
case OBJECT:
|
|
|
|
return $terms->get_terms();
|
|
|
|
case ARRAY_N:
|
|
|
|
default:
|
|
|
|
foreach ( $terms->get_terms() as $follower ) {
|
|
|
|
$items[] = $follower->name; // phpcs:ignore
|
|
|
|
}
|
|
|
|
return $items;
|
|
|
|
}
|
2023-04-21 14:56:22 +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-04-21 14:56:22 +02:00
|
|
|
public static function count_followers( $user_id ) {
|
|
|
|
return count( self::get_followers( $user_id ) );
|
|
|
|
}
|
|
|
|
|
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-04-25 09:09:07 +02:00
|
|
|
// get all Followers of a ID of the WordPress User
|
2023-04-24 20:46:51 +02:00
|
|
|
$terms = new WP_Term_Query(
|
|
|
|
array(
|
|
|
|
'taxonomy' => self::TAXONOMY,
|
|
|
|
'hide_empty' => false,
|
|
|
|
'object_ids' => $user_id,
|
|
|
|
'fields' => 'ids',
|
2023-05-02 14:39:25 +02:00
|
|
|
'meta_query' => array(
|
|
|
|
array(
|
|
|
|
'key' => 'inbox',
|
|
|
|
'compare' => 'EXISTS',
|
|
|
|
),
|
|
|
|
),
|
2023-04-24 20:46:51 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$terms = $terms->get_terms();
|
|
|
|
|
2023-04-25 09:31:28 +02:00
|
|
|
if ( ! $terms ) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2023-04-24 20:46:51 +02:00
|
|
|
global $wpdb;
|
|
|
|
$results = $wpdb->get_col(
|
|
|
|
$wpdb->prepare(
|
|
|
|
"SELECT DISTINCT meta_value FROM {$wpdb->termmeta}
|
|
|
|
WHERE term_id IN (" . implode( ', ', array_fill( 0, count( $terms ), '%d' ) ) . ")
|
|
|
|
AND meta_key = 'shared_inbox'
|
|
|
|
AND meta_value IS NOT NULL",
|
|
|
|
$terms
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
return array_filter( $results );
|
|
|
|
}
|
2023-04-21 14:56:22 +02:00
|
|
|
}
|