$entry actor_id, customer_id, object_type, object_id, action, message, context. * @return int New id, or 0 on failure. */ public function insert( array $entry ): int { return $this->insert_row( Tables::name( 'activity_log' ), array( 'actor_id' => (int) ( $entry['actor_id'] ?? 0 ), 'customer_id' => (int) ( $entry['customer_id'] ?? 0 ), 'object_type' => (string) ( $entry['object_type'] ?? '' ), 'object_id' => (int) ( $entry['object_id'] ?? 0 ), 'action' => (string) $entry['action'], 'message' => (string) ( $entry['message'] ?? '' ), 'context' => Helpers::json_encode( $entry['context'] ?? array() ), 'ip_address' => (string) ( $entry['ip_address'] ?? '' ), 'created_at_gmt' => Date::now_gmt(), ), array( '%d', '%d', '%s', '%d', '%s', '%s', '%s', '%s', '%s' ) ); } /** * Entries of a customer, newest first. * * @since 0.1.0 * * @param int $customer_id Customer id. * @param int $limit Maximum entries. * @return array> */ public function for_customer( int $customer_id, int $limit = 50 ): array { $table = Tables::name( 'activity_log' ); $rows = $this->rows( "SELECT a.*, u.display_name AS actor_name FROM {$table} a LEFT JOIN {$this->wpdb->users} u ON u.ID = a.actor_id WHERE a.customer_id = %d ORDER BY a.id DESC LIMIT %d", array( $customer_id, $limit ) ); foreach ( $rows as &$row ) { $row['id'] = (int) $row['id']; $row['actor_id'] = (int) $row['actor_id']; $row['context'] = Helpers::json_decode_array( $row['context'] ); } return $rows; } /** * Entries about one object, such as an order, oldest first so they read as a timeline. * * @since 0.1.0 * * @param string $object_type Object type. * @param int $object_id Object id. * @param int $limit Maximum entries. * @return array> */ public function for_object( string $object_type, int $object_id, int $limit = 100 ): array { $table = Tables::name( 'activity_log' ); $rows = $this->rows( "SELECT a.*, u.display_name AS actor_name FROM {$table} a LEFT JOIN {$this->wpdb->users} u ON u.ID = a.actor_id WHERE a.object_type = %s AND a.object_id = %d ORDER BY a.id ASC LIMIT %d", array( $object_type, $object_id, $limit ) ); foreach ( $rows as &$row ) { $row['id'] = (int) $row['id']; $row['actor_id'] = (int) $row['actor_id']; $row['context'] = Helpers::json_decode_array( $row['context'] ); } return $rows; } /** * Removes personal details from the log entries of a customer (used when anonymising). * * @since 0.1.0 * * @param int $customer_id Customer id. * @return void */ public function scrub_customer( int $customer_id ): void { $table = Tables::name( 'activity_log' ); $this->wpdb->query( $this->prepare( "UPDATE {$table} SET message = '', context = '[]', ip_address = '' WHERE customer_id = %d", array( $customer_id ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.PreparedSQL.InterpolatedNotPrepared,PluginCheck.Security.DirectDB.UnescapedDBParameter -- Table name from the Tables whitelist; values prepared by prepare(). } }