Skip to content

Commit

Permalink
Merge pull request #1032 from Codeinwp/feat/mastodon-api
Browse files Browse the repository at this point in the history
[Mastodon API] add share link in first comment
  • Loading branch information
vytisbulkevicius authored Feb 13, 2025
2 parents ce437c9 + f673c3b commit 9354e53
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
40 changes: 38 additions & 2 deletions includes/admin/services/class-rop-mastodon-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,10 @@ private function get_oauth_url() {
*/
private function mastodon_article_post( $post_details ) {

$new_post['status'] = $this->strip_excess_blank_lines( $post_details['content'] ) . $this->get_url( $post_details );
$new_post['status'] = $this->strip_excess_blank_lines( $post_details['content'] );
if ( empty( $this->share_link_text ) ) {
$new_post['status'] .= $this->get_url( $post_details );
}
$new_post['visibility'] = 'public';
return $new_post;
}
Expand Down Expand Up @@ -623,10 +626,14 @@ private function mastodon_media_post( $post_details ) {
}

$new_post = array(
'status' => $post_details['content'] . $this->get_url( $post_details ),
'status' => $post_details['content'],
'visibility' => 'public',
);

if ( empty( $this->share_link_text ) ) {
$new_post['status'] .= $this->get_url( $post_details );
}

$media_id = $this->upload_media( $attachment_path );
if ( $media_id > 0 ) {
$new_post['media_ids'] = array( $media_id );
Expand Down Expand Up @@ -763,6 +770,13 @@ public function share( $post_details, $args = array() ) {
$post_url = $post_details['post_url'];
$share_as_image_post = $post_details['post_with_image'];

$model = new Rop_Post_Format_Model();
$post_format = $model->get_post_format( $post_details['account_id'] );

if ( ! empty( $post_format['share_link_in_comment'] ) && ! empty( $post_format['share_link_text'] ) ) {
$this->share_link_text = str_replace( '{link}', self::get_url( $post_details ), $post_format['share_link_text'] );
}

// Mastodon link post.
if ( ! empty( $post_url ) && empty( $share_as_image_post ) && get_post_type( $post_id ) !== 'attachment' ) {
$new_post = $this->mastodon_article_post( $post_details );
Expand Down Expand Up @@ -799,6 +813,27 @@ public function share( $post_details, $args = array() ) {
$response = $this->request_new_post( $new_post );

if ( isset( $response->id ) ) {
// Create the first comment if the share link text is not empty.
if ( ! empty( $this->share_link_text ) ) {
$create_reply = $this->request_new_post(
array(
'status' => $this->share_link_text,
'in_reply_to_id' => $response->id,
)
);
$this->logger->info( sprintf( '[Mastodon reply API] Response: %s', json_encode( $create_reply ) ) );

if ( $create_reply && isset( $create_reply->id ) ) {
$this->logger->info(
sprintf(
'Successfully shared first comment to %s on %s ',
html_entity_decode( get_the_title( $post_details['post_id'] ) ),
$post_details['service']
)
);
}
}

// Save log.
$this->save_logs_on_rop(
array(
Expand All @@ -808,6 +843,7 @@ public function share( $post_details, $args = array() ) {
'link' => $post_details['post_url'],
)
);

$this->logger->alert_success(
sprintf(
'Successfully shared %s to %s on %s ',
Expand Down
2 changes: 1 addition & 1 deletion vue/src/vue-elements/post-format.vue
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@
selected_tax_filter: [],
// selected_language: this.$store.state.activePostFormat[this.account_id] ? this.$store.state.activePostFormat[this.account_id].wpml_language : [],
// post_types: this.$store.state.generalSettings.available_post_types,
postCommentSupportedServices: [ 'twitter', 'facebook', 'linkedin', 'vk' ],
postCommentSupportedServices: [ 'twitter', 'facebook', 'linkedin', 'vk', 'mastodon' ],
}
},
computed: {
Expand Down

0 comments on commit 9354e53

Please sign in to comment.