From 9a658e50a0908b591c0f14a699457eb0b6c9b4a3 Mon Sep 17 00:00:00 2001 From: Joe Dolson Date: Fri, 29 Dec 2023 15:56:59 -0600 Subject: [PATCH] Sniffs; load mastodon class --- ...astodon.php => class-wpt-mastodon-api.php} | 33 ++++++++++++++----- src/wpt-post-to-mastodon.php | 16 +++++---- src/wpt-post-to-twitter.php | 6 ++-- 3 files changed, 37 insertions(+), 18 deletions(-) rename src/classes/{class-mastodon.php => class-wpt-mastodon-api.php} (75%) diff --git a/src/classes/class-mastodon.php b/src/classes/class-wpt-mastodon-api.php similarity index 75% rename from src/classes/class-mastodon.php rename to src/classes/class-wpt-mastodon-api.php index 1fdd9eb..baf5c70 100644 --- a/src/classes/class-mastodon.php +++ b/src/classes/class-wpt-mastodon-api.php @@ -9,7 +9,7 @@ * @link https://www.joedolson.com/wp-to-twitter/ */ - if ( ! defined( 'ABSPATH' ) ) { +if ( ! defined( 'ABSPATH' ) ) { exit; } @@ -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; @@ -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 ); } /** @@ -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', diff --git a/src/wpt-post-to-mastodon.php b/src/wpt-post-to-mastodon.php index 2ff342b..a624e39 100644 --- a/src/wpt-post-to-mastodon.php +++ b/src/wpt-post-to-mastodon.php @@ -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. * @@ -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, ); @@ -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. @@ -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']; @@ -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' ); diff --git a/src/wpt-post-to-twitter.php b/src/wpt-post-to-twitter.php index 11b7da6..e679e54 100644 --- a/src/wpt-post-to-twitter.php +++ b/src/wpt-post-to-twitter.php @@ -9,7 +9,7 @@ * @link https://www.xposter.com */ - if ( ! defined( 'ABSPATH' ) ) { +if ( ! defined( 'ABSPATH' ) ) { exit; } // Exit if accessed directly. @@ -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(