Skip to content

Commit dbaddd9

Browse files
committed
Simplified and optimized code
based on the Shortcode changes
1 parent 5878a12 commit dbaddd9

File tree

1 file changed

+161
-36
lines changed

1 file changed

+161
-36
lines changed

includes/model/class-post.php

Lines changed: 161 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,103 @@
77
* @author Matthias Pfefferle
88
*/
99
class Post {
10+
/**
11+
* The WordPress Post Object.
12+
*
13+
* @var WP_Post
14+
*/
1015
private $post;
16+
17+
/**
18+
* The Post Author.
19+
*
20+
* @var string
21+
*/
1122
private $post_author;
23+
24+
/**
25+
* The Object ID.
26+
*
27+
* @var string
28+
*/
1229
private $id;
30+
31+
/**
32+
* The Object Summary.
33+
*
34+
* @var string
35+
*/
1336
private $summary;
37+
38+
/**
39+
* The Object Summary
40+
*
41+
* @var string
42+
*/
1443
private $content;
44+
45+
/**
46+
* The Object Attachments. This is usually a list of Images.
47+
*
48+
* @var array
49+
*/
1550
private $attachments;
51+
52+
/**
53+
* The Object Tags. This is usually the list of used Hashtags.
54+
*
55+
* @var array
56+
*/
1657
private $tags;
17-
private $object_type;
1858

59+
/**
60+
* The Onject Type
61+
*
62+
* @var string
63+
*/
64+
private $object_type = 'Note';
65+
66+
/**
67+
* The Allowed Tags, used in the content.
68+
*
69+
* @var array
70+
*/
71+
private $allowed_tags = array(
72+
'a' => array(
73+
'href' => array(),
74+
'title' => array(),
75+
'class' => array(),
76+
'rel' => array(),
77+
),
78+
'br' => array(),
79+
'p' => array(
80+
'class' => array(),
81+
),
82+
'span' => array(
83+
'class' => array(),
84+
),
85+
'div' => array(
86+
'class' => array(),
87+
),
88+
);
89+
90+
/**
91+
* Constructor
92+
*
93+
* @param WP_Post $post
94+
*/
1995
public function __construct( $post ) {
2096
$this->post = \get_post( $post );
21-
22-
$this->post_author = $this->post->post_author;
23-
$this->id = $this->generate_id();
24-
$this->summary = $this->generate_the_title();
25-
$this->content = $this->generate_the_content();
26-
$this->attachments = $this->generate_attachments();
27-
$this->tags = $this->generate_tags();
28-
$this->object_type = $this->generate_object_type();
2997
}
3098

99+
/**
100+
* Magic function to implement getter and setter
101+
*
102+
* @param string $method
103+
* @param string $params
104+
*
105+
* @return void
106+
*/
31107
public function __call( $method, $params ) {
32108
$var = \strtolower( \substr( $method, 4 ) );
33109

@@ -40,34 +116,53 @@ public function __call( $method, $params ) {
40116
}
41117
}
42118

119+
/**
120+
* Converts this Object into an Array.
121+
*
122+
* @return array
123+
*/
43124
public function to_array() {
44125
$post = $this->post;
45126

46127
$array = array(
47-
'id' => $this->id,
48-
'type' => $this->object_type,
128+
'id' => $this->get_id(),
129+
'type' => $this->get_object_type(),
49130
'published' => \gmdate( 'Y-m-d\TH:i:s\Z', \strtotime( $post->post_date_gmt ) ),
50131
'attributedTo' => \get_author_posts_url( $post->post_author ),
51-
'summary' => $this->summary,
132+
'summary' => $this->get_summary(),
52133
'inReplyTo' => null,
53-
'content' => $this->content,
134+
'content' => $this->get_content(),
54135
'contentMap' => array(
55-
\strstr( \get_locale(), '_', true ) => $this->content,
136+
\strstr( \get_locale(), '_', true ) => $this->get_content(),
56137
),
57138
'to' => array( 'https://www.w3.org/ns/activitystreams#Public' ),
58139
'cc' => array( 'https://www.w3.org/ns/activitystreams#Public' ),
59-
'attachment' => $this->attachments,
60-
'tag' => $this->tags,
140+
'attachment' => $this->get_attachments(),
141+
'tag' => $this->get_tags(),
61142
);
62143

63144
return \apply_filters( 'activitypub_post', $array );
64145
}
65146

147+
/**
148+
* Converts this Object into a JSON String
149+
*
150+
* @return string
151+
*/
66152
public function to_json() {
67153
return \wp_json_encode( $this->to_array(), \JSON_HEX_TAG | \JSON_HEX_AMP | \JSON_HEX_QUOT );
68154
}
69155

70-
public function generate_id() {
156+
/**
157+
* Returns the ID of an Activity Object
158+
*
159+
* @return string
160+
*/
161+
public function get_id() {
162+
if ( $this->id ) {
163+
return $this->id;
164+
}
165+
71166
$post = $this->post;
72167

73168
if ( 'trash' === get_post_status( $post ) ) {
@@ -76,10 +171,21 @@ public function generate_id() {
76171
$permalink = \get_permalink( $post );
77172
}
78173

174+
$this->id = $permalink;
175+
79176
return $permalink;
80177
}
81178

82-
public function generate_attachments() {
179+
/**
180+
* Returns a list of Image Attachments
181+
*
182+
* @return array
183+
*/
184+
public function get_attachments() {
185+
if ( $this->attachments ) {
186+
return $this->attachments;
187+
}
188+
83189
$max_images = intval( \apply_filters( 'activitypub_max_image_attachments', \get_option( 'activitypub_max_image_attachments', ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS ) ) );
84190

85191
$images = array();
@@ -140,10 +246,21 @@ public function generate_attachments() {
140246
}
141247
}
142248

249+
$this->attachments = $images;
250+
143251
return $images;
144252
}
145253

146-
public function generate_tags() {
254+
/**
255+
* Returns a list of Tags, used in the Post
256+
*
257+
* @return array
258+
*/
259+
public function get_tags() {
260+
if ( $this->tags ) {
261+
return $this->tags;
262+
}
263+
147264
$tags = array();
148265

149266
$post_tags = \get_the_tags( $this->post->ID );
@@ -158,18 +275,21 @@ public function generate_tags() {
158275
}
159276
}
160277

278+
$this->tags = $tags;
279+
161280
return $tags;
162281
}
163282

164283
/**
165284
* Returns the as2 object-type for a given post
166285
*
167-
* @param string $type the object-type
168-
* @param Object $post the post-object
169-
*
170286
* @return string the object-type
171287
*/
172-
public function generate_object_type() {
288+
public function get_object_type() {
289+
if ( $this->object_type ) {
290+
return $this->object_type;
291+
}
292+
173293
if ( 'wordpress-post-format' !== \get_option( 'activitypub_object_type', 'note' ) ) {
174294
return \ucfirst( \get_option( 'activitypub_object_type', 'note' ) );
175295
}
@@ -223,15 +343,21 @@ public function generate_object_type() {
223343
break;
224344
}
225345

346+
$this->object_type = $object_type;
347+
226348
return $object_type;
227349
}
228350

229351
/**
230-
* Generates the content for the activitypub item.
352+
* Returns the content for the ActivityPub Item.
231353
*
232354
* @return string the content
233355
*/
234-
public function generate_the_content() {
356+
public function get_content() {
357+
if ( $this->content ) {
358+
return $this->content;
359+
}
360+
235361
$post = $this->post;
236362
$content = $this->get_post_content_template();
237363

@@ -240,18 +366,16 @@ public function generate_the_content() {
240366
$content = do_shortcode( $content );
241367
wp_reset_postdata();
242368

243-
$content = \trim( \preg_replace( '/[\r\n]{2,}/', '', $content ) );
369+
$content = \wpautop( \wp_kses( $content, $this->allowed_tags ) );
244370

245-
$filtered_content = \apply_filters( 'activitypub_the_content', $content, $this->post );
371+
$filtered_content = \apply_filters( 'activitypub_the_content', $content, $post );
246372
$decoded_content = \html_entity_decode( $filtered_content, \ENT_QUOTES, 'UTF-8' );
247373

248-
$allowed_html = \apply_filters( 'activitypub_allowed_html', \get_option( 'activitypub_allowed_html', ACTIVITYPUB_ALLOWED_HTML ) );
374+
$content = \trim( \preg_replace( '/[\n\r]/', '', $content ) );
249375

250-
if ( $allowed_html ) {
251-
return \strip_tags( $decoded_content, $allowed_html );
252-
}
376+
$this->content = $content;
253377

254-
return $decoded_content;
378+
return $content;
255379
}
256380

257381
/**
@@ -261,15 +385,15 @@ public function generate_the_content() {
261385
*/
262386
public function get_post_content_template() {
263387
if ( 'excerpt' === \get_option( 'activitypub_post_content_type', 'content' ) ) {
264-
return "[ap_excerpt]\n\n<p>[ap_permalink]</p>";
388+
return "[ap_excerpt]\n\n[ap_permalink]";
265389
}
266390

267391
if ( 'title' === \get_option( 'activitypub_post_content_type', 'content' ) ) {
268-
return "<p>[ap_title]</p>\n\n<p>[ap_permalink]</p>";
392+
return "[ap_title]\n\n[ap_permalink]";
269393
}
270394

271395
if ( 'content' === \get_option( 'activitypub_post_content_type', 'content' ) ) {
272-
return "[ap_content]\n\n<p>[ap_hashtags]</p>\n\n<p>[ap_permalink]</p>";
396+
return "[ap_content]\n\n[ap_hashtags]\n\n[ap_permalink]";
273397
}
274398

275399
// Upgrade from old template codes to shortcodes.
@@ -294,7 +418,8 @@ public static function upgrade_post_content_template() {
294418
// If the old contents is blank, use the defaults.
295419
if ( '' === $old_content ) {
296420
$old_content = ACTIVITYPUB_CUSTOM_POST_CONTENT;
297-
$need_update = true; }
421+
$need_update = true;
422+
}
298423

299424
// Set the new content to be the old content.
300425
$content = $old_content;

0 commit comments

Comments
 (0)