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

209 lines
4.2 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-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() ),
'post_title' => esc_html( $this->get_name() ),
'post_author' => 0,
'post_type' => Followers::POST_TYPE,
'post_name' => esc_url_raw( $this->get_id() ),
'post_excerpt' => esc_html( $this->get_summary() ) ? $this->get_summary() : '',
'post_content' => esc_sql( $this->to_json() ),
'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 14:42:18 +02:00
$meta_input['activitypub_inbox'] = esc_url_raw( $this->get_shared_inbox() );
return $meta_input;
2023-04-24 20:46:51 +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;
}
/**
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 ) {
$object = self::init_from_json( $post->post_content );
$object->set__id( $post->ID );
$object->set_id( $post->guid );
$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
}