Skip to content

Commit

Permalink
Sniffs; load mastodon class
Browse files Browse the repository at this point in the history
  • Loading branch information
joedolson committed Dec 29, 2023
1 parent 039acf7 commit 9a658e5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @link https://www.joedolson.com/wp-to-twitter/
*/

if ( ! defined( 'ABSPATH' ) ) {
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

Expand All @@ -23,9 +23,26 @@
* @link https://github.com/Eleirbag89/MastodonBotPHP
*/
class Wpt_Mastodon_Api {
/**
* Access token for Mastodon instance.
*
* @var string
*/
private $token;

/**
* URL for instance root.
*
* @var string
*/
private $instance_url;

/**
* Construct.
*
* @param string $token Access token for Mastodon instance.
* @param string $instance_url URL to instance root.
*/
public function __construct( $token, $instance_url ) {
$this->token = $token;
$this->instance_url = $instance_url;
Expand All @@ -34,23 +51,23 @@ public function __construct( $token, $instance_url ) {
/**
* Post a status to the mastodon status endpoint.
*
* @param array $status Array posted to Mastodon. [status,visibility,language,media_ids="[]"]
* @param array $status Array posted to Mastodon. [status,visibility,language,media_ids="[]"].
*
* @return array Mastodon response.
*/
public function postStatus( $status ) {
return $this->callAPI( '/api/v1/statuses', 'POST', $status );
public function post_status( $status ) {
return $this->call_api( '/api/v1/statuses', 'POST', $status );
}

/**
* Post a media attachment to the mastodon status endpoint.
*
* @param array $media Array of media data posted to Mastodon. [file,description]
* @param array $media Array of media data posted to Mastodon. [file,description].
*
* @return array Mastodon response.
*/
public function uploadMedia( $media ) {
return $this->callAPI( '/api/v1/media', 'POST', $media );
public function upload_media( $media ) {
return $this->call_api( '/api/v1/media', 'POST', $media );
}

/**
Expand All @@ -62,7 +79,7 @@ public function uploadMedia( $media ) {
*
* @return array Mastodon response or error.
*/
public function callAPI( $endpoint, $method, $data ) {
public function call_api( $endpoint, $method, $data ) {
$headers = array(
'Authorization: Bearer ' . $this->token,
'Content-Type: multipart/form-data',
Expand Down
16 changes: 9 additions & 7 deletions src/wpt-post-to-mastodon.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
* @link https://www.xposter.com
*/

if ( ! defined( 'ABSPATH' ) ) {
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly.

require_once( plugin_dir_path( __FILE__ ) . 'classes/class-wpt-mastodon-api.php' );

/**
* Upload media to Mastodon API.
*
Expand Down Expand Up @@ -44,7 +46,7 @@ function wpt_upload_mastodon_media( $connection, $auth, $attachment, $status, $i
if ( ! $attachment_data ) {
return $status;
}
$request = array(
$request = array(
'file' => $attachment_data,
'description' => $alt_text,
);
Expand Down Expand Up @@ -86,7 +88,7 @@ function wpt_send_post_to_mastodon( $connection, $auth, $id, $status ) {
$connection = true;
$http_code = 200;
$notice = __( 'In Staging Mode:', 'wp-to-twitter' ) . ' ';
$status_id = false;
$status_id = false;
} else {
/**
* Filter the approval to send a Mastodon Toot.
Expand All @@ -99,7 +101,7 @@ function wpt_send_post_to_mastodon( $connection, $auth, $id, $status ) {
*
* @return {bool}
*/
$do_post = apply_filters( 'wpt_do_toot', true, $auth, $id, $status['text'] );
$do_post = apply_filters( 'wpt_do_toot', true, $auth, $id, $status['text'] );
$status_id = false;
// Change status array to Mastodon expectation.
$status['status'] = $status['text'];
Expand All @@ -117,9 +119,9 @@ 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->postStatus( $status );
$http_code = 200;
$status_id = $return->data->id;
$return = $connection->postStatus( $status );
$http_code = 200;
$status_id = $return->data->id;
} else {
$http_code = '000';
$notice = __( 'Status Update cancelled by custom filter.', 'wp-to-twitter' );
Expand Down
6 changes: 3 additions & 3 deletions src/wpt-post-to-twitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @link https://www.xposter.com
*/

if ( ! defined( 'ABSPATH' ) ) {
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly.

Expand All @@ -33,8 +33,8 @@ function wpt_upload_twitter_media( $connection, $auth, $attachment, $status, $id
if ( ! $attachment_data ) {
return $status;
}
$media_info = $connection->uploadMedia()->upload( $attachment_data );
$status = array(
$media_info = $connection->uploadMedia()->upload( $attachment_data );
$status = array(
'text' => $text,
'media' => array(
'media_ids' => array(
Expand Down

0 comments on commit 9a658e5

Please sign in to comment.