array( 'eyewear', 'glasses', 'sunglasses', 'eyeglasses', 'spectacles', 'optical' ), self::DRESS => array( 'dress', 'dresses', 'gown', 'gowns', 'sundress', 'sundresses' ), self::TOP => array( 'top', 'tops', 'shirt', 'shirts', 'tshirt', 'tshirts', 'tee', 'tees', 'blouse', 'blouses', 'sweater', 'sweaters', 'hoodie', 'hoodies', 'sweatshirt', 'sweatshirts', 'polo', 'polos', 'cardigan', 'cardigans', 'jumper', 'jumpers', 'pullover', 'pullovers', 'knitwear', 'jacket', 'jackets', 'camisole', 'camisoles' ), ); /** * The type for a product's category names and slugs, or null when none is supported. * * @param string[] $terms Category names and slugs. */ public static function from_terms( array $terms ): ?string { $words = array(); foreach ( $terms as $term ) { foreach ( (array) preg_split( '/[^a-z]+/', strtolower( (string) $term ), -1, PREG_SPLIT_NO_EMPTY ) as $word ) { $words[ (string) $word ] = true; } } foreach ( self::WORDS as $type => $keywords ) { foreach ( $keywords as $keyword ) { if ( isset( $words[ $keyword ] ) ) { return $type; } } } return null; } /** * The value when it is a known type, otherwise null. * * @param mixed $type Candidate, e.g. a filter result. */ public static function valid( $type ): ?string { return is_string( $type ) && isset( self::WORDS[ $type ] ) ? $type : null; } }