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 +11 -67 8.2.15.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.
@@ -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;
@@ -165,9 +157,9 @@
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
@@ -182,9 +174,9 @@
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 181 if ( ! is_array( $data ) ) {
190 182 return new \WP_Error( 'invalid_array', __( 'Invalid array', 'activitypub' ), array( 'status' => 400 ) );
@@ -203,13 +195,9 @@
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 -
199 + $key = camel_to_snake_case( $key );
212 200 call_user_func( array( $this, 'set_' . $key ), $value );
213 201 }
214 202 }
215 203 }
@@ -230,20 +218,13 @@
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 228 $vars = get_object_vars( $this );
248 229
249 230 foreach ( $vars as $key => $value ) {
@@ -261,16 +242,13 @@
261 242 $value = call_user_func( array( $this, 'get_' . $key ) );
262 243 }
263 244
264 245 if ( is_object( $value ) ) {
265 - $value = $value->to_array( false, $include_blind_audience );
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
@@ -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 }