add_help_tab( array( 'id' => 'srah-overview', 'title' => __( 'Overview', 'admintrace-activity-logger' ), 'content' => '

' . esc_html__( 'AdminTrace Activity Logger lists important changes on this site. Use the filters to narrow the journal, open Details on any event, and export the current view if you are allowed to.', 'admintrace-activity-logger' ) . '

', ) ); $screen->add_help_tab( array( 'id' => 'srah-privacy', 'title' => __( 'Privacy', 'admintrace-activity-logger' ), 'content' => '

' . esc_html__( 'Passwords and API keys are not stored. IP addresses are masked. Editors, when allowed to view the journal, see content activity and not security events.', 'admintrace-activity-logger' ) . '

', ) ); } $filters = self::filters(); $args = Access::constrain( self::query_args( $filters ) ); $result = Query::search( $args ); $manage = current_user_can( Capabilities::MANAGE ); $scoped = ! $manage; $stats = Query::stats( $scoped, $scoped ? Vocabulary::viewer_categories() : array() ); $pages = max( 1, (int) ceil( $result['total'] / $filters['per_page'] ) ); $highlight = null; if ( $filters['event'] ) { $found = Query::search( Access::constrain( array( 'id' => $filters['event'], 'per_page' => 1, ) ) ); $highlight = isset( $found['rows'][0] ) ? Access::present( $found['rows'][0], ! empty( $args['include_ip'] ) ) : false; } echo '
'; Notices::render(); self::hero( $filters ); self::stats( $stats, $manage ); if ( false === $highlight ) { echo '

' . esc_html__( 'That event is not available.', 'admintrace-activity-logger' ) . '

'; } elseif ( is_array( $highlight ) ) { echo '
'; Renderer::card( $highlight, array( 'checkbox' => false ) ); echo '
'; } self::filter_form( $filters, $manage ); $rows = Groups::collapse( $result['rows'] ); $can_del = $rows && current_user_can( Capabilities::DELETE ); if ( $can_del ) { echo '
'; wp_nonce_field( 'srah_delete' ); echo ''; echo '
'; echo '
'; } if ( ! $rows ) { echo '

' . esc_html__( 'Nothing recorded yet', 'admintrace-activity-logger' ) . '

' . esc_html__( 'New changes will appear here. Existing posts and pages are noted once, when the plugin is activated.', 'admintrace-activity-logger' ) . '

'; } elseif ( 'table' === $filters['view'] ) { self::table( $rows, ! empty( $args['include_ip'] ), $can_del ); } else { echo '
'; foreach ( $rows as $row ) { Renderer::card( Access::present( $row, ! empty( $args['include_ip'] ) ), array( 'checkbox' => $can_del ) ); } echo '
'; } if ( $can_del ) { echo '
'; } self::pagination( $filters, $pages ); if ( $manage ) { self::note_form(); } echo '

' . esc_html__( 'AdminTrace Activity Logger by Sigma Rocks', 'admintrace-activity-logger' ) . '

'; echo '
'; } /** * Read and sanitize the filter request. * * @return array */ public static function filters() { $settings = Settings::all(); $get = wp_unslash( $_GET ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only filters, each sanitized below. $view = isset( $get['view'] ) && 'table' === $get['view'] ? 'table' : ''; if ( '' === $view ) { $view = ( 'table' === $settings['default_view'] ) ? 'table' : 'timeline'; } return array( 'search' => isset( $get['s'] ) ? Text::plain( $get['s'], 120 ) : '', 'category' => isset( $get['category'] ) ? sanitize_key( $get['category'] ) : '', 'severity' => isset( $get['severity'] ) ? sanitize_key( $get['severity'] ) : '', 'initiator' => isset( $get['initiator'] ) ? sanitize_key( $get['initiator'] ) : '', 'user_id' => isset( $get['user_id'] ) ? absint( $get['user_id'] ) : 0, 'after' => isset( $get['after'] ) ? self::date_input( $get['after'] ) : '', 'before' => isset( $get['before'] ) ? self::date_input( $get['before'] ) : '', 'view' => $view, 'paged' => isset( $get['paged'] ) ? max( 1, absint( $get['paged'] ) ) : 1, 'exclude' => empty( $get['exclude_me'] ) ? 0 : 1, 'event' => isset( $get['event'] ) ? absint( $get['event'] ) : 0, 'per_page' => (int) $settings['per_page'], ); } /** * Query arguments for the current filters. * * @param array $filters Filters. * @return array */ public static function query_args( array $filters ) { return array( 'search' => $filters['search'], 'category' => $filters['category'], 'severity' => $filters['severity'], 'initiator' => $filters['initiator'], 'user_id' => $filters['user_id'], 'after' => $filters['after'], 'before' => $filters['before'], 'page' => $filters['paged'], 'per_page' => $filters['per_page'], 'exclude_user_id' => $filters['exclude'] ? get_current_user_id() : 0, ); } /** * Page header. * * @param array $filters Current filters. */ private static function hero( array $filters ) { $export = ''; if ( current_user_can( Capabilities::EXPORT ) ) { $export = wp_nonce_url( add_query_arg( array_merge( array( 'action' => 'srah_export', 'format' => 'csv', ), self::url_filters( $filters ) ), admin_url( 'admin-post.php' ) ), 'srah_export' ); } ?>

> >
__( 'Today', 'admintrace-activity-logger' ), 'value' => (int) $stats['today'], ), array( 'label' => __( 'Last 7 days', 'admintrace-activity-logger' ), 'value' => (int) $stats['week'], ), ); if ( $manage ) { $tiles[] = array( 'label' => __( 'Failed logins, 24 hours', 'admintrace-activity-logger' ), 'value' => (int) $stats['failed_logins'], ); } echo '
'; foreach ( $tiles as $tile ) { echo '

