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