Skip to content

Instantly share code, notes, and snippets.

@vmattila
Created January 6, 2013 11:56
Show Gist options
  • Save vmattila/4466712 to your computer and use it in GitHub Desktop.
Save vmattila/4466712 to your computer and use it in GitHub Desktop.
A short snippet of test case & it's output regarding WPML's behavior in wp_update_term() function. Relates to question in http://wpml.org/forums/topic/updating-a-term-with-wp_update_term-causes-a-new-translation-created/ .
Inserted FI: Array
(
[term_id] => 171
[term_taxonomy_id] => 176
)
trid after FI: 121
Inserted EN: Array
(
[term_id] => 172
[term_taxonomy_id] => 177
)
** First versions of categories inserted **
wpdb->update is run. Updates: Array
(
[trid] => 121
[language_code] => en
[source_language_code] => fi
)
WHERE: Array
(
[element_type] => tax_test_folder
[element_id] => 177
)
icl_translations table:
Array
(
[translation_id] => 408
[element_type] => tax_test_folder
[element_id] => 176
[trid] => 121
[language_code] => fi
[source_language_code] =>
)
Array
(
[translation_id] => 409
[element_type] => tax_test_folder
[element_id] => 177
[trid] => 121
[language_code] => en
[source_language_code] => fi
)
End of icl_translations table.
** First versions of categories inserted and translations linked **
** Starting to update categories **
Updated EN: Array
(
[term_id] => 172
[term_taxonomy_id] => 177
)
icl_translations table after updated EN translation:
Array
(
[translation_id] => 408
[element_type] => tax_test_folder
[element_id] => 176
[trid] => 121
[language_code] => fi
[source_language_code] =>
)
Array
(
[translation_id] => 410
[element_type] => tax_test_folder
[element_id] => 177
[trid] => 122
[language_code] => en
[source_language_code] =>
)
End of icl_translations table.
Updated FI: Array
(
[term_id] => 171
[term_taxonomy_id] => 176
)
icl_translations table after updated FI translation:
Array
(
[translation_id] => 411
[element_type] => tax_test_folder
[element_id] => 176
[trid] => 123
[language_code] => fi
[source_language_code] =>
)
Array
(
[translation_id] => 410
[element_type] => tax_test_folder
[element_id] => 177
[trid] => 122
[language_code] => en
[source_language_code] =>
)
End of icl_translations table.
******** END OF TEST RUN ********
<?php
global $sitepress; global $wpdb;
$taxonomy = 'test_folder';
$primaryLanguage = 'fi';
// First inserting Finnish category
$_POST['icl_tax_' . $taxonomy . '_language'] = $primaryLanguage; // WPML
$inserted = wp_insert_term("Category in Finnish", $taxonomy, array());
echo "Inserted FI: ".print_r($inserted,true)."\n\n";
// Saving Finnish category details + WPML $trid to variables
$fi_term_id = $inserted['term_id'];
$fi_tt_id = $inserted['term_taxonomy_id'];
$trid = $sitepress->get_element_trid($fi_tt_id, 'tax_' . $taxonomy);
echo "trid after FI: $trid\n\n";
// Then inserting English category
$_POST['icl_tax_' . $taxonomy . '_language'] = 'en'; // WPML
$inserted = wp_insert_term("Category in English", $taxonomy, array());
echo "Inserted EN: ".print_r($inserted,true)."\n\n";
// Saving English category details
$en_term_id = $inserted['term_id'];
$en_tt_id = $inserted['term_taxonomy_id'];
echo "** First versions of categories inserted **\n";
// Linking English translation to Finnish translation
$updates = array('trid' => $trid, 'language_code' => 'en', 'source_language_code' => $primaryLanguage);
$where = array('element_type' => 'tax_' . $taxonomy, 'element_id' => $en_tt_id);
$wpdb->update($wpdb->prefix . 'icl_translations', $updates, $where);
echo "wpdb->update is run. Updates: " . print_r($updates,true)." WHERE: " . print_r($where,true)."\n";
echo "icl_translations table:\n";
$icl_translations = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}icl_translations WHERE element_type = 'tax_" . $taxonomy . "'", ARRAY_A);
foreach ($icl_translations as $row) {
print_r($row);
echo "\n";
}
echo "End of icl_translations table.\n\n";
echo "** First versions of categories inserted and translations linked **\n";
echo "** Starting to update categories **\n\n";
// Updating the English category
$_POST['icl_tax_' . $taxonomy . '_language'] = 'en'; // WPML
$updated = wp_update_term($en_term_id, $taxonomy, array('name' => 'Category in English, updated'));
echo "Updated EN: ".print_r($updated,true)."\n";
echo "icl_translations table after updated EN translation:\n";
$icl_translations = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}icl_translations WHERE element_type = 'tax_" . $taxonomy . "'", ARRAY_A);
foreach ($icl_translations as $row) {
print_r($row);
echo "\n";
}
echo "End of icl_translations table.\n\n";
// Updating the Finnish category
$_POST['icl_tax_' . $taxonomy . '_language'] = 'fi'; // WPML
$updated = wp_update_term($fi_term_id, $taxonomy, array('name' => 'Category in Finnish, updated'));
echo "Updated FI: ".print_r($updated,true)."\n";
echo "icl_translations table after updated FI translation:\n";
$icl_translations = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}icl_translations WHERE element_type = 'tax_" . $taxonomy . "'", ARRAY_A);
foreach ($icl_translations as $row) {
print_r($row);
echo "\n";
}
echo "End of icl_translations table.\n\n";
echo "******** END OF TEST RUN ********\n";
@vmattila
Copy link
Author

vmattila commented Jan 6, 2013

A revised code for the second part of the test case (line 47 forwards):

// Updating the English category
$_POST['icl_tax_' . $taxonomy . '_language'] = 'en'; // WPML
$updated = wp_update_term($en_term_id, $taxonomy, array('name' => 'Category in English, updated'));
echo "Updated EN: ".print_r($updated,true)."\n";

wpml_update_translatable_content('tax_' . $taxonomy, $updated['term_taxonomy_id'], 'en' );

echo "icl_translations table after updated EN translation:\n";
$icl_translations = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}icl_translations WHERE element_type = 'tax_" . $taxonomy . "'", ARRAY_A);
foreach ($icl_translations as $row) {
    print_r($row);
    echo "\n";
}
echo "End of icl_translations table.\n\n";

// Updating the Finnish category
$_POST['icl_tax_' . $taxonomy . '_language'] = 'fi'; // WPML
$updated = wp_update_term($fi_term_id, $taxonomy, array('name' => 'Category in Finnish, updated'));
echo "Updated FI: ".print_r($updated,true)."\n";

wpml_update_translatable_content('tax_' . $taxonomy, $updated['term_taxonomy_id'], 'fi' );

echo "icl_translations table after updated FI translation:\n";
$icl_translations = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}icl_translations WHERE element_type = 'tax_" . $taxonomy . "'", ARRAY_A);
foreach ($icl_translations as $row) {
    print_r($row);
    echo "\n";
}
echo "End of icl_translations table.\n\n";
echo "******** END OF TEST RUN ********\n";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment