| @@ -1,8 +1,5 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | 3 | /** |
| 7 | 4 | * Setups the taxonomies languages and translations model |
| 8 | 5 | * |
| @@ -27,21 +24,20 @@ | ||
| 27 | 24 | parent::__construct( $model ); |
| 28 | 25 | |
| 29 | 26 | // Filters to prime terms cache |
| 30 | 27 | 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 ); | |
| 28 | + add_filter( 'wp_get_object_terms', array( $this, 'wp_get_object_terms' ), 10, 3 ); | |
| 32 | 29 | |
| 33 | 30 | add_action( 'clean_term_cache', array( $this, 'clean_term_cache' ) ); |
| 34 | 31 | } |
| 35 | 32 | |
| 36 | 33 | /** |
| 37 | - * Stores the term language in the database. | |
| 34 | + * Stores the term language in the database | |
| 38 | 35 | * |
| 39 | 36 | * @since 0.6 |
| 40 | 37 | * |
| 41 | - * @param int $term_id Term id. | |
| 42 | - * @param int|string|PLL_Language $lang Language (term_id or slug or object). | |
| 43 | - * @return void | |
| 38 | + * @param int $term_id term id | |
| 39 | + * @param int|string|object $lang language ( term_id or slug or object ) | |
| 44 | 40 | */ |
| 45 | 41 | public function set_language( $term_id, $lang ) { |
| 46 | 42 | $term_id = (int) $term_id; |
| 47 | 43 | |
| @@ -46,12 +42,10 @@ | ||
| 46 | 42 | $term_id = (int) $term_id; |
| 47 | 43 | |
| 48 | 44 | $old_lang = $this->get_language( $term_id ); |
| 49 | 45 | $old_lang = $old_lang ? $old_lang->tl_term_id : ''; |
| 46 | + $lang = $lang ? $this->model->get_language( $lang )->tl_term_id : ''; | |
| 50 | 47 | |
| 51 | - $lang = $this->model->get_language( $lang ); | |
| 52 | - $lang = $lang ? $lang->tl_term_id : ''; | |
| 53 | - | |
| 54 | 48 | if ( $old_lang !== $lang ) { |
| 55 | 49 | wp_set_object_terms( $term_id, $lang, 'term_language' ); |
| 56 | 50 | |
| 57 | 51 | // Add translation group for correct WXR export |
| @@ -69,9 +63,8 @@ | ||
| 69 | 63 | * |
| 70 | 64 | * @since 0.5 |
| 71 | 65 | * |
| 72 | 66 | * @param int $term_id term id |
| 73 | - * @return void | |
| 74 | 67 | */ |
| 75 | 68 | public function delete_language( $term_id ) { |
| 76 | 69 | wp_delete_object_term_relationships( $term_id, 'term_language' ); |
| 77 | 70 | } |
| @@ -82,9 +75,9 @@ | ||
| 82 | 75 | * @since 0.1 |
| 83 | 76 | * |
| 84 | 77 | * @param int|string $value term id or term slug |
| 85 | 78 | * @param string $taxonomy optional taxonomy needed when the term slug is passed as first parameter |
| 86 | - * @return PLL_Language|false PLL_Language object, false if no language is associated to that term | |
| 79 | + * @return bool|object PLL_Language object, false if no language is associated to that term | |
| 87 | 80 | */ |
| 88 | 81 | public function get_language( $value, $taxonomy = '' ) { |
| 89 | 82 | if ( is_numeric( $value ) ) { |
| 90 | 83 | $term_id = $value; |
| @@ -91,12 +84,9 @@ | ||
| 91 | 84 | } |
| 92 | 85 | |
| 93 | 86 | // get_term_by still not cached in WP 3.5.1 but internally, the function is always called by term_id |
| 94 | 87 | elseif ( is_string( $value ) && $taxonomy ) { |
| 95 | - $term = get_term_by( 'slug', $value, $taxonomy ); | |
| 96 | - if ( $term instanceof WP_Term ) { | |
| 97 | - $term_id = $term->term_id; | |
| 98 | - } | |
| 88 | + $term_id = wpcom_vip_get_term_by( 'slug', $value, $taxonomy )->term_id; | |
| 99 | 89 | } |
| 100 | 90 | |
| 101 | 91 | // Get the language and make sure it is a PLL_Language object |
| 102 | 92 | return isset( $term_id ) && ( $lang = $this->get_object_term( $term_id, 'term_language' ) ) ? $this->model->get_language( $lang->term_id ) : false; |
| @@ -102,15 +92,14 @@ | ||
| 102 | 92 | return isset( $term_id ) && ( $lang = $this->get_object_term( $term_id, 'term_language' ) ) ? $this->model->get_language( $lang->term_id ) : false; |
| 103 | 93 | } |
| 104 | 94 | |
| 105 | 95 | /** |
| 106 | - * Tells whether a translation term must updated. | |
| 96 | + * Tells whether a translation term must updated | |
| 107 | 97 | * |
| 108 | 98 | * @since 2.3 |
| 109 | 99 | * |
| 110 | - * @param int $id Post id or term id. | |
| 111 | - * @param int[] $translations An associative array of translations with language code as key and translation id as value. | |
| 112 | - * @return bool | |
| 100 | + * @param array $id Post id or term id | |
| 101 | + * @param array $translations An associative array of translations with language code as key and translation id as value | |
| 113 | 102 | */ |
| 114 | 103 | protected function should_update_translation_group( $id, $translations ) { |
| 115 | 104 | // Don't do anything if no translations have been added to the group |
| 116 | 105 | $old_translations = $this->get_translations( $id ); |
| @@ -128,9 +117,8 @@ | ||
| 128 | 117 | * |
| 129 | 118 | * @since 0.5 |
| 130 | 119 | * |
| 131 | 120 | * @param int $id term id |
| 132 | - * @return void | |
| 133 | 121 | */ |
| 134 | 122 | public function delete_translation( $id ) { |
| 135 | 123 | global $wpdb; |
| 136 | 124 | $slug = array_search( $id, $this->get_translations( $id ) ); // in case some plugin stores the same value with different key |
| @@ -139,9 +127,9 @@ | ||
| 139 | 127 | wp_delete_object_term_relationships( $id, 'term_translations' ); |
| 140 | 128 | |
| 141 | 129 | if ( ! doing_action( 'pre_delete_term' ) && $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( * ) FROM $wpdb->terms WHERE term_id = %d;", $id ) ) ) { |
| 142 | 130 | // Always keep a group for terms to allow relationships remap when importing from a WXR file |
| 143 | - $translations = array( $slug => $id ); | |
| 131 | + $translations[ $slug ] = $id; | |
| 144 | 132 | wp_insert_term( $group = uniqid( 'pll_' ), 'term_translations', array( 'description' => maybe_serialize( $translations ) ) ); |
| 145 | 133 | wp_set_object_terms( $id, $group, 'term_translations' ); |
| 146 | 134 | } |
| 147 | 135 | } |
| @@ -160,19 +148,17 @@ | ||
| 160 | 148 | return " INNER JOIN $wpdb->term_relationships AS pll_tr ON pll_tr.object_id = $alias.term_id"; |
| 161 | 149 | } |
| 162 | 150 | |
| 163 | 151 | /** |
| 164 | - * Caches the language and translations when terms are queried by get_terms(). | |
| 152 | + * Cache language and translations when terms are queried by get_terms | |
| 165 | 153 | * |
| 166 | 154 | * @since 1.2 |
| 167 | 155 | * |
| 168 | - * @param WP_Term[]|int[] $terms Queried terms. | |
| 169 | - * @param string[] $taxonomies Queried taxonomies. | |
| 170 | - * @return WP_Term[]|int[] Unmodified $terms. | |
| 156 | + * @param array $terms queried terms | |
| 157 | + * @param array $taxonomies queried taxonomies | |
| 158 | + * @return array unmodified $terms | |
| 171 | 159 | */ |
| 172 | 160 | public function _prime_terms_cache( $terms, $taxonomies ) { |
| 173 | - $term_ids = array(); | |
| 174 | - | |
| 175 | 161 | if ( is_array( $terms ) && $this->model->is_translated_taxonomy( $taxonomies ) ) { |
| 176 | 162 | foreach ( $terms as $term ) { |
| 177 | 163 | $term_ids[] = is_object( $term ) ? $term->term_id : (int) $term; |
| 178 | 164 | } |
| @@ -184,18 +170,19 @@ | ||
| 184 | 170 | return $terms; |
| 185 | 171 | } |
| 186 | 172 | |
| 187 | 173 | /** |
| 188 | - * When terms are found for posts, add their language and translations to cache. | |
| 174 | + * When terms are found for posts, add their language and translations to cache | |
| 189 | 175 | * |
| 190 | 176 | * @since 1.2 |
| 191 | 177 | * |
| 192 | - * @param WP_Term[] $terms Array of terms for the given object or objects. | |
| 193 | - * @param int[] $object_ids Array of object IDs for which terms were retrieved. | |
| 194 | - * @param string[] $taxonomies Array of taxonomy names from which terms were retrieved. | |
| 195 | - * @return WP_Term[] Unmodified $terms. | |
| 178 | + * @param array $terms terms found | |
| 179 | + * @param array $object_ids not used | |
| 180 | + * @param array $taxonomies terms taxonomies | |
| 181 | + * @return array unmodified $terms | |
| 196 | 182 | */ |
| 197 | 183 | public function wp_get_object_terms( $terms, $object_ids, $taxonomies ) { |
| 184 | + $taxonomies = explode( "', '", trim( $taxonomies, "'" ) ); | |
| 198 | 185 | if ( ! in_array( 'term_translations', $taxonomies ) ) { |
| 199 | 186 | $this->_prime_terms_cache( $terms, $taxonomies ); |
| 200 | 187 | } |
| 201 | 188 | return $terms; |
| @@ -201,14 +188,13 @@ | ||
| 201 | 188 | return $terms; |
| 202 | 189 | } |
| 203 | 190 | |
| 204 | 191 | /** |
| 205 | - * When the term cache is cleaned, cleans the object term cache too. | |
| 192 | + * When the term cache is cleaned, clean the object term cache too | |
| 206 | 193 | * |
| 207 | 194 | * @since 2.0 |
| 208 | 195 | * |
| 209 | - * @param int[] $ids An array of term IDs. | |
| 210 | - * @return void | |
| 196 | + * @param array $ids An array of term IDs. | |
| 211 | 197 | */ |
| 212 | 198 | public function clean_term_cache( $ids ) { |
| 213 | 199 | clean_object_term_cache( $ids, 'term' ); |
| 214 | 200 | } |