| 1 |
<?php |
| 2 |
namespace Activitypub; |
| 3 |
|
| 4 |
/** |
| 5 |
* ActivityPub Post Class |
| 6 |
* |
| 7 |
* @author Matthias Pfefferle |
| 8 |
*/ |
| 9 |
class Post { |
| 10 |
private $post; |
| 11 |
|
| 12 |
/** |
| 13 |
* Initialize the class, registering WordPress hooks |
| 14 |
*/ |
| 15 |
public static function init() { |
| 16 |
add_filter( 'activitypub_the_summary', array( '\Activitypub\Post', 'add_backlink' ), 10, 2 ); |
| 17 |
add_filter( 'activitypub_the_content', array( '\Activitypub\Post', 'add_backlink' ), 10, 2 ); |
| 18 |
} |
| 19 |
|
| 20 |
public function __construct( $post = null ) { |
| 21 |
$this->post = get_post( $post ); |
| 22 |
} |
| 23 |
|
| 24 |
public function get_post() { |
| 25 |
return $this->post; |
| 26 |
} |
| 27 |
|
| 28 |
public function get_post_author() { |
| 29 |
return $this->post->post_author; |
| 30 |
} |
| 31 |
|
| 32 |
public function to_array() { |
| 33 |
$post = $this->post; |
| 34 |
|
| 35 |
$array = array( |
| 36 |
'id' => get_permalink( $post ), |
| 37 |
'type' => $this->get_object_type(), |
| 38 |
'published' => date( 'Y-m-d\TH:i:s\Z', strtotime( $post->post_date ) ), |
| 39 |
'attributedTo' => get_author_posts_url( $post->post_author ), |
| 40 |
'summary' => $this->get_the_summary(), |
| 41 |
'inReplyTo' => null, |
| 42 |
'content' => $this->get_the_content(), |
| 43 |
'contentMap' => array( |
| 44 |
strstr( get_locale(), '_', true ) => $this->get_the_content(), |
| 45 |
), |
| 46 |
'to' => array( 'https://www.w3.org/ns/activitystreams#Public' ), |
| 47 |
'cc' => array( 'https://www.w3.org/ns/activitystreams#Public' ), |
| 48 |
'attachment' => $this->get_attachments(), |
| 49 |
'tag' => $this->get_tags(), |
| 50 |
); |
| 51 |
|
| 52 |
return apply_filters( 'activitypub_post', $array ); |
| 53 |
} |
| 54 |
|
| 55 |
public function to_json() { |
| 56 |
return wp_json_encode( $this->to_array(), JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_QUOT ); |
| 57 |
} |
| 58 |
|
| 59 |
public function get_attachments() { |
| 60 |
$max_images = apply_filters( 'activitypub_max_images', 3 ); |
| 61 |
|
| 62 |
$images = array(); |
| 63 |
|
| 64 |
// max images can't be negative or zero |
| 65 |
if ( $max_images <= 0 ) { |
| 66 |
$max_images = 1; |
| 67 |
} |
| 68 |
|
| 69 |
$id = $this->post->ID; |
| 70 |
|
| 71 |
$image_ids = array(); |
| 72 |
// list post thumbnail first if this post has one |
| 73 |
if ( function_exists( 'has_post_thumbnail' ) && has_post_thumbnail( $id ) ) { |
| 74 |
$image_ids[] = get_post_thumbnail_id( $id ); |
| 75 |
$max_images--; |
| 76 |
} |
| 77 |
// then list any image attachments |
| 78 |
$query = new \WP_Query( |
| 79 |
array( |
| 80 |
'post_parent' => $id, |
| 81 |
'post_status' => 'inherit', |
| 82 |
'post_type' => 'attachment', |
| 83 |
'post_mime_type' => 'image', |
| 84 |
'order' => 'ASC', |
| 85 |
'orderby' => 'menu_order ID', |
| 86 |
'posts_per_page' => $max_images, |
| 87 |
) |
| 88 |
); |
| 89 |
foreach ( $query->get_posts() as $attachment ) { |
| 90 |
if ( ! in_array( $attachment->ID, $image_ids, true ) ) { |
| 91 |
$image_ids[] = $attachment->ID; |
| 92 |
} |
| 93 |
} |
| 94 |
// get URLs for each image |
| 95 |
foreach ( $image_ids as $id ) { |
| 96 |
$thumbnail = wp_get_attachment_image_src( $id, 'full' ); |
| 97 |
$mimetype = get_post_mime_type( $id ); |
| 98 |
|
| 99 |
if ( $thumbnail ) { |
| 100 |
$images[] = array( |
| 101 |
'url' => $thumbnail[0], |
| 102 |
'type' => $mimetype, |
| 103 |
); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
$attachments = array(); |
| 108 |
|
| 109 |
// add attachments |
| 110 |
if ( $images ) { |
| 111 |
foreach ( $images as $image ) { |
| 112 |
$attachment = array( |
| 113 |
'type' => 'Image', |
| 114 |
'url' => $image['url'], |
| 115 |
'mediaType' => $image['type'], |
| 116 |
); |
| 117 |
$attachments[] = $attachment; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
return $attachments; |
| 122 |
} |
| 123 |
|
| 124 |
public function get_tags() { |
| 125 |
$tags = array(); |
| 126 |
|
| 127 |
$post_tags = get_the_tags( $this->post->ID ); |
| 128 |
if ( $post_tags ) { |
| 129 |
foreach( $post_tags as $post_tag ) { |
| 130 |
$tag = array( |
| 131 |
'type' => 'Hashtag', |
| 132 |
'href' => get_tag_link( $post_tag->term_id ), |
| 133 |
'name' => '#' . $post_tag->slug, |
| 134 |
); |
| 135 |
$tags[] = $tag; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
return $tags; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Returns the as2 object-type for a given post |
| 144 |
* |
| 145 |
* @param string $type the object-type |
| 146 |
* @param Object $post the post-object |
| 147 |
* |
| 148 |
* @return string the object-type |
| 149 |
*/ |
| 150 |
public function get_object_type() { |
| 151 |
if ( 'wordpress-post-format' !== get_option( 'activitypub_object_type', 'note' ) ) { |
| 152 |
return ucfirst( get_option( 'activitypub_object_type', 'note' ) ); |
| 153 |
} |
| 154 |
|
| 155 |
$post_type = get_post_type( $this->post ); |
| 156 |
switch ( $post_type ) { |
| 157 |
case 'post': |
| 158 |
$post_format = get_post_format( $this->post ); |
| 159 |
switch ( $post_format ) { |
| 160 |
case 'aside': |
| 161 |
case 'status': |
| 162 |
case 'quote': |
| 163 |
case 'note': |
| 164 |
$object_type = 'Note'; |
| 165 |
break; |
| 166 |
case 'gallery': |
| 167 |
case 'image': |
| 168 |
$object_type = 'Image'; |
| 169 |
break; |
| 170 |
case 'video': |
| 171 |
$object_type = 'Video'; |
| 172 |
break; |
| 173 |
case 'audio': |
| 174 |
$object_type = 'Audio'; |
| 175 |
break; |
| 176 |
default: |
| 177 |
$object_type = 'Article'; |
| 178 |
break; |
| 179 |
} |
| 180 |
break; |
| 181 |
case 'page': |
| 182 |
$object_type = 'Page'; |
| 183 |
break; |
| 184 |
case 'attachment': |
| 185 |
$mime_type = get_post_mime_type(); |
| 186 |
$media_type = preg_replace( '/(\/[a-zA-Z]+)/i', '', $mime_type ); |
| 187 |
switch ( $media_type ) { |
| 188 |
case 'audio': |
| 189 |
$object_type = 'Audio'; |
| 190 |
break; |
| 191 |
case 'video': |
| 192 |
$object_type = 'Video'; |
| 193 |
break; |
| 194 |
case 'image': |
| 195 |
$object_type = 'Image'; |
| 196 |
break; |
| 197 |
} |
| 198 |
break; |
| 199 |
default: |
| 200 |
$object_type = 'Article'; |
| 201 |
break; |
| 202 |
} |
| 203 |
|
| 204 |
return $object_type; |
| 205 |
} |
| 206 |
|
| 207 |
public function get_the_content() { |
| 208 |
if ( 'excerpt' === get_option( 'activitypub_post_content_type', 'content' ) ) { |
| 209 |
return $this->get_the_post_summary(); |
| 210 |
} |
| 211 |
|
| 212 |
return $this->get_the_post_content(); |
| 213 |
} |
| 214 |
|
| 215 |
public function get_the_summary() { |
| 216 |
if ( 'Article' === $this->get_object_type() ) { |
| 217 |
$excerpt = $this->get_the_post_excerpt( 400 ); |
| 218 |
|
| 219 |
return html_entity_decode( $excerpt, ENT_QUOTES, 'UTF-8' ); |
| 220 |
} |
| 221 |
|
| 222 |
return null; |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Get the excerpt for a post for use outside of the loop. |
| 227 |
* |
| 228 |
* @param int Optional excerpt length. |
| 229 |
* |
| 230 |
* @return string The excerpt. |
| 231 |
*/ |
| 232 |
public function get_the_post_excerpt( $excerpt_length = 400 ) { |
| 233 |
$post = $this->post; |
| 234 |
|
| 235 |
$excerpt = get_post_field( 'post_excerpt', $post ); |
| 236 |
|
| 237 |
if ( '' === $excerpt ) { |
| 238 |
|
| 239 |
$content = get_post_field( 'post_content', $post ); |
| 240 |
|
| 241 |
// An empty string will make wp_trim_excerpt do stuff we do not want. |
| 242 |
if ( '' !== $content ) { |
| 243 |
|
| 244 |
$excerpt = strip_shortcodes( $content ); |
| 245 |
|
| 246 |
/** This filter is documented in wp-includes/post-template.php */ |
| 247 |
$excerpt = apply_filters( 'the_content', $excerpt ); |
| 248 |
$excerpt = str_replace( ']]>', ']]>', $excerpt ); |
| 249 |
|
| 250 |
$excerpt_length = apply_filters( 'excerpt_length', $excerpt_length ); |
| 251 |
|
| 252 |
/** This filter is documented in wp-includes/formatting.php */ |
| 253 |
$excerpt_more = apply_filters( 'excerpt_more', ' [...]' ); |
| 254 |
|
| 255 |
$excerpt = wp_trim_words( $excerpt, $excerpt_length, $excerpt_more ); |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
return $excerpt; |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* Get the content for a post for use outside of the loop. |
| 264 |
* |
| 265 |
* @return string The content. |
| 266 |
*/ |
| 267 |
public function get_the_post_content( $with_link = true ) { |
| 268 |
$post = $this->post; |
| 269 |
|
| 270 |
$content = get_post_field( 'post_content', $post ); |
| 271 |
|
| 272 |
$filtered_content = apply_filters( 'the_content', $content ); |
| 273 |
$filtered_content = apply_filters( 'activitypub_the_content', $filtered_content, $this->post ); |
| 274 |
|
| 275 |
$decoded_content = html_entity_decode( $filtered_content, ENT_QUOTES, 'UTF-8' ); |
| 276 |
|
| 277 |
$allowed_html = apply_filters( 'activitypub_allowed_html', '<a><p>' ); |
| 278 |
|
| 279 |
return trim( preg_replace( '/[\r\n]{2,}/', '', strip_tags( $decoded_content, $allowed_html ) ) ); |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* Get the excerpt for a post for use outside of the loop. |
| 284 |
* |
| 285 |
* @param int Optional excerpt length. |
| 286 |
* |
| 287 |
* @return string The excerpt. |
| 288 |
*/ |
| 289 |
public function get_the_post_summary( $summary_length = 400 ) { |
| 290 |
$summary = $this->get_the_post_excerpt( $summary_length ); |
| 291 |
|
| 292 |
$filtered_summary = apply_filters( 'the_excerpt', $summary ); |
| 293 |
$filtered_summary = apply_filters( 'activitypub_the_summary', $filtered_summary, $this->post ); |
| 294 |
|
| 295 |
$decoded_summary = html_entity_decode( $filtered_summary, ENT_QUOTES, 'UTF-8' ); |
| 296 |
|
| 297 |
$allowed_html = apply_filters( 'activitypub_allowed_html', '<a><p>' ); |
| 298 |
|
| 299 |
return trim( preg_replace( '/[\r\n]{2,}/', '', strip_tags( $decoded_summary, $allowed_html ) ) ); |
| 300 |
} |
| 301 |
|
| 302 |
public static function add_backlink( $content, $post ) { |
| 303 |
$link = ''; |
| 304 |
|
| 305 |
if ( get_option( 'activitypub_use_shortlink', 0 ) ) { |
| 306 |
$link = esc_url( wp_get_shortlink( $post->ID ) ); |
| 307 |
} else { |
| 308 |
$link = esc_url( get_permalink( $post->ID ) ); |
| 309 |
} |
| 310 |
|
| 311 |
return $content . '<p><a href="' . $link . '">' . $link . '</a></p>'; |
| 312 |
} |
| 313 |
} |
| 314 |
|