| @@ -1,36 +1,15 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | 3 | /** |
| 7 | - * Adds actions and filters related to languages when creating, updating or deleting posts. | |
| 8 | - * Actions and filters triggered when reading posts are handled separately. | |
| 4 | + * Adds actions and filters related to languages when creating, updating or deleting posts | |
| 5 | + * Actions a filters used when reaing posts are handled separately | |
| 9 | 6 | * |
| 10 | 7 | * @since 2.4 |
| 11 | 8 | */ |
| 12 | 9 | class PLL_CRUD_Posts { |
| 13 | - /** | |
| 14 | - * @var PLL_Model | |
| 15 | - */ | |
| 16 | - protected $model; | |
| 17 | 10 | |
| 18 | 11 | /** |
| 19 | - * Preferred language to assign to a new post. | |
| 20 | - * | |
| 21 | - * @var PLL_Language | |
| 22 | - */ | |
| 23 | - protected $pref_lang; | |
| 24 | - | |
| 25 | - /** | |
| 26 | - * Current language. | |
| 27 | - * | |
| 28 | - * @var PLL_Language | |
| 29 | - */ | |
| 30 | - protected $curlang; | |
| 31 | - | |
| 32 | - /** | |
| 33 | 12 | * Constructor |
| 34 | 13 | * |
| 35 | 14 | * @since 2.4 |
| 36 | 15 | * |
| @@ -59,16 +38,15 @@ | ||
| 59 | 38 | * |
| 60 | 39 | * @since 1.5 |
| 61 | 40 | * |
| 62 | 41 | * @param int $post_id |
| 63 | - * @return void | |
| 64 | 42 | */ |
| 65 | 43 | public function set_default_language( $post_id ) { |
| 66 | 44 | if ( ! $this->model->post->get_language( $post_id ) ) { |
| 67 | - 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'] ) ) { | |
| 68 | 46 | // Defined only on admin. |
| 69 | 47 | $this->model->post->set_language( $post_id, $lang ); |
| 70 | - } 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'] ) ) { | |
| 71 | 49 | // Testing $this->pref_lang makes this test pass only on admin. |
| 72 | 50 | $this->model->post->set_language( $post_id, $lang ); |
| 73 | 51 | } elseif ( ( $parent_id = wp_get_post_parent_id( $post_id ) ) && $parent_lang = $this->model->post->get_language( $parent_id ) ) { |
| 74 | 52 | $this->model->post->set_language( $post_id, $parent_lang ); |
| @@ -82,19 +60,18 @@ | ||
| 82 | 60 | } |
| 83 | 61 | } |
| 84 | 62 | |
| 85 | 63 | /** |
| 86 | - * Called when a post ( or page ) is saved, published or updated. | |
| 64 | + * Called when a post ( or page ) is saved, published or updated | |
| 87 | 65 | * |
| 88 | 66 | * @since 0.1 |
| 89 | - * @since 2.3 Does not save the language and translations anymore, unless the post has no language yet. | |
| 67 | + * @since 2.3 Does not save the language and translations anymore, unless the post has no language yet | |
| 90 | 68 | * |
| 91 | - * @param int $post_id Post id of the post being saved. | |
| 92 | - * @param WP_Post $post The post being saved. | |
| 93 | - * @return void | |
| 69 | + * @param int $post_id | |
| 70 | + * @param object $post | |
| 94 | 71 | */ |
| 95 | 72 | public function save_post( $post_id, $post ) { |
| 96 | - // Does nothing except on post types which are filterable. | |
| 73 | + // Does nothing except on post types which are filterable | |
| 97 | 74 | if ( $this->model->is_translated_post_type( $post->post_type ) ) { |
| 98 | 75 | if ( $id = wp_is_post_revision( $post_id ) ) { |
| 99 | 76 | $post_id = $id; |
| 100 | 77 | } |
| @@ -105,15 +82,15 @@ | ||
| 105 | 82 | $this->set_default_language( $post_id ); |
| 106 | 83 | } |
| 107 | 84 | |
| 108 | 85 | /** |
| 109 | - * Fires after the post language and translations are saved. | |
| 86 | + * Fires after the post language and translations are saved | |
| 110 | 87 | * |
| 111 | 88 | * @since 1.2 |
| 112 | 89 | * |
| 113 | - * @param int $post_id Post id. | |
| 114 | - * @param WP_Post $post Post object. | |
| 115 | - * @param int[] $translations The list of translations post ids. | |
| 90 | + * @param int $post_id Post id | |
| 91 | + * @param object $post Post object | |
| 92 | + * @param array $translations The list of translations post ids | |
| 116 | 93 | */ |
| 117 | 94 | do_action( 'pll_save_post', $post_id, $post, $this->model->post->get_translations( $post_id ) ); |
| 118 | 95 | } |
| 119 | 96 | } |
| @@ -118,17 +95,16 @@ | ||
| 118 | 95 | } |
| 119 | 96 | } |
| 120 | 97 | |
| 121 | 98 | /** |
| 122 | - * Makes sure that saved terms are in the right language (especially tags with same name in different languages). | |
| 99 | + * Make sure saved terms are in the right language (especially tags with same name in different languages) | |
| 123 | 100 | * |
| 124 | 101 | * @since 2.3 |
| 125 | 102 | * |
| 126 | - * @param int $object_id Object ID. | |
| 127 | - * @param WP_Term[] $terms An array of object terms. | |
| 128 | - * @param int[] $tt_ids An array of term taxonomy IDs. | |
| 129 | - * @param string $taxonomy Taxonomy slug. | |
| 130 | - * @return void | |
| 103 | + * @param int $object_id Object ID. | |
| 104 | + * @param array $terms An array of object terms. | |
| 105 | + * @param array $tt_ids An array of term taxonomy IDs. | |
| 106 | + * @param string $taxonomy Taxonomy slug. | |
| 131 | 107 | */ |
| 132 | 108 | public function set_object_terms( $object_id, $terms, $tt_ids, $taxonomy ) { |
| 133 | 109 | static $avoid_recursion; |
| 134 | 110 | |
| @@ -144,9 +120,8 @@ | ||
| 144 | 120 | } |
| 145 | 121 | |
| 146 | 122 | $term_ids = array_combine( $terms, $terms ); |
| 147 | 123 | $languages = array_map( array( $this->model->term, 'get_language' ), $term_ids ); |
| 148 | - $languages = array_filter( $languages ); // Remove terms without language. | |
| 149 | 124 | $languages = wp_list_pluck( $languages, 'slug' ); |
| 150 | 125 | $wrong_terms = array_diff( $languages, array( $lang->slug ) ); |
| 151 | 126 | |
| 152 | 127 | if ( ! empty( $wrong_terms ) ) { |
| @@ -218,9 +193,8 @@ | ||
| 218 | 193 | * |
| 219 | 194 | * @since 0.1 |
| 220 | 195 | * |
| 221 | 196 | * @param int $post_id |
| 222 | - * @return void | |
| 223 | 197 | */ |
| 224 | 198 | public function delete_post( $post_id ) { |
| 225 | 199 | if ( ! wp_is_post_revision( $post_id ) ) { |
| 226 | 200 | $this->model->post->delete_translation( $post_id ); |
| @@ -232,10 +206,10 @@ | ||
| 232 | 206 | * Thanks to Bruno "Aesqe" Babic and its plugin file gallery in which I took all the ideas for this function |
| 233 | 207 | * |
| 234 | 208 | * @since 0.9 |
| 235 | 209 | * |
| 236 | - * @param string $file Path to the file to delete. | |
| 237 | - * @return string Empty or unmodified path. | |
| 210 | + * @param string $file | |
| 211 | + * @return string unmodified $file | |
| 238 | 212 | */ |
| 239 | 213 | public function wp_delete_file( $file ) { |
| 240 | 214 | global $wpdb; |
| 241 | 215 | |
| @@ -249,12 +223,11 @@ | ||
| 249 | 223 | ) |
| 250 | 224 | ); |
| 251 | 225 | |
| 252 | 226 | if ( ! empty( $ids ) ) { |
| 253 | - // Regenerate intermediate sizes if it's an image ( since we could not prevent WP deleting them before ). | |
| 254 | - require_once ABSPATH . 'wp-admin/includes/image.php'; // In case the file is deleted outside admin. | |
| 255 | - wp_update_attachment_metadata( $ids[0], wp_slash( wp_generate_attachment_metadata( $ids[0], $file ) ) ); // Directly uses update_post_meta, so expects slashed. | |
| 256 | - 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 | |
| 257 | 230 | } |
| 258 | 231 | |
| 259 | 232 | return $file; |
| 260 | 233 | } |
| @@ -263,56 +236,43 @@ | ||
| 263 | 236 | * Creates a media translation |
| 264 | 237 | * |
| 265 | 238 | * @since 1.8 |
| 266 | 239 | * |
| 267 | - * @param int $post_id Original attachment id. | |
| 268 | - * @param string|object $lang New translation language. | |
| 269 | - * @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 | |
| 270 | 243 | */ |
| 271 | 244 | public function create_media_translation( $post_id, $lang ) { |
| 272 | - if ( empty( $post_id ) ) { | |
| 273 | - return 0; | |
| 274 | - } | |
| 245 | + $post = get_post( $post_id ); | |
| 275 | 246 | |
| 276 | - $post = get_post( $post_id, ARRAY_A ); | |
| 277 | - | |
| 278 | 247 | if ( empty( $post ) ) { |
| 279 | - return 0; | |
| 248 | + return $post; | |
| 280 | 249 | } |
| 281 | 250 | |
| 282 | - $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 | |
| 283 | 252 | |
| 284 | - if ( empty( $lang ) ) { | |
| 285 | - return 0; | |
| 286 | - } | |
| 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 | |
| 255 | + $post->ID = null; // Will force the creation | |
| 256 | + $post->post_parent = ( $post->post_parent && $tr_parent = $this->model->post->get_translation( $post->post_parent, $lang->slug ) ) ? $tr_parent : 0; | |
| 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 | |
| 287 | 260 | |
| 288 | - // Create a new attachment ( translate attachment parent if exists ). | |
| 289 | - add_filter( 'pll_enable_duplicate_media', '__return_false', 99 ); // Avoid a conflict with automatic duplicate at upload. | |
| 290 | - unset( $post['ID'] ); // Will force the creation. | |
| 291 | - $post['post_parent'] = ( $post['post_parent'] && $tr_parent = $this->model->post->get_translation( $post['post_parent'], $lang->slug ) ) ? $tr_parent : 0; | |
| 292 | - $post['tax_input'] = array( 'language' => array( $lang->slug ) ); // Assigns the language. | |
| 293 | - $tr_id = wp_insert_attachment( wp_slash( $post ) ); | |
| 294 | - remove_filter( 'pll_enable_duplicate_media', '__return_false', 99 ); // Restore automatic duplicate at upload. | |
| 295 | - | |
| 296 | - // Copy metadata. | |
| 297 | - $data = wp_get_attachment_metadata( $post_id, true ); // Unfiltered. | |
| 298 | - if ( is_array( $data ) ) { | |
| 299 | - 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 | + } | |
| 300 | 266 | } |
| 301 | 267 | |
| 302 | - // Copy attached file. | |
| 303 | - if ( $file = get_attached_file( $post_id, true ) ) { // Unfiltered. | |
| 304 | - update_attached_file( $tr_id, wp_slash( $file ) ); // Directly uses update_post_meta, so expects slashed. | |
| 305 | - } | |
| 268 | + $this->model->post->set_language( $tr_id, $lang ); | |
| 306 | 269 | |
| 307 | - // Copy alternative text. Direct use of the meta as there is no filtered wrapper to manipulate it. | |
| 308 | - if ( $text = get_post_meta( $post_id, '_wp_attachment_image_alt', true ) ) { | |
| 309 | - 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; | |
| 310 | 273 | } |
| 311 | 274 | |
| 312 | - $this->model->post->set_language( $tr_id, $lang ); | |
| 313 | - | |
| 314 | - $translations = $this->model->post->get_translations( $post_id ); | |
| 315 | 275 | $translations[ $lang->slug ] = $tr_id; |
| 316 | 276 | $this->model->post->save_translations( $tr_id, $translations ); |
| 317 | 277 | |
| 318 | 278 | /** |
| @@ -319,11 +279,11 @@ | ||
| 319 | 279 | * Fires after a media translation is created |
| 320 | 280 | * |
| 321 | 281 | * @since 1.6.4 |
| 322 | 282 | * |
| 323 | - * @param int $post_id Post id of the source media. | |
| 324 | - * @param int $tr_id Post id of the new media translation. | |
| 325 | - * @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 | |
| 326 | 286 | */ |
| 327 | 287 | do_action( 'pll_translate_media', $post_id, $tr_id, $lang->slug ); |
| 328 | 288 | return $tr_id; |
| 329 | 289 | } |