Confirm Action

A common pattern is to ask the user for confirmation before running a destructive action. ActionTypeCallback ships with a default confirm_callback() that wraps the standard jQuery-Confirm dialog, so most callbacks only need to set the dialog texts on the parent class and return a CALLBACK_TYPE_SCRIPT payload from execute_callback().

Custom Confirmation Dialog

namespace TS\YourPlugin\Modules\Maintenance\Actions;
 
use TS\WonderfulRelations\Includes\Constants;
use TS\WonderfulRelations\System\Action\ActionType\ActionType;
use TS\WonderfulRelations\System\Action\ActionType\ActionTypeCallback;
 
class ConfirmRebuild extends ActionTypeCallback {
 
    public function __construct() {
        parent::__construct();
        new ActionType( "your_plugin_confirm_rebuild", $this );
    }
 
    public function execute_callback(): array {
        $payload           = $_POST['data'];
        $payload['action'] = "your_plugin_rebuild_now";
 
        $script = "
            let config = new ConfirmObject(
                '" . __( 'Attention!', 'wonderful-relations' ) . "',
                'red',
                '" . __( 'Delete all entries and rebuild?', 'wonderful-relations' ) . "'
            );
            config.addButton('cancel', '" . __( 'Abort', 'wonderful-relations' ) . "', function () {});
            config.addAjaxButton('confirm', '" . __( 'Yes', 'wonderful-relations' ) . "', " . wp_json_encode( $payload ) . ");
            jQuery.confirm( config.getJQueryConfirmConfig() );
        ";
 
        return array( Constants::CALLBACK_TYPE_SCRIPT, $script );
    }
}
 
new ConfirmRebuild();

The trick is to swap the action field on the payload so the user’s “Yes” click triggers the real action (your_plugin_rebuild_now), while the original click only triggers the confirmation step.