| @@ -91,8 +91,33 @@ | ||
| 91 | 91 | */ |
| 92 | 92 | abstract public function get_language( $id ); |
| 93 | 93 | |
| 94 | 94 | /** |
| 95 | + * Assigns a new language to an object, taking care of the translations group. | |
| 96 | + * | |
| 97 | + * @since 3.1 | |
| 98 | + * | |
| 99 | + * @param int $id Object id. | |
| 100 | + * @param PLL_Language $lang New language to assign to the object. | |
| 101 | + * @return void | |
| 102 | + */ | |
| 103 | + public function update_language( $id, $lang ) { | |
| 104 | + if ( $this->get_language( $id ) === $lang ) { | |
| 105 | + return; | |
| 106 | + } | |
| 107 | + | |
| 108 | + $this->set_language( $id, $lang ); | |
| 109 | + | |
| 110 | + $translations = $this->get_translations( $id ); | |
| 111 | + | |
| 112 | + if ( $translations ) { | |
| 113 | + // Remove the post's former language from the new translations group. | |
| 114 | + $translations = array_diff( $translations, array( $id ) ); | |
| 115 | + $this->save_translations( $id, $translations ); | |
| 116 | + } | |
| 117 | + } | |
| 118 | + | |
| 119 | + /** | |
| 95 | 120 | * Wrap wp_get_object_terms() to cache it and return only one object. |
| 96 | 121 | * inspired by the WordPress function get_the_terms(). |
| 97 | 122 | * |
| 98 | 123 | * @since 1.2 |
| @@ -163,14 +188,17 @@ | ||
| 163 | 188 | * @since 0.5 |
| 164 | 189 | * |
| 165 | 190 | * @param int $id Object id ( typically a post_id or term_id ). |
| 166 | 191 | * @param int[] $translations An associative array of translations with language code as key and translation id as value. |
| 167 | - * @return void | |
| 192 | + * | |
| 193 | + * @return int[] An associative array with language codes as key and post ids as values. | |
| 168 | 194 | */ |
| 169 | 195 | public function save_translations( $id, $translations ) { |
| 196 | + $translations = $this->validate_translations( $translations ); | |
| 197 | + | |
| 170 | 198 | $id = (int) $id; |
| 171 | 199 | |
| 172 | - if ( ( $lang = $this->get_language( $id ) ) && is_array( $translations ) ) { | |
| 200 | + if ( $lang = $this->get_language( $id ) ) { | |
| 173 | 201 | // Sanitize the translations array. |
| 174 | 202 | $translations = array_map( 'intval', $translations ); |
| 175 | 203 | $translations = array_merge( array( $lang->slug => $id ), $translations ); // Make sure this object is in translations. |
| 176 | 204 | $translations = array_diff( $translations, array( 0 ) ); // Don't keep non translated languages. |
| @@ -210,9 +238,31 @@ | ||
| 210 | 238 | wp_delete_term( $term_id, $this->tax_translations ); |
| 211 | 239 | } |
| 212 | 240 | } |
| 213 | 241 | } |
| 242 | + return $translations; | |
| 243 | + } else { | |
| 244 | + return array(); | |
| 214 | 245 | } |
| 246 | + } | |
| 247 | + | |
| 248 | + /** | |
| 249 | + * Returns translations after checking the translated post is in the right language | |
| 250 | + * | |
| 251 | + * @since 3.1 | |
| 252 | + * | |
| 253 | + * @param int[] $translations An associative array of translations with language code as key and translation id as value. | |
| 254 | + * | |
| 255 | + * @return int[] | |
| 256 | + */ | |
| 257 | + public function validate_translations( $translations ) { | |
| 258 | + $valid_translations = array(); | |
| 259 | + | |
| 260 | + foreach ( $translations as $lang => $tr_id ) { | |
| 261 | + $valid_translations[ $lang ] = ( $tr_id && $this->get_language( (int) $tr_id )->slug == $lang ) ? (int) $tr_id : 0; | |
| 262 | + } | |
| 263 | + | |
| 264 | + return $valid_translations; | |
| 215 | 265 | } |
| 216 | 266 | |
| 217 | 267 | /** |
| 218 | 268 | * Deletes a translation of a post or term. |