PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.2.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.2.1
6.2.2 6.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 All 155 releases
← All changes | modules/logs/classes/class-log-db.php +296 -231 5.0.3 → 6.2.1 View file →
@@ -10,8 +10,13 @@
10 10 namespace MainWP\Dashboard\Module\Log;
11 11
12 12 use MainWP\Dashboard\MainWP_DB;
13 13
14 +// Exit if accessed directly.
15 +if ( ! defined( 'ABSPATH' ) ) {
16 + exit;
17 +}
18 +
14 19 /**
15 20 * Class Log_DB
16 21 *
17 22 * @package MainWP\Dashboard
@@ -17,280 +22,340 @@
17 22 * @package MainWP\Dashboard
18 23 */
19 24 class Log_DB extends MainWP_DB {
20 25
21 - /**
22 - * Holds the driver instance
23 - *
24 - * @var Log_DB_Driver
25 - */
26 - public $driver;
26 + /**
27 + * Holds the driver instance
28 + *
29 + * @var Log_DB_Driver
30 + */
31 + public $driver;
27 32
28 - /**
29 - * Number of records in last request
30 - *
31 - * @var int
32 - */
33 - protected $found_records_count = 0;
33 + /**
34 + * Number of records in last request
35 + *
36 + * @var int
37 + */
38 + protected $found_records_count = 0;
34 39
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 40
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 - }
41 + /**
42 + * Hold logs sites options.
43 + *
44 + * @var int
45 + */
46 + protected $logs_wp_options;
58 47
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 48
68 - $data = $this->sanitize_record( $record );
49 + /**
50 + * Hold logs extra data.
51 + *
52 + * @var int
53 + */
54 + protected $logs_extra_data = array();
69 55
70 - if ( empty( $data ) ) {
71 - return false;
72 - }
73 56
74 - $record_id = $this->driver->insert_record( $data );
57 + /**
58 + * Constructor.
59 + *
60 + * Run each time the class is called.
61 + *
62 + * @param array $driver db driver.
63 + */
64 + public function __construct( $driver ) {
65 + parent::__construct();
66 + $this->driver = $driver;
67 + }
75 68
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 );
69 + /**
70 + * Insert a record
71 + *
72 + * @param array $record New record.
73 + *
74 + * @return int
75 + */
76 + public function insert( $record ) {
77 + if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
78 + return false;
79 + }
84 80
85 - return false;
86 - }
81 + /**
82 + * Filter allows modification of record information
83 + *
84 + * @param array $record
85 + *
86 + * @return array
87 + */
88 + $record = apply_filters( 'mainwp_module_log_record_array', $record );
87 89
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 );
90 + /**
91 + * Hook mainwp_module_log_enable_insert_log_type.
92 + *
93 + * @since 5.5.
94 + */
95 + $enable_log_type = apply_filters( 'mainwp_module_log_enable_insert_log_type', true, $record );
95 96
96 - return absint( $record_id );
97 - }
97 + if ( ! $enable_log_type ) {
98 + return false;
99 + }
98 100
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 - }
101 + $data = $this->sanitize_record( $record );
110 102
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 - );
103 + if ( empty( $data ) ) {
104 + return false;
105 + }
123 106
124 - // Records can have only these fields.
125 - $record = array_intersect_key( $record, $record_defaults );
107 + $record_id = $this->driver->insert_record( $data );
126 108
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 - }
109 + if ( ! $record_id ) {
110 + /**
111 + * Fires on a record insertion error
112 + *
113 + * @param array $record
114 + * @param mixed $result
115 + */
116 + do_action( 'mainwp_module_log_record_insert_error', $record, false );
133 117
134 - return $value;
135 - },
136 - $record
137 - );
138 - }
118 + return false;
119 + }
139 120
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 - );
121 + /**
122 + * Fires after a record has been inserted
123 + *
124 + * @param int $record_id
125 + * @param array $record
126 + */
127 + do_action( 'mainwp_module_log_record_inserted', $record_id, $record );
158 128
159 - $args = wp_parse_args( $args, $defaults );
129 + return absint( $record_id );
130 + }
160 131
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 );
132 + /**
133 + * Ensure the record matches our schema.
134 + *
135 + * @param array $record Record to store.
136 + *
137 + * @return array
138 + */
139 + protected function sanitize_record( $record ) {
140 + if ( ! is_array( $record ) ) {
141 + return array();
142 + }
167 143
168 - $result = (array) $this->driver->get_records( $args );
169 - $this->found_records_count = isset( $result['count'] ) ? $result['count'] : 0;
144 + $record_defaults = array(
145 + 'site_id' => null,
146 + 'user_id' => null,
147 + 'user_login' => null,
148 + 'created' => null,
149 + 'log_type_id' => null,
150 + 'item' => null,
151 + 'connector' => null,
152 + 'context' => null,
153 + 'action' => null,
154 + 'state' => null,
155 + 'duration' => null,
156 + 'meta' => array(),
157 + );
170 158
171 - return empty( $result['items'] ) ? array() : $result['items'];
172 - }
159 + // Records can have only these fields.
160 + $record = array_intersect_key( $record, $record_defaults );
173 161
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 - }
162 + // Sanitize all record values.
163 + return array_map(
164 + function ( $value ) {
165 + if ( ! is_array( $value ) ) {
166 + return wp_strip_all_tags( $value );
167 + }
184 168
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 - }
169 + return $value;
170 + },
171 + $record
172 + );
173 + }
193 174
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 - }
175 + /**
176 + * Get logs records
177 + *
178 + * @param array $args Arguments to filter result by.
179 + *
180 + * @return array Log Records
181 + */
182 + public function get_records( $args ) {
183 + $defaults = array(
184 + // Search param.
185 + 'search' => null,
186 + 'search_field' => 'item',
187 + 'records_per_page' => get_option( 'posts_per_page', 20 ),
188 + 'paged' => 1,
189 + // Order.
190 + 'order' => 'desc',
191 + 'orderby' => 'date',
192 + );
202 193
194 + $args = wp_parse_args( $args, $defaults );
203 195
196 + /**
197 + * Filter allows additional arguments to query $args
198 + *
199 + * @return array Array of query arguments
200 + */
201 + $args = apply_filters( 'mainwp_module_log_query_args', $args );
204 202
203 + $result = (array) $this->driver->get_records( $args );
204 + $this->found_records_count = isset( $result['count'] ) ? $result['count'] : 0;
205 + $this->logs_wp_options = isset( $result['sites_opts'] ) && is_array( $result['sites_opts'] ) ? $result['sites_opts'] : array();
206 + $this->logs_extra_data = isset( $result['extra_data'] ) && is_array( $result['extra_data'] ) ? $result['extra_data'] : array();
205 207
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 ) {
208 + return empty( $result['items'] ) ? array() : $result['items'];
209 + }
215 210
216 - global $wpdb;
211 + /**
212 + * Helper function, backwards compatibility
213 + *
214 + * @param array $args Argument to filter result by.
215 + *
216 + * @return array Log Records
217 + */
218 + public function query( $args ) {
219 + return $this->get_records( $args );
220 + }
217 221
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;
222 + /**
223 + * Return the number of records found in last request
224 + *
225 + * @return int
226 + */
227 + public function get_found_records_count() {
228 + return $this->found_records_count;
229 + }
220 230
221 - $logs = MainWP_DB::instance()->query( $sql );
222 - $stats_data = array();
231 + /**
232 + * Return the number of records found in last request
233 + *
234 + * @return int
235 + */
236 + public function get_logs_sites_opts() {
237 + return $this->logs_wp_options;
238 + }
223 239
224 - $done = false;
225 - while ( $logs && ( $item = MainWP_DB::fetch_object( $logs ) ) ) {
226 - $conn = $item->connector;
227 - $cont = $item->context;
228 - $act = $item->action;
240 + /**
241 + * Return the number of records found in last request
242 + *
243 + * @return int
244 + */
245 + public function get_logs_extra_data() {
246 + return $this->logs_extra_data;
247 + }
229 248
230 - if ( empty( $conn ) || empty( $cont ) || empty( $act ) ) {
231 - continue;
232 - }
249 + /**
250 + * Public getter to return table names
251 + *
252 + * @return array
253 + */
254 + public function get_table_names() {
255 + return $this->driver->get_table_names();
256 + }
233 257
234 - $year = (int) gmdate( 'Y', $item->created );
235 258
236 - if ( $year < 2020 ) {
237 - return;
238 - }
259 + /**
260 + * Create compact logs and erase records from the database.
261 + *
262 + * @param int $start_time start time to compact.
263 + * @param int $end_time end time to compact.
264 + *
265 + * @return mixed results.
266 + */
267 + public function create_compact_and_erase_records( $start_time, $end_time ) { //phpcs:ignore -- NOSONAR - complex.
239 268
240 - if ( ! isset( $stats_data[ $year ] ) ) {
241 - $stats_data[ $year ] = array();
242 - }
269 + global $wpdb;
243 270
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 - }
271 + $where_compact = $wpdb->prepare( ' AND `logs`.`created` >= %d AND `logs`.`created` <= %d ', $start_time, $end_time );
272 + $sql = "SELECT `logs`.* FROM {$wpdb->mainwp_tbl_logs} logs WHERE `logs`.`connector` != 'compact' " . $where_compact;
250 273
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 - }
274 + $logs = MainWP_DB::instance()->query( $sql );
275 + $stats_data = array();
261 276
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 - }
277 + $done = false;
278 + while ( $logs && ( $item = MainWP_DB::fetch_object( $logs ) ) ) {
279 + $conn = $item->connector;
280 + $cont = $item->context;
281 + $act = $item->action;
269 282
270 - $this->erase_log_records( $start_time, $end_time );
271 - }
272 - }
283 + if ( empty( $conn ) || empty( $cont ) || empty( $act ) ) {
284 + continue;
285 + }
273 286
287 + $year = (int) gmdate( 'Y', intval( $item->created / 1000000 ) );
274 288
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;
289 + if ( $year < 2020 ) {
290 + return;
291 + }
285 292
286 - $where = $wpdb->prepare( ' AND `logs`.`created` >= %d AND `logs`.`created` <= %d', $start_time, $end_time );
293 + if ( ! isset( $stats_data[ $year ] ) ) {
294 + $stats_data[ $year ] = array();
295 + }
287 296
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 - }
297 + if ( ! isset( $stats_data[ $year ][ $conn ] ) ) {
298 + $stats_data[ $year ][ $conn ] = array();
299 + }
300 + if ( ! isset( $stats_data[ $year ][ $conn ][ $cont ] ) ) {
301 + $stats_data[ $year ][ $conn ][ $cont ] = array();
302 + }
303 +
304 + if ( ! isset( $stats_data[ $year ][ $conn ][ $cont ][ $act ] ) ) {
305 + $stats_data[ $year ][ $conn ][ $cont ][ $act ] = array(
306 + 'count' => 0,
307 + 'duration' => 0,
308 + );
309 + }
310 + $stats_data[ $year ][ $conn ][ $cont ][ $act ]['count'] += 1;
311 + $stats_data[ $year ][ $conn ][ $cont ][ $act ]['duration'] += $item->duration;
312 + $done = true;
313 + }
314 +
315 + if ( $done ) {
316 + foreach ( $stats_data as $year => $data ) {
317 + $year_data = array(
318 + 'data' => $data,
319 + );
320 + do_action( 'mainwp_compact_action', 'saved', $year, $year_data, $start_time, $end_time );
321 + }
322 +
323 + $this->erase_log_records( $start_time, $end_time );
324 + }
325 + }
326 +
327 +
328 + /**
329 + * Clears logs records from the database.
330 + *
331 + * @param int $start_time start time.
332 + * @param int $end_time start end.
333 + *
334 + * @return int results number.
335 + */
336 + private function erase_log_records( $start_time, $end_time ) {
337 + global $wpdb;
338 +
339 + $where = $wpdb->prepare( ' AND `logs`.`created` >= %d AND `logs`.`created` <= %d', $start_time, $end_time );
340 +
341 + return $wpdb->query( //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Destructive operation; caching DELETE is inappropriate.
342 + "DELETE `logs`, `meta`
343 + FROM {$wpdb->mainwp_tbl_logs} AS `logs`
344 + LEFT JOIN {$wpdb->mainwp_tbl_logs_meta} AS `meta`
345 + ON `meta`.`meta_log_id` = `logs`.`log_id`
346 + WHERE `logs`.`connector` != 'compact' " . $where // phpcs:ignore -- escaped.
347 + );
348 + }
349 +
350 + /**
351 + * Metho check if site action log existed.
352 + *
353 + * @param int $site_id Site Id.
354 + * @param string $object_id Object Id.
355 + *
356 + * @return mixed Results.
357 + */
358 + public function is_site_action_log_existed( $site_id = false, $object_id = false ) { //phpcs:ignore -- NOSONAR compatible.
359 + return false;
360 + }
296 361 }