WordPress

admin — Wyświetl powiadomienie w edytorze bloków po haku wp_insert_post_data

  • 17 lutego, 2023
  • 2 min read
admin — Wyświetl powiadomienie w edytorze bloków po haku wp_insert_post_data


Mam niestandardowy typ postu, który sprawdza i weryfikuje niektóre (niestandardowe) pola meta dodane wraz z nim po opublikowaniu. ja używam wp_insert_post_data w celu:

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;
}

Chociaż system sprawdzania poprawności działa poprawnie, nie mogę wyświetlić żadnych powiadomień o błędzie. Za każdym razem, gdy kod napotka nieprawidłową wartość meta podczas „publikowania”, przywraca status posta do „szkicowej” i „Zapisano wersję roboczą” Zapowiedźwyskakuje komunikat.

Warto przeczytać!  tworzenie wtyczek — czy WP REST API buforuje wewnętrznie wykonywane żądania?

Po kilku badaniach odkryłem, że edytor bloków używa javascript do wyświetlania niestandardowych powiadomień. Ale nie mogę zrozumieć, jak wywołać funkcję javascript (plik już umieszczony w kolejce w admin) po sprawdzeniu poprawności z 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 );
}

Każdy rodzaj pomocy jest mile widziany. Dziękuję za przeczytanie tego miejsca i zastanowienie się.


Źródło