PluginProbe
Polylang / 2.6.2
Polylang v2.6.2
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
← All changes | include/crud-posts.php +21 -26 2.72.6.2 View file →
@@ -120,9 +120,8 @@
120 120 }
121 121
122 122 $term_ids = array_combine( $terms, $terms );
123 123 $languages = array_map( array( $this->model->term, 'get_language' ), $term_ids );
124 - $languages = array_filter( $languages ); // Remove terms without language.
125 124 $languages = wp_list_pluck( $languages, 'slug' );
126 125 $wrong_terms = array_diff( $languages, array( $lang->slug ) );
127 126
128 127 if ( ! empty( $wrong_terms ) ) {
@@ -226,9 +225,9 @@
226 225
227 226 if ( ! empty( $ids ) ) {
228 227 // Regenerate intermediate sizes if it's an image ( since we could not prevent WP deleting them before ).
229 228 require_once ABSPATH . 'wp-admin/includes/image.php'; // In case the file is deleted outside admin.
230 - wp_update_attachment_metadata( $ids[0], wp_slash( wp_generate_attachment_metadata( $ids[0], $file ) ) ); // Directly uses update_post_meta, so expects slashed.
229 + wp_update_attachment_metadata( $ids[0], wp_generate_attachment_metadata( $ids[0], $file ) );
231 230 return ''; // Prevent deleting the main file.
232 231 }
233 232
234 233 return $file;
@@ -238,11 +237,11 @@
238 237 * Creates a media translation
239 238 *
240 239 * @since 1.8
241 240 *
242 - * @param int $post_id Original attachment id.
243 - * @param string|object $lang New translation language.
244 - * @return int Attachment id of the translated media.
241 + * @param int $post_id
242 + * @param string|object $lang
243 + * @return int id of the translated media
245 244 */
246 245 public function create_media_translation( $post_id, $lang ) {
247 246 if ( empty( $post_id ) ) {
248 247 return $post_id;
@@ -253,36 +252,32 @@
253 252 if ( empty( $post ) ) {
254 253 return $post;
255 254 }
256 255
257 - $lang = $this->model->get_language( $lang ); // Make sure we get a valid language slug.
256 + $lang = $this->model->get_language( $lang ); // Make sure we get a valid language slug
258 257
259 - // Create a new attachment ( translate attachment parent if exists ).
260 - add_filter( 'pll_enable_duplicate_media', '__return_false', 99 ); // Avoid a conflict with automatic duplicate at upload.
258 + // Create a new attachment ( translate attachment parent if exists )
259 + add_filter( 'pll_enable_duplicate_media', '__return_false', 99 ); // Avoid a conflict with automatic duplicate at upload
261 260 $post->ID = null; // Will force the creation
262 261 $post->post_parent = ( $post->post_parent && $tr_parent = $this->model->post->get_translation( $post->post_parent, $lang->slug ) ) ? $tr_parent : 0;
263 - $post->tax_input = array( 'language' => array( $lang->slug ) ); // Assigns the language.
262 + $post->tax_input = array( 'language' => array( $lang->slug ) ); // Assigns the language
264 263 $tr_id = wp_insert_attachment( wp_slash( (array) $post ) );
265 - remove_filter( 'pll_enable_duplicate_media', '__return_false', 99 ); // Restore automatic duplicate at upload.
264 + remove_filter( 'pll_enable_duplicate_media', '__return_false', 99 ); // Restore automatic duplicate at upload
266 265
267 - // Copy metadata.
268 - if ( $data = wp_get_attachment_metadata( $post_id, true ) ) { // Unfiltered.
269 - wp_update_attachment_metadata( $tr_id, wp_slash( $data ) ); // Directly uses update_post_meta, so expects slashed.
266 + // Copy metadata, attached file and alternative text
267 + foreach ( array( '_wp_attachment_metadata', '_wp_attached_file', '_wp_attachment_image_alt' ) as $key ) {
268 + if ( $meta = get_post_meta( $post_id, $key, true ) ) {
269 + add_post_meta( $tr_id, $key, wp_slash( $meta ) );
270 + }
270 271 }
271 272
272 - // Copy attached file.
273 - if ( $file = get_attached_file( $post_id, true ) ) { // Unfiltered.
274 - update_attached_file( $tr_id, wp_slash( $file ) ); // Directly uses update_post_meta, so expects slashed.
275 - }
273 + $this->model->post->set_language( $tr_id, $lang );
276 274
277 - // Copy alternative text. Direct use of the meta as there is no filtered wrapper to manipulate it.
278 - if ( $text = get_post_meta( $post_id, '_wp_attachment_image_alt', true ) ) {
279 - add_post_meta( $tr_id, '_wp_attachment_image_alt', wp_slash( $text ) );
275 + $translations = $this->model->post->get_translations( $post_id );
276 + if ( ! $translations && $src_lang = $this->model->post->get_language( $post_id ) ) {
277 + $translations[ $src_lang->slug ] = $post_id;
280 278 }
281 279
282 - $this->model->post->set_language( $tr_id, $lang );
283 -
284 - $translations = $this->model->post->get_translations( $post_id );
285 280 $translations[ $lang->slug ] = $tr_id;
286 281 $this->model->post->save_translations( $tr_id, $translations );
287 282
288 283 /**
@@ -289,11 +284,11 @@
289 284 * Fires after a media translation is created
290 285 *
291 286 * @since 1.6.4
292 287 *
293 - * @param int $post_id Post id of the source media.
294 - * @param int $tr_id Post id of the new media translation.
295 - * @param string $slug Language code of the new translation.
288 + * @param int $post_id post id of the source media
289 + * @param int $tr_id post id of the new media translation
290 + * @param string $slug language code of the new translation
296 291 */
297 292 do_action( 'pll_translate_media', $post_id, $tr_id, $lang->slug );
298 293 return $tr_id;
299 294 }