Actualizarea postului WordPress DUPĂ actualizările câmpului ACF (Programare, WordPress, Tranziție, Câmpuri Personalizate Avansate, Status, Acfpro)

YorlinqNL a intrebat.
a intrebat.

Codul de mai jos trimite un e-mail de fiecare dată când (schimb câmpul STATUT și apoi) securizez/actualizez postarea. Dar nu funcționează așa cum vreau eu și știu ce este greșit:

Actualizările de câmpuri sunt întotdeauna salvate DUPĂ ce postul este salvat, astfel încât trimite întotdeauna valorile anterioare ale câmpului (este ca și cum aș fi mereu cu un pas în urmă).

Cum pot lăsa ca POST UPDATE să vină ULTIMA dată, astfel încât acțiunea declanșată ulterior (trimiterea de e-mail) să aibă incluse cele mai recente valori ale câmpului?

function yl_send_booking_email_after_post_update( $new_status, $old_status, $post ) {

    if ( $new_status == $old_status ) {

        if ( 'booking' === get_post_type() ) { // check the custom post type

            $send_email = get_post_meta( $post->ID, 'bookings_field_send_email', true );

            if ( $send_email === 'yes' ) {

                $status = get_post_meta( $post->ID, 'bookings_field_status', true );

                if ( $status === 'confirmed' ) {
                    $subject = get_post_meta( $post->ID, 'bookings_field_email_title_confirmed', true );            
                } else if ( $status === 'changed' ) {
                    $subject = get_post_meta( $post->ID, 'bookings_field_email_title_changed', true );
                } else if ( $status === 'canceled by guest' ) {
                    $subject = get_post_meta( $post->ID, 'bookings_field_email_title_canceled_by_guest', true );
                } else {
                    $subject = get_post_meta( $post->ID, 'bookings_field_email_title_canceled_by_us', true );
                }

                if ( $status === 'confirmed' ) {
                    $body = get_post_meta( $post->ID, 'bookings_field_email_content_confirmed', true );         
                } else if ( $status === 'changed' ) {
                    $body = get_post_meta( $post->ID, 'bookings_field_email_content_changed', true );
                } else if ( $status === 'canceled by guest' ) {
                    $body = get_post_meta( $post->ID, 'bookings_field_email_content_canceled_by_guest', true );
                } else {
                    $body = get_post_meta( $post->ID, 'bookings_field_email_content_canceled_by_us', true );
                }

                $to = get_post_meta( $post->ID, 'bookings_field_email', true );

                $headers = array
                    (
                    'From: ' . get_bloginfo('name') . ' <' . get_bloginfo('admin_email') . '>',
                    'Bcc: ' . get_bloginfo('admin_email'),
                    'X-Mailer: PHP/' . phpversion(),
                    'MIME-Version: 1.0',
                    'Content-type: text/html; charset=iso-8859-1'
                    );
                $headers = implode( "r
" , $headers );

                wp_mail( $to, $subject, $body, $headers );

            }

        }

    }

}

add_action( 'transition_post_status', 'yl_send_booking_email_after_post_update', 15, 3 );

UPDATE:După ce am primit câteva informații de la echipa ACF a trebuit să-mi schimb codul. A se vedea mai jos. Nu primesc NICI o eroare, dar NU se trimite niciun e-mail… Aveți vreo idee?

add_action( 'acf/save_post', 'yl_send_booking_email_after_post_update', 5 );
function yl_send_booking_email_after_post_update( $post_id ) {

    if ( 'booking' === get_post_type() ) { // check the custom post type

        // Get submitted values.
        $values = $_POST['acf'];

        // Check if a specific value was updated.
        if( isset($_POST['acf']['bookings_field_status']) ) {

            $send_email = $_POST['acf']['bookings_field_send_email'];

            if ( $send_email === 'yes' ) {

                $status = $_POST['acf']['bookings_field_status'];

                if ( $status === 'confirmed' ) {
                    $subject = $_POST['acf']['bookings_field_email_subject_confirmed'];
                } elseif ( $status === 'changed' ) {
                    $subject = $_POST['acf']['bookings_field_email_subject_changed'];
                } elseif ( $status === 'canceled by guest' ) {
                    $subject = $_POST['acf']['bookings_field_email_subject_canceled_by_guest'];
                } else {
                    $subject = $_POST['acf']['bookings_field_email_subject_canceled_by_us'];
                }

                if ( $status === 'confirmed' ) {
                    $subject = $_POST['acf']['bookings_field_email_body_confirmed'];
                } elseif ( $status === 'changed' ) {
                    $subject = $_POST['acf']['bookings_field_email_body_changed'];
                } elseif ( $status === 'canceled by guest' ) {
                    $subject = $_POST['acf']['bookings_field_email_body_canceled_by_guest'];
                } else {
                    $subject = $_POST['acf']['bookings_field_email_body_canceled_by_us'];
                }

                $to = $_POST['acf']['bookings_field_email'];

                $headers = array
                    (
                    'From: ' . get_bloginfo('name') . ' <' . get_bloginfo('admin_email') . '>',
                    'Bcc: ' . get_bloginfo('admin_email'),
                    'X-Mailer: PHP/' . phpversion(),
                    'MIME-Version: 1.0',
                    'Content-type: text/html; charset=iso-8859-1'
                );
                $headers = implode( "r
" , $headers );

                wp_mail( $to, $subject, $body, $headers );

            }

        }

    }

}

2 răspunsuri
YorlinqNL

UPDATE 2Am reușit să funcționeze. Codul de mai jos:

add_action('acf/save_post', 'yl_send_booking_email_after_status_update', 5);
function yl_send_booking_email_after_status_update( $post_id ) {

    // Get submitted values.
    $values = $_POST['acf'];

    // Check if a specific value was updated.
    if( isset($_POST['acf']['field_5ed8117407663']) ) {

        $to         = $_POST['acf']['field_5ed41758ed4b5'];
        $subject    = $_POST['acf']['field_5ed81174076f2'];
        $message    = $_POST['acf']['field_5ed8117407741'];

        $headers = array
            (
            'From: ' . get_bloginfo('name') . ' <' . get_bloginfo('admin_email') . '>',
            'Bcc: ' . get_bloginfo('admin_email'),
            'X-Mailer: PHP/' . phpversion(),
            'MIME-Version: 1.0',
            'Content-type: text/html; charset=iso-8859-1'
        );
        $headers = implode( "r
" , $headers );

        wp_mail( $to, $subject, $message, $headers );

    }
}

YorlinqNL

Codul complet de lucru (pentru cei cărora le place să folosească):

add_action('acf/save_post', 'yl_send_booking_email_after_status_update', 5);
function yl_send_booking_email_after_status_update( $post_id ) {

    // Get submitted values.
    $values = $_POST['acf'];

    // Check if a specific value was updated.
    if ( isset($_POST['acf']['field_5ed8117407663']) ) {

        $booking_status = $_POST['acf']['field_5ed8117407663'];
        $send_email     = $_POST['acf']['field_5ed81174076ac'];

        if ( $send_email == 'yes' ) {

            $to = $_POST['acf']['field_5ed41758ed4b5'];

            if ( $booking_status == 'confirmed' ) {
                $subject = $_POST['acf']['field_5ed81174076f2'];
                $message = $_POST['acf']['field_5ed8117407741'];
            } elseif ( $booking_status == 'changed' ) {
                $subject = $_POST['acf']['field_5ed8117407788'];
                $message = $_POST['acf']['field_5ed81174077df'];
            } elseif ( $booking_status == 'canceled by guest' ) {
                $subject = $_POST['acf']['field_5ed8117407829'];
                $message = $_POST['acf']['field_5ed8117407870'];
            } else {
                $subject = $_POST['acf']['field_5ed81174078b7'];
                $message = $_POST['acf']['field_5ed81174078fd'];
            }

            $headers = array
                (
                'From: ' . get_bloginfo('name') . ' <' . get_bloginfo('admin_email') . '>',
                'Bcc: ' . get_bloginfo('admin_email'),
                'X-Mailer: PHP/' . phpversion(),
                'MIME-Version: 1.0',
                'Content-type: text/html; charset=iso-8859-1'
            );
            $headers = implode( "r
" , $headers );

            wp_mail( $to, $subject, $message, $headers );

        }

    }
}