Simplified and optimized code
based on the Shortcode changes
This commit is contained in:
parent
5878a12c83
commit
dbaddd9189
1 changed files with 167 additions and 42 deletions
|
@ -7,27 +7,103 @@ namespace Activitypub\Model;
|
||||||
* @author Matthias Pfefferle
|
* @author Matthias Pfefferle
|
||||||
*/
|
*/
|
||||||
class Post {
|
class Post {
|
||||||
|
/**
|
||||||
|
* The WordPress Post Object.
|
||||||
|
*
|
||||||
|
* @var WP_Post
|
||||||
|
*/
|
||||||
private $post;
|
private $post;
|
||||||
private $post_author;
|
|
||||||
private $id;
|
|
||||||
private $summary;
|
|
||||||
private $content;
|
|
||||||
private $attachments;
|
|
||||||
private $tags;
|
|
||||||
private $object_type;
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Post Author.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $post_author;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Object ID.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Object Summary.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $summary;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Object Summary
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $content;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Object Attachments. This is usually a list of Images.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private $attachments;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Object Tags. This is usually the list of used Hashtags.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private $tags;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Onject Type
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $object_type = 'Note';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Allowed Tags, used in the content.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private $allowed_tags = array(
|
||||||
|
'a' => array(
|
||||||
|
'href' => array(),
|
||||||
|
'title' => array(),
|
||||||
|
'class' => array(),
|
||||||
|
'rel' => array(),
|
||||||
|
),
|
||||||
|
'br' => array(),
|
||||||
|
'p' => array(
|
||||||
|
'class' => array(),
|
||||||
|
),
|
||||||
|
'span' => array(
|
||||||
|
'class' => array(),
|
||||||
|
),
|
||||||
|
'div' => array(
|
||||||
|
'class' => array(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*
|
||||||
|
* @param WP_Post $post
|
||||||
|
*/
|
||||||
public function __construct( $post ) {
|
public function __construct( $post ) {
|
||||||
$this->post = \get_post( $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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Magic function to implement getter and setter
|
||||||
|
*
|
||||||
|
* @param string $method
|
||||||
|
* @param string $params
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
public function __call( $method, $params ) {
|
public function __call( $method, $params ) {
|
||||||
$var = \strtolower( \substr( $method, 4 ) );
|
$var = \strtolower( \substr( $method, 4 ) );
|
||||||
|
|
||||||
|
@ -40,34 +116,53 @@ class Post {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts this Object into an Array.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
public function to_array() {
|
public function to_array() {
|
||||||
$post = $this->post;
|
$post = $this->post;
|
||||||
|
|
||||||
$array = array(
|
$array = array(
|
||||||
'id' => $this->id,
|
'id' => $this->get_id(),
|
||||||
'type' => $this->object_type,
|
'type' => $this->get_object_type(),
|
||||||
'published' => \gmdate( 'Y-m-d\TH:i:s\Z', \strtotime( $post->post_date_gmt ) ),
|
'published' => \gmdate( 'Y-m-d\TH:i:s\Z', \strtotime( $post->post_date_gmt ) ),
|
||||||
'attributedTo' => \get_author_posts_url( $post->post_author ),
|
'attributedTo' => \get_author_posts_url( $post->post_author ),
|
||||||
'summary' => $this->summary,
|
'summary' => $this->get_summary(),
|
||||||
'inReplyTo' => null,
|
'inReplyTo' => null,
|
||||||
'content' => $this->content,
|
'content' => $this->get_content(),
|
||||||
'contentMap' => array(
|
'contentMap' => array(
|
||||||
\strstr( \get_locale(), '_', true ) => $this->content,
|
\strstr( \get_locale(), '_', true ) => $this->get_content(),
|
||||||
),
|
),
|
||||||
'to' => array( 'https://www.w3.org/ns/activitystreams#Public' ),
|
'to' => array( 'https://www.w3.org/ns/activitystreams#Public' ),
|
||||||
'cc' => array( 'https://www.w3.org/ns/activitystreams#Public' ),
|
'cc' => array( 'https://www.w3.org/ns/activitystreams#Public' ),
|
||||||
'attachment' => $this->attachments,
|
'attachment' => $this->get_attachments(),
|
||||||
'tag' => $this->tags,
|
'tag' => $this->get_tags(),
|
||||||
);
|
);
|
||||||
|
|
||||||
return \apply_filters( 'activitypub_post', $array );
|
return \apply_filters( 'activitypub_post', $array );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts this Object into a JSON String
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public function to_json() {
|
public function to_json() {
|
||||||
return \wp_json_encode( $this->to_array(), \JSON_HEX_TAG | \JSON_HEX_AMP | \JSON_HEX_QUOT );
|
return \wp_json_encode( $this->to_array(), \JSON_HEX_TAG | \JSON_HEX_AMP | \JSON_HEX_QUOT );
|
||||||
}
|
}
|
||||||
|
|
||||||
public function generate_id() {
|
/**
|
||||||
|
* Returns the ID of an Activity Object
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function get_id() {
|
||||||
|
if ( $this->id ) {
|
||||||
|
return $this->id;
|
||||||
|
}
|
||||||
|
|
||||||
$post = $this->post;
|
$post = $this->post;
|
||||||
|
|
||||||
if ( 'trash' === get_post_status( $post ) ) {
|
if ( 'trash' === get_post_status( $post ) ) {
|
||||||
|
@ -76,10 +171,21 @@ class Post {
|
||||||
$permalink = \get_permalink( $post );
|
$permalink = \get_permalink( $post );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->id = $permalink;
|
||||||
|
|
||||||
return $permalink;
|
return $permalink;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function generate_attachments() {
|
/**
|
||||||
|
* Returns a list of Image Attachments
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function get_attachments() {
|
||||||
|
if ( $this->attachments ) {
|
||||||
|
return $this->attachments;
|
||||||
|
}
|
||||||
|
|
||||||
$max_images = intval( \apply_filters( 'activitypub_max_image_attachments', \get_option( 'activitypub_max_image_attachments', ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS ) ) );
|
$max_images = intval( \apply_filters( 'activitypub_max_image_attachments', \get_option( 'activitypub_max_image_attachments', ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS ) ) );
|
||||||
|
|
||||||
$images = array();
|
$images = array();
|
||||||
|
@ -140,10 +246,21 @@ class Post {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->attachments = $images;
|
||||||
|
|
||||||
return $images;
|
return $images;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function generate_tags() {
|
/**
|
||||||
|
* Returns a list of Tags, used in the Post
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function get_tags() {
|
||||||
|
if ( $this->tags ) {
|
||||||
|
return $this->tags;
|
||||||
|
}
|
||||||
|
|
||||||
$tags = array();
|
$tags = array();
|
||||||
|
|
||||||
$post_tags = \get_the_tags( $this->post->ID );
|
$post_tags = \get_the_tags( $this->post->ID );
|
||||||
|
@ -158,18 +275,21 @@ class Post {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->tags = $tags;
|
||||||
|
|
||||||
return $tags;
|
return $tags;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the as2 object-type for a given post
|
* 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
|
* @return string the object-type
|
||||||
*/
|
*/
|
||||||
public function generate_object_type() {
|
public function get_object_type() {
|
||||||
|
if ( $this->object_type ) {
|
||||||
|
return $this->object_type;
|
||||||
|
}
|
||||||
|
|
||||||
if ( 'wordpress-post-format' !== \get_option( 'activitypub_object_type', 'note' ) ) {
|
if ( 'wordpress-post-format' !== \get_option( 'activitypub_object_type', 'note' ) ) {
|
||||||
return \ucfirst( \get_option( 'activitypub_object_type', 'note' ) );
|
return \ucfirst( \get_option( 'activitypub_object_type', 'note' ) );
|
||||||
}
|
}
|
||||||
|
@ -223,15 +343,21 @@ class Post {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->object_type = $object_type;
|
||||||
|
|
||||||
return $object_type;
|
return $object_type;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates the content for the activitypub item.
|
* Returns the content for the ActivityPub Item.
|
||||||
*
|
*
|
||||||
* @return string the content
|
* @return string the content
|
||||||
*/
|
*/
|
||||||
public function generate_the_content() {
|
public function get_content() {
|
||||||
|
if ( $this->content ) {
|
||||||
|
return $this->content;
|
||||||
|
}
|
||||||
|
|
||||||
$post = $this->post;
|
$post = $this->post;
|
||||||
$content = $this->get_post_content_template();
|
$content = $this->get_post_content_template();
|
||||||
|
|
||||||
|
@ -240,18 +366,16 @@ class Post {
|
||||||
$content = do_shortcode( $content );
|
$content = do_shortcode( $content );
|
||||||
wp_reset_postdata();
|
wp_reset_postdata();
|
||||||
|
|
||||||
$content = \trim( \preg_replace( '/[\r\n]{2,}/', '', $content ) );
|
$content = \wpautop( \wp_kses( $content, $this->allowed_tags ) );
|
||||||
|
|
||||||
$filtered_content = \apply_filters( 'activitypub_the_content', $content, $this->post );
|
$filtered_content = \apply_filters( 'activitypub_the_content', $content, $post );
|
||||||
$decoded_content = \html_entity_decode( $filtered_content, \ENT_QUOTES, 'UTF-8' );
|
$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 ) );
|
$content = \trim( \preg_replace( '/[\n\r]/', '', $content ) );
|
||||||
|
|
||||||
if ( $allowed_html ) {
|
$this->content = $content;
|
||||||
return \strip_tags( $decoded_content, $allowed_html );
|
|
||||||
}
|
|
||||||
|
|
||||||
return $decoded_content;
|
return $content;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -261,15 +385,15 @@ class Post {
|
||||||
*/
|
*/
|
||||||
public function get_post_content_template() {
|
public function get_post_content_template() {
|
||||||
if ( 'excerpt' === \get_option( 'activitypub_post_content_type', 'content' ) ) {
|
if ( 'excerpt' === \get_option( 'activitypub_post_content_type', 'content' ) ) {
|
||||||
return "[ap_excerpt]\n\n<p>[ap_permalink]</p>";
|
return "[ap_excerpt]\n\n[ap_permalink]";
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( 'title' === \get_option( 'activitypub_post_content_type', 'content' ) ) {
|
if ( 'title' === \get_option( 'activitypub_post_content_type', 'content' ) ) {
|
||||||
return "<p>[ap_title]</p>\n\n<p>[ap_permalink]</p>";
|
return "[ap_title]\n\n[ap_permalink]";
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( 'content' === \get_option( 'activitypub_post_content_type', 'content' ) ) {
|
if ( 'content' === \get_option( 'activitypub_post_content_type', 'content' ) ) {
|
||||||
return "[ap_content]\n\n<p>[ap_hashtags]</p>\n\n<p>[ap_permalink]</p>";
|
return "[ap_content]\n\n[ap_hashtags]\n\n[ap_permalink]";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Upgrade from old template codes to shortcodes.
|
// Upgrade from old template codes to shortcodes.
|
||||||
|
@ -294,7 +418,8 @@ class Post {
|
||||||
// If the old contents is blank, use the defaults.
|
// If the old contents is blank, use the defaults.
|
||||||
if ( '' === $old_content ) {
|
if ( '' === $old_content ) {
|
||||||
$old_content = ACTIVITYPUB_CUSTOM_POST_CONTENT;
|
$old_content = ACTIVITYPUB_CUSTOM_POST_CONTENT;
|
||||||
$need_update = true; }
|
$need_update = true;
|
||||||
|
}
|
||||||
|
|
||||||
// Set the new content to be the old content.
|
// Set the new content to be the old content.
|
||||||
$content = $old_content;
|
$content = $old_content;
|
||||||
|
|
Loading…
Reference in a new issue