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

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

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