PluginProbe
ActivityPub / 0.4.3
ActivityPub v0.4.3
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 / class-activitypub-post.php

class-activitypub-post.php in ActivityPub 0.4.3, at includes/class-activitypub-post.php

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