private static function upgrade_2_0() { global $wpdb; global $sitepress; //Check if the old options are set and in case move them to the new plugin settings, then delete the old ones $old_starting_help = get_option('_wpml_media_starting_help'); if ($old_starting_help) { WPML_Media::update_setting('starting_help', $old_starting_help); delete_option('_wpml_media_starting_help'); } //Create translated media $target_language = $sitepress->get_default_language(); $attachment_ids_prepared = $wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_type = %s", 'attachment'); $attachment_ids = $wpdb->get_col($attachment_ids_prepared); //Let's first set the language of all images in default languages foreach ($attachment_ids as $attachment_id) { $wpml_media_lang = get_post_meta($attachment_id, 'wpml_media_lang', true); $wpml_media_duplicate_of = get_post_meta($attachment_id, 'wpml_media_duplicate_of', true); if (!$wpml_media_duplicate_of && (!$wpml_media_lang || $wpml_media_lang == $target_language)) { $trid = $sitepress->get_element_trid($attachment_id, 'post_attachment'); if ($trid) { //Since trid exists, get the language from there $target_language = $sitepress->get_language_for_element($attachment_id, 'post_attachment'); } $sitepress->set_element_language_details($attachment_id, 'post_attachment', $trid, $target_language); } } //Then all the translations foreach ($attachment_ids as $attachment_id) { $wpml_media_lang = get_post_meta($attachment_id, 'wpml_media_lang', true); $wpml_media_duplicate_of = get_post_meta($attachment_id, 'wpml_media_duplicate_of', true); if ($wpml_media_duplicate_of) { $source_language = null; $trid = $sitepress->get_element_trid($wpml_media_duplicate_of, 'post_attachment'); $source_language = false; if ($trid) { //Get the source language of the attachment, just in case is from a language different than the default $source_language = $sitepress->get_language_for_element($wpml_media_duplicate_of, 'post_attachment'); //Fix bug on 1.6, where duplicated images are set to the default language if ($wpml_media_lang == $source_language) { $wpml_media_lang = false; $attachment = get_post($attachment_id); if ($attachment->post_parent) { $parent_post = get_post($attachment->post_parent); $post_parent_language = $sitepress->get_language_for_element($parent_post->ID, 'post_' . $parent_post->post_type); if ($post_parent_language) { $wpml_media_lang = $post_parent_language; } } if (!$wpml_media_lang) { //Trash orphan image wp_delete_attachment($attachment_id); } } } if ($wpml_media_lang) { $sitepress->set_element_language_details($attachment_id, 'post_attachment', $trid, $wpml_media_lang, $target_language, $source_language); } } } //Remove old media translation meta //Remove both meta just in case $attachment_ids = $wpdb->get_col($attachment_ids_prepared); foreach ($attachment_ids as $attachment_id) { delete_post_meta($attachment_id, 'wpml_media_duplicate_of'); delete_post_meta($attachment_id, 'wpml_media_lang'); } //Featured images WPML_Media::duplicate_featured_images(); }