PluginProbe
ActivityPub / 2.1.0
ActivityPub v2.1.0
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
activitypub / includes / transformer / class-base.php

class-base.php in ActivityPub 2.1.0, at includes/transformer/class-base.php

124 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Activitypub\Transformer;
3
4 use WP_Post;
5 use WP_Comment;
6
7 use Activitypub\Activity\Activity;
8 use Activitypub\Activity\Base_Object;
9
10 /**
11 * WordPress Base Transformer
12 *
13 * Transformers are responsible for transforming a WordPress objects into different ActivityPub
14 * Object-Types or Activities.
15 */
16 abstract class Base {
17 /**
18 * The WP_Post or WP_Comment object.
19 *
20 * This is the source object of the transformer.
21 *
22 * @var WP_Post|WP_Comment
23 */
24 protected $wp_object;
25
26 /**
27 * Static function to Transform a WordPress Object.
28 *
29 * This helps to chain the output of the Transformer.
30 *
31 * @param WP_Post|WP_Comment $wp_object The WordPress object
32 *
33 * @return Base_Object
34 */
35 public static function transform( $object ) {
36 return new static( $object );
37 }
38
39 /**
40 * Base constructor.
41 *
42 * @param WP_Post|WP_Comment $wp_object The WordPress object
43 */
44 public function __construct( $wp_object ) {
45 $this->wp_object = $wp_object;
46 }
47
48 /**
49 * Transform all properties with available get(ter) functions.
50 *
51 * @param Base_Object|object $object
52 *
53 * @return Base_Object|object $object
54 */
55 protected function transform_object_properties( $activitypub_object ) {
56 $vars = $activitypub_object->get_object_var_keys();
57
58 foreach ( $vars as $var ) {
59 $getter = 'get_' . $var;
60
61 if ( method_exists( $this, $getter ) ) {
62 $value = call_user_func( array( $this, $getter ) );
63
64 if ( isset( $value ) ) {
65 $setter = 'set_' . $var;
66
67 call_user_func( array( $activitypub_object, $setter ), $value );
68 }
69 }
70 }
71 return $activitypub_object;
72 }
73
74 /**
75 * Transform the WordPress Object into an ActivityPub Object.
76 *
77 * @return Activitypub\Activity\Base_Object
78 */
79 public function to_object() {
80 $activitypub_object = new Base_Object();
81 $activitypub_object = $this->transform_object_properties( $activitypub_object );
82
83 return $activitypub_object;
84 }
85
86 /**
87 * Transforms the ActivityPub Object to an Activity
88 *
89 * @param string $type The Activity-Type.
90 *
91 * @return \Activitypub\Activity\Activity The Activity.
92 */
93 public function to_activity( $type ) {
94 $object = $this->to_object();
95
96 $activity = new Activity();
97 $activity->set_type( $type );
98
99 // Pre-fill the Activity with data (for example cc and to).
100 $activity->set_object( $object );
101
102 // Use simple Object (only ID-URI) for Like and Announce
103 if ( in_array( $type, array( 'Like', 'Announce' ), true ) ) {
104 $activity->set_object( $object->get_id() );
105 }
106
107 return $activity;
108 }
109
110 /**
111 * Returns the ID of the WordPress Object.
112 *
113 * @return int The ID of the WordPress Object
114 */
115 abstract public function get_wp_user_id();
116
117 /**
118 * Change the User-ID of the WordPress Post.
119 *
120 * @return int The User-ID of the WordPress Post
121 */
122 abstract public function change_wp_user_id( $user_id );
123 }
124