PluginProbe
Polylang / 2.6.2
Polylang v2.6.2
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 2.6.2, at include/translated-term.php

202 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Setups the taxonomies languages and translations model
5 *
6 * @since 1.8
7 */
8 class PLL_Translated_Term extends PLL_Translated_Object {
9
10 /**
11 * Constructor
12 *
13 * @since 1.8
14 *
15 * @param object $model
16 */
17 public function __construct( &$model ) {
18 $this->object_type = 'term'; // For taxonomies
19 $this->type = 'term'; // For capabilities
20 $this->tax_language = 'term_language';
21 $this->tax_translations = 'term_translations';
22 $this->tax_tt = 'tl_term_taxonomy_id';
23
24 parent::__construct( $model );
25
26 // Filters to prime terms cache
27 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 );
29
30 add_action( 'clean_term_cache', array( $this, 'clean_term_cache' ) );
31 }
32
33 /**
34 * Stores the term language in the database
35 *
36 * @since 0.6
37 *
38 * @param int $term_id term id
39 * @param int|string|object $lang language ( term_id or slug or object )
40 */
41 public function set_language( $term_id, $lang ) {
42 $term_id = (int) $term_id;
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 : '';
47
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 }
59 }
60
61 /**
62 * Removes the term language in database
63 *
64 * @since 0.5
65 *
66 * @param int $term_id term id
67 */
68 public function delete_language( $term_id ) {
69 wp_delete_object_term_relationships( $term_id, 'term_language' );
70 }
71
72 /**
73 * Returns the language of a term
74 *
75 * @since 0.1
76 *
77 * @param int|string $value term id or term slug
78 * @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
80 */
81 public function get_language( $value, $taxonomy = '' ) {
82 if ( is_numeric( $value ) ) {
83 $term_id = $value;
84 }
85
86 // get_term_by still not cached in WP 3.5.1 but internally, the function is always called by term_id
87 elseif ( is_string( $value ) && $taxonomy ) {
88 $term_id = wpcom_vip_get_term_by( 'slug', $value, $taxonomy )->term_id;
89 }
90
91 // Get the language and make sure it is a PLL_Language object
92 return isset( $term_id ) && ( $lang = $this->get_object_term( $term_id, 'term_language' ) ) ? $this->model->get_language( $lang->term_id ) : false;
93 }
94
95 /**
96 * Tells whether a translation term must updated
97 *
98 * @since 2.3
99 *
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
102 */
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 ) );
113 }
114
115 /**
116 * Deletes a translation
117 *
118 * @since 0.5
119 *
120 * @param int $id term id
121 */
122 public function delete_translation( $id ) {
123 global $wpdb;
124 $slug = array_search( $id, $this->get_translations( $id ) ); // in case some plugin stores the same value with different key
125
126 parent::delete_translation( $id );
127 wp_delete_object_term_relationships( $id, 'term_translations' );
128
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[ $slug ] = $id;
132 wp_insert_term( $group = uniqid( 'pll_' ), 'term_translations', array( 'description' => maybe_serialize( $translations ) ) );
133 wp_set_object_terms( $id, $group, 'term_translations' );
134 }
135 }
136
137 /**
138 * A join clause to add to sql queries when filtering by language is needed directly in query
139 *
140 * @since 1.2
141 * @since 2.6 The `$alias` parameter was added.
142 *
143 * @param string $alias Alias for $wpdb->terms table
144 * @return string join clause
145 */
146 public function join_clause( $alias = 't' ) {
147 global $wpdb;
148 return " INNER JOIN $wpdb->term_relationships AS pll_tr ON pll_tr.object_id = $alias.term_id";
149 }
150
151 /**
152 * Cache language and translations when terms are queried by get_terms
153 *
154 * @since 1.2
155 *
156 * @param array $terms queried terms
157 * @param array $taxonomies queried taxonomies
158 * @return array unmodified $terms
159 */
160 public function _prime_terms_cache( $terms, $taxonomies ) {
161 if ( is_array( $terms ) && $this->model->is_translated_taxonomy( $taxonomies ) ) {
162 foreach ( $terms as $term ) {
163 $term_ids[] = is_object( $term ) ? $term->term_id : (int) $term;
164 }
165 }
166
167 if ( ! empty( $term_ids ) ) {
168 update_object_term_cache( array_unique( $term_ids ), 'term' ); // Adds language and translation of terms to cache
169 }
170 return $terms;
171 }
172
173 /**
174 * When terms are found for posts, add their language and translations to cache
175 *
176 * @since 1.2
177 *
178 * @param array $terms terms found
179 * @param array $object_ids not used
180 * @param array $taxonomies terms taxonomies
181 * @return array unmodified $terms
182 */
183 public function wp_get_object_terms( $terms, $object_ids, $taxonomies ) {
184 $taxonomies = explode( "', '", trim( $taxonomies, "'" ) );
185 if ( ! in_array( 'term_translations', $taxonomies ) ) {
186 $this->_prime_terms_cache( $terms, $taxonomies );
187 }
188 return $terms;
189 }
190
191 /**
192 * When the term cache is cleaned, clean the object term cache too
193 *
194 * @since 2.0
195 *
196 * @param array $ids An array of term IDs.
197 */
198 public function clean_term_cache( $ids ) {
199 clean_object_term_cache( $ids, 'term' );
200 }
201 }
202