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

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