PluginProbe
Polylang / 1.9.2
Polylang v1.9.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 1.9.2, at include/translated-term.php

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