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

195 lines
4.2 KiB
PHP
Raw Normal View History

2018-09-30 22:51:22 +02:00
<?php
2019-11-18 20:57:00 +01:00
namespace Activitypub\Model;
2023-08-01 18:37:16 +02:00
use Activitypub\Transformer\Post as Post_Transformer;
2018-09-30 22:51:22 +02:00
/**
* ActivityPub Post Class
*
* @author Matthias Pfefferle
*/
class Post {
/**
2023-08-01 18:37:16 +02:00
* The \Activitypub\Activity\Base_Object object.
*
2023-08-01 18:37:16 +02:00
* @var \Activitypub\Activity\Base_Object
*/
2023-08-01 18:37:16 +02:00
protected $object;
/**
2023-08-01 18:37:16 +02:00
* The WordPress Post Object.
*
2023-08-01 18:37:16 +02:00
* @var WP_Post
*/
2023-08-01 18:37:16 +02:00
private $post;
/**
2023-08-01 18:37:16 +02:00
* Constructor
*
2023-08-01 18:37:16 +02:00
* @param WP_Post $post
* @param int $post_author
*/
2023-08-01 18:37:16 +02:00
// 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' );
2018-09-30 22:51:22 +02:00
2023-08-01 18:37:16 +02:00
$this->post = $post;
$this->object = Post_Transformer::transform( $post )->to_object();
}
/**
2023-08-01 18:37:16 +02:00
* Returns the User ID.
*
2023-08-01 18:37:16 +02:00
* @return int the User ID.
*/
2023-08-01 18:37:16 +02:00
public function get_user_id() {
return apply_filters( 'activitypub_post_user_id', $this->post->post_author, $this->post );
}
2018-09-30 22:51:22 +02:00
/**
2023-08-01 18:37:16 +02:00
* Converts this Object into an Array.
*
2023-08-01 18:37:16 +02:00
* @return array the array representation of a Post.
*/
2023-08-01 18:37:16 +02:00
public function to_array() {
return \apply_filters( 'activitypub_post', $this->object->to_array(), $this->post );
}
2018-09-30 22:51:22 +02:00
/**
2023-08-01 18:37:16 +02:00
* Returns the Actor of this Object.
*
2023-08-01 18:37:16 +02:00
* @return string The URL of the Actor.
*/
2023-08-01 18:37:16 +02:00
public function get_actor() {
$user = User_Factory::get_by_id( $this->get_user_id() );
2020-05-14 21:37:59 +02:00
2023-08-01 18:37:16 +02:00
return $user->get_url();
}
2018-09-30 22:51:22 +02:00
/**
2023-08-01 18:37:16 +02:00
* Converts this Object into a JSON String
*
2023-08-01 18:37:16 +02:00
* @return string
*/
2023-08-01 18:37:16 +02:00
public function to_json() {
return \wp_json_encode( $this->to_array(), \JSON_HEX_TAG | \JSON_HEX_AMP | \JSON_HEX_QUOT );
}
/**
2023-08-01 18:37:16 +02:00
* Returns the URL of an Activity Object
*
* @return string
*/
2023-08-01 18:37:16 +02:00
public function get_url() {
return $this->object->get_url();
}
/**
* Returns the ID of an Activity Object
*
* @return string
*/
public function get_id() {
2023-08-01 18:37:16 +02:00
return $this->object->get_id();
2020-05-14 18:02:49 +02:00
}
/**
* Returns a list of Image Attachments
*
* @return array
*/
public function get_attachments() {
2023-08-01 18:37:16 +02:00
return $this->object->get_attachment();
}
/**
* Returns a list of Tags, used in the Post
*
* @return array
*/
public function get_tags() {
2023-08-01 18:37:16 +02:00
return $this->object->get_tag();
2018-09-30 22:51:22 +02:00
}
2022-04-15 09:17:00 +02:00
2023-02-06 06:05:30 +01:00
public function get_replies() {
if ( $this->replies ) {
return $this->replies;
}
2022-04-15 03:04:43 +02:00
$replies = null;
if ( $this->post->comment_count > 0 ) {
$args = array(
'post_id' => $this->post->ID,
2022-04-15 03:04:43 +02:00
'hierarchical' => false,
2022-04-15 09:17:00 +02:00
'status' => 'approve',
2022-04-15 03:04:43 +02:00
);
$comments = \get_comments( $args );
2022-04-15 09:17:00 +02:00
$items = array();
foreach ( $comments as $comment ) {
2022-04-15 03:04:43 +02:00
// include self replies
if ( $this->post->post_author === $comment->user_id ) {
$comment_url = \add_query_arg( //
2022-04-15 09:17:00 +02:00
array(
2022-04-15 03:04:43 +02:00
'p' => $this->post->ID,
'replytocom' => $comment->comment_ID,
2022-04-15 09:17:00 +02:00
),
2022-04-15 03:04:43 +02:00
trailingslashit( site_url() )
);
$items[] = $comment_url;
2022-09-28 19:18:30 +02:00
} else {
$ap_object = \unserialize( \get_comment_meta( $comment->comment_ID, 'ap_object', true ) );
$comment_url = \get_comment_meta( $comment->comment_ID, 'source_url', true );
2022-09-28 19:18:30 +02:00
if ( ! empty( $comment_url ) ) {
$items[] = \get_comment_meta( $comment->comment_ID, 'source_url', true );
}
2022-04-15 03:04:43 +02:00
}
}
2022-04-15 09:17:00 +02:00
$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,
),
);
2022-04-15 03:04:43 +02:00
}
2023-02-06 06:05:30 +01:00
$this->replies = $replies;
2022-04-15 03:04:43 +02:00
return $replies;
}
2018-09-30 22:51:22 +02:00
/**
* Returns the as2 object-type for a given post
*
* @return string the object-type
*/
public function get_object_type() {
2023-08-01 18:37:16 +02:00
return $this->object->get_type();
2018-09-30 22:51:22 +02:00
}
/**
* Returns the content for the ActivityPub Item.
*
* @return string the content
*/
public function get_content() {
2023-08-01 18:37:16 +02:00
return $this->object->get_content();
2022-10-05 21:58:12 +02:00
}
2022-04-15 03:04:43 +02:00
/**
* Get deleted datetime
*/
public function get_deleted() {
$post = $this->post;
$deleted = null;
2022-09-28 19:18:30 +02:00
if ( 'trash' === $post->post_status ) {
2023-02-06 06:05:30 +01:00
$this->deleted = \gmdate( 'Y-m-d\TH:i:s\Z', \strtotime( $post->post_modified_gmt ) );
2022-04-15 03:04:43 +02:00
}
return $deleted;
}
2018-09-30 22:51:22 +02:00
}