wip refactoring the transformers
Some checks failed
PHP_CodeSniffer / phpcs (push) Failing after 2s
Unit Testing / phpunit (5.6, 6.2) (push) Failing after 2s
Unit Testing / phpunit (7.0) (push) Failing after 2s
Unit Testing / phpunit (7.2) (push) Failing after 2s
Unit Testing / phpunit (7.3) (push) Failing after 3s
Unit Testing / phpunit (7.4) (push) Failing after 3s
Unit Testing / phpunit (8.0) (push) Failing after 3s
Unit Testing / phpunit (8.1) (push) Failing after 3s
Unit Testing / phpunit (8.2) (push) Failing after 3s
Unit Testing / phpunit (latest) (push) Failing after 3s

This commit is contained in:
André Menrath 2023-11-29 23:46:13 +01:00
parent c7ff99e77f
commit ebb747368d
14 changed files with 71 additions and 112 deletions

View file

@ -457,11 +457,11 @@ class Base_Object {
}
if ( \strncasecmp( $method, 'set', 3 ) === 0 ) {
$this->set( $var, $params[0] );
return $this->set( $var, $params[0] );
}
if ( \strncasecmp( $method, 'add', 3 ) === 0 ) {
$this->add( $var, $params[0] );
return $this->add( $var, $params[0] );
}
}
@ -524,7 +524,7 @@ class Base_Object {
$this->$key = $value;
return $this->$key;
return $this;
}
/**
@ -549,7 +549,7 @@ class Base_Object {
$this->$key = $attributes;
return $this->$key;
return $this;
}
/**
@ -639,7 +639,7 @@ class Base_Object {
$value = call_user_func( array( $this, 'get_' . $key ) );
}
if ( is_object( $value ) ) {
if ( is_object( $value ) && is_subclass_of( $value, self::class ) ) {
$value = $value->to_array();
}

View file

@ -1,23 +0,0 @@
<?php
/**
* Inspired by the PHP ActivityPub Library by @Landrok
*
* @link https://github.com/landrok/activitypub
*/
namespace Activitypub\Activity;
use Activitypub\Activity\Base_Object;
/**
* Event is an implementation of one of the
* Activity Streams Event object type
*
* The Object is the primary base type for the Activity Streams
* vocabulary.
*
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-event
*/
class Note extends Base_Object {
protected $type = 'Event';
}

View file

@ -1,23 +0,0 @@
<?php
/**
* Inspired by the PHP ActivityPub Library by @Landrok
*
* @link https://github.com/landrok/activitypub
*/
namespace Activitypub\Activity;
use Activitypub\Activity\Base_Object;
/**
* Note is an implementation of one of the
* Activity Streams Note object type
*
* The Object is the primary base type for the Activity Streams
* vocabulary.
*
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-note
*/
class Note extends Base_Object {
protected $type = 'Note';
}

View file

@ -65,8 +65,7 @@ class Activity_Dispatcher {
return;
}
$transformer = Transformers_Manager::instance()->get_transformer( $wp_post );
$object = $transformer->to_object();
$object = Transformers_Manager::instance()->transform( $wp_post );
$activity = new Activity();
$activity->set_type( $type );
@ -102,8 +101,7 @@ class Activity_Dispatcher {
return;
}
$transformer = Transformers_Manager::instance()->get_transformer( $wp_post );
$object = $transformer->to_object();
$object = Transformers_Manager::instance()->transform( $wp_post );
$activity = new Activity();
$activity->set_type( 'Announce' );

View file

