| 1 |
<?php |
| 2 |
|
| 3 |
class Logtivity_Log_Index_Controller |
| 4 |
{ |
| 5 |
public function __construct() |
| 6 |
{ |
| 7 |
add_action("wp_ajax_nopriv_logtivity_log_index_filter", [$this, 'search']); |
| 8 |
add_action("wp_ajax_logtivity_log_index_filter", [$this, 'search']); |
| 9 |
} |
| 10 |
|
| 11 |
public function search() |
| 12 |
{ |
| 13 |
if ( !current_user_can( 'manage_options' ) ) { |
| 14 |
wp_die( __( 'You do not have sufficient permissions to access this page.' ) ); |
| 15 |
} |
| 16 |
|
| 17 |
$response = json_decode( |
| 18 |
(new Logtivity_Api)->get('/logs', [ |
| 19 |
'page' => $this->getInput('page'), |
| 20 |
'action' => $this->getInput('search_action'), |
| 21 |
'context' => $this->getInput('search_context'), |
| 22 |
'action_user' => $this->getInput('action_user'), |
| 23 |
]) |
| 24 |
); |
| 25 |
|
| 26 |
if (property_exists($response, 'message') && $response->message) { |
| 27 |
return $this->errorReponse($response->message); |
| 28 |
} |
| 29 |
|
| 30 |
return $this->successResponse($response); |
| 31 |
} |
| 32 |
|
| 33 |
private function successResponse($response) |
| 34 |
{ |
| 35 |
return wp_send_json([ |
| 36 |
'view' => logtivity_view('_logs-loop', [ |
| 37 |
'logs' => $response->data, |
| 38 |
'meta' => $response->meta, |
| 39 |
'hasNextPage' => $response->links->next, |
| 40 |
]) |
| 41 |
]); |
| 42 |
} |
| 43 |
|
| 44 |
private function errorReponse($message) |
| 45 |
{ |
| 46 |
return wp_send_json([ |
| 47 |
'view' => logtivity_view('_logs-loop', [ |
| 48 |
'message' => $message, |
| 49 |
'logs' => [], |
| 50 |
]) |
| 51 |
]); |
| 52 |
} |
| 53 |
|
| 54 |
private function getInput($field) |
| 55 |
{ |
| 56 |
return ( isset($_GET[$field]) && is_string($_GET[$field]) ? $_GET[$field] : null); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
new Logtivity_Log_Index_Controller; |