updated plugin ActivityPub
version 3.3.3
This commit is contained in:
@ -1,11 +1,18 @@
|
||||
<?php
|
||||
/**
|
||||
* Shortcodes class file.
|
||||
*
|
||||
* @package Activitypub
|
||||
*/
|
||||
|
||||
namespace Activitypub;
|
||||
|
||||
use function Activitypub\esc_hashtag;
|
||||
|
||||
/**
|
||||
* Shortcodes class.
|
||||
*/
|
||||
class Shortcodes {
|
||||
/**
|
||||
* Register the shortcodes
|
||||
* Register the shortcodes.
|
||||
*/
|
||||
public static function register() {
|
||||
foreach ( get_class_methods( self::class ) as $shortcode ) {
|
||||
@ -16,7 +23,7 @@ class Shortcodes {
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister the shortcodes
|
||||
* Unregister the shortcodes.
|
||||
*/
|
||||
public static function unregister() {
|
||||
foreach ( get_class_methods( self::class ) as $shortcode ) {
|
||||
@ -27,15 +34,11 @@ class Shortcodes {
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates output for the 'ap_hashtags' shortcode
|
||||
*
|
||||
* @param array $atts The Shortcode attributes.
|
||||
* @param string $content The ActivityPub post-content.
|
||||
* @param string $tag The tag/name of the Shortcode.
|
||||
* Generates output for the 'ap_hashtags' shortcode.
|
||||
*
|
||||
* @return string The post tags as hashtags.
|
||||
*/
|
||||
public static function hashtags( $atts, $content, $tag ) {
|
||||
public static function hashtags() {
|
||||
$item = self::get_item();
|
||||
|
||||
if ( ! $item ) {
|
||||
@ -64,13 +67,9 @@ class Shortcodes {
|
||||
/**
|
||||
* Generates output for the 'ap_title' Shortcode
|
||||
*
|
||||
* @param array $atts The Shortcode attributes.
|
||||
* @param string $content The ActivityPub post-content.
|
||||
* @param string $tag The tag/name of the Shortcode.
|
||||
*
|
||||
* @return string The post title.
|
||||
*/
|
||||
public static function title( $atts, $content, $tag ) {
|
||||
public static function title() {
|
||||
$item = self::get_item();
|
||||
|
||||
if ( ! $item ) {
|
||||
@ -108,87 +107,13 @@ class Shortcodes {
|
||||
$excerpt_length = ACTIVITYPUB_EXCERPT_LENGTH;
|
||||
}
|
||||
|
||||
$excerpt = \get_post_field( 'post_excerpt', $item );
|
||||
|
||||
if ( 'attachment' === $item->post_type ) {
|
||||
// get title of attachment with fallback to alt text.
|
||||
$content = wp_get_attachment_caption( $item->ID );
|
||||
if ( empty( $content ) ) {
|
||||
$content = get_post_meta( $item->ID, '_wp_attachment_image_alt', true );
|
||||
}
|
||||
} elseif ( '' === $excerpt ) {
|
||||
$content = \get_post_field( 'post_content', $item );
|
||||
|
||||
// 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 );
|
||||
|
||||
$excerpt_more = \apply_filters( 'activitypub_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;
|
||||
}
|
||||
}
|
||||
$excerpt = generate_post_summary( $item, $excerpt_length );
|
||||
|
||||
return \apply_filters( 'the_excerpt', $excerpt );
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates output for the 'ap_content' Shortcode
|
||||
* Generates output for the 'ap_content' Shortcode.
|
||||
*
|
||||
* @param array $atts The Shortcode attributes.
|
||||
* @param string $content The ActivityPub post-content.
|
||||
@ -203,7 +128,7 @@ class Shortcodes {
|
||||
return '';
|
||||
}
|
||||
|
||||
// prevent inception
|
||||
// Prevent inception.
|
||||
remove_shortcode( 'ap_content' );
|
||||
|
||||
$atts = shortcode_atts(
|
||||
@ -215,7 +140,7 @@ class Shortcodes {
|
||||
$content = '';
|
||||
|
||||
if ( 'attachment' === $item->post_type ) {
|
||||
// get title of attachment with fallback to alt text.
|
||||
// Get title of attachment with fallback to alt text.
|
||||
$content = wp_get_attachment_caption( $item->ID );
|
||||
if ( empty( $content ) ) {
|
||||
$content = get_post_meta( $item->ID, '_wp_attachment_image_alt', true );
|
||||
@ -231,7 +156,7 @@ class Shortcodes {
|
||||
$content = wp_filter_content_tags( $content );
|
||||
}
|
||||
|
||||
// replace script and style elements
|
||||
// Replace script and style elements.
|
||||
$content = \preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $content );
|
||||
$content = strip_shortcodes( $content );
|
||||
$content = \trim( \preg_replace( '/[\n\r\t]/', '', $content ) );
|
||||
@ -243,7 +168,7 @@ class Shortcodes {
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates output for the 'ap_permalink' Shortcode
|
||||
* Generates output for the 'ap_permalink' Shortcode.
|
||||
*
|
||||
* @param array $atts The Shortcode attributes.
|
||||
* @param string $content The ActivityPub post-content.
|
||||
@ -277,7 +202,7 @@ class Shortcodes {
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates output for the 'ap_shortlink' Shortcode
|
||||
* Generates output for the 'ap_shortlink' Shortcode.
|
||||
*
|
||||
* @param array $atts The Shortcode attributes.
|
||||
* @param string $content The ActivityPub post-content.
|
||||
@ -311,7 +236,7 @@ class Shortcodes {
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates output for the 'ap_image' Shortcode
|
||||
* Generates output for the 'ap_image' Shortcode.
|
||||
*
|
||||
* @param array $atts The Shortcode attributes.
|
||||
* @param string $content The ActivityPub post-content.
|
||||
@ -354,15 +279,11 @@ class Shortcodes {
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates output for the 'ap_hashcats' Shortcode
|
||||
*
|
||||
* @param array $atts The Shortcode attributes.
|
||||
* @param string $content The ActivityPub post-content.
|
||||
* @param string $tag The tag/name of the Shortcode.
|
||||
* Generates output for the 'ap_hashcats' Shortcode.
|
||||
*
|
||||
* @return string The post categories as hashtags.
|
||||
*/
|
||||
public static function hashcats( $atts, $content, $tag ) {
|
||||
public static function hashcats() {
|
||||
$item = self::get_item();
|
||||
|
||||
if ( ! $item ) {
|
||||
@ -389,15 +310,11 @@ class Shortcodes {
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates output for the 'ap_author' Shortcode
|
||||
*
|
||||
* @param array $atts The Shortcode attributes.
|
||||
* @param string $content The ActivityPub post-content.
|
||||
* @param string $tag The tag/name of the Shortcode.
|
||||
* Generates output for the 'ap_author' Shortcode.
|
||||
*
|
||||
* @return string The author name.
|
||||
*/
|
||||
public static function author( $atts, $content, $tag ) {
|
||||
public static function author() {
|
||||
$item = self::get_item();
|
||||
|
||||
if ( ! $item ) {
|
||||
@ -405,7 +322,7 @@ class Shortcodes {
|
||||
}
|
||||
|
||||
$author_id = \get_post_field( 'post_author', $item->ID );
|
||||
$name = \get_the_author_meta( 'display_name', $author_id );
|
||||
$name = \get_the_author_meta( 'display_name', $author_id );
|
||||
|
||||
if ( ! $name ) {
|
||||
return '';
|
||||
@ -415,15 +332,11 @@ class Shortcodes {
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates output for the 'ap_authorurl' Shortcode
|
||||
*
|
||||
* @param array $atts The Shortcode attributes.
|
||||
* @param string $content The ActivityPub post-content.
|
||||
* @param string $tag The tag/name of the Shortcode.
|
||||
* Generates output for the 'ap_authorurl' Shortcode.
|
||||
*
|
||||
* @return string The author URL.
|
||||
*/
|
||||
public static function authorurl( $atts, $content, $tag ) {
|
||||
public static function authorurl() {
|
||||
$item = self::get_item();
|
||||
|
||||
if ( ! $item ) {
|
||||
@ -431,7 +344,7 @@ class Shortcodes {
|
||||
}
|
||||
|
||||
$author_id = \get_post_field( 'post_author', $item->ID );
|
||||
$url = \get_the_author_meta( 'user_url', $author_id );
|
||||
$url = \get_the_author_meta( 'user_url', $author_id );
|
||||
|
||||
if ( ! $url ) {
|
||||
return '';
|
||||
@ -441,63 +354,46 @@ class Shortcodes {
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates output for the 'ap_blogurl' Shortcode
|
||||
*
|
||||
* @param array $atts The Shortcode attributes.
|
||||
* @param string $content The ActivityPub post-content.
|
||||
* @param string $tag The tag/name of the Shortcode.
|
||||
* Generates output for the 'ap_blogurl' Shortcode.
|
||||
*
|
||||
* @return string The site URL.
|
||||
*/
|
||||
public static function blogurl( $atts, $content, $tag ) {
|
||||
public static function blogurl() {
|
||||
return \esc_url( \get_bloginfo( 'url' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates output for the 'ap_blogname' Shortcode
|
||||
*
|
||||
* @param array $atts The Shortcode attributes.
|
||||
* @param string $content The ActivityPub post-content.
|
||||
* @param string $tag The tag/name of the Shortcode.
|
||||
* Generates output for the 'ap_blogname' Shortcode.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function blogname( $atts, $content, $tag ) {
|
||||
public static function blogname() {
|
||||
return \wp_strip_all_tags( \get_bloginfo( 'name' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates output for the 'ap_blogdesc' Shortcode
|
||||
*
|
||||
* @param array $atts The Shortcode attributes.
|
||||
* @param string $content The ActivityPub post-content.
|
||||
* @param string $tag The tag/name of the Shortcode.
|
||||
* Generates output for the 'ap_blogdesc' Shortcode.
|
||||
*
|
||||
* @return string The site description.
|
||||
*/
|
||||
public static function blogdesc( $atts, $content, $tag ) {
|
||||
public static function blogdesc() {
|
||||
return \wp_strip_all_tags( \get_bloginfo( 'description' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates output for the 'ap_date' Shortcode
|
||||
*
|
||||
* @param array $atts The Shortcode attributes.
|
||||
* @param string $content The ActivityPub post-content.
|
||||
* @param string $tag The tag/name of the Shortcode.
|
||||
* Generates output for the 'ap_date' Shortcode.
|
||||
*
|
||||
* @return string The post date.
|
||||
*/
|
||||
public static function date( $atts, $content, $tag ) {
|
||||
public static function date() {
|
||||
$item = self::get_item();
|
||||
|
||||
if ( ! $item ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$datetime = \get_post_datetime( $item );
|
||||
$datetime = \get_post_datetime( $item );
|
||||
$dateformat = \get_option( 'date_format' );
|
||||
$timeformat = \get_option( 'time_format' );
|
||||
|
||||
$date = $datetime->format( $dateformat );
|
||||
|
||||
@ -509,23 +405,18 @@ class Shortcodes {
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates output for the 'ap_time' Shortcode
|
||||
*
|
||||
* @param array $atts The Shortcode attributes.
|
||||
* @param string $content The ActivityPub post-content.
|
||||
* @param string $tag The tag/name of the Shortcode.
|
||||
* Generates output for the 'ap_time' Shortcode.
|
||||
*
|
||||
* @return string The post time.
|
||||
*/
|
||||
public static function time( $atts, $content, $tag ) {
|
||||
public static function time() {
|
||||
$item = self::get_item();
|
||||
|
||||
if ( ! $item ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$datetime = \get_post_datetime( $item );
|
||||
$dateformat = \get_option( 'date_format' );
|
||||
$datetime = \get_post_datetime( $item );
|
||||
$timeformat = \get_option( 'time_format' );
|
||||
|
||||
$date = $datetime->format( $timeformat );
|
||||
@ -538,22 +429,18 @@ class Shortcodes {
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates output for the 'ap_datetime' Shortcode
|
||||
*
|
||||
* @param array $atts The Shortcode attributes.
|
||||
* @param string $content The ActivityPub post-content.
|
||||
* @param string $tag The tag/name of the Shortcode.
|
||||
* Generates output for the 'ap_datetime' Shortcode.
|
||||
*
|
||||
* @return string The post date/time.
|
||||
*/
|
||||
public static function datetime( $atts, $content, $tag ) {
|
||||
public static function datetime() {
|
||||
$item = self::get_item();
|
||||
|
||||
if ( ! $item ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$datetime = \get_post_datetime( $item );
|
||||
$datetime = \get_post_datetime( $item );
|
||||
$dateformat = \get_option( 'date_format' );
|
||||
$timeformat = \get_option( 'time_format' );
|
||||
|
||||
@ -572,7 +459,7 @@ class Shortcodes {
|
||||
* Checks if item (WP_Post) is "public", a supported post type
|
||||
* and not password protected.
|
||||
*
|
||||
* @return null|WP_Post The WordPress item.
|
||||
* @return null|\WP_Post The WordPress item.
|
||||
*/
|
||||
protected static function get_item() {
|
||||
$post = \get_post();
|
||||
|
Reference in New Issue
Block a user