show_in_rest ) ) { /* translators: %s: taxonomy name */ return Helpers::not_found( sprintf( __( 'Taxonomy "%s" does not exist or is not available.', 'seeb-abilities' ), $name ) ); } return $taxonomy; } /** * Resolves term IDs, slugs or names to term IDs. Integers must be existing term IDs. Strings * are matched by slug, then by name, and are created when missing and the user can create terms. * * @param string $taxonomy Taxonomy name. * @param array $terms Term IDs, slugs or names. * * @return int[]|WP_Error */ public static function resolve( string $taxonomy, array $terms ) { $tax_object = get_taxonomy( $taxonomy ); $ids = []; foreach ( $terms as $term ) { if ( is_int( $term ) ) { $found = get_term( $term, $taxonomy ); if ( ! $found || is_wp_error( $found ) ) { /* translators: 1: term ID, 2: taxonomy name */ return Helpers::not_found( sprintf( __( 'Term %1$d does not exist in taxonomy "%2$s".', 'seeb-abilities' ), $term, $taxonomy ) ); } $ids[] = $found->term_id; continue; } $term = trim( (string) $term ); if ( '' === $term ) { continue; } $found = get_term_by( 'slug', sanitize_title( $term ), $taxonomy ); if ( ! $found ) { $found = get_term_by( 'name', $term, $taxonomy ); } if ( $found ) { $ids[] = $found->term_id; continue; } if ( ! current_user_can( $tax_object->cap->edit_terms ) ) { /* translators: 1: term name, 2: taxonomy name */ return Helpers::forbidden( sprintf( __( 'Term "%1$s" does not exist in taxonomy "%2$s" and you are not allowed to create it.', 'seeb-abilities' ), $term, $taxonomy ) ); } $created = wp_insert_term( wp_slash( $term ), $taxonomy ); if ( is_wp_error( $created ) ) { return $created; } $ids[] = (int) $created['term_id']; } return array_values( array_unique( $ids ) ); } /** * Checks that the user can assign terms of the taxonomy to posts of the post type. * * @param string $taxonomy Taxonomy name. * @param string $post_type Post type. * * @return true|WP_Error */ public static function check_assign( string $taxonomy, string $post_type ) { $tax_object = self::taxonomy( $taxonomy ); if ( is_wp_error( $tax_object ) ) { return $tax_object; } if ( ! is_object_in_taxonomy( $post_type, $taxonomy ) ) { /* translators: 1: taxonomy name, 2: post type */ return Helpers::error( 'invalid_taxonomy', sprintf( __( 'Taxonomy "%1$s" is not registered for post type "%2$s".', 'seeb-abilities' ), $taxonomy, $post_type ) ); } if ( ! current_user_can( $tax_object->cap->assign_terms ) ) { /* translators: %s: taxonomy name */ return Helpers::forbidden( sprintf( __( 'You are not allowed to assign terms of taxonomy "%s".', 'seeb-abilities' ), $taxonomy ) ); } return true; } /** * Sets terms of several taxonomies on a post. * * @param int $post_id Post ID. * @param array $terms Term IDs, slugs or names keyed by taxonomy. * @param bool $append Whether to keep the existing terms. * * @return true|WP_Error */ public static function set_post_terms( int $post_id, array $terms, bool $append = false ) { $post_type = (string) get_post_type( $post_id ); foreach ( $terms as $taxonomy => $values ) { $check = self::check_assign( (string) $taxonomy, $post_type ); if ( is_wp_error( $check ) ) { return $check; } $ids = self::resolve( (string) $taxonomy, (array) $values ); if ( is_wp_error( $ids ) ) { return $ids; } $result = wp_set_object_terms( $post_id, $ids, (string) $taxonomy, $append ); if ( is_wp_error( $result ) ) { return $result; } } return true; } }