add_action( 'mainwp_insight_events_dismiss_actions', array( &$this, 'ajax_sites_changes_dismiss_selected' ) ); MainWP_Post_Handler::instance()->add_action( 'mainwp_insight_events_dismiss_all', array( &$this, 'ajax_sites_changes_dismiss_all' ) ); } /** * Method get_class_name() * * @return string __CLASS__ Class name. */ public static function get_class_name() { return __CLASS__; } /** * Method init_menu() * * Add Insights Overview sub menu "Insights". */ public function init_menu() { static::$page_current = add_submenu_page( 'mainwp_tab', esc_html__( 'Sites Changes', 'mainwp' ), '
' . esc_html__( 'Sites Changes', 'mainwp' ) . '
', 'read', 'InsightsManage', array( $this, 'render_insights_actions', ) ); add_action( 'load-' . static::$page_current, array( &$this, 'on_load_page' ) ); static::init_left_menu(); } /** * Initiates left menu. */ public static function init_left_menu() { MainWP_Menu::add_left_menu( array( 'title' => esc_html__( 'Sites Changes', 'mainwp' ), 'parent_key' => 'managesites', 'slug' => 'InsightsManage', 'href' => 'admin.php?page=InsightsManage', 'icon' => '', 'desc' => 'Sites Changes', 'leftsub_order_level2' => 3.4, ), 2 ); } /** * Renders manage insights. * * @return void */ public function render_insights_actions() { if ( ! \mainwp_current_user_can( 'dashboard', 'manage_insights_actions' ) ) { \mainwp_do_not_have_permissions( esc_html__( 'manage insights actions', 'mainwp' ) ); return; } $this->on_show_page(); } /** * Method on_load_page() * * Run on page load. * * @uses \MainWP\Dashboard\MainWP_Manage_Sites_List_Table * @uses \MainWP\Dashboard\MainWP_System::enqueue_postbox_scripts() */ public function on_load_page() { add_filter( 'mainwp_header_actions_right', array( &$this, 'add_screen_options' ), 10, 2 ); } /** * Method add_screen_options() * * Create Page Settings button. * * @param mixed $input Page Settings button HTML. * * @return mixed Page Settings button. */ public function add_screen_options( $input ) { return $input . ' '; } /** * Method on_show_page() * * When the page loads render the body content. */ public function on_show_page() { static::render_header( 'overview' ); $insights_filters = $this->get_insights_filters( true ); static::render_logs_overview_top( $insights_filters ); $this->load_events_list_table(); // for events table list. /** * Action: mainwp_logs_manage_table_top * * Fires at the top of the widget. * * @since 5.4 */ do_action( 'mainwp_logs_manage_table_top', 'recent_events' ); ?> list_events_table->display(); /** * Action: mainwp_logs_widget_bottom * * Fires at the bottom of the widget. * * @since 5.4 */ do_action( 'mainwp_logs_manage_table_bottom', 'recent_events' ); ?> render_screen_options(); } /** * Method get_insights_filters() * * Get insights filters. * * @param bool $save_filter To save filter. */ public function get_insights_filters( $save_filter = false ) { //phpcs:ignore -- NOSONAR - complex method. $filters = array( 'client', 'range', 'group', 'user', 'dtsstart', 'dtsstop', 'source', 'sites', 'events' ); $get_saved = true; foreach ( $filters as $filter ) { if ( isset( $_REQUEST[ $filter ] ) ) { //phpcs:ignore -- safe. $get_saved = false; break; } } $filter_ranges = ''; $filter_groups_ids = ''; $filter_client_ids = ''; $filter_user_ids = ''; $filter_dtsstart = ''; $filter_dtsstop = ''; $filter_source = ''; $filter_sites = ''; $filter_events = ''; $array_clients_ids = array(); $array_groups_ids = array(); $array_usersfilter_sites_ids = array(); $array_sites_ids = array(); $array_events_list = array(); $array_source_list = array(); $update_filter = false; if ( $get_saved ) { $filters_saved = get_user_option( 'mainwp_module_logs_manage_filters_saved' ); if ( ! is_array( $filters_saved ) ) { $filters_saved = static::get_default_filters(); } $filter_ranges = isset( $filters_saved['ranges'] ) && ! empty( $filters_saved['ranges'] ) ? $filters_saved['ranges'] : false; $filter_groups_ids = isset( $filters_saved['groups_ids'] ) && ! empty( $filters_saved['groups_ids'] ) ? $filters_saved['groups_ids'] : ''; $filter_client_ids = isset( $filters_saved['client_ids'] ) && ! empty( $filters_saved['client_ids'] ) ? $filters_saved['client_ids'] : false; $filter_user_ids = isset( $filters_saved['user_ids'] ) && ! empty( $filters_saved['user_ids'] ) ? $filters_saved['user_ids'] : ''; $filter_dtsstart = isset( $filters_saved['dtsstart'] ) && ! empty( $filters_saved['dtsstart'] ) ? $filters_saved['dtsstart'] : ''; $filter_dtsstop = isset( $filters_saved['dtsstop'] ) && ! empty( $filters_saved['dtsstop'] ) ? $filters_saved['dtsstop'] : ''; $filter_source = isset( $filters_saved['source'] ) && ! empty( $filters_saved['source'] ) ? $filters_saved['source'] : ''; $filter_sites = isset( $filters_saved['sites'] ) && ! empty( $filters_saved['sites'] ) ? $filters_saved['sites'] : ''; $filter_events = isset( $filters_saved['events'] ) && ! empty( $filters_saved['events'] ) ? $filters_saved['events'] : ''; } else { // phpcs:disable WordPress.Security.NonceVerification $filter_ranges = isset( $_REQUEST['range'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['range'] ) ) : ''; $filter_groups_ids = isset( $_REQUEST['group'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['group'] ) ) : ''; $filter_client_ids = isset( $_REQUEST['client'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['client'] ) ) : ''; $filter_user_ids = isset( $_REQUEST['user'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['user'] ) ) : ''; $filter_dtsstart = isset( $_REQUEST['dtsstart'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['dtsstart'] ) ) : ''; $filter_dtsstop = isset( $_REQUEST['dtsstop'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['dtsstop'] ) ) : ''; $filter_source = isset( $_REQUEST['source'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['source'] ) ) : ''; $filter_sites = isset( $_REQUEST['sites'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['sites'] ) ) : ''; $filter_events = isset( $_REQUEST['events'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['events'] ) ) : ''; // phpcs:enable WordPress.Security.NonceVerification $update_filter = true; } // validate requests. if ( ! empty( $filter_client_ids ) ) { $array_clients_ids = explode( ',', $filter_client_ids ); // convert to array. if ( in_array( 'allclients', $array_clients_ids, true ) ) { $array_clients_ids = false; $filter_client_ids = ''; } else { $array_clients_ids = MainWP_Utility::array_numeric_filter( $array_clients_ids ); $filter_client_ids = implode( ',', $array_clients_ids ); } } if ( ! empty( $filter_groups_ids ) ) { $array_groups_ids = explode( ',', $filter_groups_ids ); // convert to array. if ( in_array( 'alltags', $array_groups_ids, true ) ) { $array_groups_ids = false; $filter_groups_ids = ''; } else { $array_groups_ids = MainWP_Utility::array_numeric_filter( $array_groups_ids ); $filter_groups_ids = implode( ',', $array_groups_ids ); } } if ( ! empty( $filter_user_ids ) ) { $array_usersfilter_sites_ids = explode( ',', $filter_user_ids ); // convert to array. if ( in_array( 'allusers', $array_usersfilter_sites_ids, true ) ) { $array_usersfilter_sites_ids = false; $filter_user_ids = ''; } else { // to valid and fix filters. $users_filters = array(); foreach ( $array_usersfilter_sites_ids as $users_filter ) { if ( 2 === substr_count( $users_filter, '-' ) ) { $users_filters[] = $users_filter; } } $array_usersfilter_sites_ids = $users_filters; $filter_user_ids = implode( ',', $users_filters ); } } $sources_conds = ''; if ( ! empty( $filter_source ) ) { $array_source_list = explode( ',', $filter_source ); // convert to array. if ( in_array( 'allsource', $array_source_list, true ) || ( in_array( 'dashboard', $array_source_list ) && in_array( 'wp-admin', $array_source_list ) ) ) { $filter_source = ''; $array_source_list = false; } if ( is_array( $array_source_list ) ) { $wpadmin_source = true; $dashboard_source = true; if ( ! in_array( 'wp-admin', $array_source_list ) ) { $wpadmin_source = false; } if ( ! in_array( 'dashboard', $array_source_list ) ) { $dashboard_source = false; } if ( $wpadmin_source && ! $dashboard_source ) { $sources_conds = 'wp-admin-only'; } elseif ( ! $wpadmin_source && $dashboard_source ) { $sources_conds = 'dashboard-only'; } } } if ( ! empty( $filter_sites ) ) { $array_sites_ids = explode( ',', $filter_sites ); // convert to array. if ( in_array( 'allsites', $array_sites_ids, true ) ) { $array_sites_ids = false; $filter_sites = ''; } else { $array_sites_ids = MainWP_Utility::array_numeric_filter( $array_sites_ids ); $filter_sites = implode( ',', $array_sites_ids ); } } if ( ! empty( $filter_events ) ) { $array_events_list = explode( ',', $filter_events ); // convert to array. if ( in_array( 'allevents', $array_events_list, true ) ) { $filter_events = ''; $array_events_list = false; } } if ( $save_filter && $update_filter ) { MainWP_Utility::update_user_option( 'mainwp_module_logs_manage_filters_saved', array( 'ranges' => $filter_ranges, 'groups_ids' => $filter_groups_ids, 'client_ids' => $filter_client_ids, 'user_ids' => $filter_user_ids, 'dtsstart' => $filter_dtsstart, 'dtsstop' => $filter_dtsstop, 'source' => $filter_source, 'sites' => $filter_sites, 'events' => $filter_events, ) ); } return compact( 'filter_ranges', 'filter_groups_ids', 'filter_client_ids', 'filter_user_ids', 'filter_dtsstart', 'filter_dtsstop', 'array_clients_ids', 'array_groups_ids', 'array_usersfilter_sites_ids', 'filter_source', 'filter_sites', 'filter_events', 'sources_conds', 'array_sites_ids', 'array_events_list', 'array_source_list', ); } /** * Method get default logs filters() */ public static function get_default_filters() { $format = 'Y-m-d'; return array( 'ranges' => 'thismonth', 'dtsstart' => gmdate( $format, strtotime( gmdate( 'Y-m-01' ) ) ), 'dtsstop' => gmdate( $format, time() ), ); } /** * Render Manage Tasks Table Top. * * @param array $insights_filters Insights filters. */ public static function render_logs_overview_top( $insights_filters ) { //phpcs:ignore -- NOSONAR - complex. $manager = Log_Manager::instance(); $filter_ranges = ''; $filter_groups_ids = ''; $filter_client_ids = ''; $filter_user_ids = ''; $filter_dtsstart = ''; $filter_dtsstop = ''; $array_clients_ids = array(); $array_groups_ids = array(); $array_usersfilter_sites_ids = array(); $filter_source = ''; $filter_sites = ''; $filter_events = ''; $array_source_list = array(); $array_sites_ids = array(); $array_events_list = array(); extract( $insights_filters ); //phpcs:ignore -- ok. $default_filter = false; // extracted values. if ( ( empty( $filter_ranges ) || 'thismonth' === $filter_ranges ) && empty( $filter_groups_ids ) && empty( $filter_client_ids ) && empty( $filter_user_ids ) && empty( $filter_dtsstart ) && empty( $filter_dtsstop ) && empty( $array_clients_ids ) && empty( $array_groups_ids ) && empty( $array_usersfilter_sites_ids ) && empty( $array_source_list ) && empty( $array_sites_ids ) && empty( $array_events_list ) ) { $default_filter = true; } $disable_dt = ( '' === $filter_ranges || 'custom' === $filter_ranges ) ? false : true; $groups = MainWP_DB_Common::instance()->get_groups_for_current_user(); if ( ! is_array( $groups ) ) { $groups = array(); } ?>
array( 'start' => gmdate( $format, strtotime( 'today' ) ), 'end' => gmdate( $format, strtotime( 'today' ) ), ), 'yesterday' => array( 'start' => gmdate( $format, strtotime( '-1 day', $time ) ), 'end' => gmdate( $format, strtotime( '-1 day', $time ) ), ), 'thisweek' => array( 'start' => gmdate( $format, strtotime( 'last monday', $time ) ), 'end' => gmdate( $format, $time ), ), 'thismonth' => array( 'start' => gmdate( $format, strtotime( gmdate( 'Y-m-01' ) ) ), 'end' => gmdate( $format, $time ), ), 'lastmonth' => array( 'start' => gmdate( $format, strtotime( 'first day of last month' ) ), 'end' => gmdate( $format, strtotime( 'first day of this month' ) - 1 ), ), 'thisyear' => array( 'start' => gmdate( $format, strtotime( 'first day of January ' . gmdate( 'Y' ) ) ), 'end' => gmdate( $format, $time ), ), 'lastyear' => array( 'start' => gmdate( $format, strtotime( 'first day of January ' . gmdate( 'Y' ) . '-1 year' ) ), 'end' => gmdate( $format, strtotime( 'last day of December ' . gmdate( 'Y' ) . '-1 year' ) ), ), 'custom' => array( 'start' => gmdate( $format, strtotime( 'last monday', $time ) ), 'end' => gmdate( $format, $time ), ), ); $ranges_values = wp_json_encode( $ranges_values ); ?> list_events_table = new Log_Events_List_Table( $manager, $this->table_id_prefix ); } /** * Method ajax_manage_events_display_rows() * * Display table rows, optimize for shared hosting or big networks. */ public function ajax_manage_events_display_rows() { MainWP_Post_Handler::instance()->check_security( 'mainwp_module_log_manage_events_display_rows' ); $this->load_events_list_table(); $insights_filters = $this->get_insights_filters(); // get ajax filters. $this->list_events_table->prepare_items( false, $insights_filters ); $output = $this->list_events_table->ajax_get_datatable_rows(); MainWP_Logger::instance()->log_execution_time( 'ajax_events_display_rows()' ); wp_send_json( $output ); } /** * Method render_header() * * Render page header. */ public static function render_header() { $params = array( 'title' => esc_html__( 'Sites Changes', 'mainwp' ), 'which' => 'page_log_manage_insights', 'wrap_class' => 'mainwp-log-mananage-insights-wrapper', ); MainWP_UI::render_top_header( $params ); } /** * Method ajax_sites_changes_dismiss_selected() */ public function ajax_sites_changes_dismiss_selected() { MainWP_Post_Handler::instance()->secure_request( 'mainwp_insight_events_dismiss_actions' ); // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $log_id = isset( $_POST['log_id'] ) ? intval( $_POST['log_id'] ) : 0; $log = false; if ( ! empty( $log_id ) ) { $log = Log_DB_Helper::instance()->get_log_by_id( $log_id ); } if ( empty( $log ) ) { wp_die( wp_json_encode( array( 'error' => 'Invalid change ID or Change not found.' ) ) ); } $update = array( 'log_id' => $log_id, 'dismiss' => 1, ); Log_DB_Helper::instance()->update_log( $update ); wp_die( wp_json_encode( array( 'success' => 'yes' ) ) ); } /** * Method ajax_sites_changes_dismiss_all() */ public function ajax_sites_changes_dismiss_all() { MainWP_Post_Handler::instance()->secure_request( 'mainwp_insight_events_dismiss_all' ); Log_DB_Helper::instance()->dismiss_all_changes(); wp_die( wp_json_encode( array( 'success' => 'yes' ) ) ); } /** * Method render_screen_options() * * Render Page Settings Modal. */ public function render_screen_options() { // phpcs:ignore -- NOSONAR -Current complexity is the only way to achieve desired results, pull request solutions appreciated. $columns = $this->list_events_table->get_columns(); if ( isset( $columns['cb'] ) ) { unset( $columns['cb'] ); } if ( isset( $columns['status'] ) ) { $columns['status'] = esc_html__( 'Status', 'mainwp' ); } $sites_per_page = get_option( 'mainwp_default_manage_insights_events_per_page', 25 ); if ( isset( $columns['col_action'] ) && empty( $columns['col_action'] ) ) { $columns['col_action'] = esc_html__( 'Actions', 'mainwp' ); } $show_cols = get_user_option( 'mainwp_settings_show_insights_events_columns' ); if ( false === $show_cols ) { // to backwards. $show_cols = array(); foreach ( $columns as $name => $title ) { $show_cols[ $name ] = 1; } $user = wp_get_current_user(); if ( $user ) { update_user_option( $user->ID, 'mainwp_settings_show_insights_events_columns', $show_cols, true ); } } if ( ! is_array( $show_cols ) ) { $show_cols = array(); } ?>