2018-09-30 22:51:22 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ActivityPub Post Class
|
|
|
|
*
|
|
|
|
* @author Matthias Pfefferle
|
|
|
|
*/
|
|
|
|
class Activitypub_Post {
|
|
|
|
private $post;
|
|
|
|
|
|
|
|
public function __construct( $post = null ) {
|
2018-12-20 11:33:08 +01:00
|
|
|
$this->post = get_post( $post );
|
2018-09-30 22:51:22 +02:00
|
|
|
}
|
|
|
|
|
2018-12-20 11:33:08 +01:00
|
|
|
public function get_post() {
|
|
|
|
return $this->post;
|
|
|
|
}
|
2018-09-30 22:51:22 +02:00
|
|
|
|
2018-12-20 11:33:08 +01:00
|
|
|
public function get_post_author() {
|
|
|
|
return $this->post->post_author;
|
|
|
|
}
|
2018-09-30 22:51:22 +02:00
|
|
|
|
2018-12-20 11:33:08 +01:00
|
|
|
public function to_array() {
|
|
|
|
$post = $this->post;
|
2018-09-30 22:51:22 +02:00
|
|
|
|
2018-12-20 11:33:08 +01:00
|
|
|
$array = array(
|
|
|
|
'id' => get_permalink( $post ),
|
2018-09-30 22:51:22 +02:00
|
|
|
'type' => $this->get_object_type(),
|
|
|
|
'published' => date( 'Y-m-d\TH:i:s\Z', strtotime( $post->post_date ) ),
|
|
|
|
'attributedTo' => get_author_posts_url( $post->post_author ),
|
2019-01-16 21:50:45 +01:00
|
|
|
'summary' => ( $this->get_object_type() == 'Article' ) ? $this->get_the_post_excerpt( 400, false ) : null,
|
2018-09-30 22:51:22 +02:00
|
|
|
'inReplyTo' => null,
|
2019-01-04 19:57:33 +01:00
|
|
|
'content' => $this->get_the_content(),
|
2018-09-30 22:51:22 +02:00
|
|
|
'contentMap' => array(
|
2019-01-04 19:57:33 +01:00
|
|
|
strstr( get_locale(), '_', true ) => $this->get_the_content(),
|
2018-09-30 22:51:22 +02:00
|
|
|
),
|
2018-12-20 11:33:08 +01:00
|
|
|
'to' => array( 'https://www.w3.org/ns/activitystreams#Public' ),
|
|
|
|
'cc' => array( 'https://www.w3.org/ns/activitystreams#Public' ),
|
|
|
|
'attachment' => $this->get_attachments(),
|
|
|
|
'tag' => $this->get_tags(),
|
2018-09-30 22:51:22 +02:00
|
|
|
);
|
|
|
|
|
2018-12-20 11:33:08 +01:00
|
|
|
return apply_filters( 'activitypub_post', $array );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function to_json() {
|
|
|
|
return wp_json_encode( $this->to_array() );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_attachments() {
|
|
|
|
$max_images = apply_filters( 'activitypub_max_images', 3 );
|
|
|
|
|
|
|
|
$images = array();
|
|
|
|
|
|
|
|
// max images can't be negative or zero
|
|
|
|
if ( $max_images <= 0 ) {
|
|
|
|
$max_images = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
$id = $this->post->ID;
|
|
|
|
|
|
|
|
$image_ids = array();
|
|
|
|
// list post thumbnail first if this post has one
|
|
|
|
if ( function_exists( 'has_post_thumbnail' ) && has_post_thumbnail( $id ) ) {
|
|
|
|
$image_ids[] = get_post_thumbnail_id( $id );
|
|
|
|
$max_images--;
|
|
|
|
}
|
|
|
|
// then list any image attachments
|
|
|
|
$query = new WP_Query(
|
|
|
|
array(
|
|
|
|
'post_parent' => $id,
|
|
|
|
'post_status' => 'inherit',
|
|
|
|
'post_type' => 'attachment',
|
|
|
|
'post_mime_type' => 'image',
|
|
|
|
'order' => 'ASC',
|
|
|
|
'orderby' => 'menu_order ID',
|
|
|
|
'posts_per_page' => $max_images,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
foreach ( $query->get_posts() as $attachment ) {
|
|
|
|
if ( ! in_array( $attachment->ID, $image_ids ) ) {
|
|
|
|
$image_ids[] = $attachment->ID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// get URLs for each image
|
|
|
|
foreach ( $image_ids as $id ) {
|
|
|
|
$thumbnail = wp_get_attachment_image_src( $id, 'full' );
|
|
|
|
$mimetype = get_post_mime_type( $id );
|
|
|
|
|
|
|
|
if ( $thumbnail ) {
|
|
|
|
$images[] = array(
|
|
|
|
'url' => $thumbnail[0],
|
|
|
|
'type' => $mimetype
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$attachments = array();
|
|
|
|
|
|
|
|
// add attachments
|
|
|
|
if ( $images ) {
|
|
|
|
foreach ( $images as $image ) {
|
|
|
|
$attachment = array(
|
|
|
|
"type" => "Image",
|
|
|
|
"url" => $image['url'],
|
|
|
|
"mediaType" => $image['type'],
|
|
|
|
);
|
|
|
|
$attachments[] = $attachment;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $attachments;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_tags() {
|
|
|
|
$tags = array();
|
|
|
|
|
|
|
|
$post_tags = get_the_tags( $this->post->ID );
|
|
|
|
if ( $post_tags ) {
|
|
|
|
foreach( $post_tags as $post_tag ) {
|
|
|
|
$tag = array(
|
|
|
|
"type" => "Hashtag",
|
|
|
|
"href" => get_tag_link( $post_tag->term_id ),
|
2018-12-28 22:40:57 +01:00
|
|
|
"name" => '#' . $post_tag->slug,
|
2018-12-20 11:33:08 +01:00
|
|
|
);
|
|
|
|
$tags[] = $tag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $tags;
|
2018-09-30 22:51:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the as2 object-type for a given post
|
|
|
|
*
|
|
|
|
* @param string $type the object-type
|
|
|
|
* @param Object $post the post-object
|
|
|
|
*
|
|
|
|
* @return string the object-type
|
|
|
|
*/
|
|
|
|
public function get_object_type() {
|
2019-01-04 19:57:33 +01:00
|
|
|
if ( 'wordpress-post-format' !== get_option( 'activitypub_object_type', 'note' ) ) {
|
2018-12-28 22:40:57 +01:00
|
|
|
return ucfirst( get_option( 'activitypub_object_type', 'note' ) );
|
|
|
|
}
|
|
|
|
|
2018-09-30 22:51:22 +02:00
|
|
|
$post_type = get_post_type( $this->post );
|
|
|
|
switch ( $post_type ) {
|
|
|
|
case 'post':
|
|
|
|
$post_format = get_post_format( $this->post );
|
|
|
|
switch ( $post_format ) {
|
|
|
|
case 'aside':
|
|
|
|
case 'status':
|
|
|
|
case 'quote':
|
|
|
|
case 'note':
|
|
|
|
$object_type = 'Note';
|
|
|
|
break;
|
|
|
|
case 'gallery':
|
|
|
|
case 'image':
|
|
|
|
$object_type = 'Image';
|
|
|
|
break;
|
|
|
|
case 'video':
|
|
|
|
$object_type = 'Video';
|
|
|
|
break;
|
|
|
|
case 'audio':
|
|
|
|
$object_type = 'Audio';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$object_type = 'Article';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'page':
|
|
|
|
$object_type = 'Page';
|
|
|
|
break;
|
|
|
|
case 'attachment':
|
|
|
|
$mime_type = get_post_mime_type();
|
|
|
|
$media_type = preg_replace( '/(\/[a-zA-Z]+)/i', '', $mime_type );
|
|
|
|
switch ( $media_type ) {
|
|
|
|
case 'audio':
|
|
|
|
$object_type = 'Audio';
|
|
|
|
break;
|
|
|
|
case 'video':
|
|
|
|
$object_type = 'Video';
|
|
|
|
break;
|
|
|
|
case 'image':
|
|
|
|
$object_type = 'Image';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$object_type = 'Article';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $object_type;
|
|
|
|
}
|
2019-01-04 19:57:33 +01:00
|
|
|
|
|
|
|
public function get_the_content() {
|
|
|
|
if ( 'excerpt' === get_option( 'activitypub_post_content_type', 'excerpt' ) ) {
|
|
|
|
return $this->get_the_post_excerpt();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->get_the_post_content();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the excerpt for a post for use outside of the loop.
|
|
|
|
*
|
|
|
|
* @param int Optional excerpt length.
|
|
|
|
*
|
|
|
|
* @return string The excerpt.
|
|
|
|
*/
|
2019-01-16 21:50:45 +01:00
|
|
|
public function get_the_post_excerpt( $excerpt_length = 400, $with_link = true ) {
|
2019-01-04 19:57:33 +01:00
|
|
|
$post = $this->post;
|
|
|
|
|
|
|
|
$excerpt = get_post_field( 'post_excerpt', $post );
|
|
|
|
|
|
|
|
if ( '' === $excerpt ) {
|
|
|
|
|
|
|
|
$content = get_post_field( 'post_content', $post );
|
|
|
|
|
|
|
|
// An empty string will make wp_trim_excerpt do stuff we do not want.
|
|
|
|
if ( '' !== $content ) {
|
|
|
|
|
|
|
|
$excerpt = strip_shortcodes( $content );
|
|
|
|
|
|
|
|
/** This filter is documented in wp-includes/post-template.php */
|
|
|
|
$excerpt = apply_filters( 'the_content', $excerpt );
|
|
|
|
$excerpt = str_replace( ']]>', ']]>', $excerpt );
|
|
|
|
|
|
|
|
$excerpt_length = apply_filters( 'excerpt_length', $excerpt_length );
|
|
|
|
|
|
|
|
/** This filter is documented in wp-includes/formatting.php */
|
|
|
|
$excerpt_more = apply_filters( 'excerpt_more', ' [...]' );
|
|
|
|
|
|
|
|
$excerpt = wp_trim_words( $excerpt, $excerpt_length, $excerpt_more );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$filtered_excerpt = apply_filters( 'the_excerpt', $excerpt );
|
|
|
|
|
2019-01-16 21:50:45 +01:00
|
|
|
if ( $with_link ) {
|
|
|
|
$link = '';
|
|
|
|
|
|
|
|
if ( get_option( 'activitypub_use_shortlink', 0 ) ) {
|
|
|
|
$link = esc_url( wp_get_shortlink( $this->post->ID ) );
|
|
|
|
} else {
|
|
|
|
$link = esc_url( get_permalink( $this->post->ID ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
$filtered_excerpt = $filtered_excerpt . "\n\n" . '<a href="' . $link . '">' . $link . '</a>';
|
|
|
|
}
|
2019-01-04 19:57:33 +01:00
|
|
|
|
|
|
|
$allowed_html = apply_filters( 'activitypub_allowed_html', '<a>' );
|
|
|
|
|
2019-01-16 21:50:45 +01:00
|
|
|
return trim( preg_replace( '/[\r\n]{2,}/', "\n\n", strip_tags( $filtered_excerpt, $allowed_html ) ) );
|
2019-01-04 19:57:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the content for a post for use outside of the loop.
|
|
|
|
*
|
|
|
|
* @return string The content.
|
|
|
|
*/
|
2019-01-16 21:50:45 +01:00
|
|
|
public function get_the_post_content( $with_link = true ) {
|
2019-01-04 19:57:33 +01:00
|
|
|
$post = $this->post;
|
|
|
|
|
|
|
|
$content = get_post_field( 'post_content', $post );
|
|
|
|
|
|
|
|
$filtered_content = apply_filters( 'the_content', $content );
|
|
|
|
|
2019-01-16 21:50:45 +01:00
|
|
|
if ( $with_link ) {
|
|
|
|
$link = '';
|
|
|
|
|
|
|
|
if ( get_option( 'activitypub_use_shortlink', 0 ) ) {
|
|
|
|
$link = esc_url( wp_get_shortlink( $this->post->ID ) );
|
|
|
|
} else {
|
|
|
|
$link = esc_url( get_permalink( $this->post->ID ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
$filtered_content = $filtered_content . "\n\n" . '<a href="' . $link . '">' . $link . '</a>';
|
|
|
|
}
|
|
|
|
|
2019-01-04 19:57:33 +01:00
|
|
|
$allowed_html = apply_filters( 'activitypub_allowed_html', '<a>' );
|
|
|
|
|
|
|
|
return trim( preg_replace( '/[\r\n]{2,}/', "\n\n", strip_tags( $filtered_content, $allowed_html ) ) );
|
|
|
|
}
|
2018-09-30 22:51:22 +02:00
|
|
|
}
|