| @@ -6,9 +6,9 @@ | ||
| 6 | 6 | * @since 1.8 |
| 7 | 7 | */ |
| 8 | 8 | abstract class PLL_Translated_Object { |
| 9 | 9 | public $model; |
| 10 | - protected $object_type, $type, $tax_language, $tax_translations, $tax_tt; | |
| 10 | + protected $object_type, $tax_language, $tax_translations, $tax_tt; | |
| 11 | 11 | |
| 12 | 12 | /** |
| 13 | 13 | * Constructor |
| 14 | 14 | * |
| @@ -37,9 +37,9 @@ | ||
| 37 | 37 | * @param string $taxonomy Polylang taxonomy depending if we are looking for a post ( or term ) language ( or translation ) |
| 38 | 38 | * @return bool|object the term associated to the object in the requested taxonomy if exists, false otherwise |
| 39 | 39 | */ |
| 40 | 40 | public function get_object_term( $object_id, $taxonomy ) { |
| 41 | - if ( empty( $object_id ) || is_wp_error( $object_id ) ) { | |
| 41 | + if ( empty( $object_id ) ) { | |
| 42 | 42 | return false; |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | $object_id = (int) $object_id; |
| @@ -49,10 +49,9 @@ | ||
| 49 | 49 | // query language and translations at the same time |
| 50 | 50 | $taxonomies = array( $this->tax_language, $this->tax_translations ); |
| 51 | 51 | |
| 52 | 52 | // query terms |
| 53 | - $terms = array(); | |
| 54 | - foreach ( wp_get_object_terms( $object_id, $taxonomies, array( 'update_term_meta_cache' => false ) ) as $t ) { | |
| 53 | + foreach ( wp_get_object_terms( $object_id, $taxonomies ) as $t ) { | |
| 55 | 54 | $terms[ $t->taxonomy ] = $t; |
| 56 | 55 | if ( $t->taxonomy == $taxonomy ) { |
| 57 | 56 | $term = $t; |
| 58 | 57 | } |
| @@ -71,19 +70,16 @@ | ||
| 71 | 70 | return empty( $term ) ? false : $term; |
| 72 | 71 | } |
| 73 | 72 | |
| 74 | 73 | /** |
| 75 | - * Tells whether a translation term must be updated | |
| 74 | + * Tells wether to store a translation term | |
| 76 | 75 | * |
| 77 | - * @since 2.3 | |
| 76 | + * @since 1.8 | |
| 78 | 77 | * |
| 79 | - * @param array $id Post id or term id | |
| 80 | - * @param array $translations An associative array of translations with language code as key and translation id as value | |
| 78 | + * @param array $translations an associative array of translations with language code as key and translation id as value | |
| 81 | 79 | */ |
| 82 | - protected function should_update_translation_group( $id, $translations ) { | |
| 83 | - // Don't do anything if no translations have been added to the group | |
| 84 | - $old_translations = $this->get_translations( $id ); // Includes at least $id itself | |
| 85 | - return count( array_diff_assoc( $translations, $old_translations ) ) > 0; | |
| 80 | + protected function keep_translation_group( $translations ) { | |
| 81 | + return count( $translations ) > 1; | |
| 86 | 82 | } |
| 87 | 83 | |
| 88 | 84 | /** |
| 89 | 85 | * Saves translations for posts or terms |
| @@ -89,10 +85,10 @@ | ||
| 89 | 85 | * Saves translations for posts or terms |
| 90 | 86 | * |
| 91 | 87 | * @since 0.5 |
| 92 | 88 | * |
| 93 | - * @param int $id Post id or term id | |
| 94 | - * @param array $translations An associative array of translations with language code as key and translation id as value | |
| 89 | + * @param int $id post id or term id | |
| 90 | + * @param array $translations an associative array of translations with language code as key and translation id as value | |
| 95 | 91 | */ |
| 96 | 92 | public function save_translations( $id, $translations ) { |
| 97 | 93 | $id = (int) $id; |
| 98 | 94 | |
| @@ -108,23 +104,24 @@ | ||
| 108 | 104 | foreach ( array_diff_assoc( $old_translations, $translations ) as $object_id ) { |
| 109 | 105 | $this->delete_translation( $object_id ); |
| 110 | 106 | } |
| 111 | 107 | |
| 112 | - // Check id we need to create or update the translation group | |
| 113 | - if ( $this->should_update_translation_group( $id, $translations ) ) { | |
| 108 | + // don't create a translation group for untranslated posts as it is useless | |
| 109 | + // but we need one for terms to allow relationships remap when importing from a WXR file | |
| 110 | + if ( $this->keep_translation_group( $translations ) ) { | |
| 114 | 111 | $terms = wp_get_object_terms( $translations, $this->tax_translations ); |
| 115 | 112 | $term = reset( $terms ); |
| 116 | 113 | |
| 117 | 114 | // create a new term if necessary |
| 118 | 115 | if ( empty( $term ) ) { |
| 119 | - wp_insert_term( $group = uniqid( 'pll_' ), $this->tax_translations, array( 'description' => maybe_serialize( $translations ) ) ); | |
| 116 | + wp_insert_term( $group = uniqid( 'pll_' ), $this->tax_translations, array( 'description' => serialize( $translations ) ) ); | |
| 120 | 117 | } |
| 121 | 118 | else { |
| 122 | 119 | // take care not to overwrite extra data stored in description field, if any |
| 123 | - $d = maybe_unserialize( $term->description ); | |
| 120 | + $d = unserialize( $term->description ); | |
| 124 | 121 | $d = is_array( $d ) ? array_diff_key( $d, $old_translations ) : array(); // remove old translations |
| 125 | 122 | $d = array_merge( $d, $translations ); // add new one |
| 126 | - wp_update_term( $group = (int) $term->term_id, $this->tax_translations, array( 'description' => maybe_serialize( $d ) ) ); | |
| 123 | + wp_update_term( $group = (int) $term->term_id, $this->tax_translations, array( 'description' => serialize( $d ) ) ); | |
| 127 | 124 | } |
| 128 | 125 | |
| 129 | 126 | // link all translations to the new term |
| 130 | 127 | foreach ( $translations as $p ) { |
| @@ -153,9 +150,9 @@ | ||
| 153 | 150 | $id = (int) $id; |
| 154 | 151 | $term = $this->get_object_term( $id, $this->tax_translations ); |
| 155 | 152 | |
| 156 | 153 | if ( ! empty( $term ) ) { |
| 157 | - $d = maybe_unserialize( $term->description ); | |
| 154 | + $d = unserialize( $term->description ); | |
| 158 | 155 | $slug = array_search( $id, $this->get_translations( $id ) ); // in case some plugin stores the same value with different key |
| 159 | 156 | unset( $d[ $slug ] ); |
| 160 | 157 | |
| 161 | 158 | if ( empty( $d ) ) { |
| @@ -161,9 +158,9 @@ | ||
| 161 | 158 | if ( empty( $d ) ) { |
| 162 | 159 | wp_delete_term( (int) $term->term_id, $this->tax_translations ); |
| 163 | 160 | } |
| 164 | 161 | else { |
| 165 | - wp_update_term( (int) $term->term_id, $this->tax_translations, array( 'description' => maybe_serialize( $d ) ) ); | |
| 162 | + wp_update_term( (int) $term->term_id, $this->tax_translations, array( 'description' => serialize( $d ) ) ); | |
| 166 | 163 | } |
| 167 | 164 | } |
| 168 | 165 | } |
| 169 | 166 | |
| @@ -176,11 +173,11 @@ | ||
| 176 | 173 | * @return array an associative array of translations with language code as key and translation id as value |
| 177 | 174 | */ |
| 178 | 175 | public function get_translations( $id ) { |
| 179 | 176 | $term = $this->get_object_term( $id, $this->tax_translations ); |
| 180 | - $translations = empty( $term ) ? array() : maybe_unserialize( $term->description ); | |
| 177 | + $translations = empty( $term ) ? array() : unserialize( $term->description ); | |
| 181 | 178 | |
| 182 | - // make sure we return only translations ( thus we allow plugins to store other information in the array ) | |
| 179 | + // make sure we return only translations ( thus we allow plugins to store other informations in the array ) | |
| 183 | 180 | if ( is_array( $translations ) ) { |
| 184 | 181 | $translations = array_intersect_key( $translations, array_flip( $this->model->get_languages_list( array( 'fields' => 'slug' ) ) ) ); |
| 185 | 182 | } |
| 186 | 183 | |
| @@ -220,9 +217,8 @@ | ||
| 220 | 217 | * @param int|string|object $lang language ( term_id or slug or object ) |
| 221 | 218 | * @return bool|int the translation post id or term id if exists, otherwise the post id or term id, false if the post has no language |
| 222 | 219 | */ |
| 223 | 220 | public function get( $id, $lang ) { |
| 224 | - $id = (int) $id; | |
| 225 | 221 | $obj_lang = $this->get_language( $id ); // FIXME is this necessary? |
| 226 | 222 | if ( ! $lang || ! $obj_lang ) { |
| 227 | 223 | return false; |
| 228 | 224 | } |
| @@ -235,26 +231,26 @@ | ||
| 235 | 231 | * A where clause to add to sql queries when filtering by language is needed directly in query |
| 236 | 232 | * |
| 237 | 233 | * @since 1.2 |
| 238 | 234 | * |
| 239 | - * @param object|array|string $lang a PLL_Language object or a comma separated list of language slug or an array of language slugs | |
| 235 | + * @param object|array|string $lang a PLL_Language object or a comma separated list of languag slug or an array of language slugs | |
| 240 | 236 | * @return string where clause |
| 241 | 237 | */ |
| 242 | 238 | public function where_clause( $lang ) { |
| 239 | + global $wpdb; | |
| 243 | 240 | $tt_id = $this->tax_tt; |
| 244 | 241 | |
| 245 | 242 | // $lang is an object |
| 246 | 243 | // generally the case if the query is coming from Polylang |
| 247 | 244 | if ( is_object( $lang ) ) { |
| 248 | - return ' AND pll_tr.term_taxonomy_id = ' . absint( $lang->$tt_id ); | |
| 245 | + return $wpdb->prepare( ' AND pll_tr.term_taxonomy_id = %d', $lang->$tt_id ); | |
| 249 | 246 | } |
| 250 | 247 | |
| 251 | 248 | // $lang is a comma separated list of slugs ( or an array of slugs ) |
| 252 | 249 | // generally the case is the query is coming from outside with 'lang' parameter |
| 253 | - $slugs = is_array( $lang ) ? $lang : explode( ',', $lang ); | |
| 254 | - $languages = array(); | |
| 250 | + $slugs = is_array( $lang ) ? $lang : explode( ',', $lang ); | |
| 255 | 251 | foreach ( $slugs as $slug ) { |
| 256 | - $languages[] = absint( $this->model->get_language( $slug )->$tt_id ); | |
| 252 | + $languages[] = (int) $this->model->get_language( $slug )->$tt_id; | |
| 257 | 253 | } |
| 258 | 254 | |
| 259 | 255 | return ' AND pll_tr.term_taxonomy_id IN ( ' . implode( ',', $languages ) . ' )'; |
| 260 | 256 | } |
| @@ -271,44 +267,6 @@ | ||
| 271 | 267 | public function get_objects_in_language( $lang ) { |
| 272 | 268 | global $wpdb; |
| 273 | 269 | $tt_id = $this->tax_tt; |
| 274 | 270 | return $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $lang->$tt_id ) ); |
| 275 | - } | |
| 276 | - | |
| 277 | - /** | |
| 278 | - * Check if a user can synchronize translations | |
| 279 | - * | |
| 280 | - * @since 2.6 | |
| 281 | - * | |
| 282 | - * @param int $id Object id | |
| 283 | - * @return bool | |
| 284 | - */ | |
| 285 | - public function current_user_can_synchronize( $id ) { | |
| 286 | - /** | |
| 287 | - * Filters whether a synchronization capability check should take place | |
| 288 | - * | |
| 289 | - * @since 2.6 | |
| 290 | - * | |
| 291 | - * @param $check null to enable the capability check, | |
| 292 | - * true to always allow the synchronization, | |
| 293 | - * false to always disallow the synchronization. | |
| 294 | - * Defaults to true. | |
| 295 | - * @param $id The synchronization source object id | |
| 296 | - */ | |
| 297 | - $check = apply_filters( "pll_pre_current_user_can_synchronize_{$this->type}", true, $id ); | |
| 298 | - if ( null !== $check ) { | |
| 299 | - return $check; | |
| 300 | - } | |
| 301 | - | |
| 302 | - if ( ! current_user_can( "edit_{$this->type}", $id ) ) { | |
| 303 | - return false; | |
| 304 | - } | |
| 305 | - | |
| 306 | - foreach ( $this->get_translations( $id ) as $tr_id ) { | |
| 307 | - if ( $tr_id !== $id && ! current_user_can( "edit_{$this->type}", $tr_id ) ) { | |
| 308 | - return false; | |
| 309 | - } | |
| 310 | - } | |
| 311 | - | |
| 312 | - return true; | |
| 313 | 271 | } |
| 314 | 272 | } |