| @@ -9,10 +9,12 @@ | ||
| 9 | 9 | namespace MainWP\Dashboard\Module\Log; |
| 10 | 10 | |
| 11 | 11 | use MainWP\Dashboard\MainWP_Logger; |
| 12 | 12 | use MainWP\Dashboard\MainWP_Post_Handler; |
| 13 | +use MainWP\Dashboard\MainWP_Utility; | |
| 14 | +use MainWP\Dashboard\MainWP_DB; | |
| 15 | +use MainWP\Dashboard\MainWP_Connect; | |
| 13 | 16 | |
| 14 | - | |
| 15 | 17 | defined( 'ABSPATH' ) || exit; |
| 16 | 18 | |
| 17 | 19 | use DateTime; |
| 18 | 20 | use DateTimeZone; |
| @@ -23,328 +25,393 @@ | ||
| 23 | 25 | * Class - Log_Admin |
| 24 | 26 | */ |
| 25 | 27 | class Log_Admin { |
| 26 | 28 | |
| 27 | - /** | |
| 28 | - * Holds Instance of manager object | |
| 29 | - * | |
| 30 | - * @var Log_manager | |
| 31 | - */ | |
| 32 | - public $manager; | |
| 29 | + /** | |
| 30 | + * Holds Instance of manager object | |
| 31 | + * | |
| 32 | + * @var Log_manager | |
| 33 | + */ | |
| 34 | + public $manager; | |
| 33 | 35 | |
| 34 | 36 | |
| 35 | - /** | |
| 36 | - * Menu page screen id | |
| 37 | - * | |
| 38 | - * @var string | |
| 39 | - */ | |
| 40 | - public $screen_id = array(); | |
| 37 | + /** | |
| 38 | + * Menu page screen id | |
| 39 | + * | |
| 40 | + * @var string | |
| 41 | + */ | |
| 42 | + public $screen_id = array(); | |
| 41 | 43 | |
| 42 | - /** | |
| 43 | - * List table object | |
| 44 | - * | |
| 45 | - * @var List_Table | |
| 46 | - */ | |
| 47 | - public $list_table = null; | |
| 44 | + /** | |
| 45 | + * List table object | |
| 46 | + * | |
| 47 | + * @var List_Table | |
| 48 | + */ | |
| 49 | + public $list_table = null; | |
| 48 | 50 | |
| 49 | - /** | |
| 50 | - * Parent page of the records and settings pages | |
| 51 | - * | |
| 52 | - * @var string | |
| 53 | - */ | |
| 54 | - public $admin_parent_page = 'admin.php'; | |
| 51 | + /** | |
| 52 | + * Parent page of the records and settings pages | |
| 53 | + * | |
| 54 | + * @var string | |
| 55 | + */ | |
| 56 | + public $admin_parent_page = 'admin.php'; | |
| 55 | 57 | |
| 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 | - ); | |
| 58 | + /** | |
| 59 | + * Class constructor. | |
| 60 | + * | |
| 61 | + * @param Log_Manager $manager Instance of manager object. | |
| 62 | + */ | |
| 63 | + public function __construct( $manager ) { | |
| 64 | + $this->manager = $manager; | |
| 65 | + add_action( 'init', array( &$this, 'init' ) ); | |
| 66 | + // Load admin scripts and styles. | |
| 67 | + add_action( | |
| 68 | + 'admin_enqueue_scripts', | |
| 69 | + array( | |
| 70 | + $this, | |
| 71 | + 'admin_enqueue_scripts', | |
| 72 | + ), | |
| 73 | + 9 | |
| 74 | + ); | |
| 73 | 75 | |
| 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 | - } | |
| 76 | + // Auto purge setup. | |
| 77 | + add_action( 'admin_init', array( $this, 'admin_init' ) ); | |
| 78 | + $log_page = Log_Insights_Page::instance(); | |
| 79 | + MainWP_Post_Handler::instance()->add_action( 'mainwp_module_log_delete_records', array( $this, 'ajax_delete_records' ) ); | |
| 80 | + MainWP_Post_Handler::instance()->add_action( 'mainwp_module_log_compact_records', array( $this, 'ajax_compact_records' ) ); | |
| 81 | + MainWP_Post_Handler::instance()->add_action( 'mainwp_module_log_clean_up_child_logs', array( $this, 'ajax_clean_up_child_logs' ) ); | |
| 82 | + 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' ) ); | |
| 83 | + MainWP_Post_Handler::instance()->add_action( 'mainwp_module_log_widget_insights_display_rows', array( $log_page, 'ajax_events_display_rows' ) ); | |
| 84 | + MainWP_Post_Handler::instance()->add_action( 'mainwp_module_log_widget_events_overview_display_rows', array( $log_page, 'ajax_events_overview_display_rows' ) ); | |
| 85 | + MainWP_Post_Handler::instance()->add_action( 'mainwp_module_log_update_dismissed_db', array( $log_page, 'ajax_archive_dismissed_db' ) ); | |
| 86 | + MainWP_Post_Handler::instance()->add_action( 'mainwp_module_log_cancel_update_dismissed_db', array( $log_page, 'ajax_cancel_update_dismissed_db' ) ); | |
| 87 | + } | |
| 78 | 88 | |
| 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 | - Log_Events_Filter_Segment::get_instance()->admin_init(); | |
| 88 | - } | |
| 89 | + /** | |
| 90 | + * Initiate Hooks | |
| 91 | + * | |
| 92 | + * Initiates hooks for the Dashboard insights module. | |
| 93 | + */ | |
| 94 | + public function init() { | |
| 95 | + add_action( 'mainwp_help_sidebar_content', array( $this, 'mainwp_help_content' ) ); | |
| 96 | + } | |
| 89 | 97 | |
| 98 | + /** | |
| 99 | + * Handle admin_init action. | |
| 100 | + */ | |
| 101 | + public function admin_init() { | |
| 102 | + Log_Events_Filter_Segment::get_instance()->admin_init(); | |
| 103 | + $this->handle_post_archive_data(); | |
| 104 | + } | |
| 90 | 105 | |
| 91 | - /** | |
| 92 | - * Init sub menu logs. | |
| 93 | - * | |
| 94 | - * @param array $subpages sub pages. | |
| 95 | - * | |
| 96 | - * @action init | |
| 97 | - */ | |
| 98 | - public function add_subpage_menu_logs( $subpages = array() ) { | |
| 99 | - $subpages[] = array( | |
| 100 | - 'title' => esc_html__( 'Dashboard Insights', 'mainwp' ), | |
| 101 | - 'slug' => 'DashboardInsights', | |
| 102 | - 'callback' => array( $this, 'render_list_table' ), | |
| 103 | - 'class' => 'mainwp-logs-menu', | |
| 104 | - ); | |
| 105 | - return $subpages; | |
| 106 | - } | |
| 106 | + /** | |
| 107 | + * Handle archive data action. | |
| 108 | + */ | |
| 109 | + public function handle_post_archive_data() { | |
| 110 | + if ( isset( $_GET['clearArchivedSitesChangesData'] ) && isset( $_GET['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'clear_archived_sites_changes' ) ) { // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 111 | + Log_DB_Archive::instance()->truncate_archive_tables(); | |
| 112 | + wp_safe_redirect( esc_url( admin_url( 'admin.php?page=MainWPTools' ) ) ); | |
| 113 | + die(); | |
| 114 | + } | |
| 115 | + } | |
| 107 | 116 | |
| 108 | - /** | |
| 109 | - * Render main page | |
| 110 | - */ | |
| 111 | - public function render_list_table() { | |
| 112 | - $this->list_table = new Log_List_Table( $this->manager ); | |
| 113 | - /** This action is documented in ../pages/page-mainwp-manage-sites.php */ | |
| 114 | - do_action( 'mainwp_pageheader_settings', 'DashboardInsights' ); | |
| 115 | - ?> | |
| 116 | - <div id="mainwp-manage-sites-content" class="ui segment"> | |
| 117 | - <div class="ui form"> | |
| 118 | - <h3 class="ui dividing header"><?php esc_html_e( 'Dashboard Insights', 'mainwp' ); ?></h3> | |
| 119 | - <h4 class="ui header"><?php printf( esc_html__( 'Total logs size: %1$s (MB)', 'mainwp' ), esc_html( $this->get_db_size() ) ); ?></h4> | |
| 120 | - <div id="mainwp-message-zone" style="display:none;" class="ui message"></div> | |
| 121 | - <form method="post" class="mainwp-table-container"> | |
| 122 | - <?php | |
| 123 | - wp_nonce_field( 'mainwp-admin-nonce' ); | |
| 124 | - $this->list_table->display(); | |
| 125 | - ?> | |
| 126 | - </form> | |
| 127 | - </div> | |
| 128 | - </div> | |
| 129 | - <?php | |
| 130 | - /** This action is documented in ../pages/page-mainwp-manage-sites.php */ | |
| 131 | - do_action( 'mainwp_pagefooter_settings', 'DashboardInsights' ); | |
| 132 | - } | |
| 117 | + /** | |
| 118 | + * Enqueue scripts/styles for admin screen | |
| 119 | + * | |
| 120 | + * @action admin_enqueue_scripts | |
| 121 | + * | |
| 122 | + * @param string $hook Current hook. | |
| 123 | + * | |
| 124 | + * @return void | |
| 125 | + */ | |
| 126 | + public function admin_enqueue_scripts( $hook ) { | |
| 127 | + $script_screens = array( 'mainwp_page_InsightsOverview', 'mainwp_page_SettingsDashboardInsights', 'mainwp_page_SettingsInsights', 'mainwp_page_InsightsManage' ); | |
| 128 | + wp_enqueue_style( 'mainwp-module-log-admin', $this->manager->locations['url'] . 'ui/css/admin.css', array(), $this->manager->get_version() ); | |
| 133 | 129 | |
| 134 | - /** | |
| 135 | - * Enqueue scripts/styles for admin screen | |
| 136 | - * | |
| 137 | - * @action admin_enqueue_scripts | |
| 138 | - * | |
| 139 | - * @param string $hook Current hook. | |
| 140 | - * | |
| 141 | - * @return void | |
| 142 | - */ | |
| 143 | - public function admin_enqueue_scripts( $hook ) { | |
| 144 | - $script_screens = array( 'mainwp_page_InsightsOverview', 'mainwp_page_SettingsDashboardInsights', 'mainwp_page_SettingsInsights' ); | |
| 145 | - wp_enqueue_style( 'mainwp-module-log-admin', $this->manager->locations['url'] . 'ui/css/admin.css', array(), $this->manager->get_version() ); | |
| 130 | + if ( in_array( $hook, $script_screens, true ) ) { | |
| 131 | + wp_enqueue_script( | |
| 132 | + 'mainwp-module-log-admin', | |
| 133 | + $this->manager->locations['url'] . 'ui/js/admin.js', | |
| 134 | + array( | |
| 135 | + 'jquery', | |
| 136 | + 'mainwp', | |
| 137 | + ), | |
| 138 | + $this->manager->get_version(), | |
| 139 | + false | |
| 140 | + ); | |
| 146 | 141 | |
| 147 | - if ( in_array( $hook, $script_screens, true ) ) { | |
| 148 | - wp_enqueue_script( | |
| 149 | - 'mainwp-module-log-admin', | |
| 150 | - $this->manager->locations['url'] . 'ui/js/admin.js', | |
| 151 | - array( | |
| 152 | - 'jquery', | |
| 153 | - 'mainwp', | |
| 154 | - ), | |
| 155 | - $this->manager->get_version(), | |
| 156 | - false | |
| 157 | - ); | |
| 142 | + if ( in_array( $hook, array( 'mainwp_page_InsightsOverview' ), true ) ) { | |
| 143 | + add_filter( | |
| 144 | + 'mainwp_admin_enqueue_scripts', | |
| 145 | + function ( $scripts ) { | |
| 146 | + if ( is_array( $scripts ) ) { | |
| 147 | + $scripts['apexcharts'] = true; | |
| 148 | + } | |
| 149 | + return $scripts; | |
| 150 | + } | |
| 151 | + ); | |
| 152 | + } | |
| 158 | 153 | |
| 159 | - if ( in_array( $hook, array( 'mainwp_page_InsightsOverview' ), true ) ) { | |
| 160 | - add_filter( | |
| 161 | - 'mainwp_admin_enqueue_scripts', | |
| 162 | - function ( $scripts ) { | |
| 163 | - if ( is_array( $scripts ) ) { | |
| 164 | - $scripts['apexcharts'] = true; | |
| 165 | - } | |
| 166 | - return $scripts; | |
| 167 | - } | |
| 168 | - ); | |
| 169 | - } | |
| 154 | + wp_localize_script( | |
| 155 | + 'mainwp-module-log-admin', | |
| 156 | + 'mainwpModuleLog', | |
| 157 | + array( | |
| 158 | + 'i18n' => array(), | |
| 159 | + 'gmt_offset' => get_option( 'gmt_offset' ), | |
| 160 | + ) | |
| 161 | + ); | |
| 162 | + } | |
| 163 | + } | |
| 170 | 164 | |
| 171 | - wp_localize_script( | |
| 172 | - 'mainwp-module-log-admin', | |
| 173 | - 'mainwpModuleLog', | |
| 174 | - array( | |
| 175 | - 'i18n' => array(), | |
| 176 | - 'gmt_offset' => get_option( 'gmt_offset' ), | |
| 177 | - ) | |
| 178 | - ); | |
| 179 | - } | |
| 180 | - } | |
| 165 | + /** | |
| 166 | + * Method hook_schedules_cron_listing(). | |
| 167 | + * | |
| 168 | + * @param array $cron_list Cron info. | |
| 169 | + * @return array Cron info. | |
| 170 | + */ | |
| 171 | + public function hook_schedules_cron_listing( $cron_list = array() ) { | |
| 181 | 172 | |
| 173 | + if ( ! $this->manager->is_enabled_auto_archive_logs() ) { | |
| 174 | + return $cron_list; | |
| 175 | + } | |
| 182 | 176 | |
| 183 | - /** | |
| 184 | - * Method ajax_display_rows() | |
| 185 | - * | |
| 186 | - * Display table rows, optimize for shared hosting or big networks. | |
| 187 | - */ | |
| 188 | - public function ajax_display_rows() { | |
| 189 | - MainWP_Post_Handler::instance()->check_security( 'mainwp_module_log_display_rows' ); | |
| 190 | - $this->load_list_table(); | |
| 191 | - $this->list_table->prepare_items(); | |
| 192 | - $output = $this->list_table->ajax_get_datatable_rows(); | |
| 193 | - MainWP_Logger::instance()->log_execution_time( 'ajax_display_rows()' ); | |
| 194 | - wp_send_json( $output ); | |
| 195 | - } | |
| 177 | + if ( ! is_array( $cron_list ) ) { | |
| 178 | + $cron_list = array(); | |
| 179 | + } | |
| 180 | + $start_lasttime = get_option( 'mainwp_module_log_last_time_auto_archive_logs', 0 ); | |
| 181 | + $start_nexttime = wp_next_scheduled( 'mainwp_module_log_cron_job_auto_archive' ); | |
| 182 | + $cron_list['mainwp_module_log_cron_job_auto_archive'] = array( | |
| 183 | + 'title' => __( 'Auto archive Network Activity logs', 'mainwp' ), | |
| 184 | + 'action' => 'mainwp_module_log_cron_job_auto_archive', | |
| 185 | + 'frequency' => __( 'Once daily', 'mainwp' ), | |
| 186 | + 'last_run' => empty( $start_lasttime ) ? 'N/A' : MainWP_Utility::format_timestamp( $start_lasttime ), | |
| 187 | + 'next_run' => empty( $start_nexttime ) ? 'N/A' : MainWP_Utility::format_timestamp( $start_nexttime ), | |
| 188 | + ); | |
| 189 | + return $cron_list; | |
| 190 | + } | |
| 196 | 191 | |
| 197 | - /** | |
| 198 | - * Handle ajax delete logs records. | |
| 199 | - */ | |
| 200 | - public function ajax_delete_records() { | |
| 201 | - MainWP_Post_Handler::instance()->check_security( 'mainwp_module_log_delete_records' ); | |
| 192 | + /** | |
| 193 | + * Handle ajax delete logs records. | |
| 194 | + */ | |
| 195 | + public function ajax_delete_records() { | |
| 196 | + MainWP_Post_Handler::instance()->check_security( 'mainwp_module_log_delete_records' ); | |
| 202 | 197 | |
| 203 | - $start_date = isset( $_POST['startdate'] ) ? sanitize_text_field( wp_unslash( $_POST['startdate'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 204 | - $end_date = isset( $_POST['enddate'] ) ? sanitize_text_field( wp_unslash( $_POST['enddate'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 198 | + $start_date = isset( $_POST['startdate'] ) ? sanitize_text_field( wp_unslash( $_POST['startdate'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 199 | + $end_date = isset( $_POST['enddate'] ) ? sanitize_text_field( wp_unslash( $_POST['enddate'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 205 | 200 | |
| 206 | - $start_time = ! empty( $start_date ) ? strtotime( $start_date . ' 00:00:00' ) : ''; | |
| 207 | - $end_time = ! empty( $end_date ) ? strtotime( $end_date . ' 23:59:59' ) : ''; | |
| 201 | + $start_time = ! empty( $start_date ) ? strtotime( $start_date . ' 00:00:00' ) : ''; | |
| 202 | + $end_time = ! empty( $end_date ) ? strtotime( $end_date . ' 23:59:59' ) : ''; | |
| 208 | 203 | |
| 209 | - if ( ! is_numeric( $start_time ) || ! is_numeric( $end_time ) || $start_time > $end_time ) { | |
| 210 | - die( wp_json_encode( array( 'error' => esc_html__( 'Invalid Start date or end date. Please try again.' ) ) ) ); | |
| 211 | - } | |
| 204 | + if ( ! is_numeric( $start_time ) || ! is_numeric( $end_time ) || $start_time > $end_time ) { | |
| 205 | + die( wp_json_encode( array( 'error' => esc_html__( 'Invalid Start date or end date. Please try again.', 'mainwp' ) ) ) ); | |
| 206 | + } | |
| 212 | 207 | |
| 213 | - $this->manager->db->create_compact_and_erase_records( $start_time, $end_time ); | |
| 208 | + $this->manager->db->create_compact_and_erase_records( $start_time, $end_time ); | |
| 214 | 209 | |
| 215 | - wp_send_json( array( 'result' => 'SUCCESS' ) ); | |
| 216 | - } | |
| 210 | + wp_send_json( array( 'result' => 'SUCCESS' ) ); | |
| 211 | + } | |
| 217 | 212 | |
| 218 | 213 | |
| 219 | - /** | |
| 220 | - * Handle ajax compact logs records. | |
| 221 | - */ | |
| 222 | - public function ajax_compact_records() { | |
| 223 | - MainWP_Post_Handler::instance()->check_security( 'mainwp_module_log_compact_records' ); | |
| 214 | + /** | |
| 215 | + * Handle ajax compact logs records. | |
| 216 | + */ | |
| 217 | + public function ajax_compact_records() { | |
| 218 | + MainWP_Post_Handler::instance()->check_security( 'mainwp_module_log_compact_records' ); | |
| 224 | 219 | |
| 225 | - $year = isset( $_POST['year'] ) ? intval( $_POST['year'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 220 | + $year = isset( $_POST['year'] ) ? intval( $_POST['year'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 226 | 221 | |
| 227 | - if ( $year < 2022 ) { | |
| 228 | - die( wp_json_encode( array( 'error' => esc_html__( 'Invalid selected year. Please try again.' ) ) ) ); | |
| 229 | - } | |
| 222 | + if ( $year < 2022 ) { | |
| 223 | + die( wp_json_encode( array( 'error' => esc_html__( 'Invalid selected year. Please try again.', 'mainwp' ) ) ) ); | |
| 224 | + } | |
| 230 | 225 | |
| 231 | - $aday = $year . '-12-15'; // a day in last month. | |
| 226 | + $aday = $year . '-12-15'; // a day in last month. | |
| 232 | 227 | |
| 233 | - $start_time = strtotime( $year . '-1-1 00:00:00' ); | |
| 234 | - $end_time = strtotime( gmdate( 'Y-m-t', strtotime( $aday ) ) . ' 23:59:59' ); | |
| 228 | + $start_time = strtotime( $year . '-1-1 00:00:00' ); | |
| 229 | + $end_time = strtotime( gmdate( 'Y-m-t', strtotime( $aday ) ) . ' 23:59:59' ); | |
| 235 | 230 | |
| 236 | - $this->manager->db->create_compact_and_erase_records( $start_time, $end_time ); | |
| 231 | + $this->manager->db->create_compact_and_erase_records( $start_time, $end_time ); | |
| 237 | 232 | |
| 238 | - wp_send_json( array( 'result' => 'SUCCESS' ) ); | |
| 239 | - } | |
| 233 | + wp_send_json( array( 'result' => 'SUCCESS' ) ); | |
| 234 | + } | |
| 240 | 235 | |
| 241 | - /** | |
| 242 | - * Method load_sites_table() | |
| 243 | - * | |
| 244 | - * Load sites table. | |
| 245 | - */ | |
| 246 | - public function load_list_table() { | |
| 247 | - $this->list_table = new Log_List_Table( $this->manager ); | |
| 248 | - } | |
| 236 | + /** | |
| 237 | + * Handle ajax clean up child logs. | |
| 238 | + */ | |
| 239 | + public function ajax_clean_up_child_logs() { | |
| 240 | + MainWP_Post_Handler::instance()->check_security( 'mainwp_module_log_clean_up_child_logs' ); | |
| 249 | 241 | |
| 242 | + $siteid = isset( $_POST['siteid'] ) ? intval( $_POST['siteid'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 243 | + $website = false; | |
| 250 | 244 | |
| 251 | - /** | |
| 252 | - * Schedules a purge of records. | |
| 253 | - * | |
| 254 | - * @return void | |
| 255 | - */ | |
| 256 | - public function hook_purge_scheduled_action() { | |
| 257 | - $enable_schedule = is_array( $this->manager->settings->options ) && ! empty( $this->manager->settings->options['enabled'] ) && ! empty( $this->manager->settings->options['auto_purge'] ) ? true : false; | |
| 258 | - if ( $enable_schedule ) { | |
| 259 | - $last_purge = get_option( 'mainwp_module_log_last_time_auto_purge_logs' ); | |
| 260 | - $next_purge = get_option( 'mainwp_module_log_next_time_auto_purge_logs' ); | |
| 261 | - $days = false; | |
| 262 | - if ( is_array( $this->manager->settings->options ) && isset( $this->manager->settings->options['records_ttl'] ) ) { | |
| 263 | - $days = intval( $this->manager->settings->options['records_ttl'] ); | |
| 264 | - } else { | |
| 265 | - $days = 100; | |
| 266 | - } | |
| 245 | + if ( $siteid > 0 ) { | |
| 246 | + $website = MainWP_DB::instance()->get_website_by_id( $siteid ); | |
| 247 | + } | |
| 267 | 248 | |
| 268 | - if ( defined( 'MAINWP_MODULE_LOG_KEEP_RECORDS_TTL' ) && is_numeric( MAINWP_MODULE_LOG_KEEP_RECORDS_TTL ) && MAINWP_MODULE_LOG_KEEP_RECORDS_TTL > 0 ) { | |
| 269 | - $days = MAINWP_MODULE_LOG_KEEP_RECORDS_TTL; | |
| 270 | - } | |
| 249 | + if ( empty( $siteid ) || empty( $website ) ) { | |
| 250 | + die( wp_json_encode( array( 'error' => esc_html__( 'Invalid site id or website not found. Please try again.', 'mainwp' ) ) ) ); | |
| 251 | + } | |
| 271 | 252 | |
| 272 | - if ( $days ) { | |
| 273 | - $time = time(); | |
| 274 | - $next_time_purge = false; | |
| 275 | - if ( false === $last_purge && false === $next_purge ) { | |
| 276 | - $next_purge = $time + $days * DAY_IN_SECONDS; | |
| 277 | - } elseif ( ! empty( $next_purge ) && $time > (int) $next_purge ) { | |
| 278 | - do_action( 'mainwp_log_action', 'module log :: purge logs schedule start.', MainWP_Logger::LOGS_AUTO_PURGE_LOG_PRIORITY ); | |
| 253 | + $information = MainWP_Connect::fetch_url_authed( $website, 'clean_up_child_logs' ); | |
| 279 | 254 | |
| 280 | - $end_time = $time - $days * DAY_IN_SECONDS; | |
| 281 | - $start_time = ! empty( $last_purge ) ? $last_purge : $end_time - $days * DAY_IN_SECONDS; | |
| 255 | + if ( ! is_array( $information ) ) { | |
| 256 | + die( wp_json_encode( array( 'error' => esc_html__( 'Invalid response from child site. Please try again.', 'mainwp' ) ) ) ); | |
| 257 | + } | |
| 282 | 258 | |
| 283 | - $this->manager->db->create_compact_and_erase_records( false, $end_time ); | |
| 284 | - update_option( 'mainwp_module_log_last_time_auto_purge_logs', $time ); | |
| 285 | - $next_time_purge = $time + $days * DAY_IN_SECONDS; | |
| 286 | - } | |
| 287 | - if ( $next_time_purge ) { | |
| 288 | - update_option( 'mainwp_module_log_next_time_auto_purge_logs', $next_time_purge ); | |
| 289 | - } | |
| 290 | - } | |
| 291 | - } | |
| 292 | - } | |
| 259 | + $rsult = array(); | |
| 260 | + if ( isset( $information['success'] ) && 1 === intval( $information['success'] ) ) { | |
| 261 | + $rsult['success'] = 1; | |
| 293 | 262 | |
| 263 | + if ( isset( $information['dbsize_activitylogs'] ) ) { | |
| 264 | + MainWP_DB::instance()->update_website_option( $website, 'dbsize_activitylogs', $information['dbsize_activitylogs'] ); | |
| 265 | + } | |
| 266 | + } elseif ( isset( $information['error'] ) ) { | |
| 267 | + $rsult['error'] = $information['error']; | |
| 268 | + } | |
| 294 | 269 | |
| 270 | + wp_send_json( $rsult ); | |
| 271 | + } | |
| 295 | 272 | |
| 296 | - /** | |
| 297 | - * Get db size. | |
| 298 | - * | |
| 299 | - * @return string Return current db size. | |
| 300 | - */ | |
| 301 | - public function get_db_size() { | |
| 302 | - $size = get_transient( 'mainwp_module_log_transient_db_logs_size' ); | |
| 303 | - if ( false !== $size ) { | |
| 304 | - return $size; | |
| 305 | - } | |
| 273 | + /** | |
| 274 | + * Schedules a purge of records. | |
| 275 | + * | |
| 276 | + * @compatible | |
| 277 | + * | |
| 278 | + * @return void | |
| 279 | + */ | |
| 280 | + public function hook_purge_scheduled_action() { //phpcs:ignore -- NOSONAR - complex. | |
| 281 | + $enable_schedule = is_array( $this->manager->settings->options ) && ! empty( $this->manager->settings->options['enabled'] ) && ! empty( $this->manager->settings->options['auto_purge'] ) ? true : false; | |
| 282 | + if ( $enable_schedule ) { | |
| 283 | + $last_purge = get_option( 'mainwp_module_log_last_time_auto_purge_logs' ); | |
| 284 | + $next_purge = get_option( 'mainwp_module_log_next_time_auto_purge_logs' ); | |
| 285 | + $days = 100; | |
| 286 | + if ( is_array( $this->manager->settings->options ) && isset( $this->manager->settings->options['records_ttl'] ) ) { | |
| 287 | + $days = intval( $this->manager->settings->options['records_ttl'] ); | |
| 288 | + } | |
| 306 | 289 | |
| 307 | - global $wpdb; | |
| 308 | - $sql = $wpdb->prepare( | |
| 309 | - 'SELECT | |
| 310 | - ROUND(SUM(DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024, 2) | |
| 311 | - FROM INFORMATION_SCHEMA.TABLES | |
| 312 | - WHERE | |
| 313 | - TABLE_SCHEMA = %s | |
| 314 | - AND table_name = %s | |
| 315 | - OR table_name = %s', | |
| 316 | - $wpdb->dbname, | |
| 317 | - $wpdb->mainwp_tbl_logs, | |
| 318 | - $wpdb->mainwp_tbl_logs_meta | |
| 319 | - ); | |
| 290 | + if ( defined( 'MAINWP_MODULE_LOG_KEEP_RECORDS_TTL' ) && is_numeric( MAINWP_MODULE_LOG_KEEP_RECORDS_TTL ) && MAINWP_MODULE_LOG_KEEP_RECORDS_TTL > 0 ) { | |
| 291 | + $days = MAINWP_MODULE_LOG_KEEP_RECORDS_TTL; | |
| 292 | + } | |
| 320 | 293 | |
| 321 | - $dbsize_mb = $wpdb->get_var( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery -- prepared SQL. | |
| 294 | + if ( $days ) { | |
| 295 | + $time = time(); | |
| 296 | + $next_time_purge = false; | |
| 297 | + if ( false === $last_purge && false === $next_purge ) { | |
| 298 | + $next_time_purge = $time + $days * DAY_IN_SECONDS; | |
| 299 | + } elseif ( ! empty( $next_purge ) && $time > (int) $next_purge ) { | |
| 300 | + do_action( 'mainwp_log_action', 'module log :: purge logs schedule start.', MainWP_Logger::LOGS_AUTO_PURGE_LOG_PRIORITY ); | |
| 301 | + $end_time = $time - $days * DAY_IN_SECONDS; | |
| 302 | + $start_time = ! empty( $last_purge ) ? $last_purge : $end_time - $days * DAY_IN_SECONDS; | |
| 303 | + $this->manager->db->create_compact_and_erase_records( $start_time, $end_time ); | |
| 304 | + update_option( 'mainwp_module_log_last_time_auto_purge_logs', $time ); | |
| 305 | + $next_time_purge = $time + $days * DAY_IN_SECONDS; | |
| 306 | + } | |
| 307 | + if ( $next_time_purge ) { | |
| 308 | + update_option( 'mainwp_module_log_next_time_auto_purge_logs', $next_time_purge ); | |
| 309 | + } | |
| 310 | + } | |
| 311 | + } | |
| 312 | + } | |
| 322 | 313 | |
| 323 | - set_transient( 'mainwp_module_log_transient_db_logs_size', $dbsize_mb, 15 * MINUTE_IN_SECONDS ); | |
| 324 | 314 | |
| 325 | - return $dbsize_mb; | |
| 326 | - } | |
| 315 | + /** | |
| 316 | + * Render logs db large size notice. | |
| 317 | + * | |
| 318 | + * @param int $limit DB size limit in MByte, default 300 MB. | |
| 319 | + * @param string $which The page slug where the notice is displayed. | |
| 320 | + */ | |
| 321 | + public function render_logs_db_notice( $limit = 300, $which = '' ) { | |
| 322 | + if ( empty( $limit ) ) { | |
| 323 | + $limit = 300; // MB. | |
| 324 | + } | |
| 325 | + if ( MainWP_Utility::show_mainwp_message( 'notice', 'logs-db-size-large' ) ) { | |
| 326 | + $size = Log_DB_Helper::instance()->get_db_size(); | |
| 327 | + if ( $size >= $limit ) { | |
| 328 | + ?> | |
| 329 | + <div class="ui yellow message" <?php echo 'overview' === $which || 'client_overview' === $which ? 'style="margin: 1em;"' : ''; ?>> | |
| 330 | + <i class="close icon mainwp-notice-dismiss" notice-id="logs-db-size-large"></i> | |
| 331 | + <?php | |
| 332 | + /* translators: 1: Database size in MB, 2: Opening anchor tag, 3: Closing anchor tag */ | |
| 333 | + printf( esc_html__( 'Your Network Activity logs are using a lot of database space (%1$s MB). Go to %2$sMainWP > Settings > Network Activity Settings%3$s and enable Automatically archive logs, then set a Data retention period to keep the table size under control.', 'mainwp' ), esc_html( $size ), '<a href="admin.php?page=SettingsInsights#field-log-enable-auto-archive">', '</a>' ); // NOSONAR - noopener - open safe. | |
| 334 | + ?> | |
| 335 | + </div> | |
| 336 | + <?php | |
| 337 | + } | |
| 338 | + } | |
| 339 | + } | |
| 327 | 340 | |
| 328 | - /** | |
| 329 | - * Get WP users. | |
| 330 | - * | |
| 331 | - * @return array Array of users. | |
| 332 | - */ | |
| 333 | - public function get_all_users() { | |
| 334 | - $list_users = array(); | |
| 335 | - $all_users = get_users(); | |
| 336 | - if ( is_array( $all_users ) ) { | |
| 337 | - foreach ( $all_users as $user ) { | |
| 338 | - if ( empty( $user->ID ) ) { | |
| 339 | - continue; | |
| 340 | - } | |
| 341 | - $fields = array(); | |
| 342 | - $fields['id'] = $user->ID; | |
| 343 | - $fields['login'] = $user->user_login; | |
| 344 | - $fields['nicename'] = $user->user_nicename; | |
| 345 | - $list_users[] = $fields; | |
| 346 | - } | |
| 347 | - } | |
| 348 | - return $list_users; | |
| 349 | - } | |
| 341 | + /** | |
| 342 | + * Render logs db update notice. | |
| 343 | + */ | |
| 344 | + public function render_update_db_notice() { | |
| 345 | + $status = ''; | |
| 346 | + $dbver = Log_Install::instance()->get_current_logs_db_ver(); | |
| 347 | + if ( version_compare( $dbver, '1.0.1.26', '=' ) && ! get_option( 'mainwp_module_logs_updates_dismissed_db_cancelled' ) ) { // NOSONAR - no ip,checks ver <= 22 only. | |
| 348 | + $status = get_option( 'mainwp_module_logs_updates_dismissed_db_process_status', '' ); | |
| 349 | + } | |
| 350 | + ?> | |
| 351 | + <div class="ui green message" id="module-log-update-dissmised-logs-running" style="display:none;"></div> | |
| 352 | + <?php | |
| 353 | + if ( 'require_update' === $status && MainWP_Utility::show_mainwp_message( 'notice', 'logs-db-update-required' ) ) { | |
| 354 | + delete_option( 'mainwp_module_logs_updates_dismissed_db_cancelled' ); // To ensure it does not retrieve old saved values. | |
| 355 | + ?> | |
| 356 | + <div class="ui yellow message"> | |
| 357 | + <i class="close icon mainwp-notice-dismiss" notice-id="logs-db-update-required"></i> | |
| 358 | + <?php | |
| 359 | + /* translators: 1: Opening anchor tag, 2: Closing anchor tag */ | |
| 360 | + printf( esc_html__( 'Your \'Network Activity\' database needs to be updated. Click %1$shere%2$s to start the update.', 'mainwp' ), '<a href="javascript:void(0);" id="module-update-logs-db-requirement">', '</a>' ); | |
| 361 | + ?> | |
| 362 | + </div> | |
| 363 | + <?php | |
| 364 | + } elseif ( 'running' === $status ) { | |
| 365 | + ?> | |
| 366 | + <script type="text/javascript"> | |
| 367 | + jQuery(function ($) { | |
| 368 | + mainwp_module_logs_start_update_dismissed_db(); | |
| 369 | + }); | |
| 370 | + </script> | |
| 371 | + <?php | |
| 372 | + } | |
| 373 | + } | |
| 374 | + | |
| 375 | + /** | |
| 376 | + * Get WP users. | |
| 377 | + * | |
| 378 | + * @return array Array of users. | |
| 379 | + */ | |
| 380 | + public function get_all_users() { | |
| 381 | + $users_sites = Log_DB_Helper::instance()->get_logs_users(); | |
| 382 | + return ! empty( $users_sites ) ? $users_sites : array(); | |
| 383 | + } | |
| 384 | + | |
| 385 | + /** | |
| 386 | + * Method mainwp_help_content() | |
| 387 | + * | |
| 388 | + * Creates the MainWP Help Documentation List for the help component in the sidebar. | |
| 389 | + */ | |
| 390 | + public static function mainwp_help_content() { | |
| 391 | + $allow_pages = array( 'InsightsOverview' ); | |
| 392 | + if ( isset( $_GET['page'] ) && in_array( $_GET['page'], $allow_pages, true ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 393 | + ?> | |
| 394 | + <p><?php esc_html_e( 'If you need help with the Dashboard Insights module, please review following help documents', 'mainwp' ); ?></p> | |
| 395 | + <div class="ui list"> | |
| 396 | + <div class="item"><i class="external alternate icon"></i> <a href="https://docs.mainwp.com/add-ons/security/dashboard-insights-extension#dashboard-insights" target="_blank">Dashboard Insights</a></div> <?php // NOSONAR -- compatible with help. ?> | |
| 397 | + <div class="item"><i class="external alternate icon"></i> <a href="https://docs.mainwp.com/add-ons/security/dashboard-insights-extension#comprehensive-filtering-options" target="_blank">Filtering Options</a></div> <?php // NOSONAR -- compatible with help. ?> | |
| 398 | + <div class="item"><i class="external alternate icon"></i> <a href="https://docs.mainwp.com/add-ons/security/dashboard-insights-extension#export-the-data-and-charts-from-an-individual-widget" target="_blank">Export Insights Data</a></div> <?php // NOSONAR -- compatible with help. ?> | |
| 399 | + <?php | |
| 400 | + /** | |
| 401 | + * Action: mainwp_module_dashboard_insights_help_item | |
| 402 | + * | |
| 403 | + * Fires at the bottom of the help articles list in the Help sidebar on the Insights page. | |
| 404 | + * | |
| 405 | + * Suggested HTML markup: | |
| 406 | + * | |
| 407 | + * <div class="item"><a href="Your custom URL">Your custom text</a></div> | |
| 408 | + * | |
| 409 | + * @since 5.2 | |
| 410 | + */ | |
| 411 | + do_action( 'mainwp_module_dashboard_insights_help_item' ); | |
| 412 | + ?> | |
| 413 | + </div> | |
| 414 | + <?php | |
| 415 | + } | |
| 416 | + } | |
| 350 | 417 | } |