private function handle_ajax_request($ajax_request)
{
$result = call_user_func([$this, $ajax_request], $_REQUEST);
$request_type = !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest' ? 'ajax' : 'direct';
if ('direct' === $request_type) {
$callback = 'on_' . $ajax_request;
if (method_exists($this, $callback)) {
$this->{$callback}($result);
}
}
if (is_wp_error($result)) {
if ('ajax' === $request_type) {
wp_send_json_error($result->get_error_message());
}
$callback = "on_{$ajax_request}_error";
if (method_exists($this, $callback)) {
$this->{$callback}($result);
}
die;
}
if ('ajax' === $request_type) {
wp_send_json_success($result);
}
$callback = "on_{$ajax_request}_success";
if (method_exists($this, $callback)) {
$this->{$callback}($result);
}
die;
}