I’m currently facing an issue when trying to send an email to the listing owner when a published listing is deleted (moved to trash). While I think I’m close to achieving my goal, I’m encountering a few problems that I can’t resolve on my own. I hope someone can help!
What I Want to Achieve
When an admin deletes a listing (moving it from “publish” to “trash”), an email should be sent to the listing owner informing them of the deletion. This is intended to notify the user about their listing status.
The Problem
- Status Transitions Are Inconsistent or Reversed:
When tracking the old and new status of a listing, I noticed that the transitions are inverted. For example:
- Moving from “draft” to “publish” appears in logs as “publish” to “draft”.
- Similarly, moving from “publish” to “trash” is logged as “trash” to “publish”.
- This makes it a bit tricky to identify the correct transition, but I’ve accounted for this inversion in my logic. Below are some example logs:Logs for Listing ID 1416:
[28-Nov-2024 14:42:50 UTC] Debug: Listing ID: 1416, Old Status: auto-draft, New Status: new
[28-Nov-2024 14:43:12 UTC] Debug: Listing ID: 1416, Old Status: pending, New Status: auto-draft
[28-Nov-2024 14:43:23 UTC] Debug: Listing ID: 1416, Old Status: publish, New Status: pending
[28-Nov-2024 14:43:30 UTC] Debug: Listing ID: 1416, Old Status: trash, New Status: publish
As you can see:
- “publish” to “pending” might actually represent moderation approval.
- “trash” to “publish” is where I intend to send the email (as it seems reversed).
- Cannot Retrieve the Listing Owner’s Email:
When I attempt to fetch the listing owner’s email during the “trash” → “publish” transition (to send the email), I receive this error:
Debug: No user found for Listing ID: 1416.
I’ve tried fetching the owner’s email using $listing->get_owner_id()
and get_userdata()
, but something seems to be going wrong. I suspect the issue might be in how the owner is linked to the listing.
The Code I’m Using
Here’s the code I’ve implemented so far:
add_action('hivepress/v1/models/listing/update_status', function($listing_id, $old_status, $new_status) {
// Log transitions for debugging
error_log("Debug: Listing ID: $listing_id, Old Status: $old_status, New Status: $new_status");
// Check if the listing transitions from "trash" to "publish" (reversed logic)
if ($old_status === 'trash' && $new_status === 'publish') {
// Get the listing object
$listing = new HivePress\Models\Listing($listing_id);
// If the listing is found
if ($listing) {
// Retrieve the owner's email
$user = get_userdata($listing->get_owner_id());
if ($user) {
$user_email = $user->user_email;
// Prepare the email
$subject = "Your listing is now live";
$message = "Hello,\n\nYour listing titled '{$listing->get_title()}' is now live.\n\nThank you for your understanding.\n\nThe Lig'Annonces Team.";
$headers = ['Content-Type: text/plain; charset=UTF-8'];
// Send the email via WP Mail SMTP
wp_mail($user_email, $subject, $message, $headers);
error_log("Debug: Email sent to $user_email.");
} else {
error_log("Debug: No user found for Listing ID: $listing_id.");
}
}
}
}, 10, 3);
What I Need Help With
- Is there a better or more reliable way to retrieve the listing owner’s email?
$listing->get_owner_id()
seems to fail in my case. Am I using it incorrectly?
- Why are the status transitions reversed in HivePress?
- Is this expected behavior, or might there be something wrong with my setup?
- Is there anything I’ve missed in my logic or code that could improve the reliability of this feature?
Any advice or help would be greatly appreciated. Thanks in advance!