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 +42 -75 3.02.1 View file →
@@ -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,44 +21,34 @@
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 - add_filter( 'get_object_terms', array( $this, 'wp_get_object_terms' ), 10, 3 );
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' ) );
34 30 }
35 31
36 32 /**
37 - * Stores the term language in the database.
33 + * Stores the term language in the database
38 34 *
39 35 * @since 0.6
40 36 *
41 - * @param int $term_id Term id.
42 - * @param int|string|PLL_Language $lang Language (term_id or slug or object).
43 - * @return void
37 + * @param int $term_id term id
38 + * @param int|string|object $lang language ( term_id or slug or object )
44 39 */
45 40 public function set_language( $term_id, $lang ) {
46 41 $term_id = (int) $term_id;
42 + wp_set_object_terms( $term_id, $lang ? $this->model->get_language( $lang )->tl_term_id : '', 'term_language' );
47 43
48 - $old_lang = $this->get_language( $term_id );
49 - $old_lang = $old_lang ? $old_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 - $lang = $this->model->get_language( $lang );
52 - $lang = $lang ? $lang->tl_term_id : '';
53 -
54 - if ( $old_lang !== $lang ) {
55 - wp_set_object_terms( $term_id, $lang, 'term_language' );
56 -
57 - // Add translation group for correct WXR export
58 - $translations = $this->get_translations( $term_id );
59 - if ( $slug = array_search( $term_id, $translations ) ) {
60 - unset( $translations[ $slug ] );
61 - }
62 -
63 - $this->save_translations( $term_id, $translations );
64 - }
50 + $this->save_translations( $term_id, $translations );
65 51 }
66 52
67 53 /**
68 54 * Removes the term language in database
@@ -69,9 +55,8 @@
69 55 *
70 56 * @since 0.5
71 57 *
72 58 * @param int $term_id term id
73 - * @return void
74 59 */
75 60 public function delete_language( $term_id ) {
76 61 wp_delete_object_term_relationships( $term_id, 'term_language' );
77 62 }
@@ -82,9 +67,9 @@
82 67 * @since 0.1
83 68 *
84 69 * @param int|string $value term id or term slug
85 70 * @param string $taxonomy optional taxonomy needed when the term slug is passed as first parameter
86 - * @return PLL_Language|false PLL_Language object, false if no language is associated to that term
71 + * @return bool|object PLL_Language object, false if no language is associated to that term
87 72 */
88 73 public function get_language( $value, $taxonomy = '' ) {
89 74 if ( is_numeric( $value ) ) {
90 75 $term_id = $value;
@@ -91,37 +76,24 @@
91 76 }
92 77
93 78 // get_term_by still not cached in WP 3.5.1 but internally, the function is always called by term_id
94 79 elseif ( is_string( $value ) && $taxonomy ) {
95 - $term = get_term_by( 'slug', $value, $taxonomy );
96 - if ( $term instanceof WP_Term ) {
97 - $term_id = $term->term_id;
98 - }
80 + $term_id = wpcom_vip_get_term_by( 'slug', $value , $taxonomy )->term_id;
99 81 }
100 82
101 - // 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
102 84 return isset( $term_id ) && ( $lang = $this->get_object_term( $term_id, 'term_language' ) ) ? $this->model->get_language( $lang->term_id ) : false;
103 85 }
104 86
105 87 /**
106 - * Tells whether a translation term must updated.
88 + * Tells the parent class to always store a translation term
107 89 *
108 - * @since 2.3
90 + * @since 1.8
109 91 *
110 - * @param int $id Post id or term id.
111 - * @param int[] $translations An associative array of translations with language code as key and translation id as value.
112 - * @return bool
92 + * @param array $translations: an associative array of translations with language code as key and translation id as value
113 93 */
114 - protected function should_update_translation_group( $id, $translations ) {
115 - // Don't do anything if no translations have been added to the group
116 - $old_translations = $this->get_translations( $id );
117 - if ( count( $translations ) > 1 && count( array_diff_assoc( $translations, $old_translations ) ) > 0 ) {
118 - return true;
119 - }
120 -
121 - // But we need a translation group for terms to allow relationships remap when importing from a WXR file
122 - $term = $this->get_object_term( $id, $this->tax_translations );
123 - return empty( $term ) || count( array_diff_assoc( $translations, $old_translations ) );
94 + protected function keep_translation_group( $translations ) {
95 + return true;
124 96 }
125 97
126 98 /**
127 99 * Deletes a translation
@@ -128,9 +100,8 @@
128 100 *
129 101 * @since 0.5
130 102 *
131 103 * @param int $id term id
132 - * @return void
133 104 */
134 105 public function delete_translation( $id ) {
135 106 global $wpdb;
136 107 $slug = array_search( $id, $this->get_translations( $id ) ); // in case some plugin stores the same value with different key
@@ -135,14 +106,13 @@
135 106 global $wpdb;
136 107 $slug = array_search( $id, $this->get_translations( $id ) ); // in case some plugin stores the same value with different key
137 108
138 109 parent::delete_translation( $id );
139 - wp_delete_object_term_relationships( $id, 'term_translations' );
140 110
141 - if ( ! doing_action( 'pre_delete_term' ) && $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( * ) FROM $wpdb->terms WHERE term_id = %d;", $id ) ) ) {
142 - // Always keep a group for terms to allow relationships remap when importing from a WXR file
143 - $translations = array( $slug => $id );
144 - 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 ) ) );
145 115 wp_set_object_terms( $id, $group, 'term_translations' );
146 116 }
147 117 }
148 118
@@ -149,30 +119,26 @@
149 119 /**
150 120 * A join clause to add to sql queries when filtering by language is needed directly in query
151 121 *
152 122 * @since 1.2
153 - * @since 2.6 The `$alias` parameter was added.
154 123 *
155 - * @param string $alias Alias for $wpdb->terms table
156 124 * @return string join clause
157 125 */
158 - public function join_clause( $alias = 't' ) {
126 + public function join_clause() {
159 127 global $wpdb;
160 - 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";
161 129 }
162 130
163 131 /**
164 - * Caches the language and translations when terms are queried by get_terms().
132 + * Cache language and translations when terms are queried by get_terms
165 133 *
166 134 * @since 1.2
167 135 *
168 - * @param WP_Term[]|int[] $terms Queried terms.
169 - * @param string[] $taxonomies Queried taxonomies.
170 - * @return WP_Term[]|int[] Unmodified $terms.
136 + * @param array $terms queried terms
137 + * @param array $taxonomies queried taxonomies
138 + * @return array unmodified $terms
171 139 */
172 140 public function _prime_terms_cache( $terms, $taxonomies ) {
173 - $term_ids = array();
174 -
175 141 if ( is_array( $terms ) && $this->model->is_translated_taxonomy( $taxonomies ) ) {
176 142 foreach ( $terms as $term ) {
177 143 $term_ids[] = is_object( $term ) ? $term->term_id : (int) $term;
178 144 }
@@ -178,24 +144,25 @@
178 144 }
179 145 }
180 146
181 147 if ( ! empty( $term_ids ) ) {
182 - 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
183 149 }
184 150 return $terms;
185 151 }
186 152
187 153 /**
188 - * When terms are found for posts, add their language and translations to cache.
154 + * When terms are found for posts, add their language and translations to cache
189 155 *
190 156 * @since 1.2
191 157 *
192 - * @param WP_Term[] $terms Array of terms for the given object or objects.
193 - * @param int[] $object_ids Array of object IDs for which terms were retrieved.
194 - * @param string[] $taxonomies Array of taxonomy names from which terms were retrieved.
195 - * @return WP_Term[] Unmodified $terms.
158 + * @param array $terms terms found
159 + * @param array $object_ids not used
160 + * @param array $taxonomies terms taxonomies
161 + * @return array unmodified $terms
196 162 */
197 163 public function wp_get_object_terms( $terms, $object_ids, $taxonomies ) {
164 + $taxonomies = explode( "', '", trim( $taxonomies, "'" ) );
198 165 if ( ! in_array( 'term_translations', $taxonomies ) ) {
199 166 $this->_prime_terms_cache( $terms, $taxonomies );
200 167 }
201 168 return $terms;
@@ -201,15 +168,15 @@
201 168 return $terms;
202 169 }
203 170
204 171 /**
205 - * When the term cache is cleaned, cleans the object term cache too.
172 + * When the term cache is cleaned, clean the object term cache too
206 173 *
207 174 * @since 2.0
208 175 *
209 - * @param int[] $ids An array of term IDs.
210 - * @return void
176 + * @param array $ids An array of term IDs.
177 + * @param string $taxonomy Taxonomy slug.
211 178 */
212 - public function clean_term_cache( $ids ) {
179 + function clean_term_cache( $ids ) {
213 180 clean_object_term_cache( $ids, 'term' );
214 181 }
215 182 }