PluginProbe
Polylang / 3.1.4
Polylang v3.1.4
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 +34 -22 2.73.1.4 View file →
@@ -1,5 +1,8 @@
1 1 <?php
2 +/**
3 + * @package Polylang
4 + */
2 5
3 6 /**
4 7 * Setups the taxonomies languages and translations model
5 8 *
@@ -24,20 +27,21 @@
24 27 parent::__construct( $model );
25 28
26 29 // Filters to prime terms cache
27 30 add_filter( 'get_terms', array( $this, '_prime_terms_cache' ), 10, 2 );
28 - add_filter( 'wp_get_object_terms', array( $this, 'wp_get_object_terms' ), 10, 3 );
31 + add_filter( 'get_object_terms', array( $this, 'wp_get_object_terms' ), 10, 3 );
29 32
30 33 add_action( 'clean_term_cache', array( $this, 'clean_term_cache' ) );
31 34 }
32 35
33 36 /**
34 - * Stores the term language in the database
37 + * Stores the term language in the database.
35 38 *
36 39 * @since 0.6
37 40 *
38 - * @param int $term_id term id
39 - * @param int|string|object $lang language ( term_id or slug or object )
41 + * @param int $term_id Term id.
42 + * @param int|string|PLL_Language $lang Language (term_id or slug or object).
43 + * @return void
40 44 */
41 45 public function set_language( $term_id, $lang ) {
42 46 $term_id = (int) $term_id;
43 47
@@ -42,10 +46,12 @@
42 46 $term_id = (int) $term_id;
43 47
44 48 $old_lang = $this->get_language( $term_id );
45 49 $old_lang = $old_lang ? $old_lang->tl_term_id : '';
46 - $lang = $lang ? $this->model->get_language( $lang )->tl_term_id : '';
47 50
51 + $lang = $this->model->get_language( $lang );
52 + $lang = $lang ? $lang->tl_term_id : '';
53 +
48 54 if ( $old_lang !== $lang ) {
49 55 wp_set_object_terms( $term_id, $lang, 'term_language' );
50 56
51 57 // Add translation group for correct WXR export
@@ -63,8 +69,9 @@
63 69 *
64 70 * @since 0.5
65 71 *
66 72 * @param int $term_id term id
73 + * @return void
67 74 */
68 75 public function delete_language( $term_id ) {
69 76 wp_delete_object_term_relationships( $term_id, 'term_language' );
70 77 }
@@ -75,9 +82,9 @@
75 82 * @since 0.1
76 83 *
77 84 * @param int|string $value term id or term slug
78 85 * @param string $taxonomy optional taxonomy needed when the term slug is passed as first parameter
79 - * @return bool|object PLL_Language object, false if no language is associated to that term
86 + * @return PLL_Language|false PLL_Language object, false if no language is associated to that term
80 87 */
81 88 public function get_language( $value, $taxonomy = '' ) {
82 89 if ( is_numeric( $value ) ) {
83 90 $term_id = $value;
@@ -84,9 +91,12 @@
84 91 }
85 92
86 93 // get_term_by still not cached in WP 3.5.1 but internally, the function is always called by term_id
87 94 elseif ( is_string( $value ) && $taxonomy ) {
88 - $term_id = get_term_by( 'slug', $value, $taxonomy )->term_id;
95 + $term = get_term_by( 'slug', $value, $taxonomy );
96 + if ( $term instanceof WP_Term ) {
97 + $term_id = $term->term_id;
98 + }
89 99 }
90 100
91 101 // Get the language and make sure it is a PLL_Language object
92 102 return isset( $term_id ) && ( $lang = $this->get_object_term( $term_id, 'term_language' ) ) ? $this->model->get_language( $lang->term_id ) : false;
@@ -92,14 +102,15 @@
92 102 return isset( $term_id ) && ( $lang = $this->get_object_term( $term_id, 'term_language' ) ) ? $this->model->get_language( $lang->term_id ) : false;
93 103 }
94 104
95 105 /**
96 - * Tells whether a translation term must updated
106 + * Tells whether a translation term must updated.
97 107 *
98 108 * @since 2.3
99 109 *
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
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
102 113 */
103 114 protected function should_update_translation_group( $id, $translations ) {
104 115 // Don't do anything if no translations have been added to the group
105 116 $old_translations = $this->get_translations( $id );
@@ -117,8 +128,9 @@
117 128 *
118 129 * @since 0.5
119 130 *
120 131 * @param int $id term id
132 + * @return void
121 133 */
122 134 public function delete_translation( $id ) {
123 135 global $wpdb;
124 136 $slug = array_search( $id, $this->get_translations( $id ) ); // in case some plugin stores the same value with different key
@@ -148,15 +160,15 @@
148 160 return " INNER JOIN $wpdb->term_relationships AS pll_tr ON pll_tr.object_id = $alias.term_id";
149 161 }
150 162
151 163 /**
152 - * Cache language and translations when terms are queried by get_terms
164 + * Caches the language and translations when terms are queried by get_terms().
153 165 *
154 166 * @since 1.2
155 167 *
156 - * @param array $terms queried terms
157 - * @param array $taxonomies queried taxonomies
158 - * @return array unmodified $terms
168 + * @param WP_Term[]|int[] $terms Queried terms.
169 + * @param string[] $taxonomies Queried taxonomies.
170 + * @return WP_Term[]|int[] Unmodified $terms.
159 171 */
160 172 public function _prime_terms_cache( $terms, $taxonomies ) {
161 173 $term_ids = array();
162 174
@@ -172,19 +184,18 @@
172 184 return $terms;
173 185 }
174 186
175 187 /**
176 - * When terms are found for posts, add their language and translations to cache
188 + * When terms are found for posts, add their language and translations to cache.
177 189 *
178 190 * @since 1.2
179 191 *
180 - * @param array $terms terms found
181 - * @param array $object_ids not used
182 - * @param array $taxonomies terms taxonomies
183 - * @return array unmodified $terms
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.
184 196 */
185 197 public function wp_get_object_terms( $terms, $object_ids, $taxonomies ) {
186 - $taxonomies = explode( "', '", trim( $taxonomies, "'" ) );
187 198 if ( ! in_array( 'term_translations', $taxonomies ) ) {
188 199 $this->_prime_terms_cache( $terms, $taxonomies );
189 200 }
190 201 return $terms;
@@ -190,13 +201,14 @@
190 201 return $terms;
191 202 }
192 203
193 204 /**
194 - * When the term cache is cleaned, clean the object term cache too
205 + * When the term cache is cleaned, cleans the object term cache too.
195 206 *
196 207 * @since 2.0
197 208 *
198 - * @param array $ids An array of term IDs.
209 + * @param int[] $ids An array of term IDs.
210 + * @return void
199 211 */
200 212 public function clean_term_cache( $ids ) {
201 213 clean_object_term_cache( $ids, 'term' );
202 214 }