0

I have "inherited" an event-booking WP plugin and for some reason (apparently, after moving the website into another location of the same server) this is now the situation:

As expected, new reservations are added to the database with the relevant information and can be seen in the custom post type (Bookings) admin page, but...

  • The booking confirmation e-mail only reach registered users (admins) when they make a booking as a test, but not unregistered ones (common visitors) for actual bookings.

  • The notification mail (for "real" bookings by users) to the website owner have a few empty strings instead of the custom field values they should contain.

At the beginning, I though the problem was in wp_mail() usage, so I tried tinkering with it (like, using PHP mail() instead of mailgun SMTP). But looking at this method:

   public static function send_request($booking_id)
{
    $booking_info = self::get_booking_info($booking_id);
    PC::Debug($booking_info, "booking_info in send_request");
    /* PC::Debug() shows an almost empty array, except for 'event' and 'name' */

    $subject = self::get_title_info($booking_id) . ' - ' . self::$request['subject'][$booking_info['language']];
    $message = self::$request['client'][$booking_info['language']] . self::$footer;
    // Not working = Empty 'To' field
    wp_mail($booking_info['mail'], $subject, $message, self::$headers);

    $subject = self::get_title_info($booking_id) . ' - ' . self::$request['subject']['it'];
    $message = self::$request['admin'] . self::get_info_for_admin($booking_id);
    // Working
    wp_mail(self::admin_mails(), $subject, $message, self::$headers);

}

Made me realize the only difference seems in the first parameter of wp_mail(), and I have verified (thanks to WP Mail Log plugin) and its raw or JSON format that the "Receiver:" field for these mails is actually empty. This field in "common users" version is derived from:

private static function get_booking_info($booking_id)
{
$post = get_post( $booking_id );
PC::Debug($post);
// Debug is fine

return array(
'name' => $post->post_title,
'event' => MP_Booking_Register::get_event_name($post->booking_show),
'mail' => $post->booking_email,
'language' => $post->booking_language,
'seats' => $post->booking_seats,
'date' => $post->booking_date,
);
}

Now, those custom fields are generated by this function:

private static function insert_post($booking_info) {
        $booking = array(
            'post_title'    => $booking_info['name'],
            'post_type'     => 'booking',
            'post_status'   => 'publish',
            'post_date'     => date('Y-m-d H:i:s', strtotime('now')),
            'post_author'   => 1,
        );
        PC::debug($booking_info,"booking_info in insert_post()");
        // Debug OK

        $booking_id = wp_insert_post($booking);
        PC::debug($booking_id,"booking_id (NEW) in insert_post()");
        // Debug OK

        add_post_meta($booking_id, 'booking_first_name', $booking_info['first_name'], true);
        add_post_meta($booking_id, 'booking_last_name', $booking_info['last_name'], true);
        add_post_meta($booking_id, 'booking_email', $booking_info['mail'], true);
        add_post_meta($booking_id, 'booking_language',$booking_info['language'], true);
        add_post_meta($booking_id, 'booking_seats', $booking_info['seats'], true);
        add_post_meta($booking_id, 'booking_show', $booking_info['event'], true);
        add_post_meta($booking_id, 'booking_date', $booking_info['date'], true);
        add_post_meta($booking_id, 'booking_state', 'Richiesto', true);

        $postCustom = get_post_custom($booking_id);
        PC::debug($postCustom, "Custom Fields");

        $allMeta = get_post_meta($booking_id);
        PC::debug($allMeta,"All Post Meta");

        return $booking_id;
    }

Using PC::Debug() I can correctly see $booking_id and $booking_info, but $postCustom and $postMeta are empty arrays. So... Does add_post_meta() fail? If so, why?

Relevant classes:

https://gist.github.com/CarloMartini/cc4083b4cf3634c74a910a3ae80f44b6

https://gist.github.com/CarloMartini/61ff9d0fc961b5e94bc7a938c6b5fc4d

  • Can you provide a print_r($booking_info); thinking maybe the $booking_id is empty – Drupalizeme Nov 12 '17 at 17:38
  • I updated the answer, with some PC::Debug() for PHP Console data. – CharlesM Nov 13 '17 at 14:03
  • You can verify by get_post_meta($booking_id); This will show all the meta fields and values that the post_id=$booking_id – Drupalizeme Nov 13 '17 at 14:11
  • Yes, I have tried that (you can see it at the end of the last code sample), and the array is empty. Any ideas why? Thank you! – CharlesM Nov 13 '17 at 14:16
  • Is there an entry in the wp_posts with the ID of $booking_id with custom post type (Bookings)? Also this $booking_id = wp_insert_post($booking); should be check for returning a valid ID and not WP_ERROR – Drupalizeme Nov 13 '17 at 14:19
0

Solved!

The problem was Database Caching (for casual users) in the W3 Total Cache.

Since most of the meta-data were added "on the fly" to the custom post type structure, they were not immediately accessible to wp_mail() or other methods.

I have now disable DB Caching and the plugin is working fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.