Fatal error when gathering postmeta when deleting a donation #6798
-
Hi there! I got redirected here from a support thread in wordpress.org We have a plugin that stores some metadata on posts when a snippet from us is used on their post content. We need to do some cleanup when those posts are deleted. As this snippet can be placed in any post type, we hook into the WP Steps to reproduce:
add_action( 'before_delete_post', function( $post_id ) {
// See https://developer.wordpress.org/reference/functions/get_post_meta/
// get_post_meta should be returning an ARRAY
// regardless of whether there is a meta entry for this post and this meta key
$meta = get_post_meta( $post_id, 'toolset-meta' );
if ( count( $meta ) > 0 ) {
// Clean our internal cache
}
});
A fatal error will be thrown: After some debugging, I narrowed this down to public function get_meta( $id = 0, $meta_key = '', $single = false ) {
if ( ! $this->is_filter_callback ) {
return get_metadata( $this->meta_type, $id, $meta_key, $single );
}
$id = $this->sanitize_id( $id );
// Bailout.
if ( ! $this->is_valid_post_type( $id ) ) {
return $this->check;
}
if ( $this->raw_result ) {
// Here, this method returns a string (hence the FALSE parameter to get_metadata, and a default value of an empty string) regardless of the $single value passed to the method.
if ( ! ( $value = get_metadata( $this->meta_type, $id, $meta_key, false ) ) ) {
$value = '';
}
// Reset flag.
$this->raw_result = false;
} else {
$value = get_metadata( $this->meta_type, $id, $meta_key, $single );
}
$this->is_filter_callback = false;
return $value;
} I would like to understand the logic behind this decision, to better understand if there is any good reason for us to go on and patch this on our side. In my opinion, we should not be providing a patch for our clients since we are legitimately using a WP API hook expecting it to return what the official documentation states it should return - in other words, I would kindly ask you to review this piece of logic and adjust it so it fits the documented behavior of the filter it is hooking into. Thanks in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
It looks like that line was a part of the original commit for that class back in August of, 2017. I don't see any additional commit history or documentation to explain why this was written to return a string in that instance. As for the error, this was written back in the That said, we should be respecting the return types of WP API hooks and I'll bring this to the team so that we can incorporate this into our work on |
Beta Was this translation helpful? Give feedback.
-
@decodekult in the meantime, since WordPress does not enforce return types I would suggest adding a is_countable check as defensive programming. if ( is_countable($meta) && count( $meta ) > 0 ) {
// Clean our internal cache
} |
Beta Was this translation helpful? Give feedback.
-
Hi @decodekult! As you can imagine, that code is rather deeply engrained in a rather old part of GiveWP, and the way it works — modifying post meta calls — makes it difficult to track down. I took a stab at cleaning it up to see if it would cause absolute chaos. The good news was that it didn't seem to rain to fire, but I did find at least one part of our forms that break. This tells me at least some parts of the GiveWP code rely on this (admittedly odd) behavior. The other good news is that this portion of our codebase is becoming increasingly legacy. Soon we'll be releasing GiveWP 3.0 which introduces an entirely new kind of forms that do not rely on this legacy code. We'll still be supporting the older (called v2) forms for a while, but moving everyone towards the new (v3) forms as quickly as possible. We then plan on cleaning up a lot of the legacy code in 4.0. So where does that put us now? Well, the reality is that this meta issue is probably not going to be resolved in the near future. The infrastructure doesn't allow us to safely change its behavior without risking the integrity of our customers' donation forms, which we take seriously. We may give it another shot later, but I don't want anyone to wait on that. In the meantime, I believe @kjohnson's proposed solution is the best path forward that shouldn't cause any adverse side-effects for your plugin. Since the appropriate behavior of the meta is to be countable, it's merely a minor redundancy. It's simply the fastest, and safest way (on both sides) to resolve the compatibility issues. Thank you so much for bringing this to our attention. We always want to wisely and responsibly make GiveWP better with the best interest of the echosystem and our customers in mind. |
Beta Was this translation helpful? Give feedback.
Hi @decodekult!
I just wanted to circle back and let you know we have a PR that was just approved and is now being sent to QA for testing: #7062
Hopefully that will go through smoothly and we'll get a release out in the next week or two with this in place!