admin – Display notice in block editor after wp_insert_post_data hook

Question

I have a custom post type which checks and validates some (custom) meta fields added with it upon publishing. I am using wp_insert_post_data for the purpose:

public function __construct()
{
    $this->sconfig= ['post_type'=> 'event', 'slug'=>'events'];
    add_filter('wp_insert_post_data', array($this, 'validate_event_meta'), 99, 2);
    add_filter('post_updated_messages', array($this, 'event_save_update_msg'), 10, 1);
    //add_action('admin_notices', array($this, 'event_save_update_msg'));
}

function validate_event_meta($postData, $postArray)
{
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $postData;
    }
    if (array_key_exists('post_type', $postData) && $postData['post_type'] === $this->sconfig['post_type']) {
        if (array_key_exists('post_status', $postData) && $postData['post_status'] === 'publish') {
            $valstat = $this->get_meta_posted_vals();
            /*
            $valstat['stat'] is 0 or 1 depending on validation success
            $valstat['log'] has the error message
            */
            if ($valstat['stat'] == 0) {
                $postData['post_status'] = 'draft';
                set_transient(get_current_user_id() . '_event8273_add_notice', 'ERROR: ' . $valstat['log']);
                add_filter('redirect_post_location', array($this, 'alter_event_save_redirect'), 99);
            }
         }
    }
    return $postData;
}

function alter_event_save_redirect($location)
{
    remove_filter('redirect_post_location', __FUNCTION__, 99);
    $location = remove_query_arg('message', $location);
    $location = add_query_arg('message', 99, $location);
    return $location;
}

function event_save_update_msg($messages)
{
    $message = get_transient(get_current_user_id() . '_event8273_add_notice');
    if ($message) {
        delete_transient(get_current_user_id() . '_event8273_add_notice');
        //echo $message;
        //$messages['post'][99] = $message;
        $messages[$this->sconfig['post_type']][99] = $message;
    }
    return $messages;
}

Though the validation system is working correctly, I cannot display any notices on the error. Each time the code encounters invalid meta value during ‘publish’, it reverts the post into ‘draft’ status and the ‘Draft Saved Preview‘ message pops up.

Upon some research, I have found that the block editor uses javascript to display custom notices. But what I cannot understand is how to call the javascript function (file already enqueued in admin) after the validation from wp_insert_post_data.

function event_save_alert(errmsg)
{
    ( function ( wp ) {
        wp.data.dispatch( 'core/notices' ).createNotice(
            'error', // Can be one of: success, info, warning, error.
            errmsg, // Text string to display.
            {
                isDismissible: true, // Whether the user can dismiss the notice.
                // Any actions the user can perform.
                actions: [
                    {
                        url: '#',
                        label: 'View post',
                    },
                ],
            }
        );
    } )( window.wp );
}

Any kind of help is appreciated. Thanks for reading this far and giving it a thought.

0
sariDon 1 year 2023-02-17T02:12:00-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse