PluginProbe
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity / 3.3.10
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity v3.3.10
3.3.10 3.3.9 3.3.8 trunk 1.0 1.1.0 1.10.0 1.11.0 1.11.1 1.12.0 1.13.0 1.14.0 1.15.0 1.16.0 1.17.0 1.17.1 1.18.0 1.19.0 1.2.0 1.20.0 1.20.1 1.3.0 1.3.1 1.4.0 1.5.0 All 68 releases
logtivity / Core / Admin / Logtivity_Log_Index_Controller.php

Logtivity_Log_Index_Controller.php in Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity 3.3.10, at Core/Admin/Logtivity_Log_Index_Controller.php

138 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @package Logtivity
5 * @contact logtivity.io, [email protected]
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_logtivity_log_index_filter', [$this, 'search']);
34 }
35
36 /**
37 * @return self
38 */
39 public static function init(): self
40 {
41 return new static();
42 }
43
44 /**
45 * @return void
46 */
47 public function search(): void
48 {
49 check_ajax_referer('logtivity_log_index_filter', 'logtivity_log_index_nonce');
50
51 if (current_user_can(Logtivity::ACCESS_LOGS)) {
52 $api = Logtivity::log();
53
54 if ($api->getOption()->getApiKey()) {
55 $response = $api
56 ->get(
57 '/logs',
58 [
59 'page' => $this->getInput('page'),
60 'action' => $this->getInput('search_action'),
61 'context' => $this->getInput('search_context'),
62 'action_user' => $this->getInput('action_user'),
63 ]
64 );
65
66 if ($response) {
67 $response = json_decode(json_encode($response));
68
69 $this->renderView([
70 'message' => $response->error,
71 'logs' => $response->body->data ?? [],
72 'meta' => $response->body->meta ?? null,
73 'hasNextPage' => $response->body->links->next ?? null,
74 ]);
75 } else {
76 $this->renderView($api->getConnectionMessage());
77 }
78
79 } else {
80 $this->welcomeResponse();
81 }
82 } else {
83 $this->renderView(__('You do not have sufficient permissions to access this page.'));
84 }
85 }
86
87 /**
88 * @param string $field
89 *
90 * @return ?string
91 */
92 protected function getInput(string $field): ?string
93 {
94 return isset($_GET[$field])
95 ? sanitize_text_field(wp_unslash($_GET[$field]))
96 : null;
97 }
98
99 /**
100 * @param string|array $response
101 *
102 * @return void
103 */
104 protected function renderView($viewData): void
105 {
106 if (is_string($viewData)) {
107 $viewData = ['message' => $viewData];
108 }
109
110 $viewData = array_merge(
111 [
112 'message' => null,
113 'logs' => [],
114 'meta' => null,
115 'hasNextPage' => null,
116 ],
117 $viewData
118 );
119
120 wp_send_json([
121 'view' => logtivity_view('_logs-loop', $viewData),
122 ]);
123 }
124
125 /**
126 * @return void
127 */
128 protected function welcomeResponse()
129 {
130 wp_send_json([
131 'view' => logtivity_view('activation', [
132 'display' => true,
133 'logo' => false,
134 ]),
135 ]);
136 }
137 }
138