diff --git a/.github/workflows/phpcs.yml b/.github/workflows/phpcs.yml index 8834947..b7392e1 100644 --- a/.github/workflows/phpcs.yml +++ b/.github/workflows/phpcs.yml @@ -21,7 +21,7 @@ jobs: uses: pat-s/always-upload-cache@v1.1.4 with: path: ${{ steps.composer-cache.outputs.dir }} - # Use the hash of composer.json as the key for your cache if you do not commit composer.lock. + # Use the hash of composer.json as the key for your cache if you do not commit composer.lock. key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }} #key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} restore-keys: ${{ runner.os }}-composer- diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 0756e1f..4c70a1e 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -1,7 +1,7 @@ name: Unit Testing on: - push: - pull_request: + push: + pull_request: jobs: phpunit: runs-on: ubuntu-latest diff --git a/activitypub.php b/activitypub.php index 9dd8f82..d855d9f 100644 --- a/activitypub.php +++ b/activitypub.php @@ -19,10 +19,11 @@ namespace Activitypub; * Initialize plugin */ function init() { + \defined( 'ACTIVITYPUB_EXCERPT_LENGTH' ) || \define( 'ACTIVITYPUB_EXCERPT_LENGTH', 400 ); \defined( 'ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS' ) || \define( 'ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS', 3 ); \defined( 'ACTIVITYPUB_HASHTAGS_REGEXP' ) || \define( 'ACTIVITYPUB_HASHTAGS_REGEXP', '(?:(?<=\s)|(?<=
)|(?<= %title% %hashtags% %shortlink% [ap_title] [ap_hashtags] [ap_shortlink] ' . __( 'The following Template Tags are available:', 'activitypub' ) . ' ' . __( 'You may also use any Shortcode normally available to you on your site, however be aware that Shortcodes may significantly increase the size of your content depending on what they do.', 'activitypub' ) . ' ' . __( 'Note: the old Template Tags are now deprecated and automatically converted to the new ones.', 'activitypub' ) . ' ' . \wp_kses( \__( 'Let me know if you miss a template pattern.', 'activitypub' ), 'activitypub' ) . ' %permalink% [ap_permalink] %title% %permalink% [ap_title] [ap_permalink] %hashtags% %permalink% [ap_hashtags] [ap_permalink]
Let me know if you miss a template pattern.', 'activitypub' ), 'default' ); ?>
)|^)#([A-Za-z0-9_]+)(?:(?=\s|[[:punct:]]|$))' );
\defined( 'ACTIVITYPUB_ALLOWED_HTML' ) || \define( 'ACTIVITYPUB_ALLOWED_HTML', '
',
+ \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 static function title( $atts, $content, $tag ) {
+ $post_id = get_the_ID();
+
+ if ( ! $post_id ) {
+ return '';
+ }
+
+ return \get_the_title( $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 static function excerpt( $atts, $content, $tag ) {
+ $post = get_post();
+
+ if ( ! $post ) {
+ return '';
+ }
+
+ $atts = shortcode_atts(
+ array( 'length' => ACTIVITYPUB_EXCERPT_LENGTH ),
+ $atts,
+ $tag
+ );
+
+ $excerpt_length = intval( $atts['length'] );
+
+ if ( 0 === $excerpt_length ) {
+ $excerpt_length = ACTIVITYPUB_EXCERPT_LENGTH;
+ }
+
+ $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 ' );
- \defined( 'ACTIVITYPUB_CUSTOM_POST_CONTENT' ) || \define( 'ACTIVITYPUB_CUSTOM_POST_CONTENT', "
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 static function content( $atts, $content, $tag ) {
+ $post = get_post();
+
+ if ( ! $post ) {
+ return '';
+ }
+
+ $content = \get_post_field( 'post_content', $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 static function permalink( $atts, $content, $tag ) {
+ $post = get_post();
+
+ if ( ! $post ) {
+ return '';
+ }
+
+ $atts = shortcode_atts(
+ array(
+ 'type' => 'url',
+ ),
+ $atts,
+ $tag
+ );
+
+ if ( 'url' === $atts['type'] ) {
+ return \esc_url( \get_permalink( $post->ID ) );
+ }
+
+ return \sprintf( '%1$s', \esc_url( \get_permalink( $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 static function shortlink( $atts, $content, $tag ) {
+ $post = get_post();
+
+ if ( ! $post ) {
+ return '';
+ }
+
+ $atts = shortcode_atts(
+ array(
+ 'type' => 'url',
+ ),
+ $atts,
+ $tag
+ );
+
+ if ( 'url' === $atts['type'] ) {
+ return \esc_url( \wp_get_shortlink( $post->ID ) );
+ }
+
+ return \sprintf( '%1$s', \esc_url( \wp_get_shortlink( $post->ID ) ) );
+ }
+
+ /**
+ * 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 static function image( $atts, $content, $tag ) {
+ $post_id = get_the_ID();
+
+ if ( ! $post_id ) {
+ return '';
+ }
+
+ $atts = shortcode_atts(
+ array(
+ 'type' => 'full',
+ ),
+ $atts,
+ $tag
+ );
+
+ $size = 'full';
+
+ if ( in_array(
+ $atts['type'],
+ array( 'thumbnail', 'medium', 'large', 'full' ),
+ true
+ ) ) {
+ $size = $atts['type'];
+ }
+
+ $image = \get_the_post_thumbnail_url( $post_id, $size );
+
+ if ( ! $image ) {
+ return '';
+ }
+
+ return \esc_url( $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 static function hashcats( $atts, $content, $tag ) {
+ $post_id = get_the_ID();
+
+ if ( ! $post_id ) {
+ return '';
+ }
+
+ $categories = \get_the_category( $post_id );
+
+ if ( ! $categories ) {
+ return '';
+ }
+
+ $hash_tags = array();
+
+ foreach ( $categories as $category ) {
+ $hash_tags[] = \sprintf( '#%s', \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 static function author( $atts, $content, $tag ) {
+ $post = get_post();
+
+ if ( ! $post ) {
+ return '';
+ }
+
+ $name = \get_the_author_meta( 'display_name', $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 static function authorurl( $atts, $content, $tag ) {
+ $post = get_post();
+
+ if ( ! $post ) {
+ return '';
+ }
+
+ $url = \get_the_author_meta( 'user_url', $post->post_author );
+
+ if ( ! $url ) {
+ return '';
+ }
+
+ return \esc_url( $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 static function blogurl( $atts, $content, $tag ) {
+ return \esc_url( \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 static 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 static 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 static function date( $atts, $content, $tag ) {
+ $post = get_post();
+
+ if ( ! $post ) {
+ return '';
+ }
+
+ $datetime = \get_post_datetime( $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 static function time( $atts, $content, $tag ) {
+ $post = get_post();
+
+ if ( ! $post ) {
+ return '';
+ }
+
+ $datetime = \get_post_datetime( $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 static function datetime( $atts, $content, $tag ) {
+ $post = get_post();
+
+ if ( ! $post ) {
+ return '';
+ }
+
+ $datetime = \get_post_datetime( $post );
+ $dateformat = \get_option( 'date_format' );
+ $timeformat = \get_option( 'time_format' );
+
+ $date = $datetime->format( $dateformat . ' @ ' . $timeformat );
+
+ if ( ! $date ) {
+ return '';
+ }
+
+ return $date;
+ }
+}
diff --git a/includes/help.php b/includes/help.php
index f84ccaf..1f29119 100644
--- a/includes/help.php
+++ b/includes/help.php
@@ -1,5 +1,51 @@
add_help_tab(
+ array(
+ 'id' => 'template-tags',
+ 'title' => \__( 'Template Tags', 'activitypub' ),
+ 'content' =>
+ '
+
' .
+ '[ap_title]
[ap_content]
[ap_excerpt lenght="400"]
[ap_permalink type="url"]
url
or html
(an <a /> tag to the url).', 'activitypub' ), 'default' ) . '[ap_shortlink type="url"]
url
or html
(an <a /> tag to the url). I can recommend Hum, to prettify the Shortlinks.', 'activitypub' ), 'default' ) . '[ap_hashtags]
[ap_hashcats]
[ap_image type=full]
thumbnail
, medium
, large
, full
', 'activitypub' ), 'default' ) . '[ap_author]
[ap_authorurl]
[ap_date]
[ap_time]
[ap_datetime]
[ap_blogurl]
[ap_blogname]
[ap_blogdesc]
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 );
+ return $content;
}
- /**
- * 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 $content
- * @param WP_Post $post
- *
- * @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
- *
- * @param string $content
- * @param WP_Post $post
- *
- * @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( '#%s', \get_tag_link( $tag ), $tag->slug );
- }
-
- return \implode( ' ', $hash_tags );
- }
}
diff --git a/readme.txt b/readme.txt
index 7ef54de..bf786ca 100644
--- a/readme.txt
+++ b/readme.txt
@@ -91,6 +91,7 @@ Project maintained on GitHub at [pfefferle/wordpress-activitypub](https://github
= v.next =
* Add configuration item for number of images to attach. props [@mexon](https://github.com/mexon)
+* Use shortcodes instead of custom templates, to setup the Activity Post-Content. props [@toolstack](https://github.com/toolstack)
= 0.15.0 =
diff --git a/templates/settings.php b/templates/settings.php
index 809ab15..24cd5c4 100644
--- a/templates/settings.php
+++ b/templates/settings.php
@@ -56,22 +56,22 @@
+
-
+
%title%
- %content%
- %excerpt%
- %permalink%
- %shortlink%
- Hum, to prettify the Shortlinks', 'activitypub' ), 'default' ); ?>%hashtags%
- [ap_title]
- [ap_content]
- [ap_excerpt]
- [ap_permalink]
- [ap_shortlink]
- Hum.', 'activitypub' ), 'default' ); ?>[ap_hashtags]
- [ap_hashcats]
- [ap_image]
-