From b5fa16b464d238111db0323bd060b187b2f1db86 Mon Sep 17 00:00:00 2001 From: Greg Date: Sun, 22 Jan 2023 01:13:46 -0500 Subject: [PATCH] Move the shortcodes to their own class. --- activitypub.php | 1 + includes/class-shortcodes.php | 464 ++++++++++++++++++++++++++++++++++ includes/model/class-post.php | 352 +------------------------- 3 files changed, 466 insertions(+), 351 deletions(-) create mode 100644 includes/class-shortcodes.php diff --git a/activitypub.php b/activitypub.php index f02c054..f590c34 100644 --- a/activitypub.php +++ b/activitypub.php @@ -28,6 +28,7 @@ function init() { \define( 'ACTIVITYPUB_PLUGIN_BASENAME', plugin_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/class-signature.php'; require_once \dirname( __FILE__ ) . '/includes/class-webfinger.php'; diff --git a/includes/class-shortcodes.php b/includes/class-shortcodes.php new file mode 100644 index 0000000..7b25128 --- /dev/null +++ b/includes/class-shortcodes.php @@ -0,0 +1,464 @@ +post = $post; + } else { + $this->post = false; + } + + foreach( get_class_methods( $this ) as $shortcode ) { + if( $shortcode != 'init' && strpos( $shortcode, '__' ) !== 0 ) { + add_shortcode( 'ap_' . $shortcode, array( $this, $shortcode ) ); + } + } + } + + /** + * Generates output for the ap_hashtags shortcode + * + * @param array $atts shortcode attributes + * @param string $content shortcode content + * @param string $tag shortcode tag name + * + * @return string + */ + public function hashtags( $atts, $content, $tag ) { + if( $this->post === false ) { + return ''; + } + + $tags = \get_the_tags( $this->post->ID ); + + if ( ! $tags ) { + return ''; + } + + $hash_tags = array(); + + foreach ( $tags as $tag ) { + $hash_tags[] = \sprintf( '', \get_tag_link( $tag ), $tag->slug ); + } + + return \implode( ' ', $hash_tags ); + } + + /** + * Generates output for the ap_title shortcode + * + * @param array $atts shortcode attributes + * @param string $content shortcode content + * @param string $tag shortcode tag name + * + * @return string + */ + public function title( $atts, $content, $tag ) { + if( $this->post === false ) { + return ''; + } + + return \get_the_title( $this->post->ID );; + } + + /** + * Generates output for the ap_excerpt shortcode + * + * @param array $atts shortcode attributes + * @param string $content shortcode content + * @param string $tag shortcode tag name + * + * @return string + */ + public function excerpt( $atts, $content, $tag ) { + if( $this->post === false ) { + return ''; + } + + $length = ACTIVITYPUB_EXCERPT_LENGTH; + + if( is_array( $atts ) && array_key_exists( 'length', $atts ) ) { + $length = intval( $atts['length'] ); + } + + if( $length == 0 ) { $length = ACTIVITYPUB_EXCERPT_LENGTH; } + + $excerpt = \get_post_field( 'post_excerpt', $this->post ); + + if ( '' === $excerpt ) { + + $content = \get_post_field( 'post_content', $this->post ); + + // 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
as the linebreak just in case there are any newlines existing in the excerpt from the user. + // There won't be any
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, '
' ), '
' ) ); + + // 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; + } + } + + return \apply_filters( 'the_excerpt', $excerpt ); + } + + /** + * Generates output for the ap_content shortcode + * + * @param array $atts shortcode attributes + * @param string $content shortcode content + * @param string $tag shortcode tag name + * + * @return string + */ + public function content( $atts, $content, $tag ) { + if( $this->post === false ) { + return ''; + } + + $content = \get_post_field( 'post_content', $this->post ); + + return \apply_filters( 'the_content', $content ); + } + + /** + * Generates output for the ap_permalink shortcode + * + * @param array $atts shortcode attributes + * @param string $content shortcode content + * @param string $tag shortcode tag name + * + * @return string + */ + public function permalink( $atts, $content, $tag ) { + if( $this->post === false ) { + return ''; + } + + return \sprintf( '%1$s', \esc_url( \get_permalink( $this->post->ID ) ) ); + } + + /** + * Generates output for the ap_shortlink shortcode + * + * @param array $atts shortcode attributes + * @param string $content shortcode content + * @param string $tag shortcode tag name + * + * @return string + */ + public function shortlink( $atts, $content, $tag ) { + if( $this->post === false ) { + return ''; + } + + return \sprintf( '%1$s', \esc_url( \wp_get_shortlink( $this->post->ID ) ) ); + } + + /** + * Generates output for the ap_thumbnail shortcode + * + * @param array $atts shortcode attributes + * @param string $content shortcode content + * @param string $tag shortcode tag name + * + * @return string + */ + public function thumbnail( $atts, $content, $tag ) { + if( $this->post === false ) { + return ''; + } + + $image = \get_the_post_thumbnail_url( $this->post->ID, 'thumbnail' ); + + if ( ! $image ) { + return ''; + } + + return $image; + } + + /** + * Generates output for the ap_image shortcode + * + * @param array $atts shortcode attributes + * @param string $content shortcode content + * @param string $tag shortcode tag name + * + * @return string + */ + public function image( $atts, $content, $tag ) { + if( $this->post === false ) { + return ''; + } + + $image = \get_the_post_thumbnail_url( $this->post->ID, 'full' ); + + if ( ! $image ) { + return ''; + } + + return $image; + } + + /** + * Generates output for the ap_hashcats shortcode + * + * @param array $atts shortcode attributes + * @param string $content shortcode content + * @param string $tag shortcode tag name + * + * @return string + */ + public function hashcats( $atts, $content, $tag ) { + if( $this->post === false ) { + return ''; + } + + $categories = \get_the_category( $this->post->ID ); + + if ( ! $categories ) { + return ''; + } + + $hash_tags = array(); + + foreach ( $categories as $category ) { + $hash_tags[] = \sprintf( '', \get_category_link( $category ), $category->slug ); + } + + return \implode( ' ', $hash_tags ); + } + + /** + * Generates output for the ap_author shortcode + * + * @param array $atts shortcode attributes + * @param string $content shortcode content + * @param string $tag shortcode tag name + * + * @return string + */ + public function author( $atts, $content, $tag ) { + if( $this->post === false ) { + return ''; + } + + $name = \get_the_author_meta( 'display_name', $this->post->post_author ); + + if ( ! $name ) { + return ''; + } + + return $name; + } + + /** + * Generates output for the ap_authorurl shortcode + * + * @param array $atts shortcode attributes + * @param string $content shortcode content + * @param string $tag shortcode tag name + * + * @return string + */ + public function authorurl( $atts, $content, $tag ) { + if( $this->post === false ) { + return ''; + } + + $url = \get_the_author_meta( 'user_url', $this->post->post_author ); + + if ( ! $url ) { + return ''; + } + + return $url; + } + + /** + * Generates output for the ap_blogurl shortcode + * + * @param array $atts shortcode attributes + * @param string $content shortcode content + * @param string $tag shortcode tag name + * + * @return string + */ + public function blogurl( $atts, $content, $tag ) { + return \get_bloginfo('url'); + } + + /** + * Generates output for the ap_blogname shortcode + * + * @param array $atts shortcode attributes + * @param string $content shortcode content + * @param string $tag shortcode tag name + * + * @return string + */ + public function blogname( $atts, $content, $tag ) { + return \get_bloginfo('name'); + } + + /** + * Generates output for the ap_blogdesc shortcode + * + * @param array $atts shortcode attributes + * @param string $content shortcode content + * @param string $tag shortcode tag name + * + * @return string + */ + public function blogdesc( $atts, $content, $tag ) { + return \get_bloginfo('description'); + } + + /** + * Generates output for the ap_date shortcode + * + * @param array $atts shortcode attributes + * @param string $content shortcode content + * @param string $tag shortcode tag name + * + * @return string + */ + public function date( $atts, $content, $tag ) { + if( $this->post === false ) { + return ''; + } + + $datetime = \get_post_datetime( $this->post ); + $dateformat = \get_option( 'date_format' ); + $timeformat = \get_option( 'time_format' ); + + $date = $datetime->format( $dateformat ); + + if ( ! $date ) { + return ''; + } + + return $date; + } + + /** + * Generates output for the ap_time shortcode + * + * @param array $atts shortcode attributes + * @param string $content shortcode content + * @param string $tag shortcode tag name + * + * @return string + */ + public function time( $atts, $content, $tag ) { + if( $this->post === false ) { + return ''; + } + + $datetime = \get_post_datetime( $this->post ); + $dateformat = \get_option( 'date_format' ); + $timeformat = \get_option( 'time_format' ); + + $date = $datetime->format( $timeformat ); + + if ( ! $date ) { + return ''; + } + + return $date; + } + + /** + * Generates output for the ap_datetime shortcode + * + * @param array $atts shortcode attributes + * @param string $content shortcode content + * @param string $tag shortcode tag name + * + * @return string + */ + public function datetime( $atts, $content, $tag ) { + if( $this->post === false ) { + return ''; + } + + $datetime = \get_post_datetime( $this->post ); + $dateformat = \get_option( 'date_format' ); + $timeformat = \get_option( 'time_format' ); + + $date = $datetime->format( $dateformat . ' @ ' . $timeformat ); + + if ( ! $date ) { + return ''; + } + + return $date; + } + +} \ No newline at end of file diff --git a/includes/model/class-post.php b/includes/model/class-post.php index c881e75..d528be6 100644 --- a/includes/model/class-post.php +++ b/includes/model/class-post.php @@ -229,102 +229,6 @@ class Post { return $object_type; } - /** - * Outputs the shortcode content. - * - * @param array $atts the attributes of the shortcode - * @param string $content the content between opening and closing shortcodes - * @param string $tag the name of the shortcode being processed - * - */ - public function shortcode_content( $atts, $content, $tag ) { - $tag = strtolower( $tag ); - $post = $this->post; - - $text = ''; - - switch( $tag ) { - case 'ap_title': - $text = \get_the_title( $post->ID ); - - break; - case 'ap_excerpt': - $length = ACTIVITYPUB_EXCERPT_LENGTH; - - if( is_array( $atts ) && array_key_exists( 'length', $atts ) ) { - $length = intval( $atts['length'] ); - } - - if( $length == 0 ) { $length = ACTIVITYPUB_EXCERPT_LENGTH; } - - $text = $this->get_the_post_excerpt( $length ); - - break; - case 'ap_content': - $text = $this->get_the_post_content(); - - break; - case 'ap_permalink': - $text = $this->get_the_post_link( 'permalink' ); - - break; - case 'ap_shortlink': - $text = $this->get_the_post_link( 'shortlink' ); - - break; - case 'ap_hashtags': - $text = $this->get_the_post_hashtags(); - - break; - case 'ap_thumbnail': - $text = $this->get_the_post_image( 'thumbnail' ); - - break; - case 'ap_image': - $text = $this->get_the_post_image(); - - break; - case 'ap_hashcats': - $text = $this->get_the_post_categories(); - - break; - case 'ap_author': - $text = $this->get_the_post_author(); - - break; - case 'ap_authorurl': - $text = $this->get_the_post_author_url(); - - break; - case 'ap_blogurl': - $text = \get_bloginfo('url'); - - break; - case 'ap_blogname': - $text = \get_bloginfo('name'); - - break; - case 'ap_blogdesc': - $text = \get_bloginfo('description'); - - break; - case 'ap_date': - $text = $this->get_the_post_date( 'time' ); - - break; - case 'ap_time': - $text = $this->get_the_post_date( 'date' ); - - break; - case 'ap_datetime': - $text = $this->get_the_post_date( 'both' ); - - break; - } - - return $text; - } - /** * Generates the content for the activitypub item. * @@ -334,11 +238,7 @@ class Post { $post = $this->post; $content = $this->get_post_content_template(); - $shortcodes = array( 'ap_title', 'ap_excerpt', 'ap_content', 'ap_permalink', 'ap_shortlink', 'ap_hashtags', 'ap_thumbnail', 'ap_image', 'ap_hashcats', 'ap_author', 'ap_authorurl', 'ap_blogurl', 'ap_blogname', 'ap_blogdesc', 'ap_date', 'ap_time', 'ap_datetime' ); - - foreach( $shortcodes as $tag ) { - add_shortcode( $tag, [ $this, 'shortcode_content' ] ); - } + $shortcodes = new \Activitypub\Shortcodes( $post ); // Fill in the shortcodes. $content = do_shortcode( $content ); @@ -417,254 +317,4 @@ class Post { return $content; } - /** - * Get the excerpt for a post for use outside of the loop. - * - * @param int Optional excerpt length. - * - * @return string The excerpt. - */ - public function get_the_post_excerpt( $excerpt_length = ACTIVITYPUB_EXCERPT_LENGTH ) { - $post = $this->post; - - $excerpt = \get_post_field( 'post_excerpt', $post ); - - if ( '' === $excerpt ) { - - $content = \get_post_field( 'post_content', $post ); - - // 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
as the linebreak just in case there are any newlines existing in the excerpt from the user. - // There won't be any
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, '
' ), '
' ) ); - - // 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; - } - } - - return \apply_filters( 'the_excerpt', $excerpt ); - } - - /** - * Get the content for a post for use outside of the loop. - * - * @return string The content. - */ - public function get_the_post_content() { - $post = $this->post; - - $content = \get_post_field( 'post_content', $post ); - - return \apply_filters( 'the_content', $content ); - } - - /** - * Adds a backlink to the post/summary content - * - * @param string $type - * - * @return string - */ - public function get_the_post_link( $type = 'permalink' ) { - $post = $this->post; - - if ( 'shortlink' === $type ) { - $link = \esc_url( \wp_get_shortlink( $post->ID ) ); - } elseif ( 'permalink' === $type ) { - $link = \esc_url( \get_permalink( $post->ID ) ); - } else { - return ''; - } - - return \sprintf( '%1$s', $link ); - } - - /** - * Adds all tags as hashtags to the post/summary content - * - * @return string - */ - public function get_the_post_hashtags() { - $post = $this->post; - $tags = \get_the_tags( $post->ID ); - - if ( ! $tags ) { - return ''; - } - - $hash_tags = array(); - - foreach ( $tags as $tag ) { - $hash_tags[] = \sprintf( '', \get_tag_link( $tag ), $tag->slug ); - } - - return \implode( ' ', $hash_tags ); - } - - /** - * Adds the featured image url to the post/summary content - * - * @param string $size - * - * @return string - */ - public function get_the_post_image( $size = 'full' ) { - $post = $this->post; - - if( $size == '' ) { $size = 'full'; } - - $image = \get_the_post_thumbnail_url( $post->ID, $size ); - - if ( ! $image ) { - return ''; - } - - return $image; - } - - /** - * Adds all categories as hashtags to the post/summary content - * - * @return string - */ - public function get_the_post_categories() { - $post = $this->post; - $categories = \get_the_category( $post->ID ); - - if ( ! $categories ) { - return ''; - } - - $hash_tags = array(); - - foreach ( $categories as $category ) { - $hash_tags[] = \sprintf( '', \get_category_link( $category ), $category->slug ); - } - - return \implode( ' ', $hash_tags ); - } - - /** - * Adds author to the post/summary content - * - * @return string - */ - public function get_the_post_author() { - $post = $this->post; - $name = \get_the_author_meta( 'display_name', $post->post_author ); - - if ( ! $name ) { - return ''; - } - - return $name; - } - - /** - * Adds author's url to the post/summary content - * - * @return string - */ - public function get_the_post_profile_url() { - $post = $this->post; - $url = \get_the_author_meta( 'user_url', $post->post_author ); - - if ( ! $url ) { - return ''; - } - - return $url; - } - - /** - * Adds the post date/time to the post/summary content - * - * @param string display - * - * @return string - */ - public function get_the_post_date( $display = 'both' ) { - $post = $this->post; - $datetime = \get_post_datetime( $post ); - $dateformat = \get_option( 'date_format' ); - $timeformat = \get_option( 'time_format' ); - - switch( $display ) { - case 'date': - $date = $datetime->format( $dateformat ); - break; - case 'time': - $date = $datetime->format( $timeformat ); - break; - default: - $date = $datetime->format( $dateformat . ' @ ' . $timeformat ); - break; - } - - if ( ! $date ) { - return ''; - } - - return $date; - } - }