*/ public function for_post( MapConfig $config, int $post_id ): array { $properties = array(); foreach ( $config->properties as $row ) { if ( ! is_array( $row ) ) { continue; } $name = (string) ( $row['property'] ?? '' ); $ref = FieldRef::parse( $row['field'] ?? null ); if ( '' === $name || null === $ref ) { continue; } $value = FieldRef::KIND_TAX === $ref['kind'] ? $this->term_slugs( $post_id, $ref['name'] ) : $this->meta_value( $post_id, $ref['name'] ); if ( null !== $value ) { $properties[ $name ] = $value; } } return $properties; } /** * A post's terms in one taxonomy, flattened when single. * * @param int $post_id Post id. * @param string $taxonomy Taxonomy name. * @return string|string[]|null */ private function term_slugs( int $post_id, string $taxonomy ) { $terms = get_the_terms( $post_id, $taxonomy ); if ( ! is_array( $terms ) || array() === $terms ) { return null; } $slugs = array_map( static fn( \WP_Term $term ): string => (string) $term->slug, $terms ); return 1 === count( $slugs ) ? $slugs[0] : array_values( $slugs ); } /** * A post's meta value as a string, or null when empty. * * @param int $post_id Post id. * @param string $key Meta key. */ private function meta_value( int $post_id, string $key ): ?string { $value = get_post_meta( $post_id, $key, true ); if ( ! is_scalar( $value ) || '' === (string) $value ) { return null; } return (string) $value; } }