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