2018-09-30 22:51:22 +02:00
|
|
|
<?php
|
2019-11-18 20:57:00 +01:00
|
|
|
namespace Activitypub\Model;
|
2019-02-24 12:07:41 +01:00
|
|
|
|
2018-09-30 22:51:22 +02:00
|
|
|
/**
|
|
|
|
* ActivityPub Post Class
|
|
|
|
*
|
|
|
|
* @author Matthias Pfefferle
|
|
|
|
*/
|
2019-02-24 12:07:41 +01:00
|
|
|
class Post {
|
2018-09-30 22:51:22 +02:00
|
|
|
private $post;
|
2020-05-14 21:37:59 +02:00
|
|
|
private $post_author;
|
2020-07-21 09:23:35 +02:00
|
|
|
private $id;
|
2020-05-14 21:37:59 +02:00
|
|
|
private $summary;
|
|
|
|
private $content;
|
|
|
|
private $attachments;
|
2020-05-14 22:33:09 +02:00
|
|
|
private $tags;
|
2020-05-14 21:37:59 +02:00
|
|
|
private $object_type;
|
2018-09-30 22:51:22 +02:00
|
|
|
|
|
|
|
public function __construct( $post = null ) {
|
2023-01-16 16:26:38 +01:00
|
|
|
if( $post ) {
|
|
|
|
$this->post = \get_post( $post );
|
|
|
|
|
|
|
|
$this->post_author = $this->post->post_author;
|
|
|
|
$this->id = $this->generate_id();
|
|
|
|
$this->summary = $this->generate_the_title();
|
|
|
|
$this->content = $this->generate_the_content();
|
|
|
|
$this->attachments = $this->generate_attachments();
|
|
|
|
$this->tags = $this->generate_tags();
|
|
|
|
$this->object_type = $this->generate_object_type();
|
|
|
|
}
|
2023-01-13 21:47:13 +01:00
|
|
|
|
2018-12-20 11:33:08 +01:00
|
|
|
}
|
2018-09-30 22:51:22 +02:00
|
|
|
|
2020-05-14 21:37:59 +02:00
|
|
|
public function __call( $method, $params ) {
|
|
|
|
$var = \strtolower( \substr( $method, 4 ) );
|
|
|
|
|
|
|
|
if ( \strncasecmp( $method, 'get', 3 ) === 0 ) {
|
|
|
|
return $this->$var;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( \strncasecmp( $method, 'set', 3 ) === 0 ) {
|
|
|
|
$this->$var = $params[0];
|
|
|
|
}
|
2018-12-20 11:33:08 +01:00
|
|
|
}
|
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(
|
2020-07-21 09:23:35 +02:00
|
|
|
'id' => $this->id,
|
2020-05-14 21:37:59 +02:00
|
|
|
'type' => $this->object_type,
|
2022-01-27 13:09:11 +01:00
|
|
|
'published' => \gmdate( 'Y-m-d\TH:i:s\Z', \strtotime( $post->post_date_gmt ) ),
|
2019-09-27 10:12:59 +02:00
|
|
|
'attributedTo' => \get_author_posts_url( $post->post_author ),
|
2020-05-14 21:37:59 +02:00
|
|
|
'summary' => $this->summary,
|
2018-09-30 22:51:22 +02:00
|
|
|
'inReplyTo' => null,
|
2020-05-14 21:37:59 +02:00
|
|
|
'content' => $this->content,
|
2018-09-30 22:51:22 +02:00
|
|
|
'contentMap' => array(
|
2020-05-14 21:37:59 +02:00
|
|
|
\strstr( \get_locale(), '_', true ) => $this->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' ),
|
2020-05-14 21:37:59 +02:00
|
|
|
'attachment' => $this->attachments,
|
|
|
|
'tag' => $this->tags,
|
2018-09-30 22:51:22 +02:00
|
|
|
);
|
|
|
|
|
2019-09-27 10:12:59 +02:00
|
|
|
return \apply_filters( 'activitypub_post', $array );
|
2018-12-20 11:33:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function to_json() {
|
2020-05-12 19:42:09 +02:00
|
|
|
return \wp_json_encode( $this->to_array(), \JSON_HEX_TAG | \JSON_HEX_AMP | \JSON_HEX_QUOT );
|
2018-12-20 11:33:08 +01:00
|
|
|
}
|
|
|
|
|
2020-07-21 09:23:35 +02:00
|
|
|
public function generate_id() {
|
2022-12-27 15:48:14 +01:00
|
|
|
$post = $this->post;
|
|
|
|
|
|
|
|
if ( 'trash' === get_post_status( $post ) ) {
|
2022-12-27 16:58:49 +01:00
|
|
|
$permalink = \get_post_meta( $post->ID, 'activitypub_canonical_url', true );
|
2022-12-27 15:48:14 +01:00
|
|
|
} else {
|
|
|
|
$permalink = \get_permalink( $post );
|
|
|
|
}
|
2020-05-14 18:02:49 +02:00
|
|
|
|
2022-12-27 15:48:14 +01:00
|
|
|
return $permalink;
|
2020-05-14 18:02:49 +02:00
|
|
|
}
|
|
|
|
|
2020-05-14 21:37:59 +02:00
|
|
|
public function generate_attachments() {
|
2023-01-12 21:29:21 +01:00
|
|
|
$max_images = intval( \apply_filters( 'activitypub_max_image_attachments', \get_option( 'activitypub_max_image_attachments', ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS ) ) );
|
2018-12-20 11:33:08 +01:00
|
|
|
|
|
|
|
$images = array();
|
|
|
|
|
|
|
|
// max images can't be negative or zero
|
|
|
|
if ( $max_images <= 0 ) {
|
2023-01-12 21:55:33 +01:00
|
|
|
return $images;
|
2018-12-20 11:33:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$id = $this->post->ID;
|
|
|
|
|
|
|
|
$image_ids = array();
|
2023-01-12 22:21:48 +01:00
|
|
|
|
2018-12-20 11:33:08 +01:00
|
|
|
// list post thumbnail first if this post has one
|
2019-09-27 10:12:59 +02:00
|
|
|
if ( \function_exists( 'has_post_thumbnail' ) && \has_post_thumbnail( $id ) ) {
|
|
|
|
$image_ids[] = \get_post_thumbnail_id( $id );
|
2018-12-20 11:33:08 +01:00
|
|
|
$max_images--;
|
|
|
|
}
|
2023-01-12 22:21:48 +01:00
|
|
|
|
|
|
|
if ( $max_images > 0 ) {
|
|
|
|
// 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, true ) ) {
|
|
|
|
$image_ids[] = $attachment->ID;
|
|
|
|
}
|
2018-12-20 11:33:08 +01:00
|
|
|
}
|
|
|
|
}
|
2019-09-27 11:37:15 +02:00
|
|
|
|
|
|
|
$image_ids = \array_unique( $image_ids );
|
|
|
|
|
2018-12-20 11:33:08 +01:00
|
|
|
// get URLs for each image
|
|
|
|
foreach ( $image_ids as $id ) {
|
2020-01-18 19:36:46 +01:00
|
|
|
$alt = \get_post_meta( $id, '_wp_attachment_image_alt', true );
|
2019-09-27 10:12:59 +02:00
|
|
|
$thumbnail = \wp_get_attachment_image_src( $id, 'full' );
|
|
|
|
$mimetype = \get_post_mime_type( $id );
|
2018-12-20 11:33:08 +01:00
|
|
|
|
|
|
|
if ( $thumbnail ) {
|
2020-01-18 19:36:46 +01:00
|
|
|
$image = array(
|
2019-02-24 12:07:41 +01:00
|
|
|
'type' => 'Image',
|
2020-01-18 19:36:46 +01:00
|
|
|
'url' => $thumbnail[0],
|
2020-05-14 21:37:59 +02:00
|
|
|
'mediaType' => $mimetype,
|
2018-12-20 11:33:08 +01:00
|
|
|
);
|
2020-01-18 19:36:46 +01:00
|
|
|
if ( $alt ) {
|
|
|
|
$image['name'] = $alt;
|
|
|
|
}
|
|
|
|
$images[] = $image;
|
2018-12-20 11:33:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 19:36:46 +01:00
|
|
|
return $images;
|
2018-12-20 11:33:08 +01:00
|
|
|
}
|
|
|
|
|
2020-05-14 21:37:59 +02:00
|
|
|
public function generate_tags() {
|
2018-12-20 11:33:08 +01:00
|
|
|
$tags = array();
|
|
|
|
|
2019-09-27 10:12:59 +02:00
|
|
|
$post_tags = \get_the_tags( $this->post->ID );
|
2018-12-20 11:33:08 +01:00
|
|
|
if ( $post_tags ) {
|
2019-03-02 21:10:42 +01:00
|
|
|
foreach ( $post_tags as $post_tag ) {
|
2018-12-20 11:33:08 +01:00
|
|
|
$tag = array(
|
2019-02-24 12:07:41 +01:00
|
|
|
'type' => 'Hashtag',
|
2019-09-27 10:12:59 +02:00
|
|
|
'href' => \get_tag_link( $post_tag->term_id ),
|
2019-02-24 12:07:41 +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
|
|
|
|
*/
|
2020-05-14 21:37:59 +02:00
|
|
|
public function generate_object_type() {
|
2019-09-27 10:12:59 +02:00
|
|
|
if ( 'wordpress-post-format' !== \get_option( 'activitypub_object_type', 'note' ) ) {
|
|
|
|
return \ucfirst( \get_option( 'activitypub_object_type', 'note' ) );
|
2018-12-28 22:40:57 +01:00
|
|
|
}
|
|
|
|
|
2019-09-27 10:12:59 +02:00
|
|
|
$post_type = \get_post_type( $this->post );
|
2018-09-30 22:51:22 +02:00
|
|
|
switch ( $post_type ) {
|
|
|
|
case 'post':
|
2019-09-27 10:12:59 +02:00
|
|
|
$post_format = \get_post_format( $this->post );
|
2018-09-30 22:51:22 +02:00
|
|
|
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':
|
2019-09-27 10:12:59 +02:00
|
|
|
$mime_type = \get_post_mime_type();
|
|
|
|
$media_type = \preg_replace( '/(\/[a-zA-Z]+)/i', '', $mime_type );
|
2018-09-30 22:51:22 +02:00
|
|
|
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
|
|
|
|
2023-01-16 16:27:27 +01:00
|
|
|
/**
|
|
|
|
* Outputs the shortcode content.
|
|
|
|
*
|
|
|
|
* @param array $atts the attributes of the shortcode
|
|
|
|
* @param string $content the content between opening and closing shortcodes
|
|
|
|
* @param string $tag the name of the shortcode being processed
|
|
|
|
*
|
|
|
|
*/
|
2023-01-13 21:47:13 +01:00
|
|
|
public function shortcode_content( $atts, $content, $tag ) {
|
|
|
|
$tag = strtolower( $tag );
|
|
|
|
$post = $this->post;
|
|
|
|
|
2023-01-16 18:51:18 +01:00
|
|
|
$text = '';
|
|
|
|
|
2023-01-13 21:47:13 +01:00
|
|
|
switch( $tag ) {
|
|
|
|
case 'ap_title':
|
2023-01-16 18:51:18 +01:00
|
|
|
$text = \get_the_title( $post->ID );
|
2023-01-13 21:47:13 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
case 'ap_excerpt':
|
2023-01-13 22:11:52 +01:00
|
|
|
$length = ACTIVITYPUB_EXCERPT_LENGTH;
|
|
|
|
|
|
|
|
if( is_array( $atts ) && array_key_exists( 'length', $atts ) ) {
|
|
|
|
$length = intval( $atts['length'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( $length == 0 ) { $length = ACTIVITYPUB_EXCERPT_LENGTH; }
|
|
|
|
|
2023-01-16 18:51:18 +01:00
|
|
|
$text = $this->get_the_post_excerpt( $length );
|
2023-01-13 21:47:13 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
case 'ap_content':
|
2023-01-16 18:51:18 +01:00
|
|
|
$text = $this->get_the_post_content();
|
2023-01-13 21:47:13 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
case 'ap_permalink':
|
2023-01-16 18:51:18 +01:00
|
|
|
$text = $this->get_the_post_link( 'permalink' );
|
2023-01-13 21:47:13 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
case 'ap_shortlink':
|
2023-01-16 18:51:18 +01:00
|
|
|
$text = $this->get_the_post_link( 'shortlink' );
|
2023-01-13 21:47:13 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
case 'ap_hashtags':
|
2023-01-16 18:51:18 +01:00
|
|
|
$text = $this->get_the_post_hashtags();
|
2023-01-13 21:47:13 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
case 'ap_thumbnail':
|
2023-01-16 18:51:18 +01:00
|
|
|
$text = $this->get_the_post_image( 'thumbnail' );
|
2023-01-13 21:47:13 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
case 'ap_image':
|
2023-01-16 18:51:18 +01:00
|
|
|
$text = $this->get_the_post_image();
|
2023-01-13 21:47:13 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
case 'ap_hashcats':
|
2023-01-16 18:51:18 +01:00
|
|
|
$text = $this->get_the_post_categories();
|
2023-01-13 21:47:13 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
case 'ap_author':
|
2023-01-16 18:51:18 +01:00
|
|
|
$text = $this->get_the_post_author();
|
2023-01-13 21:47:13 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
case 'ap_authorurl':
|
2023-01-16 18:51:18 +01:00
|
|
|
$text = $this->get_the_post_author_url();
|
2023-01-13 21:47:13 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
case 'ap_blogurl':
|
2023-01-16 18:51:18 +01:00
|
|
|
$text = \get_bloginfo('url');
|
2023-01-13 21:47:13 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
case 'ap_blogname':
|
2023-01-16 18:51:18 +01:00
|
|
|
$text = \get_bloginfo('name');
|
2023-01-13 21:47:13 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
case 'ap_blogdesc':
|
2023-01-16 18:51:18 +01:00
|
|
|
$text = \get_bloginfo('description');
|
2023-01-13 21:47:13 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
case 'ap_date':
|
2023-01-16 18:51:18 +01:00
|
|
|
$text = $this->get_the_post_date( 'time' );
|
2023-01-13 21:47:13 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
case 'ap_time':
|
2023-01-16 18:51:18 +01:00
|
|
|
$text = $this->get_the_post_date( 'date' );
|
2023-01-13 21:47:13 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
case 'ap_datetime':
|
2023-01-16 18:51:18 +01:00
|
|
|
$text = $this->get_the_post_date( 'both' );
|
2023-01-13 21:47:13 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2023-01-16 18:51:18 +01:00
|
|
|
|
|
|
|
return $text;
|
2023-01-13 21:47:13 +01:00
|
|
|
}
|
|
|
|
|
2023-01-16 16:27:27 +01:00
|
|
|
/**
|
|
|
|
* Generates the content for the activitypub item.
|
|
|
|
*
|
|
|
|
* @return string the content
|
|
|
|
*/
|
2020-05-14 21:37:59 +02:00
|
|
|
public function generate_the_content() {
|
2020-07-21 09:23:35 +02:00
|
|
|
$post = $this->post;
|
|
|
|
$content = $this->get_post_content_template();
|
2019-01-04 19:57:33 +01:00
|
|
|
|
2023-01-16 18:51:18 +01:00
|
|
|
$shortcodes = array( 'ap_title', 'ap_excerpt', 'ap_content', 'ap_permalink', 'ap_shortlink', 'ap_hashtags', 'ap_thumbnail', 'ap_image', 'ap_hashcats', 'ap_author', 'ap_authorurl', 'ap_blogurl', 'ap_blogname', 'ap_blogdesc', 'ap_date', 'ap_time', 'ap_datetime' );
|
|
|
|
|
|
|
|
foreach( $shortcodes as $tag ) {
|
|
|
|
add_shortcode( $tag, [ $this, 'shortcode_content' ] );
|
|
|
|
}
|
|
|
|
|
2023-01-13 21:47:13 +01:00
|
|
|
// Fill in the shortcodes.
|
|
|
|
$content = do_shortcode( $content );
|
2020-07-21 09:23:35 +02:00
|
|
|
|
|
|
|
$content = \trim( \preg_replace( '/[\r\n]{2,}/', '', $content ) );
|
|
|
|
|
|
|
|
$filtered_content = \apply_filters( 'activitypub_the_content', $content, $this->post );
|
|
|
|
$decoded_content = \html_entity_decode( $filtered_content, \ENT_QUOTES, 'UTF-8' );
|
|
|
|
|
|
|
|
$allowed_html = \apply_filters( 'activitypub_allowed_html', \get_option( 'activitypub_allowed_html', ACTIVITYPUB_ALLOWED_HTML ) );
|
|
|
|
|
|
|
|
if ( $allowed_html ) {
|
|
|
|
return \strip_tags( $decoded_content, $allowed_html );
|
2020-01-22 15:57:01 +01:00
|
|
|
}
|
|
|
|
|
2020-07-21 09:23:35 +02:00
|
|
|
return $decoded_content;
|
2019-01-04 19:57:33 +01:00
|
|
|
}
|
|
|
|
|
2023-01-16 16:27:27 +01:00
|
|
|
/**
|
|
|
|
* Gets the template to use to generate the content of the activitypub item.
|
|
|
|
*
|
|
|
|
* @return string the template
|
|
|
|
*/
|
2020-07-21 09:23:35 +02:00
|
|
|
public function get_post_content_template() {
|
|
|
|
if ( 'excerpt' === \get_option( 'activitypub_post_content_type', 'content' ) ) {
|
2023-01-13 21:47:13 +01:00
|
|
|
return "[ap_excerpt]\n\n<p>[ap_permalink]</p>";
|
2020-07-21 09:23:35 +02:00
|
|
|
}
|
2019-02-20 20:25:19 +01:00
|
|
|
|
2020-07-21 09:23:35 +02:00
|
|
|
if ( 'title' === \get_option( 'activitypub_post_content_type', 'content' ) ) {
|
2023-01-13 21:47:13 +01:00
|
|
|
return "<p><strong>[ap_title]</strong></p>\n\n<p>[ap_permalink]</p>";
|
2019-02-02 23:56:05 +01:00
|
|
|
}
|
|
|
|
|
2020-07-21 09:23:35 +02:00
|
|
|
if ( 'content' === \get_option( 'activitypub_post_content_type', 'content' ) ) {
|
2023-01-13 21:47:13 +01:00
|
|
|
return "[ap_content]\n\n<p>[ap_hashtags]</p>\n\n<p>[ap_permalink]</p>";
|
|
|
|
}
|
|
|
|
|
2023-01-16 16:27:27 +01:00
|
|
|
// Upgrade from old template codes to shortcodes.
|
|
|
|
$content = $this->upgrade_post_content_template();
|
|
|
|
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the custom template to use shortcodes instead of the deprecated templates.
|
|
|
|
*
|
|
|
|
* @return string the updated template content
|
|
|
|
*/
|
|
|
|
public function upgrade_post_content_template() {
|
2023-01-13 21:47:13 +01:00
|
|
|
// Get the custom template.
|
2023-01-16 16:27:27 +01:00
|
|
|
$old_content = \get_option( 'activitypub_custom_post_content', ACTIVITYPUB_CUSTOM_POST_CONTENT );
|
|
|
|
|
|
|
|
// If the old content exists but is a blank string, we're going to need a flag to updated it even
|
|
|
|
// after setting it to the default contents.
|
|
|
|
$need_update = false;
|
|
|
|
|
|
|
|
// If the old contents is blank, use the defaults.
|
|
|
|
if( $old_content == "" ) { $old_content = ACTIVITYPUB_CUSTOM_POST_CONTENT; $need_update = true; }
|
|
|
|
|
|
|
|
// Set the new content to be the old content.
|
|
|
|
$content = $old_content;
|
2023-01-13 21:47:13 +01:00
|
|
|
|
2023-01-16 16:27:27 +01:00
|
|
|
// Convert old templates to shortcodes.
|
2023-01-13 21:47:13 +01:00
|
|
|
$content = \str_replace( '%title%', '[ap_title]', $content );
|
|
|
|
$content = \str_replace( '%excerpt%', '[ap_excerpt]', $content );
|
|
|
|
$content = \str_replace( '%content%', '[ap_content]', $content );
|
|
|
|
$content = \str_replace( '%permalink%', '[ap_permalink]', $content );
|
|
|
|
$content = \str_replace( '%shortlink%', '[ap_shortlink]', $content );
|
|
|
|
$content = \str_replace( '%hashtags%', '[ap_hashtags]', $content );
|
|
|
|
$content = \str_replace( '%tags%', '[ap_hashtags]', $content );
|
|
|
|
|
|
|
|
// Store the new template if required.
|
2023-01-16 16:27:27 +01:00
|
|
|
if( $content != $old_content || $need_update ) {
|
2023-01-13 21:47:13 +01:00
|
|
|
\update_option( 'activitypub_custom_post_content', $content );
|
2020-07-21 09:23:35 +02:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:47:13 +01:00
|
|
|
return $content;
|
2019-02-02 23:56:05 +01:00
|
|
|
}
|
|
|
|
|
2019-01-04 19:57:33 +01:00
|
|
|
/**
|
|
|
|
* Get the excerpt for a post for use outside of the loop.
|
|
|
|
*
|
|
|
|
* @param int Optional excerpt length.
|
|
|
|
*
|
|
|
|
* @return string The excerpt.
|
|
|
|
*/
|
2023-01-13 22:11:52 +01:00
|
|
|
public function get_the_post_excerpt( $excerpt_length = ACTIVITYPUB_EXCERPT_LENGTH ) {
|
2019-01-04 19:57:33 +01:00
|
|
|
$post = $this->post;
|
|
|
|
|
2019-09-27 10:12:59 +02:00
|
|
|
$excerpt = \get_post_field( 'post_excerpt', $post );
|
2019-01-04 19:57:33 +01:00
|
|
|
|
|
|
|
if ( '' === $excerpt ) {
|
|
|
|
|
2019-09-27 10:12:59 +02:00
|
|
|
$content = \get_post_field( 'post_content', $post );
|
2019-01-04 19:57:33 +01:00
|
|
|
|
|
|
|
// An empty string will make wp_trim_excerpt do stuff we do not want.
|
|
|
|
if ( '' !== $content ) {
|
|
|
|
|
2019-09-27 10:12:59 +02:00
|
|
|
$excerpt = \strip_shortcodes( $content );
|
2019-01-04 19:57:33 +01:00
|
|
|
|
|
|
|
/** This filter is documented in wp-includes/post-template.php */
|
2019-09-27 10:12:59 +02:00
|
|
|
$excerpt = \apply_filters( 'the_content', $excerpt );
|
|
|
|
$excerpt = \str_replace( ']]>', ']]>', $excerpt );
|
2019-01-04 19:57:33 +01:00
|
|
|
|
2023-01-07 02:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strip out any remaining tags.
|
|
|
|
$excerpt = \wp_strip_all_tags( $excerpt );
|
|
|
|
|
|
|
|
/** This filter is documented in wp-includes/formatting.php */
|
|
|
|
$excerpt_more = \apply_filters( 'excerpt_more', ' [...]' );
|
2023-01-16 15:28:10 +01:00
|
|
|
$excerpt_more_len = strlen( $excerpt_more );
|
2023-01-07 02:04:05 +01:00
|
|
|
|
|
|
|
// We now have a excerpt, but we need to check it's length, it may be longer than we want for two reasons:
|
|
|
|
//
|
|
|
|
// * The user has entered a manual excerpt which is longer that what we want.
|
|
|
|
// * No manual excerpt exists so we've used the content which might be longer than we want.
|
|
|
|
//
|
|
|
|
// Either way, let's trim it up if we need too. Also, don't forget to take into account the more indicator
|
|
|
|
// as part of the total length.
|
|
|
|
//
|
|
|
|
|
|
|
|
// Setup a variable to hold the current excerpts length.
|
|
|
|
$current_excerpt_length = strlen( $excerpt );
|
|
|
|
|
|
|
|
// Setup a variable to keep track of our target length.
|
2023-01-13 17:02:16 +01:00
|
|
|
$target_excerpt_length = $excerpt_length - $excerpt_more_len;
|
2023-01-07 02:04:05 +01:00
|
|
|
|
|
|
|
// Setup a variable to keep track of the current max length.
|
2023-01-13 17:02:16 +01:00
|
|
|
$current_excerpt_max = $target_excerpt_length;
|
2023-01-07 02:04:05 +01:00
|
|
|
|
|
|
|
// This is a loop since we can't calculate word break the string after 'the_excpert' filter has run (we would break
|
|
|
|
// all kinds of html tags), so we have to cut the excerpt down a bit at a time until we hit our target length.
|
2023-01-16 15:28:10 +01:00
|
|
|
while ( $current_excerpt_length > $target_excerpt_length && $current_excerpt_max > 0 ) {
|
2023-01-07 02:04:05 +01:00
|
|
|
// Trim the excerpt based on wordwrap() positioning.
|
|
|
|
// Note: we're using <br> as the linebreak just in case there are any newlines existing in the excerpt from the user.
|
|
|
|
// There won't be any <br> left after we've run wp_strip_all_tags() in the code above, so they're
|
|
|
|
// safe to use here. It won't be included in the final excerpt as the substr() will trim it off.
|
2023-01-13 17:02:16 +01:00
|
|
|
$excerpt = substr( $excerpt, 0, strpos( wordwrap( $excerpt, $current_excerpt_max, '<br>' ), '<br>' ) );
|
2023-01-07 02:04:05 +01:00
|
|
|
|
|
|
|
// If something went wrong, or we're in a language that wordwrap() doesn't understand,
|
|
|
|
// just chop it off and don't worry about breaking in the middle of a word.
|
2023-01-16 15:28:10 +01:00
|
|
|
if ( strlen( $excerpt ) > $excerpt_length - $excerpt_more_len ) {
|
2023-01-13 17:02:16 +01:00
|
|
|
$excerpt = substr( $excerpt, 0, $current_excerpt_max );
|
2023-01-07 02:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add in the more indicator.
|
|
|
|
$excerpt = $excerpt . $excerpt_more;
|
|
|
|
|
|
|
|
// Run it through the excerpt filter which will add some html tags back in.
|
|
|
|
$excerpt_filtered = apply_filters( 'the_excerpt', $excerpt );
|
2019-01-04 19:57:33 +01:00
|
|
|
|
2023-01-07 02:04:05 +01:00
|
|
|
// Now set the current excerpt length to this new filtered length.
|
|
|
|
$current_excerpt_length = strlen( $excerpt_filtered );
|
2019-01-04 19:57:33 +01:00
|
|
|
|
2023-01-07 02:04:05 +01:00
|
|
|
// Check to see if we're over the target length.
|
2023-01-16 15:28:10 +01:00
|
|
|
if ( $current_excerpt_length > $target_excerpt_length ) {
|
2023-01-07 02:04:05 +01:00
|
|
|
// If so, remove 20 characters from the current max and run the loop again.
|
2023-01-13 17:02:16 +01:00
|
|
|
$current_excerpt_max = $current_excerpt_max - 20;
|
2019-01-04 19:57:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 09:23:35 +02:00
|
|
|
return \apply_filters( 'the_excerpt', $excerpt );
|
2019-01-04 19:57:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the content for a post for use outside of the loop.
|
|
|
|
*
|
|
|
|
* @return string The content.
|
|
|
|
*/
|
2020-07-21 09:23:35 +02:00
|
|
|
public function get_the_post_content() {
|
2019-01-04 19:57:33 +01:00
|
|
|
$post = $this->post;
|
|
|
|
|
2019-09-27 10:12:59 +02:00
|
|
|
$content = \get_post_field( 'post_content', $post );
|
2019-01-04 19:57:33 +01:00
|
|
|
|
2020-07-21 09:23:35 +02:00
|
|
|
return \apply_filters( 'the_content', $content );
|
2019-02-02 23:56:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-07-21 09:23:35 +02:00
|
|
|
* Adds a backlink to the post/summary content
|
2019-02-02 23:56:05 +01:00
|
|
|
*
|
2023-01-13 21:47:13 +01:00
|
|
|
* @param string $type
|
2019-02-02 23:56:05 +01:00
|
|
|
*
|
2020-07-21 09:23:35 +02:00
|
|
|
* @return string
|
2019-02-02 23:56:05 +01:00
|
|
|
*/
|
2020-07-21 09:23:35 +02:00
|
|
|
public function get_the_post_link( $type = 'permalink' ) {
|
|
|
|
$post = $this->post;
|
2019-01-16 21:50:45 +01:00
|
|
|
|
2020-07-21 09:23:35 +02:00
|
|
|
if ( 'shortlink' === $type ) {
|
|
|
|
$link = \esc_url( \wp_get_shortlink( $post->ID ) );
|
|
|
|
} elseif ( 'permalink' === $type ) {
|
|
|
|
$link = \esc_url( \get_permalink( $post->ID ) );
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
2019-01-04 19:57:33 +01:00
|
|
|
|
2020-07-21 09:23:35 +02:00
|
|
|
return \sprintf( '<a href="%1$s">%1$s</a>', $link );
|
2019-02-02 23:56:05 +01:00
|
|
|
}
|
|
|
|
|
2019-03-02 21:10:42 +01:00
|
|
|
/**
|
2020-07-21 09:23:35 +02:00
|
|
|
* Adds all tags as hashtags to the post/summary content
|
2019-03-02 21:10:42 +01:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2020-07-21 09:23:35 +02:00
|
|
|
public function get_the_post_hashtags() {
|
|
|
|
$post = $this->post;
|
|
|
|
$tags = \get_the_tags( $post->ID );
|
2019-02-02 23:56:05 +01:00
|
|
|
|
2020-07-21 09:23:35 +02:00
|
|
|
if ( ! $tags ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$hash_tags = array();
|
|
|
|
|
|
|
|
foreach ( $tags as $tag ) {
|
|
|
|
$hash_tags[] = \sprintf( '<a rel="tag" class="u-tag u-category" href="%s">#%s</a>', \get_tag_link( $tag ), $tag->slug );
|
2019-02-02 23:56:05 +01:00
|
|
|
}
|
|
|
|
|
2020-07-21 09:23:35 +02:00
|
|
|
return \implode( ' ', $hash_tags );
|
2019-01-04 19:57:33 +01:00
|
|
|
}
|
2023-01-13 21:47:13 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds the featured image url to the post/summary content
|
|
|
|
*
|
|
|
|
* @param string $size
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_the_post_image( $size = 'full' ) {
|
|
|
|
$post = $this->post;
|
|
|
|
|
|
|
|
if( $size == '' ) { $size = 'full'; }
|
|
|
|
|
|
|
|
$image = \get_the_post_thumbnail_url( $post->ID, $size );
|
|
|
|
|
|
|
|
if ( ! $image ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $image;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds all categories as hashtags to the post/summary content
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_the_post_categories() {
|
|
|
|
$post = $this->post;
|
|
|
|
$categories = \get_the_category( $post->ID );
|
|
|
|
|
|
|
|
if ( ! $categories ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$hash_tags = array();
|
|
|
|
|
|
|
|
foreach ( $categories as $category ) {
|
|
|
|
$hash_tags[] = \sprintf( '<a rel="tag" class="u-tag u-category" href="%s">#%s</a>', \get_category_link( $category ), $category->slug );
|
|
|
|
}
|
|
|
|
|
|
|
|
return \implode( ' ', $hash_tags );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds author to the post/summary content
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_the_post_author() {
|
|
|
|
$post = $this->post;
|
|
|
|
$name = \get_the_author_meta( 'display_name', $post->post_author );
|
|
|
|
|
|
|
|
if ( ! $name ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds author's url to the post/summary content
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_the_post_profile_url() {
|
|
|
|
$post = $this->post;
|
|
|
|
$url = \get_the_author_meta( 'user_url', $post->post_author );
|
|
|
|
|
|
|
|
if ( ! $url ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds the post date/time to the post/summary content
|
|
|
|
*
|
|
|
|
* @param string display
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_the_post_date( $display = 'both' ) {
|
|
|
|
$post = $this->post;
|
|
|
|
$datetime = \get_post_datetime( $post );
|
|
|
|
$dateformat = \get_option( 'date_format' );
|
|
|
|
$timeformat = \get_option( 'time_format' );
|
|
|
|
|
|
|
|
switch( $display ) {
|
|
|
|
case 'date':
|
|
|
|
$date = $datetime->format( $dateformat );
|
|
|
|
break;
|
|
|
|
case 'time':
|
|
|
|
$date = $datetime->format( $timeformat );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$date = $datetime->format( $dateformat . ' @ ' . $timeformat );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! $date ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $date;
|
|
|
|
}
|
|
|
|
|
2018-09-30 22:51:22 +02:00
|
|
|
}
|