Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transformers: Migrate from wp_object to items #1165

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions includes/transformer/class-attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';

Expand All @@ -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;
}
Expand Down
16 changes: 8 additions & 8 deletions includes/transformer/class-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,28 @@ abstract class Base {
*
* @var WP_Post|WP_Comment
*/
protected $wp_object;
protected $item;

/**
* Static function to Transform a WordPress Object.
*
* 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;
obenland marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -124,7 +124,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 );
}

/**
Expand Down
33 changes: 16 additions & 17 deletions includes/transformer/class-comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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();
}

/**
Expand All @@ -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 = '';

Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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 );
}

/**
Expand Down Expand Up @@ -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 );
}

/**
Expand All @@ -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(
Expand All @@ -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;
}

Expand Down Expand Up @@ -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(), '_-' ) );

/**
Expand All @@ -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 );
}

/**
Expand All @@ -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 );
Expand All @@ -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 ) );
}

/**
Expand All @@ -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',
Expand Down
Loading
Loading