@ -35,7 +35,7 @@ class Post {
$this->post = $post;
$transformer = new Transformer_Post();
$this->object = $transformer->set_wp_post( $post )->to_object();
$this->object = $transformer->transform( $post );
}
/**

View file

@ -168,7 +168,7 @@ class Collection {
);
foreach ( $posts as $post ) {
$response['orderedItems'][] = Transformers_Manager::instance()->get_transformer( $post )->to_object()->to_array();
$response['orderedItems'][] = $object = Transformers_Manager::instance()->transform( $post );
}
$rest_response = new WP_REST_Response( $response, 200 );

View file

@ -105,12 +105,11 @@ class Outbox {
);
foreach ( $posts as $post ) {
$transformer = \Activitypub\Transformer\Transformers_Manager::instance()->get_transformer( $post );
$post = $transformer->to_object();
$object = \Activitypub\Transformer\Transformers_Manager::instance()->transform( $post );
$activity = new Activity();
$activity->set_type( 'Create' );
$activity->set_context( null );
$activity->set_object( $post );
$activity->set_object( $object );
$json->orderedItems[] = $activity->to_array(); // phpcs:ignore
}

View file

@ -578,42 +578,9 @@ abstract class Base {
* Transforms the WP_Post object to an ActivityPub Object
*
* @see \Activitypub\Activity\Base_Object
* @param WP_Post $wp_post The WordPress Post
*
* @return \Activitypub\Activity\Base_Object The ActivityPub Object
*/
public function to_object() {
$wp_post = $this->wp_post;
$object = new Base_Object();
$object->set_id( $this->get_id() );
$object->set_url( $this->get_url() );
$object->set_type( $this->get_object_type() );
$published = \strtotime( $wp_post->post_date_gmt );
$object->set_published( \gmdate( 'Y-m-d\TH:i:s\Z', $published ) );
$updated = \strtotime( $wp_post->post_modified_gmt );
if ( $updated > $published ) {
$object->set_updated( \gmdate( 'Y-m-d\TH:i:s\Z', $updated ) );
}
$object->set_attributed_to( $this->get_attributed_to() );
$object->set_content( $this->get_content() );
$object->set_content_map( $this->get_content_map );
$path = sprintf( 'users/%d/followers', intval( $wp_post->post_author ) );
$object->set_to(
array(
'https://www.w3.org/ns/activitystreams#Public',
get_rest_url_by_path( $path ),
)
);
$object->set_cc( $this->get_cc() );
$object->set_attachment( $this->get_attachments() );
$object->set_tag( $this->get_tags() );
return $object;
}
abstract public function transform();
}

View file

@ -106,4 +106,47 @@ class Post extends Base {
return $object_type;
}
/**
* Transforms the WP_Post object to an ActivityPub Object
*
* @see \Activitypub\Activity\Base_Object
* @param WP_Post $wp_post The WordPress Post
*
* @return \Activitypub\Activity\Base_Object The ActivityPub Object
*/
public function transform() {
$object = new Base_Object();
$object->set_id( $this->get_id() );
$object->set_url( $this->get_url() );
$object->set_type( $this->get_object_type() );
$published = \strtotime( $wp_post->post_date_gmt );
$object->set_published( \gmdate( 'Y-m-d\TH:i:s\Z', $published ) );
$updated = \strtotime( $wp_post->post_modified_gmt );
if ( $updated > $published ) {
$object->set_updated( \gmdate( 'Y-m-d\TH:i:s\Z', $updated ) );
}
$object->set_attributed_to( $this->get_attributed_to() );
$object->set_content( $this->get_content() );
$object->set_content_map( $this->get_content_map );
$path = sprintf( 'users/%d/followers', intval( $wp_post->post_author ) );
$object->set_to(
array(
'https://www.w3.org/ns/activitystreams#Public',
get_rest_url_by_path( $path ),
)
);
$object->set_cc( $this->get_cc() );
$object->set_attachment( $this->get_attachments() );
$object->set_tag( $this->get_tags() );
return $object;
}
}

View file

