| @@ -3,34 +3,16 @@ | ||
| 3 | 3 | * @package Polylang |
| 4 | 4 | */ |
| 5 | 5 | |
| 6 | 6 | /** |
| 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. | |
| 7 | + * Adds actions and filters related to languages when creating, updating or deleting posts | |
| 8 | + * Actions a filters used when reaing posts are handled separately | |
| 9 | 9 | * |
| 10 | 10 | * @since 2.4 |
| 11 | 11 | */ |
| 12 | 12 | class PLL_CRUD_Posts { |
| 13 | - /** | |
| 14 | - * @var PLL_Model | |
| 15 | - */ | |
| 16 | - protected $model; | |
| 17 | 13 | |
| 18 | 14 | /** |
| 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 | 15 | * Constructor |
| 34 | 16 | * |
| 35 | 17 | * @since 2.4 |
| 36 | 18 | * |
| @@ -59,9 +41,8 @@ | ||
| 59 | 41 | * |
| 60 | 42 | * @since 1.5 |
| 61 | 43 | * |
| 62 | 44 | * @param int $post_id |
| 63 | - * @return void | |
| 64 | 45 | */ |
| 65 | 46 | public function set_default_language( $post_id ) { |
| 66 | 47 | if ( ! $this->model->post->get_language( $post_id ) ) { |
| 67 | 48 | if ( ! empty( $_GET['new_lang'] ) && $lang = $this->model->get_language( sanitize_key( $_GET['new_lang'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| @@ -82,19 +63,18 @@ | ||
| 82 | 63 | } |
| 83 | 64 | } |
| 84 | 65 | |
| 85 | 66 | /** |
| 86 | - * Called when a post ( or page ) is saved, published or updated. | |
| 67 | + * Called when a post ( or page ) is saved, published or updated | |
| 87 | 68 | * |
| 88 | 69 | * @since 0.1 |
| 89 | - * @since 2.3 Does not save the language and translations anymore, unless the post has no language yet. | |
| 70 | + * @since 2.3 Does not save the language and translations anymore, unless the post has no language yet | |
| 90 | 71 | * |
| 91 | - * @param int $post_id Post id of the post being saved. | |
| 92 | - * @param WP_Post $post The post being saved. | |
| 93 | - * @return void | |
| 72 | + * @param int $post_id | |
| 73 | + * @param object $post | |
| 94 | 74 | */ |
| 95 | 75 | public function save_post( $post_id, $post ) { |
| 96 | - // Does nothing except on post types which are filterable. | |
| 76 | + // Does nothing except on post types which are filterable | |
| 97 | 77 | if ( $this->model->is_translated_post_type( $post->post_type ) ) { |
| 98 | 78 | if ( $id = wp_is_post_revision( $post_id ) ) { |
| 99 | 79 | $post_id = $id; |
| 100 | 80 | } |
| @@ -105,15 +85,15 @@ | ||
| 105 | 85 | $this->set_default_language( $post_id ); |
| 106 | 86 | } |
| 107 | 87 | |
| 108 | 88 | /** |
| 109 | - * Fires after the post language and translations are saved. | |
| 89 | + * Fires after the post language and translations are saved | |
| 110 | 90 | * |
| 111 | 91 | * @since 1.2 |
| 112 | 92 | * |
| 113 | - * @param int $post_id Post id. | |
| 114 | - * @param WP_Post $post Post object. | |
| 115 | - * @param int[] $translations The list of translations post ids. | |
| 93 | + * @param int $post_id Post id | |
| 94 | + * @param object $post Post object | |
| 95 | + * @param array $translations The list of translations post ids | |
| 116 | 96 | */ |
| 117 | 97 | do_action( 'pll_save_post', $post_id, $post, $this->model->post->get_translations( $post_id ) ); |
| 118 | 98 | } |
| 119 | 99 | } |
| @@ -118,17 +98,16 @@ | ||
| 118 | 98 | } |
| 119 | 99 | } |
| 120 | 100 | |
| 121 | 101 | /** |
| 122 | - * Makes sure that saved terms are in the right language (especially tags with same name in different languages). | |
| 102 | + * Make sure saved terms are in the right language (especially tags with same name in different languages) | |
| 123 | 103 | * |
| 124 | 104 | * @since 2.3 |
| 125 | 105 | * |
| 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 | |
| 106 | + * @param int $object_id Object ID. | |
| 107 | + * @param array $terms An array of object terms. | |
| 108 | + * @param array $tt_ids An array of term taxonomy IDs. | |
| 109 | + * @param string $taxonomy Taxonomy slug. | |
| 131 | 110 | */ |
| 132 | 111 | public function set_object_terms( $object_id, $terms, $tt_ids, $taxonomy ) { |
| 133 | 112 | static $avoid_recursion; |
| 134 | 113 | |
| @@ -218,9 +197,8 @@ | ||
| 218 | 197 | * |
| 219 | 198 | * @since 0.1 |
| 220 | 199 | * |
| 221 | 200 | * @param int $post_id |
| 222 | - * @return void | |
| 223 | 201 | */ |
| 224 | 202 | public function delete_post( $post_id ) { |
| 225 | 203 | if ( ! wp_is_post_revision( $post_id ) ) { |
| 226 | 204 | $this->model->post->delete_translation( $post_id ); |
| @@ -269,34 +247,29 @@ | ||
| 269 | 247 | * @return int Attachment id of the translated media. |
| 270 | 248 | */ |
| 271 | 249 | public function create_media_translation( $post_id, $lang ) { |
| 272 | 250 | if ( empty( $post_id ) ) { |
| 273 | - return 0; | |
| 251 | + return $post_id; | |
| 274 | 252 | } |
| 275 | 253 | |
| 276 | - $post = get_post( $post_id, ARRAY_A ); | |
| 254 | + $post = get_post( $post_id ); | |
| 277 | 255 | |
| 278 | 256 | if ( empty( $post ) ) { |
| 279 | - return 0; | |
| 257 | + return $post; | |
| 280 | 258 | } |
| 281 | 259 | |
| 282 | 260 | $lang = $this->model->get_language( $lang ); // Make sure we get a valid language slug. |
| 283 | 261 | |
| 284 | - if ( empty( $lang ) ) { | |
| 285 | - return 0; | |
| 286 | - } | |
| 287 | - | |
| 288 | 262 | // Create a new attachment ( translate attachment parent if exists ). |
| 289 | 263 | 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 ) ); | |
| 264 | + $post->ID = null; // Will force the creation | |
| 265 | + $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 ) ); | |
| 294 | 268 | remove_filter( 'pll_enable_duplicate_media', '__return_false', 99 ); // Restore automatic duplicate at upload. |
| 295 | 269 | |
| 296 | 270 | // Copy metadata. |
| 297 | - $data = wp_get_attachment_metadata( $post_id, true ); // Unfiltered. | |
| 298 | - if ( is_array( $data ) ) { | |
| 271 | + if ( $data = wp_get_attachment_metadata( $post_id, true ) ) { // Unfiltered. | |
| 299 | 272 | wp_update_attachment_metadata( $tr_id, wp_slash( $data ) ); // Directly uses update_post_meta, so expects slashed. |
| 300 | 273 | } |
| 301 | 274 | |
| 302 | 275 | // Copy attached file. |