diff --git a/includes/transformer/class-attachment.php b/includes/transformer/class-attachment.php index 98aaf8bf4..ef3e1d1fd 100644 --- a/includes/transformer/class-attachment.php +++ b/includes/transformer/class-attachment.php @@ -24,7 +24,7 @@ class Attachment extends Post { * @return array The Attachments. */ protected function get_attachment() { - $mime_type = get_post_mime_type( $this->wp_object->ID ); + $mime_type = get_post_mime_type( $this->item->ID ); $media_type = preg_replace( '/(\/[a-zA-Z]+)/i', '', $mime_type ); $type = ''; @@ -40,11 +40,11 @@ protected function get_attachment() { $attachment = array( 'type' => $type, - 'url' => wp_get_attachment_url( $this->wp_object->ID ), + 'url' => wp_get_attachment_url( $this->item->ID ), 'mediaType' => $mime_type, ); - $alt = \get_post_meta( $this->wp_object->ID, '_wp_attachment_image_alt', true ); + $alt = \get_post_meta( $this->item->ID, '_wp_attachment_image_alt', true ); if ( $alt ) { $attachment['name'] = $alt; } diff --git a/includes/transformer/class-base.php b/includes/transformer/class-base.php index fd275fd3b..488ddc64b 100644 --- a/includes/transformer/class-base.php +++ b/includes/transformer/class-base.php @@ -28,6 +28,15 @@ abstract class Base { * * @var WP_Post|WP_Comment */ + protected $item; + + /** + * The WP_Post or WP_Comment object. + * + * @deprecated 4.8.0 Use $item instead. + * + * @var WP_Post|WP_Comment + */ protected $wp_object; /** @@ -35,21 +44,22 @@ 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 $item The WordPress object. * * @return Base */ - public static function transform( $wp_object ) { - return new static( $wp_object ); + public static function transform( $item ) { + return new static( $item ); } /** * Base constructor. * - * @param WP_Post|WP_Comment $wp_object The WordPress object. + * @param WP_Post|WP_Comment $item The WordPress object. */ - public function __construct( $wp_object ) { - $this->wp_object = $wp_object; + public function __construct( $item ) { + $this->item = $item; + $this->wp_object = $item; } /** @@ -124,7 +134,7 @@ abstract protected function get_id(); * @return array The replies collection. */ public function get_replies() { - return Replies::get_collection( $this->wp_object ); + return Replies::get_collection( $this->item ); } /** diff --git a/includes/transformer/class-comment.php b/includes/transformer/class-comment.php index 9b1ec64ff..a529d8ded 100644 --- a/includes/transformer/class-comment.php +++ b/includes/transformer/class-comment.php @@ -33,7 +33,7 @@ class Comment extends Base { * @return int The User-ID of the WordPress Comment */ public function get_wp_user_id() { - return $this->wp_object->user_id; + return $this->item->user_id; } /** @@ -42,7 +42,7 @@ public function get_wp_user_id() { * @param int $user_id The new user ID. */ public function change_wp_user_id( $user_id ) { - $this->wp_object->user_id = $user_id; + $this->item->user_id = $user_id; } /** @@ -58,7 +58,7 @@ protected function get_attributed_to() { return $user->get_id(); } - return Actors::get_by_id( $this->wp_object->user_id )->get_id(); + return Actors::get_by_id( $this->item->user_id )->get_id(); } /** @@ -69,7 +69,7 @@ protected function get_attributed_to() { * @return string The content. */ protected function get_content() { - $comment = $this->wp_object; + $comment = $this->item; $content = $comment->comment_content; $mentions = ''; @@ -112,7 +112,7 @@ protected function get_content() { * @return false|string|null The URL of the in-reply-to. */ protected function get_in_reply_to() { - $comment = $this->wp_object; + $comment = $this->item; $parent_comment = null; if ( $comment->comment_parent ) { @@ -140,8 +140,7 @@ protected function get_in_reply_to() { * @return string ActivityPub URI for comment */ protected function get_id() { - $comment = $this->wp_object; - return Comment_Utils::generate_id( $comment ); + return Comment_Utils::generate_id( $this->item ); } /** @@ -206,7 +205,7 @@ protected function get_mentions() { * * @return array The filtered list of mentions. */ - return apply_filters( 'activitypub_extract_mentions', array(), $this->wp_object->comment_content, $this->wp_object ); + return apply_filters( 'activitypub_extract_mentions', array(), $this->item->comment_content, $this->item ); } /** @@ -215,7 +214,7 @@ protected function get_mentions() { * @return array The list of ancestors. */ protected function get_comment_ancestors() { - $ancestors = get_comment_ancestors( $this->wp_object ); + $ancestors = get_comment_ancestors( $this->item ); // Now that we have the full tree of ancestors, only return the ones received from the fediverse. return array_filter( @@ -235,8 +234,8 @@ function ( $comment_id ) { * @return array The list of all Repliers. */ public function extract_reply_context( $mentions = array() ) { - // Check if `$this->wp_object` is a WP_Comment. - if ( 'WP_Comment' !== get_class( $this->wp_object ) ) { + // Check if `$this->item` is a WP_Comment. + if ( 'WP_Comment' !== get_class( $this->item ) ) { return $mentions; } @@ -265,7 +264,7 @@ public function extract_reply_context( $mentions = array() ) { * @return string The locale of the post. */ public function get_locale() { - $comment_id = $this->wp_object->ID; + $comment_id = $this->item->ID; $lang = \strtolower( \strtok( \get_locale(), '_-' ) ); /** @@ -277,7 +276,7 @@ public function get_locale() { * * @return string The filtered locale of the comment. */ - return apply_filters( 'activitypub_comment_locale', $lang, $comment_id, $this->wp_object ); + return apply_filters( 'activitypub_comment_locale', $lang, $comment_id, $this->item ); } /** @@ -286,8 +285,8 @@ public function get_locale() { * @return string|null The updated date of the comment. */ public function get_updated() { - $updated = \get_comment_meta( $this->wp_object->comment_ID, 'activitypub_comment_modified', true ); - $published = \get_comment_meta( $this->wp_object->comment_ID, 'activitypub_comment_published', true ); + $updated = \get_comment_meta( $this->item->comment_ID, 'activitypub_comment_modified', true ); + $published = \get_comment_meta( $this->item->comment_ID, 'activitypub_comment_published', true ); if ( $updated > $published ) { return \gmdate( 'Y-m-d\TH:i:s\Z', $updated ); @@ -302,7 +301,7 @@ public function get_updated() { * @return string The published date of the comment. */ public function get_published() { - return \gmdate( 'Y-m-d\TH:i:s\Z', \strtotime( $this->wp_object->comment_date_gmt ) ); + return \gmdate( 'Y-m-d\TH:i:s\Z', \strtotime( $this->item->comment_date_gmt ) ); } /** @@ -329,7 +328,7 @@ public function get_type() { * @return array The to of the comment. */ public function get_to() { - $path = sprintf( 'actors/%d/followers', intval( $this->wp_object->comment_author ) ); + $path = sprintf( 'actors/%d/followers', intval( $this->item->comment_author ) ); return array( 'https://www.w3.org/ns/activitystreams#Public', diff --git a/includes/transformer/class-post.php b/includes/transformer/class-post.php index f3cbcf960..d1440ebf1 100644 --- a/includes/transformer/class-post.php +++ b/includes/transformer/class-post.php @@ -46,7 +46,7 @@ class Post extends Base { * @return int The ID of the WordPress Post */ public function get_wp_user_id() { - return $this->wp_object->post_author; + return $this->item->post_author; } /** @@ -57,7 +57,7 @@ public function get_wp_user_id() { * @return Post The Post Object. */ public function change_wp_user_id( $user_id ) { - $this->wp_object->post_author = $user_id; + $this->item->post_author = $user_id; return $this; } @@ -70,7 +70,7 @@ public function change_wp_user_id( $user_id ) { * @return \Activitypub\Activity\Base_Object The ActivityPub Object */ public function to_object() { - $post = $this->wp_object; + $post = $this->item; $object = parent::to_object(); $content_warning = get_content_warning( $post ); @@ -115,7 +115,7 @@ public function get_actor_object() { return $blog_user; } - $user = Actors::get_by_id( $this->wp_object->post_author ); + $user = Actors::get_by_id( $this->item->post_author ); if ( $user && ! is_wp_error( $user ) ) { $this->actor_object = $user; @@ -132,7 +132,7 @@ public function get_actor_object() { */ public function get_id() { $last_legacy_id = (int) \get_option( 'activitypub_last_post_with_permalink_as_id', 0 ); - $post_id = (int) $this->wp_object->ID; + $post_id = (int) $this->item->ID; if ( $post_id > $last_legacy_id ) { // Generate URI based on post ID. @@ -148,7 +148,7 @@ public function get_id() { * @return string The Posts URL. */ public function get_url() { - $post = $this->wp_object; + $post = $this->item; switch ( \get_post_status( $post ) ) { case 'trash': @@ -187,7 +187,7 @@ protected function get_attributed_to() { * @return array|null The Image or null if no image is available. */ protected function get_image() { - $post_id = $this->wp_object->ID; + $post_id = $this->item->ID; // List post thumbnail first if this post has one. if ( @@ -240,7 +240,7 @@ protected function get_image() { * @return array|null The Icon or null if no icon is available. */ protected function get_icon() { - $post_id = $this->wp_object->ID; + $post_id = $this->item->ID; // List post thumbnail first if this post has one. if ( \has_post_thumbnail( $post_id ) ) { @@ -297,7 +297,7 @@ protected function get_icon() { */ protected function get_attachment() { // Remove attachments from drafts. - if ( 'draft' === \get_post_status( $this->wp_object ) ) { + if ( 'draft' === \get_post_status( $this->item ) ) { return array(); } @@ -322,7 +322,7 @@ protected function get_attachment() { 'audio' => array(), 'video' => array(), ); - $id = $this->wp_object->ID; + $id = $this->item->ID; // List post thumbnail first if this post has one. if ( \function_exists( 'has_post_thumbnail' ) && \has_post_thumbnail( $id ) ) { @@ -331,13 +331,13 @@ protected function get_attachment() { $media = $this->get_enclosures( $media ); - if ( site_supports_blocks() && \has_blocks( $this->wp_object->post_content ) ) { + if ( site_supports_blocks() && \has_blocks( $this->item->post_content ) ) { $media = $this->get_block_attachments( $media, $max_media ); } else { $media = $this->get_classic_editor_images( $media, $max_media ); } - $media = $this->filter_media_by_object_type( $media, \get_post_format( $this->wp_object ), $this->wp_object ); + $media = $this->filter_media_by_object_type( $media, \get_post_format( $this->item ), $this->item ); $unique_ids = \array_unique( \array_column( $media, 'id' ) ); $media = \array_intersect_key( $media, $unique_ids ); $media = \array_slice( $media, 0, $max_media ); @@ -345,24 +345,24 @@ protected function get_attachment() { /** * 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. + * @param array $media The media array grouped by type. + * @param WP_Post $item The post object. * * @return array The filtered attachment IDs. */ - $media = \apply_filters( 'activitypub_attachment_ids', $media, $this->wp_object ); + $media = \apply_filters( 'activitypub_attachment_ids', $media, $this->item ); $attachments = \array_filter( \array_map( array( $this, 'wp_attachment_to_activity_attachment' ), $media ) ); /** * Filter the attachments for a post. * - * @param array $attachments The attachments. - * @param WP_Post $this->wp_object The post object. + * @param array $attachments The attachments. + * @param WP_Post $item The post object. * * @return array The filtered attachments. */ - return \apply_filters( 'activitypub_attachments', $attachments, $this->wp_object ); + return \apply_filters( 'activitypub_attachments', $attachments, $this->item ); } /** @@ -373,7 +373,7 @@ protected function get_attachment() { * @return array The media array extended with enclosures. */ public function get_enclosures( $media ) { - $enclosures = get_enclosures( $this->wp_object->ID ); + $enclosures = get_enclosures( $this->item->ID ); if ( ! $enclosures ) { return $media; @@ -423,7 +423,7 @@ protected function get_block_attachments( $media, $max_media ) { return array(); } - $blocks = \parse_blocks( $this->wp_object->post_content ); + $blocks = \parse_blocks( $this->item->post_content ); return $this->get_media_from_blocks( $blocks, $media ); } @@ -556,7 +556,7 @@ protected function get_classic_editor_image_embeds( $max_images ) { $images = array(); $base = get_upload_baseurl(); - $content = \get_post_field( 'post_content', $this->wp_object ); + $content = \get_post_field( 'post_content', $this->item ); $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. @@ -634,7 +634,7 @@ protected function get_classic_editor_image_attachments( $max_images ) { $images = array(); $query = new \WP_Query( array( - 'post_parent' => $this->wp_object->ID, + 'post_parent' => $this->item->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', @@ -656,22 +656,22 @@ protected function get_classic_editor_image_attachments( $max_images ) { /** * Filter media IDs by 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. + * @param array $media The media array grouped by type. + * @param string $type The object type. + * @param WP_Post $item The post object. * * @return array The filtered media IDs. */ - protected function filter_media_by_object_type( $media, $type, $wp_object ) { + protected function filter_media_by_object_type( $media, $type, $item ) { /** * Filter the object type for media attachments. * - * @param string $type The object type. - * @param WP_Post $wp_object The post object. + * @param string $type The object type. + * @param WP_Post $item The post object. * * @return string The filtered object type. */ - $type = \apply_filters( 'filter_media_by_object_type', \strtolower( $type ), $wp_object ); + $type = \apply_filters( 'filter_media_by_object_type', \strtolower( $type ), $item ); if ( ! empty( $media[ $type ] ) ) { return $media[ $type ]; @@ -813,13 +813,13 @@ protected function get_type() { return \ucfirst( $post_format_setting ); } - $has_title = \post_type_supports( $this->wp_object->post_type, 'title' ); - $content = \wp_strip_all_tags( $this->wp_object->post_content ); + $has_title = \post_type_supports( $this->item->post_type, 'title' ); + $content = \wp_strip_all_tags( $this->item->post_content ); // Check if the post has a title. if ( ! $has_title || - ! $this->wp_object->post_title || + ! $this->item->post_title || \strlen( $content ) <= ACTIVITYPUB_NOTE_LENGTH ) { return 'Note'; @@ -827,11 +827,11 @@ protected function get_type() { // Default to Note. $object_type = 'Note'; - $post_type = \get_post_type( $this->wp_object ); + $post_type = \get_post_type( $this->item ); if ( 'page' === $post_type ) { $object_type = 'Page'; - } elseif ( ! \get_post_format( $this->wp_object ) ) { + } elseif ( ! \get_post_format( $this->item ) ) { $object_type = 'Article'; } @@ -899,7 +899,7 @@ public function get_audience() { protected function get_tag() { $tags = array(); - $post_tags = \get_the_tags( $this->wp_object->ID ); + $post_tags = \get_the_tags( $this->item->ID ); if ( $post_tags ) { foreach ( $post_tags as $post_tag ) { $tag = array( @@ -940,11 +940,11 @@ protected function get_summary() { } // Remove Teaser from drafts. - if ( ! $this->is_preview() && 'draft' === \get_post_status( $this->wp_object ) ) { + if ( ! $this->is_preview() && 'draft' === \get_post_status( $this->item ) ) { return \__( '(This post is being modified)', 'activitypub' ); } - return generate_post_summary( $this->wp_object ); + return generate_post_summary( $this->item ); } /** @@ -960,7 +960,7 @@ protected function get_name() { return null; } - $title = \get_the_title( $this->wp_object->ID ); + $title = \get_the_title( $this->item->ID ); if ( ! $title ) { return null; @@ -984,7 +984,7 @@ protected function get_content() { add_filter( 'activitypub_reply_block', '__return_empty_string' ); // Remove Content from drafts. - if ( ! $this->is_preview() && 'draft' === \get_post_status( $this->wp_object ) ) { + if ( ! $this->is_preview() && 'draft' === \get_post_status( $this->item ) ) { return \__( '(This post is being modified)', 'activitypub' ); } @@ -1002,7 +1002,7 @@ protected function get_content() { add_filter( 'render_block_core/embed', array( $this, 'revert_embed_links' ), 10, 2 ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited - $post = $this->wp_object; + $post = $this->item; $content = $this->get_post_content_template(); // It seems that shortcodes are only applied to published posts. @@ -1064,10 +1064,10 @@ protected function get_post_content_template() { * shortcodes like [ap_title] and [ap_content] that are processed during content * generation. * - * @param string $template The template string containing shortcodes. - * @param WP_Post $wp_object The WordPress post object being transformed. + * @param string $template The template string containing shortcodes. + * @param WP_Post $item The WordPress post object being transformed. */ - return apply_filters( 'activitypub_object_content_template', $template, $this->wp_object ); + return apply_filters( 'activitypub_object_content_template', $template, $this->item ); } /** @@ -1088,8 +1088,8 @@ protected function get_mentions() { return apply_filters( 'activitypub_extract_mentions', array(), - $this->wp_object->post_content . ' ' . $this->wp_object->post_excerpt, - $this->wp_object + $this->item->post_content . ' ' . $this->item->post_excerpt, + $this->item ); } @@ -1099,7 +1099,7 @@ protected function get_mentions() { * @return string The locale of the post. */ public function get_locale() { - $post_id = $this->wp_object->ID; + $post_id = $this->item->ID; $lang = \strtolower( \strtok( \get_locale(), '_-' ) ); /** @@ -1111,7 +1111,7 @@ public function get_locale() { * * @return string The filtered locale of the post. */ - return apply_filters( 'activitypub_post_locale', $lang, $post_id, $this->wp_object ); + return apply_filters( 'activitypub_post_locale', $lang, $post_id, $this->item ); } /** @@ -1122,7 +1122,7 @@ public function get_locale() { * @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 ); + $blocks = \parse_blocks( $this->item->post_content ); foreach ( $blocks as $block ) { if ( 'activitypub/reply' === $block['blockName'] && isset( $block['attrs']['url'] ) ) { @@ -1140,7 +1140,7 @@ public function get_in_reply_to() { * @return string The published date of the post. */ public function get_published() { - $published = \strtotime( $this->wp_object->post_date_gmt ); + $published = \strtotime( $this->item->post_date_gmt ); return \gmdate( 'Y-m-d\TH:i:s\Z', $published ); } @@ -1151,8 +1151,8 @@ public function get_published() { * @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 ); + $published = \strtotime( $this->item->post_date_gmt ); + $updated = \strtotime( $this->item->post_modified_gmt ); if ( $updated > $published ) { return \gmdate( 'Y-m-d\TH:i:s\Z', $updated ); diff --git a/integration/class-seriously-simple-podcasting.php b/integration/class-seriously-simple-podcasting.php index 167af7e3e..7533db98a 100644 --- a/integration/class-seriously-simple-podcasting.php +++ b/integration/class-seriously-simple-podcasting.php @@ -29,7 +29,7 @@ class Seriously_Simple_Podcasting extends Post { * @return array The attachments array. */ public function get_attachment() { - $post = $this->wp_object; + $post = $this->item; $attachment = array( 'type' => \esc_attr( ucfirst( \get_post_meta( $post->ID, 'episode_type', true ) ?? 'Audio' ) ), 'url' => \esc_url( \get_post_meta( $post->ID, 'audio_file', true ) ), @@ -67,6 +67,6 @@ public function get_type() { * @return string The content. */ public function get_content() { - return generate_post_summary( $this->wp_object ); + return generate_post_summary( $this->item ); } }