rl, $lang_info ) { return $url; } function wp_get_attachment_url( $url, $post_id ) { global $sitepress; return $sitepress->convert_url( $url ); } function icl_ls_languages( $w_active_languages ) { static $doing_it = false; if ( is_attachment() && ! $doing_it ) { $doing_it = true; // Always include missing languages. $w_active_languages = icl_get_languages( 'skip_missing=0' ); $doing_it = false; } return $w_active_languages; } function get_post_metadata( $value, $object_id, $meta_key, $single ) { if ( $meta_key == '_thumbnail_id' ) { global $wpdb; $thumbnail_prepared = $wpdb->prepare( "SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = %s", array( $object_id, $meta_key, ) ); $thumbnail = $wpdb->get_var( $thumbnail_prepared ); if ( $thumbnail == null ) { // see if it's available in the original language. $post_type_prepared = $wpdb->prepare( "SELECT post_type FROM {$wpdb->posts} WHERE ID = %d", array( $object_id ) ); $post_type = $wpdb->get_var( $post_type_prepared ); $trid_prepared = $wpdb->prepare( "SELECT trid, source_language_code FROM {$wpdb->prefix}icl_translations WHERE element_id=%d AND element_type = %s", array( $object_id, 'post_' . $post_type, ) ); $trid = $wpdb->get_row( $trid_prepared ); if ( $trid ) { global $sitepress; $translations = $sitepress->get_element_translations( $trid->trid, 'post_' . $post_type ); if ( isset( $translations[ $trid->source_language_code ] ) ) { $translation = $translations[ $trid->source_language_code ]; // see if the original has a thumbnail. $thumbnail_prepared = $wpdb->prepare( "SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = %s", array( $translation->element_id, $meta_key, ) ); $thumbnail = $wpdb->get_var( $thumbnail_prepared ); if ( $thumbnail ) { $value = $thumbnail; } } } } else { $value = $thumbnail; } } return $value; } /** * @param string $menu_id */ public function menu( $menu_id ) { if ( 'WPML' !== $menu_id ) { return; } $menu_label = __( 'Media Translation', 'wpml-media' ); $menu = array(); $menu['order'] = 600; $menu['page_title'] = $menu_label; $menu['menu_title'] = $menu_label; $menu['capability'] = 'edit_others_posts'; $menu['menu_slug'] = 'wpml-media'; $menu['function'] = array( $this, 'menu_content' ); do_action( 'wpml_admin_menu_register_item', $menu ); } public function menu_content() { $menus = $this->menus_factory->create(); $menus->display(); } /** * @param $ids * @param $target_language * * @return array|string */ public function translate_attachment_ids( $ids, $target_language ) { global $sitepress; $return_string = false; if ( ! is_array( $ids ) ) { $attachment_ids = explode( ',', $ids ); $return_string = true; } $translated_ids = array(); if ( ! empty( $attachment_ids ) ) { foreach ( $attachment_ids as $attachment_id ) { // Fallback to the original ID $translated_id = $attachment_id; // Find the ID translation $trid = $sitepress->get_element_trid( $attachment_id, 'post_attachment' ); if ( $trid ) { $id_translations = $sitepress->get_element_translations( $trid, 'post_attachment', false, true ); foreach ( $id_translations as $language_code => $id_translation ) { if ( $language_code == $target_language ) { $translated_id = $id_translation->element_id; break; } } } $translated_ids[] = $translated_id; } } if ( $return_string ) { return implode( ',', $translated_ids ); } return $translated_ids; } /** * Update query for media-upload.php page. * * @param object $query WP_Query */ public function filter_media_upload_items( $query ) { $current_lang = $this->sitepress->get_current_language(); $ids = icl_cache_get( '_media_upload_attachments' . $current_lang ); if ( false === $ids ) { $tbl = $this->wpdb->prefix . 'icl_translations'; $db_query = " SELECT posts.ID FROM {$this->wpdb->posts} as posts, $tbl as icl_translations WHERE posts.post_type = 'attachment' AND icl_translations.element_id = posts.ID AND icl_translations.language_code = %s "; $posts = $this->wpdb->get_results( $this->wpdb->prepare( $db_query, $current_lang ) ); $ids = array(); if ( ! empty( $posts ) ) { foreach ( $posts as $post ) { $ids[] = absint( $post->ID ); } } icl_cache_set( '_media_upload_attachments' . $current_lang, $ids ); } $query->set( 'post__in', $ids ); } }