PluginProbe
ActivityPub / 4.7.1
ActivityPub v4.7.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.7.1, at includes/class-shortcodes.php

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