updated plugin ActivityPub version 3.3.3

This commit is contained in:
2024-10-09 12:44:17 +00:00
committed by Gitium
parent fb4b27bbc6
commit c54fa007bd
106 changed files with 7070 additions and 2918 deletions

View File

@ -1,10 +1,14 @@
<?php
/**
* Attachment Transformer Class file.
*
* @package Activitypub
*/
namespace Activitypub\Transformer;
use Activitypub\Transformer\Post;
/**
* WordPress Attachment Transformer
* WordPress Attachment Transformer.
*
* The Attachment Transformer is responsible for transforming a WP_Post object into different other
* Object-Types.
@ -22,6 +26,7 @@ class Attachment extends Post {
protected function get_attachment() {
$mime_type = get_post_mime_type( $this->wp_object->ID );
$media_type = preg_replace( '/(\/[a-zA-Z]+)/i', '', $mime_type );
$type = '';
switch ( $media_type ) {
case 'audio':

View File

@ -1,15 +1,21 @@
<?php
/**
* Base Transformer Class file.
*
* @package Activitypub
*/
namespace Activitypub\Transformer;
use WP_Error;
use WP_Post;
use WP_Comment;
use Activitypub\Activity\Activity;
use Activitypub\Activity\Base_Object;
use Activitypub\Collection\Replies;
/**
* WordPress Base Transformer
* WordPress Base Transformer.
*
* Transformers are responsible for transforming a WordPress objects into different ActivityPub
* Object-Types or Activities.
@ -29,18 +35,18 @@ abstract class Base {
*
* This helps to chain the output of the Transformer.
*
* @param WP_Post|WP_Comment $wp_object The WordPress object
* @param WP_Post|WP_Comment $wp_object The WordPress object.
*
* @return Base
*/
public static function transform( $object ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.objectFound
return new static( $object );
public static function transform( $wp_object ) {
return new static( $wp_object );
}
/**
* Base constructor.
*
* @param WP_Post|WP_Comment $wp_object The WordPress object
* @param WP_Post|WP_Comment $wp_object The WordPress object.
*/
public function __construct( $wp_object ) {
$this->wp_object = $wp_object;
@ -49,9 +55,9 @@ abstract class Base {
/**
* Transform all properties with available get(ter) functions.
*
* @param Base_Object|object $object
* @param Base_Object|object $activitypub_object The ActivityPub Object.
*
* @return Base_Object|object $object
* @return Base_Object|object
*/
protected function transform_object_properties( $activitypub_object ) {
$vars = $activitypub_object->get_object_var_keys();
@ -75,13 +81,12 @@ abstract class Base {
/**
* Transform the WordPress Object into an ActivityPub Object.
*
* @return Activitypub\Activity\Base_Object
* @return Base_Object|object The ActivityPub Object.
*/
public function to_object() {
$activitypub_object = new Base_Object();
$activitypub_object = $this->transform_object_properties( $activitypub_object );
return $activitypub_object;
return $this->transform_object_properties( $activitypub_object );
}
/**
@ -89,7 +94,7 @@ abstract class Base {
*
* @param string $type The Activity-Type.
*
* @return \Activitypub\Activity\Activity The Activity.
* @return Activity The Activity.
*/
public function to_activity( $type ) {
$object = $this->to_object();
@ -100,7 +105,7 @@ abstract class Base {
// Pre-fill the Activity with data (for example cc and to).
$activity->set_object( $object );
// Use simple Object (only ID-URI) for Like and Announce
// Use simple Object (only ID-URI) for Like and Announce.
if ( in_array( $type, array( 'Like', 'Announce' ), true ) ) {
$activity->set_object( $object->get_id() );
}
@ -108,17 +113,27 @@ abstract class Base {
return $activity;
}
/**
* Get the ID of the WordPress Object.
*/
abstract protected function get_id();
/**
* Get the replies Collection.
*/
public function get_replies() {
return Replies::get_collection( $this->wp_object );
}
/**
* Returns the ID of the WordPress Object.
*
* @return int The ID of the WordPress Object
*/
abstract public function get_wp_user_id();
/**
* Change the User-ID of the WordPress Post.
*
* @return int The User-ID of the WordPress Post
* @param int $user_id The new user ID.
*/
abstract public function change_wp_user_id( $user_id );
}

View File

@ -1,21 +1,23 @@
<?php
namespace Activitypub\Transformer;
/**
* WordPress Comment Transformer file.
*
* @package Activitypub
*/
use WP_Comment;
use WP_Comment_Query;
namespace Activitypub\Transformer;
use Activitypub\Webfinger;
use Activitypub\Comment as Comment_Utils;
use Activitypub\Model\Blog;
use Activitypub\Collection\Users;
use Activitypub\Transformer\Base;
use function Activitypub\is_single_user;
use function Activitypub\get_rest_url_by_path;
use function Activitypub\get_comment_ancestors;
/**
* WordPress Comment Transformer
* WordPress Comment Transformer.
*
* The Comment Transformer is responsible for transforming a WP_Comment object into different
* Object-Types.
@ -37,18 +39,18 @@ class Comment extends Base {
/**
* Change the User-ID of the WordPress Comment.
*
* @return int The User-ID of the WordPress Comment
* @param int $user_id The new user ID.
*/
public function change_wp_user_id( $user_id ) {
$this->wp_object->user_id = $user_id;
}
/**
* Transforms the WP_Comment object to an ActivityPub Object
* Transforms the WP_Comment object to an ActivityPub Object.
*
* @see \Activitypub\Activity\Base_Object
*
* @return \Activitypub\Activity\Base_Object The ActivityPub Object
* @return \Activitypub\Activity\Base_Object The ActivityPub Object.
*/
public function to_object() {
$comment = $this->wp_object;
@ -106,41 +108,49 @@ class Comment extends Base {
* @return string The content.
*/
protected function get_content() {
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$comment = $this->wp_object;
$content = $comment->comment_content;
/**
* Filter the content of the comment.
*
* @param string $content The content of the comment.
* @param \WP_Comment $comment The comment object.
* @param array $args The arguments.
*
* @return string The filtered content of the comment.
*/
$content = \apply_filters( 'comment_text', $content, $comment, array() );
$content = \preg_replace( '/[\n\r\t]/', '', $content );
$content = \trim( $content );
$content = \apply_filters( 'activitypub_the_content', $content, $comment );
return $content;
/**
* Filter the content of the comment.
*
* @param string $content The content of the comment.
* @param \WP_Comment $comment The comment object.
*
* @return string The filtered content of the comment.
*/
return \apply_filters( 'activitypub_the_content', $content, $comment );
}
/**
* Returns the in-reply-to for the ActivityPub Item.
*
* @return int The URL of the in-reply-to.
* @return false|string|null The URL of the in-reply-to.
*/
protected function get_in_reply_to() {
$comment = $this->wp_object;
$comment = $this->wp_object;
$parent_comment = null;
$in_reply_to = null;
if ( $comment->comment_parent ) {
$parent_comment = \get_comment( $comment->comment_parent );
}
if ( $parent_comment ) {
$comment_meta = \get_comment_meta( $parent_comment->comment_ID );
if ( ! empty( $comment_meta['source_id'][0] ) ) {
$in_reply_to = $comment_meta['source_id'][0];
} elseif ( ! empty( $comment_meta['source_url'][0] ) ) {
$in_reply_to = $comment_meta['source_url'][0];
} elseif ( ! empty( $parent_comment->user_id ) ) {
$in_reply_to = Comment_Utils::get_source_id( $parent_comment->comment_ID );
if ( ! $in_reply_to && ! empty( $parent_comment->user_id ) ) {
$in_reply_to = Comment_Utils::generate_id( $parent_comment );
}
} else {
@ -196,7 +206,7 @@ class Comment extends Base {
$mentions = $this->get_mentions();
if ( $mentions ) {
foreach ( $mentions as $mention => $url ) {
$tag = array(
$tag = array(
'type' => 'Mention',
'href' => \esc_url( $url ),
'name' => \esc_html( $mention ),
@ -216,6 +226,15 @@ class Comment extends Base {
protected function get_mentions() {
\add_filter( 'activitypub_extract_mentions', array( $this, 'extract_reply_context' ) );
/**
* Filter the mentions in the comment.
*
* @param array $mentions The list of mentions.
* @param string $content The content of the comment.
* @param \WP_Comment $comment The comment object.
*
* @return array The filtered list of mentions.
*/
return apply_filters( 'activitypub_extract_mentions', array(), $this->wp_object->comment_content, $this->wp_object );
}
@ -227,7 +246,7 @@ class Comment extends Base {
protected function get_comment_ancestors() {
$ancestors = get_comment_ancestors( $this->wp_object );
// Now that we have the full tree of ancestors, only return the ones received from the fediverse
// Now that we have the full tree of ancestors, only return the ones received from the fediverse.
return array_filter(
$ancestors,
function ( $comment_id ) {
@ -240,12 +259,12 @@ class Comment extends Base {
* Collect all other Users that participated in this comment-thread
* to send them a notification about the new reply.
*
* @param array $mentions The already mentioned ActivityPub users
* @param array $mentions The already mentioned ActivityPub users.
*
* @return array The list of all Repliers.
*/
public function extract_reply_context( $mentions ) {
// Check if `$this->wp_object` is a WP_Comment
// Check if `$this->wp_object` is a WP_Comment.
if ( 'WP_Comment' !== get_class( $this->wp_object ) ) {
return $mentions;
}
@ -260,7 +279,7 @@ class Comment extends Base {
if ( $comment && ! empty( $comment->comment_author_url ) ) {
$acct = Webfinger::uri_to_acct( $comment->comment_author_url );
if ( $acct && ! is_wp_error( $acct ) ) {
$acct = str_replace( 'acct:', '@', $acct );
$acct = str_replace( 'acct:', '@', $acct );
$mentions[ $acct ] = $comment->comment_author_url;
}
}
@ -281,9 +300,9 @@ class Comment extends Base {
/**
* Filter the locale of the comment.
*
* @param string $lang The locale of the comment.
* @param int $comment_id The comment ID.
* @param WP_Post $post The comment object.
* @param string $lang The locale of the comment.
* @param int $comment_id The comment ID.
* @param \WP_Post $post The comment object.
*
* @return string The filtered locale of the comment.
*/

View File

@ -1,26 +1,31 @@
<?php
/**
* Transformer Factory Class file.
*
* @package Activitypub
*/
namespace Activitypub\Transformer;
use WP_Error;
use Activitypub\Transformer\Base;
use Activitypub\Transformer\Post;
use Activitypub\Transformer\Comment;
use Activitypub\Transformer\Attachment;
/**
* Transformer Factory
* Transformer Factory.
*/
class Factory {
/**
* @param mixed $object The object to transform
* @return \Activitypub\Transformer|\WP_Error The transformer to use, or an error.
* Get the transformer for a given object.
*
* @param mixed $data The object to transform.
*
* @return Base|WP_Error The transformer to use, or an error.
*/
public static function get_transformer( $object ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.objectFound
if ( ! \is_object( $object ) ) {
public static function get_transformer( $data ) {
if ( ! \is_object( $data ) ) {
return new WP_Error( 'invalid_object', __( 'Invalid object', 'activitypub' ) );
}
$class = \get_class( $object );
$class = \get_class( $data );
/**
* Filter the transformer for a given object.
@ -46,12 +51,12 @@ class Factory {
* }, 10, 3 );
*
* @param Base $transformer The transformer to use.
* @param mixed $object The object to transform.
* @param mixed $data The object to transform.
* @param string $object_class The class of the object to transform.
*
* @return mixed The transformer to use.
*/
$transformer = \apply_filters( 'activitypub_transformer', null, $object, $class );
$transformer = \apply_filters( 'activitypub_transformer', null, $data, $class );
if ( $transformer ) {
if (
@ -64,15 +69,15 @@ class Factory {
return $transformer;
}
// use default transformer
// Use default transformer.
switch ( $class ) {
case 'WP_Post':
if ( 'attachment' === $object->post_type ) {
return new Attachment( $object );
if ( 'attachment' === $data->post_type ) {
return new Attachment( $data );
}
return new Post( $object );
return new Post( $data );
case 'WP_Comment':
return new Comment( $object );
return new Comment( $data );
default:
return null;
}

View File

@ -1,20 +1,26 @@
<?php
/**
* WordPress Post Transformer Class file.
*
* @package Activitypub
*/
namespace Activitypub\Transformer;
use WP_Post;
use Activitypub\Shortcodes;
use Activitypub\Model\Blog;
use Activitypub\Transformer\Base;
use Activitypub\Collection\Users;
use function Activitypub\esc_hashtag;
use function Activitypub\is_single_user;
use function Activitypub\get_enclosures;
use function Activitypub\get_rest_url_by_path;
use function Activitypub\site_supports_blocks;
use function Activitypub\generate_post_summary;
use function Activitypub\get_content_warning;
/**
* WordPress Post Transformer
* WordPress Post Transformer.
*
* The Post Transformer is responsible for transforming a WP_Post object into different other
* Object-Types.
@ -24,6 +30,13 @@ use function Activitypub\site_supports_blocks;
* - Activitypub\Activity\Base_Object
*/
class Post extends Base {
/**
* The User as Actor Object.
*
* @var \Activitypub\Activity\Actor
*/
private $actor_object = null;
/**
* Returns the ID of the WordPress Post.
*
@ -36,7 +49,9 @@ class Post extends Base {
/**
* Change the User-ID of the WordPress Post.
*
* @return int The User-ID of the WordPress Post
* @param int $user_id The new user ID.
*
* @return Post The Post Object.
*/
public function change_wp_user_id( $user_id ) {
$this->wp_object->post_author = $user_id;
@ -52,42 +67,54 @@ class Post extends Base {
* @return \Activitypub\Activity\Base_Object The ActivityPub Object
*/
public function to_object() {
$post = $this->wp_object;
$post = $this->wp_object;
$object = parent::to_object();
$published = \strtotime( $post->post_date_gmt );
$object->set_published( \gmdate( 'Y-m-d\TH:i:s\Z', $published ) );
$updated = \strtotime( $post->post_modified_gmt );
if ( $updated > $published ) {
$object->set_updated( \gmdate( 'Y-m-d\TH:i:s\Z', $updated ) );
$content_warning = get_content_warning( $post );
if ( ! empty( $content_warning ) ) {
$object->set_sensitive( true );
$object->set_summary( $content_warning );
$object->set_summary_map( null );
}
$object->set_content_map(
array(
$this->get_locale() => $this->get_content(),
)
);
$path = sprintf( 'actors/%d/followers', intval( $post->post_author ) );
$object->set_to(
array(
'https://www.w3.org/ns/activitystreams#Public',
get_rest_url_by_path( $path ),
)
);
return $object;
}
/**
* Returns the User-Object of the Author of the Post.
*
* If `single_user` mode is enabled, the Blog-User is returned.
*
* @return \Activitypub\Activity\Actor The User-Object.
*/
protected function get_actor_object() {
if ( $this->actor_object ) {
return $this->actor_object;
}
$blog_user = new Blog();
$this->actor_object = $blog_user;
if ( is_single_user() ) {
return $blog_user;
}
$user = Users::get_by_id( $this->wp_object->post_author );
if ( $user && ! is_wp_error( $user ) ) {
$this->actor_object = $user;
return $user;
}
return $blog_user;
}
/**
* Returns the ID of the Post.
*
* @return string The Posts ID.
*/
public function get_id() {
protected function get_id() {
return $this->get_url();
}
@ -99,13 +126,21 @@ class Post extends Base {
public function get_url() {
$post = $this->wp_object;
if ( 'trash' === get_post_status( $post ) ) {
$permalink = \get_post_meta( $post->ID, 'activitypub_canonical_url', true );
} elseif ( 'draft' === get_post_status( $post ) && get_sample_permalink( $post->ID ) ) {
$sample = get_sample_permalink( $post->ID );
$permalink = str_replace( array( '%pagename%', '%postname%' ), $sample[1], $sample[0] );
} else {
$permalink = \get_permalink( $post );
switch ( \get_post_status( $post ) ) {
case 'trash':
$permalink = \get_post_meta( $post->ID, 'activitypub_canonical_url', true );
break;
case 'draft':
// Get_sample_permalink is in wp-admin, not always loaded.
if ( ! \function_exists( '\get_sample_permalink' ) ) {
require_once ABSPATH . 'wp-admin/includes/post.php';
}
$sample = \get_sample_permalink( $post->ID );
$permalink = \str_replace( array( '%pagename%', '%postname%' ), $sample[1], $sample[0] );
break;
default:
$permalink = \get_permalink( $post );
break;
}
return \esc_url( $permalink );
@ -119,19 +154,7 @@ class Post extends Base {
* @return string The User-URL.
*/
protected function get_attributed_to() {
$blog_user = new Blog();
if ( is_single_user() ) {
return $blog_user->get_url();
}
$user = Users::get_by_id( $this->wp_object->post_author );
if ( $user && ! is_wp_error( $user ) ) {
return $user->get_url();
}
return $blog_user->get_url();
return $this->get_actor_object()->get_url();
}
/**
@ -161,7 +184,7 @@ class Post extends Base {
);
$id = $this->wp_object->ID;
// list post thumbnail first if this post has one
// List post thumbnail first if this post has one.
if ( \function_exists( 'has_post_thumbnail' ) && \has_post_thumbnail( $id ) ) {
$media['image'][] = array( 'id' => \get_post_thumbnail_id( $id ) );
}
@ -179,155 +202,27 @@ class Post extends Base {
$media = \array_intersect_key( $media, $unique_ids );
$media = \array_slice( $media, 0, $max_media );
return \array_filter( \array_map( array( self::class, 'wp_attachment_to_activity_attachment' ), $media ) );
}
/**
* Filter the attachment IDs for a post.
*
* @param array $media The media array grouped by type.
* @param WP_Post $this->wp_object The post object.
*
* @return array The filtered attachment IDs.
*/
$media = \apply_filters( 'activitypub_attachment_ids', $media, $this->wp_object );
/**
* Get media attachments from blocks. They will be formatted as ActivityPub attachments, not as WP attachments.
*
* @param array $media The media array grouped by type.
* @param int $max_media The maximum number of attachments to return.
*
* @return array The attachments.
*/
protected function get_block_attachments( $media, $max_media ) {
// max media can't be negative or zero
if ( $max_media <= 0 ) {
return array();
}
$attachments = \array_filter( \array_map( array( self::class, 'wp_attachment_to_activity_attachment' ), $media ) );
$blocks = \parse_blocks( $this->wp_object->post_content );
$media = self::get_media_from_blocks( $blocks, $media );
return $media;
}
/**
* Get image attachments from the classic editor.
* This is imperfect as the contained images aren't necessarily the
* same as the attachments.
*
* @param int $max_images The maximum number of images to return.
*
* @return array The attachment IDs.
*/
protected function get_classic_editor_image_attachments( $max_images ) {
// max images can't be negative or zero
if ( $max_images <= 0 ) {
return array();
}
$images = array();
$query = new \WP_Query(
array(
'post_parent' => $this->wp_object->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, $images, true ) ) {
$images[] = array( 'id' => $attachment->ID );
}
}
return $images;
}
/**
* Get image embeds from the classic editor by parsing HTML.
*
* @param int $max_images The maximum number of images to return.
*
* @return array The attachments.
*/
protected function get_classic_editor_image_embeds( $max_images ) {
// if someone calls that function directly, bail
if ( ! \class_exists( '\WP_HTML_Tag_Processor' ) ) {
return array();
}
// max images can't be negative or zero
if ( $max_images <= 0 ) {
return array();
}
$images = array();
$base = \wp_get_upload_dir()['baseurl'];
$content = \get_post_field( 'post_content', $this->wp_object );
$tags = new \WP_HTML_Tag_Processor( $content );
// This linter warning is a false positive - we have to
// re-count each time here as we modify $images.
// phpcs:ignore Squiz.PHP.DisallowSizeFunctionsInLoops.Found
while ( $tags->next_tag( 'img' ) && ( \count( $images ) <= $max_images ) ) {
$src = $tags->get_attribute( 'src' );
// If the img source is in our uploads dir, get the
// associated ID. Note: if there's a -500x500
// type suffix, we remove it, but we try the original
// first in case the original image is actually called
// that. Likewise, we try adding the -scaled suffix for
// the case that this is a small version of an image
// that was big enough to get scaled down on upload:
// https://make.wordpress.org/core/2019/10/09/introducing-handling-of-big-images-in-wordpress-5-3/
if ( null !== $src && \str_starts_with( $src, $base ) ) {
$img_id = \attachment_url_to_postid( $src );
if ( 0 === $img_id ) {
$count = 0;
$src = preg_replace( '/-(?:\d+x\d+)(\.[a-zA-Z]+)$/', '$1', $src, 1, $count );
if ( $count > 0 ) {
$img_id = \attachment_url_to_postid( $src );
}
}
if ( 0 === $img_id ) {
$src = preg_replace( '/(\.[a-zA-Z]+)$/', '-scaled$1', $src );
$img_id = \attachment_url_to_postid( $src );
}
if ( 0 !== $img_id ) {
$images[] = array(
'id' => $img_id,
'alt' => $tags->get_attribute( 'alt' ),
);
}
}
}
return $images;
}
/**
* Get post images from the classic editor.
* Note that audio/video attachments are only supported in the block editor.
*
* @param array $media The media array grouped by type.
* @param int $max_images The maximum number of images to return.
*
* @return array The attachments.
*/
protected function get_classic_editor_images( $media, $max_images ) {
// max images can't be negative or zero
if ( $max_images <= 0 ) {
return array();
}
if ( \count( $media['image'] ) <= $max_images ) {
if ( \class_exists( '\WP_HTML_Tag_Processor' ) ) {
$media['image'] = \array_merge( $media['image'], $this->get_classic_editor_image_embeds( $max_images ) );
} else {
$media['image'] = \array_merge( $media['image'], $this->get_classic_editor_image_attachments( $max_images ) );
}
}
return $media;
/**
* Filter the attachments for a post.
*
* @param array $attachments The attachments.
* @param WP_Post $this->wp_object The post object.
*
* @return array The filtered attachments.
*/
return \apply_filters( 'activitypub_attachments', $attachments, $this->wp_object );
}
/**
@ -345,11 +240,11 @@ class Post extends Base {
}
foreach ( $enclosures as $enclosure ) {
// check if URL is an attachment
// Check if URL is an attachment.
$attachment_id = \attachment_url_to_postid( $enclosure['url'] );
if ( $attachment_id ) {
$enclosure['id'] = $attachment_id;
$enclosure['url'] = \wp_get_attachment_url( $attachment_id );
$enclosure['id'] = $attachment_id;
$enclosure['url'] = \wp_get_attachment_url( $attachment_id );
$enclosure['mediaType'] = \get_post_mime_type( $attachment_id );
}
@ -372,17 +267,36 @@ class Post extends Base {
return $media;
}
/**
* Get media attachments from blocks. They will be formatted as ActivityPub attachments, not as WP attachments.
*
* @param array $media The media array grouped by type.
* @param int $max_media The maximum number of attachments to return.
*
* @return array The attachments.
*/
protected function get_block_attachments( $media, $max_media ) {
// Max media can't be negative or zero.
if ( $max_media <= 0 ) {
return array();
}
$blocks = \parse_blocks( $this->wp_object->post_content );
return self::get_media_from_blocks( $blocks, $media );
}
/**
* Recursively get media IDs from blocks.
* @param array $blocks The blocks to search for media IDs
* @param array $media The media IDs to append new IDs to
* @param int $max_media The maximum number of media to return.
*
* @param array $blocks The blocks to search for media IDs.
* @param array $media The media IDs to append new IDs to.
*
* @return array The image IDs.
*/
protected static function get_media_from_blocks( $blocks, $media ) {
foreach ( $blocks as $block ) {
// recurse into inner blocks
// Recurse into inner blocks.
if ( ! empty( $block['innerBlocks'] ) ) {
$media = self::get_media_from_blocks( $block['innerBlocks'], $media );
}
@ -443,22 +357,160 @@ class Post extends Base {
return $media;
}
/**
* Get post images from the classic editor.
* Note that audio/video attachments are only supported in the block editor.
*
* @param array $media The media array grouped by type.
* @param int $max_images The maximum number of images to return.
*
* @return array The attachments.
*/
protected function get_classic_editor_images( $media, $max_images ) {
// Max images can't be negative or zero.
if ( $max_images <= 0 ) {
return array();
}
if ( \count( $media['image'] ) <= $max_images ) {
if ( \class_exists( '\WP_HTML_Tag_Processor' ) ) {
$media['image'] = \array_merge( $media['image'], $this->get_classic_editor_image_embeds( $max_images ) );
} else {
$media['image'] = \array_merge( $media['image'], $this->get_classic_editor_image_attachments( $max_images ) );
}
}
return $media;
}
/**
* Get image embeds from the classic editor by parsing HTML.
*
* @param int $max_images The maximum number of images to return.
*
* @return array The attachments.
*/
protected function get_classic_editor_image_embeds( $max_images ) {
// If someone calls that function directly, bail.
if ( ! \class_exists( '\WP_HTML_Tag_Processor' ) ) {
return array();
}
// Max images can't be negative or zero.
if ( $max_images <= 0 ) {
return array();
}
$images = array();
$base = \wp_get_upload_dir()['baseurl'];
$content = \get_post_field( 'post_content', $this->wp_object );
$tags = new \WP_HTML_Tag_Processor( $content );
// This linter warning is a false positive - we have to re-count each time here as we modify $images.
// phpcs:ignore Squiz.PHP.DisallowSizeFunctionsInLoops.Found
while ( $tags->next_tag( 'img' ) && ( \count( $images ) <= $max_images ) ) {
$src = $tags->get_attribute( 'src' );
/*
* If the img source is in our uploads dir, get the
* associated ID. Note: if there's a -500x500
* type suffix, we remove it, but we try the original
* first in case the original image is actually called
* that. Likewise, we try adding the -scaled suffix for
* the case that this is a small version of an image
* that was big enough to get scaled down on upload:
* https://make.wordpress.org/core/2019/10/09/introducing-handling-of-big-images-in-wordpress-5-3/
*/
if ( null !== $src && \str_starts_with( $src, $base ) ) {
$img_id = \attachment_url_to_postid( $src );
if ( 0 === $img_id ) {
$count = 0;
$src = preg_replace( '/-(?:\d+x\d+)(\.[a-zA-Z]+)$/', '$1', $src, 1, $count );
if ( $count > 0 ) {
$img_id = \attachment_url_to_postid( $src );
}
}
if ( 0 === $img_id ) {
$src = preg_replace( '/(\.[a-zA-Z]+)$/', '-scaled$1', $src );
$img_id = \attachment_url_to_postid( $src );
}
if ( 0 !== $img_id ) {
$images[] = array(
'id' => $img_id,
'alt' => $tags->get_attribute( 'alt' ),
);
}
}
}
return $images;
}
/**
* Get image attachments from the classic editor.
* This is imperfect as the contained images aren't necessarily the
* same as the attachments.
*
* @param int $max_images The maximum number of images to return.
*
* @return array The attachment IDs.
*/
protected function get_classic_editor_image_attachments( $max_images ) {
// Max images can't be negative or zero.
if ( $max_images <= 0 ) {
return array();
}
$images = array();
$query = new \WP_Query(
array(
'post_parent' => $this->wp_object->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, $images, true ) ) {
$images[] = array( 'id' => $attachment->ID );
}
}
return $images;
}
/**
* Filter media IDs by object type.
*
* @param array $media The media array grouped by type.
* @param string $type The object type.
* @param array $media The media array grouped by type.
* @param string $type The object type.
* @param WP_Post $wp_object The post object.
*
* @return array The filtered media IDs.
*/
protected static function filter_media_by_object_type( $media, $type, $wp_object ) {
/**
* Filter the object type for media attachments.
*
* @param string $type The object type.
* @param WP_Post $wp_object The post object.
*
* @return string The filtered object type.
*/
$type = \apply_filters( 'filter_media_by_object_type', \strtolower( $type ), $wp_object );
if ( ! empty( $media[ $type ] ) ) {
return $media[ $type ];
}
return array_filter( array_merge( array(), ...array_values( $media ) ) );
return array_filter( array_merge( ...array_values( $media ) ) );
}
/**
@ -477,7 +529,7 @@ class Post extends Base {
$attachment = array();
$mime_type = \get_post_mime_type( $id );
$mime_type_parts = \explode( '/', $mime_type );
// switching on image/audio/video
// Switching on image/audio/video.
switch ( $mime_type_parts[0] ) {
case 'image':
$image_size = 'large';
@ -524,16 +576,24 @@ class Post extends Base {
'url' => \esc_url( \wp_get_attachment_url( $id ) ),
'name' => \esc_attr( \get_the_title( $id ) ),
);
$meta = wp_get_attachment_metadata( $id );
// height and width for videos
$meta = wp_get_attachment_metadata( $id );
// Height and width for videos.
if ( isset( $meta['width'] ) && isset( $meta['height'] ) ) {
$attachment['width'] = \esc_attr( $meta['width'] );
$attachment['width'] = \esc_attr( $meta['width'] );
$attachment['height'] = \esc_attr( $meta['height'] );
}
// @todo: add `icon` support for audio/video attachments. Maybe use post thumbnail?
break;
}
/**
* Filter the attachment for a post.
*
* @param array $attachment The attachment.
* @param int $id The attachment ID.
*
* @return array The filtered attachment.
*/
return \apply_filters( 'activitypub_attachment', $attachment, $id );
}
@ -589,7 +649,7 @@ class Post extends Base {
}
// Default to Article.
$object_type = 'Note';
$object_type = 'Article';
$post_format = 'standard';
if ( \get_theme_support( 'post-formats' ) ) {
@ -613,7 +673,7 @@ class Post extends Base {
$object_type = 'Page';
break;
default:
$object_type = 'Note';
$object_type = 'Article';
break;
}
@ -640,7 +700,11 @@ class Post extends Base {
return $cc;
}
/**
* Returns the Audience for the Post.
*
* @return string|null The audience.
*/
public function get_audience() {
if ( is_single_user() ) {
return null;
@ -663,7 +727,7 @@ class Post extends Base {
$post_tags = \get_the_tags( $this->wp_object->ID );
if ( $post_tags ) {
foreach ( $post_tags as $post_tag ) {
$tag = array(
$tag = array(
'type' => 'Hashtag',
'href' => \esc_url( \get_tag_link( $post_tag->term_id ) ),
'name' => esc_hashtag( $post_tag->name ),
@ -675,7 +739,7 @@ class Post extends Base {
$mentions = $this->get_mentions();
if ( $mentions ) {
foreach ( $mentions as $mention => $url ) {
$tag = array(
$tag = array(
'type' => 'Mention',
'href' => \esc_url( $url ),
'name' => \esc_html( $mention ),
@ -705,24 +769,7 @@ class Post extends Base {
return \__( '(This post is being modified)', 'activitypub' );
}
$content = \get_post_field( 'post_content', $this->wp_object->ID );
$content = \html_entity_decode( $content );
$content = \wp_strip_all_tags( $content );
$content = \trim( $content );
$content = \preg_replace( '/\R+/m', "\n\n", $content );
$content = \preg_replace( '/[\r\t]/', '', $content );
$excerpt_more = \apply_filters( 'activitypub_excerpt_more', '[...]' );
$length = 500;
$length = $length - strlen( $excerpt_more );
if ( \strlen( $content ) > $length ) {
$content = \wordwrap( $content, $length, '</activitypub-summary>' );
$content = \explode( '</activitypub-summary>', $content, 2 );
$content = $content[0];
}
return $content . ' ' . $excerpt_more;
return generate_post_summary( $this->wp_object );
}
/**
@ -759,6 +806,8 @@ class Post extends Base {
* @return string The content.
*/
protected function get_content() {
add_filter( 'activitypub_reply_block', '__return_empty_string' );
// Remove Content from drafts.
if ( 'draft' === \get_post_status( $this->wp_object ) ) {
return \__( '(This post is being modified)', 'activitypub' );
@ -794,7 +843,7 @@ class Post extends Base {
$content = \apply_filters( 'activitypub_the_content', $content, $post );
// Don't need these any more, should never appear in a post.
// Don't need these anymore, should never appear in a post.
Shortcodes::unregister();
return $content;
@ -819,7 +868,8 @@ class Post extends Base {
$template = "[ap_content]\n\n[ap_permalink type=\"html\"]\n\n[ap_hashtags]";
break;
default:
$template = \get_option( 'activitypub_custom_post_content', ACTIVITYPUB_CUSTOM_POST_CONTENT );
$content = \get_option( 'activitypub_custom_post_content', ACTIVITYPUB_CUSTOM_POST_CONTENT );
$template = empty( $content ) ? ACTIVITYPUB_CUSTOM_POST_CONTENT : $content;
break;
}
@ -838,7 +888,21 @@ class Post extends Base {
* @return array The list of @-Mentions.
*/
protected function get_mentions() {
return apply_filters( 'activitypub_extract_mentions', array(), $this->wp_object->post_content, $this->wp_object );
/**
* Filter the mentions in the post content.
*
* @param array $mentions The mentions.
* @param string $content The post content.
* @param WP_Post $post The post object.
*
* @return array The filtered mentions.
*/
return apply_filters(
'activitypub_extract_mentions',
array(),
$this->wp_object->post_content . ' ' . $this->wp_object->post_excerpt,
$this->wp_object
);
}
/**
@ -862,6 +926,108 @@ class Post extends Base {
return apply_filters( 'activitypub_post_locale', $lang, $post_id, $this->wp_object );
}
/**
* Returns the in-reply-to URL of the post.
*
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-inreplyto
*
* @return string|null The in-reply-to URL of the post.
*/
public function get_in_reply_to() {
$blocks = \parse_blocks( $this->wp_object->post_content );
foreach ( $blocks as $block ) {
if ( 'activitypub/reply' === $block['blockName'] ) {
// We only support one reply block per post for now.
return $block['attrs']['url'];
}
}
return null;
}
/**
* Returns the recipient of the post.
*
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-to
*
* @return array The recipient URLs of the post.
*/
public function get_to() {
return array(
'https://www.w3.org/ns/activitystreams#Public',
$this->get_actor_object()->get_followers(),
);
}
/**
* Returns the published date of the post.
*
* @return string The published date of the post.
*/
public function get_published() {
$published = \strtotime( $this->wp_object->post_date_gmt );
return \gmdate( 'Y-m-d\TH:i:s\Z', $published );
}
/**
* Returns the updated date of the post.
*
* @return string|null The updated date of the post.
*/
public function get_updated() {
$published = \strtotime( $this->wp_object->post_date_gmt );
$updated = \strtotime( $this->wp_object->post_modified_gmt );
if ( $updated > $published ) {
return \gmdate( 'Y-m-d\TH:i:s\Z', $updated );
}
return null;
}
/**
* Returns the content map for the post.
*
* @return array The content map for the post.
*/
public function get_content_map() {
return array(
$this->get_locale() => $this->get_content(),
);
}
/**
* Returns the name map for the post.
*
* @return array The name map for the post.
*/
public function get_name_map() {
if ( ! $this->get_name() ) {
return null;
}
return array(
$this->get_locale() => $this->get_name(),
);
}
/**
* Returns the summary map for the post.
*
* @return array The summary map for the post.
*/
public function get_summary_map() {
if ( ! $this->get_summary() ) {
return null;
}
return array(
$this->get_locale() => $this->get_summary(),
);
}
/**
* Transform Embed blocks to block level link.
*
@ -870,8 +1036,8 @@ class Post extends Base {
* @see https://www.w3.org/TR/activitypub/#security-sanitizing-content
* @see https://www.w3.org/wiki/ActivityPub/Primer/HTML
*
* @param string $block_content The block content (html)
* @param object $block The block object
* @param string $block_content The block content (html).
* @param object $block The block object.
*
* @return string A block level link
*/