PluginProbe
ActivityPub / 5.3.1
ActivityPub v5.3.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
← All changes | includes/class-shortcodes.php +83 -170 2.0.05.3.1 View file →
@@ -1,12 +1,19 @@
1 1 <?php
2 +/**
3 + * Shortcodes class file.
4 + *
5 + * @package Activitypub
6 + */
7 +
2 8 namespace Activitypub;
3 9
4 -use function Activitypub\esc_hashtag;
5 -
10 +/**
11 + * Shortcodes class.
12 + */
6 13 class Shortcodes {
7 14 /**
8 - * Register the shortcodes
15 + * Register the shortcodes.
9 16 */
10 17 public static function register() {
11 18 foreach ( get_class_methods( self::class ) as $shortcode ) {
12 19 if ( 'init' !== $shortcode ) {
@@ -15,9 +22,9 @@
15 22 }
16 23 }
17 24
18 25 /**
19 - * Unregister the shortcodes
26 + * Unregister the shortcodes.
20 27 */
21 28 public static function unregister() {
22 29 foreach ( get_class_methods( self::class ) as $shortcode ) {
23 30 if ( 'init' !== $shortcode ) {
@@ -26,17 +33,13 @@
26 33 }
27 34 }
28 35
29 36 /**
30 - * Generates output for the 'ap_hashtags' shortcode
37 + * Generates output for the 'ap_hashtags' shortcode.
31 38 *
32 - * @param array $atts The Shortcode attributes.
33 - * @param string $content The ActivityPub post-content.
34 - * @param string $tag The tag/name of the Shortcode.
35 - *
36 39 * @return string The post tags as hashtags.
37 40 */
38 - public static function hashtags( $atts, $content, $tag ) {
41 + public static function hashtags() {
39 42 $item = self::get_item();
40 43
41 44 if ( ! $item ) {
42 45 return '';
@@ -76,9 +79,25 @@
76 79 if ( ! $item ) {
77 80 return '';
78 81 }
79 82
80 - return \wp_strip_all_tags( \get_the_title( $item->ID ), true );
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 );
81 100 }
82 101
83 102 /**
84 103 * Generates output for the 'ap_excerpt' Shortcode
@@ -107,89 +126,16 @@
107 126 if ( 0 === $excerpt_length ) {
108 127 $excerpt_length = ACTIVITYPUB_EXCERPT_LENGTH;
109 128 }
110 129
111 - $excerpt = \get_post_field( 'post_excerpt', $item );
130 + $excerpt = generate_post_summary( $item, $excerpt_length );
112 131
113 - if ( 'attachment' === $item->post_type ) {
114 - // get title of attachment with fallback to alt text.
115 - $content = wp_get_attachment_caption( $item->ID );
116 - if ( empty( $content ) ) {
117 - $content = get_post_meta( $item->ID, '_wp_attachment_image_alt', true );
118 - }
119 - } elseif ( '' === $excerpt ) {
120 - $content = \get_post_field( 'post_content', $item );
121 -
122 - // An empty string will make wp_trim_excerpt do stuff we do not want.
123 - if ( '' !== $content ) {
124 - $excerpt = \strip_shortcodes( $content );
125 -
126 - /** This filter is documented in wp-includes/post-template.php */
127 - $excerpt = \apply_filters( 'the_content', $excerpt );
128 - $excerpt = \str_replace( ']]>', ']]&gt;', $excerpt );
129 - }
130 - }
131 -
132 - // Strip out any remaining tags.
133 - $excerpt = \wp_strip_all_tags( $excerpt );
134 -
135 - $excerpt_more = \apply_filters( 'activitypub_excerpt_more', ' [&hellip;]' );
136 - $excerpt_more_len = strlen( $excerpt_more );
137 -
138 - // We now have a excerpt, but we need to check it's length, it may be longer than we want for two reasons:
139 - //
140 - // * The user has entered a manual excerpt which is longer that what we want.
141 - // * No manual excerpt exists so we've used the content which might be longer than we want.
142 - //
143 - // Either way, let's trim it up if we need too. Also, don't forget to take into account the more indicator
144 - // as part of the total length.
145 - //
146 -
147 - // Setup a variable to hold the current excerpts length.
148 - $current_excerpt_length = strlen( $excerpt );
149 -
150 - // Setup a variable to keep track of our target length.
151 - $target_excerpt_length = $excerpt_length - $excerpt_more_len;
152 -
153 - // Setup a variable to keep track of the current max length.
154 - $current_excerpt_max = $target_excerpt_length;
155 -
156 - // This is a loop since we can't calculate word break the string after 'the_excpert' filter has run (we would break
157 - // all kinds of html tags), so we have to cut the excerpt down a bit at a time until we hit our target length.
158 - while ( $current_excerpt_length > $target_excerpt_length && $current_excerpt_max > 0 ) {
159 - // Trim the excerpt based on wordwrap() positioning.
160 - // Note: we're using <br> as the linebreak just in case there are any newlines existing in the excerpt from the user.
161 - // There won't be any <br> left after we've run wp_strip_all_tags() in the code above, so they're
162 - // safe to use here. It won't be included in the final excerpt as the substr() will trim it off.
163 - $excerpt = substr( $excerpt, 0, strpos( wordwrap( $excerpt, $current_excerpt_max, '<br>' ), '<br>' ) );
164 -
165 - // If something went wrong, or we're in a language that wordwrap() doesn't understand,
166 - // just chop it off and don't worry about breaking in the middle of a word.
167 - if ( strlen( $excerpt ) > $excerpt_length - $excerpt_more_len ) {
168 - $excerpt = substr( $excerpt, 0, $current_excerpt_max );
169 - }
170 -
171 - // Add in the more indicator.
172 - $excerpt = $excerpt . $excerpt_more;
173 -
174 - // Run it through the excerpt filter which will add some html tags back in.
175 - $excerpt_filtered = apply_filters( 'the_excerpt', $excerpt );
176 -
177 - // Now set the current excerpt length to this new filtered length.
178 - $current_excerpt_length = strlen( $excerpt_filtered );
179 -
180 - // Check to see if we're over the target length.
181 - if ( $current_excerpt_length > $target_excerpt_length ) {
182 - // If so, remove 20 characters from the current max and run the loop again.
183 - $current_excerpt_max = $current_excerpt_max - 20;
184 - }
185 - }
186 -
132 + /** This filter is documented in wp-includes/post-template.php */
187 133 return \apply_filters( 'the_excerpt', $excerpt );
188 134 }
189 135
190 136 /**
191 - * Generates output for the 'ap_content' Shortcode
137 + * Generates output for the 'ap_content' Shortcode.
192 138 *
193 139 * @param array $atts The Shortcode attributes.
194 140 * @param string $content The ActivityPub post-content.
195 141 * @param string $tag The tag/name of the Shortcode.
@@ -202,9 +148,9 @@
202 148 if ( ! $item ) {
203 149 return '';
204 150 }
205 151
206 - // prevent inception
152 + // Prevent inception.
207 153 remove_shortcode( 'ap_content' );
208 154
209 155 $atts = shortcode_atts(
210 156 array( 'apply_filters' => 'yes' ),
@@ -214,29 +160,34 @@
214 160
215 161 $content = '';
216 162
217 163 if ( 'attachment' === $item->post_type ) {
218 - // get title of attachment with fallback to alt text.
164 + // Get title of attachment with fallback to alt text.
219 165 $content = wp_get_attachment_caption( $item->ID );
220 166 if ( empty( $content ) ) {
221 167 $content = get_post_meta( $item->ID, '_wp_attachment_image_alt', true );
222 168 }
223 - } else {
169 + }
170 +
171 + if ( empty( $content ) ) {
224 172 $content = \get_post_field( 'post_content', $item );
173 + }
225 174
226 - if ( 'yes' === $atts['apply_filters'] ) {
227 - $content = \apply_filters( 'the_content', $content );
228 - } else {
229 - $content = do_blocks( $content );
230 - $content = wptexturize( $content );
231 - $content = wp_filter_content_tags( $content );
175 + if ( 'yes' === $atts['apply_filters'] ) {
176 + /** This filter is documented in wp-includes/post-template.php */
177 + $content = \apply_filters( 'the_content', $content );
178 + } else {
179 + if ( site_supports_blocks() ) {
180 + $content = \do_blocks( $content );
232 181 }
182 + $content = \wptexturize( $content );
183 + $content = \wp_filter_content_tags( $content );
184 + }
233 185
234 - // replace script and style elements
235 - $content = \preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $content );
236 - $content = strip_shortcodes( $content );
237 - $content = \trim( \preg_replace( '/[\n\r\t]/', '', $content ) );
238 - }
186 + // Replace script and style elements.
187 + $content = \preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $content );
188 + $content = \strip_shortcodes( $content );
189 + $content = \trim( \preg_replace( '/[\n\r\t]/', '', $content ) );
239 190
240 191 add_shortcode( 'ap_content', array( 'Activitypub\Shortcodes', 'content' ) );
241 192
242 193 return $content;
@@ -242,9 +193,9 @@
242 193 return $content;
243 194 }
244 195
245 196 /**
246 - * Generates output for the 'ap_permalink' Shortcode
197 + * Generates output for the 'ap_permalink' Shortcode.
247 198 *
248 199 * @param array $atts The Shortcode attributes.
249 200 * @param string $content The ActivityPub post-content.
250 201 * @param string $tag The tag/name of the Shortcode.
@@ -265,20 +216,20 @@
265 216 $atts,
266 217 $tag
267 218 );
268 219
269 - if ( 'url' === $atts['type'] ) {
220 + if ( 'html' !== $atts['type'] ) {
270 221 return \esc_url( \get_permalink( $item->ID ) );
271 222 }
272 223
273 224 return \sprintf(
274 - '<a href="%1$s">%1$s</a>',
225 + '<a href="%1$s" class="status-link unhandled-link">%1$s</a>',
275 226 \esc_url( \get_permalink( $item->ID ) )
276 227 );
277 228 }
278 229
279 230 /**
280 - * Generates output for the 'ap_shortlink' Shortcode
231 + * Generates output for the 'ap_shortlink' Shortcode.
281 232 *
282 233 * @param array $atts The Shortcode attributes.
283 234 * @param string $content The ActivityPub post-content.
284 235 * @param string $tag The tag/name of the Shortcode.
@@ -299,20 +250,20 @@
299 250 $atts,
300 251 $tag
301 252 );
302 253
303 - if ( 'url' === $atts['type'] ) {
254 + if ( 'html' !== $atts['type'] ) {
304 255 return \esc_url( \wp_get_shortlink( $item->ID ) );
305 256 }
306 257
307 258 return \sprintf(
308 - '<a href="%1$s">%1$s</a>',
259 + '<a href="%1$s" class="status-link unhandled-link">%1$s</a>',
309 260 \esc_url( \wp_get_shortlink( $item->ID ) )
310 261 );
311 262 }
312 263
313 264 /**
314 - * Generates output for the 'ap_image' Shortcode
265 + * Generates output for the 'ap_image' Shortcode.
315 266 *
316 267 * @param array $atts The Shortcode attributes.
317 268 * @param string $content The ActivityPub post-content.
318 269 * @param string $tag The tag/name of the Shortcode.
@@ -353,17 +304,13 @@
353 304 return \esc_url( $image );
354 305 }
355 306
356 307 /**
357 - * Generates output for the 'ap_hashcats' Shortcode
308 + * Generates output for the 'ap_hashcats' Shortcode.
358 309 *
359 - * @param array $atts The Shortcode attributes.
360 - * @param string $content The ActivityPub post-content.
361 - * @param string $tag The tag/name of the Shortcode.
362 - *
363 310 * @return string The post categories as hashtags.
364 311 */
365 - public static function hashcats( $atts, $content, $tag ) {
312 + public static function hashcats() {
366 313 $item = self::get_item();
367 314
368 315 if ( ! $item ) {
369 316 return '';
@@ -388,17 +335,13 @@
388 335 return \implode( ' ', $hash_tags );
389 336 }
390 337
391 338 /**
392 - * Generates output for the 'ap_author' Shortcode
339 + * Generates output for the 'ap_author' Shortcode.
393 340 *
394 - * @param array $atts The Shortcode attributes.
395 - * @param string $content The ActivityPub post-content.
396 - * @param string $tag The tag/name of the Shortcode.
397 - *
398 341 * @return string The author name.
399 342 */
400 - public static function author( $atts, $content, $tag ) {
343 + public static function author() {
401 344 $item = self::get_item();
402 345
403 346 if ( ! $item ) {
404 347 return '';
@@ -404,9 +347,9 @@
404 347 return '';
405 348 }
406 349
407 350 $author_id = \get_post_field( 'post_author', $item->ID );
408 - $name = \get_the_author_meta( 'display_name', $author_id );
351 + $name = \get_the_author_meta( 'display_name', $author_id );
409 352
410 353 if ( ! $name ) {
411 354 return '';
412 355 }
@@ -414,17 +357,13 @@
414 357 return wp_strip_all_tags( $name );
415 358 }
416 359
417 360 /**
418 - * Generates output for the 'ap_authorurl' Shortcode
361 + * Generates output for the 'ap_authorurl' Shortcode.
419 362 *
420 - * @param array $atts The Shortcode attributes.
421 - * @param string $content The ActivityPub post-content.
422 - * @param string $tag The tag/name of the Shortcode.
423 - *
424 363 * @return string The author URL.
425 364 */
426 - public static function authorurl( $atts, $content, $tag ) {
365 + public static function authorurl() {
427 366 $item = self::get_item();
428 367
429 368 if ( ! $item ) {
430 369 return '';
@@ -430,9 +369,9 @@
430 369 return '';
431 370 }
432 371
433 372 $author_id = \get_post_field( 'post_author', $item->ID );
434 - $url = \get_the_author_meta( 'user_url', $author_id );
373 + $url = \get_the_author_meta( 'user_url', $author_id );
435 374
436 375 if ( ! $url ) {
437 376 return '';
438 377 }
@@ -440,56 +379,40 @@
440 379 return \esc_url( $url );
441 380 }
442 381
443 382 /**
444 - * Generates output for the 'ap_blogurl' Shortcode
383 + * Generates output for the 'ap_blogurl' Shortcode.
445 384 *
446 - * @param array $atts The Shortcode attributes.
447 - * @param string $content The ActivityPub post-content.
448 - * @param string $tag The tag/name of the Shortcode.
449 - *
450 385 * @return string The site URL.
451 386 */
452 - public static function blogurl( $atts, $content, $tag ) {
387 + public static function blogurl() {
453 388 return \esc_url( \get_bloginfo( 'url' ) );
454 389 }
455 390
456 391 /**
457 - * Generates output for the 'ap_blogname' Shortcode
392 + * Generates output for the 'ap_blogname' Shortcode.
458 393 *
459 - * @param array $atts The Shortcode attributes.
460 - * @param string $content The ActivityPub post-content.
461 - * @param string $tag The tag/name of the Shortcode.
462 - *
463 394 * @return string
464 395 */
465 - public static function blogname( $atts, $content, $tag ) {
396 + public static function blogname() {
466 397 return \wp_strip_all_tags( \get_bloginfo( 'name' ) );
467 398 }
468 399
469 400 /**
470 - * Generates output for the 'ap_blogdesc' Shortcode
401 + * Generates output for the 'ap_blogdesc' Shortcode.
471 402 *
472 - * @param array $atts The Shortcode attributes.
473 - * @param string $content The ActivityPub post-content.
474 - * @param string $tag The tag/name of the Shortcode.
475 - *
476 403 * @return string The site description.
477 404 */
478 - public static function blogdesc( $atts, $content, $tag ) {
405 + public static function blogdesc() {
479 406 return \wp_strip_all_tags( \get_bloginfo( 'description' ) );
480 407 }
481 408
482 409 /**
483 - * Generates output for the 'ap_date' Shortcode
410 + * Generates output for the 'ap_date' Shortcode.
484 411 *
485 - * @param array $atts The Shortcode attributes.
486 - * @param string $content The ActivityPub post-content.
487 - * @param string $tag The tag/name of the Shortcode.
488 - *
489 412 * @return string The post date.
490 413 */
491 - public static function date( $atts, $content, $tag ) {
414 + public static function date() {
492 415 $item = self::get_item();
493 416
494 417 if ( ! $item ) {
495 418 return '';
@@ -494,11 +417,10 @@
494 417 if ( ! $item ) {
495 418 return '';
496 419 }
497 420
498 - $datetime = \get_post_datetime( $item );
421 + $datetime = \get_post_datetime( $item );
499 422 $dateformat = \get_option( 'date_format' );
500 - $timeformat = \get_option( 'time_format' );
501 423
502 424 $date = $datetime->format( $dateformat );
503 425
504 426 if ( ! $date ) {
@@ -508,17 +430,13 @@
508 430 return $date;
509 431 }
510 432
511 433 /**
512 - * Generates output for the 'ap_time' Shortcode
434 + * Generates output for the 'ap_time' Shortcode.
513 435 *
514 - * @param array $atts The Shortcode attributes.
515 - * @param string $content The ActivityPub post-content.
516 - * @param string $tag The tag/name of the Shortcode.
517 - *
518 436 * @return string The post time.
519 437 */
520 - public static function time( $atts, $content, $tag ) {
438 + public static function time() {
521 439 $item = self::get_item();
522 440
523 441 if ( ! $item ) {
524 442 return '';
@@ -523,10 +441,9 @@
523 441 if ( ! $item ) {
524 442 return '';
525 443 }
526 444
527 - $datetime = \get_post_datetime( $item );
528 - $dateformat = \get_option( 'date_format' );
445 + $datetime = \get_post_datetime( $item );
529 446 $timeformat = \get_option( 'time_format' );
530 447
531 448 $date = $datetime->format( $timeformat );
532 449
@@ -537,17 +454,13 @@
537 454 return $date;
538 455 }
539 456
540 457 /**
541 - * Generates output for the 'ap_datetime' Shortcode
458 + * Generates output for the 'ap_datetime' Shortcode.
542 459 *
543 - * @param array $atts The Shortcode attributes.
544 - * @param string $content The ActivityPub post-content.
545 - * @param string $tag The tag/name of the Shortcode.
546 - *
547 460 * @return string The post date/time.
548 461 */
549 - public static function datetime( $atts, $content, $tag ) {
462 + public static function datetime() {
550 463 $item = self::get_item();
551 464
552 465 if ( ! $item ) {
553 466 return '';
@@ -552,9 +465,9 @@
552 465 if ( ! $item ) {
553 466 return '';
554 467 }
555 468
556 - $datetime = \get_post_datetime( $item );
469 + $datetime = \get_post_datetime( $item );
557 470 $dateformat = \get_option( 'date_format' );
558 471 $timeformat = \get_option( 'time_format' );
559 472
560 473 $date = $datetime->format( $dateformat . ' @ ' . $timeformat );
@@ -571,9 +484,9 @@
571 484 *
572 485 * Checks if item (WP_Post) is "public", a supported post type
573 486 * and not password protected.
574 487 *
575 - * @return null|WP_Post The WordPress item.
488 + * @return null|\WP_Post The WordPress item.
576 489 */
577 490 protected static function get_item() {
578 491 $post = \get_post();
579 492