wp_new_term_args[ 'name' ] = $term; $this->taxonomy = $taxonomy; } else { $this->is_valid = false; return; } if ( $parent ) { $this->wp_new_term_args[ 'parent' ] = $parent; } if ( $description ) { $this->wp_new_term_args[ 'description' ] = $description; } if ( $term_group ) { $this->wp_new_term_args[ 'term_group' ] = $term_group; } $this->wp_new_term_args[ 'term_group' ] = $term_group; $this->is_valid = $this->set_language_information( $trid, $original_tax_id, $lang_code, $source_language ); $this->set_action_type(); if ( ! $this->is_update || ( $this->is_update && $slug != $this->old_slug && ! empty( $slug ) ) ) { if ( trim( $slug ) == '' ) { $slug = sanitize_title( $term ); } $slug = WPML_Terms_Translations::term_unique_slug( $slug, $taxonomy, $lang_code ); $this->wp_new_term_args[ 'slug' ] = $slug; } } /** * Writes the term update or creation action saved in this object to the database. * @return array|false * Returns either an array containing the term_id and term_taxonomy_id of the term resulting from this database * write or false on error. */ public function execute() { global $sitepress; $switch_lang = new WPML_Temporary_Switch_Language( $sitepress, $this->lang_code ); remove_action( 'create_term', array( $sitepress, 'create_term' ), 1 ); remove_action( 'edit_term', array( $sitepress, 'create_term' ), 1 ); add_action( 'create_term', array( $this, 'add_term_language_action' ), 1, 3 ); $new_term = false; if ( $this->is_valid ) { if ( $this->is_update && $this->term_id ) { $new_term = wp_update_term( $this->term_id, $this->taxonomy, $this->wp_new_term_args ); } else { $new_term = wp_insert_term( $this->wp_new_term_args[ 'name' ], $this->taxonomy, $this->wp_new_term_args ); } } add_action( 'create_term', array( $sitepress, 'create_term' ), 1, 3 ); add_action( 'edit_term', array( $sitepress, 'create_term' ), 1, 3 ); remove_action( 'create_term', array( $this, 'add_term_language_action' ), 1, 3 ); if ( ! is_array( $new_term ) ) { $new_term = false; } unset( $switch_lang ); return $new_term; } /** * This action is to be hooked to the WP create_term and edit_term hooks. * It sets the correct language information after a term is saved. * * @param int|string $term_id * @param int|string $term_taxonomy_id * @param string $taxonomy */ public function add_term_language_action( $term_id, $term_taxonomy_id, $taxonomy ) { if ( $this->is_valid && ! $this->is_update && $this->taxonomy == $taxonomy ) { $this->sitepress->set_element_language_details( $term_taxonomy_id, 'tax_' . $taxonomy, $this->trid, $this->lang_code, $this->source_lang_code ); } } /** * Sets the language variables for this object. * @param bool|int $trid * @param bool|int $original_tax_id * @param string $lang_code * @param bool|string $source_language * @return bool True if the given language parameters allowed for determining valid language information, false * otherwise. */ private function set_language_information( $trid, $original_tax_id, $lang_code, $source_language ) { if ( ! $lang_code || ! $this->sitepress->is_active_language( $lang_code ) ) { return false; } else { $this->lang_code = $lang_code; } if ( ! $trid && $original_tax_id ) { $trid = $this->sitepress->get_element_trid( $original_tax_id, 'tax_' . $this->taxonomy ); } if ( $trid ) { $this->trid = $trid; $this->existing_translations = $this->sitepress->get_element_translations( $trid, 'tax_' . $this->taxonomy ); // Only set valid source languages if ( $source_language && isset( $translations[ $source_language ] ) ) { $this->source_lang_code = $source_language; } else { foreach ( $this->existing_translations as $lang => $translation ) { if ( $original_tax_id && isset( $translation->element_id ) && $translation->element_id == $original_tax_id && isset( $translation->language_code ) && $translation->language_code ) { $this->source_lang_code = $translation->language_code; break; } elseif ( isset( $translation->language_code ) && $translation->language_code && ! $translation->source_language_code ) { $this->source_lang_code = $translation->language_code; } } } } return true; } /** * Sets the action type of this object. * In case of this action being an update the is_update flag is set true. * Also the term_id of the existing term is saved in $this->term_id. */ private function set_action_type() { if ( ! $this->trid ) { $this->is_update = false; } elseif ( isset( $this->existing_translations[ $this->lang_code ] ) ) { $existing_db_entry = $this->existing_translations[ $this->lang_code ]; if ( isset( $existing_db_entry->element_id ) && $existing_db_entry->element_id ) { // Term update actions need information about the term_id, not the term_taxonomy_id saved in the element_id column of icl_translations. $term = $this->wpdb->get_row( $this->wpdb->prepare( "SELECT t.term_id, t.slug FROM {$this->wpdb->terms} AS t JOIN {$this->wpdb->term_taxonomy} AS tt ON t.term_id=tt.term_id WHERE term_taxonomy_id=%d", $existing_db_entry->element_id ) ); if ( $term->term_id && $term->slug ) { $this->is_update = true; $this->term_id = $term->term_id; $this->old_slug = $term->slug; } else { $this->is_update = false; } } else { $this->sitepress->delete_element_translation( $this->trid, 'tax_' . $this->taxonomy, $this->lang_code ); $this->is_update = false; } } else { $this->is_update = false; } } }