PluginProbe
ActivityPub / 3.2.5
ActivityPub v3.2.5
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-shortcodes.php

class-shortcodes.php in ActivityPub 3.2.5, at includes/class-shortcodes.php

526 lines 11.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Activitypub;
3
4 use function Activitypub\esc_hashtag;
5 use function Activitypub\generate_post_summary;
6
7 class Shortcodes {
8 /**
9 * Register the shortcodes
10 */
11 public static function register() {
12 foreach ( get_class_methods( self::class ) as $shortcode ) {
13 if ( 'init' !== $shortcode ) {
14 add_shortcode( 'ap_' . $shortcode, array( self::class, $shortcode ) );
15 }
16 }
17 }
18
19 /**
20 * Unregister the shortcodes
21 */
22 public static function unregister() {
23 foreach ( get_class_methods( self::class ) as $shortcode ) {
24 if ( 'init' !== $shortcode ) {
25 remove_shortcode( 'ap_' . $shortcode );
26 }
27 }
28 }
29
30 /**
31 * Generates output for the 'ap_hashtags' shortcode
32 *
33 * @param array $atts The Shortcode attributes.
34 * @param string $content The ActivityPub post-content.
35 * @param string $tag The tag/name of the Shortcode.
36 *
37 * @return string The post tags as hashtags.
38 */
39 public static function hashtags( $atts, $content, $tag ) {
40 $item = self::get_item();
41
42 if ( ! $item ) {
43 return '';
44 }
45
46 $tags = \get_the_tags( $item->ID );
47
48 if ( ! $tags ) {
49 return '';
50 }
51
52 $hash_tags = array();
53
54 foreach ( $tags as $tag ) {
55 $hash_tags[] = \sprintf(
56 '<a rel="tag" class="hashtag u-tag u-category" href="%s">%s</a>',
57 \esc_url( \get_tag_link( $tag ) ),
58 esc_hashtag( $tag->name )
59 );
60 }
61
62 return \implode( ' ', $hash_tags );
63 }
64
65 /**
66 * Generates output for the 'ap_title' Shortcode
67 *
68 * @param array $atts The Shortcode attributes.
69 * @param string $content The ActivityPub post-content.
70 * @param string $tag The tag/name of the Shortcode.
71 *
72 * @return string The post title.
73 */
74 public static function title( $atts, $content, $tag ) {
75 $item = self::get_item();
76
77 if ( ! $item ) {
78 return '';
79 }
80
81 return \wp_strip_all_tags( \get_the_title( $item->ID ), true );
82 }
83
84 /**
85 * Generates output for the 'ap_excerpt' Shortcode
86 *
87 * @param array $atts The Shortcode attributes.
88 * @param string $content The ActivityPub post-content.
89 * @param string $tag The tag/name of the Shortcode.
90 *
91 * @return string The post excerpt.
92 */
93 public static function excerpt( $atts, $content, $tag ) {
94 $item = self::get_item();
95
96 if ( ! $item ) {
97 return '';
98 }
99
100 $atts = shortcode_atts(
101 array( 'length' => ACTIVITYPUB_EXCERPT_LENGTH ),
102 $atts,
103 $tag
104 );
105
106 $excerpt_length = intval( $atts['length'] );
107
108 if ( 0 === $excerpt_length ) {
109 $excerpt_length = ACTIVITYPUB_EXCERPT_LENGTH;
110 }
111
112 $excerpt = generate_post_summary( $item, $excerpt_length );
113
114 return \apply_filters( 'the_excerpt', $excerpt );
115 }
116
117 /**
118 * Generates output for the 'ap_content' Shortcode
119 *
120 * @param array $atts The Shortcode attributes.
121 * @param string $content The ActivityPub post-content.
122 * @param string $tag The tag/name of the Shortcode.
123 *
124 * @return string The post content.
125 */
126 public static function content( $atts, $content, $tag ) {
127 $item = self::get_item();
128
129 if ( ! $item ) {
130 return '';
131 }
132
133 // prevent inception
134 remove_shortcode( 'ap_content' );
135
136 $atts = shortcode_atts(
137 array( 'apply_filters' => 'yes' ),
138 $atts,
139 $tag
140 );
141
142 $content = '';
143
144 if ( 'attachment' === $item->post_type ) {
145 // get title of attachment with fallback to alt text.
146 $content = wp_get_attachment_caption( $item->ID );
147 if ( empty( $content ) ) {
148 $content = get_post_meta( $item->ID, '_wp_attachment_image_alt', true );
149 }
150 } else {
151 $content = \get_post_field( 'post_content', $item );
152
153 if ( 'yes' === $atts['apply_filters'] ) {
154 $content = \apply_filters( 'the_content', $content );
155 } else {
156 $content = do_blocks( $content );
157 $content = wptexturize( $content );
158 $content = wp_filter_content_tags( $content );
159 }
160
161 // replace script and style elements
162 $content = \preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $content );
163 $content = strip_shortcodes( $content );
164 $content = \trim( \preg_replace( '/[\n\r\t]/', '', $content ) );
165 }
166
167 add_shortcode( 'ap_content', array( 'Activitypub\Shortcodes', 'content' ) );
168
169 return $content;
170 }
171
172 /**
173 * Generates output for the 'ap_permalink' Shortcode
174 *
175 * @param array $atts The Shortcode attributes.
176 * @param string $content The ActivityPub post-content.
177 * @param string $tag The tag/name of the Shortcode.
178 *
179 * @return string The post permalink.
180 */
181 public static function permalink( $atts, $content, $tag ) {
182 $item = self::get_item();
183
184 if ( ! $item ) {
185 return '';
186 }
187
188 $atts = shortcode_atts(
189 array(
190 'type' => 'url',
191 ),
192 $atts,
193 $tag
194 );
195
196 if ( 'url' === $atts['type'] ) {
197 return \esc_url( \get_permalink( $item->ID ) );
198 }
199
200 return \sprintf(
201 '<a href="%1$s" class="status-link unhandled-link">%1$s</a>',
202 \esc_url( \get_permalink( $item->ID ) )
203 );
204 }
205
206 /**
207 * Generates output for the 'ap_shortlink' Shortcode
208 *
209 * @param array $atts The Shortcode attributes.
210 * @param string $content The ActivityPub post-content.
211 * @param string $tag The tag/name of the Shortcode.
212 *
213 * @return string The post shortlink.
214 */
215 public static function shortlink( $atts, $content, $tag ) {
216 $item = self::get_item();
217
218 if ( ! $item ) {
219 return '';
220 }
221
222 $atts = shortcode_atts(
223 array(
224 'type' => 'url',
225 ),
226 $atts,
227 $tag
228 );
229
230 if ( 'url' === $atts['type'] ) {
231 return \esc_url( \wp_get_shortlink( $item->ID ) );
232 }
233
234 return \sprintf(
235 '<a href="%1$s" class="status-link unhandled-link">%1$s</a>',
236 \esc_url( \wp_get_shortlink( $item->ID ) )
237 );
238 }
239
240 /**
241 * Generates output for the 'ap_image' Shortcode
242 *
243 * @param array $atts The Shortcode attributes.
244 * @param string $content The ActivityPub post-content.
245 * @param string $tag The tag/name of the Shortcode.
246 *
247 * @return string
248 */
249 public static function image( $atts, $content, $tag ) {
250 $item = self::get_item();
251
252 if ( ! $item ) {
253 return '';
254 }
255
256 $atts = shortcode_atts(
257 array(
258 'type' => 'full',
259 ),
260 $atts,
261 $tag
262 );
263
264 $size = 'full';
265
266 if ( in_array(
267 $atts['type'],
268 array( 'thumbnail', 'medium', 'large', 'full' ),
269 true
270 ) ) {
271 $size = $atts['type'];
272 }
273
274 $image = \get_the_post_thumbnail_url( $item->ID, $size );
275
276 if ( ! $image ) {
277 return '';
278 }
279
280 return \esc_url( $image );
281 }
282
283 /**
284 * Generates output for the 'ap_hashcats' Shortcode
285 *
286 * @param array $atts The Shortcode attributes.
287 * @param string $content The ActivityPub post-content.
288 * @param string $tag The tag/name of the Shortcode.
289 *
290 * @return string The post categories as hashtags.
291 */
292 public static function hashcats( $atts, $content, $tag ) {
293 $item = self::get_item();
294
295 if ( ! $item ) {
296 return '';
297 }
298
299 $categories = \get_the_category( $item->ID );
300
301 if ( ! $categories ) {
302 return '';
303 }
304
305 $hash_tags = array();
306
307 foreach ( $categories as $category ) {
308 $hash_tags[] = \sprintf(
309 '<a rel="tag" class="hashtag u-tag u-category" href="%s">%s</a>',
310 \esc_url( \get_category_link( $category ) ),
311 esc_hashtag( $category->name )
312 );
313 }
314
315 return \implode( ' ', $hash_tags );
316 }
317
318 /**
319 * Generates output for the 'ap_author' Shortcode
320 *
321 * @param array $atts The Shortcode attributes.
322 * @param string $content The ActivityPub post-content.
323 * @param string $tag The tag/name of the Shortcode.
324 *
325 * @return string The author name.
326 */
327 public static function author( $atts, $content, $tag ) {
328 $item = self::get_item();
329
330 if ( ! $item ) {
331 return '';
332 }
333
334 $author_id = \get_post_field( 'post_author', $item->ID );
335 $name = \get_the_author_meta( 'display_name', $author_id );
336
337 if ( ! $name ) {
338 return '';
339 }
340
341 return wp_strip_all_tags( $name );
342 }
343
344 /**
345 * Generates output for the 'ap_authorurl' Shortcode
346 *
347 * @param array $atts The Shortcode attributes.
348 * @param string $content The ActivityPub post-content.
349 * @param string $tag The tag/name of the Shortcode.
350 *
351 * @return string The author URL.
352 */
353 public static function authorurl( $atts, $content, $tag ) {
354 $item = self::get_item();
355
356 if ( ! $item ) {
357 return '';
358 }
359
360 $author_id = \get_post_field( 'post_author', $item->ID );
361 $url = \get_the_author_meta( 'user_url', $author_id );
362
363 if ( ! $url ) {
364 return '';
365 }
366
367 return \esc_url( $url );
368 }
369
370 /**
371 * Generates output for the 'ap_blogurl' Shortcode
372 *
373 * @param array $atts The Shortcode attributes.
374 * @param string $content The ActivityPub post-content.
375 * @param string $tag The tag/name of the Shortcode.
376 *
377 * @return string The site URL.
378 */
379 public static function blogurl( $atts, $content, $tag ) {
380 return \esc_url( \get_bloginfo( 'url' ) );
381 }
382
383 /**
384 * Generates output for the 'ap_blogname' Shortcode
385 *
386 * @param array $atts The Shortcode attributes.
387 * @param string $content The ActivityPub post-content.
388 * @param string $tag The tag/name of the Shortcode.
389 *
390 * @return string
391 */
392 public static function blogname( $atts, $content, $tag ) {
393 return \wp_strip_all_tags( \get_bloginfo( 'name' ) );
394 }
395
396 /**
397 * Generates output for the 'ap_blogdesc' Shortcode
398 *
399 * @param array $atts The Shortcode attributes.
400 * @param string $content The ActivityPub post-content.
401 * @param string $tag The tag/name of the Shortcode.
402 *
403 * @return string The site description.
404 */
405 public static function blogdesc( $atts, $content, $tag ) {
406 return \wp_strip_all_tags( \get_bloginfo( 'description' ) );
407 }
408
409 /**
410 * Generates output for the 'ap_date' Shortcode
411 *
412 * @param array $atts The Shortcode attributes.
413 * @param string $content The ActivityPub post-content.
414 * @param string $tag The tag/name of the Shortcode.
415 *
416 * @return string The post date.
417 */
418 public static function date( $atts, $content, $tag ) {
419 $item = self::get_item();
420
421 if ( ! $item ) {
422 return '';
423 }
424
425 $datetime = \get_post_datetime( $item );
426 $dateformat = \get_option( 'date_format' );
427 $timeformat = \get_option( 'time_format' );
428
429 $date = $datetime->format( $dateformat );
430
431 if ( ! $date ) {
432 return '';
433 }
434
435 return $date;
436 }
437
438 /**
439 * Generates output for the 'ap_time' Shortcode
440 *
441 * @param array $atts The Shortcode attributes.
442 * @param string $content The ActivityPub post-content.
443 * @param string $tag The tag/name of the Shortcode.
444 *
445 * @return string The post time.
446 */
447 public static function time( $atts, $content, $tag ) {
448 $item = self::get_item();
449
450 if ( ! $item ) {
451 return '';
452 }
453
454 $datetime = \get_post_datetime( $item );
455 $dateformat = \get_option( 'date_format' );
456 $timeformat = \get_option( 'time_format' );
457
458 $date = $datetime->format( $timeformat );
459
460 if ( ! $date ) {
461 return '';
462 }
463
464 return $date;
465 }
466
467 /**
468 * Generates output for the 'ap_datetime' Shortcode
469 *
470 * @param array $atts The Shortcode attributes.
471 * @param string $content The ActivityPub post-content.
472 * @param string $tag The tag/name of the Shortcode.
473 *
474 * @return string The post date/time.
475 */
476 public static function datetime( $atts, $content, $tag ) {
477 $item = self::get_item();
478
479 if ( ! $item ) {
480 return '';
481 }
482
483 $datetime = \get_post_datetime( $item );
484 $dateformat = \get_option( 'date_format' );
485 $timeformat = \get_option( 'time_format' );
486
487 $date = $datetime->format( $dateformat . ' @ ' . $timeformat );
488
489 if ( ! $date ) {
490 return '';
491 }
492
493 return $date;
494 }
495
496 /**
497 * Get a WordPress item to federate.
498 *
499 * Checks if item (WP_Post) is "public", a supported post type
500 * and not password protected.
501 *
502 * @return null|WP_Post The WordPress item.
503 */
504 protected static function get_item() {
505 $post = \get_post();
506
507 if ( ! $post ) {
508 return null;
509 }
510
511 if ( 'publish' !== \get_post_status( $post ) ) {
512 return null;
513 }
514
515 if ( \post_password_required( $post ) ) {
516 return null;
517 }
518
519 if ( ! \in_array( \get_post_type( $post ), \get_post_types_by_support( 'activitypub' ), true ) ) {
520 return null;
521 }
522
523 return $post;
524 }
525 }
526