| @@ -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,9 +38,8 @@ | ||
| 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 | 45 | if ( ! empty( $_GET['new_lang'] ) && $lang = $this->model->get_language( sanitize_key( $_GET['new_lang'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| @@ -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 | |
| @@ -218,9 +194,8 @@ | ||
| 218 | 194 | * |
| 219 | 195 | * @since 0.1 |
| 220 | 196 | * |
| 221 | 197 | * @param int $post_id |
| 222 | - * @return void | |
| 223 | 198 | */ |
| 224 | 199 | public function delete_post( $post_id ) { |
| 225 | 200 | if ( ! wp_is_post_revision( $post_id ) ) { |
| 226 | 201 | $this->model->post->delete_translation( $post_id ); |
| @@ -269,34 +244,29 @@ | ||
| 269 | 244 | * @return int Attachment id of the translated media. |
| 270 | 245 | */ |
| 271 | 246 | public function create_media_translation( $post_id, $lang ) { |
| 272 | 247 | if ( empty( $post_id ) ) { |
| 273 | - return 0; | |
| 248 | + return $post_id; | |
| 274 | 249 | } |
| 275 | 250 | |
| 276 | - $post = get_post( $post_id, ARRAY_A ); | |
| 251 | + $post = get_post( $post_id ); | |
| 277 | 252 | |
| 278 | 253 | if ( empty( $post ) ) { |
| 279 | - return 0; | |
| 254 | + return $post; | |
| 280 | 255 | } |
| 281 | 256 | |
| 282 | 257 | $lang = $this->model->get_language( $lang ); // Make sure we get a valid language slug. |
| 283 | 258 | |
| 284 | - if ( empty( $lang ) ) { | |
| 285 | - return 0; | |
| 286 | - } | |
| 287 | - | |
| 288 | 259 | // Create a new attachment ( translate attachment parent if exists ). |
| 289 | 260 | 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 ) ); | |
| 261 | + $post->ID = null; // Will force the creation | |
| 262 | + $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. | |
| 264 | + $tr_id = wp_insert_attachment( wp_slash( (array) $post ) ); | |
| 294 | 265 | remove_filter( 'pll_enable_duplicate_media', '__return_false', 99 ); // Restore automatic duplicate at upload. |
| 295 | 266 | |
| 296 | 267 | // Copy metadata. |
| 297 | - $data = wp_get_attachment_metadata( $post_id, true ); // Unfiltered. | |
| 298 | - if ( is_array( $data ) ) { | |
| 268 | + if ( $data = wp_get_attachment_metadata( $post_id, true ) ) { // Unfiltered. | |
| 299 | 269 | wp_update_attachment_metadata( $tr_id, wp_slash( $data ) ); // Directly uses update_post_meta, so expects slashed. |
| 300 | 270 | } |
| 301 | 271 | |
| 302 | 272 | // Copy attached file. |