PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.0.2
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.0.2
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-admin.php

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

350 lines 10.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Centralized manager for WordPress backend functionality.
4 *
5 * @package MainWP\Dashboard
6 * @version 4.5.1
7 */
8
9 namespace MainWP\Dashboard\Module\Log;
10
11 use MainWP\Dashboard\MainWP_Logger;
12 use MainWP\Dashboard\MainWP_Post_Handler;
13
14
15 defined( 'ABSPATH' ) || exit;
16
17 use DateTime;
18 use DateTimeZone;
19 use DateInterval;
20 use WP_CLI;
21
22 /**
23 * Class - Log_Admin
24 */
25 class Log_Admin {
26
27 /**
28 * Holds Instance of manager object
29 *
30 * @var Log_manager
31 */
32 public $manager;
33
34
35 /**
36 * Menu page screen id
37 *
38 * @var string
39 */
40 public $screen_id = array();
41
42 /**
43 * List table object
44 *
45 * @var List_Table
46 */
47 public $list_table = null;
48
49 /**
50 * Parent page of the records and settings pages
51 *
52 * @var string
53 */
54 public $admin_parent_page = 'admin.php';
55
56 /**
57 * Class constructor.
58 *
59 * @param Log_Manager $manager Instance of manager object.
60 */
61 public function __construct( $manager ) {
62 $this->manager = $manager;
63 add_filter( 'mainwp_getsubpages_settings', array( $this, 'add_subpage_menu_logs' ) );
64 // Load admin scripts and styles.
65 add_action(
66 'admin_enqueue_scripts',
67 array(
68 $this,
69 'admin_enqueue_scripts',
70 ),
71 9
72 );
73
74 // Auto purge setup.
75 add_action( 'wp_loaded', array( $this, 'hook_purge_scheduled_action' ) );
76 add_action( 'admin_init', array( $this, 'admin_init' ) );
77 }
78
79 /**
80 * Handle admin_init action.
81 */
82 public function admin_init() {
83 MainWP_Post_Handler::instance()->add_action( 'mainwp_module_log_display_rows', array( $this, 'ajax_display_rows' ) );
84 MainWP_Post_Handler::instance()->add_action( 'mainwp_module_log_delete_records', array( $this, 'ajax_delete_records' ) );
85 MainWP_Post_Handler::instance()->add_action( 'mainwp_module_log_compact_records', array( $this, 'ajax_compact_records' ) );
86 MainWP_Post_Handler::instance()->add_action( 'mainwp_module_log_events_display_rows', array( Log_Insights_Page::instance(), 'ajax_events_display_rows' ) );
87 }
88
89
90 /**
91 * Init sub menu logs.
92 *
93 * @param array $subpages sub pages.
94 *
95 * @action init
96 */
97 public function add_subpage_menu_logs( $subpages = array() ) {
98 $subpages[] = array(
99 'title' => esc_html__( 'Dashboard Insights', 'mainwp' ),
100 'slug' => 'DashboardInsights',
101 'callback' => array( $this, 'render_list_table' ),
102 'class' => 'mainwp-logs-menu',
103 );
104 return $subpages;
105 }
106
107 /**
108 * Render main page
109 */
110 public function render_list_table() {
111 $this->list_table = new Log_List_Table( $this->manager );
112 /** This action is documented in ../pages/page-mainwp-manage-sites.php */
113 do_action( 'mainwp_pageheader_settings', 'DashboardInsights' );
114 ?>
115 <div id="mainwp-manage-sites-content" class="ui segment">
116 <div class="ui form">
117 <h3 class="ui dividing header"><?php esc_html_e( 'Dashboard Insights', 'mainwp' ); ?></h3>
118 <h4 class="ui header"><?php printf( esc_html__( 'Total logs size: %1$s (MB)', 'mainwp' ), esc_html( $this->get_db_size() ) ); ?></h4>
119 <div id="mainwp-message-zone" style="display:none;" class="ui message"></div>
120 <form method="post" class="mainwp-table-container">
121 <?php
122 wp_nonce_field( 'mainwp-admin-nonce' );
123 $this->list_table->display();
124 ?>
125 </form>
126 </div>
127 </div>
128 <?php
129 /** This action is documented in ../pages/page-mainwp-manage-sites.php */
130 do_action( 'mainwp_pagefooter_settings', 'DashboardInsights' );
131 }
132
133 /**
134 * Enqueue scripts/styles for admin screen
135 *
136 * @action admin_enqueue_scripts
137 *
138 * @param string $hook Current hook.
139 *
140 * @return void
141 */
142 public function admin_enqueue_scripts( $hook ) {
143 $script_screens = array( 'mainwp_page_InsightsOverview', 'mainwp_page_SettingsDashboardInsights', 'mainwp_page_SettingsInsights' );
144 wp_enqueue_style( 'mainwp-module-log-admin', $this->manager->locations['url'] . 'ui/css/admin.css', array(), $this->manager->get_version() );
145
146 if ( in_array( $hook, $script_screens, true ) ) {
147 wp_enqueue_script(
148 'mainwp-module-log-admin',
149 $this->manager->locations['url'] . 'ui/js/admin.js',
150 array(
151 'jquery',
152 'mainwp',
153 ),
154 $this->manager->get_version(),
155 false
156 );
157
158 if ( in_array( $hook, array( 'mainwp_page_InsightsOverview' ), true ) ) {
159 add_filter(
160 'mainwp_admin_enqueue_scripts',
161 function ( $scripts ) {
162 if ( is_array( $scripts ) ) {
163 $scripts['apexcharts'] = true;
164 }
165 return $scripts;
166 }
167 );
168 }
169
170 wp_localize_script(
171 'mainwp-module-log-admin',
172 'mainwpModuleLog',
173 array(
174 'i18n' => array(),
175 'gmt_offset' => get_option( 'gmt_offset' ),
176 )
177 );
178 }
179 }
180
181
182 /**
183 * Method ajax_display_rows()
184 *
185 * Display table rows, optimize for shared hosting or big networks.
186 */
187 public function ajax_display_rows() {
188 MainWP_Post_Handler::instance()->check_security( 'mainwp_module_log_display_rows' );
189 $this->load_list_table();
190 $this->list_table->prepare_items();
191 $output = $this->list_table->ajax_get_datatable_rows();
192 MainWP_Logger::instance()->log_execution_time( 'ajax_display_rows()' );
193 wp_send_json( $output );
194 }
195
196 /**
197 * Handle ajax delete logs records.
198 */
199 public function ajax_delete_records() {
200 MainWP_Post_Handler::instance()->check_security( 'mainwp_module_log_delete_records' );
201
202 $start_date = isset( $_POST['startdate'] ) ? sanitize_text_field( wp_unslash( $_POST['startdate'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
203 $end_date = isset( $_POST['enddate'] ) ? sanitize_text_field( wp_unslash( $_POST['enddate'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
204
205 $start_time = ! empty( $start_date ) ? strtotime( $start_date . ' 00:00:00' ) : '';
206 $end_time = ! empty( $end_date ) ? strtotime( $end_date . ' 23:59:59' ) : '';
207
208 if ( ! is_numeric( $start_time ) || ! is_numeric( $end_time ) || $start_time > $end_time ) {
209 die( wp_json_encode( array( 'error' => esc_html__( 'Invalid Start date or end date. Please try again.' ) ) ) );
210 }
211
212 $this->manager->db->create_compact_and_erase_records( $start_time, $end_time );
213
214 wp_send_json( array( 'result' => 'SUCCESS' ) );
215 }
216
217
218 /**
219 * Handle ajax compact logs records.
220 */
221 public function ajax_compact_records() {
222 MainWP_Post_Handler::instance()->check_security( 'mainwp_module_log_compact_records' );
223
224 $year = isset( $_POST['year'] ) ? intval( $_POST['year'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
225
226 if ( $year < 2022 ) {
227 die( wp_json_encode( array( 'error' => esc_html__( 'Invalid selected year. Please try again.' ) ) ) );
228 }
229
230 $aday = $year . '-12-15'; // a day in last month.
231
232 $start_time = strtotime( $year . '-1-1 00:00:00' );
233 $end_time = strtotime( gmdate( 'Y-m-t', strtotime( $aday ) ) . ' 23:59:59' );
234
235 $this->manager->db->create_compact_and_erase_records( $start_time, $end_time );
236
237 wp_send_json( array( 'result' => 'SUCCESS' ) );
238 }
239
240 /**
241 * Method load_sites_table()
242 *
243 * Load sites table.
244 */
245 public function load_list_table() {
246 $this->list_table = new Log_List_Table( $this->manager );
247 }
248
249
250 /**
251 * Schedules a purge of records.
252 *
253 * @return void
254 */
255 public function hook_purge_scheduled_action() {
256 $enable_schedule = is_array( $this->manager->settings->options ) && ! empty( $this->manager->settings->options['enabled'] ) && ! empty( $this->manager->settings->options['auto_purge'] ) ? true : false;
257 if ( $enable_schedule ) {
258 $last_purge = get_option( 'mainwp_module_log_last_time_auto_purge_logs' );
259 $next_purge = get_option( 'mainwp_module_log_next_time_auto_purge_logs' );
260 $days = false;
261 if ( is_array( $this->manager->settings->options ) && isset( $this->manager->settings->options['records_ttl'] ) ) {
262 $days = intval( $this->manager->settings->options['records_ttl'] );
263 } else {
264 $days = 100;
265 }
266
267 if ( defined( 'MAINWP_MODULE_LOG_KEEP_RECORDS_TTL' ) && is_numeric( MAINWP_MODULE_LOG_KEEP_RECORDS_TTL ) && MAINWP_MODULE_LOG_KEEP_RECORDS_TTL > 0 ) {
268 $days = MAINWP_MODULE_LOG_KEEP_RECORDS_TTL;
269 }
270
271 if ( $days ) {
272 $time = time();
273 $next_time_purge = false;
274 if ( false === $last_purge && false === $next_purge ) {
275 $next_purge = $time + $days * DAY_IN_SECONDS;
276 } elseif ( ! empty( $next_purge ) && $time > (int) $next_purge ) {
277 do_action( 'mainwp_log_action', 'module log :: purge logs schedule start.', MainWP_Logger::LOGS_AUTO_PURGE_LOG_PRIORITY );
278
279 $end_time = $time - $days * DAY_IN_SECONDS;
280 $start_time = ! empty( $last_purge ) ? $last_purge : $end_time - $days * DAY_IN_SECONDS;
281
282 $this->manager->db->create_compact_and_erase_records( false, $end_time );
283 update_option( 'mainwp_module_log_last_time_auto_purge_logs', $time );
284 $next_time_purge = $time + $days * DAY_IN_SECONDS;
285 }
286 if ( $next_time_purge ) {
287 update_option( 'mainwp_module_log_next_time_auto_purge_logs', $next_time_purge );
288 }
289 }
290 }
291 }
292
293
294
295 /**
296 * Get db size.
297 *
298 * @return string Return current db size.
299 */
300 public function get_db_size() {
301 $size = get_transient( 'mainwp_module_log_transient_db_logs_size' );
302 if ( false !== $size ) {
303 return $size;
304 }
305
306 global $wpdb;
307 $sql = $wpdb->prepare(
308 'SELECT
309 ROUND(SUM(DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024, 2)
310 FROM INFORMATION_SCHEMA.TABLES
311 WHERE
312 TABLE_SCHEMA = %s
313 AND table_name = %s
314 OR table_name = %s',
315 $wpdb->dbname,
316 $wpdb->mainwp_tbl_logs,
317 $wpdb->mainwp_tbl_logs_meta
318 );
319
320 $dbsize_mb = $wpdb->get_var( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery -- prepared SQL.
321
322 set_transient( 'mainwp_module_log_transient_db_logs_size', $dbsize_mb, 15 * MINUTE_IN_SECONDS );
323
324 return $dbsize_mb;
325 }
326
327 /**
328 * Get WP users.
329 *
330 * @return array Array of users.
331 */
332 public function get_all_users() {
333 $list_users = array();
334 $all_users = get_users();
335 if ( is_array( $all_users ) ) {
336 foreach ( $all_users as $user ) {
337 if ( empty( $user->ID ) ) {
338 continue;
339 }
340 $fields = array();
341 $fields['id'] = $user->ID;
342 $fields['login'] = $user->user_login;
343 $fields['nicename'] = $user->user_nicename;
344 $list_users[] = $fields;
345 }
346 }
347 return $list_users;
348 }
349 }
350