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

422 lines
7.6 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;
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 {
2023-04-25 09:09:07 +02:00
/**
* The Object ID
*
* @var int
*/
2023-04-24 20:46:51 +02:00
private $id;
2023-04-25 09:09:07 +02:00
/**
* The Actor-URL of the Follower
*
* @var string
*/
2023-04-24 20:46:51 +02:00
private $actor;
2023-04-25 09:09:07 +02:00
/**
* The Object Name
*
* This is the same as the Actor-URL
*
* @var string
*/
2023-04-24 20:46:51 +02:00
private $name;
2023-04-25 09:09:07 +02:00
/**
* The Username
*
* @var string
*/
2023-04-24 20:46:51 +02:00
private $username;
/**
* The User-Summary
*
* @var string
*/
private $summary;
2023-04-25 09:09:07 +02:00
/**
* The Avatar URL
*
* @var string
*/
2023-04-24 20:46:51 +02:00
private $avatar;
2023-04-25 09:09:07 +02:00
/**
* The URL to the Followers Inbox
*
* @var string
*/
2023-04-24 20:46:51 +02:00
private $inbox;
2023-04-25 09:09:07 +02:00
/**
* The URL to the Servers Shared-Inbox
*
* If the Server does not support Shared-Inboxes,
* the Inbox will be stored.
*
* @var string
*/
2023-04-24 20:46:51 +02:00
private $shared_inbox;
2023-04-25 09:09:07 +02:00
/**
* The date, the Follower was updated
*
* @var string untixtimestamp
*/
2023-04-24 20:46:51 +02:00
private $updated_at;
2023-04-25 09:09:07 +02:00
/**
* The complete Remote-Profile of the Follower
*
* @var array
*/
2023-04-24 20:46:51 +02:00
private $meta;
2023-05-02 14:39:25 +02:00
/**
* The latest received error.
*
* This will only temporary and will saved to $this->errors
*
* @var string
*/
private $error;
/**
* A list of errors
*
* @var array
*/
private $errors;
/**
* The WordPress User ID, or 0 for whole site.
* @var int
*/
private $user_id;
2023-04-25 09:09:07 +02:00
/**
* Maps the meta fields to the local db fields
*
* @var array
*/
2023-04-24 20:46:51 +02:00
private $map_meta = array(
'name' => 'name',
'preferredUsername' => 'username',
'inbox' => 'inbox',
);
/**
* Constructor
*
* @param string|WP_Post $actor The Actor-URL or WP_Post Object.
* @param int $user_id The WordPress User ID. 0 Represents the whole site.
2023-04-24 20:46:51 +02:00
*/
2023-06-15 11:48:09 +02:00
public function __construct( $actor ) {
$post = null;
if ( \is_a( $actor, 'WP_Post' ) ) {
$post = $actor;
} else {
2023-06-15 11:48:09 +02:00
global $wpdb;
$post_id = $wpdb->get_var(
$wpdb->prepare(
"SELECT ID FROM $wpdb->posts WHERE guid=%s",
esc_sql( $actor )
)
);
if ( $post_id ) {
$post = get_post( $post_id );
} else {
$this->actor = $actor;
}
}
if ( $post ) {
2023-06-15 11:48:09 +02:00
$this->id = $post->ID;
$this->actor = $post->guid;
$this->updated_at = $post->post_modified;
2023-04-24 20:46:51 +02:00
}
}
/**
* Magic function to implement getter and setter
*
2023-05-10 15:02:01 +02:00
* @param string $method The method name.
* @param string $params The method params.
2023-04-24 20:46:51 +02:00
*
* @return void
*/
public function __call( $method, $params ) {
$var = \strtolower( \substr( $method, 4 ) );
if ( \strncasecmp( $method, 'get', 3 ) === 0 ) {
if ( empty( $this->$var ) ) {
return $this->get( $var );
}
return $this->$var;
}
if ( \strncasecmp( $method, 'set', 3 ) === 0 ) {
$this->$var = $params[0];
}
}
2023-05-11 09:46:26 +02:00
/**
* Magic function to return the Actor-URL when the Object is used as a string
*
* @return string
*/
public function __toString() {
return $this->get_actor();
}
2023-05-10 15:02:01 +02:00
/**
* Prefill the Object with the meta data.
*
* @param array $meta The meta data.
*
* @return void
*/
public function from_meta( $meta ) {
$this->meta = $meta;
foreach ( $this->map_meta as $remote => $internal ) {
if ( ! empty( $meta[ $remote ] ) ) {
$this->$internal = $meta[ $remote ];
}
}
if ( ! empty( $meta['icon']['url'] ) ) {
$this->avatar = $meta['icon']['url'];
}
if ( ! empty( $meta['endpoints']['sharedInbox'] ) ) {
$this->shared_inbox = $meta['endpoints']['sharedInbox'];
} elseif ( ! empty( $meta['inbox'] ) ) {
$this->shared_inbox = $meta['inbox'];
}
$this->updated_at = \time();
}
2023-05-10 15:02:01 +02:00
/**
* Get the data by the given attribute
*
* @param string $attribute The attribute name.
*
* @return mixed The attribute value.
*/
2023-04-24 20:46:51 +02:00
public function get( $attribute ) {
if ( ! is_null( $this->$attribute ) ) {
2023-04-24 20:46:51 +02:00
return $this->$attribute;
}
2023-06-15 11:48:09 +02:00
$attribute_value = get_post_meta( $this->id, $attribute, true );
2023-06-15 11:48:09 +02:00
if ( $attribute_value ) {
$this->$attribute = $attribute_value;
return $attribute_value;
2023-04-24 20:46:51 +02:00
}
$attribute_value = $this->get_meta_by( $attribute );
if ( $attribute_value ) {
$this->$attribute = $attribute_value;
return $attribute_value;
}
return null;
2023-04-24 20:46:51 +02:00
}
2023-05-12 10:23:58 +02:00
/**
* Set new Error
*
* @param mixed $error The latest HTTP-Error.
*
* @return void
*/
public function set_error( $error ) {
$this->errors = array();
$this->error = $error;
}
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() {
if ( $this->errors ) {
return $this->errors;
}
$this->errors = get_post_meta( $this->id, 'errors' );
2023-05-02 14:39:25 +02:00
return $this->errors;
}
2023-05-10 15:02:01 +02:00
/**
* Reset (delete) all errors.
*
* @return void
*/
public function reset_errors() {
2023-06-15 11:48:09 +02:00
delete_post_meta( $this->id, '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() {
$errors = $this->get_errors();
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() {
$errors = $this->get_errors();
if ( is_array( $errors ) && ! empty( $errors ) ) {
return reset( $errors );
}
return '';
}
2023-05-10 15:02:01 +02:00
/**
* Get the meta data by the given attribute.
*
* @param string $attribute The attribute name.
*
* @return mixed $attribute The attribute value.
*/
public function get_meta_by( $attribute ) {
$meta = $this->get_meta();
if ( ! is_array( $meta ) ) {
return null;
}
2023-05-02 14:39:25 +02:00
// try mapped data (see $this->map_meta)
2023-04-24 20:46:51 +02:00
foreach ( $this->map_meta as $remote => $local ) {
if ( $attribute === $local && isset( $meta[ $remote ] ) ) {
return $meta[ $remote ];
}
}
return null;
}
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-16 11:40:26 +02:00
$this->updated_at = \time();
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() {
$args = array(
2023-06-15 11:48:09 +02:00
'ID' => $this->id,
'guid' => $this->actor,
'post_title' => $this->get_name(),
'post_author' => 0,
'post_type' => Followers::POST_TYPE,
'post_content' => $this->get_summary(),
2023-06-16 11:40:26 +02:00
'post_status' => 'publish',
2023-06-15 11:48:09 +02:00
'post_modified' => gmdate( 'Y-m-d H:i:s', $this->updated_at ),
'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() {
if ( $this->id ) {
$this->update();
} else {
$this->save();
}
}
2023-05-10 15:02:01 +02:00
/**
* Delete the current Follower-Object.
*
* @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() {
$attributes = array( 'inbox', 'shared_inbox', 'avatar', 'name', 'username', 'meta' );
$meta_input = array();
2023-04-24 20:46:51 +02:00
foreach ( $attributes as $attribute ) {
if ( $this->get( $attribute ) ) {
$meta_input[ $attribute ] = $this->get( $attribute );
2023-04-24 20:46:51 +02:00
}
}
2023-05-02 14:39:25 +02:00
if ( $this->error ) {
if ( is_string( $this->error ) ) {
$error = $this->error;
} elseif ( is_wp_error( $this->error ) ) {
$error = $this->error->get_error_message();
} else {
$error = __( 'Unknown Error or misconfigured Error-Message', 'activitypub' );
}
$meta_input['errors'] = array( $error );
2023-05-02 14:39:25 +02:00
}
return $meta_input;
2023-04-24 20:46:51 +02:00
}
}