PluginProbe
ActivityPub / 4.0.2
ActivityPub v4.0.2
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 4.0.2, at includes/transformer/class-base.php

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