$details Non-secret context. */ public static function record( $type, array $details = array() ) { global $wpdb; if ( ! isset( $wpdb ) ) { return; } $wpdb->insert( Schema::table( 'events' ), array( 'event_type' => substr( sanitize_key( $type ), 0, 64 ), 'user_id' => function_exists( 'get_current_user_id' ) ? (int) get_current_user_id() : 0, 'details' => wp_json_encode( self::redact( $details ) ), 'created_at' => gmdate( 'Y-m-d H:i:s' ), ), array( '%s', '%d', '%s', '%s' ) ); } /** * @param array $details * @return array */ public static function redact( array $details ) { $out = array(); foreach ( $details as $k => $v ) { foreach ( self::REDACT_KEYS as $needle ) { if ( false !== stripos( (string) $k, $needle ) ) { $out[ $k ] = '[redacted]'; continue 2; } } $out[ $k ] = is_array( $v ) ? self::redact( $v ) : ( is_scalar( $v ) ? substr( (string) $v, 0, 500 ) : null ); } return $out; } /** @return array> */ public static function recent( $limit = 50 ) { global $wpdb; $table = Schema::table( 'events' ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name is internal. return (array) $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$table} ORDER BY id DESC LIMIT %d", (int) $limit ), ARRAY_A ); } }