PluginProbe
ActivityPub / 7.8.2
ActivityPub v7.8.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 7.8.2, at includes/class-shortcodes.php

496 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 // 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 $attributes 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( $attributes, $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 $attributes = shortcode_atts(
95 array( 'type' => 'plain' ),
96 $attributes,
97 $tag
98 );
99
100 if ( 'html' !== $attributes['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 $attributes 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( $attributes, $content, $tag ) {
117 $item = self::get_item();
118
119 if ( ! $item ) {
120 return '';
121 }
122
123 $attributes = shortcode_atts(
124 array( 'length' => ACTIVITYPUB_EXCERPT_LENGTH ),
125 $attributes,
126 $tag
127 );
128
129 $excerpt_length = intval( $attributes['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 $attributes 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( $attributes, $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 $attributes = shortcode_atts(
161 array( 'apply_filters' => 'yes' ),
162 $attributes,
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' === $attributes['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 = Sanitize::clean_html( $content );
195 $content = Sanitize::strip_whitespace( $content );
196
197 add_shortcode( 'ap_content', array( 'Activitypub\Shortcodes', 'content' ) );
198
199 return $content;
200 }
201
202 /**
203 * Generates output for the 'ap_permalink' Shortcode.
204 *
205 * @param array $attributes The Shortcode attributes.
206 * @param string $content The ActivityPub post-content.
207 * @param string $tag The tag/name of the Shortcode.
208 *
209 * @return string The post permalink.
210 */
211 public static function permalink( $attributes, $content, $tag ) {
212 $item = self::get_item();
213
214 if ( ! $item ) {
215 return '';
216 }
217
218 $attributes = shortcode_atts(
219 array(
220 'type' => 'url',
221 ),
222 $attributes,
223 $tag
224 );
225
226 if ( 'html' !== $attributes['type'] ) {
227 return \esc_url( \get_permalink( $item->ID ) );
228 }
229
230 return \sprintf(
231 '<a href="%1$s" class="status-link unhandled-link">%1$s</a>',
232 \esc_url( \get_permalink( $item->ID ) )
233 );
234 }
235
236 /**
237 * Generates output for the 'ap_shortlink' Shortcode.
238 *
239 * @param array $attributes The Shortcode attributes.
240 * @param string $content The ActivityPub post-content.
241 * @param string $tag The tag/name of the Shortcode.
242 *
243 * @return string The post shortlink.
244 */
245 public static function shortlink( $attributes, $content, $tag ) {
246 $item = self::get_item();
247
248 if ( ! $item ) {
249 return '';
250 }
251
252 $attributes = shortcode_atts(
253 array(
254 'type' => 'url',
255 ),
256 $attributes,
257 $tag
258 );
259
260 if ( 'html' !== $attributes['type'] ) {
261 return \esc_url( \wp_get_shortlink( $item->ID ) );
262 }
263
264 return \sprintf(
265 '<a href="%1$s" class="status-link unhandled-link">%1$s</a>',
266 \esc_url( \wp_get_shortlink( $item->ID ) )
267 );
268 }
269
270 /**
271 * Generates output for the 'ap_image' Shortcode.
272 *
273 * @param array $attributes The Shortcode attributes.
274 * @param string $content The ActivityPub post-content.
275 * @param string $tag The tag/name of the Shortcode.
276 *
277 * @return string
278 */
279 public static function image( $attributes, $content, $tag ) {
280 $item = self::get_item();
281
282 if ( ! $item ) {
283 return '';
284 }
285
286 $attributes = shortcode_atts(
287 array(
288 'type' => 'full',
289 ),
290 $attributes,
291 $tag
292 );
293
294 $size = 'full';
295
296 if ( in_array(
297 $attributes['type'],
298 array( 'thumbnail', 'medium', 'large', 'full' ),
299 true
300 ) ) {
301 $size = $attributes['type'];
302 }
303
304 $image = \get_the_post_thumbnail_url( $item->ID, $size );
305
306 if ( ! $image ) {
307 return '';
308 }
309
310 return \esc_url( $image );
311 }
312
313 /**
314 * Generates output for the 'ap_hashcats' Shortcode.
315 *
316 * @deprecated 7.0.0
317 *
318 * @return string The post categories as hashtags.
319 */
320 public static function hashcats() {
321 return '';
322 }
323
324 /**
325 * Generates output for the 'ap_author' Shortcode.
326 *
327 * @return string The author name.
328 */
329 public static function author() {
330 $item = self::get_item();
331
332 if ( ! $item ) {
333 return '';
334 }
335
336 $author_id = \get_post_field( 'post_author', $item->ID );
337 $name = \get_the_author_meta( 'display_name', $author_id );
338
339 if ( ! $name ) {
340 return '';
341 }
342
343 return wp_strip_all_tags( $name );
344 }
345
346 /**
347 * Generates output for the 'ap_authorurl' Shortcode.
348 *
349 * @return string The author URL.
350 */
351 public static function authorurl() {
352 $item = self::get_item();
353
354 if ( ! $item ) {
355 return '';
356 }
357
358 $author_id = \get_post_field( 'post_author', $item->ID );
359 $url = \get_the_author_meta( 'user_url', $author_id );
360
361 if ( ! $url ) {
362 return '';
363 }
364
365 return \esc_url( $url );
366 }
367
368 /**
369 * Generates output for the 'ap_blogurl' Shortcode.
370 *
371 * @return string The site URL.
372 */
373 public static function blogurl() {
374 return \esc_url( \get_bloginfo( 'url' ) );
375 }
376
377 /**
378 * Generates output for the 'ap_blogname' Shortcode.
379 *
380 * @return string
381 */
382 public static function blogname() {
383 return \wp_strip_all_tags( \get_bloginfo( 'name' ) );
384 }
385
386 /**
387 * Generates output for the 'ap_blogdesc' Shortcode.
388 *
389 * @return string The site description.
390 */
391 public static function blogdesc() {
392 return \wp_strip_all_tags( \get_bloginfo( 'description' ) );
393 }
394
395 /**
396 * Generates output for the 'ap_date' Shortcode.
397 *
398 * @return string The post date.
399 */
400 public static function date() {
401 $item = self::get_item();
402
403 if ( ! $item ) {
404 return '';
405 }
406
407 $datetime = \get_post_datetime( $item );
408 $dateformat = \get_option( 'date_format' );
409
410 $date = $datetime->format( $dateformat );
411
412 if ( ! $date ) {
413 return '';
414 }
415
416 return $date;
417 }
418
419 /**
420 * Generates output for the 'ap_time' Shortcode.
421 *
422 * @return string The post time.
423 */
424 public static function time() {
425 $item = self::get_item();
426
427 if ( ! $item ) {
428 return '';
429 }
430
431 $datetime = \get_post_datetime( $item );
432 $date = $datetime->format( \get_option( 'time_format' ) );
433
434 if ( ! $date ) {
435 return '';
436 }
437
438 return $date;
439 }
440
441 /**
442 * Generates output for the 'ap_datetime' Shortcode.
443 *
444 * @return string The post date/time.
445 */
446 public static function datetime() {
447 $item = self::get_item();
448
449 if ( ! $item ) {
450 return '';
451 }
452
453 $datetime = \get_post_datetime( $item );
454 $date_format = \get_option( 'date_format' );
455 $time_format = \get_option( 'time_format' );
456
457 $date = $datetime->format( $date_format . ' @ ' . $time_format );
458
459 if ( ! $date ) {
460 return '';
461 }
462
463 return $date;
464 }
465
466 /**
467 * Get a WordPress item to federate.
468 *
469 * Checks if item (WP_Post) is "public", a supported post type
470 * and not password protected.
471 *
472 * @return null|\WP_Post The WordPress item.
473 */
474 protected static function get_item() {
475 $post = \get_post();
476
477 if ( ! $post ) {
478 return null;
479 }
480
481 if ( 'publish' !== \get_post_status( $post ) ) {
482 return null;
483 }
484
485 if ( \post_password_required( $post ) ) {
486 return null;
487 }
488
489 if ( ! \in_array( \get_post_type( $post ), \get_post_types_by_support( 'activitypub' ), true ) ) {
490 return null;
491 }
492
493 return $post;
494 }
495 }
496