translator = $translator; } /** * Translate a term, creating or updating the linked translation. * * @return int Translated term ID. * @throws \RuntimeException When the term cannot be created. */ public function translate( int $term_id, string $lang, bool $force = false ): int { $term_id = Groups::source_id( 'term', $term_id ); $term = get_term( $term_id ); if ( ! $term instanceof \WP_Term ) { throw new \RuntimeException( esc_html__( 'Unknown term.', 'translateplus' ) ); } if ( $lang === Languages::source() ) { return $term_id; } $existing = Groups::translation( 'term', $term_id, $lang ); if ( $existing && ! get_term( $existing ) ) { Groups::delete_object( 'term', $existing ); $existing = 0; } if ( $existing && ! $force ) { return $existing; } $tr = $this->translator->translate( array( 'name' => $term->name, 'description' => $term->description, ), Languages::source(), $lang ); $parent = 0; if ( $term->parent ) { $parent = $this->translate( (int) $term->parent, $lang ); } $args = array( 'description' => $tr['description'], 'parent' => $parent, ); if ( $existing ) { $args['name'] = $tr['name']; wp_update_term( $existing, $term->taxonomy, wp_slash( $args ) ); $target = $existing; } else { $args['slug'] = $this->unique_slug( sanitize_title( $tr['name'] ), $term->taxonomy, $lang ); $result = wp_insert_term( wp_slash( $tr['name'] ), $term->taxonomy, wp_slash( $args ) ); if ( is_wp_error( $result ) ) { // A term with the same name exists in this parent: reuse it only if it is already this language. $dup = (int) $result->get_error_data( 'term_exists' ); if ( $dup && Groups::lang_of( 'term', $dup ) === $lang ) { $target = $dup; } else { $args['slug'] = $this->unique_slug( sanitize_title( $tr['name'] ) . '-' . Languages::slug( $lang ), $term->taxonomy, $lang ); $result = wp_insert_term( wp_slash( $tr['name'] . ' (' . strtoupper( $lang ) . ')' ), $term->taxonomy, wp_slash( $args ) ); if ( is_wp_error( $result ) ) { throw new \RuntimeException( esc_html( $result->get_error_message() ) ); } $target = (int) $result['term_id']; wp_update_term( $target, $term->taxonomy, array( 'name' => wp_slash( $tr['name'] ) ) ); } } else { $target = (int) $result['term_id']; } } // Copy term meta (thumbnails, display type, ordering). foreach ( get_term_meta( $term_id ) as $key => $values ) { if ( 0 === strpos( (string) $key, '_tp_' ) ) { continue; } delete_term_meta( $target, $key ); foreach ( $values as $v ) { add_term_meta( $target, $key, wp_slash( maybe_unserialize( $v ) ) ); } } Groups::link( 'term', $term_id, $target, $lang, Groups::STATUS_TRANSLATED, md5( $term->name . "\0" . $term->description ) ); do_action( 'translateplus_term_translated', $target, $term, $lang ); return $target; } private function unique_slug( string $slug, string $taxonomy, string $lang ): string { $candidate = $slug; if ( term_exists( $candidate, $taxonomy ) ) { $candidate = $slug . '-' . Languages::slug( $lang ); } $i = 2; while ( term_exists( $candidate, $taxonomy ) ) { $candidate = $slug . '-' . Languages::slug( $lang ) . '-' . $i++; } return $candidate; } }