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

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