2023-04-24 20:46:51 +02:00
|
|
|
<?php
|
|
|
|
namespace Activitypub\Model;
|
|
|
|
|
2023-06-15 11:48:09 +02:00
|
|
|
use WP_Query;
|
2023-06-23 14:54:29 +02:00
|
|
|
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.
|
|
|
|
*
|
2023-06-12 18:38:15 +02:00
|
|
|
* @author Matt Wiebe
|
2023-04-24 20:46:51 +02:00
|
|
|
* @author Matthias Pfefferle
|
|
|
|
*
|
|
|
|
* @see https://www.w3.org/TR/activitypub/#follow-activity-inbox
|
|
|
|
*/
|
2023-06-23 14:54:29 +02:00
|
|
|
class Follower extends Actor {
|
2023-04-25 09:09:07 +02:00
|
|
|
/**
|
2023-06-23 14:54:29 +02:00
|
|
|
* The complete Remote-Profile of the Follower
|
2023-04-25 09:09:07 +02:00
|
|
|
*
|
2023-06-23 14:54:29 +02:00
|
|
|
* @var array
|
2023-04-25 09:09:07 +02:00
|
|
|
*/
|
2023-06-23 14:54:29 +02:00
|
|
|
protected $_id; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
|
2023-04-24 20:46:51 +02:00
|
|
|
|
2023-04-25 09:09:07 +02:00
|
|
|
/**
|
2023-06-23 14:54:29 +02:00
|
|
|
* The complete Remote-Profile of the Follower
|
2023-04-25 09:09:07 +02:00
|
|
|
*
|
2023-06-23 14:54:29 +02:00
|
|
|
* @var array
|
2023-04-25 09:09:07 +02:00
|
|
|
*/
|
2023-06-23 14:54:29 +02:00
|
|
|
protected $_shared_inbox; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
|
2023-04-24 20:46:51 +02:00
|
|
|
|
2023-04-25 09:09:07 +02:00
|
|
|
/**
|
|
|
|
* The complete Remote-Profile of the Follower
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2023-06-23 14:54:29 +02:00
|
|
|
protected $_actor; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
|
2023-04-24 20:46:51 +02:00
|
|
|
|
2023-05-02 14:39:25 +02:00
|
|
|
/**
|
|
|
|
* The latest received error.
|
|
|
|
*
|
|
|
|
* This will only temporary and will saved to $this->errors
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2023-06-23 14:54:29 +02:00
|
|
|
protected $_error; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
|
2023-05-02 14:39:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A list of errors
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2023-06-23 14:54:29 +02:00
|
|
|
protected $_errors; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
|
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 ) {
|
2023-06-23 14:54:29 +02:00
|
|
|
$this->_errors = array();
|
|
|
|
$this->_error = $error;
|
2023-05-12 10:23:58 +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-06-23 14:54:29 +02:00
|
|
|
if ( $this->_errors ) {
|
|
|
|
return $this->_errors;
|
2023-05-02 14:39:25 +02:00
|
|
|
}
|
|
|
|
|
2023-06-23 14:54:29 +02:00
|
|
|
$this->_errors = get_post_meta( $this->_id, 'errors' );
|
|
|
|
return $this->_errors;
|
2023-05-02 14:39:25 +02:00
|
|
|
}
|
|
|
|
|
2023-05-10 15:02:01 +02:00
|
|
|
/**
|
|
|
|
* Reset (delete) all errors.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2023-05-10 14:18:56 +02:00
|
|
|
public function reset_errors() {
|
2023-06-23 14:54:29 +02:00
|
|
|
delete_post_meta( $this->_id, 'errors' );
|
2023-05-10 14:18:56 +02:00
|
|
|
}
|
|
|
|
|
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-06-23 14:54:29 +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-06-23 14:54:29 +02:00
|
|
|
public function get_url() {
|
|
|
|
if ( ! $this->url ) {
|
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->url;
|
|
|
|
}
|
|
|
|
|
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-06-23 14:54:29 +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-06-12 18:38:15 +02:00
|
|
|
$args = array(
|
2023-06-23 14:54:29 +02:00
|
|
|
'ID' => $this->get__id(),
|
|
|
|
'guid' => $this->get_id(),
|
2023-06-15 11:48:09 +02:00
|
|
|
'post_title' => $this->get_name(),
|
|
|
|
'post_author' => 0,
|
|
|
|
'post_type' => Followers::POST_TYPE,
|
2023-07-05 12:18:48 +02:00
|
|
|
'post_name' => $this->get_id(),
|
2023-06-22 10:01:15 +02:00
|
|
|
'post_content' => $this->get_summary(),
|
2023-06-16 11:40:26 +02:00
|
|
|
'post_status' => 'publish',
|
2023-06-15 11:48:09 +02:00
|
|
|
'meta_input' => $this->get_post_meta_input(),
|
2023-04-24 20:46:51 +02:00
|
|
|
);
|
2023-06-23 14:54:29 +02:00
|
|
|
|
2023-06-15 11:48:09 +02:00
|
|
|
$post_id = wp_insert_post( $args );
|
2023-06-23 14:54:29 +02:00
|
|
|
$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-06-23 14:54:29 +02:00
|
|
|
if ( $this->_id ) {
|
2023-04-24 20:46:51 +02:00
|
|
|
$this->update();
|
|
|
|
} else {
|
|
|
|
$this->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-10 15:02:01 +02:00
|
|
|
/**
|
|
|
|
* Delete the current Follower-Object.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2023-05-10 14:18:56 +02:00
|
|
|
public function delete() {
|
2023-06-23 14:54:29 +02:00
|
|
|
wp_delete_post( $this->_id );
|
2023-05-10 14:18:56 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2023-06-12 18:38:15 +02:00
|
|
|
protected function get_post_meta_input() {
|
2023-06-23 14:54:29 +02:00
|
|
|
$attributes = array( 'inbox', '_shared_inbox', 'icon', 'preferred_username', '_actor', 'url' );
|
2023-06-12 18:38:15 +02:00
|
|
|
|
|
|
|
$meta_input = array();
|
2023-04-24 20:46:51 +02:00
|
|
|
|
2023-04-27 09:57:50 +02:00
|
|
|
foreach ( $attributes as $attribute ) {
|
|
|
|
if ( $this->get( $attribute ) ) {
|
2023-06-12 18:38:15 +02:00
|
|
|
$meta_input[ $attribute ] = $this->get( $attribute );
|
2023-04-24 20:46:51 +02:00
|
|
|
}
|
|
|
|
}
|
2023-05-02 14:39:25 +02:00
|
|
|
|
2023-06-23 14:54:29 +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();
|
2023-05-02 14:39:25 +02:00
|
|
|
} else {
|
2023-06-23 14:54:29 +02:00
|
|
|
$_error = __( 'Unknown Error or misconfigured Error-Message', 'activitypub' );
|
2023-05-02 14:39:25 +02:00
|
|
|
}
|
|
|
|
|
2023-06-23 14:54:29 +02:00
|
|
|
$meta_input['_errors'] = $_error;
|
2023-05-02 14:39:25 +02:00
|
|
|
}
|
2023-06-12 18:38:15 +02:00
|
|
|
|
|
|
|
return $meta_input;
|
2023-04-24 20:46:51 +02:00
|
|
|
}
|
2023-06-23 14:54:29 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts an ActivityPub Array to an Follower Object.
|
|
|
|
*
|
|
|
|
* @param array $array The ActivityPub Array.
|
|
|
|
*
|
|
|
|
* @return Activitypub\Model\Follower The Follower Object.
|
|
|
|
*/
|
|
|
|
public static function from_array( $array ) {
|
|
|
|
$object = parent::from_array( $array );
|
|
|
|
$object->set__actor( $array );
|
|
|
|
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$post_id = $wpdb->get_var(
|
|
|
|
$wpdb->prepare(
|
|
|
|
"SELECT ID FROM $wpdb->posts WHERE guid=%s",
|
|
|
|
esc_sql( $object->get_id() )
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( $post_id ) {
|
|
|
|
$post = get_post( $post_id );
|
|
|
|
$object->set__id( $post->ID );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty( $object->get_endpoints()['sharedInbox'] ) ) {
|
|
|
|
$object->_shared_inbox = $object->get_endpoints()['sharedInbox'];
|
|
|
|
} elseif ( ! empty( $object->get_inbox() ) ) {
|
|
|
|
$object->_shared_inbox = $object->get_inbox();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $object;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a Custom-Post-Type input to an Activitypub\Model\Follower.
|
|
|
|
*
|
|
|
|
* @return string The JSON string.
|
|
|
|
*
|
|
|
|
* @return array Activitypub\Model\Follower
|
|
|
|
*/
|
|
|
|
public static function from_custom_post_type( $post ) {
|
|
|
|
$object = new static();
|
|
|
|
|
|
|
|
$object->set__id( $post->ID );
|
|
|
|
$object->set_id( $post->guid );
|
|
|
|
$object->set_name( $post->post_title );
|
|
|
|
$object->set_summary( $post->post_content );
|
|
|
|
$object->set_url( get_post_meta( $post->ID, 'url', true ) );
|
|
|
|
$object->set_icon( get_post_meta( $post->ID, 'icon', true ) );
|
|
|
|
$object->set_preferred_username( get_post_meta( $post->ID, 'preferred_username', true ) );
|
|
|
|
$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;
|
|
|
|
}
|
2023-04-24 20:46:51 +02:00
|
|
|
}
|