Keyed by target ID. */ public function targets( string $owner_type, int $owner_id, string $target_type ): array { // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery $rows = (array) $this->db->get_results( $this->db->prepare( "SELECT target_id, price_minor, duration FROM {$this->table} WHERE owner_type = %s AND owner_id = %d AND target_type = %s ORDER BY target_id", $owner_type, $owner_id, $target_type ), ARRAY_A ); $out = array(); foreach ( $rows as $row ) { $out[ (int) $row['target_id'] ] = array( 'id' => (int) $row['target_id'], 'price' => null === $row['price_minor'] ? null : (int) $row['price_minor'], 'duration' => null === $row['duration'] ? null : (int) $row['duration'], ); } return $out; } /** * Target IDs for many owners at once. * * @param string $owner_type Owner type. * @param int[] $owner_ids Owners. * @param string $target_type Target type. * @return array Owner ID => target IDs. */ public function map( string $owner_type, array $owner_ids, string $target_type ): array { $owner_ids = array_values( array_filter( array_map( 'intval', $owner_ids ) ) ); if ( array() === $owner_ids ) { return array(); } $args = array_merge( array( $owner_type, $target_type ), $owner_ids ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.DirectDatabaseQuery $rows = (array) $this->db->get_results( $this->db->prepare( "SELECT owner_id, target_id FROM {$this->table} WHERE owner_type = %s AND target_type = %s AND owner_id IN (" . self::in( $owner_ids ) . ')', $args ), ARRAY_A ); $out = array_fill_keys( $owner_ids, array() ); foreach ( $rows as $row ) { $out[ (int) $row['owner_id'] ][] = (int) $row['target_id']; } return $out; } /** * Owners linked to a target, e.g. the bookables a staff member offers. * * @param string $owner_type Owner type. * @param string $target_type Target type. * @param int $target_id Target. * @return int[] */ public function owners( string $owner_type, string $target_type, int $target_id ): array { // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery return array_map( 'intval', (array) $this->db->get_col( $this->db->prepare( "SELECT owner_id FROM {$this->table} WHERE owner_type = %s AND target_type = %s AND target_id = %d ORDER BY owner_id", $owner_type, $target_type, $target_id ) ) ); } /** * Replace an owner's links of one type. * * @param string $owner_type Owner type. * @param int $owner_id Owner. * @param string $target_type Target type. * @param array $targets IDs or {id, price, duration}. */ public function set( string $owner_type, int $owner_id, string $target_type, array $targets ): void { $this->db->delete( $this->table, array( 'owner_type' => $owner_type, 'owner_id' => $owner_id, 'target_type' => $target_type, ) ); $seen = array(); foreach ( $targets as $target ) { $target = is_array( $target ) ? $target : array( 'id' => (int) $target ); $id = (int) ( $target['id'] ?? 0 ); if ( $id <= 0 || isset( $seen[ $id ] ) ) { continue; } $seen[ $id ] = true; $row = array( 'owner_type' => $owner_type, 'owner_id' => $owner_id, 'target_type' => $target_type, 'target_id' => $id, ); if ( isset( $target['price'] ) && null !== $target['price'] ) { $row['price_minor'] = max( 0, (int) $target['price'] ); } if ( isset( $target['duration'] ) && null !== $target['duration'] ) { $row['duration'] = max( 5, (int) $target['duration'] ); } $this->db->insert( $this->table, $row ); } } /** * Remove every link to or from something being archived. * * @param string $type Type. * @param int $id ID. */ public function forget( string $type, int $id ): void { $this->db->delete( $this->table, array( 'owner_type' => $type, 'owner_id' => $id, ) ); $this->db->delete( $this->table, array( 'target_type' => $type, 'target_id' => $id, ) ); } }