| @@ -1,8 +1,5 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | 3 | /** |
| 7 | 4 | * Adds actions and filters related to languages when creating, updating or deleting posts |
| 8 | 5 | * Actions a filters used when reaing posts are handled separately |
| @@ -44,12 +41,12 @@ | ||
| 44 | 41 | * @param int $post_id |
| 45 | 42 | */ |
| 46 | 43 | public function set_default_language( $post_id ) { |
| 47 | 44 | if ( ! $this->model->post->get_language( $post_id ) ) { |
| 48 | - if ( ! empty( $_GET['new_lang'] ) && $lang = $this->model->get_language( sanitize_key( $_GET['new_lang'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 45 | + if ( ! empty( $_GET['new_lang'] ) && $lang = $this->model->get_language( $_GET['new_lang'] ) ) { | |
| 49 | 46 | // Defined only on admin. |
| 50 | 47 | $this->model->post->set_language( $post_id, $lang ); |
| 51 | - } elseif ( ! isset( $this->pref_lang ) && ! empty( $_REQUEST['lang'] ) && $lang = $this->model->get_language( sanitize_key( $_REQUEST['lang'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 48 | + } elseif ( ! isset( $this->pref_lang ) && ! empty( $_REQUEST['lang'] ) && $lang = $this->model->get_language( $_REQUEST['lang'] ) ) { | |
| 52 | 49 | // Testing $this->pref_lang makes this test pass only on admin. |
| 53 | 50 | $this->model->post->set_language( $post_id, $lang ); |
| 54 | 51 | } elseif ( ( $parent_id = wp_get_post_parent_id( $post_id ) ) && $parent_lang = $this->model->post->get_language( $parent_id ) ) { |
| 55 | 52 | $this->model->post->set_language( $post_id, $parent_lang ); |
| @@ -123,9 +120,8 @@ | ||
| 123 | 120 | } |
| 124 | 121 | |
| 125 | 122 | $term_ids = array_combine( $terms, $terms ); |
| 126 | 123 | $languages = array_map( array( $this->model->term, 'get_language' ), $term_ids ); |
| 127 | - $languages = array_filter( $languages ); // Remove terms without language. | |
| 128 | 124 | $languages = wp_list_pluck( $languages, 'slug' ); |
| 129 | 125 | $wrong_terms = array_diff( $languages, array( $lang->slug ) ); |
| 130 | 126 | |
| 131 | 127 | if ( ! empty( $wrong_terms ) ) { |
| @@ -210,10 +206,10 @@ | ||
| 210 | 206 | * Thanks to Bruno "Aesqe" Babic and its plugin file gallery in which I took all the ideas for this function |
| 211 | 207 | * |
| 212 | 208 | * @since 0.9 |
| 213 | 209 | * |
| 214 | - * @param string $file Path to the file to delete. | |
| 215 | - * @return string Empty or unmodified path. | |
| 210 | + * @param string $file | |
| 211 | + * @return string unmodified $file | |
| 216 | 212 | */ |
| 217 | 213 | public function wp_delete_file( $file ) { |
| 218 | 214 | global $wpdb; |
| 219 | 215 | |
| @@ -227,12 +223,11 @@ | ||
| 227 | 223 | ) |
| 228 | 224 | ); |
| 229 | 225 | |
| 230 | 226 | if ( ! empty( $ids ) ) { |
| 231 | - // Regenerate intermediate sizes if it's an image ( since we could not prevent WP deleting them before ). | |
| 232 | - require_once ABSPATH . 'wp-admin/includes/image.php'; // In case the file is deleted outside admin. | |
| 233 | - wp_update_attachment_metadata( $ids[0], wp_slash( wp_generate_attachment_metadata( $ids[0], $file ) ) ); // Directly uses update_post_meta, so expects slashed. | |
| 234 | - return ''; // Prevent deleting the main file. | |
| 227 | + // Regenerate intermediate sizes if it's an image ( since we could not prevent WP deleting them before ) | |
| 228 | + wp_update_attachment_metadata( $ids[0], wp_generate_attachment_metadata( $ids[0], $file ) ); | |
| 229 | + return ''; // Prevent deleting the main file | |
| 235 | 230 | } |
| 236 | 231 | |
| 237 | 232 | return $file; |
| 238 | 233 | } |
| @@ -241,17 +236,13 @@ | ||
| 241 | 236 | * Creates a media translation |
| 242 | 237 | * |
| 243 | 238 | * @since 1.8 |
| 244 | 239 | * |
| 245 | - * @param int $post_id Original attachment id. | |
| 246 | - * @param string|object $lang New translation language. | |
| 247 | - * @return int Attachment id of the translated media. | |
| 240 | + * @param int $post_id | |
| 241 | + * @param string|object $lang | |
| 242 | + * @return int id of the translated media | |
| 248 | 243 | */ |
| 249 | 244 | public function create_media_translation( $post_id, $lang ) { |
| 250 | - if ( empty( $post_id ) ) { | |
| 251 | - return $post_id; | |
| 252 | - } | |
| 253 | - | |
| 254 | 245 | $post = get_post( $post_id ); |
| 255 | 246 | |
| 256 | 247 | if ( empty( $post ) ) { |
| 257 | 248 | return $post; |
| @@ -256,36 +247,32 @@ | ||
| 256 | 247 | if ( empty( $post ) ) { |
| 257 | 248 | return $post; |
| 258 | 249 | } |
| 259 | 250 | |
| 260 | - $lang = $this->model->get_language( $lang ); // Make sure we get a valid language slug. | |
| 251 | + $lang = $this->model->get_language( $lang ); // Make sure we get a valid language slug | |
| 261 | 252 | |
| 262 | - // Create a new attachment ( translate attachment parent if exists ). | |
| 263 | - add_filter( 'pll_enable_duplicate_media', '__return_false', 99 ); // Avoid a conflict with automatic duplicate at upload. | |
| 253 | + // Create a new attachment ( translate attachment parent if exists ) | |
| 254 | + add_filter( 'pll_enable_duplicate_media', '__return_false', 99 ); // Avoid a conflict with automatic duplicate at upload | |
| 264 | 255 | $post->ID = null; // Will force the creation |
| 265 | 256 | $post->post_parent = ( $post->post_parent && $tr_parent = $this->model->post->get_translation( $post->post_parent, $lang->slug ) ) ? $tr_parent : 0; |
| 266 | - $post->tax_input = array( 'language' => array( $lang->slug ) ); // Assigns the language. | |
| 267 | - $tr_id = wp_insert_attachment( wp_slash( (array) $post ) ); | |
| 268 | - remove_filter( 'pll_enable_duplicate_media', '__return_false', 99 ); // Restore automatic duplicate at upload. | |
| 257 | + $post->tax_input = array( 'language' => array( $lang->slug ) ); // Assigns the language | |
| 258 | + $tr_id = wp_insert_attachment( $post ); | |
| 259 | + remove_filter( 'pll_enable_duplicate_media', '__return_false', 99 ); // Restore automatic duplicate at upload | |
| 269 | 260 | |
| 270 | - // Copy metadata. | |
| 271 | - if ( $data = wp_get_attachment_metadata( $post_id, true ) ) { // Unfiltered. | |
| 272 | - wp_update_attachment_metadata( $tr_id, wp_slash( $data ) ); // Directly uses update_post_meta, so expects slashed. | |
| 261 | + // Copy metadata, attached file and alternative text | |
| 262 | + foreach ( array( '_wp_attachment_metadata', '_wp_attached_file', '_wp_attachment_image_alt' ) as $key ) { | |
| 263 | + if ( $meta = get_post_meta( $post_id, $key, true ) ) { | |
| 264 | + add_post_meta( $tr_id, $key, $meta ); | |
| 265 | + } | |
| 273 | 266 | } |
| 274 | 267 | |
| 275 | - // Copy attached file. | |
| 276 | - if ( $file = get_attached_file( $post_id, true ) ) { // Unfiltered. | |
| 277 | - update_attached_file( $tr_id, wp_slash( $file ) ); // Directly uses update_post_meta, so expects slashed. | |
| 278 | - } | |
| 268 | + $this->model->post->set_language( $tr_id, $lang ); | |
| 279 | 269 | |
| 280 | - // Copy alternative text. Direct use of the meta as there is no filtered wrapper to manipulate it. | |
| 281 | - if ( $text = get_post_meta( $post_id, '_wp_attachment_image_alt', true ) ) { | |
| 282 | - add_post_meta( $tr_id, '_wp_attachment_image_alt', wp_slash( $text ) ); | |
| 270 | + $translations = $this->model->post->get_translations( $post_id ); | |
| 271 | + if ( ! $translations && $src_lang = $this->model->post->get_language( $post_id ) ) { | |
| 272 | + $translations[ $src_lang->slug ] = $post_id; | |
| 283 | 273 | } |
| 284 | 274 | |
| 285 | - $this->model->post->set_language( $tr_id, $lang ); | |
| 286 | - | |
| 287 | - $translations = $this->model->post->get_translations( $post_id ); | |
| 288 | 275 | $translations[ $lang->slug ] = $tr_id; |
| 289 | 276 | $this->model->post->save_translations( $tr_id, $translations ); |
| 290 | 277 | |
| 291 | 278 | /** |
| @@ -292,11 +279,11 @@ | ||
| 292 | 279 | * Fires after a media translation is created |
| 293 | 280 | * |
| 294 | 281 | * @since 1.6.4 |
| 295 | 282 | * |
| 296 | - * @param int $post_id Post id of the source media. | |
| 297 | - * @param int $tr_id Post id of the new media translation. | |
| 298 | - * @param string $slug Language code of the new translation. | |
| 283 | + * @param int $post_id post id of the source media | |
| 284 | + * @param int $tr_id post id of the new media translation | |
| 285 | + * @param string $slug language code of the new translation | |
| 299 | 286 | */ |
| 300 | 287 | do_action( 'pll_translate_media', $post_id, $tr_id, $lang->slug ); |
| 301 | 288 | return $tr_id; |
| 302 | 289 | } |