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