| 1 |
<?php |
| 2 |
/** |
| 3 |
* Attachment Transformer Class file. |
| 4 |
* |
| 5 |
* @package Activitypub |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Activitypub\Transformer; |
| 9 |
|
| 10 |
/** |
| 11 |
* WordPress Attachment Transformer. |
| 12 |
* |
| 13 |
* The Attachment Transformer is responsible for transforming a WP_Post object into different other |
| 14 |
* Object-Types. |
| 15 |
* |
| 16 |
* Currently supported are: |
| 17 |
* |
| 18 |
* - Activitypub\Activity\Base_Object |
| 19 |
* |
| 20 |
* Redaction is inherited from {@see Post::is_redacted()}: `is_post_publicly_queryable()` |
| 21 |
* already resolves an attachment's own visibility, password, post-type support, and — for |
| 22 |
* attached media — its parent's visibility, so no attachment-specific override is needed. |
| 23 |
*/ |
| 24 |
class Attachment extends Post { |
| 25 |
/** |
| 26 |
* Generates all Media Attachments for a Post. |
| 27 |
* |
| 28 |
* @return array The Attachments. |
| 29 |
*/ |
| 30 |
protected function get_attachment() { |
| 31 |
$mime_type = \get_post_mime_type( $this->item->ID ); |
| 32 |
$mime_type_parts = \explode( '/', $mime_type ); |
| 33 |
$type = ''; |
| 34 |
|
| 35 |
switch ( $mime_type_parts[0] ) { |
| 36 |
case 'audio': |
| 37 |
$type = 'Audio'; |
| 38 |
break; |
| 39 |
case 'video': |
| 40 |
$type = 'Video'; |
| 41 |
break; |
| 42 |
case 'image': |
| 43 |
$type = 'Image'; |
| 44 |
break; |
| 45 |
} |
| 46 |
|
| 47 |
$attachment = array( |
| 48 |
'type' => $type, |
| 49 |
'url' => \wp_get_attachment_url( $this->item->ID ), |
| 50 |
'mediaType' => $mime_type, |
| 51 |
); |
| 52 |
|
| 53 |
$alt = \get_post_meta( $this->item->ID, '_wp_attachment_image_alt', true ); |
| 54 |
if ( $alt ) { |
| 55 |
// `name` is plain text in the JSON. |
| 56 |
$attachment['name'] = \wp_strip_all_tags( \html_entity_decode( $alt, ENT_QUOTES, 'UTF-8' ) ); |
| 57 |
} |
| 58 |
|
| 59 |
return $attachment; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Returns the ActivityStreams 2.0 Object-Type for a Post based on the |
| 64 |
* settings and the Post-Type. |
| 65 |
* |
| 66 |
* @see https://www.w3.org/TR/activitystreams-vocabulary/#activity-types |
| 67 |
* |
| 68 |
* @return string The Object-Type. |
| 69 |
*/ |
| 70 |
protected function get_type() { |
| 71 |
return 'Note'; |
| 72 |
} |
| 73 |
} |
| 74 |
|