Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Autoshare For Twitter Support #221

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 55 additions & 25 deletions includes/class-social-plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +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_after_status_update', array( 'Social_Plugins', 'autoshare_for_twitter_after_status_update' ), 10, 3 );
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 All @@ -21,18 +21,18 @@ public static function url_to_name_plugins( $name, $url ) {
}
}

public static function array_flatten( $array ) {
if ( ! is_array( $array ) ) {
return false;
}
$result = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$result = array_merge($result, self::array_flatten($value));
} else {
$result[$key] = $value;
}
}
public static function array_flatten( $array ) {
if ( ! is_array( $array ) ) {
return false;
}
$result = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$result = array_merge($result, self::array_flatten($value));
} else {
$result[$key] = $value;
}
}
return $result;
}

Expand All @@ -46,7 +46,7 @@ public static function add_syn_plugins( $urls ) {
return $urls;
}
$keys = array_keys( $keys );

foreach( $keys as $key ) {
if ( 0 === strpos( $key, 'snap' ) && 6 === strlen( $key ) ) {
$meta = get_post_meta( get_the_ID(), $key, true );
Expand Down Expand Up @@ -86,17 +86,6 @@ public static function add_syn_plugins( $urls ) {
return array_merge( $see_on, $urls );
}

public static function autoshare_for_twitter_after_status_update( $response, $update_data, $post ) {
$post = get_post( $post );
if ( $post ) {
$data = get_post_meta( $post->ID, 'autoshare_status', true );
$tweet_id = $tweet_status['twitter_id'] ?? '';
$handle = $tweet_status['handle'] ?? 'i/web';
$url = esc_url( 'https://twitter.com/' . $handle . '/status/' . $tweet_id );
add_post_syndication_link( $post->ID, $url );
}
}

public static function add_links_from_snap() {
global $nxs_snapAvNts;
global $post;
Expand Down Expand Up @@ -188,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