Skip to content

Commit

Permalink
Return data for both X and Mastodon; show separate messages in test r…
Browse files Browse the repository at this point in the history
…outes
  • Loading branch information
joedolson committed Dec 30, 2023
1 parent dc7f5f5 commit 6bf7a66
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
13 changes: 8 additions & 5 deletions src/wp-to-twitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ function wpt_check_recent_tweet( $id, $auth ) {
* @param int $id Post ID.
* @param boolean $media Whether to upload media attached to the post specified in $id.
*
* @return boolean Success of query.
* @return boolean|array False if blocked, array of statuses if attempted.
*/
function wpt_post_to_service( $text, $auth = false, $id = false, $media = false ) {
$return = wpt_post_to_twitter( $text, $auth, $id, $media );
Expand All @@ -336,10 +336,11 @@ function wpt_post_to_service( $text, $auth = false, $id = false, $media = false
* @param int $id Post ID.
* @param boolean $media Whether to upload media attached to the post specified in $id.
*
* @return boolean Success of query.
* @return boolean|array False if blocked, array of statuses if attempted.
*/
function wpt_post_to_twitter( $twit, $auth = false, $id = false, $media = false ) {
// If an ID is set but the post is not currently present or published, ignore.
$return = array();
if ( $id ) {
$status = get_post_status( $id );
if ( ! $status || 'publish' !== $status ) {
Expand Down Expand Up @@ -422,17 +423,19 @@ function wpt_post_to_twitter( $twit, $auth = false, $id = false, $media = false
$status = wpt_upload_twitter_media( $connection, $auth, $attachment, $status, $id );
$response = wpt_send_post_to_twitter( $connection, $auth, $id, $status );
wpt_post_submit_handler( $connection, $response, $id, $auth, $twit );
$return['xcom'] = $response;
}
if ( wpt_mastodon_connection( $auth ) ) {
$connection = wpt_mastodon_connection( $auth );
$status = wpt_upload_mastodon_media( $connection, $auth, $attachment, $status, $id );
$response = wpt_send_post_to_mastodon( $connection, $auth, $id, $status );
wpt_post_submit_handler( $connection, $response, $id, $auth, $twit );
$return['mastodon'] = $response;
}
wpt_mail( 'X Connection', "$twit, $auth, $id, $media", $id );
if ( $connection ) {
if ( ! empty( $return ) ) {

return $response['return'];
return $return;
} else {
wpt_set_log( 'wpt_status_message', $id, __( 'No API connection found.', 'wp-to-twitter' ) );

Expand Down Expand Up @@ -501,7 +504,7 @@ function wpt_post_submit_handler( $connection, $response, $id, $auth, $twit ) {
if ( ! $has_status_id && $status_id ) {
update_post_meta( $id, '_wpt_status_id', $status_id );
}
wpt_set_log( 'wpt_status_message', $id, $notice . __( 'Status sent successfully.', 'wp-to-twitter' ) );
wpt_set_log( 'wpt_status_message', $id, $notice . ' ' . __( 'Status sent successfully.', 'wp-to-twitter' ) );
}
}

Expand Down
13 changes: 10 additions & 3 deletions src/wpt-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,18 @@ function wpt_check_functions() {
if ( wtt_oauth_test() || wpt_mastodon_connection() ) {
$rand = wp_rand( 1000000, 9999999 );
$testpost = wpt_post_to_service( "This is a test of XPoster. $shrink ($rand)" );
if ( $testpost ) {
$message .= '<li><strong>' . __( 'XPoster successfully submitted a status update to X.com.', 'wp-to-twitter' ) . '</strong></li>';
if ( $testpost && ! empty( $testpost ) ) {
foreach ( $testpost as $key => $test ) {
if ( 'xcom' === $key ) {
$message .= '<li><strong>' . __( 'XPoster successfully submitted a status update to X.com.', 'wp-to-twitter' ) . '</strong></li>';
}
if ( 'mastodon' === $key ) {
$message .= '<li><strong>' . __( 'XPoster successfully submitted a status update to your Mastodon instance.', 'wp-to-twitter' ) . '</strong></li>';
}
}
} else {
$error = wpt_get_log( 'wpt_status_message', 'test' );
$message .= '<li class="error"><strong>' . __( 'XPoster failed to submit an update to X.com.', 'wp-to-twitter' ) . '</strong></li>';
$message .= '<li class="error"><strong>' . __( 'XPoster failed to submit status updates.', 'wp-to-twitter' ) . '</strong></li>';
$message .= "<li class='error'>$error</li>";
}
} else {
Expand Down
6 changes: 4 additions & 2 deletions src/wpt-post-to-mastodon.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ function wpt_upload_mastodon_media( $connection, $auth, $attachment, $status, $i
* @return array
*/
function wpt_send_post_to_mastodon( $connection, $auth, $id, $status ) {
$notice = '';
/**
* Turn on staging mode. Staging mode is automatically turned on if WPT_STAGING_MODE constant is defined.
*
Expand Down Expand Up @@ -126,13 +127,14 @@ function wpt_send_post_to_mastodon( $connection, $auth, $id, $status ) {
$success = true;
$http_code = 200;
$status_id = $return['id'];
$notice .= __( 'Successful status update sent to Mastodon.', 'wp-to-twitter' );
} else {
$http_code = 401;
$notice = __( 'Status update failed.', 'wp-to-twitter' );
$notice .= __( 'Mastodon status update failed.', 'wp-to-twitter' );
}
} else {
$http_code = '000';
$notice = __( 'Status Update cancelled by custom filter.', 'wp-to-twitter' );
$notice .= __( 'Mastodon status update cancelled by custom filter.', 'wp-to-twitter' );
}
}

Expand Down

0 comments on commit 6bf7a66

Please sign in to comment.