Skip to content

Commit

Permalink
Fix variable passing; add failure condition for mastodon
Browse files Browse the repository at this point in the history
  • Loading branch information
joedolson committed Dec 30, 2023
1 parent a95716d commit dc7f5f5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
9 changes: 4 additions & 5 deletions src/wp-to-twitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand All @@ -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 ) {
/**
Expand All @@ -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.
Expand Down
12 changes: 10 additions & 2 deletions src/wpt-post-to-mastodon.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] );
Expand All @@ -121,15 +122,22 @@ 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' );
}
}

return array(
'return' => $success,
'http' => $http_code,
'notice' => $notice,
'status_id' => $status_id,
Expand Down
3 changes: 3 additions & 0 deletions src/wpt-post-to-twitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit dc7f5f5

Please sign in to comment.