PluginProbe
ActivityPub / 3.2.4
ActivityPub v3.2.4
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 3.2.4, at includes/transformer/class-base.php

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