wordpress-activitypub/includes/transformer/class-base.php
André Menrath 174262d22c
Add/event objects (#629)
* remove redundant property definitions

* Add redused context for actors.

* Add classes to construct Moblizon compatible events

* Bind the context to the activitypub object
- change the propertyname  which stores the json-ld context from context to _context, because context is already reserved in the ActivityStreams vocabulary.
- cleanup currently unused code

* fix phpcs

* Remove PostalAddress object: it's enough (at least atm) to directly write the array in transformers.

* Remove _context property from ActivityPub objects in favour of getter function get_json_ld_context()

* fix unit tests: ActivityPub Activity objects have a custom getter for the JsonLD context

* fix phpcs

* fix unit-tests to also support php5.6

* fix phpcs

* add param include_json_ld_context to to_array function
This allows to not set the @context in the resulting array.

* propagate the param include_json_ld_context to nested calls of to_array.

* fix phpcs

* Nested AcitivityPub objects: never build context in inner items in to_array function

* fix: param of set_address may also be an array

* fix typo in comment

* always prefix json-ld context with json-ld and move event class to sub-namespace

* fix usage of reserved object keyword
seems it should not be used as a namespace either

* Merge commit 'b2271cda6b857f879e0abd4f3c6683642d725267' into add/event-objects

* Fix calling non-static function as static

Co-authored-by: Matthias Pfefferle <pfefferle@users.noreply.github.com>

* Partly fix Json-LD contexts in collections

* Update includes/activity/class-base-object.php

* this is implicit

We already set the correct user with `$transformer->change_wp_user_id( $user_id );` so the Actor will be generated properly. We can change the behaviour, but we should not use both.

* this change prevents the Activity to re-use Object vars

this should stay as is, because it pre-fills the Activity with data (for example cc and to) and it will no longer be done with your change.

It is on purpose that it first sets the object and then replaces it with the URI.

See: https://github.com/Automattic/wordpress-activitypub/blob/master/includes/activity/class-activity.php#L195

* add `$include_json_ld_context` support to `to_json`

* disable some more contexts

* remove whitespace

* Add php-comment for 7ed17c042a

* Fix JSON-LD context for ActivityPub objects: child classes may override it.

* coding standards

* call folder/namespace `Extended_Object`

 to be consistent with folder names in singular

* fix: unnessesary nesting of extended-objects

* remove license

I hope this is fine, to have the complete plugin under the MIT @Menrath ?!?

---------

Co-authored-by: Matthias Pfefferle <pfefferle@users.noreply.github.com>
2024-01-18 16:35:52 +01:00

123 lines
2.8 KiB
PHP

<?php
namespace Activitypub\Transformer;
use WP_Post;
use WP_Comment;
use Activitypub\Activity\Activity;
use Activitypub\Activity\Base_Object;
/**
* WordPress Base Transformer
*
* Transformers are responsible for transforming a WordPress objects into different ActivityPub
* Object-Types or Activities.
*/
abstract class Base {
/**
* The WP_Post or WP_Comment object.
*
* This is the source object of the transformer.
*
* @var WP_Post|WP_Comment
*/
protected $wp_object;
/**
* 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
*
* @return Base_Object
*/
public static function transform( $object ) {
return new static( $object );
}
/**
* Base constructor.
*
* @param WP_Post|WP_Comment $wp_object The WordPress object
*/
public function __construct( $wp_object ) {
$this->wp_object = $wp_object;
}
/**
* Transform all properties with available get(ter) functions.
*
* @param Base_Object|object $object
*
* @return Base_Object|object $object
*/
protected function transform_object_properties( $activitypub_object ) {
$vars = $activitypub_object->get_object_var_keys();
foreach ( $vars as $var ) {
$getter = 'get_' . $var;
if ( method_exists( $this, $getter ) ) {
$value = call_user_func( array( $this, $getter ) );
if ( isset( $value ) ) {
$setter = 'set_' . $var;
call_user_func( array( $activitypub_object, $setter ), $value );
}
}
}
return $activitypub_object;
}
/**
* Transform the WordPress Object into an ActivityPub Object.
*
* @return Activitypub\Activity\Base_Object
*/
public function to_object() {
$activitypub_object = new Base_Object();
$activitypub_object = $this->transform_object_properties( $activitypub_object );
return $activitypub_object;
}
/**
* Transforms the ActivityPub Object to an Activity
*
* @param string $type The Activity-Type.
*
* @return \Activitypub\Activity\Activity The Activity.
*/
public function to_activity( $type ) {
$object = $this->to_object();
$activity = new Activity();
$activity->set_type( $type );
// 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
if ( in_array( $type, array( 'Like', 'Announce' ), true ) ) {
$activity->set_object( $object->get_id() );
}
return $activity;
}
/**
* 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
*/
abstract public function change_wp_user_id( $user_id );
}