Skip to content

Commit

Permalink
Hook into autoshare_for_twitter_post_tweet_status_updated action to h…
Browse files Browse the repository at this point in the history
…andle syndication links.
  • Loading branch information
elvismdev committed Jan 6, 2025
1 parent 535841d commit ccda7ff
Showing 1 changed file with 10 additions and 50 deletions.
60 changes: 10 additions & 50 deletions includes/class-syndication-links.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,69 +10,28 @@
namespace TenUp\AutoshareForTwitter\Core\Syndication;

use TenUp\AutoshareForTwitter\Utils;
use const TenUp\AutoshareForTwitter\Core\Post_Meta\TWITTER_STATUS_KEY;

/**
* Class Syndication_Links
*/
class Syndication_Links {

/**
* Initialize the hooks.
* Initialize the hook.
*/
public static function init() {
if ( ! class_exists( 'Syn_Meta' ) ) {
return;
}

// Hook for when post is first published.
add_action( 'transition_post_status', [ __CLASS__, 'handle_syndication_on_publish' ], 20, 3 );

// Hook for when tweet is posted (covers both initial publish and retweet scenarios).
add_action( 'autoshare_for_twitter_after_status_update', [ __CLASS__, 'handle_syndication_after_tweet' ], 10, 3 );
}

/**
* Handle adding syndication links when post is published.
*
* @param string $new_status New post status.
* @param string $old_status Old post status.
* @param WP_Post $post Post object.
*/
public static function handle_syndication_on_publish( $new_status, $old_status, $post ) {
if ( 'publish' !== $new_status || 'publish' === $old_status ) {
return;
}

// Wait a moment to ensure tweet is processed.
add_action(
'shutdown',
function() use ( $post ) {
self::add_tweet_to_syndication_links( $post->ID );
}
);
}

/**
* Handle syndication after tweet is posted.
*
* @param object $response Twitter API response.
* @param array $update_data Tweet data.
* @param WP_Post $post Post object.
*/
public static function handle_syndication_after_tweet( $response, $update_data, $post ) {
self::add_tweet_to_syndication_links( $post->ID );
// Hook for when tweet is posted and saved into post metadata.
add_action( 'autoshare_for_twitter_post_tweet_status_updated', [ __CLASS__, 'handle_syndication_after_tweet_status_updated' ], 10, 2 );
}

/**
* Add tweet URL to syndication links.
* Handle syndication after tweet status meta is updated.
*
* @param int $post_id Post ID.
* @param int $post_id The post ID.
* @param array $tweet_meta The tweet meta array containing tweet status data.
*/
public static function add_tweet_to_syndication_links( $post_id ) {
$tweet_meta = Utils\get_autoshare_for_twitter_meta( $post_id, TWITTER_STATUS_KEY );

if ( empty( $tweet_meta ) || ! is_array( $tweet_meta ) ) {
public static function handle_syndication_after_tweet_status_updated( $post_id, $tweet_meta ) {
if ( empty( $tweet_meta ) ) {
return;
}

Expand All @@ -97,8 +56,9 @@ public static function add_tweet_to_syndication_links( $post_id ) {
* @param string $url URL to check.
* @return bool
*/
public static function is_valid_tweet_url( $url ) {
private static function is_valid_tweet_url( $url ) {
return (
! empty( $url ) &&
false !== strpos( $url, '/status/' ) &&
strlen( $url ) > 30 &&
wp_http_validate_url( $url )
Expand Down

0 comments on commit ccda7ff

Please sign in to comment.