term_id; } $term = get_term_by( 'slug', sanitize_title( $name ), $taxonomy ); if ( $term ) { return (int) $term->term_id; } $norm = bcal_normalize_name( $name ); if ( '' !== $norm ) { $found = get_terms( array( 'taxonomy' => $taxonomy, 'hide_empty' => false, 'number' => 1, 'fields' => 'ids', 'meta_key' => 'bcal_alias_norm', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key 'meta_value' => $norm, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value ) ); if ( ! is_wp_error( $found ) && $found ) { return (int) $found[0]; } } if ( ! $create ) { return 0; } $result = wp_insert_term( $name, $taxonomy ); if ( is_wp_error( $result ) ) { $existing = $result->get_error_data( 'term_exists' ); return $existing ? (int) $existing : 0; } return (int) $result['term_id']; } /** * Converts a comma separated list of slugs or IDs to term IDs. * * @param string|array $value Slugs or IDs. * @param string $taxonomy Taxonomy. * @return array */ function bcal_terms_to_ids( $value, $taxonomy ) { $items = is_array( $value ) ? $value : explode( ',', (string) $value ); $ids = array(); foreach ( $items as $item ) { $item = trim( (string) $item ); if ( '' === $item ) { continue; } if ( ctype_digit( $item ) ) { $ids[] = (int) $item; continue; } $term = get_term_by( 'slug', sanitize_title( $item ), $taxonomy ); if ( $term ) { $ids[] = (int) $term->term_id; } } return array_values( array_unique( $ids ) ); }