PluginProbe
Polylang / 2.2
Polylang v2.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.2, 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 wp_delete_object_term_relationships( $id, 'term_translations' );
111
112 if ( $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( * ) FROM $wpdb->terms WHERE term_id = %d;", $id ) ) ) {
113 // Always keep a group for terms to allow relationships remap when importing from a WXR file
114 $translations[ $slug ] = $id;
115 wp_insert_term( $group = uniqid( 'pll_' ), 'term_translations', array( 'description' => serialize( $translations ) ) );
116 wp_set_object_terms( $id, $group, 'term_translations' );
117 }
118 }
119
120 /**
121 * A join clause to add to sql queries when filtering by language is needed directly in query
122 *
123 * @since 1.2
124 *
125 * @return string join clause
126 */
127 public function join_clause() {
128 global $wpdb;
129 return " INNER JOIN $wpdb->term_relationships AS pll_tr ON pll_tr.object_id = t.term_id";
130 }
131
132 /**
133 * Cache language and translations when terms are queried by get_terms
134 *
135 * @since 1.2
136 *
137 * @param array $terms queried terms
138 * @param array $taxonomies queried taxonomies
139 * @return array unmodified $terms
140 */
141 public function _prime_terms_cache( $terms, $taxonomies ) {
142 if ( is_array( $terms ) && $this->model->is_translated_taxonomy( $taxonomies ) ) {
143 foreach ( $terms as $term ) {
144 $term_ids[] = is_object( $term ) ? $term->term_id : (int) $term;
145 }
146 }
147
148 if ( ! empty( $term_ids ) ) {
149 update_object_term_cache( array_unique( $term_ids ), 'term' ); // Adds language and translation of terms to cache
150 }
151 return $terms;
152 }
153
154 /**
155 * When terms are found for posts, add their language and translations to cache
156 *
157 * @since 1.2
158 *
159 * @param array $terms terms found
160 * @param array $object_ids not used
161 * @param array $taxonomies terms taxonomies
162 * @return array unmodified $terms
163 */
164 public function wp_get_object_terms( $terms, $object_ids, $taxonomies ) {
165 $taxonomies = explode( "', '", trim( $taxonomies, "'" ) );
166 if ( ! in_array( 'term_translations', $taxonomies ) ) {
167 $this->_prime_terms_cache( $terms, $taxonomies );
168 }
169 return $terms;
170 }
171
172 /**
173 * When the term cache is cleaned, clean the object term cache too
174 *
175 * @since 2.0
176 *
177 * @param array $ids An array of term IDs.
178 */
179 function clean_term_cache( $ids ) {
180 clean_object_term_cache( $ids, 'term' );
181 }
182 }
183