updated plugin ActivityPub version 8.3.0
This commit is contained in:
@ -17,6 +17,10 @@ use function Activitypub\snake_to_camel_case;
|
||||
* It is used to create objects that might be unknown by the plugin but
|
||||
* conform to the ActivityStreams vocabulary.
|
||||
*
|
||||
* Provides generic magic methods for getting, setting, and adding properties
|
||||
* through __call(). Specific property documentation is in the classes where
|
||||
* the properties are actually defined.
|
||||
*
|
||||
* @since 5.3.0
|
||||
*/
|
||||
#[\AllowDynamicProperties]
|
||||
@ -62,6 +66,8 @@ class Generic_Object {
|
||||
*
|
||||
* @param string $method The method name.
|
||||
* @param string $params The method params.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function __call( $method, $params ) {
|
||||
$var = \strtolower( \substr( $method, 4 ) );
|
||||
@ -81,6 +87,8 @@ class Generic_Object {
|
||||
if ( \strncasecmp( $method, 'add', 3 ) === 0 ) {
|
||||
return $this->add( $var, $params[0] );
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -114,7 +122,7 @@ class Generic_Object {
|
||||
* @param string $key The key to set.
|
||||
* @param mixed $value The value to add.
|
||||
*
|
||||
* @return mixed The value.
|
||||
* @return mixed|void The value.
|
||||
*/
|
||||
public function add( $key, $value ) {
|
||||
if ( empty( $value ) ) {
|
||||
@ -158,7 +166,7 @@ class Generic_Object {
|
||||
*
|
||||
* @param string $json The JSON string.
|
||||
*
|
||||
* @return Generic_Object|\WP_Error An Object built from the JSON string or WP_Error when it's not a JSON string.
|
||||
* @return static|\WP_Error An Object built from the JSON string or WP_Error when it's not a JSON string.
|
||||
*/
|
||||
public static function init_from_json( $json ) {
|
||||
$array = \json_decode( $json, true );
|
||||
@ -175,7 +183,7 @@ class Generic_Object {
|
||||
*
|
||||
* @param array $data The object array.
|
||||
*
|
||||
* @return Generic_Object|\WP_Error An Object built from the input array or WP_Error when it's not an array.
|
||||
* @return static|\WP_Error An Object built from the input array or WP_Error when it's not an array.
|
||||
*/
|
||||
public static function init_from_array( $data ) {
|
||||
if ( ! is_array( $data ) ) {
|
||||
@ -196,7 +204,11 @@ class Generic_Object {
|
||||
public function from_array( $data ) {
|
||||
foreach ( $data as $key => $value ) {
|
||||
if ( null !== $value ) {
|
||||
$key = camel_to_snake_case( $key );
|
||||
// Convert camelCase to snake_case if not prefixed with '_'.
|
||||
if ( ! \str_starts_with( $key, '_' ) ) {
|
||||
$key = camel_to_snake_case( $key );
|
||||
}
|
||||
|
||||
call_user_func( array( $this, 'set_' . $key ), $value );
|
||||
}
|
||||
}
|
||||
@ -219,11 +231,18 @@ class Generic_Object {
|
||||
* It tries to get the object attributes if they exist
|
||||
* and falls back to the getters. Empty values are ignored.
|
||||
*
|
||||
* By default, `bto` and `bcc` (the blind audience fields) are stripped
|
||||
* from the output per ActivityPub Section 6, so every serialization path
|
||||
* is safe for emission. Internal callers that need to persist the blind
|
||||
* audience (e.g., outbox/inbox storage) can opt in by passing
|
||||
* `$include_blind_audience = true`.
|
||||
*
|
||||
* @param bool $include_json_ld_context Whether to include the JSON-LD context. Default true.
|
||||
* @param bool $include_blind_audience Whether to keep `bto` and `bcc` in the output. Default false.
|
||||
*
|
||||
* @return array An array built from the Object.
|
||||
*/
|
||||
public function to_array( $include_json_ld_context = true ) {
|
||||
public function to_array( $include_json_ld_context = true, $include_blind_audience = false ) {
|
||||
$array = array();
|
||||
$vars = get_object_vars( $this );
|
||||
|
||||
@ -243,11 +262,14 @@ class Generic_Object {
|
||||
}
|
||||
|
||||
if ( is_object( $value ) ) {
|
||||
$value = $value->to_array( false );
|
||||
$value = $value->to_array( false, $include_blind_audience );
|
||||
}
|
||||
|
||||
// If value is still empty, ignore it for the array and continue.
|
||||
if ( isset( $value ) ) {
|
||||
if ( is_array( $value ) && $this->is_namespaced( $key ) ) {
|
||||
foreach ( $value as $sub_key => $sub_value ) {
|
||||
$array[ snake_to_camel_case( $key ) . ':' . snake_to_camel_case( $sub_key ) ] = $sub_value;
|
||||
}
|
||||
} elseif ( isset( $value ) ) {
|
||||
$array[ snake_to_camel_case( $key ) ] = $value;
|
||||
}
|
||||
}
|
||||
@ -281,18 +303,33 @@ class Generic_Object {
|
||||
*
|
||||
* @return array The filtered array of the ActivityPub object.
|
||||
*/
|
||||
return \apply_filters( "activitypub_activity_{$class}_object_array", $array, $this->id, $this );
|
||||
$array = \apply_filters( "activitypub_activity_{$class}_object_array", $array, $this->id, $this );
|
||||
|
||||
if ( ! $include_blind_audience ) {
|
||||
/*
|
||||
* Strip `bto` and `bcc` from the serialized array per ActivityPub Section 6.
|
||||
* Callers that need the blind audience either read it from the object via
|
||||
* `get_bto()` / `get_bcc()` or opt in with `$include_blind_audience = true`.
|
||||
*/
|
||||
unset( $array['bto'], $array['bcc'] );
|
||||
if ( isset( $array['object'] ) && \is_array( $array['object'] ) ) {
|
||||
unset( $array['object']['bto'], $array['object']['bcc'] );
|
||||
}
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert Object to JSON.
|
||||
*
|
||||
* @param bool $include_json_ld_context Whether to include the JSON-LD context. Default true.
|
||||
* @param bool $include_blind_audience Whether to keep `bto` and `bcc` in the output. Default false.
|
||||
*
|
||||
* @return string The JSON string.
|
||||
*/
|
||||
public function to_json( $include_json_ld_context = true ) {
|
||||
$array = $this->to_array( $include_json_ld_context );
|
||||
public function to_json( $include_json_ld_context = true, $include_blind_audience = false ) {
|
||||
$array = $this->to_array( $include_json_ld_context, $include_blind_audience );
|
||||
$options = \JSON_HEX_TAG | \JSON_HEX_AMP | \JSON_HEX_QUOT | \JSON_UNESCAPED_SLASHES;
|
||||
|
||||
/**
|
||||
@ -322,4 +359,23 @@ class Generic_Object {
|
||||
public function get_json_ld_context() {
|
||||
return static::JSON_LD_CONTEXT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if an attribute is in a namespace.
|
||||
*
|
||||
* @param string $attribute The attribute to check.
|
||||
*
|
||||
* @return bool Whether the attribute is namespaced.
|
||||
*/
|
||||
private function is_namespaced( $attribute ) {
|
||||
$namespaces = array();
|
||||
|
||||
foreach ( static::JSON_LD_CONTEXT as $context ) {
|
||||
if ( is_array( $context ) ) {
|
||||
$namespaces = \array_merge( $namespaces, $context );
|
||||
}
|
||||
}
|
||||
|
||||
return isset( $namespaces[ $attribute ] ) && \wp_http_validate_url( $namespaces[ $attribute ] );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user