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