PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.2.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.2.1
6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 5.2.2 5.3 All 153 releases
mainwp / modules / logs / classes / class-log-db.php

class-log-db.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 5.2.1, at modules/logs/classes/class-log-db.php

297 lines 7.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP Database Logs.
4 *
5 * This file handles all interactions with the Client DB.
6 *
7 * @package MainWP/Dashboard
8 */
9
10 namespace MainWP\Dashboard\Module\Log;
11
12 use MainWP\Dashboard\MainWP_DB;
13
14 /**
15 * Class Log_DB
16 *
17 * @package MainWP\Dashboard
18 */
19 class Log_DB extends MainWP_DB {
20
21 /**
22 * Holds the driver instance
23 *
24 * @var Log_DB_Driver
25 */
26 public $driver;
27
28 /**
29 * Number of records in last request
30 *
31 * @var int
32 */
33 protected $found_records_count = 0;
34
35 /**
36 * Constructor.
37 *
38 * Run each time the class is called.
39 *
40 * @param array $driver db driver.
41 */
42 public function __construct( $driver ) {
43 parent::__construct();
44 $this->driver = $driver;
45 }
46
47 /**
48 * Insert a record
49 *
50 * @param array $record New record.
51 *
52 * @return int
53 */
54 public function insert( $record ) {
55 if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
56 return false;
57 }
58
59 /**
60 * Filter allows modification of record information
61 *
62 * @param array $record
63 *
64 * @return array
65 */
66 $record = apply_filters( 'mainwp_module_log_record_array', $record );
67
68 $data = $this->sanitize_record( $record );
69
70 if ( empty( $data ) ) {
71 return false;
72 }
73
74 $record_id = $this->driver->insert_record( $data );
75
76 if ( ! $record_id ) {
77 /**
78 * Fires on a record insertion error
79 *
80 * @param array $record
81 * @param mixed $result
82 */
83 do_action( 'mainwp_module_log_record_insert_error', $record, false );
84
85 return false;
86 }
87
88 /**
89 * Fires after a record has been inserted
90 *
91 * @param int $record_id
92 * @param array $record
93 */
94 do_action( 'mainwp_module_log_record_inserted', $record_id, $record );
95
96 return absint( $record_id );
97 }
98
99 /**
100 * Ensure the record matches our schema.
101 *
102 * @param array $record Record to store.
103 *
104 * @return array
105 */
106 protected function sanitize_record( $record ) {
107 if ( ! is_array( $record ) ) {
108 return array();
109 }
110
111 $record_defaults = array(
112 'site_id' => null,
113 'user_id' => null,
114 'created' => null,
115 'item' => null,
116 'connector' => null,
117 'context' => null,
118 'action' => null,
119 'state' => null,
120 'duration' => null,
121 'meta' => array(),
122 );
123
124 // Records can have only these fields.
125 $record = array_intersect_key( $record, $record_defaults );
126
127 // Sanitize all record values.
128 return array_map(
129 function ( $value ) {
130 if ( ! is_array( $value ) ) {
131 return wp_strip_all_tags( $value );
132 }
133
134 return $value;
135 },
136 $record
137 );
138 }
139
140 /**
141 * Get logs records
142 *
143 * @param array $args Arguments to filter result by.
144 *
145 * @return array Log Records
146 */
147 public function get_records( $args ) {
148 $defaults = array(
149 // Search param.
150 'search' => null,
151 'search_field' => 'item',
152 'records_per_page' => get_option( 'posts_per_page', 20 ),
153 'paged' => 1,
154 // Order.
155 'order' => 'desc',
156 'orderby' => 'date',
157 );
158
159 $args = wp_parse_args( $args, $defaults );
160
161 /**
162 * Filter allows additional arguments to query $args
163 *
164 * @return array Array of query arguments
165 */
166 $args = apply_filters( 'mainwp_module_log_query_args', $args );
167
168 $result = (array) $this->driver->get_records( $args );
169 $this->found_records_count = isset( $result['count'] ) ? $result['count'] : 0;
170
171 return empty( $result['items'] ) ? array() : $result['items'];
172 }
173
174 /**
175 * Helper function, backwards compatibility
176 *
177 * @param array $args Argument to filter result by.
178 *
179 * @return array Log Records
180 */
181 public function query( $args ) {
182 return $this->get_records( $args );
183 }
184
185 /**
186 * Return the number of records found in last request
187 *
188 * @return int
189 */
190 public function get_found_records_count() {
191 return $this->found_records_count;
192 }
193
194 /**
195 * Public getter to return table names
196 *
197 * @return array
198 */
199 public function get_table_names() {
200 return $this->driver->get_table_names();
201 }
202
203
204
205
206 /**
207 * Create compact logs and erase records from the database.
208 *
209 * @param int $start_time start time to compact.
210 * @param int $end_time end time to compact.
211 *
212 * @return mixed results.
213 */
214 public function create_compact_and_erase_records( $start_time, $end_time ) { //phpcs:ignore -- NOSONAR - complex.
215
216 global $wpdb;
217
218 $where_compact = $wpdb->prepare( ' AND `logs`.`created` >= %d AND `logs`.`created` <= %d ', $start_time, $end_time );
219 $sql = "SELECT `logs`.* FROM {$wpdb->mainwp_tbl_logs} logs WHERE `logs`.`connector` != 'compact' " . $where_compact;
220
221 $logs = MainWP_DB::instance()->query( $sql );
222 $stats_data = array();
223
224 $done = false;
225 while ( $logs && ( $item = MainWP_DB::fetch_object( $logs ) ) ) {
226 $conn = $item->connector;
227 $cont = $item->context;
228 $act = $item->action;
229
230 if ( empty( $conn ) || empty( $cont ) || empty( $act ) ) {
231 continue;
232 }
233
234 $year = (int) gmdate( 'Y', $item->created );
235
236 if ( $year < 2020 ) {
237 return;
238 }
239
240 if ( ! isset( $stats_data[ $year ] ) ) {
241 $stats_data[ $year ] = array();
242 }
243
244 if ( ! isset( $stats_data[ $year ][ $conn ] ) ) {
245 $stats_data[ $year ][ $conn ] = array();
246 }
247 if ( ! isset( $stats_data[ $year ][ $conn ][ $cont ] ) ) {
248 $stats_data[ $year ][ $conn ][ $cont ] = array();
249 }
250
251 if ( ! isset( $stats_data[ $year ][ $conn ][ $cont ][ $act ] ) ) {
252 $stats_data[ $year ][ $conn ][ $cont ][ $act ] = array(
253 'count' => 0,
254 'duration' => 0,
255 );
256 }
257 $stats_data[ $year ][ $conn ][ $cont ][ $act ]['count'] += 1;
258 $stats_data[ $year ][ $conn ][ $cont ][ $act ]['duration'] += $item->duration;
259 $done = true;
260 }
261
262 if ( $done ) {
263 foreach ( $stats_data as $year => $data ) {
264 $year_data = array(
265 'data' => $data,
266 );
267 do_action( 'mainwp_compact_action', 'saved', $year, $year_data, $start_time, $end_time );
268 }
269
270 $this->erase_log_records( $start_time, $end_time );
271 }
272 }
273
274
275 /**
276 * Clears logs records from the database.
277 *
278 * @param int $start_time start time.
279 * @param int $end_time start end.
280 *
281 * @return int results number.
282 */
283 private function erase_log_records( $start_time, $end_time ) {
284 global $wpdb;
285
286 $where = $wpdb->prepare( ' AND `logs`.`created` >= %d AND `logs`.`created` <= %d', $start_time, $end_time );
287
288 return $wpdb->query( //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
289 "DELETE `logs`, `meta`
290 FROM {$wpdb->mainwp_tbl_logs} AS `logs`
291 LEFT JOIN {$wpdb->mainwp_tbl_logs_meta} AS `meta`
292 ON `meta`.`meta_log_id` = `logs`.`log_id`
293 WHERE `logs`.`connector` != 'compact' " . $where // phpcs:ignore -- escaped.
294 );
295 }
296 }
297