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

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