PluginProbe
ActivityPub / 5.7.0
ActivityPub v5.7.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
← All changes | includes/activity/class-generic-object.php +28 -84 9.2.05.7.0 View file →
@@ -16,12 +16,8 @@
16 16 * This class is used to create Generic Objects.
17 17 * It is used to create objects that might be unknown by the plugin but
18 18 * conform to the ActivityStreams vocabulary.
19 19 *
20 - * Provides generic magic methods for getting, setting, and adding properties
21 - * through __call(). Specific property documentation is in the classes where
22 - * the properties are actually defined.
23 - *
24 20 * @since 5.3.0
25 21 */
26 22 #[\AllowDynamicProperties]
27 23 class Generic_Object {
@@ -65,10 +61,8 @@
65 61 * Magic function to implement getter and setter.
66 62 *
67 63 * @param string $method The method name.
68 64 * @param string $params The method params.
69 - *
70 - * @return mixed
71 65 */
72 66 public function __call( $method, $params ) {
73 67 $var = \strtolower( \substr( $method, 4 ) );
74 68
@@ -86,10 +80,8 @@
86 80
87 81 if ( \strncasecmp( $method, 'add', 3 ) === 0 ) {
88 82 return $this->add( $var, $params[0] );
89 83 }
90 -
91 - return null;
92 84 }
93 85
94 86 /**
95 87 * Generic getter.
@@ -98,9 +90,9 @@
98 90 *
99 91 * @return mixed The value.
100 92 */
101 93 public function get( $key ) {
102 - return \call_user_func( array( $this, 'get_' . $key ) );
94 + return call_user_func( array( $this, 'get_' . $key ) );
103 95 }
104 96
105 97 /**
106 98 * Generic setter.
@@ -121,9 +113,9 @@
121 113 *
122 114 * @param string $key The key to set.
123 115 * @param mixed $value The value to add.
124 116 *
125 - * @return mixed|void The value.
117 + * @return mixed The value.
126 118 */
127 119 public function add( $key, $value ) {
128 120 if ( empty( $value ) ) {
129 121 return;
@@ -132,21 +124,21 @@
132 124 if ( ! isset( $this->$key ) ) {
133 125 $this->$key = array();
134 126 }
135 127
136 - if ( \is_string( $this->$key ) ) {
128 + if ( is_string( $this->$key ) ) {
137 129 $this->$key = array( $this->$key );
138 130 }
139 131
140 132 $attributes = $this->$key;
141 133
142 - if ( \is_array( $value ) ) {
143 - $attributes = \array_merge( $attributes, $value );
134 + if ( is_array( $value ) ) {
135 + $attributes = array_merge( $attributes, $value );
144 136 } else {
145 137 $attributes[] = $value;
146 138 }
147 139
148 - $this->$key = \array_unique( $attributes );
140 + $this->$key = array_unique( $attributes );
149 141
150 142 return $this->$key;
151 143 }
152 144
@@ -157,9 +149,9 @@
157 149 *
158 150 * @return boolean True if the object has the key.
159 151 */
160 152 public function has( $key ) {
161 - return \property_exists( $this, $key );
153 + return property_exists( $this, $key );
162 154 }
163 155
164 156 /**
165 157 * Convert JSON input to an array.
@@ -165,15 +157,15 @@
165 157 * Convert JSON input to an array.
166 158 *
167 159 * @param string $json The JSON string.
168 160 *
169 - * @return static|\WP_Error An Object built from the JSON string or WP_Error when it's not a JSON string.
161 + * @return Generic_Object|\WP_Error An Object built from the JSON string or WP_Error when it's not a JSON string.
170 162 */
171 163 public static function init_from_json( $json ) {
172 164 $array = \json_decode( $json, true );
173 165
174 - if ( ! \is_array( $array ) ) {
175 - return new \WP_Error( 'invalid_json', \__( 'Invalid JSON', 'activitypub' ), array( 'status' => 400 ) );
166 + if ( ! is_array( $array ) ) {
167 + return new \WP_Error( 'invalid_json', __( 'Invalid JSON', 'activitypub' ), array( 'status' => 400 ) );
176 168 }
177 169
178 170 return self::init_from_array( $array );
179 171 }
@@ -182,13 +174,13 @@
182 174 * Convert input array to a Base_Object.
183 175 *
184 176 * @param array $data The object array.
185 177 *
186 - * @return static|\WP_Error An Object built from the input array or WP_Error when it's not an array.
178 + * @return Generic_Object|\WP_Error An Object built from the input array or WP_Error when it's not an array.
187 179 */
188 180 public static function init_from_array( $data ) {
189 - if ( ! \is_array( $data ) ) {
190 - return new \WP_Error( 'invalid_array', \__( 'Invalid array', 'activitypub' ), array( 'status' => 400 ) );
181 + if ( ! is_array( $data ) ) {
182 + return new \WP_Error( 'invalid_array', __( 'Invalid array', 'activitypub' ), array( 'status' => 400 ) );
191 183 }
192 184
193 185 $object = new static();
194 186 $object->from_array( $data );
@@ -203,14 +195,10 @@
203 195 */
204 196 public function from_array( $data ) {
205 197 foreach ( $data as $key => $value ) {
206 198 if ( null !== $value ) {
207 - // Convert camelCase to snake_case if not prefixed with '_'.
208 - if ( ! \str_starts_with( $key, '_' ) ) {
209 - $key = camel_to_snake_case( $key );
210 - }
211 -
212 - \call_user_func( array( $this, 'set_' . $key ), $value );
199 + $key = camel_to_snake_case( $key );
200 + call_user_func( array( $this, 'set_' . $key ), $value );
213 201 }
214 202 }
215 203 }
216 204
@@ -230,22 +218,15 @@
230 218 *
231 219 * It tries to get the object attributes if they exist
232 220 * and falls back to the getters. Empty values are ignored.
233 221 *
234 - * By default, `bto` and `bcc` (the blind audience fields) are stripped
235 - * from the output per ActivityPub Section 6, so every serialization path
236 - * is safe for emission. Internal callers that need to persist the blind
237 - * audience (e.g., outbox/inbox storage) can opt in by passing
238 - * `$include_blind_audience = true`.
239 - *
240 222 * @param bool $include_json_ld_context Whether to include the JSON-LD context. Default true.
241 - * @param bool $include_blind_audience Whether to keep `bto` and `bcc` in the output. Default false.
242 223 *
243 224 * @return array An array built from the Object.
244 225 */
245 - public function to_array( $include_json_ld_context = true, $include_blind_audience = false ) {
226 + public function to_array( $include_json_ld_context = true ) {
246 227 $array = array();
247 - $vars = \get_object_vars( $this );
228 + $vars = get_object_vars( $this );
248 229
249 230 foreach ( $vars as $key => $value ) {
250 231 if ( \is_wp_error( $value ) ) {
251 232 continue;
@@ -251,26 +232,23 @@
251 232 continue;
252 233 }
253 234
254 235 // Ignore all _prefixed keys.
255 - if ( '_' === \substr( $key, 0, 1 ) ) {
236 + if ( '_' === substr( $key, 0, 1 ) ) {
256 237 continue;
257 238 }
258 239
259 240 // If value is empty, try to get it from a getter.
260 241 if ( ! $value ) {
261 - $value = \call_user_func( array( $this, 'get_' . $key ) );
242 + $value = call_user_func( array( $this, 'get_' . $key ) );
262 243 }
263 244
264 - if ( \is_object( $value ) ) {
265 - $value = $value->to_array( false, $include_blind_audience );
245 + if ( is_object( $value ) ) {
246 + $value = $value->to_array( false );
266 247 }
267 248
268 - if ( \is_array( $value ) && $this->is_namespaced( $key ) ) {
269 - foreach ( $value as $sub_key => $sub_value ) {
270 - $array[ snake_to_camel_case( $key ) . ':' . snake_to_camel_case( $sub_key ) ] = $sub_value;
271 - }
272 - } elseif ( isset( $value ) ) {
249 + // If value is still empty, ignore it for the array and continue.
250 + if ( isset( $value ) ) {
273 251 $array[ snake_to_camel_case( $key ) ] = $value;
274 252 }
275 253 }
276 254
@@ -275,13 +253,13 @@
275 253 }
276 254
277 255 if ( $include_json_ld_context ) {
278 256 // Get JsonLD context and move it to '@context' at the top.
279 - $array = \array_merge( array( '@context' => $this->get_json_ld_context() ), $array );
257 + $array = array_merge( array( '@context' => $this->get_json_ld_context() ), $array );
280 258 }
281 259
282 260 $class = new \ReflectionClass( $this );
283 - $class = \strtolower( $class->getShortName() );
261 + $class = strtolower( $class->getShortName() );
284 262
285 263 /**
286 264 * Filter the array of the ActivityPub object.
287 265 *
@@ -302,23 +280,9 @@
302 280 * @param Generic_Object $object The ActivityPub object.
303 281 *
304 282 * @return array The filtered array of the ActivityPub object.
305 283 */
306 - $array = \apply_filters( "activitypub_activity_{$class}_object_array", $array, $this->id, $this );
307 -
308 - if ( ! $include_blind_audience ) {
309 - /*
310 - * Strip `bto` and `bcc` from the serialized array per ActivityPub Section 6.
311 - * Callers that need the blind audience either read it from the object via
312 - * `get_bto()` / `get_bcc()` or opt in with `$include_blind_audience = true`.
313 - */
314 - unset( $array['bto'], $array['bcc'] );
315 - if ( isset( $array['object'] ) && \is_array( $array['object'] ) ) {
316 - unset( $array['object']['bto'], $array['object']['bcc'] );
317 - }
318 - }
319 -
320 - return $array;
284 + return \apply_filters( "activitypub_activity_{$class}_object_array", $array, $this->id, $this );
321 285 }
322 286
323 287 /**
324 288 * Convert Object to JSON.
@@ -323,14 +287,13 @@
323 287 /**
324 288 * Convert Object to JSON.
325 289 *
326 290 * @param bool $include_json_ld_context Whether to include the JSON-LD context. Default true.
327 - * @param bool $include_blind_audience Whether to keep `bto` and `bcc` in the output. Default false.
328 291 *
329 292 * @return string The JSON string.
330 293 */
331 - public function to_json( $include_json_ld_context = true, $include_blind_audience = false ) {
332 - $array = $this->to_array( $include_json_ld_context, $include_blind_audience );
294 + public function to_json( $include_json_ld_context = true ) {
295 + $array = $this->to_array( $include_json_ld_context );
333 296 $options = \JSON_HEX_TAG | \JSON_HEX_AMP | \JSON_HEX_QUOT | \JSON_UNESCAPED_SLASHES;
334 297
335 298 /**
336 299 * Options to be passed to json_encode().
@@ -357,25 +320,6 @@
357 320 * @return array $context A compacted JSON-LD context for the ActivityPub object.
358 321 */
359 322 public function get_json_ld_context() {
360 323 return static::JSON_LD_CONTEXT;
361 - }
362 -
363 - /**
364 - * Checks if an attribute is in a namespace.
365 - *
366 - * @param string $attribute The attribute to check.
367 - *
368 - * @return bool Whether the attribute is namespaced.
369 - */
370 - private function is_namespaced( $attribute ) {
371 - $namespaces = array();
372 -
373 - foreach ( static::JSON_LD_CONTEXT as $context ) {
374 - if ( \is_array( $context ) ) {
375 - $namespaces = \array_merge( $namespaces, $context );
376 - }
377 - }
378 -
379 - return isset( $namespaces[ $attribute ] ) && \wp_http_validate_url( $namespaces[ $attribute ] );
380 324 }
381 325 }