| @@ -1,8 +1,5 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | 3 | /** |
| 7 | 4 | * Setups the taxonomies languages and translations model |
| 8 | 5 | * |
| @@ -17,10 +14,9 @@ | ||
| 17 | 14 | * |
| 18 | 15 | * @param object $model |
| 19 | 16 | */ |
| 20 | 17 | public function __construct( &$model ) { |
| 21 | - $this->object_type = 'term'; // For taxonomies | |
| 22 | - $this->type = 'term'; // For capabilities | |
| 18 | + $this->object_type = 'term'; | |
| 23 | 19 | $this->tax_language = 'term_language'; |
| 24 | 20 | $this->tax_translations = 'term_translations'; |
| 25 | 21 | $this->tax_tt = 'tl_term_taxonomy_id'; |
| 26 | 22 | |
| @@ -25,9 +21,9 @@ | ||
| 25 | 21 | $this->tax_tt = 'tl_term_taxonomy_id'; |
| 26 | 22 | |
| 27 | 23 | parent::__construct( $model ); |
| 28 | 24 | |
| 29 | - // Filters to prime terms cache | |
| 25 | + // filters to prime terms cache | |
| 30 | 26 | add_filter( 'get_terms', array( $this, '_prime_terms_cache' ), 10, 2 ); |
| 31 | 27 | add_filter( 'wp_get_object_terms', array( $this, 'wp_get_object_terms' ), 10, 3 ); |
| 32 | 28 | |
| 33 | 29 | add_action( 'clean_term_cache', array( $this, 'clean_term_cache' ) ); |
| @@ -42,24 +38,17 @@ | ||
| 42 | 38 | * @param int|string|object $lang language ( term_id or slug or object ) |
| 43 | 39 | */ |
| 44 | 40 | public function set_language( $term_id, $lang ) { |
| 45 | 41 | $term_id = (int) $term_id; |
| 42 | + wp_set_object_terms( $term_id, $lang ? $this->model->get_language( $lang )->tl_term_id : '', 'term_language' ); | |
| 46 | 43 | |
| 47 | - $old_lang = $this->get_language( $term_id ); | |
| 48 | - $old_lang = $old_lang ? $old_lang->tl_term_id : ''; | |
| 49 | - $lang = $lang ? $this->model->get_language( $lang )->tl_term_id : ''; | |
| 44 | + // add translation group for correct WXR export | |
| 45 | + $translations = $this->get_translations( $term_id ); | |
| 46 | + if ( $slug = array_search( $term_id, $translations ) ) { | |
| 47 | + unset( $translations[ $slug ] ); | |
| 48 | + } | |
| 50 | 49 | |
| 51 | - if ( $old_lang !== $lang ) { | |
| 52 | - wp_set_object_terms( $term_id, $lang, 'term_language' ); | |
| 53 | - | |
| 54 | - // Add translation group for correct WXR export | |
| 55 | - $translations = $this->get_translations( $term_id ); | |
| 56 | - if ( $slug = array_search( $term_id, $translations ) ) { | |
| 57 | - unset( $translations[ $slug ] ); | |
| 58 | - } | |
| 59 | - | |
| 60 | - $this->save_translations( $term_id, $translations ); | |
| 61 | - } | |
| 50 | + $this->save_translations( $term_id, $translations ); | |
| 62 | 51 | } |
| 63 | 52 | |
| 64 | 53 | /** |
| 65 | 54 | * Removes the term language in database |
| @@ -87,33 +76,24 @@ | ||
| 87 | 76 | } |
| 88 | 77 | |
| 89 | 78 | // get_term_by still not cached in WP 3.5.1 but internally, the function is always called by term_id |
| 90 | 79 | elseif ( is_string( $value ) && $taxonomy ) { |
| 91 | - $term_id = get_term_by( 'slug', $value, $taxonomy )->term_id; | |
| 80 | + $term_id = wpcom_vip_get_term_by( 'slug', $value , $taxonomy )->term_id; | |
| 92 | 81 | } |
| 93 | 82 | |
| 94 | - // Get the language and make sure it is a PLL_Language object | |
| 83 | + // get the language and make sure it is a PLL_Language object | |
| 95 | 84 | return isset( $term_id ) && ( $lang = $this->get_object_term( $term_id, 'term_language' ) ) ? $this->model->get_language( $lang->term_id ) : false; |
| 96 | 85 | } |
| 97 | 86 | |
| 98 | 87 | /** |
| 99 | - * Tells whether a translation term must updated | |
| 88 | + * Tells the parent class to always store a translation term | |
| 100 | 89 | * |
| 101 | - * @since 2.3 | |
| 90 | + * @since 1.8 | |
| 102 | 91 | * |
| 103 | - * @param array $id Post id or term id | |
| 104 | - * @param array $translations An associative array of translations with language code as key and translation id as value | |
| 92 | + * @param array $translations: an associative array of translations with language code as key and translation id as value | |
| 105 | 93 | */ |
| 106 | - protected function should_update_translation_group( $id, $translations ) { | |
| 107 | - // Don't do anything if no translations have been added to the group | |
| 108 | - $old_translations = $this->get_translations( $id ); | |
| 109 | - if ( count( $translations ) > 1 && count( array_diff_assoc( $translations, $old_translations ) ) > 0 ) { | |
| 110 | - return true; | |
| 111 | - } | |
| 112 | - | |
| 113 | - // But we need a translation group for terms to allow relationships remap when importing from a WXR file | |
| 114 | - $term = $this->get_object_term( $id, $this->tax_translations ); | |
| 115 | - return empty( $term ) || count( array_diff_assoc( $translations, $old_translations ) ); | |
| 94 | + protected function keep_translation_group( $translations ) { | |
| 95 | + return true; | |
| 116 | 96 | } |
| 117 | 97 | |
| 118 | 98 | /** |
| 119 | 99 | * Deletes a translation |
| @@ -126,14 +106,13 @@ | ||
| 126 | 106 | global $wpdb; |
| 127 | 107 | $slug = array_search( $id, $this->get_translations( $id ) ); // in case some plugin stores the same value with different key |
| 128 | 108 | |
| 129 | 109 | parent::delete_translation( $id ); |
| 130 | - wp_delete_object_term_relationships( $id, 'term_translations' ); | |
| 131 | 110 | |
| 132 | - if ( ! doing_action( 'pre_delete_term' ) && $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( * ) FROM $wpdb->terms WHERE term_id = %d;", $id ) ) ) { | |
| 133 | - // Always keep a group for terms to allow relationships remap when importing from a WXR file | |
| 134 | - $translations = array( $slug => $id ); | |
| 135 | - wp_insert_term( $group = uniqid( 'pll_' ), 'term_translations', array( 'description' => maybe_serialize( $translations ) ) ); | |
| 111 | + if ( $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( * ) FROM $wpdb->terms WHERE term_id = %d;", $id ) ) ) { | |
| 112 | + // always keep a group for terms to allow relationships remap when importing from a WXR file | |
| 113 | + $translations[ $slug ] = $id; | |
| 114 | + wp_insert_term( $group = uniqid( 'pll_' ), 'term_translations', array( 'description' => serialize( $translations ) ) ); | |
| 136 | 115 | wp_set_object_terms( $id, $group, 'term_translations' ); |
| 137 | 116 | } |
| 138 | 117 | } |
| 139 | 118 | |
| @@ -140,16 +119,14 @@ | ||
| 140 | 119 | /** |
| 141 | 120 | * A join clause to add to sql queries when filtering by language is needed directly in query |
| 142 | 121 | * |
| 143 | 122 | * @since 1.2 |
| 144 | - * @since 2.6 The `$alias` parameter was added. | |
| 145 | 123 | * |
| 146 | - * @param string $alias Alias for $wpdb->terms table | |
| 147 | 124 | * @return string join clause |
| 148 | 125 | */ |
| 149 | - public function join_clause( $alias = 't' ) { | |
| 126 | + public function join_clause() { | |
| 150 | 127 | global $wpdb; |
| 151 | - return " INNER JOIN $wpdb->term_relationships AS pll_tr ON pll_tr.object_id = $alias.term_id"; | |
| 128 | + return " INNER JOIN $wpdb->term_relationships AS pll_tr ON pll_tr.object_id = t.term_id"; | |
| 152 | 129 | } |
| 153 | 130 | |
| 154 | 131 | /** |
| 155 | 132 | * Cache language and translations when terms are queried by get_terms |
| @@ -160,10 +137,8 @@ | ||
| 160 | 137 | * @param array $taxonomies queried taxonomies |
| 161 | 138 | * @return array unmodified $terms |
| 162 | 139 | */ |
| 163 | 140 | public function _prime_terms_cache( $terms, $taxonomies ) { |
| 164 | - $term_ids = array(); | |
| 165 | - | |
| 166 | 141 | if ( is_array( $terms ) && $this->model->is_translated_taxonomy( $taxonomies ) ) { |
| 167 | 142 | foreach ( $terms as $term ) { |
| 168 | 143 | $term_ids[] = is_object( $term ) ? $term->term_id : (int) $term; |
| 169 | 144 | } |
| @@ -169,9 +144,9 @@ | ||
| 169 | 144 | } |
| 170 | 145 | } |
| 171 | 146 | |
| 172 | 147 | if ( ! empty( $term_ids ) ) { |
| 173 | - update_object_term_cache( array_unique( $term_ids ), 'term' ); // Adds language and translation of terms to cache | |
| 148 | + update_object_term_cache( array_unique( $term_ids ), 'term' ); // adds language and translation of terms to cache | |
| 174 | 149 | } |
| 175 | 150 | return $terms; |
| 176 | 151 | } |
| 177 | 152 | |
| @@ -197,10 +172,11 @@ | ||
| 197 | 172 | * When the term cache is cleaned, clean the object term cache too |
| 198 | 173 | * |
| 199 | 174 | * @since 2.0 |
| 200 | 175 | * |
| 201 | - * @param array $ids An array of term IDs. | |
| 176 | + * @param array $ids An array of term IDs. | |
| 177 | + * @param string $taxonomy Taxonomy slug. | |
| 202 | 178 | */ |
| 203 | - public function clean_term_cache( $ids ) { | |
| 179 | + function clean_term_cache( $ids ) { | |
| 204 | 180 | clean_object_term_cache( $ids, 'term' ); |
| 205 | 181 | } |
| 206 | 182 | } |