From dc7f5f568955afe983efdecc1d0074f3796a13b2 Mon Sep 17 00:00:00 2001 From: Joe Dolson Date: Sat, 30 Dec 2023 17:47:23 -0600 Subject: [PATCH] Fix variable passing; add failure condition for mastodon --- src/wp-to-twitter.php | 9 ++++----- src/wpt-post-to-mastodon.php | 12 ++++++++++-- src/wpt-post-to-twitter.php | 3 +++ 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/wp-to-twitter.php b/src/wp-to-twitter.php index a55cc13..27685ee 100644 --- a/src/wp-to-twitter.php +++ b/src/wp-to-twitter.php @@ -451,13 +451,12 @@ function wpt_post_to_twitter( $twit, $auth = false, $id = false, $media = false * @param string $twit Posted status text. */ function wpt_post_submit_handler( $connection, $response, $id, $auth, $twit ) { - $error = $response['error']; $return = $response['return']; $http_code = $response['http']; $notice = $response['notice']; $tweet_id = isset( $response['tweet_id'] ) ? $response['tweet_id'] : false; $status_id = isset( $response['status_id'] ) ? $response['status_id'] : false; - wpt_mail( "X.com Response: $http_code", $error, $id ); // DEBUG. + wpt_mail( "X.com Response: $http_code", $notice, $id ); // DEBUG. // only save last status if successful. if ( 200 === $http_code ) { if ( ! $auth ) { @@ -466,7 +465,7 @@ function wpt_post_submit_handler( $connection, $response, $id, $auth, $twit ) { update_user_meta( $auth, 'wpt_last_tweet', $twit ); } } - wpt_save_error( $id, $auth, $twit, $error, $http_code, time() ); + wpt_save_error( $id, $auth, $twit, $notice, $http_code, time() ); wpt_save_success( $id, $twit, $http_code ); if ( ! $return ) { /** @@ -480,8 +479,8 @@ function wpt_post_submit_handler( $connection, $response, $id, $auth, $twit ) { * @param {int} $id Post ID for status update. * @param {string} $error Error message returned. */ - do_action( 'wpt_tweet_failed', $connection, $id, $error ); - wpt_set_log( 'wpt_status_message', $id, $error ); + do_action( 'wpt_tweet_failed', $connection, $id, $notice ); + wpt_set_log( 'wpt_status_message', $id, $notice ); } else { /** * Executes an action after a status is posted successfully. diff --git a/src/wpt-post-to-mastodon.php b/src/wpt-post-to-mastodon.php index dcb45ab..60e9ca8 100644 --- a/src/wpt-post-to-mastodon.php +++ b/src/wpt-post-to-mastodon.php @@ -104,6 +104,7 @@ function wpt_send_post_to_mastodon( $connection, $auth, $id, $status ) { */ $do_post = apply_filters( 'wpt_do_toot', true, $auth, $id, $status['text'] ); $status_id = false; + $success = false; // Change status array to Mastodon expectation. $status['status'] = $status['text']; unset( $status['text'] ); @@ -121,8 +122,14 @@ function wpt_send_post_to_mastodon( $connection, $auth, $id, $status ) { $status = apply_filters( 'wpt_filter_mastodon_status', $status, $id, $auth ); if ( $do_post ) { $return = $connection->post_status( $status ); - $http_code = 200; - $status_id = $return['id']; + if ( isset( $return['id'] ) ) { + $success = true; + $http_code = 200; + $status_id = $return['id']; + } else { + $http_code = 401; + $notice = __( 'Status update failed.', 'wp-to-twitter' ); + } } else { $http_code = '000'; $notice = __( 'Status Update cancelled by custom filter.', 'wp-to-twitter' ); @@ -130,6 +137,7 @@ function wpt_send_post_to_mastodon( $connection, $auth, $id, $status ) { } return array( + 'return' => $success, 'http' => $http_code, 'notice' => $notice, 'status_id' => $status_id, diff --git a/src/wpt-post-to-twitter.php b/src/wpt-post-to-twitter.php index e679e54..67d8bb7 100644 --- a/src/wpt-post-to-twitter.php +++ b/src/wpt-post-to-twitter.php @@ -100,10 +100,12 @@ function wpt_send_post_to_twitter( $connection, $auth, $id, $status ) { */ $do_tweet = apply_filters( 'wpt_do_tweet', true, $auth, $id, $status['text'] ); $tweet_id = false; + $success = false; if ( $do_tweet ) { try { $return = $connection->tweet()->create()->performRequest( $status, true ); $http_code = 200; + $success = true; $tweet_id = $return->data->id; $headers = $return->headers; $rate_limit = array( @@ -146,6 +148,7 @@ function wpt_send_post_to_twitter( $connection, $auth, $id, $status ) { } return array( + 'return' => $success, 'http' => $http_code, 'notice' => $notice, 'tweet_id' => $tweet_id,