PluginProbe
Polylang / 3.0
Polylang v3.0
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
polylang / include / translated-term.php

translated-term.php in Polylang 3.0, at include/translated-term.php

216 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package Polylang
4 */
5
6 /**
7 * Setups the taxonomies languages and translations model
8 *
9 * @since 1.8
10 */
11 class PLL_Translated_Term extends PLL_Translated_Object {
12
13 /**
14 * Constructor
15 *
16 * @since 1.8
17 *
18 * @param object $model
19 */
20 public function __construct( &$model ) {
21 $this->object_type = 'term'; // For taxonomies
22 $this->type = 'term'; // For capabilities
23 $this->tax_language = 'term_language';
24 $this->tax_translations = 'term_translations';
25 $this->tax_tt = 'tl_term_taxonomy_id';
26
27 parent::__construct( $model );
28
29 // Filters to prime terms cache
30 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 );
32
33 add_action( 'clean_term_cache', array( $this, 'clean_term_cache' ) );
34 }
35
36 /**
37 * Stores the term language in the database.
38 *
39 * @since 0.6
40 *
41 * @param int $term_id Term id.
42 * @param int|string|PLL_Language $lang Language (term_id or slug or object).
43 * @return void
44 */
45 public function set_language( $term_id, $lang ) {
46 $term_id = (int) $term_id;
47
48 $old_lang = $this->get_language( $term_id );
49 $old_lang = $old_lang ? $old_lang->tl_term_id : '';
50
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 }
65 }
66
67 /**
68 * Removes the term language in database
69 *
70 * @since 0.5
71 *
72 * @param int $term_id term id
73 * @return void
74 */
75 public function delete_language( $term_id ) {
76 wp_delete_object_term_relationships( $term_id, 'term_language' );
77 }
78
79 /**
80 * Returns the language of a term
81 *
82 * @since 0.1
83 *
84 * @param int|string $value term id or term slug
85 * @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
87 */
88 public function get_language( $value, $taxonomy = '' ) {
89 if ( is_numeric( $value ) ) {
90 $term_id = $value;
91 }
92
93 // get_term_by still not cached in WP 3.5.1 but internally, the function is always called by term_id
94 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 }
99 }
100
101 // Get the language and make sure it is a PLL_Language object
102 return isset( $term_id ) && ( $lang = $this->get_object_term( $term_id, 'term_language' ) ) ? $this->model->get_language( $lang->term_id ) : false;
103 }
104
105 /**
106 * Tells whether a translation term must updated.
107 *
108 * @since 2.3
109 *
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
113 */
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 ) );
124 }
125
126 /**
127 * Deletes a translation
128 *
129 * @since 0.5
130 *
131 * @param int $id term id
132 * @return void
133 */
134 public function delete_translation( $id ) {
135 global $wpdb;
136 $slug = array_search( $id, $this->get_translations( $id ) ); // in case some plugin stores the same value with different key
137
138 parent::delete_translation( $id );
139 wp_delete_object_term_relationships( $id, 'term_translations' );
140
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 ) ) );
145 wp_set_object_terms( $id, $group, 'term_translations' );
146 }
147 }
148
149 /**
150 * A join clause to add to sql queries when filtering by language is needed directly in query
151 *
152 * @since 1.2
153 * @since 2.6 The `$alias` parameter was added.
154 *
155 * @param string $alias Alias for $wpdb->terms table
156 * @return string join clause
157 */
158 public function join_clause( $alias = 't' ) {
159 global $wpdb;
160 return " INNER JOIN $wpdb->term_relationships AS pll_tr ON pll_tr.object_id = $alias.term_id";
161 }
162
163 /**
164 * Caches the language and translations when terms are queried by get_terms().
165 *
166 * @since 1.2
167 *
168 * @param WP_Term[]|int[] $terms Queried terms.
169 * @param string[] $taxonomies Queried taxonomies.
170 * @return WP_Term[]|int[] Unmodified $terms.
171 */
172 public function _prime_terms_cache( $terms, $taxonomies ) {
173 $term_ids = array();
174
175 if ( is_array( $terms ) && $this->model->is_translated_taxonomy( $taxonomies ) ) {
176 foreach ( $terms as $term ) {
177 $term_ids[] = is_object( $term ) ? $term->term_id : (int) $term;
178 }
179 }
180
181 if ( ! empty( $term_ids ) ) {
182 update_object_term_cache( array_unique( $term_ids ), 'term' ); // Adds language and translation of terms to cache
183 }
184 return $terms;
185 }
186
187 /**
188 * When terms are found for posts, add their language and translations to cache.
189 *
190 * @since 1.2
191 *
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.
196 */
197 public function wp_get_object_terms( $terms, $object_ids, $taxonomies ) {
198 if ( ! in_array( 'term_translations', $taxonomies ) ) {
199 $this->_prime_terms_cache( $terms, $taxonomies );
200 }
201 return $terms;
202 }
203
204 /**
205 * When the term cache is cleaned, cleans the object term cache too.
206 *
207 * @since 2.0
208 *
209 * @param int[] $ids An array of term IDs.
210 * @return void
211 */
212 public function clean_term_cache( $ids ) {
213 clean_object_term_cache( $ids, 'term' );
214 }
215 }
216