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

BUG: Calling wp_delete_attachment will break action_queue_meta_sync #2461

Closed
Shrimpstronaut opened this issue Nov 16, 2021 · 2 comments · Fixed by #2483
Closed

BUG: Calling wp_delete_attachment will break action_queue_meta_sync #2461

Shrimpstronaut opened this issue Nov 16, 2021 · 2 comments · Fixed by #2483
Assignees
Labels
bug Something isn't working high priority

Comments

@Shrimpstronaut
Copy link

Shrimpstronaut commented Nov 16, 2021

Describe the bug
When calling wp_delete_attachment the Elasticpress plugin will throw the following error: Trying to get property 'ID' of non-object in /app/web/app/plugins/elasticpress/includes/classes/Indexable/Post/Post.php on line 672.

Steps to Reproduce

  1. call wp_delete_attachment with an attachment-ID and force-delete true
  2. check logs
  3. see error

Expected behavior
The attachment should be deleted and the necessary changes should be synced to Elasticsearch.

Logs
Full stack trace of the exception:

NOTICE: PHP message: PHP  18. wp_delete_attachment($post_id = 135414, $force_delete = TRUE) /app/web/app/plugins/REDACTED.php:1172
NOTICE: PHP message: PHP  19. delete_metadata($meta_type = 'post', $object_id = NULL, $meta_key = '_thumbnail_id', $meta_value = 135414, $delete_all = TRUE) /app/web/wp/wp-includes/post.php:6165
NOTICE: PHP message: PHP  20. do_action($hook_name = 'deleted_post_meta', ...$arg = variadic([0 => '3258749'], 0, '_thumbnail_id', 135414)) /app/web/wp/wp-includes/meta.php:463
NOTICE: PHP message: PHP  21. WP_Hook->do_action($args = [0 => [0 => '3258749'], 1 => 0, 2 => '_thumbnail_id', 3 => 135414]) /app/web/wp/wp-includes/plugin.php:470
NOTICE: PHP message: PHP  22. WP_Hook->apply_filters($value = '', $args = [0 => [0 => '3258749'], 1 => 0, 2 => '_thumbnail_id', 3 => 135414]) /app/web/wp/wp-includes/class-wp-hook.php:327
NOTICE: PHP message: PHP  23. ElasticPress\Indexable\Post\SyncManager->action_queue_meta_sync($meta_id = [0 => '3258749'], $object_id = 0, $meta_key = '_thumbnail_id', $meta_value = 135414) /app/web/wp/wp-includes/class-wp-hook.php:303
NOTICE: PHP message: PHP  24. ElasticPress\Indexable\Post\Post->prepare_meta($post = NULL) /app/web/app/plugins/elasticpress/includes/classes/Indexable/Post/SyncManager.php:125

Additional context
When calling wp_delete_attachment the following code will be called and executed:

// Delete all for any posts.
delete_metadata( 'post', null, '_thumbnail_id', $post_id, true );

As we can see, the second argument passed into the delete_metadata function is null. This parameter references the object_id ("ID of the object metadata is for"). This is done to remove any references to the _thumbnail_id field for all existing posts. Removing the object_id (e.g. setting it to 0) however, will then cause the error above.

Workaround
To fix this issue, I have currently implemented the following filter:

add_filter('ep_skip_post_meta_sync', [$this, 'skipMetaSync'], 10, 5);

public function skipMetaSync($skip, $post, $meta_id, $meta_key, $meta_value): bool
{
    if (null === $post) {
        return true;
    }

    return $skip;
}
@Shrimpstronaut Shrimpstronaut added the bug Something isn't working label Nov 16, 2021
@felipeelia
Copy link
Member

felipeelia commented Nov 18, 2021

Hey @Shrimpstronaut, first of all, thanks for opening the issue!

Instead of simply skipping if $object_id is null, it would be even better if we could honor the $delete_all variable somehow, finding the object IDs that should be updated. The delete_metadata() function does that with this piece of code:

if ( $delete_all ) {
	if ( '' !== $meta_value && null !== $meta_value && false !== $meta_value ) {
		$object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $type_column FROM $table WHERE meta_key = %s AND meta_value = %s", $meta_key, $meta_value ) );
	} else {
		$object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $type_column FROM $table WHERE meta_key = %s", $meta_key ) );
	}
}

@oscarssanchez
Copy link
Contributor

closing per #2483

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working high priority
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants