| 1 |
<?php |
| 2 |
/** |
| 3 |
* Base Transformer Class file. |
| 4 |
* |
| 5 |
* @package Activitypub |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Activitypub\Transformer; |
| 9 |
|
| 10 |
use WP_Comment; |
| 11 |
use WP_Post; |
| 12 |
use WP_Term; |
| 13 |
|
| 14 |
use Activitypub\Activity\Activity; |
| 15 |
use Activitypub\Collection\Actors; |
| 16 |
use Activitypub\Activity\Base_Object; |
| 17 |
|
| 18 |
/** |
| 19 |
* WordPress Base Transformer. |
| 20 |
* |
| 21 |
* Transformers are responsible for transforming a WordPress objects into different ActivityPub |
| 22 |
* Object-Types or Activities. |
| 23 |
*/ |
| 24 |
abstract class Base { |
| 25 |
/** |
| 26 |
* The WP_Post or WP_Comment object. |
| 27 |
* |
| 28 |
* This is the source object of the transformer. |
| 29 |
* |
| 30 |
* @var WP_Post|WP_Comment|Base_Object|string|array|WP_Term |
| 31 |
*/ |
| 32 |
protected $item; |
| 33 |
|
| 34 |
/** |
| 35 |
* The WP_Post or WP_Comment object. |
| 36 |
* |
| 37 |
* @deprecated version 5.0.0 |
| 38 |
* |
| 39 |
* @var WP_Post|WP_Comment |
| 40 |
*/ |
| 41 |
protected $wp_object; |
| 42 |
|
| 43 |
/** |
| 44 |
* The content visibility. |
| 45 |
* |
| 46 |
* @var string |
| 47 |
*/ |
| 48 |
protected $content_visibility; |
| 49 |
|
| 50 |
/** |
| 51 |
* Static function to Transform a WordPress Object. |
| 52 |
* |
| 53 |
* This helps to chain the output of the Transformer. |
| 54 |
* |
| 55 |
* @param WP_Post|WP_Comment|Base_Object|string|array|WP_term $item The item that should be transformed. |
| 56 |
* |
| 57 |
* @return Base |
| 58 |
*/ |
| 59 |
public static function transform( $item ) { |
| 60 |
return new static( $item ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Base constructor. |
| 65 |
* |
| 66 |
* @param WP_Post|WP_Comment|Base_Object|string|array|WP_Term $item The item that should be transformed. |
| 67 |
*/ |
| 68 |
public function __construct( $item ) { |
| 69 |
$this->item = $item; |
| 70 |
$this->wp_object = $item; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Transform all properties with available get(ter) functions. |
| 75 |
* |
| 76 |
* @param Base_Object $activity_object The ActivityPub Object. |
| 77 |
* |
| 78 |
* @return Base_Object|\WP_Error The transformed ActivityPub Object or WP_Error on failure. |
| 79 |
*/ |
| 80 |
protected function transform_object_properties( $activity_object ) { |
| 81 |
if ( ! $activity_object || \is_wp_error( $activity_object ) ) { |
| 82 |
return $activity_object; |
| 83 |
} |
| 84 |
|
| 85 |
$vars = $activity_object->get_object_var_keys(); |
| 86 |
|
| 87 |
foreach ( $vars as $var ) { |
| 88 |
$getter = 'get_' . $var; |
| 89 |
|
| 90 |
if ( \method_exists( $this, $getter ) ) { |
| 91 |
$value = \call_user_func( array( $this, $getter ) ); |
| 92 |
|
| 93 |
if ( null !== $value ) { |
| 94 |
$setter = 'set_' . $var; |
| 95 |
|
| 96 |
/** |
| 97 |
* Filter the value before it is set to the Activity-Object `$activity_object`. |
| 98 |
* |
| 99 |
* @param mixed $value The value that should be set. |
| 100 |
* @param mixed $item The Object. |
| 101 |
*/ |
| 102 |
$value = \apply_filters( "activitypub_transform_{$setter}", $value, $this->item ); |
| 103 |
|
| 104 |
/** |
| 105 |
* Filter the value before it is set to the Activity-Object `$activity_object`. |
| 106 |
* |
| 107 |
* @param mixed $value The value that should be set. |
| 108 |
* @param string $var The variable name. |
| 109 |
* @param mixed $item The Object. |
| 110 |
*/ |
| 111 |
$value = \apply_filters( 'activitypub_transform_set', $value, $var, $this->item ); |
| 112 |
|
| 113 |
\call_user_func( array( $activity_object, $setter ), $value ); |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
return $activity_object; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Transform the item into an ActivityPub Object. |
| 123 |
* |
| 124 |
* @return Base_Object|object The Activity-Object. |
| 125 |
*/ |
| 126 |
public function to_object() { |
| 127 |
$activity_object = new Base_Object(); |
| 128 |
$activity_object = $this->transform_object_properties( $activity_object ); |
| 129 |
|
| 130 |
if ( \is_wp_error( $activity_object ) ) { |
| 131 |
return $activity_object; |
| 132 |
} |
| 133 |
|
| 134 |
$activity_object = $this->set_audience( $activity_object ); |
| 135 |
|
| 136 |
return $activity_object; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Get the content visibility. |
| 141 |
* |
| 142 |
* @return string The content visibility. |
| 143 |
*/ |
| 144 |
public function get_content_visibility() { |
| 145 |
if ( ! $this->content_visibility ) { |
| 146 |
return ACTIVITYPUB_CONTENT_VISIBILITY_PUBLIC; |
| 147 |
} |
| 148 |
|
| 149 |
return $this->content_visibility; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Set the content visibility. |
| 154 |
* |
| 155 |
* @param string $content_visibility The content visibility. |
| 156 |
*/ |
| 157 |
public function set_content_visibility( $content_visibility ) { |
| 158 |
$this->content_visibility = $content_visibility; |
| 159 |
|
| 160 |
return $this; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Set the audience. |
| 165 |
* |
| 166 |
* @param Base_Object $activity_object The ActivityPub Object. |
| 167 |
* |
| 168 |
* @return Base_Object The ActivityPub Object. |
| 169 |
*/ |
| 170 |
protected function set_audience( $activity_object ) { |
| 171 |
$public = 'https://www.w3.org/ns/activitystreams#Public'; |
| 172 |
$actor = Actors::get_by_resource( $this->get_attributed_to() ); |
| 173 |
if ( ! $actor || is_wp_error( $actor ) ) { |
| 174 |
$followers = null; |
| 175 |
} else { |
| 176 |
$followers = $actor->get_followers(); |
| 177 |
} |
| 178 |
$mentions = array_values( $this->get_mentions() ); |
| 179 |
|
| 180 |
switch ( $this->get_content_visibility() ) { |
| 181 |
case ACTIVITYPUB_CONTENT_VISIBILITY_PUBLIC: |
| 182 |
$activity_object->add_to( $public ); |
| 183 |
$activity_object->add_cc( $followers ); |
| 184 |
$activity_object->add_cc( $mentions ); |
| 185 |
break; |
| 186 |
case ACTIVITYPUB_CONTENT_VISIBILITY_QUIET_PUBLIC: |
| 187 |
$activity_object->add_to( $followers ); |
| 188 |
$activity_object->add_to( $mentions ); |
| 189 |
$activity_object->add_cc( $public ); |
| 190 |
break; |
| 191 |
case ACTIVITYPUB_CONTENT_VISIBILITY_PRIVATE: |
| 192 |
$activity_object->add_to( $mentions ); |
| 193 |
} |
| 194 |
|
| 195 |
return $activity_object; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Transform the item to an ActivityPub ID. |
| 200 |
* |
| 201 |
* @return string The ID of the WordPress Object. |
| 202 |
*/ |
| 203 |
public function to_id() { |
| 204 |
/* @var Attachment|Comment|Json|Post|User $this Object transformer. */ |
| 205 |
return $this->get_id(); |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Transforms the ActivityPub Object to an Activity |
| 210 |
* |
| 211 |
* @param string $type The Activity-Type. |
| 212 |
* |
| 213 |
* @return Activity The Activity. |
| 214 |
*/ |
| 215 |
public function to_activity( $type ) { |
| 216 |
$object = $this->to_object(); |
| 217 |
|
| 218 |
$activity = new Activity(); |
| 219 |
$activity->set_type( $type ); |
| 220 |
|
| 221 |
// Pre-fill the Activity with data (for example cc and to). |
| 222 |
$activity->set_object( $object ); |
| 223 |
|
| 224 |
// Use simple Object (only ID-URI) for Like and Announce. |
| 225 |
if ( 'Like' === $type ) { |
| 226 |
$activity->set_object( $object->get_id() ); |
| 227 |
} |
| 228 |
|
| 229 |
return $activity; |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Returns a generic locale based on the Blog settings. |
| 234 |
* |
| 235 |
* @return string The locale of the blog. |
| 236 |
*/ |
| 237 |
protected function get_locale() { |
| 238 |
$lang = \strtolower( \strtok( \get_locale(), '_-' ) ); |
| 239 |
|
| 240 |
if ( $this->item instanceof \WP_Post ) { |
| 241 |
/** |
| 242 |
* Deprecates the `activitypub_post_locale` filter. |
| 243 |
* |
| 244 |
* @param string $lang The locale of the post. |
| 245 |
* @param mixed $item The post object. |
| 246 |
* |
| 247 |
* @return string The filtered locale of the post. |
| 248 |
*/ |
| 249 |
$lang = apply_filters_deprecated( |
| 250 |
'activitypub_post_locale', |
| 251 |
array( |
| 252 |
$lang, |
| 253 |
$this->item->ID, |
| 254 |
$this->item, |
| 255 |
), |
| 256 |
'5.4.0', |
| 257 |
'activitypub_locale', |
| 258 |
'Use the `activitypub_locale` filter instead.' |
| 259 |
); |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* Filter the locale of the post. |
| 264 |
* |
| 265 |
* @param string $lang The locale of the post. |
| 266 |
* @param mixed $item The post object. |
| 267 |
* |
| 268 |
* @return string The filtered locale of the post. |
| 269 |
*/ |
| 270 |
return apply_filters( 'activitypub_locale', $lang, $this->item ); |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* Returns the default media type for an Object. |
| 275 |
* |
| 276 |
* @return string The media type. |
| 277 |
*/ |
| 278 |
public function get_media_type() { |
| 279 |
return 'text/html'; |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* Returns the content map for the post. |
| 284 |
* |
| 285 |
* @return array|null The content map for the post or null if not set. |
| 286 |
*/ |
| 287 |
protected function get_content_map() { |
| 288 |
if ( ! \method_exists( $this, 'get_content' ) || ! $this->get_content() ) { |
| 289 |
return null; |
| 290 |
} |
| 291 |
|
| 292 |
return array( |
| 293 |
$this->get_locale() => $this->get_content(), |
| 294 |
); |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Returns the name map for the post. |
| 299 |
* |
| 300 |
* @return array|null The name map for the post or null if not set. |
| 301 |
*/ |
| 302 |
protected function get_name_map() { |
| 303 |
if ( ! \method_exists( $this, 'get_name' ) || ! $this->get_name() ) { |
| 304 |
return null; |
| 305 |
} |
| 306 |
|
| 307 |
return array( |
| 308 |
$this->get_locale() => $this->get_name(), |
| 309 |
); |
| 310 |
} |
| 311 |
|
| 312 |
/** |
| 313 |
* Returns the summary map for the post. |
| 314 |
* |
| 315 |
* @return array|null The summary map for the post or null if not set. |
| 316 |
*/ |
| 317 |
protected function get_summary_map() { |
| 318 |
if ( ! \method_exists( $this, 'get_summary' ) || ! $this->get_summary() ) { |
| 319 |
return null; |
| 320 |
} |
| 321 |
|
| 322 |
return array( |
| 323 |
$this->get_locale() => $this->get_summary(), |
| 324 |
); |
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* Returns the tags for the post. |
| 329 |
* |
| 330 |
* @return array The tags for the post. |
| 331 |
*/ |
| 332 |
protected function get_tag() { |
| 333 |
$tags = array(); |
| 334 |
$mentions = $this->get_mentions(); |
| 335 |
|
| 336 |
foreach ( $mentions as $mention => $url ) { |
| 337 |
$tags[] = array( |
| 338 |
'type' => 'Mention', |
| 339 |
'href' => \esc_url( $url ), |
| 340 |
'name' => \esc_html( $mention ), |
| 341 |
); |
| 342 |
} |
| 343 |
|
| 344 |
return \array_unique( $tags, SORT_REGULAR ); |
| 345 |
} |
| 346 |
|
| 347 |
/** |
| 348 |
* Get the attributed to. |
| 349 |
* |
| 350 |
* @return string The attributed to. |
| 351 |
*/ |
| 352 |
protected function get_attributed_to() { |
| 353 |
return null; |
| 354 |
} |
| 355 |
|
| 356 |
/** |
| 357 |
* Extracts mentions from the content. |
| 358 |
* |
| 359 |
* @return array The mentions. |
| 360 |
*/ |
| 361 |
protected function get_mentions() { |
| 362 |
$content = ''; |
| 363 |
|
| 364 |
if ( method_exists( $this, 'get_content' ) ) { |
| 365 |
$content = $content . ' ' . $this->get_content(); |
| 366 |
} |
| 367 |
|
| 368 |
if ( method_exists( $this, 'get_summary' ) ) { |
| 369 |
$content = $content . ' ' . $this->get_summary(); |
| 370 |
} |
| 371 |
|
| 372 |
/** |
| 373 |
* Filter the mentions in the post content. |
| 374 |
* |
| 375 |
* @param array $mentions The mentions. |
| 376 |
* @param string $content The post content. |
| 377 |
* @param WP_Post $post The post object. |
| 378 |
* |
| 379 |
* @return array The filtered mentions. |
| 380 |
*/ |
| 381 |
return apply_filters( |
| 382 |
'activitypub_extract_mentions', |
| 383 |
array(), |
| 384 |
$content, |
| 385 |
$this->item |
| 386 |
); |
| 387 |
} |
| 388 |
} |
| 389 |
|