Skip to content

Instantly share code, notes, and snippets.

@janboddez
Last active May 25, 2023 12:02
Show Gist options
  • Select an option

  • Save janboddez/984843ebc7271f0cc7ab8856269e37d0 to your computer and use it in GitHub Desktop.

Select an option

Save janboddez/984843ebc7271f0cc7ab8856269e37d0 to your computer and use it in GitHub Desktop.
<?php
add_action( 'activitypub_safe_remote_post_response', function( $response, $url, $body, $user_id ) {
$activity = json_decode( $body );
if ( empty( $activity->type ) || 'Create' !== $activity->type ) {
error_log( 'Not a Create activity.' );
return;
}
$url = $activity->object->id;
$post_id = url_to_postid( $url );
// error_log( $url );
// error_log( $post_id );
if ( '' !== get_post_meta( $post_id, '_share_on_mastodon_boosted', true ) ) {
error_log( 'Already boosted.' );
return;
}
if ( ! filter_var( $url, FILTER_VALIDATE_URL ) ) {
error_log( 'Invalid URL.' );
return;
}
if ( ! class_exists( '\\Share_On_Mastodon\\Share_On_Mastodon' ) ) {
error_log( 'Share on Mastodon not active.' );
return;
}
$options = \Share_On_Mastodon\get_options();
if ( empty( $options['mastodon_host'] ) ) {
error_log( 'Missing Mastodon instance.' );
return;
}
if ( empty( $options['mastodon_access_token'] ) ) {
error_log( 'Missing Mastodon token.' );
return;
}
$args = array(
'q' => $url,
'type' => 'statuses',
'resolve' => true,
);
// Ask our Mastodon instance to "search" for this URL. This should also add it to the instance's database, if it doesn't already exist.
$response = wp_remote_get(
esc_url_raw( $options['mastodon_host'] . '/api/v2/search' ) . '?' . http_build_query( $args ), // @todo: Use `add_query_arg()`?
array(
'headers' => array(
'Authorization' => 'Bearer ' . $options['mastodon_access_token'],
),
'timeout' => 15,
)
);
\Share_On_Mastodon\debug_log( $response );
if ( is_wp_error( $response ) ) {
// Something went wrong.
\Share_On_Mastodon\debug_log( $response );
update_post_meta( $post_id, '_share_on_mastodon_boosted', '0' ); // On failure.
return;
}
$json = json_decode( $response['body'], true );
if ( ! empty( $json['statuses'][0]['id'] ) && 1 === count( $json['statuses'] ) ) {
// Our instance returned a single status. We need its ID in order to boost it.
$response = wp_remote_post(
esc_url_raw( $options['mastodon_host'] . "/api/v1/statuses/{$json['statuses'][0]['id']}/reblog" ),
array(
'headers' => array(
'Authorization' => 'Bearer ' . $options['mastodon_access_token'],
),
'timeout' => 15,
)
);
// @todo: Check the response code instead.
if ( is_wp_error( $response ) ) {
// Something went wrong.
\Share_On_Mastodon\debug_log( $response );
update_post_meta( $post_id, '_share_on_mastodon_boosted', '0' ); // On failure.
return;
} elseif ( ! empty( $response['body'] ) ) {
\Share_On_Mastodon\debug_log( $response['body'] );
update_post_meta( $post_id, '_share_on_mastodon_boosted', sanitize_text_field( $json['statuses'][0]['id'] ) ); // On success.
}
}
update_post_meta( $post_id, '_share_on_mastodon_boosted', '0' ); // On failure.
}, 999, 4 );
@janboddez
Copy link
Author

Does not work if the app (the Share on Mastodon plugin) was registered without read:search scope! I.e., requires some manual hacking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment