*/ private $users_by_id = array(); /** * Activity type labels cache. * * @var array */ private $types = array(); /** * Constructor. */ public function __construct() { parent::__construct( array( 'singular' => 'activity', 'plural' => 'activities', 'ajax' => false, ) ); $this->types = Activitysignature_Helpers::get_activity_types(); } /** * Render activity log tab content. */ public static function render_page() { $table = new Activitysignature_Activity_Table(); $table->prepare_items(); self::render_delete_notice(); $activity_type = isset( $_GET['activity_type'] ) ? sanitize_key( wp_unslash( $_GET['activity_type'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $date_from = isset( $_GET['date_from'] ) ? sanitize_text_field( wp_unslash( $_GET['date_from'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $date_to = isset( $_GET['date_to'] ) ? sanitize_text_field( wp_unslash( $_GET['date_to'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $search_term = isset( $_GET['s'] ) ? sanitize_text_field( wp_unslash( $_GET['s'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $types = Activitysignature_Helpers::get_activity_types(); ?>

display(); ?>
get_columns(); $sortable = $this->get_sortable_columns(); $this->_column_headers = array( $columns, array(), $sortable ); $settings = Activitysignature_Helpers::get_settings(); $per_page = max( 5, (int) $settings['items_per_page'] ); $current_page = $this->get_pagenum(); $offset = ( $current_page - 1 ) * $per_page; $result = $this->query_activities( $per_page, $offset ); $this->items = $result['items']; $user_ids = array(); foreach ( $this->items as &$item ) { $item['_decoded'] = json_decode( $item['activity_data'], true ); if ( ! is_array( $item['_decoded'] ) ) { $item['_decoded'] = array(); } $uid = (int) $item['user_id']; if ( $uid > 0 ) { $user_ids[] = $uid; } $actor_id = isset( $item['actor_user_id'] ) ? (int) $item['actor_user_id'] : 0; if ( $actor_id > 0 ) { $user_ids[] = $actor_id; } } unset( $item ); $user_ids = array_unique( $user_ids ); if ( ! empty( $user_ids ) ) { $users = get_users( array( 'include' => $user_ids, 'fields' => array( 'ID', 'user_login' ), ) ); foreach ( $users as $user ) { $this->users_by_id[ (int) $user->ID ] = $user; } } $this->set_pagination_args( array( 'total_items' => $result['total'], 'per_page' => $per_page, 'total_pages' => (int) ceil( $result['total'] / max( 1, $per_page ) ), ) ); } /** * Delete selected activity rows before the screen renders, then redirect. */ public static function handle_bulk_actions() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Routing only; process_bulk_action() checks the nonce. $tab = isset( $_REQUEST['tab'] ) ? sanitize_key( wp_unslash( $_REQUEST['tab'] ) ) : ''; if ( 'activity' !== $tab || ! current_user_can( 'manage_options' ) ) { return; } $table = new self(); $table->process_bulk_action(); } /** * Notice after a redirected activity-row delete. */ private static function render_delete_notice() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only result of a redirect. if ( ! isset( $_GET['activitysignature_rows_deleted'] ) ) { return; } $deleted = absint( wp_unslash( $_GET['activitysignature_rows_deleted'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( $deleted > 0 ) { $message = sprintf( /* translators: %d: number of deleted activity rows */ _n( '%d activity record deleted.', '%d activity records deleted.', $deleted, 'activitysignature' ), $deleted ); } else { $message = __( 'No activity records were deleted.', 'activitysignature' ); } $class = $deleted > 0 ? 'notice-success' : 'notice-info'; echo '

' . esc_html( $message ) . '

'; } /** Delete selected activity records. */ private function process_bulk_action() { if ( 'delete' !== $this->current_action() || empty( $_POST['activity_ids'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing return; } check_admin_referer( 'activitysignature_activity_bulk', 'activitysignature_activity_nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have permission to perform this action.', 'activitysignature' ), '', array( 'response' => 403 ) ); } $ids = array_values( array_filter( array_map( 'absint', (array) wp_unslash( $_POST['activity_ids'] ) ) ) ); $deleted = 0; if ( $ids ) { $deleted = Activitysignature_Helpers::delete_rows_by_ids( $ids ); if ( false === $deleted ) { $deleted = 0; } } $this->redirect_after_bulk_delete( (int) $deleted ); } /** * Return to the activity log without replaying the delete POST. * * @param int $deleted Rows deleted. */ private function redirect_after_bulk_delete( $deleted ) { $args = array( 'activitysignature_rows_deleted' => max( 0, (int) $deleted ), ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Filter values from the current list URL. $activity_type = isset( $_GET['activity_type'] ) ? sanitize_key( wp_unslash( $_GET['activity_type'] ) ) : ''; if ( $activity_type && isset( Activitysignature_Helpers::get_activity_types()[ $activity_type ] ) ) { $args['activity_type'] = $activity_type; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended $date_from = isset( $_GET['date_from'] ) ? sanitize_text_field( wp_unslash( $_GET['date_from'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $date_to = isset( $_GET['date_to'] ) ? sanitize_text_field( wp_unslash( $_GET['date_to'] ) ) : ''; if ( $date_from && Activitysignature_Helpers::is_valid_date( $date_from ) ) { $args['date_from'] = $date_from; } if ( $date_to && Activitysignature_Helpers::is_valid_date( $date_to ) ) { $args['date_to'] = $date_to; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended $search = isset( $_GET['s'] ) ? sanitize_text_field( wp_unslash( $_GET['s'] ) ) : ''; if ( '' !== $search ) { $args['s'] = $search; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended $orderby = isset( $_GET['orderby'] ) ? sanitize_key( wp_unslash( $_GET['orderby'] ) ) : ''; if ( in_array( $orderby, array( 'activity_date', 'activity_type' ), true ) ) { $args['orderby'] = $orderby; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended $order = isset( $_GET['order'] ) ? strtolower( sanitize_key( wp_unslash( $_GET['order'] ) ) ) : ''; if ( in_array( $order, array( 'asc', 'desc' ), true ) ) { $args['order'] = $order; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended $paged = isset( $_GET['paged'] ) ? absint( wp_unslash( $_GET['paged'] ) ) : 0; if ( $paged > 1 ) { $args['paged'] = $paged; } wp_safe_redirect( add_query_arg( $args, Activitysignature_Helpers::get_admin_url( 'activity' ) ) ); exit; } /** * Columns. * * @return array */ public function get_columns() { return array( 'cb' => '', 'activity_date' => __( 'Date', 'activitysignature' ), 'user' => __( 'User', 'activitysignature' ), 'actor' => __( 'Performed by', 'activitysignature' ), 'activity_type' => __( 'Activity', 'activitysignature' ), 'details' => __( 'Details', 'activitysignature' ), ); } /** * Sortable columns. * * @return array */ public function get_sortable_columns() { return array( 'activity_date' => array( 'activity_date', true ), 'activity_type' => array( 'activity_type', false ), ); } /** * Query activity rows with filters. * * @param int $per_page Per page. * @param int $offset Offset. * * @return array{items: array, total: int} */ private function query_activities( $per_page, $offset ) { global $wpdb; $table = Activitysignature_Helpers::get_table_name(); $activity_type = isset( $_GET['activity_type'] ) ? sanitize_key( wp_unslash( $_GET['activity_type'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $date_from = isset( $_GET['date_from'] ) ? sanitize_text_field( wp_unslash( $_GET['date_from'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $date_to = isset( $_GET['date_to'] ) ? sanitize_text_field( wp_unslash( $_GET['date_to'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $search_term = isset( $_GET['s'] ) ? sanitize_text_field( wp_unslash( $_GET['s'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $allowed_types = array_keys( $this->types ); $has_type = ( $activity_type && in_array( $activity_type, $allowed_types, true ) ) ? 1 : 0; $has_from = ( $date_from && Activitysignature_Helpers::is_valid_date( $date_from ) && '' !== $date_from ) ? 1 : 0; $has_to = ( $date_to && Activitysignature_Helpers::is_valid_date( $date_to ) && '' !== $date_to ) ? 1 : 0; $has_search = $search_term ? 1 : 0; $from_sql = $has_from ? $date_from . ' 00:00:00' : ''; $to_sql = $has_to ? $date_to . ' 23:59:59' : ''; $like = $has_search ? '%' . $wpdb->esc_like( $search_term ) . '%' : ''; $orderby = isset( $_GET['orderby'] ) ? sanitize_key( wp_unslash( $_GET['orderby'] ) ) : 'activity_date'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $order = isset( $_GET['order'] ) ? sanitize_key( wp_unslash( $_GET['order'] ) ) : 'desc'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( ! in_array( $orderby, array( 'activity_date', 'activity_type' ), true ) ) { $orderby = 'activity_date'; } $order_asc = ( 'asc' === strtolower( $order ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table list table; request-scoped filters. $total = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE ( %d = 0 OR activity_type = %s ) AND ( %d = 0 OR activity_date >= %s ) AND ( %d = 0 OR activity_date <= %s ) AND ( %d = 0 OR user_login LIKE %s OR user_email LIKE %s OR ip_address LIKE %s )', $table, $has_type, $activity_type, $has_from, $from_sql, $has_to, $to_sql, $has_search, $like, $like, $like ) ); // ASC/DESC are literals (not placeholders) so the SQL string stays static for Plugin Check. if ( $order_asc ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table list table; request-scoped filters. $items = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE ( %d = 0 OR activity_type = %s ) AND ( %d = 0 OR activity_date >= %s ) AND ( %d = 0 OR activity_date <= %s ) AND ( %d = 0 OR user_login LIKE %s OR user_email LIKE %s OR ip_address LIKE %s ) ORDER BY %i ASC LIMIT %d OFFSET %d', $table, $has_type, $activity_type, $has_from, $from_sql, $has_to, $to_sql, $has_search, $like, $like, $like, $orderby, $per_page, $offset ), ARRAY_A ); } else { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table list table; request-scoped filters. $items = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE ( %d = 0 OR activity_type = %s ) AND ( %d = 0 OR activity_date >= %s ) AND ( %d = 0 OR activity_date <= %s ) AND ( %d = 0 OR user_login LIKE %s OR user_email LIKE %s OR ip_address LIKE %s ) ORDER BY %i DESC LIMIT %d OFFSET %d', $table, $has_type, $activity_type, $has_from, $from_sql, $has_to, $to_sql, $has_search, $like, $like, $like, $orderby, $per_page, $offset ), ARRAY_A ); } return array( 'items' => is_array( $items ) ? $items : array(), 'total' => $total, ); } /** Render the most frequent recent failed-login targets. */ private static function render_failed_login_summary() { global $wpdb; $table = Activitysignature_Helpers::get_table_name(); $since = ( new DateTimeImmutable( 'now', wp_timezone() ) )->modify( '-24 hours' )->format( 'Y-m-d H:i:s' ); $cache_key = 'activitysignature_failed_login_summary_' . get_current_blog_id(); $rows = wp_cache_get( $cache_key, Activitysignature_Helpers::CACHE_GROUP ); if ( false === $rows ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- Custom table; result cached via wp_cache_* below. $rows = $wpdb->get_results( $wpdb->prepare( 'SELECT user_login, user_email, ip_address, COUNT(*) AS attempts, MAX(activity_date) AS latest FROM %i WHERE activity_type = %s AND activity_date >= %s GROUP BY user_login, user_email, ip_address ORDER BY attempts DESC LIMIT 5', $table, 'user_login_failed', $since ), ARRAY_A ); wp_cache_set( $cache_key, is_array( $rows ) ? $rows : array(), Activitysignature_Helpers::CACHE_GROUP, 5 * MINUTE_IN_SECONDS ); } if ( ! $rows ) { return; } ?>
'; } /** * Column output. * * @param array $item Row. * @param string $column_name Column. * * @return string */ public function column_default( $item, $column_name ) { $data = isset( $item['_decoded'] ) && is_array( $item['_decoded'] ) ? $item['_decoded'] : array(); switch ( $column_name ) { case 'activity_date': return esc_html( Activitysignature_Helpers::format_datetime( $item['activity_date'] ) ); case 'user': $user_login = ! empty( $item['user_login'] ) ? $item['user_login'] : ( isset( $data['user_login'] ) ? $data['user_login'] : '' ); $user_id = (int) $item['user_id']; if ( $user_id > 0 && isset( $this->users_by_id[ $user_id ] ) ) { $user = $this->users_by_id[ $user_id ]; return '' . esc_html( $user->user_login ) . ''; } return $user_login ? esc_html( $user_login ) : '—'; case 'activity_type': $type = $item['activity_type']; $label = isset( $this->types[ $type ] ) ? $this->types[ $type ] : $type; return esc_html( $label ); case 'actor': $actor_id = (int) $item['actor_user_id']; return $actor_id && isset( $this->users_by_id[ $actor_id ] ) ? esc_html( $this->users_by_id[ $actor_id ]->user_login ) : esc_html__( 'System', 'activitysignature' ); case 'details': $summary = $this->format_details( $item['activity_type'], $data ); $rows = ''; foreach ( $data as $key => $value ) { $rows .= '
' . esc_html( ucwords( str_replace( '_', ' ', $key ) ) ) . '
' . esc_html( is_array( $value ) ? implode( ', ', $value ) : (string) $value ) . '
'; } return '
' . esc_html( $summary ) . '
' . $rows . '
'; default: return '—'; } } /** * Human-readable details string. * * @param string $type Activity type. * @param array $data Decoded data. * * @return string */ private function format_details( $type, $data ) { switch ( $type ) { case 'user_login': case 'user_login_failed': $ip = isset( $data['ip_address'] ) ? $data['ip_address'] : ''; return $ip ? sprintf( /* translators: %s: IP address */ __( 'IP: %s', 'activitysignature' ), $ip ) : '—'; case 'user_role_changed': $new = isset( $data['new_role'] ) ? $data['new_role'] : ''; $old = isset( $data['old_roles'] ) && is_array( $data['old_roles'] ) ? implode( ', ', $data['old_roles'] ) : ''; return sprintf( /* translators: 1: old roles, 2: new role */ __( '%1$s → %2$s', 'activitysignature' ), $old ? $old : '—', $new ? $new : '—' ); case 'user_updated': case 'user_created': case 'user_deleted': $email = isset( $data['user_email'] ) ? $data['user_email'] : ''; return $email ? $email : '—'; default: return '—'; } } /** * Empty state. */ public function no_items() { esc_html_e( 'No activity found.', 'activitysignature' ); } /** @return array */ protected function get_bulk_actions() { return array( 'delete' => __( 'Delete', 'activitysignature' ) ); } }