From 88ce7049cf4dc933a99c12c8899adcdc9b76d31a Mon Sep 17 00:00:00 2001 From: Tung Du Date: Wed, 14 Jul 2021 12:14:47 +0700 Subject: [PATCH] fix: sending publish event with Yoast SEO activated --- includes/functions/content-sync.php | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/includes/functions/content-sync.php b/includes/functions/content-sync.php index e9aa65e0..64086cef 100644 --- a/includes/functions/content-sync.php +++ b/includes/functions/content-sync.php @@ -40,6 +40,12 @@ function setup() { * @return null|WP_Error */ function track_event( $new_status, $old_status, $post ) { + + // Don't send any event when creating new article. + if ( 'auto-draft' === $new_status || 'inherit' === $new_status ) { + return; + } + $tracker = init_tracker(); $action = ''; @@ -55,7 +61,6 @@ function track_event( $new_status, $old_status, $post ) { } // publish, update, delete or unpublish - if ( 'publish' === $new_status && 'publish' !== $old_status ) { $action = 'publish'; } elseif ( 'publish' === $new_status && 'publish' === $old_status ) { @@ -74,6 +79,13 @@ function track_event( $new_status, $old_status, $post ) { } if ( class_exists( 'WPSEO_Meta' ) ) { + $pending_action = get_transient( 'sophi_content_sync_pending_' . $post->ID ); + + // Only set temporary action when publishing content + if ( ! $pending_action && 'publish' === $action ) { + set_transient( 'sophi_content_sync_pending_' . $post->ID, $action, MINUTE_IN_SECONDS ); + } + return add_action( 'wpseo_saved_postdata', function() use ( $tracker, $post, $action ) { send_track_event( $tracker, $post, $action ); } ); @@ -92,9 +104,15 @@ function track_event( $new_status, $old_status, $post ) { * @param string $action Publishing action. */ function send_track_event( $tracker, $post, $action ) { + $pending_action = get_transient( 'sophi_content_sync_pending_' . $post->ID ); $data = get_post_data( $post ); $data['action'] = $action; + if ( $pending_action ) { + $data['action'] = $pending_action; + delete_transient( 'sophi_content_sync_pending_' . $post->ID ); + } + $tracker->trackUnstructEvent( [ 'schema' => 'iglu:com.sophi/content_update/jsonschema/2-0-0',