| @@ -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 |
| @@ -31,9 +36,26 @@ | ||
| 31 | 36 | * @var int |
| 32 | 37 | */ |
| 33 | 38 | protected $found_records_count = 0; |
| 34 | 39 | |
| 40 | + | |
| 35 | 41 | /** |
| 42 | + * Hold logs sites options. | |
| 43 | + * | |
| 44 | + * @var int | |
| 45 | + */ | |
| 46 | + protected $logs_wp_options; | |
| 47 | + | |
| 48 | + | |
| 49 | + /** | |
| 50 | + * Hold logs extra data. | |
| 51 | + * | |
| 52 | + * @var int | |
| 53 | + */ | |
| 54 | + protected $logs_extra_data = array(); | |
| 55 | + | |
| 56 | + | |
| 57 | + /** | |
| 36 | 58 | * Constructor. |
| 37 | 59 | * |
| 38 | 60 | * Run each time the class is called. |
| 39 | 61 | * |
| @@ -64,8 +86,19 @@ | ||
| 64 | 86 | * @return array |
| 65 | 87 | */ |
| 66 | 88 | $record = apply_filters( 'mainwp_module_log_record_array', $record ); |
| 67 | 89 | |
| 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 ); | |
| 96 | + | |
| 97 | + if ( ! $enable_log_type ) { | |
| 98 | + return false; | |
| 99 | + } | |
| 100 | + | |
| 68 | 101 | $data = $this->sanitize_record( $record ); |
| 69 | 102 | |
| 70 | 103 | if ( empty( $data ) ) { |
| 71 | 104 | return false; |
| @@ -108,18 +141,20 @@ | ||
| 108 | 141 | return array(); |
| 109 | 142 | } |
| 110 | 143 | |
| 111 | 144 | $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(), | |
| 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(), | |
| 122 | 157 | ); |
| 123 | 158 | |
| 124 | 159 | // Records can have only these fields. |
| 125 | 160 | $record = array_intersect_key( $record, $record_defaults ); |
| @@ -166,8 +201,10 @@ | ||
| 166 | 201 | $args = apply_filters( 'mainwp_module_log_query_args', $args ); |
| 167 | 202 | |
| 168 | 203 | $result = (array) $this->driver->get_records( $args ); |
| 169 | 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(); | |
| 170 | 207 | |
| 171 | 208 | return empty( $result['items'] ) ? array() : $result['items']; |
| 172 | 209 | } |
| 173 | 210 | |
| @@ -191,8 +228,26 @@ | ||
| 191 | 228 | return $this->found_records_count; |
| 192 | 229 | } |
| 193 | 230 | |
| 194 | 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 | + } | |
| 239 | + | |
| 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 | + } | |
| 248 | + | |
| 249 | + /** | |
| 195 | 250 | * Public getter to return table names |
| 196 | 251 | * |
| 197 | 252 | * @return array |
| 198 | 253 | */ |
| @@ -200,10 +255,8 @@ | ||
| 200 | 255 | return $this->driver->get_table_names(); |
| 201 | 256 | } |
| 202 | 257 | |
| 203 | 258 | |
| 204 | - | |
| 205 | - | |
| 206 | 259 | /** |
| 207 | 260 | * Create compact logs and erase records from the database. |
| 208 | 261 | * |
| 209 | 262 | * @param int $start_time start time to compact. |
| @@ -230,9 +283,9 @@ | ||
| 230 | 283 | if ( empty( $conn ) || empty( $cont ) || empty( $act ) ) { |
| 231 | 284 | continue; |
| 232 | 285 | } |
| 233 | 286 | |
| 234 | - $year = (int) gmdate( 'Y', $item->created ); | |
| 287 | + $year = (int) gmdate( 'Y', intval( $item->created / 1000000 ) ); | |
| 235 | 288 | |
| 236 | 289 | if ( $year < 2020 ) { |
| 237 | 290 | return; |
| 238 | 291 | } |
| @@ -284,9 +337,9 @@ | ||
| 284 | 337 | global $wpdb; |
| 285 | 338 | |
| 286 | 339 | $where = $wpdb->prepare( ' AND `logs`.`created` >= %d AND `logs`.`created` <= %d', $start_time, $end_time ); |
| 287 | 340 | |
| 288 | - return $wpdb->query( //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery | |
| 341 | + return $wpdb->query( //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Destructive operation; caching DELETE is inappropriate. | |
| 289 | 342 | "DELETE `logs`, `meta` |
| 290 | 343 | FROM {$wpdb->mainwp_tbl_logs} AS `logs` |
| 291 | 344 | LEFT JOIN {$wpdb->mainwp_tbl_logs_meta} AS `meta` |
| 292 | 345 | ON `meta`.`meta_log_id` = `logs`.`log_id` |
| @@ -291,6 +344,18 @@ | ||
| 291 | 344 | LEFT JOIN {$wpdb->mainwp_tbl_logs_meta} AS `meta` |
| 292 | 345 | ON `meta`.`meta_log_id` = `logs`.`log_id` |
| 293 | 346 | WHERE `logs`.`connector` != 'compact' " . $where // phpcs:ignore -- escaped. |
| 294 | 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; | |
| 295 | 360 | } |
| 296 | 361 | } |