wordpress-activitypub/includes/peer/class-followers.php

81 lines
1.8 KiB
PHP
Raw Permalink Normal View History

2018-12-08 00:02:18 +01:00
<?php
2019-11-18 20:57:00 +01:00
namespace Activitypub\Peer;
2018-12-08 00:02:18 +01:00
2019-02-24 13:01:28 +01:00
/**
* ActivityPub Followers DB-Class
*
* @author Matthias Pfefferle
*/
class Followers {
2018-12-08 00:02:18 +01:00
public static function get_followers( $author_id ) {
2019-09-27 10:12:59 +02:00
$followers = \get_user_option( 'activitypub_followers', $author_id );
2019-02-17 21:09:24 +01:00
if ( ! $followers ) {
2019-03-12 22:19:51 +01:00
return array();
2019-02-17 21:09:24 +01:00
}
foreach ( $followers as $key => $follower ) {
if (
2019-09-27 10:12:59 +02:00
\is_array( $follower ) &&
2019-02-17 21:09:24 +01:00
isset( $follower['type'] ) &&
'Person' === $follower['type'] &&
isset( $follower['id'] ) &&
2019-09-27 10:12:59 +02:00
false !== \filter_var( $follower['id'], FILTER_VALIDATE_URL )
2019-02-17 21:09:24 +01:00
) {
$followers[ $key ] = $follower['id'];
2019-02-17 21:09:24 +01:00
}
}
return $followers;
2018-12-08 00:02:18 +01:00
}
2019-08-21 10:38:43 +02:00
public static function count_followers( $author_id ) {
$followers = self::get_followers( $author_id );
2019-09-27 10:12:59 +02:00
return \count( $followers );
2019-08-21 10:38:43 +02:00
}
2018-12-08 00:02:18 +01:00
public static function add_follower( $actor, $author_id ) {
2019-09-27 10:12:59 +02:00
$followers = \get_user_option( 'activitypub_followers', $author_id );
2018-12-08 00:02:18 +01:00
2019-09-27 10:12:59 +02:00
if ( ! \is_string( $actor ) ) {
2019-02-02 23:56:05 +01:00
if (
2019-09-27 10:12:59 +02:00
\is_array( $actor ) &&
2019-02-02 23:56:05 +01:00
isset( $actor['type'] ) &&
'Person' === $actor['type'] &&
isset( $actor['id'] ) &&
2019-09-27 10:12:59 +02:00
false !== \filter_var( $actor['id'], FILTER_VALIDATE_URL )
2019-02-02 23:56:05 +01:00
) {
$actor = $actor['id'];
}
2019-09-27 10:12:59 +02:00
return new \WP_Error( 'invalid_actor_object', \__( 'Unknown Actor schema', 'activitypub' ), array(
'status' => 404,
2019-02-02 23:56:05 +01:00
) );
}
2019-09-27 10:12:59 +02:00
if ( ! \is_array( $followers ) ) {
2018-12-08 00:02:18 +01:00
$followers = array( $actor );
} else {
$followers[] = $actor;
}
2019-09-27 10:12:59 +02:00
$followers = \array_unique( $followers );
2018-12-08 00:02:18 +01:00
2019-09-27 10:12:59 +02:00
\update_user_meta( $author_id, 'activitypub_followers', $followers );
2018-12-08 00:02:18 +01:00
}
public static function remove_follower( $actor, $author_id ) {
2019-09-27 10:12:59 +02:00
$followers = \get_user_option( 'activitypub_followers', $author_id );
2018-12-08 00:02:18 +01:00
foreach ( $followers as $key => $value ) {
if ( $value === $actor ) {
unset( $followers[ $key ] );
2018-12-08 00:02:18 +01:00
}
}
2019-09-27 10:12:59 +02:00
\update_user_meta( $author_id, 'activitypub_followers', $followers );
2018-12-08 00:02:18 +01:00
}
}