194 lines
4.2 KiB
PHP
194 lines
4.2 KiB
PHP
<?php
|
|
namespace Activitypub\Model;
|
|
|
|
use Activitypub\Transformer\Post as Post_Transformer;
|
|
|
|
/**
|
|
* ActivityPub Post Class
|
|
*
|
|
* @author Matthias Pfefferle
|
|
*/
|
|
class Post {
|
|
/**
|
|
* The \Activitypub\Activity\Base_Object object.
|
|
*
|
|
* @var \Activitypub\Activity\Base_Object
|
|
*/
|
|
protected $object;
|
|
|
|
/**
|
|
* The WordPress Post Object.
|
|
*
|
|
* @var WP_Post
|
|
*/
|
|
private $post;
|
|
|
|
/**
|
|
* Constructor
|
|
*
|
|
* @param WP_Post $post
|
|
* @param int $post_author
|
|
*/
|
|
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed, VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
|
|
public function __construct( $post, $post_author = null ) {
|
|
_deprecated_function( __CLASS__, '1.0.0', '\Activitypub\Transformer\Post' );
|
|
|
|
$this->post = $post;
|
|
$this->object = Post_Transformer::transform( $post )->to_object();
|
|
}
|
|
|
|
/**
|
|
* Returns the User ID.
|
|
*
|
|
* @return int the User ID.
|
|
*/
|
|
public function get_user_id() {
|
|
return apply_filters( 'activitypub_post_user_id', $this->post->post_author, $this->post );
|
|
}
|
|
|
|
/**
|
|
* Converts this Object into an Array.
|
|
*
|
|
* @return array the array representation of a Post.
|
|
*/
|
|
public function to_array() {
|
|
return \apply_filters( 'activitypub_post', $this->object->to_array(), $this->post );
|
|
}
|
|
|
|
/**
|
|
* Returns the Actor of this Object.
|
|
*
|
|
* @return string The URL of the Actor.
|
|
*/
|
|
public function get_actor() {
|
|
$user = User_Factory::get_by_id( $this->get_user_id() );
|
|
|
|
return $user->get_url();
|
|
}
|
|
|
|
/**
|
|
* Converts this Object into a JSON String
|
|
*
|
|
* @return string
|
|
*/
|
|
public function to_json() {
|
|
return \wp_json_encode( $this->to_array(), \JSON_HEX_TAG | \JSON_HEX_AMP | \JSON_HEX_QUOT );
|
|
}
|
|
|
|
/**
|
|
* Returns the URL of an Activity Object
|
|
*
|
|
* @return string
|
|
*/
|
|
public function get_url() {
|
|
return $this->object->get_url();
|
|
}
|
|
|
|
/**
|
|
* Returns the ID of an Activity Object
|
|
*
|
|
* @return string
|
|
*/
|
|
public function get_id() {
|
|
return $this->object->get_id();
|
|
}
|
|
|
|
/**
|
|
* Returns a list of Image Attachments
|
|
*
|
|
* @return array
|
|
*/
|
|
public function get_attachments() {
|
|
return $this->object->get_attachment();
|
|
}
|
|
|
|
/**
|
|
* Returns a list of Tags, used in the Post
|
|
*
|
|
* @return array
|
|
*/
|
|
public function get_tags() {
|
|
return $this->object->get_tag();
|
|
}
|
|
|
|
public function get_replies() {
|
|
if ( $this->replies ) {
|
|
return $this->replies;
|
|
}
|
|
|
|
$replies = null;
|
|
if ( $this->post->comment_count > 0 ) {
|
|
$args = array(
|
|
'post_id' => $this->post->ID,
|
|
'hierarchical' => false,
|
|
'status' => 'approve',
|
|
);
|
|
$comments = \get_comments( $args );
|
|
$items = array();
|
|
|
|
foreach ( $comments as $comment ) {
|
|
// include self replies
|
|
if ( $this->post->post_author === $comment->user_id ) {
|
|
$comment_url = \add_query_arg( //
|
|
array(
|
|
'p' => $this->post->ID,
|
|
'replytocom' => $comment->comment_ID,
|
|
),
|
|
trailingslashit( site_url() )
|
|
);
|
|
$items[] = $comment_url;
|
|
} else {
|
|
$ap_object = \unserialize( \get_comment_meta( $comment->comment_ID, 'ap_object', true ) );
|
|
$comment_url = \get_comment_meta( $comment->comment_ID, 'source_url', true );
|
|
if ( ! empty( $comment_url ) ) {
|
|
$items[] = \get_comment_meta( $comment->comment_ID, 'source_url', true );
|
|
}
|
|
}
|
|
}
|
|
|
|
$replies = (object) array(
|
|
'type' => 'Collection',
|
|
'id' => \add_query_arg( array( 'replies' => '' ), $this->id ),
|
|
'first' => (object) array(
|
|
'type' => 'CollectionPage',
|
|
'partOf' => \add_query_arg( array( 'replies' => '' ), $this->id ),
|
|
'items' => $items,
|
|
),
|
|
);
|
|
}
|
|
|
|
$this->replies = $replies;
|
|
|
|
return $replies;
|
|
}
|
|
|
|
/**
|
|
* Returns the as2 object-type for a given post
|
|
*
|
|
* @return string the object-type
|
|
*/
|
|
public function get_object_type() {
|
|
return $this->object->get_type();
|
|
}
|
|
|
|
/**
|
|
* Returns the content for the ActivityPub Item.
|
|
*
|
|
* @return string the content
|
|
*/
|
|
public function get_content() {
|
|
return $this->object->get_content();
|
|
}
|
|
|
|
/**
|
|
* Get deleted datetime
|
|
*/
|
|
public function get_deleted() {
|
|
$post = $this->post;
|
|
$deleted = null;
|
|
if ( 'trash' === $post->post_status ) {
|
|
$this->deleted = \gmdate( 'Y-m-d\TH:i:s\Z', \strtotime( $post->post_modified_gmt ) );
|
|
}
|
|
return $deleted;
|
|
}
|
|
}
|