' . esc_html( $tile['label'] ) . '

' . esc_html( (string) $tile['value'] ) . '
'; } echo '
'; } /** * Filter form. * * @param array $filters Filters. * @param bool $manage Whether the user may list accounts. */ private static function filter_form( array $filters, $manage ) { ?>

'user_id', 'id' => 'srah-user', 'show_option_all' => __( 'All accounts', 'admintrace-activity-logger' ), 'selected' => $filters['user_id'], ) ); ?>

'; if ( $checkboxes ) { echo '' . esc_html__( 'Select', 'admintrace-activity-logger' ) . ''; } echo '' . esc_html__( 'When', 'admintrace-activity-logger' ) . ''; echo '' . esc_html__( 'Severity', 'admintrace-activity-logger' ) . ''; echo '' . esc_html__( 'Category', 'admintrace-activity-logger' ) . ''; echo '' . esc_html__( 'Activity', 'admintrace-activity-logger' ) . ''; echo '' . esc_html__( 'Actor', 'admintrace-activity-logger' ) . ''; echo ''; foreach ( $rows as $row ) { $row = Access::present( $row, $include_ip ); $when = \SigmaRocks\AdvanceHistory\Support\Time::display( $row['created_gmt'], isset( $row['created_ms'] ) ? (int) $row['created_ms'] : 0 ); echo ''; if ( $checkboxes ) { $label = sprintf( /* translators: %s: event summary. */ __( 'Select: %s', 'admintrace-activity-logger' ), $row['summary'] ); echo ''; } echo ''; echo '' . esc_html( Vocabulary::severity_label( $row['severity'] ) ) . ''; echo '' . esc_html( Vocabulary::category_label( $row['category'] ) ) . ''; echo '' . esc_html( $row['summary'] ) . ''; $repeats = isset( $row['repeat_count'] ) ? (int) $row['repeat_count'] : 1; if ( $repeats > 1 ) { echo ' ' . esc_html( sprintf( /* translators: %d: number of times this event happened. */ _n( '%d time', '%d times', $repeats, 'admintrace-activity-logger' ), $repeats ) ) . ''; } echo ''; echo '' . esc_html( Renderer::actor( $row ) ) . ''; echo ''; } echo ''; } /** * Pagination. * * @param array $filters Filters. * @param int $pages Total pages. */ private static function pagination( array $filters, $pages ) { if ( $pages < 2 ) { return; } $base = self::view_url( $filters, $filters['view'] ); $links = paginate_links( array( 'base' => add_query_arg( 'paged', '%#%', $base ), 'format' => '', 'current' => $filters['paged'], 'total' => $pages, 'type' => 'plain', 'prev_text' => __( 'Previous', 'admintrace-activity-logger' ), 'next_text' => __( 'Next', 'admintrace-activity-logger' ), ) ); if ( $links ) { echo ''; } } /** * Journal note form. */ private static function note_form() { ?>

format( 'Y-m-d' ) !== $value ) { return ''; } return $value; } /** * URL that preserves filters and switches view. * * @param array $filters Filters. * @param string $view View name. * @return string */ private static function view_url( array $filters, $view ) { $args = array( 'page' => 'admintrace-activity-logger', 'view' => $view, ); return add_query_arg( array_merge( $args, self::url_filters( $filters ) ), admin_url( 'admin.php' ) ); } /** * Active filters as the URL parameters that filters() reads. * * @param array $filters Filters. * @return array */ private static function url_filters( array $filters ) { $args = array(); foreach ( array( 'search' => 's', 'category' => 'category', 'severity' => 'severity', 'initiator' => 'initiator', 'user_id' => 'user_id', 'after' => 'after', 'before' => 'before', ) as $from => $to ) { if ( ! empty( $filters[ $from ] ) ) { $args[ $to ] = $filters[ $from ]; } } if ( ! empty( $filters['exclude'] ) ) { $args['exclude_me'] = 1; } return $args; } }