| @@ -1,8 +1,5 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | 3 | /** |
| 7 | 4 | * Setups the objects languages and translations model |
| 8 | 5 | * |
| @@ -9,9 +6,9 @@ | ||
| 9 | 6 | * @since 1.8 |
| 10 | 7 | */ |
| 11 | 8 | abstract class PLL_Translated_Object { |
| 12 | 9 | public $model; |
| 13 | - protected $object_type, $type, $tax_language, $tax_translations, $tax_tt; | |
| 10 | + protected $object_type, $tax_language, $tax_translations, $tax_tt; | |
| 14 | 11 | |
| 15 | 12 | /** |
| 16 | 13 | * Constructor |
| 17 | 14 | * |
| @@ -45,13 +42,8 @@ | ||
| 45 | 42 | return false; |
| 46 | 43 | } |
| 47 | 44 | |
| 48 | 45 | $object_id = (int) $object_id; |
| 49 | - | |
| 50 | - if ( $object_id < 0 ) { | |
| 51 | - return false; | |
| 52 | - } | |
| 53 | - | |
| 54 | 46 | $term = get_object_term_cache( $object_id, $taxonomy ); |
| 55 | 47 | |
| 56 | 48 | if ( false === $term ) { |
| 57 | 49 | // query language and translations at the same time |
| @@ -57,9 +49,8 @@ | ||
| 57 | 49 | // query language and translations at the same time |
| 58 | 50 | $taxonomies = array( $this->tax_language, $this->tax_translations ); |
| 59 | 51 | |
| 60 | 52 | // query terms |
| 61 | - $terms = array(); | |
| 62 | 53 | foreach ( wp_get_object_terms( $object_id, $taxonomies, array( 'update_term_meta_cache' => false ) ) as $t ) { |
| 63 | 54 | $terms[ $t->taxonomy ] = $t; |
| 64 | 55 | if ( $t->taxonomy == $taxonomy ) { |
| 65 | 56 | $term = $t; |
| @@ -123,16 +114,16 @@ | ||
| 123 | 114 | $term = reset( $terms ); |
| 124 | 115 | |
| 125 | 116 | // create a new term if necessary |
| 126 | 117 | if ( empty( $term ) ) { |
| 127 | - wp_insert_term( $group = uniqid( 'pll_' ), $this->tax_translations, array( 'description' => maybe_serialize( $translations ) ) ); | |
| 118 | + wp_insert_term( $group = uniqid( 'pll_' ), $this->tax_translations, array( 'description' => serialize( $translations ) ) ); | |
| 128 | 119 | } |
| 129 | 120 | else { |
| 130 | 121 | // take care not to overwrite extra data stored in description field, if any |
| 131 | - $d = maybe_unserialize( $term->description ); | |
| 122 | + $d = unserialize( $term->description ); | |
| 132 | 123 | $d = is_array( $d ) ? array_diff_key( $d, $old_translations ) : array(); // remove old translations |
| 133 | 124 | $d = array_merge( $d, $translations ); // add new one |
| 134 | - wp_update_term( $group = (int) $term->term_id, $this->tax_translations, array( 'description' => maybe_serialize( $d ) ) ); | |
| 125 | + wp_update_term( $group = (int) $term->term_id, $this->tax_translations, array( 'description' => serialize( $d ) ) ); | |
| 135 | 126 | } |
| 136 | 127 | |
| 137 | 128 | // link all translations to the new term |
| 138 | 129 | foreach ( $translations as $p ) { |
| @@ -161,19 +152,18 @@ | ||
| 161 | 152 | $id = (int) $id; |
| 162 | 153 | $term = $this->get_object_term( $id, $this->tax_translations ); |
| 163 | 154 | |
| 164 | 155 | if ( ! empty( $term ) ) { |
| 165 | - $d = maybe_unserialize( $term->description ); | |
| 166 | - if ( is_array( $d ) ) { | |
| 167 | - $slug = array_search( $id, $this->get_translations( $id ) ); // In case some plugin stores the same value with different key. | |
| 168 | - unset( $d[ $slug ] ); | |
| 169 | - } | |
| 156 | + $d = unserialize( $term->description ); | |
| 157 | + $slug = array_search( $id, $this->get_translations( $id ) ); // in case some plugin stores the same value with different key | |
| 158 | + unset( $d[ $slug ] ); | |
| 170 | 159 | |
| 171 | 160 | if ( empty( $d ) ) { |
| 172 | 161 | wp_delete_term( (int) $term->term_id, $this->tax_translations ); |
| 173 | - } else { | |
| 174 | - wp_update_term( (int) $term->term_id, $this->tax_translations, array( 'description' => maybe_serialize( $d ) ) ); | |
| 175 | 162 | } |
| 163 | + else { | |
| 164 | + wp_update_term( (int) $term->term_id, $this->tax_translations, array( 'description' => serialize( $d ) ) ); | |
| 165 | + } | |
| 176 | 166 | } |
| 177 | 167 | } |
| 178 | 168 | |
| 179 | 169 | /** |
| @@ -185,9 +175,9 @@ | ||
| 185 | 175 | * @return array an associative array of translations with language code as key and translation id as value |
| 186 | 176 | */ |
| 187 | 177 | public function get_translations( $id ) { |
| 188 | 178 | $term = $this->get_object_term( $id, $this->tax_translations ); |
| 189 | - $translations = empty( $term ) ? array() : maybe_unserialize( $term->description ); | |
| 179 | + $translations = empty( $term ) ? array() : unserialize( $term->description ); | |
| 190 | 180 | |
| 191 | 181 | // make sure we return only translations ( thus we allow plugins to store other information in the array ) |
| 192 | 182 | if ( is_array( $translations ) ) { |
| 193 | 183 | $translations = array_intersect_key( $translations, array_flip( $this->model->get_languages_list( array( 'fields' => 'slug' ) ) ) ); |
| @@ -248,8 +238,9 @@ | ||
| 248 | 238 | * @param object|array|string $lang a PLL_Language object or a comma separated list of language slug or an array of language slugs |
| 249 | 239 | * @return string where clause |
| 250 | 240 | */ |
| 251 | 241 | public function where_clause( $lang ) { |
| 242 | + global $wpdb; | |
| 252 | 243 | $tt_id = $this->tax_tt; |
| 253 | 244 | |
| 254 | 245 | // $lang is an object |
| 255 | 246 | // generally the case if the query is coming from Polylang |
| @@ -258,10 +249,9 @@ | ||
| 258 | 249 | } |
| 259 | 250 | |
| 260 | 251 | // $lang is a comma separated list of slugs ( or an array of slugs ) |
| 261 | 252 | // generally the case is the query is coming from outside with 'lang' parameter |
| 262 | - $slugs = is_array( $lang ) ? $lang : explode( ',', $lang ); | |
| 263 | - $languages = array(); | |
| 253 | + $slugs = is_array( $lang ) ? $lang : explode( ',', $lang ); | |
| 264 | 254 | foreach ( $slugs as $slug ) { |
| 265 | 255 | $languages[] = absint( $this->model->get_language( $slug )->$tt_id ); |
| 266 | 256 | } |
| 267 | 257 | |
| @@ -279,61 +269,7 @@ | ||
| 279 | 269 | */ |
| 280 | 270 | public function get_objects_in_language( $lang ) { |
| 281 | 271 | global $wpdb; |
| 282 | 272 | $tt_id = $this->tax_tt; |
| 283 | - | |
| 284 | - $last_changed = wp_cache_get_last_changed( 'terms' ); | |
| 285 | - $cache_key = "polylang:get_objects_in_language:{$lang->$tt_id}:{$last_changed}"; | |
| 286 | - $cache = wp_cache_get( $cache_key, 'terms' ); | |
| 287 | - | |
| 288 | - if ( false === $cache ) { | |
| 289 | - $object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $lang->$tt_id ) ); | |
| 290 | - wp_cache_set( $cache_key, $object_ids, 'terms' ); | |
| 291 | - } else { | |
| 292 | - $object_ids = (array) $cache; | |
| 293 | - } | |
| 294 | - | |
| 295 | - if ( ! $object_ids ) { | |
| 296 | - return array(); | |
| 297 | - } | |
| 298 | - | |
| 299 | - return $object_ids; | |
| 300 | - } | |
| 301 | - | |
| 302 | - /** | |
| 303 | - * Check if a user can synchronize translations | |
| 304 | - * | |
| 305 | - * @since 2.6 | |
| 306 | - * | |
| 307 | - * @param int $id Object id | |
| 308 | - * @return bool | |
| 309 | - */ | |
| 310 | - public function current_user_can_synchronize( $id ) { | |
| 311 | - /** | |
| 312 | - * Filters whether a synchronization capability check should take place | |
| 313 | - * | |
| 314 | - * @since 2.6 | |
| 315 | - * | |
| 316 | - * @param $check null to enable the capability check, | |
| 317 | - * true to always allow the synchronization, | |
| 318 | - * false to always disallow the synchronization. | |
| 319 | - * Defaults to true. | |
| 320 | - * @param $id The synchronization source object id | |
| 321 | - */ | |
| 322 | - $check = apply_filters( "pll_pre_current_user_can_synchronize_{$this->type}", true, $id ); | |
| 323 | - if ( null !== $check ) { | |
| 324 | - return $check; | |
| 325 | - } | |
| 326 | - | |
| 327 | - if ( ! current_user_can( "edit_{$this->type}", $id ) ) { | |
| 328 | - return false; | |
| 329 | - } | |
| 330 | - | |
| 331 | - foreach ( $this->get_translations( $id ) as $tr_id ) { | |
| 332 | - if ( $tr_id !== $id && ! current_user_can( "edit_{$this->type}", $tr_id ) ) { | |
| 333 | - return false; | |
| 334 | - } | |
| 335 | - } | |
| 336 | - | |
| 337 | - return true; | |
| 273 | + return $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $lang->$tt_id ) ); | |
| 338 | 274 | } |
| 339 | 275 | } |