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

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