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

546 lines
11 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-05-12 22:31:53 +02:00
use function Activitypub\get_rest_url_by_path;
2018-09-30 22:51:22 +02:00
/**
* ActivityPub Post Class
*
* @author Matthias Pfefferle
*/
class Post {
/**
* The WordPress Post Object.
*
* @var WP_Post
*/
2018-09-30 22:51:22 +02:00
private $post;
/**
* The Post Author.
*
* @var string
*/
2020-05-14 21:37:59 +02:00
private $post_author;
/**
* The Object ID.
*
* @var string
*/
private $id;
2023-04-23 22:56:45 +02:00
/**
* The Object URL.
*
* @var string
*/
private $url;
/**
* The Object Summary.
*
* @var string
*/
2020-05-14 21:37:59 +02:00
private $summary;
/**
* The Object Summary
*
* @var string
*/
2020-05-14 21:37:59 +02:00
private $content;
/**
* The Object Attachments. This is usually a list of Images.
*
* @var array
*/
2020-05-14 21:37:59 +02:00
private $attachments;
/**
* The Object Tags. This is usually the list of used Hashtags.
*
* @var array
*/
private $tags;
2018-09-30 22:51:22 +02:00
/**
* The Onject Type
*
* @var string
*/
2023-03-03 08:55:23 +01:00
private $object_type;
/**
* The Allowed Tags, used in the content.
*
* @var array
*/
private $allowed_tags = array(
'a' => array(
'href' => array(),
'title' => array(),
'class' => array(),
'rel' => array(),
),
'br' => array(),
'p' => array(
'class' => array(),
),
'span' => array(
'class' => array(),
),
'div' => array(
'class' => array(),
),
2023-03-03 08:56:15 +01:00
'ul' => array(),
'ol' => array(),
'li' => array(),
'strong' => array(
'class' => array(),
),
'b' => array(
'class' => array(),
),
'i' => array(
'class' => array(),
),
'em' => array(
'class' => array(),
),
'blockquote' => array(),
'cite' => array(),
2023-05-05 14:40:17 +02:00
'code' => array(
'class' => array(),
),
'pre' => array(
'class' => array(),
),
);
2023-02-02 07:24:27 +01:00
/**
* List of audience
*
* Also used for visibility
*
* @var array
*/
2023-02-02 02:36:29 +01:00
private $to = array( 'https://www.w3.org/ns/activitystreams#Public' );
2023-02-02 07:24:27 +01:00
/**
* List of audience
*
* Also used for visibility
*
* @var array
*/
2023-02-02 02:36:29 +01:00
private $cc = array();
/**
* Constructor
*
* @param WP_Post $post
*/
public function __construct( $post ) {
$this->post = \get_post( $post );
$path = sprintf( 'users/%d/followers', intval( $this->get_post_author() ) );
$this->add_to( get_rest_url_by_path( $path ) );
}
2018-09-30 22:51:22 +02:00
/**
* Magic function to implement getter and setter
*
* @param string $method
* @param string $params
*
* @return void
*/
2020-05-14 21:37:59 +02:00
public function __call( $method, $params ) {
$var = \strtolower( \substr( $method, 4 ) );
if ( \strncasecmp( $method, 'get', 3 ) === 0 ) {
2023-01-27 14:28:56 +01:00
if ( empty( $this->$var ) && ! empty( $this->post->$var ) ) {
return $this->post->$var;
}
2020-05-14 21:37:59 +02:00
return $this->$var;
}
if ( \strncasecmp( $method, 'set', 3 ) === 0 ) {
$this->$var = $params[0];
}
2023-02-02 02:36:29 +01:00
if ( \strncasecmp( $method, 'add', 3 ) === 0 ) {
if ( ! is_array( $this->$var ) ) {
$this->$var = $params[0];
}
if ( is_array( $params[0] ) ) {
$this->$var = array_merge( $this->$var, $params[0] );
} else {
array_push( $this->$var, $params[0] );
}
$this->$var = array_unique( $this->$var );
}
}
2018-09-30 22:51:22 +02:00
/**
* Converts this Object into an Array.
*
* @return array
*/
public function to_array() {
$post = $this->post;
2018-09-30 22:51:22 +02:00
$array = array(
'id' => $this->get_id(),
2023-04-23 22:56:45 +02:00
'url' => $this->get_url(),
'type' => $this->get_object_type(),
2022-01-27 13:09:11 +01:00
'published' => \gmdate( 'Y-m-d\TH:i:s\Z', \strtotime( $post->post_date_gmt ) ),
2019-09-27 10:12:59 +02:00
'attributedTo' => \get_author_posts_url( $post->post_author ),
'summary' => $this->get_summary(),
2018-09-30 22:51:22 +02:00
'inReplyTo' => null,
'content' => $this->get_content(),
2018-09-30 22:51:22 +02:00
'contentMap' => array(
\strstr( \get_locale(), '_', true ) => $this->get_content(),
2018-09-30 22:51:22 +02:00
),
2023-02-02 02:36:29 +01:00
'to' => $this->get_to(),
'cc' => $this->get_cc(),
'attachment' => $this->get_attachments(),
'tag' => $this->get_tags(),
2018-09-30 22:51:22 +02:00
);
2023-02-02 08:50:17 +01:00
return \apply_filters( 'activitypub_post', $array, $this->post );
}
/**
* 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 );
}
/**
2023-04-23 22:56:45 +02:00
* Returns the URL of an Activity Object
*
* @return string
*/
2023-04-23 22:56:45 +02:00
public function get_url() {
if ( $this->url ) {
return $this->url;
}
$post = $this->post;
if ( 'trash' === get_post_status( $post ) ) {
2023-04-24 09:49:06 +02:00
$permalink = \get_post_meta( $post->ID, 'activitypub_canonical_url', true );
} else {
$permalink = \get_permalink( $post );
}
2020-05-14 18:02:49 +02:00
2023-04-23 22:56:45 +02:00
$this->url = $permalink;
return $permalink;
2020-05-14 18:02:49 +02:00
}
2023-04-23 22:56:45 +02:00
/**
* Returns the ID of an Activity Object
*
* @return string
*/
public function get_id() {
if ( $this->id ) {
return $this->id;
}
$this->id = $this->get_url();
return $this->id;
}
/**
* Returns a list of Image Attachments
*
* @return array
*/
public function get_attachments() {
if ( $this->attachments ) {
return $this->attachments;
}
$max_images = intval( \apply_filters( 'activitypub_max_image_attachments', \get_option( 'activitypub_max_image_attachments', ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS ) ) );
$images = array();
// max images can't be negative or zero
if ( $max_images <= 0 ) {
return $images;
}
$id = $this->post->ID;
$image_ids = array();
2023-01-12 22:21:48 +01:00
// list post thumbnail first if this post has one
2019-09-27 10:12:59 +02:00
if ( \function_exists( 'has_post_thumbnail' ) && \has_post_thumbnail( $id ) ) {
$image_ids[] = \get_post_thumbnail_id( $id );
$max_images--;
}
2023-01-12 22:21:48 +01:00
if ( $max_images > 0 ) {
// then list any image attachments
$query = new \WP_Query(
array(
'post_parent' => $id,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'ASC',
'orderby' => 'menu_order ID',
'posts_per_page' => $max_images,
)
);
foreach ( $query->get_posts() as $attachment ) {
if ( ! \in_array( $attachment->ID, $image_ids, true ) ) {
$image_ids[] = $attachment->ID;
}
}
}
2019-09-27 11:37:15 +02:00
$image_ids = \array_unique( $image_ids );
// get URLs for each image
foreach ( $image_ids as $id ) {
$image_size = 'full';
/**
* Filter the image URL returned for each post.
*
* @param array|false $thumbnail The image URL, or false if no image is available.
* @param int $id The attachment ID.
* @param string $image_size The image size to retrieve. Set to 'full' by default.
*/
$thumbnail = apply_filters(
'activitypub_get_image',
$this->get_image( $id, $image_size ),
$id,
$image_size
);
if ( $thumbnail ) {
$mimetype = \get_post_mime_type( $id );
$alt = \get_post_meta( $id, '_wp_attachment_image_alt', true );
$image = array(
'type' => 'Image',
'url' => $thumbnail[0],
2020-05-14 21:37:59 +02:00
'mediaType' => $mimetype,
);
if ( $alt ) {
$image['name'] = $alt;
}
$images[] = $image;
}
}
$this->attachments = $images;
return $images;
}
/**
* Return details about an image attachment.
*
* @param int $id The attachment ID.
* @param string $image_size The image size to retrieve. Set to 'full' by default.
*
* @return array|false Array of image data, or boolean false if no image is available.
*/
public function get_image( $id, $image_size = 'full' ) {
/**
* Hook into the image retrieval process. Before image retrieval.
*
* @param int $id The attachment ID.
* @param string $image_size The image size to retrieve. Set to 'full' by default.
*/
do_action( 'activitypub_get_image_pre', $id, $image_size );
$thumbnail = \wp_get_attachment_image_src( $id, $image_size );
/**
* Hook into the image retrieval process. After image retrieval.
*
* @param int $id The attachment ID.
* @param string $image_size The image size to retrieve. Set to 'full' by default.
*/
do_action( 'activitypub_get_image_pre', $id, $image_size );
return $thumbnail;
}
/**
* Returns a list of Tags, used in the Post
*
* @return array
*/
public function get_tags() {
if ( $this->tags ) {
return $this->tags;
}
$tags = array();
2019-09-27 10:12:59 +02:00
$post_tags = \get_the_tags( $this->post->ID );
if ( $post_tags ) {
2019-03-02 21:10:42 +01:00
foreach ( $post_tags as $post_tag ) {
$tag = array(
'type' => 'Hashtag',
2019-09-27 10:12:59 +02:00
'href' => \get_tag_link( $post_tag->term_id ),
'name' => '#' . $post_tag->slug,
);
$tags[] = $tag;
}
}
$mentions = apply_filters( 'activitypub_extract_mentions', array(), $this->post->post_content, $this );
2022-12-09 11:59:24 +01:00
if ( $mentions ) {
foreach ( $mentions as $mention => $url ) {
$tag = array(
'type' => 'Mention',
'href' => $url,
'name' => $mention,
);
$tags[] = $tag;
}
}
$this->tags = $tags;
return $tags;
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() {
if ( $this->object_type ) {
return $this->object_type;
}
2019-09-27 10:12:59 +02:00
if ( 'wordpress-post-format' !== \get_option( 'activitypub_object_type', 'note' ) ) {
return \ucfirst( \get_option( 'activitypub_object_type', 'note' ) );
2018-12-28 22:40:57 +01:00
}
2019-09-27 10:12:59 +02:00
$post_type = \get_post_type( $this->post );
2018-09-30 22:51:22 +02:00
switch ( $post_type ) {
case 'post':
2019-09-27 10:12:59 +02:00
$post_format = \get_post_format( $this->post );
2018-09-30 22:51:22 +02:00
switch ( $post_format ) {
case 'aside':
case 'status':
case 'quote':
case 'note':
$object_type = 'Note';
break;
case 'gallery':
case 'image':
$object_type = 'Image';
break;
case 'video':
$object_type = 'Video';
break;
case 'audio':
$object_type = 'Audio';
break;
default:
$object_type = 'Article';
break;
}
break;
case 'page':
$object_type = 'Page';
break;
case 'attachment':
2019-09-27 10:12:59 +02:00
$mime_type = \get_post_mime_type();
$media_type = \preg_replace( '/(\/[a-zA-Z]+)/i', '', $mime_type );
2018-09-30 22:51:22 +02:00
switch ( $media_type ) {
case 'audio':
$object_type = 'Audio';
break;
case 'video':
$object_type = 'Video';
break;
case 'image':
$object_type = 'Image';
break;
}
break;
default:
$object_type = 'Article';
break;
}
$this->object_type = $object_type;
2018-09-30 22:51:22 +02:00
return $object_type;
}
/**
* Returns the content for the ActivityPub Item.
*
* @return string the content
*/
public function get_content() {
2023-02-02 08:18:10 +01:00
global $post;
if ( $this->content ) {
return $this->content;
}
2023-02-02 08:18:10 +01:00
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$post = $this->post;
$content = $this->get_post_content_template();
// Fill in the shortcodes.
setup_postdata( $post );
$content = do_shortcode( $content );
wp_reset_postdata();
$content = \wpautop( \wp_kses( $content, $this->allowed_tags ) );
2023-02-20 18:17:02 +01:00
$content = \trim( \preg_replace( '/[\n\r\t]/', '', $content ) );
2023-02-20 18:17:02 +01:00
$content = \apply_filters( 'activitypub_the_content', $content, $post );
$content = \html_entity_decode( $content, \ENT_QUOTES, 'UTF-8' );
$this->content = $content;
return $content;
}
/**
* Gets the template to use to generate the content of the activitypub item.
*
* @return string the template
*/
public function get_post_content_template() {
if ( 'excerpt' === \get_option( 'activitypub_post_content_type', 'content' ) ) {
2023-02-08 10:06:22 +01:00
return "[ap_excerpt]\n\n[ap_permalink type=\"html\"]";
}
if ( 'title' === \get_option( 'activitypub_post_content_type', 'content' ) ) {
2023-02-08 10:06:22 +01:00
return "[ap_title]\n\n[ap_permalink type=\"html\"]";
2019-02-02 23:56:05 +01:00
}
if ( 'content' === \get_option( 'activitypub_post_content_type', 'content' ) ) {
2023-02-08 10:06:22 +01:00
return "[ap_content]\n\n[ap_hashtags]\n\n[ap_permalink type=\"html\"]";
}
return $content;
2019-02-02 23:56:05 +01:00
}
2018-09-30 22:51:22 +02:00
}