replace filters

This commit is contained in:
Matthias Pfefferle 2023-06-28 16:43:41 +02:00
parent 43db2f2707
commit c02702f773
3 changed files with 87 additions and 62 deletions

View file

@ -78,4 +78,24 @@ class Application_User extends Blog_User {
\update_option( 'activitypub_application_user_private_key', $key_pair['private_key'] ); \update_option( 'activitypub_application_user_private_key', $key_pair['private_key'] );
} }
} }
public function get_inbox() {
return null;
}
public function get_outbox() {
return null;
}
public function get_followers() {
return null;
}
public function get_following() {
return null;
}
public function get_attachment() {
return array();
}
} }

View file

@ -5,6 +5,8 @@ use WP_Query;
use Activitypub\Signature; use Activitypub\Signature;
use Activitypub\User_Factory; use Activitypub\User_Factory;
use function Activitypub\is_user_disabled;
class Blog_User extends User { class Blog_User extends User {
/** /**
* The User-ID * The User-ID
@ -20,13 +22,19 @@ class Blog_User extends User {
*/ */
protected $type = 'Person'; protected $type = 'Person';
/** public static function from_wp_user( $user_id ) {
* The User constructor. if ( is_user_disabled( $user_id ) ) {
* return new WP_Error(
* @param int $user_id The User-ID. 'activitypub_user_not_found',
*/ \__( 'User not found', 'activitypub' ),
public function __construct() { array( 'status' => 404 )
add_filter( 'activitypub_activity_blog_user_object_array', array( $this, 'add_api_endpoints' ), 10, 2 ); );
}
$object = new static();
$object->_id = $user_id;
return $object;
} }
/** /**
@ -181,4 +189,8 @@ class Blog_User extends User {
\update_option( 'activitypub_blog_user_private_key', $key_pair['private_key'] ); \update_option( 'activitypub_blog_user_private_key', $key_pair['private_key'] );
} }
} }
public function get_attachment() {
return array();
}
} }

View file

@ -26,19 +26,16 @@ class User extends Actor {
*/ */
protected $type = 'Person'; protected $type = 'Person';
/**
* The User constructor.
*
* @param numeric $user_id The User-ID.
*/
public function __construct() {
add_filter( 'activitypub_activity_user_object_array', array( $this, 'add_api_endpoints' ), 10, 2 );
add_filter( 'activitypub_activity_user_object_array', array( $this, 'add_attachments' ), 10, 2 );
}
public static function from_wp_user( $user_id ) { public static function from_wp_user( $user_id ) {
if ( is_user_disabled( $user_id ) ) { if (
return null; is_user_disabled( $user_id ) ||
! get_user_by( 'id', $user_id )
) {
return new WP_Error(
'activitypub_user_not_found',
\__( 'User not found', 'activitypub' ),
array( 'status' => 404 )
);
} }
$object = new static(); $object = new static();
@ -47,26 +44,6 @@ class User extends Actor {
return $object; return $object;
} }
/**
* Magic function to implement getter and setter
*
* @param string $method
* @param string $params
*
* @return void
*/
public function __call( $method, $params ) {
$var = \strtolower( \substr( $method, 4 ) );
if ( \strncasecmp( $method, 'get', 3 ) === 0 ) {
return $this->$var;
}
if ( \strncasecmp( $method, 'has', 3 ) === 0 ) {
return (bool) call_user_func( 'get_' . $var, $this );
}
}
/** /**
* Get the User-ID. * Get the User-ID.
* *
@ -158,7 +135,7 @@ class User extends Actor {
} }
/** /**
* @param int $user_id * @param int $this->get__id()
* *
* @return mixed * @return mixed
*/ */
@ -184,34 +161,50 @@ class User extends Actor {
} }
/** /**
* Extend the User-Output with API-Endpoints. * Returns the Inbox-API-Endpoint.
* *
* @param array $array The User-Output. * @return string The Inbox-Endpoint.
* @param numeric $user_id The User-ID.
*
* @return array The extended User-Output.
*/ */
public function add_api_endpoints( $array, $user_id ) { public function get_inbox() {
$array['inbox'] = get_rest_url_by_path( sprintf( 'users/%d/inbox', $user_id ) ); return get_rest_url_by_path( sprintf( 'users/%d/inbox', $this->get__id() ) );
$array['outbox'] = get_rest_url_by_path( sprintf( 'users/%d/outbox', $user_id ) ); }
$array['followers'] = get_rest_url_by_path( sprintf( 'users/%d/followers', $user_id ) );
$array['following'] = get_rest_url_by_path( sprintf( 'users/%d/following', $user_id ) );
return $array; /**
* Returns the Outbox-API-Endpoint.
*
* @return string The Outbox-Endpoint.
*/
public function get_outbox() {
return get_rest_url_by_path( sprintf( 'users/%d/outbox', $this->get__id() ) );
}
/**
* Returns the Followers-API-Endpoint.
*
* @return string The Followers-Endpoint.
*/
public function get_followers() {
return get_rest_url_by_path( sprintf( 'users/%d/followers', $this->get__id() ) );
}
/**
* Returns the Following-API-Endpoint.
*
* @return string The Following-Endpoint.
*/
public function get_following() {
return get_rest_url_by_path( sprintf( 'users/%d/following', $this->get__id() ) );
} }
/** /**
* Extend the User-Output with Attachments. * Extend the User-Output with Attachments.
* *
* @param array $array The User-Output.
* @param numeric $user_id The User-ID.
*
* @return array The extended User-Output. * @return array The extended User-Output.
*/ */
public function add_attachments( $array, $user_id ) { public function get_attachment() {
$array['attachment'] = array(); $array = array();
$array['attachment']['blog_url'] = array( $array[] = array(
'type' => 'PropertyValue', 'type' => 'PropertyValue',
'name' => \__( 'Blog', 'activitypub' ), 'name' => \__( 'Blog', 'activitypub' ),
'value' => \html_entity_decode( 'value' => \html_entity_decode(
@ -221,22 +214,22 @@ class User extends Actor {
), ),
); );
$array['attachment']['profile_url'] = array( $array[] = array(
'type' => 'PropertyValue', 'type' => 'PropertyValue',
'name' => \__( 'Profile', 'activitypub' ), 'name' => \__( 'Profile', 'activitypub' ),
'value' => \html_entity_decode( 'value' => \html_entity_decode(
'<a rel="me" title="' . \esc_attr( \get_author_posts_url( $user_id ) ) . '" target="_blank" href="' . \get_author_posts_url( $user_id ) . '">' . \wp_parse_url( \get_author_posts_url( $user_id ), \PHP_URL_HOST ) . '</a>', '<a rel="me" title="' . \esc_attr( \get_author_posts_url( $this->get__id() ) ) . '" target="_blank" href="' . \get_author_posts_url( $this->get__id() ) . '">' . \wp_parse_url( \get_author_posts_url( $this->get__id() ), \PHP_URL_HOST ) . '</a>',
\ENT_QUOTES, \ENT_QUOTES,
'UTF-8' 'UTF-8'
), ),
); );
if ( \get_the_author_meta( 'user_url', $user_id ) ) { if ( \get_the_author_meta( 'user_url', $this->get__id() ) ) {
$array['attachment']['user_url'] = array( $array[] = array(
'type' => 'PropertyValue', 'type' => 'PropertyValue',
'name' => \__( 'Website', 'activitypub' ), 'name' => \__( 'Website', 'activitypub' ),
'value' => \html_entity_decode( 'value' => \html_entity_decode(
'<a rel="me" title="' . \esc_attr( \get_the_author_meta( 'user_url', $user_id ) ) . '" target="_blank" href="' . \get_the_author_meta( 'user_url', $user_id ) . '">' . \wp_parse_url( \get_the_author_meta( 'user_url', $user_id ), \PHP_URL_HOST ) . '</a>', '<a rel="me" title="' . \esc_attr( \get_the_author_meta( 'user_url', $this->get__id() ) ) . '" target="_blank" href="' . \get_the_author_meta( 'user_url', $this->get__id() ) . '">' . \wp_parse_url( \get_the_author_meta( 'user_url', $this->get__id() ), \PHP_URL_HOST ) . '</a>',
\ENT_QUOTES, \ENT_QUOTES,
'UTF-8' 'UTF-8'
), ),