2018-12-08 00:02:18 +01:00
|
|
|
<?php
|
|
|
|
|
2018-12-09 22:20:30 +01:00
|
|
|
class Db_Activitypub_Followers {
|
2018-12-08 00:02:18 +01:00
|
|
|
|
2018-12-20 11:33:08 +01:00
|
|
|
public static function get_followers( $author_id ) {
|
2019-02-17 21:09:24 +01:00
|
|
|
$followers = get_user_option( 'activitypub_followers', $author_id );
|
|
|
|
|
|
|
|
if ( ! $followers ) {
|
|
|
|
return $followers;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $followers as $key => $follower ) {
|
|
|
|
if (
|
|
|
|
is_array( $follower ) &&
|
|
|
|
isset( $follower['type'] ) &&
|
|
|
|
'Person' === $follower['type'] &&
|
|
|
|
isset( $follower['id'] ) &&
|
2019-02-19 22:43:14 +01:00
|
|
|
false !== filter_var( $follower['id'], FILTER_VALIDATE_URL )
|
2019-02-17 21:09:24 +01:00
|
|
|
) {
|
2019-02-19 22:43:14 +01:00
|
|
|
$followers[$key] = $follower['id'];
|
2019-02-17 21:09:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $followers;
|
2018-12-08 00:02:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function add_follower( $actor, $author_id ) {
|
|
|
|
$followers = get_user_option( 'activitypub_followers', $author_id );
|
|
|
|
|
2019-02-02 23:56:05 +01:00
|
|
|
if ( ! is_string( $actor ) ) {
|
|
|
|
if (
|
|
|
|
is_array( $actor ) &&
|
|
|
|
isset( $actor['type'] ) &&
|
|
|
|
'Person' === $actor['type'] &&
|
|
|
|
isset( $actor['id'] ) &&
|
2019-02-19 22:43:14 +01:00
|
|
|
false !== filter_var( $actor['id'], FILTER_VALIDATE_URL )
|
2019-02-02 23:56:05 +01:00
|
|
|
) {
|
|
|
|
$actor = $actor['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return new WP_Error( 'invalid_actor_object', __( 'Unknown Actor schema', 'activitypub' ), array(
|
|
|
|
'status' => 404
|
|
|
|
) );
|
|
|
|
}
|
|
|
|
|
2018-12-08 00:02:18 +01:00
|
|
|
if ( ! is_array( $followers ) ) {
|
|
|
|
$followers = array( $actor );
|
|
|
|
} else {
|
|
|
|
$followers[] = $actor;
|
|
|
|
}
|
|
|
|
|
|
|
|
$followers = array_unique( $followers );
|
|
|
|
|
|
|
|
update_user_meta( $author_id, 'activitypub_followers', $followers );
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function remove_follower( $actor, $author_id ) {
|
|
|
|
$followers = get_user_option( 'activitypub_followers', $author_id );
|
|
|
|
|
|
|
|
foreach ( $followers as $key => $value ) {
|
|
|
|
if ( $value === $actor) {
|
|
|
|
unset( $followers[$key] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
update_user_meta( $author_id, 'activitypub_followers', $followers );
|
|
|
|
}
|
|
|
|
}
|