> */ public function for_customer( int $customer_id, int $limit = 100 ): array { $table = Tables::name( 'customer_notes' ); $notes = $this->rows( "SELECT n.*, u.display_name AS author_name FROM {$table} n LEFT JOIN {$this->wpdb->users} u ON u.ID = n.author_id WHERE n.customer_id = %d ORDER BY n.id DESC LIMIT %d", array( $customer_id, $limit ) ); return array_map( array( $this, 'hydrate' ), $notes ); } /** * Finds a note. * * @since 0.1.0 * * @param int $id Note id. * @return array|null */ public function find( int $id ): ?array { $table = Tables::name( 'customer_notes' ); $row = $this->row( "SELECT * FROM {$table} WHERE id = %d", array( $id ) ); return null === $row ? null : $this->hydrate( $row ); } /** * Adds a note. * * @since 0.1.0 * * @param int $customer_id Customer id. * @param string $note Note text. * @param string $visibility `internal` or `customer`. * @param int $author_id WordPress user id of the author. * @return int New id, or 0 on failure. */ public function insert( int $customer_id, string $note, string $visibility, int $author_id ): int { return $this->insert_row( Tables::name( 'customer_notes' ), array( 'customer_id' => $customer_id, 'author_id' => $author_id, 'visibility' => $visibility, 'note' => $note, 'created_at_gmt' => Date::now_gmt(), ), array( '%d', '%d', '%s', '%s', '%s' ) ); } /** * Deletes a note. * * @since 0.1.0 * * @param int $id Note id. * @return bool */ public function delete( int $id ): bool { return false !== $this->wpdb->delete( Tables::name( 'customer_notes' ), array( 'id' => $id ), array( '%d' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching } /** * Deletes all notes of a customer. * * @since 0.1.0 * * @param int $customer_id Customer id. * @return void */ public function delete_for_customer( int $customer_id ): void { $this->wpdb->delete( Tables::name( 'customer_notes' ), array( 'customer_id' => $customer_id ), array( '%d' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching } /** * Casts a row. * * @param array $row Database row. * @return array */ private function hydrate( array $row ): array { $row['id'] = (int) $row['id']; $row['customer_id'] = (int) $row['customer_id']; $row['author_id'] = (int) $row['author_id']; return $row; } }