Created
January 6, 2013 11:56
-
-
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/ .
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ******** |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A revised code for the second part of the test case (line 47 forwards):