| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @package Logtivity |
| 5 |
* @contact logtivity.io, hello@logtivity.io |
| 6 |
* @copyright 2024-2026 Logtivity. All rights reserved |
| 7 |
* @license https://www.gnu.org/licenses/gpl.html GNU/GPL |
| 8 |
* |
| 9 |
* This file is part of Logtivity. |
| 10 |
* |
| 11 |
* Logtivity is free software: you can redistribute it and/or modify |
| 12 |
* it under the terms of the GNU General Public License as published by |
| 13 |
* the Free Software Foundation, either version 2 of the License, or |
| 14 |
* (at your option) any later version. |
| 15 |
* |
| 16 |
* Logtivity is distributed in the hope that it will be useful, |
| 17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 |
* GNU General Public License for more details. |
| 20 |
* |
| 21 |
* You should have received a copy of the GNU General Public License |
| 22 |
* along with Logtivity. If not, see <https://www.gnu.org/licenses/>. |
| 23 |
*/ |
| 24 |
|
| 25 |
// @phpcs:disable PSR1.Files.SideEffects.FoundWithSymbols |
| 26 |
// @phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace |
| 27 |
// @phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps |
| 28 |
|
| 29 |
class Logtivity_Log_Index_Controller |
| 30 |
{ |
| 31 |
public function __construct() |
| 32 |
{ |
| 33 |
add_action('wp_ajax_nopriv_logtivity_log_index_filter', [$this, 'search']); |
| 34 |
add_action('wp_ajax_logtivity_log_index_filter', [$this, 'search']); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* @return self |
| 39 |
*/ |
| 40 |
public static function init(): self |
| 41 |
{ |
| 42 |
return new static(); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* @return void |
| 47 |
*/ |
| 48 |
public function search(): void |
| 49 |
{ |
| 50 |
if (current_user_can(Logtivity::ACCESS_LOGS)) { |
| 51 |
$api = Logtivity::log(); |
| 52 |
|
| 53 |
if ($api->getOption()->getApiKey()) { |
| 54 |
$response = $api |
| 55 |
->get( |
| 56 |
'/logs', |
| 57 |
[ |
| 58 |
'page' => $this->getInput('page'), |
| 59 |
'action' => $this->getInput('search_action'), |
| 60 |
'context' => $this->getInput('search_context'), |
| 61 |
'action_user' => $this->getInput('action_user'), |
| 62 |
] |
| 63 |
); |
| 64 |
|
| 65 |
if ($response) { |
| 66 |
$response = json_decode(json_encode($response)); |
| 67 |
|
| 68 |
$this->renderView([ |
| 69 |
'message' => $response->error, |
| 70 |
'logs' => $response->body->data ?? [], |
| 71 |
'meta' => $response->body->meta ?? null, |
| 72 |
'hasNextPage' => $response->body->links->next ?? null, |
| 73 |
]); |
| 74 |
} else { |
| 75 |
$this->renderView($api->getConnectionMessage()); |
| 76 |
} |
| 77 |
|
| 78 |
} else { |
| 79 |
$this->welcomeResponse(); |
| 80 |
} |
| 81 |
} else { |
| 82 |
$this->renderView(__('You do not have sufficient permissions to access this page.')); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* @param string $field |
| 88 |
* |
| 89 |
* @return ?string |
| 90 |
*/ |
| 91 |
protected function getInput(string $field): ?string |
| 92 |
{ |
| 93 |
return sanitize_text_field($_GET[$field] ?? null); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* @param string|array $response |
| 98 |
* |
| 99 |
* @return void |
| 100 |
*/ |
| 101 |
protected function renderView($viewData): void |
| 102 |
{ |
| 103 |
if (is_string($viewData)) { |
| 104 |
$viewData = ['message' => $viewData]; |
| 105 |
} |
| 106 |
|
| 107 |
$viewData = array_merge( |
| 108 |
[ |
| 109 |
'message' => null, |
| 110 |
'logs' => [], |
| 111 |
'meta' => null, |
| 112 |
'hasNextPage' => null, |
| 113 |
], |
| 114 |
$viewData |
| 115 |
); |
| 116 |
|
| 117 |
wp_send_json([ |
| 118 |
'view' => logtivity_view('_logs-loop', $viewData), |
| 119 |
]); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* @return void |
| 124 |
*/ |
| 125 |
protected function welcomeResponse() |
| 126 |
{ |
| 127 |
wp_send_json([ |
| 128 |
'view' => logtivity_view('activation', [ |
| 129 |
'display' => true, |
| 130 |
'logo' => false, |
| 131 |
]), |
| 132 |
]); |
| 133 |
} |
| 134 |
} |
| 135 |
|