true, 'log_plugins' => true, 'log_themes' => true, 'log_sites' => true, 'log_posts' => true, 'log_pages' => true, 'log_users' => true, 'log_clients' => true, 'log_graph_status' => true, 'log_graph_php' => true, 'log_graph_wp' => true, 'log_graph_themes' => true, 'log_graph_plugins' => true, 'log_graph_tags' => true, 'log_graph_clients' => true, ); /** * Current page. * * @static * @var string $page Current page. */ public static $page; /** * Check if there is a session, * if there isn't one create it. * * @return static::singlton Overview Page Session. * * @uses \MainWP\Dashboard\MainWP_Overview */ public static function instance() { if ( null === static::$instance ) { static::$instance = new self(); } return static::$instance; } /** * MainWP_Overview constructor. * * Run each time the class is called. */ public function __construct() { add_action( 'admin_init', array( $this, 'admin_init' ) ); } /** * Hook admin init. */ public function admin_init() { $this->handle_update_screen_options(); } /** * Handle update screen options. */ public function handle_update_screen_options() { //phpcs:ignore -- NOSONAR - complex. $update_opts = false; if ( isset( $_POST['module_log_overview_options_nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['module_log_overview_options_nonce'] ), 'module_log_overview_options_nonce' ) ) { $update_opts = true; } if ( $update_opts ) { $show_wids = array(); if ( isset( $_POST['mainwp_show_widgets'] ) && is_array( $_POST['mainwp_show_widgets'] ) ) { $selected_wids = array_map( 'sanitize_text_field', wp_unslash( $_POST['mainwp_show_widgets'] ) ); foreach ( $selected_wids as $name ) { $show_wids[ $name ] = 1; } } if ( isset( $_POST['mainwp_widgets_name'] ) && is_array( $_POST['mainwp_widgets_name'] ) ) { $name_wids = array_map( 'sanitize_text_field', wp_unslash( $_POST['mainwp_widgets_name'] ) ); foreach ( $name_wids as $name ) { if ( ! isset( $show_wids[ $name ] ) ) { $show_wids[ $name ] = 0; } } } global $current_user; if ( $current_user ) { update_user_option( $current_user->ID, 'mainwp_module_log_overview_show_widgets', $show_wids, true ); if ( isset( $_POST['reset_module_log_overview_widgets_settings'] ) && ! empty( $_POST['reset_module_log_overview_widgets_settings'] ) ) { update_user_option( $current_user->ID, 'mainwp_module_log_overview_show_widgets', false, true ); update_user_option( $current_user->ID, 'mainwp_widgets_sorted_' . strtolower( 'mainwp_page_InsightsOverview' ), false, true ); } } } } /** * Method on_load_page() * * Run on page load. * * @param mixed $page Page name. */ public function on_load_page( $page ) { static::$page = $page; $val = get_user_option( 'screen_layout_' . $page ); if ( ! $val ) { global $current_user; update_user_option( $current_user->ID, 'screen_layout_' . $page, 2, true ); } wp_enqueue_script( 'common' ); wp_enqueue_script( 'wp-lists' ); wp_enqueue_script( 'postbox' ); wp_enqueue_script( 'dashboard' ); wp_enqueue_script( 'widgets' ); static::add_meta_boxes( $page ); add_filter( 'mainwp_header_actions_right', array( $this, 'screen_options' ), 10, 2 ); add_filter( 'mainwp_widget_boxes_show_widgets', array( $this, 'hook_show_widgets' ), 10, 2 ); add_filter( 'mainwp_page_admin_body_class', array( $this, 'hook_admin_body_class' ), 10, 1 ); } /** * Method screen_options() * * Create Page Settings button. * * @param mixed $input Page Settings button HTML. * * @return mixed Page Settings button. */ public function screen_options( $input ) { return $input . ' '; } /** * Method hook_show_widgets() * * Hook show widgets. * * @param array $values Show widgets settings. * @param string $page Page slug. * * @return array $values Show widgets settings. */ public function hook_show_widgets( $values, $page ) { if ( strtolower( $page ) === strtolower( static::$page ) ) { return get_user_option( 'mainwp_module_log_overview_show_widgets' ); } return $values; } /** * Method hook_admin_body_class() * * @param string $body_classes Body classes. * @return string $body_classes Body classes. */ public function hook_admin_body_class( $body_classes ) { return $body_classes . ' mainwp-hidden-second-level-navigation '; } /** * Initiates Insights MainWP menu. */ public static function init_left_menu() { MainWP_Menu::add_left_menu( array( 'title' => esc_html__( 'Insights', 'mainwp' ), 'parent_key' => 'mainwp_tab', 'slug' => 'InsightsOverview', 'href' => 'admin.php?page=InsightsOverview', 'icon' => '', 'desc' => 'Dashboard Insights Overview', 'nosubmenu' => true, ), 0 ); } /** * Method add_meta_boxes() * * Add MainWP Overview Page Widgets. * * @param array $page Current page. * * @uses \MainWP\Dashboard\MainWP_System_Handler::apply_filters() * @uses \MainWP\Dashboard\MainWP_System_Utility::get_current_wpid() * @uses \MainWP\Dashboard\MainWP_UI::add_widget_box() * @uses \MainWP\Dashboard\MainWP_Connection_Status::get_class_name() * @uses \MainWP\Dashboard\MainWP_Recent_Pages::get_class_name() * @uses \MainWP\Dashboard\MainWP_Recent_Posts::get_class_name() * @uses \MainWP\Dashboard\MainWP_Security_Issues_Widget::get_class_name() */ public static function add_meta_boxes( $page ) { //phpcs:ignore -- NOSONAR - complex method. /** * Get getmetaboxes * * Adds metaboxes (widgets) to the Overview page. * * @since 4.6 */ $extMetaBoxs = array(); $extMetaBoxs = apply_filters( 'mainwp_insights_getmetaboxes', $extMetaBoxs ); foreach ( $extMetaBoxs as $box ) { if ( isset( $box['plugin'] ) ) { $name = basename( $box['plugin'], '.php' ); static::$enable_widgets[ $name ] = true; } elseif ( ! empty( $box['widget_id'] ) ) { static::$enable_widgets[ $box['widget_id'] ] = true; } } /** * Unset unwanted Widgets * * Contains the list of enabled widgets and allows user to unset unwanted widgets. * * @param array $enable_widgets Array containing enabled widgets. * @param int $dashboard_siteid Child site (Overview) ID. * * @since 4.6 */ $values = apply_filters( 'mainwp_module_log_overview_enabled_widgets', static::$enable_widgets, null ); static::$enable_widgets = array_merge( static::$enable_widgets, $values ); // Load the widget. if ( ! empty( static::$enable_widgets['log_sites'] ) ) { MainWP_UI::add_widget_box( 'log_sites', array( Log_Sites_Widget::instance(), 'render' ), $page, array( -1, -1, 6, 40 ) ); } // Load the widget. if ( ! empty( static::$enable_widgets['log_clients'] ) ) { MainWP_UI::add_widget_box( 'log_clients', array( Log_Clients_Widget::instance(), 'render' ), $page, array( -1, -1, 6, 40 ) ); } // Load the widget. if ( ! empty( static::$enable_widgets['log_pages'] ) ) { MainWP_UI::add_widget_box( 'log_pages', array( Log_Pages_Widget::instance(), 'render' ), $page, array( -1, -1, 6, 40 ) ); } // Load the widget. if ( ! empty( static::$enable_widgets['log_posts'] ) ) { MainWP_UI::add_widget_box( 'log_posts', array( Log_Posts_Widget::instance(), 'render' ), $page, array( -1, -1, 6, 40 ) ); } // Load the widget. if ( ! empty( static::$enable_widgets['log_themes'] ) ) { MainWP_UI::add_widget_box( 'log_themes', array( Log_Themes_Widget::instance(), 'render' ), $page, array( -1, -1, 6, 40 ) ); } // Load the widget. if ( ! empty( static::$enable_widgets['log_plugins'] ) ) { MainWP_UI::add_widget_box( 'log_plugins', array( Log_Plugins_Widget::instance(), 'render' ), $page, array( -1, -1, 6, 40 ) ); } // Load the widget. if ( ! empty( static::$enable_widgets['log_graph_clients'] ) ) { MainWP_UI::add_widget_box( 'log_graph_clients', array( Log_Graph_Clients_Widget::instance(), 'render' ), $page, array( -1, -1, 6, 40 ) ); } // Load the widget. if ( ! empty( static::$enable_widgets['log_graph_tags'] ) ) { MainWP_UI::add_widget_box( 'log_graph_tags', array( Log_Graph_Tags_Widget::instance(), 'render' ), $page, array( -1, -1, 6, 40 ) ); } // Load the widget. if ( ! empty( static::$enable_widgets['log_graph_status'] ) ) { MainWP_UI::add_widget_box( 'log_graph_status', array( Log_Graph_Status_Widget::instance(), 'render' ), $page, array( -1, -1, 6, 40 ) ); } // Load the widget. if ( ! empty( static::$enable_widgets['log_graph_themes'] ) ) { MainWP_UI::add_widget_box( 'log_graph_themes', array( Log_Graph_Themes_Widget::instance(), 'render' ), $page, array( -1, -1, 6, 40 ) ); } // Load the widget. if ( ! empty( static::$enable_widgets['log_graph_wp'] ) ) { MainWP_UI::add_widget_box( 'log_graph_wp', array( Log_Graph_WP_Widget::instance(), 'render' ), $page, array( -1, -1, 6, 40 ) ); } // Load the widget. if ( ! empty( static::$enable_widgets['log_graph_php'] ) ) { MainWP_UI::add_widget_box( 'log_graph_php', array( Log_Graph_Php_Widget::instance(), 'render' ), $page, array( -1, -1, 6, 40 ) ); } // Load the widget. if ( ! empty( static::$enable_widgets['log_graph_plugins'] ) ) { MainWP_UI::add_widget_box( 'log_graph_plugins', array( Log_Graph_Plugins_Widget::instance(), 'render' ), $page, array( -1, -1, 6, 40 ) ); } // Load the widget. if ( ! empty( static::$enable_widgets['log_users'] ) ) { MainWP_UI::add_widget_box( 'log_users', array( Log_Users_Widget::instance(), 'render' ), $page, array( -1, -1, 6, 40 ) ); } // Load the widget. if ( ! empty( static::$enable_widgets['recent_events'] ) ) { MainWP_UI::add_widget_box( 'recent_events', array( Log_Recent_Events_Widget::instance(), 'render' ), $page, array( -1, -1, 6, 40 ) ); } $i = 1; foreach ( $extMetaBoxs as $metaBox ) { $enabled = true; if ( isset( $metaBox['plugin'] ) ) { $name = basename( $metaBox['plugin'], '.php' ); if ( isset( static::$enable_widgets[ $name ] ) && ! static::$enable_widgets[ $name ] ) { $enabled = false; } } $id = isset( $metaBox['id'] ) ? $metaBox['id'] : $i++; $id = 'advanced-' . $id; $layout = ! empty( $metaBox['layout'] ) && is_array( $metaBox['layout'] ) ? $metaBox['layout'] : array( -1, -1, 6, 40 ); if ( $enabled ) { MainWP_UI::add_widget_box( $id, $metaBox['callback'], $page, $layout ); } } } /** * 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() ), ); } /** * Method render_footer() * * Render Insights page footer. Closes the page container. */ public static function render_footer() { echo ''; } /** * Renders manage insights dashboard. * * @return void */ public function render_insights_overview() { if ( ! \mainwp_current_user_can( 'dashboard', 'access_insights_dashboard' ) ) { \mainwp_do_not_have_permissions( esc_html__( 'insights dashboard', 'mainwp' ) ); return; } $this->on_show_page(); } /** * 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 list in overview page. $this->list_events_table->prepare_items( true, $insights_filters ); $items = $this->list_events_table->items; $items_prev = ! empty( $this->list_events_table->items_prev ) ? $this->list_events_table->items_prev : array(); static::render_dashboard_body( $items, $items_prev, $insights_filters ); } /** * 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' ); $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 = ''; $array_clients_ids = array(); $array_groups_ids = array(); $array_users_ids = array(); $update_filter = false; if ( $get_saved ) { $filters_saved = get_user_option( 'mainwp_module_logs_overview_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'] : ''; } 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'] ) ) : ''; // 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_users_ids = explode( ',', $filter_user_ids ); // convert to array. if ( in_array( 'allusers', $array_users_ids, true ) ) { $array_users_ids = false; $filter_user_ids = ''; } else { $array_users_ids = MainWP_Utility::array_numeric_filter( $array_users_ids ); $filter_user_ids = implode( ',', $array_users_ids ); } } if ( $save_filter && $update_filter ) { MainWP_Utility::update_user_option( 'mainwp_module_logs_overview_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, ) ); } return compact( 'filter_ranges', 'filter_groups_ids', 'filter_client_ids', 'filter_user_ids', 'filter_dtsstart', 'filter_dtsstop', 'array_clients_ids', 'array_groups_ids', 'array_users_ids', ); } /** * 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_users_ids = 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_user_ids ) && empty( $filter_dtsstart ) && empty( $filter_dtsstop ) && empty( $array_clients_ids ) && empty( $array_groups_ids ) && empty( $array_users_ids ) ) { $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(); } static::render_layout_selection(); ?>