Converted shortcode class to static.

And added options for shortlink/permalink type.
This commit is contained in:
Greg 2023-01-23 11:59:13 -05:00
parent efa62ac4cb
commit 71f3a47589
4 changed files with 120 additions and 75 deletions

View file

@ -28,7 +28,6 @@ function init() {
\define( 'ACTIVITYPUB_PLUGIN_BASENAME', plugin_basename( __FILE__ ) ); \define( 'ACTIVITYPUB_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
\define( 'ACTIVITYPUB_PLUGIN_FILE', plugin_dir_path( __FILE__ ) . '/' . basename( __FILE__ ) ); \define( 'ACTIVITYPUB_PLUGIN_FILE', plugin_dir_path( __FILE__ ) . '/' . basename( __FILE__ ) );
require_once \dirname( __FILE__ ) . '/includes/class-shortcodes.php';
require_once \dirname( __FILE__ ) . '/includes/table/followers-list.php'; require_once \dirname( __FILE__ ) . '/includes/table/followers-list.php';
require_once \dirname( __FILE__ ) . '/includes/class-signature.php'; require_once \dirname( __FILE__ ) . '/includes/class-signature.php';
require_once \dirname( __FILE__ ) . '/includes/class-webfinger.php'; require_once \dirname( __FILE__ ) . '/includes/class-webfinger.php';
@ -72,6 +71,9 @@ function init() {
require_once \dirname( __FILE__ ) . '/includes/class-hashtag.php'; require_once \dirname( __FILE__ ) . '/includes/class-hashtag.php';
\Activitypub\Hashtag::init(); \Activitypub\Hashtag::init();
require_once \dirname( __FILE__ ) . '/includes/class-shortcodes.php';
\Activitypub\Shortcodes::init();
require_once \dirname( __FILE__ ) . '/includes/class-debug.php'; require_once \dirname( __FILE__ ) . '/includes/class-debug.php';
\Activitypub\Debug::init(); \Activitypub\Debug::init();

View file

@ -2,32 +2,15 @@
namespace Activitypub; namespace Activitypub;
class Shortcodes { class Shortcodes {
/**
* The post object we're currently working on
*
* @var WP_Post $post A WordPress Post Object
*/
private $post;
/** /**
* Class constructor, registering WordPress then shortcodes * Class constructor, registering WordPress then shortcodes
* *
* @param WP_Post $post A WordPress Post Object * @param WP_Post $post A WordPress Post Object
*/ */
public function __construct( $post = null ) { public static function init() {
if( $post == null ) { foreach( get_class_methods( 'Activitypub\Shortcodes' ) as $shortcode ) {
$post = \get_post(); if( $shortcode != 'init' ) {
} add_shortcode( 'ap_' . $shortcode, array( 'Activitypub\Shortcodes', $shortcode ) );
if( \is_object( $post ) && ! $post instanceof WP_POST ) {
$this->post = $post;
} else {
$this->post = false;
}
foreach( get_class_methods( $this ) as $shortcode ) {
if( strpos( $shortcode, '__' ) !== 0 ) {
add_shortcode( 'ap_' . $shortcode, array( $this, $shortcode ) );
} }
} }
} }
@ -41,12 +24,14 @@ class Shortcodes {
* *
* @return string * @return string
*/ */
public function hashtags( $atts, $content, $tag ) { public static function hashtags( $atts, $content, $tag ) {
if( $this->post === false ) { $post = get_post();
if( ! \is_object( $post ) || ( \is_object( $post ) && get_class( $post ) != 'WP_Post' ) ) {
return ''; return '';
} }
$tags = \get_the_tags( $this->post->ID ); $tags = \get_the_tags( $post->ID );
if ( ! $tags ) { if ( ! $tags ) {
return ''; return '';
@ -70,12 +55,14 @@ class Shortcodes {
* *
* @return string * @return string
*/ */
public function title( $atts, $content, $tag ) { public static function title( $atts, $content, $tag ) {
if( $this->post === false ) { $post = get_post();
if( ! \is_object( $post ) || ( \is_object( $post ) && get_class( $post ) != 'WP_Post' ) ) {
return ''; return '';
} }
return \get_the_title( $this->post->ID );; return \get_the_title( $post->ID );;
} }
/** /**
@ -87,24 +74,24 @@ class Shortcodes {
* *
* @return string * @return string
*/ */
public function excerpt( $atts, $content, $tag ) { public static function excerpt( $atts, $content, $tag ) {
if( $this->post === false ) { $post = get_post();
if( ! \is_object( $post ) || ( \is_object( $post ) && get_class( $post ) != 'WP_Post' ) ) {
return ''; return '';
} }
$length = ACTIVITYPUB_EXCERPT_LENGTH; $atts = shortcode_atts( array( 'length' => ACTIVITYPUB_EXCERPT_LENGTH ), $atts, $tag );
if( is_array( $atts ) && array_key_exists( 'length', $atts ) ) {
$length = intval( $atts['length'] ); $length = intval( $atts['length'] );
}
if( $length == 0 ) { $length = ACTIVITYPUB_EXCERPT_LENGTH; } if( $length == 0 ) { $length = ACTIVITYPUB_EXCERPT_LENGTH; }
$excerpt = \get_post_field( 'post_excerpt', $this->post ); $excerpt = \get_post_field( 'post_excerpt', $post );
if ( '' === $excerpt ) { if ( '' === $excerpt ) {
$content = \get_post_field( 'post_content', $this->post ); $content = \get_post_field( 'post_content', $post );
// An empty string will make wp_trim_excerpt do stuff we do not want. // An empty string will make wp_trim_excerpt do stuff we do not want.
if ( '' !== $content ) { if ( '' !== $content ) {
@ -186,12 +173,14 @@ class Shortcodes {
* *
* @return string * @return string
*/ */
public function content( $atts, $content, $tag ) { public static function content( $atts, $content, $tag ) {
if( $this->post === false ) { $post = get_post();
if( ! \is_object( $post ) || ( \is_object( $post ) && get_class( $post ) != 'WP_Post' ) ) {
return ''; return '';
} }
$content = \get_post_field( 'post_content', $this->post ); $content = \get_post_field( 'post_content', $post );
return \apply_filters( 'the_content', $content ); return \apply_filters( 'the_content', $content );
} }
@ -205,12 +194,30 @@ class Shortcodes {
* *
* @return string * @return string
*/ */
public function permalink( $atts, $content, $tag ) { public static function permalink( $atts, $content, $tag ) {
if( $this->post === false ) { $post = get_post();
if( ! \is_object( $post ) || ( \is_object( $post ) && get_class( $post ) != 'WP_Post' ) ) {
return ''; return '';
} }
return \sprintf( '<a href="%1$s">%1$s</a>', \esc_url( \get_permalink( $this->post->ID ) ) ); $atts = shortcode_atts( array( 'type' => 'html' ), $atts, $tag );
if( is_array( $atts ) && array_key_exists( 'type', $atts ) ) {
if( $atts['type'] == 'raw' ) {
return \get_permalink( $post->ID );
}
if( $atts['type'] == 'esc' ) {
return \esc_url( \get_permalink( $post->ID ) );
}
if( $atts['type'] == 'blank' ) {
return \sprintf( '<a href="%1$s" target="_blank">%1$s</a>', \esc_url( \get_permalink( $post->ID ) ) );
}
}
return \sprintf( '<a href="%1$s">%1$s</a>', \esc_url( \get_permalink( $post->ID ) ) );
} }
/** /**
@ -222,12 +229,30 @@ class Shortcodes {
* *
* @return string * @return string
*/ */
public function shortlink( $atts, $content, $tag ) { public static function shortlink( $atts, $content, $tag ) {
if( $this->post === false ) { $post = get_post();
if( ! \is_object( $post ) || ( \is_object( $post ) && get_class( $post ) != 'WP_Post' ) ) {
return ''; return '';
} }
return \sprintf( '<a href="%1$s">%1$s</a>', \esc_url( \wp_get_shortlink( $this->post->ID ) ) ); $atts = shortcode_atts( array( 'type' => 'html' ), $atts, $tag );
if( is_array( $atts ) && array_key_exists( 'type', $atts ) ) {
if( $atts['type'] == 'raw' ) {
return \wp_get_shortlink( $post->ID );
}
if( $atts['type'] == 'esc' ) {
return \esc_url( \wp_get_shortlink( $post->ID ) );
}
if( $atts['type'] == 'blank' ) {
return \sprintf( '<a href="%1$s" target="_blank">%1$s</a>', \esc_url( \wp_get_shortlink( $post->ID ) ) );
}
}
return \sprintf( '<a href="%1$s">%1$s</a>', \esc_url( \wp_get_shortlink( $post->ID ) ) );
} }
/** /**
@ -239,12 +264,14 @@ class Shortcodes {
* *
* @return string * @return string
*/ */
public function thumbnail( $atts, $content, $tag ) { public static function thumbnail( $atts, $content, $tag ) {
if( $this->post === false ) { $post = get_post();
if( ! \is_object( $post ) || ( \is_object( $post ) && get_class( $post ) != 'WP_Post' ) ) {
return ''; return '';
} }
$image = \get_the_post_thumbnail_url( $this->post->ID, 'thumbnail' ); $image = \get_the_post_thumbnail_url( $post->ID, 'thumbnail' );
if ( ! $image ) { if ( ! $image ) {
return ''; return '';
@ -262,12 +289,14 @@ class Shortcodes {
* *
* @return string * @return string
*/ */
public function image( $atts, $content, $tag ) { public static function image( $atts, $content, $tag ) {
if( $this->post === false ) { $post = get_post();
if( ! \is_object( $post ) || ( \is_object( $post ) && get_class( $post ) != 'WP_Post' ) ) {
return ''; return '';
} }
$size = 'full'; $atts = shortcode_atts( array( 'size' => 'full' ), $atts, $tag );
if( is_array( $atts ) && array_key_exists( 'size', $atts ) ) { if( is_array( $atts ) && array_key_exists( 'size', $atts ) ) {
$registered_sizes = wp_get_registered_image_subsizes(); $registered_sizes = wp_get_registered_image_subsizes();
@ -279,7 +308,7 @@ class Shortcodes {
if( ! $size ) { $size = 'full'; } if( ! $size ) { $size = 'full'; }
$image = \get_the_post_thumbnail_url( $this->post->ID, $size ); $image = \get_the_post_thumbnail_url( $post->ID, $size );
if ( ! $image ) { if ( ! $image ) {
return ''; return '';
@ -297,12 +326,14 @@ class Shortcodes {
* *
* @return string * @return string
*/ */
public function hashcats( $atts, $content, $tag ) { public static function hashcats( $atts, $content, $tag ) {
if( $this->post === false ) { $post = get_post();
if( ! \is_object( $post ) || ( \is_object( $post ) && get_class( $post ) != 'WP_Post' ) ) {
return ''; return '';
} }
$categories = \get_the_category( $this->post->ID ); $categories = \get_the_category( $post->ID );
if ( ! $categories ) { if ( ! $categories ) {
return ''; return '';
@ -326,12 +357,14 @@ class Shortcodes {
* *
* @return string * @return string
*/ */
public function author( $atts, $content, $tag ) { public static function author( $atts, $content, $tag ) {
if( $this->post === false ) { $post = get_post();
if( ! \is_object( $post ) || ( \is_object( $post ) && get_class( $post ) != 'WP_Post' ) ) {
return ''; return '';
} }
$name = \get_the_author_meta( 'display_name', $this->post->post_author ); $name = \get_the_author_meta( 'display_name', $post->post_author );
if ( ! $name ) { if ( ! $name ) {
return ''; return '';
@ -349,12 +382,14 @@ class Shortcodes {
* *
* @return string * @return string
*/ */
public function authorurl( $atts, $content, $tag ) { public static function authorurl( $atts, $content, $tag ) {
if( $this->post === false ) { $post = get_post();
if( ! \is_object( $post ) || ( \is_object( $post ) && get_class( $post ) != 'WP_Post' ) ) {
return ''; return '';
} }
$url = \get_the_author_meta( 'user_url', $this->post->post_author ); $url = \get_the_author_meta( 'user_url', $post->post_author );
if ( ! $url ) { if ( ! $url ) {
return ''; return '';
@ -372,7 +407,7 @@ class Shortcodes {
* *
* @return string * @return string
*/ */
public function blogurl( $atts, $content, $tag ) { public static function blogurl( $atts, $content, $tag ) {
return \get_bloginfo('url'); return \get_bloginfo('url');
} }
@ -385,7 +420,7 @@ class Shortcodes {
* *
* @return string * @return string
*/ */
public function blogname( $atts, $content, $tag ) { public static function blogname( $atts, $content, $tag ) {
return \get_bloginfo('name'); return \get_bloginfo('name');
} }
@ -398,7 +433,7 @@ class Shortcodes {
* *
* @return string * @return string
*/ */
public function blogdesc( $atts, $content, $tag ) { public static function blogdesc( $atts, $content, $tag ) {
return \get_bloginfo('description'); return \get_bloginfo('description');
} }
@ -411,12 +446,14 @@ class Shortcodes {
* *
* @return string * @return string
*/ */
public function date( $atts, $content, $tag ) { public static function date( $atts, $content, $tag ) {
if( $this->post === false ) { $post = get_post();
if( ! \is_object( $post ) || ( \is_object( $post ) && get_class( $post ) != 'WP_Post' ) ) {
return ''; return '';
} }
$datetime = \get_post_datetime( $this->post ); $datetime = \get_post_datetime( $post );
$dateformat = \get_option( 'date_format' ); $dateformat = \get_option( 'date_format' );
$timeformat = \get_option( 'time_format' ); $timeformat = \get_option( 'time_format' );
@ -438,12 +475,14 @@ class Shortcodes {
* *
* @return string * @return string
*/ */
public function time( $atts, $content, $tag ) { public static function time( $atts, $content, $tag ) {
if( $this->post === false ) { $post = get_post();
if( ! \is_object( $post ) || ( \is_object( $post ) && get_class( $post ) != 'WP_Post' ) ) {
return ''; return '';
} }
$datetime = \get_post_datetime( $this->post ); $datetime = \get_post_datetime( $post );
$dateformat = \get_option( 'date_format' ); $dateformat = \get_option( 'date_format' );
$timeformat = \get_option( 'time_format' ); $timeformat = \get_option( 'time_format' );
@ -465,12 +504,14 @@ class Shortcodes {
* *
* @return string * @return string
*/ */
public function datetime( $atts, $content, $tag ) { public static function datetime( $atts, $content, $tag ) {
if( $this->post === false ) { $post = get_post();
if( ! \is_object( $post ) || ( \is_object( $post ) && get_class( $post ) != 'WP_Post' ) ) {
return ''; return '';
} }
$datetime = \get_post_datetime( $this->post ); $datetime = \get_post_datetime( $post );
$dateformat = \get_option( 'date_format' ); $dateformat = \get_option( 'date_format' );
$timeformat = \get_option( 'time_format' ); $timeformat = \get_option( 'time_format' );

View file

@ -242,7 +242,9 @@ class Post {
$shortcodes = new \Activitypub\Shortcodes( $post ); $shortcodes = new \Activitypub\Shortcodes( $post );
// Fill in the shortcodes. // Fill in the shortcodes.
setup_postdata( $post );
$content = do_shortcode( $content ); $content = do_shortcode( $content );
wp_reset_postdata();
$content = \trim( \preg_replace( '/[\r\n]{2,}/', '', $content ) ); $content = \trim( \preg_replace( '/[\r\n]{2,}/', '', $content ) );

View file

@ -76,9 +76,9 @@ $registered_sizes .= '.';
<li><code>[ap_title]</code> - <?php \esc_html_e( 'The post\'s title.', 'activitypub' ); ?></li> <li><code>[ap_title]</code> - <?php \esc_html_e( 'The post\'s title.', 'activitypub' ); ?></li>
<li><code>[ap_content]</code> - <?php \esc_html_e( 'The post\'s content.', 'activitypub' ); ?></li> <li><code>[ap_content]</code> - <?php \esc_html_e( 'The post\'s content.', 'activitypub' ); ?></li>
<li><code>[ap_excerpt <i>lenght=nnn</i>]</code> - <?php \esc_html_e( 'The post\'s excerpt (default 400 chars). length parameter is optional.', 'activitypub' ); ?></li> <li><code>[ap_excerpt <i>lenght=nnn</i>]</code> - <?php \esc_html_e( 'The post\'s excerpt (default 400 chars). length parameter is optional.', 'activitypub' ); ?></li>
<li><code>[ap_permalink]</code> - <?php \esc_html_e( 'The post\'s permalink.', 'activitypub' ); ?></li> <li><code>[ap_permalink <i>type=xxx</i>]</code> - <?php \esc_html_e( 'The post\'s permalink. Type can be either: raw (the raw url, no escaping), esc (the html escaped url), html (default, an a tag to the url), blank (an a tag to the url that opens in a new window).', 'activitypub' ); ?></li>
<?php // translators: ?> <?php // translators: ?>
<li><code>[ap_shortlink]</code> - <?php echo \wp_kses( \__( 'The post\'s shortlink. I can recommend <a href="https://wordpress.org/plugins/hum/" target="_blank">Hum</a>, to prettify the Shortlinks', 'activitypub' ), 'default' ); ?></li> <li><code>[ap_shortlink <i>type=xxx</i>]</code> - <?php echo \wp_kses( \__( 'The post\'s shortlink. I can recommend <a href="https://wordpress.org/plugins/hum/" target="_blank">Hum</a>, to prettify the Shortlinks. Type can be either: raw (the raw url, no escaping), esc (the html escaped url), html (default, an a tag to the url), blank (an a tag to the url that opens in a new window).', 'activitypub' ), 'default' ); ?></li>
<li><code>[ap_hashtags]</code> - <?php \esc_html_e( 'The post\'s tags as hashtags.', 'activitypub' ); ?></li> <li><code>[ap_hashtags]</code> - <?php \esc_html_e( 'The post\'s tags as hashtags.', 'activitypub' ); ?></li>
<li><code>[ap_hashcats]</code> - <?php \esc_html_e( 'The post\'s categories as hashtags.', 'activitypub' ); ?></li> <li><code>[ap_hashcats]</code> - <?php \esc_html_e( 'The post\'s categories as hashtags.', 'activitypub' ); ?></li>
<li><code>[ap_image <i>size=xxx</i>]</code> - <?php \esc_html_e( 'The URL for the post\'s featured image, defaults to full size. The size attribute can be any of the following: ', 'activitypub' ); echo esc_html( $registered_sizes ); ?></li> <li><code>[ap_image <i>size=xxx</i>]</code> - <?php \esc_html_e( 'The URL for the post\'s featured image, defaults to full size. The size attribute can be any of the following: ', 'activitypub' ); echo esc_html( $registered_sizes ); ?></li>