2023-01-22 07:13:46 +01:00
|
|
|
<?php
|
|
|
|
namespace Activitypub;
|
|
|
|
|
|
|
|
class Shortcodes {
|
2023-01-22 17:27:13 +01:00
|
|
|
/**
|
2023-05-23 11:26:12 +02:00
|
|
|
* Class constructor, registering WordPress then Shortcodes
|
2023-01-22 17:27:13 +01:00
|
|
|
*/
|
2023-01-23 17:59:13 +01:00
|
|
|
public static function init() {
|
2023-07-17 15:25:30 +02:00
|
|
|
// do not load on admin pages
|
|
|
|
if ( is_admin() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-10 14:18:56 +02:00
|
|
|
foreach ( get_class_methods( self::class ) as $shortcode ) {
|
2023-01-23 20:08:06 +01:00
|
|
|
if ( 'init' !== $shortcode ) {
|
2023-05-10 14:18:56 +02:00
|
|
|
add_shortcode( 'ap_' . $shortcode, array( self::class, $shortcode ) );
|
2023-01-22 07:13:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-05-23 11:26:12 +02:00
|
|
|
* Generates output for the 'ap_hashtags' shortcode
|
2023-01-22 07:13:46 +01:00
|
|
|
*
|
2023-05-23 11:26:12 +02:00
|
|
|
* @param array $atts The Shortcode attributes.
|
|
|
|
* @param string $content The ActivityPub post-content.
|
|
|
|
* @param string $tag The tag/name of the Shortcode.
|
2023-01-22 07:13:46 +01:00
|
|
|
*
|
2023-05-23 11:26:12 +02:00
|
|
|
* @return string The post tags as hashtags.
|
2023-01-22 07:13:46 +01:00
|
|
|
*/
|
2023-01-23 17:59:13 +01:00
|
|
|
public static function hashtags( $atts, $content, $tag ) {
|
2023-07-18 08:20:09 +02:00
|
|
|
$item = self::get_item();
|
2023-01-23 17:59:13 +01:00
|
|
|
|
2023-07-17 17:23:13 +02:00
|
|
|
if ( ! $item ) {
|
2023-01-22 07:13:46 +01:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2023-07-17 17:23:13 +02:00
|
|
|
$tags = \get_the_tags( $item->ID );
|
2023-01-22 07:13:46 +01:00
|
|
|
|
|
|
|
if ( ! $tags ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$hash_tags = array();
|
|
|
|
|
|
|
|
foreach ( $tags as $tag ) {
|
2023-01-23 20:24:03 +01:00
|
|
|
$hash_tags[] = \sprintf(
|
|
|
|
'<a rel="tag" class="u-tag u-category" href="%s">#%s</a>',
|
2023-05-11 14:20:35 +02:00
|
|
|
\esc_url( \get_tag_link( $tag ) ),
|
2023-05-23 11:10:05 +02:00
|
|
|
\wp_strip_all_tags( $tag->slug )
|
2023-01-23 20:24:03 +01:00
|
|
|
);
|
2023-01-22 07:13:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return \implode( ' ', $hash_tags );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-05-23 11:26:12 +02:00
|
|
|
* Generates output for the 'ap_title' Shortcode
|
2023-01-22 07:13:46 +01:00
|
|
|
*
|
2023-05-23 11:26:12 +02:00
|
|
|
* @param array $atts The Shortcode attributes.
|
|
|
|
* @param string $content The ActivityPub post-content.
|
|
|
|
* @param string $tag The tag/name of the Shortcode.
|
2023-01-22 07:13:46 +01:00
|
|
|
*
|
2023-05-23 11:26:12 +02:00
|
|
|
* @return string The post title.
|
2023-01-22 07:13:46 +01:00
|
|
|
*/
|
2023-01-23 17:59:13 +01:00
|
|
|
public static function title( $atts, $content, $tag ) {
|
2023-07-18 08:20:09 +02:00
|
|
|
$item = self::get_item();
|
2023-01-23 17:59:13 +01:00
|
|
|
|
2023-07-17 17:23:13 +02:00
|
|
|
if ( ! $item ) {
|
2023-01-22 07:13:46 +01:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2023-07-17 17:23:13 +02:00
|
|
|
return \wp_strip_all_tags( \get_the_title( $item->ID ), true );
|
2023-01-23 19:43:34 +01:00
|
|
|
|
2023-01-22 07:13:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-05-23 11:26:12 +02:00
|
|
|
* Generates output for the 'ap_excerpt' Shortcode
|
2023-01-22 07:13:46 +01:00
|
|
|
*
|
2023-05-23 11:26:12 +02:00
|
|
|
* @param array $atts The Shortcode attributes.
|
|
|
|
* @param string $content The ActivityPub post-content.
|
|
|
|
* @param string $tag The tag/name of the Shortcode.
|
2023-01-22 07:13:46 +01:00
|
|
|
*
|
2023-05-23 11:26:12 +02:00
|
|
|
* @return string The post excerpt.
|
2023-01-22 07:13:46 +01:00
|
|
|
*/
|
2023-01-23 17:59:13 +01:00
|
|
|
public static function excerpt( $atts, $content, $tag ) {
|
2023-07-18 08:20:09 +02:00
|
|
|
$item = self::get_item();
|
2023-01-23 17:59:13 +01:00
|
|
|
|
2023-07-17 17:23:13 +02:00
|
|
|
if ( ! $item ) {
|
2023-01-22 07:13:46 +01:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2023-01-23 20:24:03 +01:00
|
|
|
$atts = shortcode_atts(
|
|
|
|
array( 'length' => ACTIVITYPUB_EXCERPT_LENGTH ),
|
|
|
|
$atts,
|
|
|
|
$tag
|
|
|
|
);
|
2023-01-22 07:13:46 +01:00
|
|
|
|
2023-01-23 21:13:50 +01:00
|
|
|
$excerpt_length = intval( $atts['length'] );
|
2023-01-22 07:13:46 +01:00
|
|
|
|
2023-01-23 21:13:50 +01:00
|
|
|
if ( 0 === $excerpt_length ) {
|
|
|
|
$excerpt_length = ACTIVITYPUB_EXCERPT_LENGTH;
|
2023-01-23 20:24:03 +01:00
|
|
|
}
|
2023-01-22 07:13:46 +01:00
|
|
|
|
2023-07-17 17:23:13 +02:00
|
|
|
$excerpt = \get_post_field( 'post_excerpt', $item );
|
2023-01-22 07:13:46 +01:00
|
|
|
|
|
|
|
if ( '' === $excerpt ) {
|
|
|
|
|
2023-07-17 17:23:13 +02:00
|
|
|
$content = \get_post_field( 'post_content', $item );
|
2023-01-22 07:13:46 +01:00
|
|
|
|
|
|
|
// An empty string will make wp_trim_excerpt do stuff we do not want.
|
|
|
|
if ( '' !== $content ) {
|
|
|
|
$excerpt = \strip_shortcodes( $content );
|
|
|
|
|
|
|
|
/** This filter is documented in wp-includes/post-template.php */
|
|
|
|
$excerpt = \apply_filters( 'the_content', $excerpt );
|
|
|
|
$excerpt = \str_replace( ']]>', ']]>', $excerpt );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strip out any remaining tags.
|
|
|
|
$excerpt = \wp_strip_all_tags( $excerpt );
|
|
|
|
|
|
|
|
/** This filter is documented in wp-includes/formatting.php */
|
|
|
|
$excerpt_more = \apply_filters( 'excerpt_more', ' [...]' );
|
|
|
|
$excerpt_more_len = strlen( $excerpt_more );
|
|
|
|
|
|
|
|
// We now have a excerpt, but we need to check it's length, it may be longer than we want for two reasons:
|
|
|
|
//
|
|
|
|
// * The user has entered a manual excerpt which is longer that what we want.
|
|
|
|
// * No manual excerpt exists so we've used the content which might be longer than we want.
|
|
|
|
//
|
|
|
|
// Either way, let's trim it up if we need too. Also, don't forget to take into account the more indicator
|
|
|
|
// as part of the total length.
|
|
|
|
//
|
|
|
|
|
|
|
|
// Setup a variable to hold the current excerpts length.
|
|
|
|
$current_excerpt_length = strlen( $excerpt );
|
|
|
|
|
|
|
|
// Setup a variable to keep track of our target length.
|
|
|
|
$target_excerpt_length = $excerpt_length - $excerpt_more_len;
|
|
|
|
|
|
|
|
// Setup a variable to keep track of the current max length.
|
|
|
|
$current_excerpt_max = $target_excerpt_length;
|
|
|
|
|
|
|
|
// This is a loop since we can't calculate word break the string after 'the_excpert' filter has run (we would break
|
|
|
|
// all kinds of html tags), so we have to cut the excerpt down a bit at a time until we hit our target length.
|
|
|
|
while ( $current_excerpt_length > $target_excerpt_length && $current_excerpt_max > 0 ) {
|
|
|
|
// Trim the excerpt based on wordwrap() positioning.
|
|
|
|
// Note: we're using <br> as the linebreak just in case there are any newlines existing in the excerpt from the user.
|
|
|
|
// There won't be any <br> left after we've run wp_strip_all_tags() in the code above, so they're
|
|
|
|
// safe to use here. It won't be included in the final excerpt as the substr() will trim it off.
|
|
|
|
$excerpt = substr( $excerpt, 0, strpos( wordwrap( $excerpt, $current_excerpt_max, '<br>' ), '<br>' ) );
|
|
|
|
|
|
|
|
// If something went wrong, or we're in a language that wordwrap() doesn't understand,
|
|
|
|
// just chop it off and don't worry about breaking in the middle of a word.
|
|
|
|
if ( strlen( $excerpt ) > $excerpt_length - $excerpt_more_len ) {
|
|
|
|
$excerpt = substr( $excerpt, 0, $current_excerpt_max );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add in the more indicator.
|
|
|
|
$excerpt = $excerpt . $excerpt_more;
|
|
|
|
|
|
|
|
// Run it through the excerpt filter which will add some html tags back in.
|
|
|
|
$excerpt_filtered = apply_filters( 'the_excerpt', $excerpt );
|
|
|
|
|
|
|
|
// Now set the current excerpt length to this new filtered length.
|
|
|
|
$current_excerpt_length = strlen( $excerpt_filtered );
|
|
|
|
|
|
|
|
// Check to see if we're over the target length.
|
|
|
|
if ( $current_excerpt_length > $target_excerpt_length ) {
|
|
|
|
// If so, remove 20 characters from the current max and run the loop again.
|
|
|
|
$current_excerpt_max = $current_excerpt_max - 20;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-23 11:10:05 +02:00
|
|
|
return \apply_filters( 'the_excerpt', $excerpt );
|
2023-01-22 07:13:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-05-23 11:26:12 +02:00
|
|
|
* Generates output for the 'ap_content' Shortcode
|
2023-01-22 07:13:46 +01:00
|
|
|
*
|
2023-05-23 11:26:12 +02:00
|
|
|
* @param array $atts The Shortcode attributes.
|
|
|
|
* @param string $content The ActivityPub post-content.
|
|
|
|
* @param string $tag The tag/name of the Shortcode.
|
2023-01-22 07:13:46 +01:00
|
|
|
*
|
2023-05-23 11:26:12 +02:00
|
|
|
* @return string The post content.
|
2023-01-22 07:13:46 +01:00
|
|
|
*/
|
2023-01-23 17:59:13 +01:00
|
|
|
public static function content( $atts, $content, $tag ) {
|
2023-07-18 08:20:09 +02:00
|
|
|
$item = self::get_item();
|
2023-01-23 17:59:13 +01:00
|
|
|
|
2023-07-17 17:23:13 +02:00
|
|
|
if ( ! $item ) {
|
2023-01-22 07:13:46 +01:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2023-07-17 17:23:13 +02:00
|
|
|
// prevent inception
|
|
|
|
remove_shortcode( 'ap_content' );
|
|
|
|
|
2023-05-23 11:10:05 +02:00
|
|
|
$atts = shortcode_atts(
|
|
|
|
array( 'apply_filters' => 'yes' ),
|
|
|
|
$atts,
|
|
|
|
$tag
|
|
|
|
);
|
2023-01-22 07:13:46 +01:00
|
|
|
|
2023-07-17 17:23:13 +02:00
|
|
|
$content = \get_post_field( 'post_content', $item );
|
2023-01-31 18:43:11 +01:00
|
|
|
|
2023-05-23 11:10:05 +02:00
|
|
|
if ( 'yes' === $atts['apply_filters'] ) {
|
|
|
|
$content = \apply_filters( 'the_content', $content );
|
|
|
|
} else {
|
|
|
|
$content = do_blocks( $content );
|
|
|
|
$content = wptexturize( $content );
|
|
|
|
$content = wp_filter_content_tags( $content );
|
|
|
|
}
|
2023-02-20 21:18:03 +01:00
|
|
|
|
2023-05-23 11:13:17 +02:00
|
|
|
// replace script and style elements
|
|
|
|
$content = \preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $content );
|
2023-05-23 11:10:05 +02:00
|
|
|
$content = strip_shortcodes( $content );
|
2023-02-20 18:08:10 +01:00
|
|
|
$content = \trim( \preg_replace( '/[\n\r\t]/', '', $content ) );
|
|
|
|
|
2023-05-23 11:10:05 +02:00
|
|
|
add_shortcode( 'ap_content', array( 'Activitypub\Shortcodes', 'content' ) );
|
|
|
|
|
2023-01-31 18:43:11 +01:00
|
|
|
return $content;
|
2023-01-22 07:13:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-05-23 11:26:12 +02:00
|
|
|
* Generates output for the 'ap_permalink' Shortcode
|
2023-01-22 07:13:46 +01:00
|
|
|
*
|
2023-05-23 11:26:12 +02:00
|
|
|
* @param array $atts The Shortcode attributes.
|
|
|
|
* @param string $content The ActivityPub post-content.
|
|
|
|
* @param string $tag The tag/name of the Shortcode.
|
2023-01-22 07:13:46 +01:00
|
|
|
*
|
2023-05-23 11:26:12 +02:00
|
|
|
* @return string The post permalink.
|
2023-01-22 07:13:46 +01:00
|
|
|
*/
|
2023-01-23 17:59:13 +01:00
|
|
|
public static function permalink( $atts, $content, $tag ) {
|
2023-07-18 08:20:09 +02:00
|
|
|
$item = self::get_item();
|
2023-01-23 17:59:13 +01:00
|
|
|
|
2023-07-17 17:23:13 +02:00
|
|
|
if ( ! $item ) {
|
2023-01-22 07:13:46 +01:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2023-01-23 20:08:06 +01:00
|
|
|
$atts = shortcode_atts(
|
2023-01-23 20:11:18 +01:00
|
|
|
array(
|
2023-01-23 20:59:39 +01:00
|
|
|
'type' => 'url',
|
2023-01-23 20:11:18 +01:00
|
|
|
),
|
|
|
|
$atts,
|
|
|
|
$tag
|
|
|
|
);
|
|
|
|
|
2023-01-23 20:59:39 +01:00
|
|
|
if ( 'url' === $atts['type'] ) {
|
2023-07-17 17:23:13 +02:00
|
|
|
return \esc_url( \get_permalink( $item->ID ) );
|
2023-01-23 20:11:18 +01:00
|
|
|
}
|
2023-01-23 17:59:13 +01:00
|
|
|
|
2023-05-23 11:10:05 +02:00
|
|
|
return \sprintf(
|
|
|
|
'<a href="%1$s">%1$s</a>',
|
2023-07-17 17:23:13 +02:00
|
|
|
\esc_url( \get_permalink( $item->ID ) )
|
2023-05-23 11:10:05 +02:00
|
|
|
);
|
2023-01-22 07:13:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-05-23 11:26:12 +02:00
|
|
|
* Generates output for the 'ap_shortlink' Shortcode
|
2023-01-22 07:13:46 +01:00
|
|
|
*
|
2023-05-23 11:26:12 +02:00
|
|
|
* @param array $atts The Shortcode attributes.
|
|
|
|
* @param string $content The ActivityPub post-content.
|
|
|
|
* @param string $tag The tag/name of the Shortcode.
|
2023-01-22 07:13:46 +01:00
|
|
|
*
|
2023-05-23 11:26:12 +02:00
|
|
|
* @return string The post shortlink.
|
2023-01-22 07:13:46 +01:00
|
|
|
*/
|
2023-01-23 17:59:13 +01:00
|
|
|
public static function shortlink( $atts, $content, $tag ) {
|
2023-07-18 08:20:09 +02:00
|
|
|
$item = self::get_item();
|
2023-01-23 17:59:13 +01:00
|
|
|
|
2023-07-17 17:23:13 +02:00
|
|
|
if ( ! $item ) {
|
2023-01-22 07:13:46 +01:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2023-01-23 20:11:18 +01:00
|
|
|
$atts = shortcode_atts(
|
|
|
|
array(
|
2023-01-23 20:59:39 +01:00
|
|
|
'type' => 'url',
|
2023-01-23 20:11:18 +01:00
|
|
|
),
|
|
|
|
$atts,
|
|
|
|
$tag
|
|
|
|
);
|
|
|
|
|
2023-01-23 20:59:39 +01:00
|
|
|
if ( 'url' === $atts['type'] ) {
|
2023-07-17 17:23:13 +02:00
|
|
|
return \esc_url( \wp_get_shortlink( $item->ID ) );
|
2023-01-23 20:11:18 +01:00
|
|
|
}
|
2023-01-23 17:59:13 +01:00
|
|
|
|
2023-05-23 11:10:05 +02:00
|
|
|
return \sprintf(
|
|
|
|
'<a href="%1$s">%1$s</a>',
|
2023-07-17 17:23:13 +02:00
|
|
|
\esc_url( \wp_get_shortlink( $item->ID ) )
|
2023-05-23 11:10:05 +02:00
|
|
|
);
|
2023-01-22 07:13:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-05-23 11:26:12 +02:00
|
|
|
* Generates output for the 'ap_image' Shortcode
|
2023-01-22 07:13:46 +01:00
|
|
|
*
|
2023-05-23 11:26:12 +02:00
|
|
|
* @param array $atts The Shortcode attributes.
|
|
|
|
* @param string $content The ActivityPub post-content.
|
|
|
|
* @param string $tag The tag/name of the Shortcode.
|
2023-01-22 07:13:46 +01:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2023-01-23 17:59:13 +01:00
|
|
|
public static function image( $atts, $content, $tag ) {
|
2023-07-18 08:20:09 +02:00
|
|
|
$item = self::get_item();
|
2023-01-23 17:59:13 +01:00
|
|
|
|
2023-07-17 17:23:13 +02:00
|
|
|
if ( ! $item ) {
|
2023-01-22 07:13:46 +01:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2023-01-23 20:47:02 +01:00
|
|
|
$atts = shortcode_atts(
|
|
|
|
array(
|
|
|
|
'type' => 'full',
|
|
|
|
),
|
|
|
|
$atts,
|
|
|
|
$tag
|
|
|
|
);
|
2023-01-22 07:25:50 +01:00
|
|
|
|
2023-01-23 20:47:02 +01:00
|
|
|
$size = 'full';
|
2023-01-22 07:25:50 +01:00
|
|
|
|
2023-01-23 20:47:02 +01:00
|
|
|
if ( in_array(
|
|
|
|
$atts['type'],
|
|
|
|
array( 'thumbnail', 'medium', 'large', 'full' ),
|
|
|
|
true
|
|
|
|
) ) {
|
|
|
|
$size = $atts['type'];
|
2023-01-23 20:08:06 +01:00
|
|
|
}
|
2023-01-22 07:25:50 +01:00
|
|
|
|
2023-07-17 17:23:13 +02:00
|
|
|
$image = \get_the_post_thumbnail_url( $item->ID, $size );
|
2023-01-22 07:13:46 +01:00
|
|
|
|
|
|
|
if ( ! $image ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2023-01-23 20:59:39 +01:00
|
|
|
return \esc_url( $image );
|
2023-01-22 07:13:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-05-23 11:26:12 +02:00
|
|
|
* Generates output for the 'ap_hashcats' Shortcode
|
2023-01-22 07:13:46 +01:00
|
|
|
*
|
2023-05-23 11:26:12 +02:00
|
|
|
* @param array $atts The Shortcode attributes.
|
|
|
|
* @param string $content The ActivityPub post-content.
|
|
|
|
* @param string $tag The tag/name of the Shortcode.
|
2023-01-22 07:13:46 +01:00
|
|
|
*
|
2023-05-23 11:26:12 +02:00
|
|
|
* @return string The post categories as hashtags.
|
2023-01-22 07:13:46 +01:00
|
|
|
*/
|
2023-01-23 17:59:13 +01:00
|
|
|
public static function hashcats( $atts, $content, $tag ) {
|
2023-07-18 08:20:09 +02:00
|
|
|
$item = self::get_item();
|
2023-01-23 17:59:13 +01:00
|
|
|
|
2023-07-17 17:23:13 +02:00
|
|
|
if ( ! $item ) {
|
2023-01-22 07:13:46 +01:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2023-07-17 17:23:13 +02:00
|
|
|
$categories = \get_the_category( $item->ID );
|
2023-01-22 07:13:46 +01:00
|
|
|
|
|
|
|
if ( ! $categories ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$hash_tags = array();
|
|
|
|
|
|
|
|
foreach ( $categories as $category ) {
|
2023-05-11 14:20:35 +02:00
|
|
|
$hash_tags[] = \sprintf(
|
|
|
|
'<a rel="tag" class="u-tag u-category" href="%s">#%s</a>',
|
|
|
|
\esc_url( \get_category_link( $category ) ),
|
2023-05-23 11:10:05 +02:00
|
|
|
\wp_strip_all_tags( $category->slug )
|
2023-05-11 14:20:35 +02:00
|
|
|
);
|
2023-01-22 07:13:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return \implode( ' ', $hash_tags );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-05-23 11:26:12 +02:00
|
|
|
* Generates output for the 'ap_author' Shortcode
|
2023-01-22 07:13:46 +01:00
|
|
|
*
|
2023-05-23 11:26:12 +02:00
|
|
|
* @param array $atts The Shortcode attributes.
|
|
|
|
* @param string $content The ActivityPub post-content.
|
|
|
|
* @param string $tag The tag/name of the Shortcode.
|
2023-01-22 07:13:46 +01:00
|
|
|
*
|
2023-05-23 11:26:12 +02:00
|
|
|
* @return string The author name.
|
2023-01-22 07:13:46 +01:00
|
|
|
*/
|
2023-01-23 17:59:13 +01:00
|
|
|
public static function author( $atts, $content, $tag ) {
|
2023-07-18 08:20:09 +02:00
|
|
|
$item = self::get_item();
|
2023-01-23 17:59:13 +01:00
|
|
|
|
2023-07-17 17:23:13 +02:00
|
|
|
if ( ! $item ) {
|
2023-01-22 07:13:46 +01:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2023-07-17 17:23:13 +02:00
|
|
|
$name = \get_the_author_meta( 'display_name', $item->post_author );
|
2023-01-22 07:13:46 +01:00
|
|
|
|
|
|
|
if ( ! $name ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2023-05-23 11:10:05 +02:00
|
|
|
return wp_strip_all_tags( $name );
|
2023-01-22 07:13:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-05-23 11:26:12 +02:00
|
|
|
* Generates output for the 'ap_authorurl' Shortcode
|
2023-01-22 07:13:46 +01:00
|
|
|
*
|
2023-05-23 11:26:12 +02:00
|
|
|
* @param array $atts The Shortcode attributes.
|
|
|
|
* @param string $content The ActivityPub post-content.
|
|
|
|
* @param string $tag The tag/name of the Shortcode.
|
2023-01-22 07:13:46 +01:00
|
|
|
*
|
2023-05-23 11:26:12 +02:00
|
|
|
* @return string The author URL.
|
2023-01-22 07:13:46 +01:00
|
|
|
*/
|
2023-01-23 17:59:13 +01:00
|
|
|
public static function authorurl( $atts, $content, $tag ) {
|
2023-07-18 08:20:09 +02:00
|
|
|
$item = self::get_item();
|
2023-01-23 17:59:13 +01:00
|
|
|
|
2023-07-17 17:23:13 +02:00
|
|
|
if ( ! $item ) {
|
2023-01-22 07:13:46 +01:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2023-07-17 17:23:13 +02:00
|
|
|
$url = \get_the_author_meta( 'user_url', $item->post_author );
|
2023-01-22 07:13:46 +01:00
|
|
|
|
|
|
|
if ( ! $url ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2023-01-23 20:59:39 +01:00
|
|
|
return \esc_url( $url );
|
2023-01-22 07:13:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-05-23 11:26:12 +02:00
|
|
|
* Generates output for the 'ap_blogurl' Shortcode
|
2023-01-22 07:13:46 +01:00
|
|
|
*
|
2023-05-23 11:26:12 +02:00
|
|
|
* @param array $atts The Shortcode attributes.
|
|
|
|
* @param string $content The ActivityPub post-content.
|
|
|
|
* @param string $tag The tag/name of the Shortcode.
|
2023-01-22 07:13:46 +01:00
|
|
|
*
|
2023-05-23 11:26:12 +02:00
|
|
|
* @return string The site URL.
|
2023-01-22 07:13:46 +01:00
|
|
|
*/
|
2023-01-23 17:59:13 +01:00
|
|
|
public static function blogurl( $atts, $content, $tag ) {
|
2023-01-23 20:59:39 +01:00
|
|
|
return \esc_url( \get_bloginfo( 'url' ) );
|
2023-01-22 07:13:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-05-23 11:26:12 +02:00
|
|
|
* Generates output for the 'ap_blogname' Shortcode
|
2023-01-22 07:13:46 +01:00
|
|
|
*
|
2023-05-23 11:26:12 +02:00
|
|
|
* @param array $atts The Shortcode attributes.
|
|
|
|
* @param string $content The ActivityPub post-content.
|
|
|
|
* @param string $tag The tag/name of the Shortcode.
|
2023-01-22 07:13:46 +01:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2023-01-23 17:59:13 +01:00
|
|
|
public static function blogname( $atts, $content, $tag ) {
|
2023-05-23 11:10:05 +02:00
|
|
|
return \wp_strip_all_tags( \get_bloginfo( 'name' ) );
|
2023-01-22 07:13:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-05-23 11:26:12 +02:00
|
|
|
* Generates output for the 'ap_blogdesc' Shortcode
|
2023-01-22 07:13:46 +01:00
|
|
|
*
|
2023-05-23 11:26:12 +02:00
|
|
|
* @param array $atts The Shortcode attributes.
|
|
|
|
* @param string $content The ActivityPub post-content.
|
|
|
|
* @param string $tag The tag/name of the Shortcode.
|
2023-01-22 07:13:46 +01:00
|
|
|
*
|
2023-05-23 11:26:12 +02:00
|
|
|
* @return string The site description.
|
2023-01-22 07:13:46 +01:00
|
|
|
*/
|
2023-01-23 17:59:13 +01:00
|
|
|
public static function blogdesc( $atts, $content, $tag ) {
|
2023-05-23 11:10:05 +02:00
|
|
|
return \wp_strip_all_tags( \get_bloginfo( 'description' ) );
|
2023-01-22 07:13:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-05-23 11:26:12 +02:00
|
|
|
* Generates output for the 'ap_date' Shortcode
|
2023-01-22 07:13:46 +01:00
|
|
|
*
|
2023-05-23 11:26:12 +02:00
|
|
|
* @param array $atts The Shortcode attributes.
|
|
|
|
* @param string $content The ActivityPub post-content.
|
|
|
|
* @param string $tag The tag/name of the Shortcode.
|
2023-01-22 07:13:46 +01:00
|
|
|
*
|
2023-05-23 11:26:12 +02:00
|
|
|
* @return string The post date.
|
2023-01-22 07:13:46 +01:00
|
|
|
*/
|
2023-01-23 17:59:13 +01:00
|
|
|
public static function date( $atts, $content, $tag ) {
|
2023-07-18 08:20:09 +02:00
|
|
|
$item = self::get_item();
|
2023-01-23 17:59:13 +01:00
|
|
|
|
2023-07-17 17:23:13 +02:00
|
|
|
if ( ! $item ) {
|
2023-01-22 07:13:46 +01:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2023-07-17 17:23:13 +02:00
|
|
|
$datetime = \get_post_datetime( $item );
|
2023-01-22 07:13:46 +01:00
|
|
|
$dateformat = \get_option( 'date_format' );
|
|
|
|
$timeformat = \get_option( 'time_format' );
|
|
|
|
|
|
|
|
$date = $datetime->format( $dateformat );
|
|
|
|
|
|
|
|
if ( ! $date ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2023-05-23 11:10:05 +02:00
|
|
|
return $date;
|
2023-01-22 07:13:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-05-23 11:26:12 +02:00
|
|
|
* Generates output for the 'ap_time' Shortcode
|
2023-01-22 07:13:46 +01:00
|
|
|
*
|
2023-05-23 11:26:12 +02:00
|
|
|
* @param array $atts The Shortcode attributes.
|
|
|
|
* @param string $content The ActivityPub post-content.
|
|
|
|
* @param string $tag The tag/name of the Shortcode.
|
2023-01-22 07:13:46 +01:00
|
|
|
*
|
2023-05-23 11:26:12 +02:00
|
|
|
* @return string The post time.
|
2023-01-22 07:13:46 +01:00
|
|
|
*/
|
2023-01-23 17:59:13 +01:00
|
|
|
public static function time( $atts, $content, $tag ) {
|
2023-07-18 08:20:09 +02:00
|
|
|
$item = self::get_item();
|
2023-01-23 17:59:13 +01:00
|
|
|
|
2023-07-17 17:23:13 +02:00
|
|
|
if ( ! $item ) {
|
2023-01-22 07:13:46 +01:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2023-07-17 17:23:13 +02:00
|
|
|
$datetime = \get_post_datetime( $item );
|
2023-01-22 07:13:46 +01:00
|
|
|
$dateformat = \get_option( 'date_format' );
|
|
|
|
$timeformat = \get_option( 'time_format' );
|
|
|
|
|
|
|
|
$date = $datetime->format( $timeformat );
|
|
|
|
|
|
|
|
if ( ! $date ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2023-05-23 11:10:05 +02:00
|
|
|
return $date;
|
2023-01-22 07:13:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-05-23 11:26:12 +02:00
|
|
|
* Generates output for the 'ap_datetime' Shortcode
|
2023-01-22 07:13:46 +01:00
|
|
|
*
|
2023-05-23 11:26:12 +02:00
|
|
|
* @param array $atts The Shortcode attributes.
|
|
|
|
* @param string $content The ActivityPub post-content.
|
|
|
|
* @param string $tag The tag/name of the Shortcode.
|
2023-01-22 07:13:46 +01:00
|
|
|
*
|
2023-05-23 11:26:12 +02:00
|
|
|
* @return string The post date/time.
|
2023-01-22 07:13:46 +01:00
|
|
|
*/
|
2023-01-23 17:59:13 +01:00
|
|
|
public static function datetime( $atts, $content, $tag ) {
|
2023-07-18 08:20:09 +02:00
|
|
|
$item = self::get_item();
|
2023-01-23 17:59:13 +01:00
|
|
|
|
2023-07-17 17:23:13 +02:00
|
|
|
if ( ! $item ) {
|
2023-01-22 07:13:46 +01:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2023-07-17 17:23:13 +02:00
|
|
|
$datetime = \get_post_datetime( $item );
|
2023-01-22 07:13:46 +01:00
|
|
|
$dateformat = \get_option( 'date_format' );
|
|
|
|
$timeformat = \get_option( 'time_format' );
|
|
|
|
|
|
|
|
$date = $datetime->format( $dateformat . ' @ ' . $timeformat );
|
|
|
|
|
|
|
|
if ( ! $date ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2023-05-23 11:10:05 +02:00
|
|
|
return $date;
|
2023-01-22 07:13:46 +01:00
|
|
|
}
|
2023-07-18 08:20:09 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a WordPress item to federate.
|
|
|
|
*
|
|
|
|
* Checks if item (WP_Post) is "public", a supported post type
|
|
|
|
* and not password protected.
|
|
|
|
*
|
|
|
|
* @return null|WP_Post The WordPress item.
|
|
|
|
*/
|
|
|
|
protected static function get_item() {
|
|
|
|
$post = \get_post();
|
|
|
|
|
|
|
|
if ( ! $post ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( 'publish' !== \get_post_status( $post ) ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( \post_password_required( $post ) ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! \in_array( \get_post_type( $post ), \get_post_types_by_support( 'activitypub' ), true ) ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $post;
|
|
|
|
}
|
2023-01-23 19:43:34 +01:00
|
|
|
}
|