Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sending publish event with Yoast SEO activated #107

Merged
merged 1 commit into from
Jul 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion includes/functions/content-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';

Expand All @@ -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 ) {
Expand All @@ -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 );
} );
Expand All @@ -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',
Expand Down