PluginProbe
ActivityPub / trunk
ActivityPub vtrunk
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 trunk, at includes/class-shortcodes.php

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