@ -215,8 +215,6 @@ class Transformers_Manager {
$this->transformers = [];
foreach ( $builtin_transformers as $transformer_name ) {
include ACTIVITYPUB_PLUGIN_DIR . 'includes/transformer/class-' . $transformer_name . '.php';
$class_name = ucfirst( $transformer_name );
$class_name = '\Activitypub\Transformer\\' . $class_name;
@ -269,24 +267,24 @@ class Transformers_Manager {
* @since version_number_transformer_management_placeholder
* @access public
*
* @param WP_Post|WP_Comment $object The WordPress Post/Comment.
* @param WP_Post|WP_Comment $wp_object The WordPress Post/Comment.
*
* @return \ActivityPub\Transformer\Base|null Registered transformers.
*/
public function get_transformer( $object ) {
switch ( get_class( $object ) ) {
public function transform( $wp_object ) {
switch ( get_class( $wp_object ) ) {
case 'WP_Post':
$post_type = get_post_type( $object );
$post_type = get_post_type( $wp_object );
$transformer_mapping = \get_option( 'activitypub_transformer_mapping', self::DEFAULT_TRANSFORMER_MAPPING );
$transformer_name = $transformer_mapping[ $post_type ];
$transformer_class = $this->get_transformers( $transformer_name );
$transformer_instance = new $transformer_class();
$transformer_instance->set_wp_post( $object );
return $transformer_instance;
$transformer_instance->set_wp_post( $wp_object );
return $transformer_instance->transform();
case 'WP_Comment':
return new Comment( $object );
return new Comment( $wp_object );
default:
return apply_filters( 'activitypub_transformer', null, $object, get_class( $object ) );
return apply_filters( 'activitypub_transformer', null, $wp_object, get_class( $wp_object ) );
}
}
}

View file

@ -2,9 +2,9 @@
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$post = \get_post();
$transformer = \Activitypub\Transformer\Transformers_Manager::instance()->get_transformer( $post );
$object = \Activitypub\Transformer\Transformers_Manager::instance()->transform( $post );
$json = \array_merge( array( '@context' => \Activitypub\get_context() ), $transformer->to_object()->to_array() );
$json = \array_merge( array( '@context' => \Activitypub\get_context() ), $object->to_array() );
// filter output
$json = \apply_filters( 'activitypub_json_post_array', $json );

View file

@ -18,7 +18,7 @@ class Test_Activitypub_Activity extends WP_UnitTestCase {
);
$wp_post = get_post( $post );
$activitypub_post = \Activitypub\Transformer\Transformers_Manager::instance()->get_transformer( $post )->to_object();
$activitypub_post = \Activitypub\Transformer\Transformers_Manager::instance()->transform();
$activitypub_activity = new \Activitypub\Activity\Activity();
$activitypub_activity->set_type( 'Create' );

View file

@ -10,13 +10,13 @@ class Test_Activitypub_Post extends WP_UnitTestCase {
$permalink = \get_permalink( $post );
$activitypub_post = \Activitypub\Transformer\Transformers_Manager::instance()->get_transformer( get_post( $post ) )->to_object();
$activitypub_post = \Activitypub\Transformer\Transformers_Manager::instance()->transform( get_post( $post ) );
$this->assertEquals( $permalink, $activitypub_post->get_id() );
\wp_trash_post( $post );
$activitypub_post = \Activitypub\Transformer\Transformers_Manager::instance()->get_transformer( get_post( $post ) )->to_object();
$activitypub_post = \Activitypub\Transformer\Transformers_Manager::instance()->transform( get_post( $post ) );
$this->assertEquals( $permalink, $activitypub_post->get_id() );

View file

@ -10,7 +10,7 @@ class Test_Activitypub_Signature_Verification extends WP_UnitTestCase {
)
);
$remote_actor = \get_author_posts_url( 2 );
$activitypub_post = \Activitypub\Transformer\Transformers_Manager::instance()->get_transformer( get_post( $post ) )->to_object();
$activitypub_post = \Activitypub\Transformer\Transformers_Manager::instance()->transform( get_post( $post ) );
$activitypub_activity = new Activitypub\Activity\Activity( 'Create' );
$activitypub_activity->set_type( 'Create' );
$activitypub_activity->set_object( $activitypub_post );
@ -82,7 +82,7 @@ class Test_Activitypub_Signature_Verification extends WP_UnitTestCase {
);
$remote_actor = \get_author_posts_url( 2 );
$remote_actor_inbox = Activitypub\get_rest_url_by_path( '/inbox' );
$activitypub_post = \Activitypub\Transformer\Transformers_Manager::instance()->get_transformer( get_post( $post ) )->to_object();
$activitypub_post = \Activitypub\Transformer\Transformers_Manager::instance()->transform( get_post( $post ) );
$activitypub_activity = new Activitypub\Activity\Activity();
$activitypub_activity->set_type( 'Create' );
$activitypub_activity->set_object( $activitypub_post );