red ); if ( $row && $row->trid && ( $row->source_language_code == null || $row->source_language_code == "" ) ) { update_post_meta( $post->ID, '_wpml_media_featured', 1 ); $translations = $sitepress->get_element_translations( $row->trid, 'post_' . $post->post_type ); foreach ( $translations as $translation ) { if ( $translation->element_id != $post->ID ) { $translation_thumbnail_id = get_post_meta( $translation->element_id, '_thumbnail_id', true ); if( empty( $translation_thumbnail_id ) ){ if ( ! in_array( $translation->element_id, array_keys( $thumbnails ) ) ) { // translation doesn't have a featured image $t_thumbnail_id = icl_object_id( $thumbnails[ $post->ID ], 'attachment', false, $translation->language_code ); if ( $t_thumbnail_id == null ) { $dup_att_id = self::create_duplicate_attachment( $thumbnails[ $post->ID ], $translation->element_id, $translation->language_code ); $t_thumbnail_id = $dup_att_id; } if ( $t_thumbnail_id != null ) { update_post_meta( $translation->element_id, '_thumbnail_id', $t_thumbnail_id ); } } elseif ( $thumbnails[ $post->ID ] ) { update_post_meta( $translation->element_id, '_thumbnail_id', $thumbnails[ $post->ID ] ); } } //Double check that there is a _thumbnail_id set and in case update _wpml_media_featured if ( get_post_meta( $translation->element_id, '_thumbnail_id', true ) ) { update_post_meta( $translation->element_id, '_wpml_media_featured', 1 ); } } } } } public function batch_duplicate_featured_images() { $limit = 10; $response = array(); $found = $this->duplicate_featured_images( $limit ); $response['left'] = max( $found - $limit, 0 ); if ( $response['left'] ) { $response['message'] = sprintf( __( 'Duplicating featured images. %d left', 'sitepress' ), $response['left'] ); } else { $response['message'] = sprintf( __( 'Duplicating featured images: done!', 'sitepress' ), $response['left'] ); } echo wp_json_encode( $response ); exit; } public function batch_duplicate_media() { $limit = 10; $response = array(); $attachments_prepared = $this->wpdb->prepare( " SELECT SQL_CALC_FOUND_ROWS p1.ID, p1.post_parent FROM {$this->wpdb->posts} p1 WHERE post_type = %s AND ID NOT IN (SELECT post_id FROM {$this->wpdb->postmeta} WHERE meta_key = %s) ORDER BY p1.ID ASC LIMIT %d", array( 'attachment', 'wpml_media_processed', $limit ) ); $attachments = $this->wpdb->get_results( $attachments_prepared ); $found = $this->wpdb->get_var( "SELECT FOUND_ROWS()" ); if ( $attachments ) { foreach ( $attachments as $attachment ) { $this->create_duplicated_media( $attachment ); } } $response['left'] = max( $found - $limit, 0 ); if ( $response['left'] ) { $response['message'] = sprintf( __( 'Duplicating media. %d left', 'sitepress' ), $response['left'] ); } else { $response['message'] = sprintf( __( 'Duplicating media: done!', 'sitepress' ), $response['left'] ); } echo wp_json_encode( $response ); exit; } private function get_batch_translate_limit( $active_languages ) { global $sitepress; $limit = $sitepress->get_wp_api()->constant( 'WPML_MEDIA_BATCH_LIMIT' ); $limit = ! $limit ? floor( 10 / max( $active_languages - 1, 1 ) ) : $limit; return max( $limit, 1 ); } public function batch_translate_media() { $response = array(); $active_languages = count( $this->sitepress->get_active_languages() ); $limit = $this->get_batch_translate_limit( $active_languages ); $sql = " SELECT SQL_CALC_FOUND_ROWS p1.ID, p1.post_parent FROM {$this->wpdb->prefix}icl_translations t INNER JOIN {$this->wpdb->posts} p1 ON t.element_id = p1.ID LEFT JOIN {$this->wpdb->prefix}icl_translations tt ON t.trid = tt.trid WHERE t.element_type = 'post_attachment' AND t.source_language_code IS null GROUP BY p1.ID, p1.post_parent HAVING count(tt.language_code) < %d LIMIT %d "; $sql_prepared = $this->wpdb->prepare( $sql, array( $active_languages, $limit ) ); $attachments = $this->wpdb->get_results( $sql_prepared ); $found = $this->wpdb->get_var( "SELECT FOUND_ROWS()" ); if ( $attachments ) { foreach ( $attachments as $attachment ) { $lang = $this->sitepress->get_element_language_details( $attachment->ID, 'post_attachment' ); $this->translate_attachments( $attachment->ID, $lang->language_code, true ); } } $response['left'] = max( $found - $limit, 0 ); if ( $response['left'] ) { $response['message'] = sprintf( esc_html__( 'Translating media. %d left', 'sitepress' ), $response['left'] ); } else { $response['message'] = sprintf( esc_html__( 'Translating media: done!', 'sitepress' ), $response['left'] ); } wp_send_json( $response ); } public function batch_set_initial_language() { $default_language = $this->sitepress->get_default_language(); $limit = 10; $response = array(); $attachments_prepared = $this->wpdb->prepare( " SELECT SQL_CALC_FOUND_ROWS ID FROM {$this->wpdb->posts} WHERE post_type = %s AND ID NOT IN (SELECT element_id FROM {$this->wpdb->prefix}icl_translations WHERE element_type=%s) LIMIT %d", array( 'attachment', 'post_attachment', $limit ) ); $attachments = $this->wpdb->get_col( $attachments_prepared ); $found = $this->wpdb->get_var( "SELECT FOUND_ROWS()" ); foreach ( $attachments as $attachment_id ) { $this->sitepress->set_element_language_details( $attachment_id, 'post_attachment', false, $default_language ); } $response['left'] = max( $found - $limit, 0 ); if ( $response['left'] ) { $response['message'] = sprintf( __( 'Setting language to media. %d left', 'sitepress' ), $response['left'] ); } else { $response['message'] = sprintf( __( 'Setting language to media: done!', 'sitepress' ), $response['left'] ); } echo wp_json_encode( $response ); exit; } function batch_scan_prepare() { $response = array(); $this->wpdb->delete( $this->wpdb->postmeta, array( 'meta_key' => 'wpml_media_processed' ) ); $response['message'] = __( 'Started...', 'sitepress' ); echo wp_json_encode( $response ); exit; } public function batch_mark_processed() { $response = array(); $attachments_prepared = $this->wpdb->prepare( "SELECT ID FROM {$this->wpdb->posts} WHERE post_type=%s", array( 'attachment' ) ); $attachments = $this->wpdb->get_col( $attachments_prepared ); foreach ( $attachments as $attachment_id ) { $find_meta_prepared = $this->wpdb->prepare( "SELECT count(post_id) FROM {$this->wpdb->postmeta} WHERE meta_key=%s AND post_id = %d", array( 'wpml_media_processed', $attachment_id ) ); $meta_exists = $this->wpdb->get_var( $find_meta_prepared ); if ( $meta_exists ) { $this->wpdb->update( $this->wpdb->postmeta, array( 'meta_value' => 1 ), array( 'meta_key' => 'wpml_media_processed', 'post_id' => $attachment_id ) ); } else { $this->wpdb->insert( $this->wpdb->postmeta, array( 'meta_value' => 1, 'post_id' => $attachment_id, 'meta_key' => 'wpml_media_processed' ) ); } } $settings = get_option( '_wpml_media' ); $settings[ 'starting_help' ] = 1; update_option( '_wpml_media', $settings ); $response['message'] = __( 'Done!', 'sitepress' ); echo wp_json_encode( $response ); exit; } public function create_duplicated_media( $attachment ) { static $parents_processed = array(); if ( $attachment->post_parent && ! in_array( $attachment->post_parent, $parents_processed ) ) { // see if we have translations. $post_type_prepared = $this->wpdb->prepare( "SELECT post_type FROM {$this->wpdb->posts} WHERE ID = %d", array( $attachment->post_parent ) ); $post_type = $this->wpdb->get_var( $post_type_prepared ); $trid_prepared = $this->wpdb->prepare( "SELECT trid FROM {$this->wpdb->prefix}icl_translations WHERE element_id=%d AND element_type = %s", array( $attachment->post_parent, 'post_' . $post_type ) ); $trid = $this->wpdb->get_var( $trid_prepared ); if ( $trid ) { update_post_meta( $attachment->post_parent, '_wpml_media_duplicate', 1 ); $attachments_prepared = $this->wpdb->prepare( "SELECT ID FROM {$this->wpdb->posts} WHERE post_type = %s AND post_parent = %d", array( 'attachment', $attachment->post_parent ) ); $attachments = $this->wpdb->get_col( $attachments_prepared ); $translations = $this->sitepress->get_element_translations( $trid, 'post_' . $post_type ); foreach ( $translations as $translation ) { if ( $translation->element_id && $translation->element_id != $attachment->post_parent ) { $attachments_in_translation_prepared = $this->wpdb->prepare( "SELECT ID FROM {$this->wpdb->posts} WHERE post_type = %s AND post_parent = %d", array( 'attachment', $translation->element_id ) ); $attachments_in_translation = $this->wpdb->get_col( $attachments_in_translation_prepared ); if ( sizeof( $attachments_in_translation ) == 0 ) { // only duplicate attachments if there a none already. foreach ( $attachments as $attachment_id ) { // duplicate the attachment self::create_duplicate_attachment( $attachment_id, $translation->element_id, $translation->language_code ); } } } } } $parents_processed[] = $attachment->post_parent; } else { // no parent - set to default language $target_language = $this->sitepress->get_default_language(); //Getting the trid and language, just in case image translation already exists $trid = $this->sitepress->get_element_trid( $attachment->ID, 'post_attachment' ); if ( $trid ) { $target_language = $this->sitepress->get_language_for_element( $attachment->ID, 'post_attachment' ); } $this->sitepress->set_element_language_details( $attachment->ID, 'post_attachment', $trid, $target_language ); } //Duplicate the post meta of the source element the translation $source_element_id = SitePress::get_original_element_id_by_trid( $trid ); if ( $source_element_id ) { $this->update_attachment_metadata( $source_element_id ); } update_post_meta( $attachment->ID, 'wpml_media_processed', 1 ); } function set_content_defaults_prepare() { $response = array( 'message' => __( 'Started...', 'sitepress' ) ); echo wp_json_encode( $response ); exit; } function set_content_defaults() { $always_translate_media = $_POST[ 'always_translate_media' ]; $duplicate_media = $_POST[ 'duplicate_media' ]; $duplicate_featured = $_POST[ 'duplicate_featured' ]; $content_defaults_option = array( 'always_translate_media' => $always_translate_media == 'true', 'duplicate_media' => $duplicate_media == 'true', 'duplicate_featured' => $duplicate_featured == 'true' ); $settings = get_option( '_wpml_media' ); $settings['new_content_settings'] = $content_defaults_option; update_option( '_wpml_media', $settings ); $response = array( 'result' => true, 'message' => __( 'Settings saved', 'sitepress' ) ); wp_send_json_success( $response ); } }