PluginProbe
Polylang / 2.7.4
Polylang v2.7.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
polylang / include / translated-term.php

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

204 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 = 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 = array( $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 $term_ids = array();
162
163 if ( is_array( $terms ) && $this->model->is_translated_taxonomy( $taxonomies ) ) {
164 foreach ( $terms as $term ) {
165 $term_ids[] = is_object( $term ) ? $term->term_id : (int) $term;
166 }
167 }
168
169 if ( ! empty( $term_ids ) ) {
170 update_object_term_cache( array_unique( $term_ids ), 'term' ); // Adds language and translation of terms to cache
171 }
172 return $terms;
173 }
174
175 /**
176 * When terms are found for posts, add their language and translations to cache
177 *
178 * @since 1.2
179 *
180 * @param array $terms terms found
181 * @param array $object_ids not used
182 * @param array $taxonomies terms taxonomies
183 * @return array unmodified $terms
184 */
185 public function wp_get_object_terms( $terms, $object_ids, $taxonomies ) {
186 $taxonomies = explode( "', '", trim( $taxonomies, "'" ) );
187 if ( ! in_array( 'term_translations', $taxonomies ) ) {
188 $this->_prime_terms_cache( $terms, $taxonomies );
189 }
190 return $terms;
191 }
192
193 /**
194 * When the term cache is cleaned, clean the object term cache too
195 *
196 * @since 2.0
197 *
198 * @param array $ids An array of term IDs.
199 */
200 public function clean_term_cache( $ids ) {
201 clean_object_term_cache( $ids, 'term' );
202 }
203 }
204