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