Skip to content

Commit

Permalink
Debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
janboddez committed Sep 29, 2024
1 parent f94ceeb commit 10802da
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions includes/class-post-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public function toot( $post ) {

if ( 0 === strpos( current_action(), 'save_' ) && defined( 'REST_REQUEST' ) && REST_REQUEST ) {
// For REST requests, we use a *later* hook, which runs *after* metadata, if any, has been saved.
debug_log( "[Share on Mastodon] Delaying scheduling to `rest_after_insert_{$post->post_type}`." );
add_action( "rest_after_insert_{$post->post_type}", array( $this, 'toot' ), 20 );

// Don't do anything just yet.
Expand All @@ -113,6 +114,7 @@ public function toot( $post ) {

$options = get_options();
if ( ! empty( $options['meta_box'] && $this->is_gutenberg() && empty( $_REQUEST['meta-box-loader'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
debug_log( '[Share on Mastodon] First of two expected requests. Quitting.' );
// This has to be the first of *two* "Gutenberg requests," and we should ignore it. Note: It could be that
// `$this->is_gutenberg()` always returns `false` whenever `$_REQUEST['meta-box-loader']` is present.
// Still, doesn't hurt to check.
Expand All @@ -125,6 +127,7 @@ public function toot( $post ) {
}

if ( ! $this->setup_completed( $post ) ) {
debug_log( '[Share on Mastodon] Setup incomplete.' );
return;
}

Expand Down Expand Up @@ -161,6 +164,7 @@ public function post_to_mastodon( $post ) {
}

if ( ! $this->is_valid( $post ) ) {
debug_log( '[Share on Mastodon] Skipping post.' );
return;
}

Expand Down Expand Up @@ -241,6 +245,8 @@ public function post_to_mastodon( $post ) {
}
}

debug_log( '[Share on Mastodon] Posting to Mastodon ...' );

$response = wp_safe_remote_post(
esc_url_raw( $options['mastodon_host'] . '/api/v1/statuses' ),
array(
Expand All @@ -264,6 +270,8 @@ public function post_to_mastodon( $post ) {
$status = json_decode( $response['body'] );

if ( ! empty( $status->url ) ) {
debug_log( "[Share on Mastodon] Post shared OK at {$status->url}." );

delete_post_meta( $post->ID, '_share_on_mastodon_error' );
update_post_meta( $post->ID, '_share_on_mastodon_url', esc_url_raw( $status->url ) );

Expand All @@ -282,6 +290,8 @@ public function post_to_mastodon( $post ) {
// Provided debugging's enabled, let's store the (somehow faulty) response.
debug_log( $response );
}

debug_log( '[Share on Mastodon] All done!' );
}

/**
Expand Down Expand Up @@ -478,29 +488,34 @@ public function enqueue_scripts( $hook_suffix ) {
protected function is_valid( $post ) {
if ( 'publish' !== $post->post_status ) {
// Status is something other than `publish`.
debug_log( '[Share on Mastodon] Post not public.' );
return false;
}

if ( post_password_required( $post ) ) {
// Post is password-protected.
debug_log( '[Share on Mastodon] Post password-protected.' );
return false;
}

$options = get_options( $post->post_author );

if ( ! in_array( $post->post_type, (array) $options['post_types'], true ) ) {
// Unsupported post type.
debug_log( '[Share on Mastodon] Unsupported post type.' );
return false;
}

if ( '' !== get_post_meta( $post->ID, '_share_on_mastodon_url', true ) ) {
// Was shared before (and not "unlinked").
debug_log( '[Share on Mastodon] Post shared before.' );
return false;
}

if ( is_older_than( DAY_IN_SECONDS / 2, $post ) && '1' !== get_post_meta( $post->ID, '_share_on_mastodon', true ) ) {
// Unless the box was ticked explicitly, we won't share "older" posts. Since v0.13.0, sharing "older" posts
// is "opt-in," always.
debug_log( '[Share on Mastodon] Preventing older post from being shared automatically.' );
return false;
}

Expand Down Expand Up @@ -652,6 +667,10 @@ protected function is_gutenberg() {
return false;
}

if ( wp_doing_cron() ) {
return false;
}

$nonce = null;

if ( isset( $_REQUEST['_wpnonce'] ) ) {
Expand Down

0 comments on commit 10802da

Please sign in to comment.