| 1 |
<?php |
| 2 |
/** |
| 3 |
* Generic Object. |
| 4 |
* |
| 5 |
* @package Activitypub |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Activitypub\Activity; |
| 9 |
|
| 10 |
use function Activitypub\camel_to_snake_case; |
| 11 |
use function Activitypub\snake_to_camel_case; |
| 12 |
|
| 13 |
/** |
| 14 |
* Generic Object. |
| 15 |
* |
| 16 |
* This class is used to create Generic Objects. |
| 17 |
* It is used to create objects that might be unknown by the plugin but |
| 18 |
* conform to the ActivityStreams vocabulary. |
| 19 |
* |
| 20 |
* @since 5.3.0 |
| 21 |
*/ |
| 22 |
#[\AllowDynamicProperties] |
| 23 |
class Generic_Object { |
| 24 |
/** |
| 25 |
* The JSON-LD context for the object. |
| 26 |
* |
| 27 |
* @var array |
| 28 |
*/ |
| 29 |
const JSON_LD_CONTEXT = array( |
| 30 |
'https://www.w3.org/ns/activitystreams', |
| 31 |
); |
| 32 |
|
| 33 |
/** |
| 34 |
* The object's unique global identifier |
| 35 |
* |
| 36 |
* @see https://www.w3.org/TR/activitypub/#obj-id |
| 37 |
* |
| 38 |
* @var string |
| 39 |
*/ |
| 40 |
protected $id; |
| 41 |
|
| 42 |
/** |
| 43 |
* Magic function, to transform the object to string. |
| 44 |
* |
| 45 |
* @return string The object id. |
| 46 |
*/ |
| 47 |
public function __toString() { |
| 48 |
return $this->to_string(); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Function to transform the object to string. |
| 53 |
* |
| 54 |
* @return string The object id. |
| 55 |
*/ |
| 56 |
public function to_string() { |
| 57 |
return $this->get_id(); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Magic function to implement getter and setter. |
| 62 |
* |
| 63 |
* @param string $method The method name. |
| 64 |
* @param string $params The method params. |
| 65 |
*/ |
| 66 |
public function __call( $method, $params ) { |
| 67 |
$var = \strtolower( \substr( $method, 4 ) ); |
| 68 |
|
| 69 |
if ( \strncasecmp( $method, 'get', 3 ) === 0 ) { |
| 70 |
if ( ! $this->has( $var ) ) { |
| 71 |
return null; |
| 72 |
} |
| 73 |
|
| 74 |
return $this->$var; |
| 75 |
} |
| 76 |
|
| 77 |
if ( \strncasecmp( $method, 'set', 3 ) === 0 ) { |
| 78 |
return $this->set( $var, $params[0] ); |
| 79 |
} |
| 80 |
|
| 81 |
if ( \strncasecmp( $method, 'add', 3 ) === 0 ) { |
| 82 |
return $this->add( $var, $params[0] ); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Generic getter. |
| 88 |
* |
| 89 |
* @param string $key The key to get. |
| 90 |
* |
| 91 |
* @return mixed The value. |
| 92 |
*/ |
| 93 |
public function get( $key ) { |
| 94 |
return call_user_func( array( $this, 'get_' . $key ) ); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Generic setter. |
| 99 |
* |
| 100 |
* @param string $key The key to set. |
| 101 |
* @param string $value The value to set. |
| 102 |
* |
| 103 |
* @return mixed The value. |
| 104 |
*/ |
| 105 |
public function set( $key, $value ) { |
| 106 |
$this->$key = $value; |
| 107 |
|
| 108 |
return $this; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Generic adder. |
| 113 |
* |
| 114 |
* @param string $key The key to set. |
| 115 |
* @param mixed $value The value to add. |
| 116 |
* |
| 117 |
* @return mixed The value. |
| 118 |
*/ |
| 119 |
public function add( $key, $value ) { |
| 120 |
if ( empty( $value ) ) { |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
if ( ! isset( $this->$key ) ) { |
| 125 |
$this->$key = array(); |
| 126 |
} |
| 127 |
|
| 128 |
if ( is_string( $this->$key ) ) { |
| 129 |
$this->$key = array( $this->$key ); |
| 130 |
} |
| 131 |
|
| 132 |
$attributes = $this->$key; |
| 133 |
|
| 134 |
if ( is_array( $value ) ) { |
| 135 |
$attributes = array_merge( $attributes, $value ); |
| 136 |
} else { |
| 137 |
$attributes[] = $value; |
| 138 |
} |
| 139 |
|
| 140 |
$this->$key = array_unique( $attributes ); |
| 141 |
|
| 142 |
return $this->$key; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Check if the object has a key |
| 147 |
* |
| 148 |
* @param string $key The key to check. |
| 149 |
* |
| 150 |
* @return boolean True if the object has the key. |
| 151 |
*/ |
| 152 |
public function has( $key ) { |
| 153 |
return property_exists( $this, $key ); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Convert JSON input to an array. |
| 158 |
* |
| 159 |
* @param string $json The JSON string. |
| 160 |
* |
| 161 |
* @return Generic_Object|\WP_Error An Object built from the JSON string or WP_Error when it's not a JSON string. |
| 162 |
*/ |
| 163 |
public static function init_from_json( $json ) { |
| 164 |
$array = \json_decode( $json, true ); |
| 165 |
|
| 166 |
if ( ! is_array( $array ) ) { |
| 167 |
return new \WP_Error( 'invalid_json', __( 'Invalid JSON', 'activitypub' ), array( 'status' => 400 ) ); |
| 168 |
} |
| 169 |
|
| 170 |
return self::init_from_array( $array ); |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Convert input array to a Base_Object. |
| 175 |
* |
| 176 |
* @param array $data The object array. |
| 177 |
* |
| 178 |
* @return Generic_Object|\WP_Error An Object built from the input array or WP_Error when it's not an array. |
| 179 |
*/ |
| 180 |
public static function init_from_array( $data ) { |
| 181 |
if ( ! is_array( $data ) ) { |
| 182 |
return new \WP_Error( 'invalid_array', __( 'Invalid array', 'activitypub' ), array( 'status' => 400 ) ); |
| 183 |
} |
| 184 |
|
| 185 |
$object = new static(); |
| 186 |
$object->from_array( $data ); |
| 187 |
|
| 188 |
return $object; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Convert JSON input to an array and pre-fill the object. |
| 193 |
* |
| 194 |
* @param array $data The array. |
| 195 |
*/ |
| 196 |
public function from_array( $data ) { |
| 197 |
foreach ( $data as $key => $value ) { |
| 198 |
if ( null !== $value ) { |
| 199 |
$key = camel_to_snake_case( $key ); |
| 200 |
call_user_func( array( $this, 'set_' . $key ), $value ); |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Convert JSON input to an array and pre-fill the object. |
| 207 |
* |
| 208 |
* @param string $json The JSON string. |
| 209 |
*/ |
| 210 |
public function from_json( $json ) { |
| 211 |
$array = \json_decode( $json, true ); |
| 212 |
|
| 213 |
$this->from_array( $array ); |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Convert Object to an array. |
| 218 |
* |
| 219 |
* It tries to get the object attributes if they exist |
| 220 |
* and falls back to the getters. Empty values are ignored. |
| 221 |
* |
| 222 |
* @param bool $include_json_ld_context Whether to include the JSON-LD context. Default true. |
| 223 |
* |
| 224 |
* @return array An array built from the Object. |
| 225 |
*/ |
| 226 |
public function to_array( $include_json_ld_context = true ) { |
| 227 |
$array = array(); |
| 228 |
$vars = get_object_vars( $this ); |
| 229 |
|
| 230 |
foreach ( $vars as $key => $value ) { |
| 231 |
if ( \is_wp_error( $value ) ) { |
| 232 |
continue; |
| 233 |
} |
| 234 |
|
| 235 |
// Ignore all _prefixed keys. |
| 236 |
if ( '_' === substr( $key, 0, 1 ) ) { |
| 237 |
continue; |
| 238 |
} |
| 239 |
|
| 240 |
// If value is empty, try to get it from a getter. |
| 241 |
if ( ! $value ) { |
| 242 |
$value = call_user_func( array( $this, 'get_' . $key ) ); |
| 243 |
} |
| 244 |
|
| 245 |
if ( is_object( $value ) ) { |
| 246 |
$value = $value->to_array( false ); |
| 247 |
} |
| 248 |
|
| 249 |
// If value is still empty, ignore it for the array and continue. |
| 250 |
if ( isset( $value ) ) { |
| 251 |
$array[ snake_to_camel_case( $key ) ] = $value; |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
if ( $include_json_ld_context ) { |
| 256 |
// Get JsonLD context and move it to '@context' at the top. |
| 257 |
$array = array_merge( array( '@context' => $this->get_json_ld_context() ), $array ); |
| 258 |
} |
| 259 |
|
| 260 |
$class = new \ReflectionClass( $this ); |
| 261 |
$class = strtolower( $class->getShortName() ); |
| 262 |
|
| 263 |
/** |
| 264 |
* Filter the array of the ActivityPub object. |
| 265 |
* |
| 266 |
* @param array $array The array of the ActivityPub object. |
| 267 |
* @param string $class The class of the ActivityPub object. |
| 268 |
* @param string $id The ID of the ActivityPub object. |
| 269 |
* @param Generic_Object $object The ActivityPub object. |
| 270 |
* |
| 271 |
* @return array The filtered array of the ActivityPub object. |
| 272 |
*/ |
| 273 |
$array = \apply_filters( 'activitypub_activity_object_array', $array, $class, $this->id, $this ); |
| 274 |
|
| 275 |
/** |
| 276 |
* Filter the array of the ActivityPub object by class. |
| 277 |
* |
| 278 |
* @param array $array The array of the ActivityPub object. |
| 279 |
* @param string $id The ID of the ActivityPub object. |
| 280 |
* @param Generic_Object $object The ActivityPub object. |
| 281 |
* |
| 282 |
* @return array The filtered array of the ActivityPub object. |
| 283 |
*/ |
| 284 |
return \apply_filters( "activitypub_activity_{$class}_object_array", $array, $this->id, $this ); |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* Convert Object to JSON. |
| 289 |
* |
| 290 |
* @param bool $include_json_ld_context Whether to include the JSON-LD context. Default true. |
| 291 |
* |
| 292 |
* @return string The JSON string. |
| 293 |
*/ |
| 294 |
public function to_json( $include_json_ld_context = true ) { |
| 295 |
$array = $this->to_array( $include_json_ld_context ); |
| 296 |
$options = \JSON_HEX_TAG | \JSON_HEX_AMP | \JSON_HEX_QUOT | \JSON_UNESCAPED_SLASHES; |
| 297 |
|
| 298 |
/** |
| 299 |
* Options to be passed to json_encode(). |
| 300 |
* |
| 301 |
* @param int $options The current options flags. |
| 302 |
*/ |
| 303 |
$options = \apply_filters( 'activitypub_json_encode_options', $options ); |
| 304 |
|
| 305 |
return \wp_json_encode( $array, $options ); |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Returns the keys of the object vars. |
| 310 |
* |
| 311 |
* @return array The keys of the object vars. |
| 312 |
*/ |
| 313 |
public function get_object_var_keys() { |
| 314 |
return \array_keys( \get_object_vars( $this ) ); |
| 315 |
} |
| 316 |
|
| 317 |
/** |
| 318 |
* Returns the JSON-LD context of this object. |
| 319 |
* |
| 320 |
* @return array $context A compacted JSON-LD context for the ActivityPub object. |
| 321 |
*/ |
| 322 |
public function get_json_ld_context() { |
| 323 |
return static::JSON_LD_CONTEXT; |
| 324 |
} |
| 325 |
} |
| 326 |
|