PluginProbe
ActivityPub / 0.14.1
ActivityPub v0.14.1
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
activitypub / includes / model / class-post.php

class-post.php in ActivityPub 0.14.1, at includes/model/class-post.php

361 lines 9.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Activitypub\Model;
3
4 /**
5 * ActivityPub Post Class
6 *
7 * @author Matthias Pfefferle
8 */
9 class Post {
10 private $post;
11 private $post_author;
12 private $id;
13 private $summary;
14 private $content;
15 private $attachments;
16 private $tags;
17 private $object_type;
18
19 public function __construct( $post = null ) {
20 $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();
29 }
30
31 public function __call( $method, $params ) {
32 $var = \strtolower( \substr( $method, 4 ) );
33
34 if ( \strncasecmp( $method, 'get', 3 ) === 0 ) {
35 return $this->$var;
36 }
37
38 if ( \strncasecmp( $method, 'set', 3 ) === 0 ) {
39 $this->$var = $params[0];
40 }
41 }
42
43 public function to_array() {
44 $post = $this->post;
45
46 $array = array(
47 'id' => $this->id,
48 'type' => $this->object_type,
49 'published' => \gmdate( 'Y-m-d\TH:i:s\Z', \strtotime( $post->post_date_gmt ) ),
50 'attributedTo' => \get_author_posts_url( $post->post_author ),
51 'summary' => $this->summary,
52 'inReplyTo' => null,
53 'content' => $this->content,
54 'contentMap' => array(
55 \strstr( \get_locale(), '_', true ) => $this->content,
56 ),
57 'to' => array( 'https://www.w3.org/ns/activitystreams#Public' ),
58 'cc' => array( 'https://www.w3.org/ns/activitystreams#Public' ),
59 'attachment' => $this->attachments,
60 'tag' => $this->tags,
61 );
62
63 return \apply_filters( 'activitypub_post', $array );
64 }
65
66 public function to_json() {
67 return \wp_json_encode( $this->to_array(), \JSON_HEX_TAG | \JSON_HEX_AMP | \JSON_HEX_QUOT );
68 }
69
70 public function generate_id() {
71 $post = $this->post;
72 $permalink = \get_permalink( $post );
73
74 // replace 'trashed' for delete activity
75 return \str_replace( '__trashed', '', $permalink );
76 }
77
78 public function generate_attachments() {
79 $max_images = \apply_filters( 'activitypub_max_images', 3 );
80
81 $images = array();
82
83 // max images can't be negative or zero
84 if ( $max_images <= 0 ) {
85 $max_images = 1;
86 }
87
88 $id = $this->post->ID;
89
90 $image_ids = array();
91 // list post thumbnail first if this post has one
92 if ( \function_exists( 'has_post_thumbnail' ) && \has_post_thumbnail( $id ) ) {
93 $image_ids[] = \get_post_thumbnail_id( $id );
94 $max_images--;
95 }
96 // then list any image attachments
97 $query = new \WP_Query(
98 array(
99 'post_parent' => $id,
100 'post_status' => 'inherit',
101 'post_type' => 'attachment',
102 'post_mime_type' => 'image',
103 'order' => 'ASC',
104 'orderby' => 'menu_order ID',
105 'posts_per_page' => $max_images,
106 )
107 );
108 foreach ( $query->get_posts() as $attachment ) {
109 if ( ! \in_array( $attachment->ID, $image_ids, true ) ) {
110 $image_ids[] = $attachment->ID;
111 }
112 }
113
114 $image_ids = \array_unique( $image_ids );
115
116 // get URLs for each image
117 foreach ( $image_ids as $id ) {
118 $alt = \get_post_meta( $id, '_wp_attachment_image_alt', true );
119 $thumbnail = \wp_get_attachment_image_src( $id, 'full' );
120 $mimetype = \get_post_mime_type( $id );
121
122 if ( $thumbnail ) {
123 $image = array(
124 'type' => 'Image',
125 'url' => $thumbnail[0],
126 'mediaType' => $mimetype,
127 );
128 if ( $alt ) {
129 $image['name'] = $alt;
130 }
131 $images[] = $image;
132 }
133 }
134
135 return $images;
136 }
137
138 public function generate_tags() {
139 $tags = array();
140
141 $post_tags = \get_the_tags( $this->post->ID );
142 if ( $post_tags ) {
143 foreach ( $post_tags as $post_tag ) {
144 $tag = array(
145 'type' => 'Hashtag',
146 'href' => \get_tag_link( $post_tag->term_id ),
147 'name' => '#' . $post_tag->slug,
148 );
149 $tags[] = $tag;
150 }
151 }
152
153 return $tags;
154 }
155
156 /**
157 * Returns the as2 object-type for a given post
158 *
159 * @param string $type the object-type
160 * @param Object $post the post-object
161 *
162 * @return string the object-type
163 */
164 public function generate_object_type() {
165 if ( 'wordpress-post-format' !== \get_option( 'activitypub_object_type', 'note' ) ) {
166 return \ucfirst( \get_option( 'activitypub_object_type', 'note' ) );
167 }
168
169 $post_type = \get_post_type( $this->post );
170 switch ( $post_type ) {
171 case 'post':
172 $post_format = \get_post_format( $this->post );
173 switch ( $post_format ) {
174 case 'aside':
175 case 'status':
176 case 'quote':
177 case 'note':
178 $object_type = 'Note';
179 break;
180 case 'gallery':
181 case 'image':
182 $object_type = 'Image';
183 break;
184 case 'video':
185 $object_type = 'Video';
186 break;
187 case 'audio':
188 $object_type = 'Audio';
189 break;
190 default:
191 $object_type = 'Article';
192 break;
193 }
194 break;
195 case 'page':
196 $object_type = 'Page';
197 break;
198 case 'attachment':
199 $mime_type = \get_post_mime_type();
200 $media_type = \preg_replace( '/(\/[a-zA-Z]+)/i', '', $mime_type );
201 switch ( $media_type ) {
202 case 'audio':
203 $object_type = 'Audio';
204 break;
205 case 'video':
206 $object_type = 'Video';
207 break;
208 case 'image':
209 $object_type = 'Image';
210 break;
211 }
212 break;
213 default:
214 $object_type = 'Article';
215 break;
216 }
217
218 return $object_type;
219 }
220
221 public function generate_the_content() {
222 $post = $this->post;
223 $content = $this->get_post_content_template();
224
225 $content = \str_replace( '%title%', \get_the_title( $post->ID ), $content );
226 $content = \str_replace( '%excerpt%', $this->get_the_post_excerpt(), $content );
227 $content = \str_replace( '%content%', $this->get_the_post_content(), $content );
228 $content = \str_replace( '%permalink%', $this->get_the_post_link( 'permalink' ), $content );
229 $content = \str_replace( '%shortlink%', $this->get_the_post_link( 'shortlink' ), $content );
230 $content = \str_replace( '%hashtags%', $this->get_the_post_hashtags(), $content );
231 // backwards compatibility
232 $content = \str_replace( '%tags%', $this->get_the_post_hashtags(), $content );
233
234 $content = \trim( \preg_replace( '/[\r\n]{2,}/', '', $content ) );
235
236 $filtered_content = \apply_filters( 'activitypub_the_content', $content, $this->post );
237 $decoded_content = \html_entity_decode( $filtered_content, \ENT_QUOTES, 'UTF-8' );
238
239 $allowed_html = \apply_filters( 'activitypub_allowed_html', \get_option( 'activitypub_allowed_html', ACTIVITYPUB_ALLOWED_HTML ) );
240
241 if ( $allowed_html ) {
242 return \strip_tags( $decoded_content, $allowed_html );
243 }
244
245 return $decoded_content;
246 }
247
248 public function get_post_content_template() {
249 if ( 'excerpt' === \get_option( 'activitypub_post_content_type', 'content' ) ) {
250 return "%excerpt%\n\n<p>%permalink%</p>";
251 }
252
253 if ( 'title' === \get_option( 'activitypub_post_content_type', 'content' ) ) {
254 return "<p><strong>%title%</strong></p>\n\n<p>%permalink%</p>";
255 }
256
257 if ( 'content' === \get_option( 'activitypub_post_content_type', 'content' ) ) {
258 return "%content%\n\n<p>%hashtags%</p>\n\n<p>%permalink%</p>";
259 }
260
261 return \get_option( 'activitypub_custom_post_content', ACTIVITYPUB_CUSTOM_POST_CONTENT );
262 }
263
264 /**
265 * Get the excerpt for a post for use outside of the loop.
266 *
267 * @param int Optional excerpt length.
268 *
269 * @return string The excerpt.
270 */
271 public function get_the_post_excerpt( $excerpt_length = 400 ) {
272 $post = $this->post;
273
274 $excerpt = \get_post_field( 'post_excerpt', $post );
275
276 if ( '' === $excerpt ) {
277
278 $content = \get_post_field( 'post_content', $post );
279
280 // An empty string will make wp_trim_excerpt do stuff we do not want.
281 if ( '' !== $content ) {
282
283 $excerpt = \strip_shortcodes( $content );
284
285 /** This filter is documented in wp-includes/post-template.php */
286 $excerpt = \apply_filters( 'the_content', $excerpt );
287 $excerpt = \str_replace( ']]>', ']]>', $excerpt );
288
289 $excerpt_length = \apply_filters( 'excerpt_length', $excerpt_length );
290
291 /** This filter is documented in wp-includes/formatting.php */
292 $excerpt_more = \apply_filters( 'excerpt_more', ' [...]' );
293
294 $excerpt = \wp_trim_words( $excerpt, $excerpt_length, $excerpt_more );
295 }
296 }
297
298 return \apply_filters( 'the_excerpt', $excerpt );
299 }
300
301 /**
302 * Get the content for a post for use outside of the loop.
303 *
304 * @return string The content.
305 */
306 public function get_the_post_content() {
307 $post = $this->post;
308
309 $content = \get_post_field( 'post_content', $post );
310
311 return \apply_filters( 'the_content', $content );
312 }
313
314 /**
315 * Adds a backlink to the post/summary content
316 *
317 * @param string $content
318 * @param WP_Post $post
319 *
320 * @return string
321 */
322 public function get_the_post_link( $type = 'permalink' ) {
323 $post = $this->post;
324
325 if ( 'shortlink' === $type ) {
326 $link = \esc_url( \wp_get_shortlink( $post->ID ) );
327 } elseif ( 'permalink' === $type ) {
328 $link = \esc_url( \get_permalink( $post->ID ) );
329 } else {
330 return '';
331 }
332
333 return \sprintf( '<a href="%1$s">%1$s</a>', $link );
334 }
335
336 /**
337 * Adds all tags as hashtags to the post/summary content
338 *
339 * @param string $content
340 * @param WP_Post $post
341 *
342 * @return string
343 */
344 public function get_the_post_hashtags() {
345 $post = $this->post;
346 $tags = \get_the_tags( $post->ID );
347
348 if ( ! $tags ) {
349 return '';
350 }
351
352 $hash_tags = array();
353
354 foreach ( $tags as $tag ) {
355 $hash_tags[] = \sprintf( '<a rel="tag" class="u-tag u-category" href="%s">#%s</a>', \get_tag_link( $tag ), $tag->slug );
356 }
357
358 return \implode( ' ', $hash_tags );
359 }
360 }
361