PluginProbe
ActivityPub / 0.9.0
ActivityPub v0.9.0
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.9.0, at includes/model/class-post.php

325 lines 8.1 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
12 /**
13 * Initialize the class, registering WordPress hooks
14 */
15 public static function init() {
16 \add_filter( 'activitypub_the_summary', array( '\Activitypub\Model\Post', 'add_backlink_to_content' ), 15, 2 );
17 \add_filter( 'activitypub_the_content', array( '\Activitypub\Model\Post', 'add_backlink_to_content' ), 15, 2 );
18 }
19
20 public function __construct( $post = null ) {
21 $this->post = \get_post( $post );
22 }
23
24 public function get_post() {
25 return $this->post;
26 }
27
28 public function get_post_author() {
29 return $this->post->post_author;
30 }
31
32 public function to_array() {
33 $post = $this->post;
34
35 $array = array(
36 'id' => \get_permalink( $post ),
37 'type' => $this->get_object_type(),
38 'published' => \date( 'Y-m-d\TH:i:s\Z', \strtotime( $post->post_date ) ),
39 'attributedTo' => \get_author_posts_url( $post->post_author ),
40 'summary' => $this->get_the_summary(),
41 'inReplyTo' => null,
42 'content' => $this->get_the_content(),
43 'contentMap' => array(
44 \strstr( \get_locale(), '_', true ) => $this->get_the_content(),
45 ),
46 'to' => array( 'https://www.w3.org/ns/activitystreams#Public' ),
47 'cc' => array( 'https://www.w3.org/ns/activitystreams#Public' ),
48 'attachment' => $this->get_attachments(),
49 'tag' => $this->get_tags(),
50 );
51
52 return \apply_filters( 'activitypub_post', $array );
53 }
54
55 public function to_json() {
56 return \wp_json_encode( $this->to_array(), JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_QUOT );
57 }
58
59 public function get_attachments() {
60 $max_images = \apply_filters( 'activitypub_max_images', 3 );
61
62 $images = array();
63
64 // max images can't be negative or zero
65 if ( $max_images <= 0 ) {
66 $max_images = 1;
67 }
68
69 $id = $this->post->ID;
70
71 $image_ids = array();
72 // list post thumbnail first if this post has one
73 if ( \function_exists( 'has_post_thumbnail' ) && \has_post_thumbnail( $id ) ) {
74 $image_ids[] = \get_post_thumbnail_id( $id );
75 $max_images--;
76 }
77 // then list any image attachments
78 $query = new \WP_Query(
79 array(
80 'post_parent' => $id,
81 'post_status' => 'inherit',
82 'post_type' => 'attachment',
83 'post_mime_type' => 'image',
84 'order' => 'ASC',
85 'orderby' => 'menu_order ID',
86 'posts_per_page' => $max_images,
87 )
88 );
89 foreach ( $query->get_posts() as $attachment ) {
90 if ( ! \in_array( $attachment->ID, $image_ids, true ) ) {
91 $image_ids[] = $attachment->ID;
92 }
93 }
94
95 $image_ids = \array_unique( $image_ids );
96
97 // get URLs for each image
98 foreach ( $image_ids as $id ) {
99 $thumbnail = \wp_get_attachment_image_src( $id, 'full' );
100 $mimetype = \get_post_mime_type( $id );
101
102 if ( $thumbnail ) {
103 $images[] = array(
104 'url' => $thumbnail[0],
105 'type' => $mimetype,
106 );
107 }
108 }
109
110 $attachments = array();
111
112 // add attachments
113 if ( $images ) {
114 foreach ( $images as $image ) {
115 $attachment = array(
116 'type' => 'Image',
117 'url' => $image['url'],
118 'mediaType' => $image['type'],
119 );
120 $attachments[] = $attachment;
121 }
122 }
123
124 return $attachments;
125 }
126
127 public function get_tags() {
128 $tags = array();
129
130 $post_tags = \get_the_tags( $this->post->ID );
131 if ( $post_tags ) {
132 foreach ( $post_tags as $post_tag ) {
133 $tag = array(
134 'type' => 'Hashtag',
135 'href' => \get_tag_link( $post_tag->term_id ),
136 'name' => '#' . $post_tag->slug,
137 );
138 $tags[] = $tag;
139 }
140 }
141
142 return $tags;
143 }
144
145 /**
146 * Returns the as2 object-type for a given post
147 *
148 * @param string $type the object-type
149 * @param Object $post the post-object
150 *
151 * @return string the object-type
152 */
153 public function get_object_type() {
154 if ( 'wordpress-post-format' !== \get_option( 'activitypub_object_type', 'note' ) ) {
155 return \ucfirst( \get_option( 'activitypub_object_type', 'note' ) );
156 }
157
158 $post_type = \get_post_type( $this->post );
159 switch ( $post_type ) {
160 case 'post':
161 $post_format = \get_post_format( $this->post );
162 switch ( $post_format ) {
163 case 'aside':
164 case 'status':
165 case 'quote':
166 case 'note':
167 $object_type = 'Note';
168 break;
169 case 'gallery':
170 case 'image':
171 $object_type = 'Image';
172 break;
173 case 'video':
174 $object_type = 'Video';
175 break;
176 case 'audio':
177 $object_type = 'Audio';
178 break;
179 default:
180 $object_type = 'Article';
181 break;
182 }
183 break;
184 case 'page':
185 $object_type = 'Page';
186 break;
187 case 'attachment':
188 $mime_type = \get_post_mime_type();
189 $media_type = \preg_replace( '/(\/[a-zA-Z]+)/i', '', $mime_type );
190 switch ( $media_type ) {
191 case 'audio':
192 $object_type = 'Audio';
193 break;
194 case 'video':
195 $object_type = 'Video';
196 break;
197 case 'image':
198 $object_type = 'Image';
199 break;
200 }
201 break;
202 default:
203 $object_type = 'Article';
204 break;
205 }
206
207 return $object_type;
208 }
209
210 public function get_the_content() {
211 if ( 'excerpt' === \get_option( 'activitypub_post_content_type', 'content' ) ) {
212 return $this->get_the_post_summary();
213 }
214
215 return $this->get_the_post_content();
216 }
217
218 public function get_the_summary() {
219 if ( 'Article' === $this->get_object_type() ) {
220 $excerpt = $this->get_the_post_excerpt( 400 );
221
222 return \html_entity_decode( $excerpt, ENT_QUOTES, 'UTF-8' );
223 }
224
225 return null;
226 }
227
228 /**
229 * Get the excerpt for a post for use outside of the loop.
230 *
231 * @param int Optional excerpt length.
232 *
233 * @return string The excerpt.
234 */
235 public function get_the_post_excerpt( $excerpt_length = 400 ) {
236 $post = $this->post;
237
238 $excerpt = \get_post_field( 'post_excerpt', $post );
239
240 if ( '' === $excerpt ) {
241
242 $content = \get_post_field( 'post_content', $post );
243
244 // An empty string will make wp_trim_excerpt do stuff we do not want.
245 if ( '' !== $content ) {
246
247 $excerpt = \strip_shortcodes( $content );
248
249 /** This filter is documented in wp-includes/post-template.php */
250 $excerpt = \apply_filters( 'the_content', $excerpt );
251 $excerpt = \str_replace( ']]>', ']]>', $excerpt );
252
253 $excerpt_length = \apply_filters( 'excerpt_length', $excerpt_length );
254
255 /** This filter is documented in wp-includes/formatting.php */
256 $excerpt_more = \apply_filters( 'excerpt_more', ' [...]' );
257
258 $excerpt = \wp_trim_words( $excerpt, $excerpt_length, $excerpt_more );
259 }
260 }
261
262 return $excerpt;
263 }
264
265 /**
266 * Get the content for a post for use outside of the loop.
267 *
268 * @return string The content.
269 */
270 public function get_the_post_content() {
271 $post = $this->post;
272
273 $content = \get_post_field( 'post_content', $post );
274
275 $filtered_content = \apply_filters( 'the_content', $content );
276 $filtered_content = \apply_filters( 'activitypub_the_content', $filtered_content, $this->post );
277
278 $decoded_content = \html_entity_decode( $filtered_content, ENT_QUOTES, 'UTF-8' );
279
280 $allowed_html = \apply_filters( 'activitypub_allowed_html', '<a><p><ul><ol><li><code><blockquote><pre>' );
281
282 return \trim( \preg_replace( '/[\r\n]{2,}/', '', \strip_tags( $decoded_content, $allowed_html ) ) );
283 }
284
285 /**
286 * Get the excerpt for a post for use outside of the loop.
287 *
288 * @param int Optional excerpt length.
289 *
290 * @return string The excerpt.
291 */
292 public function get_the_post_summary( $summary_length = 400 ) {
293 $summary = $this->get_the_post_excerpt( $summary_length );
294
295 $filtered_summary = \apply_filters( 'the_excerpt', $summary );
296 $filtered_summary = \apply_filters( 'activitypub_the_summary', $filtered_summary, $this->post );
297
298 $decoded_summary = \html_entity_decode( $filtered_summary, ENT_QUOTES, 'UTF-8' );
299
300 $allowed_html = \apply_filters( 'activitypub_allowed_html', '<a><p>' );
301
302 return \trim( \preg_replace( '/[\r\n]{2,}/', '', \strip_tags( $decoded_summary, $allowed_html ) ) );
303 }
304
305 /**
306 * Adds a backlink to the post/summary content
307 *
308 * @param string $content
309 * @param WP_Post $post
310 *
311 * @return string
312 */
313 public static function add_backlink_to_content( $content, $post ) {
314 $link = '';
315
316 if ( \get_option( 'activitypub_use_shortlink', 0 ) ) {
317 $link = \esc_url( \wp_get_shortlink( $post->ID ) );
318 } else {
319 $link = \esc_url( \get_permalink( $post->ID ) );
320 }
321
322 return $content . '<p><a href="' . $link . '">' . $link . '</a></p>';
323 }
324 }
325