PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.4
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.4
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.4, at modules/logs/classes/class-log-admin.php

327 lines 12.2 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_action( 'init', array( &$this, 'init' ) );
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 * Initiate Hooks
81 *
82 * Initiates hooks for the Dashboard insights module.
83 */
84 public function init() {
85 add_action( 'mainwp_help_sidebar_content', array( $this, 'mainwp_help_content' ) );
86 }
87
88 /**
89 * Handle admin_init action.
90 */
91 public function admin_init() {
92 MainWP_Post_Handler::instance()->add_action( 'mainwp_module_log_delete_records', array( $this, 'ajax_delete_records' ) );
93 MainWP_Post_Handler::instance()->add_action( 'mainwp_module_log_compact_records', array( $this, 'ajax_compact_records' ) );
94 MainWP_Post_Handler::instance()->add_action( 'mainwp_module_log_manage_events_display_rows', array( Log_Manage_Insights_Events_Page::instance(), 'ajax_manage_events_display_rows' ) );
95 MainWP_Post_Handler::instance()->add_action( 'mainwp_module_log_widget_insights_display_rows', array( Log_Insights_Page::instance(), 'ajax_events_display_rows' ) );
96 MainWP_Post_Handler::instance()->add_action( 'mainwp_module_log_widget_events_overview_display_rows', array( Log_Insights_Page::instance(), 'ajax_events_overview_display_rows' ) );
97 Log_Events_Filter_Segment::get_instance()->admin_init();
98 }
99
100 /**
101 * Enqueue scripts/styles for admin screen
102 *
103 * @action admin_enqueue_scripts
104 *
105 * @param string $hook Current hook.
106 *
107 * @return void
108 */
109 public function admin_enqueue_scripts( $hook ) {
110 $script_screens = array( 'mainwp_page_InsightsOverview', 'mainwp_page_SettingsDashboardInsights', 'mainwp_page_SettingsInsights' );
111 wp_enqueue_style( 'mainwp-module-log-admin', $this->manager->locations['url'] . 'ui/css/admin.css', array(), $this->manager->get_version() );
112
113 if ( in_array( $hook, $script_screens, true ) ) {
114 wp_enqueue_script(
115 'mainwp-module-log-admin',
116 $this->manager->locations['url'] . 'ui/js/admin.js',
117 array(
118 'jquery',
119 'mainwp',
120 ),
121 $this->manager->get_version(),
122 false
123 );
124
125 if ( in_array( $hook, array( 'mainwp_page_InsightsOverview' ), true ) ) {
126 add_filter(
127 'mainwp_admin_enqueue_scripts',
128 function ( $scripts ) {
129 if ( is_array( $scripts ) ) {
130 $scripts['apexcharts'] = true;
131 }
132 return $scripts;
133 }
134 );
135 }
136
137 wp_localize_script(
138 'mainwp-module-log-admin',
139 'mainwpModuleLog',
140 array(
141 'i18n' => array(),
142 'gmt_offset' => get_option( 'gmt_offset' ),
143 )
144 );
145 }
146 }
147
148 /**
149 * Handle ajax delete logs records.
150 */
151 public function ajax_delete_records() {
152 MainWP_Post_Handler::instance()->check_security( 'mainwp_module_log_delete_records' );
153
154 $start_date = isset( $_POST['startdate'] ) ? sanitize_text_field( wp_unslash( $_POST['startdate'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
155 $end_date = isset( $_POST['enddate'] ) ? sanitize_text_field( wp_unslash( $_POST['enddate'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
156
157 $start_time = ! empty( $start_date ) ? strtotime( $start_date . ' 00:00:00' ) : '';
158 $end_time = ! empty( $end_date ) ? strtotime( $end_date . ' 23:59:59' ) : '';
159
160 if ( ! is_numeric( $start_time ) || ! is_numeric( $end_time ) || $start_time > $end_time ) {
161 die( wp_json_encode( array( 'error' => esc_html__( 'Invalid Start date or end date. Please try again.', 'mainwp' ) ) ) );
162 }
163
164 $this->manager->db->create_compact_and_erase_records( $start_time, $end_time );
165
166 wp_send_json( array( 'result' => 'SUCCESS' ) );
167 }
168
169
170 /**
171 * Handle ajax compact logs records.
172 */
173 public function ajax_compact_records() {
174 MainWP_Post_Handler::instance()->check_security( 'mainwp_module_log_compact_records' );
175
176 $year = isset( $_POST['year'] ) ? intval( $_POST['year'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
177
178 if ( $year < 2022 ) {
179 die( wp_json_encode( array( 'error' => esc_html__( 'Invalid selected year. Please try again.', 'mainwp' ) ) ) );
180 }
181
182 $aday = $year . '-12-15'; // a day in last month.
183
184 $start_time = strtotime( $year . '-1-1 00:00:00' );
185 $end_time = strtotime( gmdate( 'Y-m-t', strtotime( $aday ) ) . ' 23:59:59' );
186
187 $this->manager->db->create_compact_and_erase_records( $start_time, $end_time );
188
189 wp_send_json( array( 'result' => 'SUCCESS' ) );
190 }
191
192 /**
193 * Schedules a purge of records.
194 *
195 * @return void
196 */
197 public function hook_purge_scheduled_action() { //phpcs:ignore -- NOSONAR - complex.
198 $enable_schedule = is_array( $this->manager->settings->options ) && ! empty( $this->manager->settings->options['enabled'] ) && ! empty( $this->manager->settings->options['auto_purge'] ) ? true : false;
199 if ( $enable_schedule ) {
200 $last_purge = get_option( 'mainwp_module_log_last_time_auto_purge_logs' );
201 $next_purge = get_option( 'mainwp_module_log_next_time_auto_purge_logs' );
202 $days = 100;
203 if ( is_array( $this->manager->settings->options ) && isset( $this->manager->settings->options['records_ttl'] ) ) {
204 $days = intval( $this->manager->settings->options['records_ttl'] );
205 }
206
207 if ( defined( 'MAINWP_MODULE_LOG_KEEP_RECORDS_TTL' ) && is_numeric( MAINWP_MODULE_LOG_KEEP_RECORDS_TTL ) && MAINWP_MODULE_LOG_KEEP_RECORDS_TTL > 0 ) {
208 $days = MAINWP_MODULE_LOG_KEEP_RECORDS_TTL;
209 }
210
211 if ( $days ) {
212 $time = time();
213 $next_time_purge = false;
214 if ( false === $last_purge && false === $next_purge ) {
215 $next_time_purge = $time + $days * DAY_IN_SECONDS;
216 } elseif ( ! empty( $next_purge ) && $time > (int) $next_purge ) {
217 do_action( 'mainwp_log_action', 'module log :: purge logs schedule start.', MainWP_Logger::LOGS_AUTO_PURGE_LOG_PRIORITY );
218 $end_time = $time - $days * DAY_IN_SECONDS;
219 $start_time = ! empty( $last_purge ) ? $last_purge : $end_time - $days * DAY_IN_SECONDS;
220 $this->manager->db->create_compact_and_erase_records( $start_time, $end_time );
221 update_option( 'mainwp_module_log_last_time_auto_purge_logs', $time );
222 $next_time_purge = $time + $days * DAY_IN_SECONDS;
223 }
224 if ( $next_time_purge ) {
225 update_option( 'mainwp_module_log_next_time_auto_purge_logs', $next_time_purge );
226 }
227 }
228 }
229 }
230
231
232 /**
233 * Get db size.
234 *
235 * @return string Return current db size.
236 */
237 public function get_db_size() {
238 $size = get_transient( 'mainwp_module_log_transient_db_logs_size' );
239 if ( false !== $size ) {
240 return $size;
241 }
242
243 global $wpdb;
244 $sql = $wpdb->prepare(
245 'SELECT
246 ROUND(SUM(DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024, 2)
247 FROM INFORMATION_SCHEMA.TABLES
248 WHERE
249 TABLE_SCHEMA = %s
250 AND table_name = %s
251 OR table_name = %s',
252 $wpdb->dbname,
253 $wpdb->mainwp_tbl_logs,
254 $wpdb->mainwp_tbl_logs_meta
255 );
256
257 $dbsize_mb = $wpdb->get_var( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery -- prepared SQL.
258
259 set_transient( 'mainwp_module_log_transient_db_logs_size', $dbsize_mb, 15 * MINUTE_IN_SECONDS );
260
261 return $dbsize_mb;
262 }
263
264 /**
265 * Get WP users.
266 *
267 * @return array Array of users.
268 */
269 public function get_all_users() {
270 $all_users = get_users(
271 array(
272 'fields' => array( 'ID', 'user_login', 'user_nicename' ),
273 )
274 );
275 if ( empty( $all_users ) || ! is_array( $all_users ) ) {
276 return array();
277 }
278 $all_users = array_map(
279 function ( $user ) {
280 if ( empty( $user->ID ) ) {
281 return false;
282 }
283 return array(
284 'id' => $user->ID,
285 'login' => $user->user_login,
286 'nicename' => $user->user_nicename,
287 );
288 },
289 $all_users
290 );
291 return array_filter( $all_users );
292 }
293
294 /**
295 * Method mainwp_help_content()
296 *
297 * Creates the MainWP Help Documentation List for the help component in the sidebar.
298 */
299 public static function mainwp_help_content() {
300 $allow_pages = array( 'InsightsOverview' );
301 if ( isset( $_GET['page'] ) && in_array( $_GET['page'], $allow_pages, true ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended
302 ?>
303 <p><?php esc_html_e( 'If you need help with the Dashboard Insights module, please review following help documents', 'mainwp' ); ?></p>
304 <div class="ui list">
305 <div class="item"><i class="external alternate icon"></i> <a href="https://mainwp.com/kb/dashboard-insights/" target="_blank">Dashboard Insights</a></div> <?php // NOSONAR -- compatible with help. ?>
306 <div class="item"><i class="external alternate icon"></i> <a href="https://mainwp.com/kb/dashboard-insights/#comprehensive-filtering-options" target="_blank">Filtering Options</a></div> <?php // NOSONAR -- compatible with help. ?>
307 <div class="item"><i class="external alternate icon"></i> <a href="https://mainwp.com/kb/dashboard-insights/#export-the-data-and-charts-from-an-individual-widget" target="_blank">Export Insights Data</a></div> <?php // NOSONAR -- compatible with help. ?>
308 <?php
309 /**
310 * Action: mainwp_module_dashboard_insights_help_item
311 *
312 * Fires at the bottom of the help articles list in the Help sidebar on the Insights page.
313 *
314 * Suggested HTML markup:
315 *
316 * <div class="item"><a href="Your custom URL">Your custom text</a></div>
317 *
318 * @since 5.2
319 */
320 do_action( 'mainwp_module_dashboard_insights_help_item' );
321 ?>
322 </div>
323 <?php
324 }
325 }
326 }
327