wordpress-activitypub/includes/model/class-follower.php

333 lines
6.9 KiB
PHP
Raw Normal View History

2023-04-24 20:46:51 +02:00
<?php
namespace Activitypub\Model;
2023-06-15 11:48:09 +02:00
use WP_Query;
use Activitypub\Activity\Actor;
2023-04-24 20:46:51 +02:00
use Activitypub\Collection\Followers;
/**
* ActivityPub Follower Class
*
2023-04-25 09:09:07 +02:00
* This Object represents a single Follower.
* There is no direct reference to a WordPress User here.
*
* @author Matt Wiebe
2023-04-24 20:46:51 +02:00
* @author Matthias Pfefferle
*
* @see https://www.w3.org/TR/activitypub/#follow-activity-inbox
*/
class Follower extends Actor {
2023-04-25 09:09:07 +02:00
/**
* The complete Remote-Profile of the Follower
2023-04-25 09:09:07 +02:00
*
* @var array
2023-04-25 09:09:07 +02:00
*/
protected $_id; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
2023-04-24 20:46:51 +02:00
2023-05-10 15:02:01 +02:00
/**
* Get the errors.
*
* @return mixed
*/
2023-05-02 14:39:25 +02:00
public function get_errors() {
2023-07-06 14:42:18 +02:00
return get_post_meta( $this->_id, 'activitypub_errors' );
2023-05-02 14:39:25 +02:00
}
2023-07-28 00:39:22 +02:00
/**
* Get the Summary.
*
* @return int The Summary.
*/
public function get_summary() {
if ( isset( $this->summary ) ) {
return $this->summary;
}
return '';
}
2023-07-10 10:28:45 +02:00
/**
* Getter for URL attribute.
*
* Falls back to ID, if no URL is set. This is relevant for
* Plattforms like Lemmy, where the ID is the URL.
*
* @return string The URL.
*/
public function get_url() {
if ( $this->url ) {
return $this->url;
}
return $this->id;
}
2023-05-10 15:02:01 +02:00
/**
* Reset (delete) all errors.
*
* @return void
*/
public function reset_errors() {
2023-07-06 14:42:18 +02:00
delete_post_meta( $this->_id, 'activitypub_errors' );
}
2023-05-10 15:02:01 +02:00
/**
* Count the errors.
*
* @return int The number of errors.
*/
2023-05-02 14:39:25 +02:00
public function count_errors() {
2023-07-06 14:42:18 +02:00
$errors = $this->get_errors();
2023-05-02 14:39:25 +02:00
if ( is_array( $errors ) && ! empty( $errors ) ) {
return count( $errors );
}
return 0;
}
2023-05-10 15:02:01 +02:00
/**
* Return the latest error message.
*
* @return string The error message.
*/
2023-05-02 14:39:25 +02:00
public function get_latest_error_message() {
2023-07-06 14:42:18 +02:00
$errors = $this->get_errors();
2023-05-02 14:39:25 +02:00
if ( is_array( $errors ) && ! empty( $errors ) ) {
return reset( $errors );
}
return '';
}
2023-05-10 15:02:01 +02:00
/**
* Update the current Follower-Object.
*
* @return void
*/
2023-04-24 20:46:51 +02:00
public function update() {
2023-06-15 11:48:09 +02:00
$this->save();
2023-04-24 20:46:51 +02:00
}
2023-05-10 15:02:01 +02:00
/**
* Save the current Follower-Object.
*
* @return void
*/
2023-06-15 11:48:09 +02:00
public function save() {
2023-07-06 14:42:18 +02:00
if ( ! $this->get__id() ) {
global $wpdb;
$post_id = $wpdb->get_var(
$wpdb->prepare(
"SELECT ID FROM $wpdb->posts WHERE guid=%s",
esc_sql( $this->get_id() )
)
);
if ( $post_id ) {
$post = get_post( $post_id );
$this->set__id( $post->ID );
}
}
$args = array(
2023-07-06 14:42:18 +02:00
'ID' => $this->get__id(),
'guid' => esc_url_raw( $this->get_id() ),
2023-07-28 00:39:22 +02:00
'post_title' => wp_strip_all_tags( sanitize_text_field( $this->get_name() ) ),
2023-07-06 14:42:18 +02:00
'post_author' => 0,
'post_type' => Followers::POST_TYPE,
'post_name' => esc_url_raw( $this->get_id() ),
2023-07-28 00:39:22 +02:00
'post_excerpt' => sanitize_text_field( wp_kses( $this->get_summary(), 'user_description' ) ),
2023-07-06 14:42:18 +02:00
'post_status' => 'publish',
'meta_input' => $this->get_post_meta_input(),
2023-04-24 20:46:51 +02:00
);
2023-06-15 11:48:09 +02:00
$post_id = wp_insert_post( $args );
$this->_id = $post_id;
2023-04-24 20:46:51 +02:00
}
2023-05-10 15:02:01 +02:00
/**
* Upsert the current Follower-Object.
*
* @return void
*/
2023-04-24 20:46:51 +02:00
public function upsert() {
2023-07-06 14:42:18 +02:00
$this->save();
2023-04-24 20:46:51 +02:00
}
2023-05-10 15:02:01 +02:00
/**
* Delete the current Follower-Object.
*
2023-07-06 14:42:18 +02:00
* Beware that this os deleting a Follower for ALL users!!!
*
* To delete only the User connection (unfollow)
* @see \Activitypub\Rest\Followers::remove_follower()
*
2023-05-10 15:02:01 +02:00
* @return void
*/
public function delete() {
wp_delete_post( $this->_id );
}
2023-05-10 15:02:01 +02:00
/**
2023-06-15 11:48:09 +02:00
* Update the post meta.
2023-05-10 15:02:01 +02:00
*
* @return void
*/
protected function get_post_meta_input() {
$meta_input = array();
2023-07-06 16:10:48 +02:00
$meta_input['activitypub_inbox'] = $this->get_shared_inbox();
$meta_input['activitypub_actor_json'] = $this->to_json();
return $meta_input;
2023-04-24 20:46:51 +02:00
}
/**
* Get the icon.
*
* Sets a fallback to better handle API and HTML outputs.
*
* @return array The icon.
*/
public function get_icon() {
if ( isset( $this->icon['url'] ) ) {
return $this->icon;
}
return array(
'type' => 'Image',
'mediaType' => 'image/jpeg',
'url' => ACTIVITYPUB_PLUGIN_URL . 'assets/img/mp.jpg',
);
}
/**
* Get Name.
*
* Tries to extract a name from the URL or ID if not set.
*
* @return string The name.
*/
public function get_name() {
2023-07-28 00:47:20 +02:00
if ( $this->name ) {
return $this->name;
}
return $this->extract_name_from_uri();
}
/**
* The preferred Username.
*
* Tries to extract a name from the URL or ID if not set.
*
* @return string The preferred Username.
*/
public function get_preferred_username() {
2023-07-28 00:47:20 +02:00
if ( $this->preferred_username ) {
return $this->preferred_username;
}
return $this->extract_name_from_uri();
}
/**
* Get the Icon URL (Avatar)
*
* @return string The URL to the Avatar.
*/
public function get_icon_url() {
$icon = $this->get_icon();
if ( ! $icon ) {
return '';
}
if ( is_array( $icon ) ) {
return $icon['url'];
}
return $icon;
}
/**
2023-07-06 14:42:18 +02:00
* Get the shared inbox, with a fallback to the inbox.
*
2023-07-06 14:42:18 +02:00
* @return string|null The URL to the shared inbox, the inbox or null.
*/
2023-07-06 14:42:18 +02:00
public function get_shared_inbox() {
if ( ! empty( $this->get_endpoints()['sharedInbox'] ) ) {
return $this->get_endpoints()['sharedInbox'];
} elseif ( ! empty( $this->get_inbox() ) ) {
return $this->get_inbox();
}
2023-07-06 14:42:18 +02:00
return null;
}
/**
* Convert a Custom-Post-Type input to an Activitypub\Model\Follower.
*
* @return string The JSON string.
*
* @return array Activitypub\Model\Follower
*/
2023-07-06 14:42:18 +02:00
public static function init_from_cpt( $post ) {
2023-07-06 16:10:48 +02:00
$actor_json = get_post_meta( $post->ID, 'activitypub_actor_json', true );
$object = self::init_from_json( $actor_json );
$object->set__id( $post->ID );
$object->set_id( $post->guid );
2023-07-28 00:39:22 +02:00
$object->set_name( $post->post_title );
$object->set_summary( $post->post_excerpt );
$object->set_published( gmdate( 'Y-m-d H:i:s', strtotime( $post->post_published ) ) );
$object->set_updated( gmdate( 'Y-m-d H:i:s', strtotime( $post->post_modified ) ) );
return $object;
}
/**
* Infer a shortname from the Actor ID or URL. Used only for fallbacks,
* we will try to use what's supplied.
*
* @return string Hopefully the name of the Follower.
*/
protected function extract_name_from_uri() {
// prefer the URL, but fall back to the ID.
if ( $this->url ) {
$name = $this->url;
} else {
$name = $this->id;
}
if ( \filter_var( $name, FILTER_VALIDATE_URL ) ) {
$name = \rtrim( $name, '/' );
$path = \wp_parse_url( $name, PHP_URL_PATH );
if ( $path ) {
if ( \strpos( $name, '@' ) !== false ) {
// expected: https://example.com/@user (default URL pattern)
$name = \preg_replace( '|^/@?|', '', $path );
} else {
// expected: https://example.com/users/user (default ID pattern)
$parts = \explode( '/', $path );
$name = \array_pop( $parts );
}
}
} elseif (
\is_email( $name ) ||
\strpos( $name, 'acct' ) === 0 ||
\strpos( $name, '@' ) === 0
) {
// expected: user@example.com or acct:user@example (WebFinger)
$name = \ltrim( $name, '@' );
$name = \ltrim( $name, 'acct:' );
$parts = \explode( '@', $name );
$name = $parts[0];
}
return $name;
}
2023-04-24 20:46:51 +02:00
}