$entity_ids Parameter. */ public function get_for_entities( string $entity_type, array $entity_ids ): array { $ids = array_values( array_unique( array_filter( array_map( 'intval', $entity_ids ), static fn( int $id ): bool => $id > 0 ) ) ); $out = []; foreach ( $ids as $id ) { $out[ $id ] = []; } if ( '' === trim( $entity_type ) || [] === $ids ) { return $out; } $placeholders = implode( ',', array_fill( 0, count( $ids ), '%d' ) ); $sql = $this->db->prepare( "SELECT `entity_id`, `meta_key`, `meta_value` FROM %i WHERE `entity_type` = %s AND `entity_id` IN ({$placeholders})", $this->table( 'entity_meta' ), $entity_type, ...$ids ); $rows = $this->db->get_results( $sql, ARRAY_A ); if ( ! is_array( $rows ) ) { return $out; } foreach ( $rows as $row ) { if ( ! is_array( $row ) ) { continue; } $entity_id = (int) $row['entity_id']; $key = (string) $row['meta_key']; $value = array_key_exists( 'meta_value', $row ) && null !== $row['meta_value'] ? (string) $row['meta_value'] : null; $out[ $entity_id ][ $key ] = $value; } return $out; } /** * {@inheritDoc} * * @param string $entity_type Parameter. * @param int $entity_id Parameter. */ public function get_for_entity( string $entity_type, int $entity_id ): array { $map = $this->get_for_entities( $entity_type, [ $entity_id ] ); return $map[ $entity_id ] ?? []; } /** * {@inheritDoc} * * @param string $entity_type Parameter. * @param int $entity_id Parameter. * @param string $meta_key Parameter. * @param string|null $meta_value Parameter. * * @throws DomainException When arguments are invalid. */ public function put( string $entity_type, int $entity_id, string $meta_key, ?string $meta_value ): void { $this->assert_write_args( $entity_type, $entity_id, $meta_key ); $existing = $this->db->prepare( 'SELECT `id` FROM %i WHERE `entity_type` = %s AND `entity_id` = %d AND `meta_key` = %s LIMIT 1', $this->table( 'entity_meta' ), $entity_type, $entity_id, $meta_key ); $id = $this->db->get_var( $existing ); if ( null !== $id && '' !== $id ) { $this->db->update( $this->table( 'entity_meta' ), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- Bookline entity_meta column. [ 'meta_value' => $meta_value ], [ 'id' => (int) $id ] ); return; } $this->db->insert( $this->table( 'entity_meta' ), [ 'entity_type' => $entity_type, 'entity_id' => $entity_id, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Bookline entity_meta column. 'meta_key' => $meta_key, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- Bookline entity_meta column. 'meta_value' => $meta_value, ] ); } /** * {@inheritDoc} * * @param string $entity_type Parameter. * @param int $entity_id Parameter. * @param string $meta_key Parameter. * * @throws DomainException When arguments are invalid. */ public function delete( string $entity_type, int $entity_id, string $meta_key ): void { $this->assert_write_args( $entity_type, $entity_id, $meta_key ); $sql = $this->db->prepare( 'DELETE FROM %i WHERE `entity_type` = %s AND `entity_id` = %d AND `meta_key` = %s', $this->table( 'entity_meta' ), $entity_type, $entity_id, $meta_key ); $this->query_prepared( $sql ); } /** * {@inheritDoc} * * @param string $entity_type Parameter. * @param int $entity_id Parameter. * * @throws DomainException When arguments are invalid. */ public function delete_for_entity( string $entity_type, int $entity_id ): void { if ( '' === trim( $entity_type ) || $entity_id < 1 ) { throw new DomainException( 'entity_meta.args', 'entity_type and entity_id are required.' ); } $sql = $this->db->prepare( 'DELETE FROM %i WHERE `entity_type` = %s AND `entity_id` = %d', $this->table( 'entity_meta' ), $entity_type, $entity_id ); $this->query_prepared( $sql ); } /** * Validate write arguments. * * @param string $entity_type Type. * @param int $entity_id Id. * @param string $meta_key Key. * * @throws DomainException When invalid. */ private function assert_write_args( string $entity_type, int $entity_id, string $meta_key ): void { if ( '' === trim( $entity_type ) || '' === trim( $meta_key ) || $entity_id < 1 ) { throw new DomainException( 'entity_meta.args', 'entity_type, entity_id and meta_key are required.' ); } } }