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