PluginProbe
ActivityPub / 4.1.1
ActivityPub v4.1.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 / class-shortcodes.php

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

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