PluginProbe
ActivityPub / 5.7.0
ActivityPub v5.7.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 / class-shortcodes.php

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

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