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

495 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 = \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 $attributes 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( $attributes, $content, $tag ) {
211 $item = self::get_item();
212
213 if ( ! $item ) {
214 return '';
215 }
216
217 $attributes = shortcode_atts(
218 array(
219 'type' => 'url',
220 ),
221 $attributes,
222 $tag
223 );
224
225 if ( 'html' !== $attributes['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 $attributes 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( $attributes, $content, $tag ) {
245 $item = self::get_item();
246
247 if ( ! $item ) {
248 return '';
249 }
250
251 $attributes = shortcode_atts(
252 array(
253 'type' => 'url',
254 ),
255 $attributes,
256 $tag
257 );
258
259 if ( 'html' !== $attributes['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 $attributes 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( $attributes, $content, $tag ) {
279 $item = self::get_item();
280
281 if ( ! $item ) {
282 return '';
283 }
284
285 $attributes = shortcode_atts(
286 array(
287 'type' => 'full',
288 ),
289 $attributes,
290 $tag
291 );
292
293 $size = 'full';
294
295 if ( in_array(
296 $attributes['type'],
297 array( 'thumbnail', 'medium', 'large', 'full' ),
298 true
299 ) ) {
300 $size = $attributes['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 * @deprecated 7.0.0
316 *
317 * @return string The post categories as hashtags.
318 */
319 public static function hashcats() {
320 return '';
321 }
322
323 /**
324 * Generates output for the 'ap_author' Shortcode.
325 *
326 * @return string The author name.
327 */
328 public static function author() {
329 $item = self::get_item();
330
331 if ( ! $item ) {
332 return '';
333 }
334
335 $author_id = \get_post_field( 'post_author', $item->ID );
336 $name = \get_the_author_meta( 'display_name', $author_id );
337
338 if ( ! $name ) {
339 return '';
340 }
341
342 return wp_strip_all_tags( $name );
343 }
344
345 /**
346 * Generates output for the 'ap_authorurl' Shortcode.
347 *
348 * @return string The author URL.
349 */
350 public static function authorurl() {
351 $item = self::get_item();
352
353 if ( ! $item ) {
354 return '';
355 }
356
357 $author_id = \get_post_field( 'post_author', $item->ID );
358 $url = \get_the_author_meta( 'user_url', $author_id );
359
360 if ( ! $url ) {
361 return '';
362 }
363
364 return \esc_url( $url );
365 }
366
367 /**
368 * Generates output for the 'ap_blogurl' Shortcode.
369 *
370 * @return string The site URL.
371 */
372 public static function blogurl() {
373 return \esc_url( \get_bloginfo( 'url' ) );
374 }
375
376 /**
377 * Generates output for the 'ap_blogname' Shortcode.
378 *
379 * @return string
380 */
381 public static function blogname() {
382 return \wp_strip_all_tags( \get_bloginfo( 'name' ) );
383 }
384
385 /**
386 * Generates output for the 'ap_blogdesc' Shortcode.
387 *
388 * @return string The site description.
389 */
390 public static function blogdesc() {
391 return \wp_strip_all_tags( \get_bloginfo( 'description' ) );
392 }
393
394 /**
395 * Generates output for the 'ap_date' Shortcode.
396 *
397 * @return string The post date.
398 */
399 public static function date() {
400 $item = self::get_item();
401
402 if ( ! $item ) {
403 return '';
404 }
405
406 $datetime = \get_post_datetime( $item );
407 $dateformat = \get_option( 'date_format' );
408
409 $date = $datetime->format( $dateformat );
410
411 if ( ! $date ) {
412 return '';
413 }
414
415 return $date;
416 }
417
418 /**
419 * Generates output for the 'ap_time' Shortcode.
420 *
421 * @return string The post time.
422 */
423 public static function time() {
424 $item = self::get_item();
425
426 if ( ! $item ) {
427 return '';
428 }
429
430 $datetime = \get_post_datetime( $item );
431 $date = $datetime->format( \get_option( 'time_format' ) );
432
433 if ( ! $date ) {
434 return '';
435 }
436
437 return $date;
438 }
439
440 /**
441 * Generates output for the 'ap_datetime' Shortcode.
442 *
443 * @return string The post date/time.
444 */
445 public static function datetime() {
446 $item = self::get_item();
447
448 if ( ! $item ) {
449 return '';
450 }
451
452 $datetime = \get_post_datetime( $item );
453 $date_format = \get_option( 'date_format' );
454 $time_format = \get_option( 'time_format' );
455
456 $date = $datetime->format( $date_format . ' @ ' . $time_format );
457
458 if ( ! $date ) {
459 return '';
460 }
461
462 return $date;
463 }
464
465 /**
466 * Get a WordPress item to federate.
467 *
468 * Checks if item (WP_Post) is "public", a supported post type
469 * and not password protected.
470 *
471 * @return null|\WP_Post The WordPress item.
472 */
473 protected static function get_item() {
474 $post = \get_post();
475
476 if ( ! $post ) {
477 return null;
478 }
479
480 if ( 'publish' !== \get_post_status( $post ) ) {
481 return null;
482 }
483
484 if ( \post_password_required( $post ) ) {
485 return null;
486 }
487
488 if ( ! \in_array( \get_post_type( $post ), \get_post_types_by_support( 'activitypub' ), true ) ) {
489 return null;
490 }
491
492 return $post;
493 }
494 }
495