janboddez / share-on-mastodon

Easily share WordPress posts on Mastodon.
https://jan.boddez.net/wordpress/share-on-mastodon
GNU General Public License v3.0
39 stars 5 forks source link

Filtering out the title if it is a note #37

Closed jasontucker closed 1 year ago

jasontucker commented 1 year ago

Since the notes have a generated title and I had my notes tooted I was getting redundant titles. I made this modification maybe it may be useful.

`add_filter( 'share_on_mastodon_status', function( $status, $post ) { // Create a short preview of the post

// May be a better way to do this to check if the option is checked in the plugin.
if ( 'indieblocks_notes' === $post->post_type ) {
    $status = "From my notes\n\n";
    //excluding the title of the post, may want to get this from the plugin as well.
}

if ( 'post' === $post->post_type ) {
    $status .= "\"" . get_the_title($post) . "\"\n\n";
}

$status .= get_the_excerpt($post);
//  Remove the … forced by the excerpt and replace with the Unicode symbol
$status = html_entity_decode($status);
//  Add a link
$status .= "\n\n" . get_permalink( $post );
//  Add tags
$tags = get_the_tags( $post->ID );
if ( $tags ) {
    $status .= "\n\n";
    foreach ( $tags as $tag ) {
        $status .= '#' . preg_replace( '/\s/', '', $tag->name ) . ' ';
    }
}
$status = trim( $status );
return $status;

}, 10, 2 );`

janboddez commented 1 year ago

Very nice. Maybe a small typo where it says indieblocks_notes rather than indieblocks_note? Also I think for regular posts you might be appending to the existing status rather than replace it.

I now use something like this (slightly modified and thus "untested").

TL;DR: Notes get their entire status overwritten by the post's content. If I happen to have a Markdown source, I use that. I also remove Jetpack’s location stuff (if applicable). Lastly, for all post types, I add tags and a permalink (if there isn't one, yet, which is only going to be the case for notes).

add_filter( 'share_on_mastodon_status', function( $status, $post ) {
  if ( 'indieblocks_note' === $post->post_type ) { // Or whatever your note type would be.
    // Post types that use Markdown (through Jetpack) have their raw content
    // stored in `post_content_filtered`.
    if ( '1' === get_post_meta( $post->ID, '_wpcom_is_markdown', true ) ) {
      $content = $post->post_content_filtered;
    }

    if ( empty( $content ) ) {
      // Fall back to `post_content`.
      $content = $post->post_content;
    }

    // Prevent Jetpack from appending geolocation data.
    if ( class_exists( 'Jetpack_Geo_Location' ) ) {
      $jp_geo_loc = Jetpack_Geo_Location::init();
      remove_filter( 'the_content', array( $jp_geo_loc, 'the_content_microformat' ) );
    }

    // Apply `the_content` filters so as to have smart quotes and whatnot.
    $status = apply_filters( 'the_content', $content );

    // Strip tags.
    $status = wp_strip_all_tags( $status );
  }

  // Add tags as hashtags.
  $tags = get_the_tags( $post );

  if ( $tags && ! is_wp_error( $tags ) ) {
    $status .= "\n\n";

    foreach ( $tags as $tag ) {
      $tag_name = $tag->name;

      if ( preg_match( '/\s+/', $tag_name ) ) {
        // Try to CamelCase multi-word tags.
        $tag_name = preg_replace( '/\s+/', ' ', $tag_name );
        $tag_name = explode( ' ', $tag_name );
        $tag_name = implode( '', array_map( 'ucfirst', $tag_name ) );
      }

      $status .= '#' . $tag_name . ' ';
    }

    // Remove (leading and) trailing spaces.
    $status = trim( $status );
  }

  if ( false === strpos( $status, get_permalink( $post ) ) ) {
    // Append permalink.
    $status .= "\n\n" . '(' . get_permalink( $post ) . ')';
  }

  // That's it!
  return $status;
}, 99, 2 );
janboddez commented 1 year ago

Almost forgot! Even more inspiration: https://shkspr.mobi/blog/2022/11/better-sharing-of-wordpress-posts-to-mastodon/