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