Skip to content

Commit

Permalink
Move integration with Autoshare for Twitter to this upstream project.…
Browse files Browse the repository at this point in the history
… Leveraging on the updated hook autoshare_for_twitter_post_tweet_status_updated which passes post_id and tweet_meta.
  • Loading branch information
elvismdev committed Jan 21, 2025
1 parent 5360b0b commit 429bb27
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions includes/class-social-plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public static function init() {
add_filter( 'get_post_syndication_links', array( 'Social_Plugins', 'add_syn_plugins' ) );
add_filter( 'syn_links_url_to_name', array( 'Social_Plugins', 'url_to_name_plugins' ), 10, 2 );
add_action( 'wpt_tweet_posted', array( 'Social_Plugins', 'wptotwitter_to_syn_links' ), 10, 2 );
add_action( 'autoshare_for_twitter_post_tweet_status_updated', array( 'Social_Plugins', 'handle_syndication_after_tweet_status_updated' ), 10, 2 );
}

public static function url_to_name_plugins( $name, $url ) {
Expand Down Expand Up @@ -176,4 +177,45 @@ public static function wptotwitter_to_syn_links( $connection, $id ) {
return add_post_syndication_link( $id, $url );
}

/**
* Check if URL is a valid tweet URL.
*
* @param string $url URL to check.
* @return bool
*/
private static function is_valid_tweet_url( $url ) {
return (
! empty( $url ) &&
false !== strpos( $url, '/status/' ) &&
strlen( $url ) > 30 &&
wp_http_validate_url( $url )
);
}

/**
* Handle syndication after tweet status meta is updated.
*
* @param int $post_id The post ID.
* @param array $tweet_meta The tweet meta array containing tweet status data.
*/
public static function handle_syndication_after_tweet_status_updated( $post_id, $tweet_meta ) {
if ( empty( $tweet_meta ) ) {
return;
}

// Handle both single and multiple tweet formats.
$tweets = isset( $tweet_meta['twitter_id'] ) ? array( $tweet_meta ) : $tweet_meta;

foreach ( $tweets as $tweet ) {
if ( 'published' === $tweet['status'] && ! empty( $tweet['twitter_id'] ) ) {
$uri = \TenUp\AutoshareForTwitter\Utils\link_from_twitter( $tweet );

// Only add valid tweet URLs.
if ( self::is_valid_tweet_url( $uri ) ) {
Syn_Meta::add_syndication_link( get_post_type( $post_id ), $post_id, $uri );
}
}
}
}

} // End Class

0 comments on commit 429bb27

Please sign in to comment.