Claim Listing functionality – how to send email to users when their claim has been approved or denied

We have a claim listing plugin, it allows items(posts) to be claimed by users. It requires the user to “register” in order to claim that item therefore they have to enter a username and their email.

After they have submitted a claim, an email will be sent to the admin email(us) that a user is claiming for the item and gives us the option to approve or deny it.

When we approve or deny it, unfortunately no email is sent to the user informing them that the item has been approved or denied.

How could we get wordpress to send an email to the user informing them of our response?

Code: CodeShare (the code is too long and is breaking when I paste it here guys, sorry!)
As you can see, there is a wp_mail functionality in there but that’s for when an email is sent to the admin that a request of an item ownership has been requested)

This seems to be function with the issue(code below), what do we then need to add to send an email to the user to say that their request has been approved?

public static function claimListingActions(){
    if(isset($_GET['post_type']) && $_GET['post_type'] === 'ait-item'){

        if (isset($_GET['claim-action']) && !empty($_GET['post-id'])) {
            $postID = intval($_GET['post-id']);
            // admin can approve all ratings
            if (current_user_can('manage_options')) {
                switch($_GET['claim-action']){
                    case 'approve':
                        $redirect = admin_url('edit.php?post_type=ait-item&ait-notice=claim-approved');

                        $data = get_post_meta($postID, 'ait-claim-listing', true);
                        $data['status'] = 'approved';

                        update_post_meta($postID, 'ait-claim-listing', $data);

                        $user = get_user_by('email', $data['owner']);

                        // update also the _ait-item_item-author data field -> prevent errors
                        update_post_meta($postID, '_ait-item_item-author', array('author' => $user->ID));

                        wp_update_post( array('ID' => $postID, 'post_author' => $user->ID), true );
                    break;
                    case 'decline':
                        $redirect = admin_url('edit.php?post_type=ait-item&ait-notice=claim-declined');

                        $data = get_post_meta($postID, 'ait-claim-listing', true);
                        $data['status'] = 'unclaimed';
                        $data['owner'] = '-';
                        $data['date'] = '-';

                        update_post_meta($postID, 'ait-claim-listing', $data);

                        $user = new WP_User($data['author']);

                        // update also the _ait-item_item-author data field -> prevent errors
                        update_post_meta($postID, '_ait-item_item-author', array('author' => $user->ID));

                        wp_update_post( array('ID' => $postID, 'post_author' => $user->ID) );
                    break;
                }
                wp_safe_redirect( $redirect );
                exit();
            }
        }
    }
}

1 Answer
1

Unfortunately, the developers didn’t give you any hooks in their code to allow for customization without hacking it. But all is not lost – there are some hooks in WP that you could use to trigger your emails.

The ideal would be to trigger off the update_post_meta(). That function is simply a wrapper for update_metadata() which has lots of hooks in it.

Here’s an untested example of a direction you could go. Hopefully it gives you something to work off of.

/**
 * @param int    $meta_id    ID of updated metadata entry.
 * @param int    $object_id  Post ID.
 * @param string $meta_key   Meta key.
 * @param mixed  $meta_value Meta value. This will be a PHP-serialized string representation of the value if
 *                           the value is an array, an object, or itself a PHP-serialized string.
 */
add_action( 'updated_postmeta', function( $meta_id, $object_id, $meta_key, $meta_value ) {
    if ( 'ait-claim-listing' == $meta_key ) {
        if ( 'approved' == $meta_value['status'] ) {
            $email_to = $meta_value['owner'];
            $subject="Your approved email subject";
            $message="Your approved email message...";
        }

        if ( 'unclaimed' == $meta_value['status'] ) {
            $email_to = $meta_value['author'];
            $subject="Your unclaimed email subject";
            $message="Your unclaimed email message...";
        }

        $result = wp_mail( $email_to, $subject, $message );
    }
}, 10, 4 );

Keep in mind, this is untested and just a thought process based on the information in your question. You may need to make some adjustments to make it really work.

Leave a Comment