PluginProbe
Polylang / 1.5
Polylang v1.5
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
← All changes | include/model.php +571 -461 2.7.11.5 View file →
@@ -1,643 +1,753 @@
1 1 <?php
2 2
3 -/**
4 - * Setups the language and translations model based on WordPress taxonomies
3 +/*
4 + * setups the language and translations model based on WordPress taxonomies
5 5 *
6 6 * @since 1.2
7 7 */
8 8 class PLL_Model {
9 - public $cache; // Our internal non persistent cache object
10 9 public $options;
11 - public $post, $term; // Translated objects models
10 + private $languages; // used to cache the list of languages
12 11
13 - /**
14 - * Constructor
15 - * setups translated objects sub models
16 - * setups filters and actions
12 + /*
13 + * constructor: registers custom taxonomies and setups filters and actions
17 14 *
18 15 * @since 1.2
19 16 *
20 17 * @param array $options Polylang options
21 18 */
22 - public function __construct( &$options ) {
19 + public function __construct(&$options) {
23 20 $this->options = &$options;
24 21
25 - $this->cache = new PLL_Cache();
26 - $this->post = new PLL_Translated_Post( $this ); // translated post sub model
27 - $this->term = new PLL_Translated_Term( $this ); // translated term sub model
22 + // register our taxonomies as soon as possible
23 + // this is early registration, not ready for rewrite rules as wp_rewrite will be setup later
24 + $args = array('label' => false, 'public' => false, 'query_var' => false, 'rewrite' => false, '_pll' => true);
25 + register_taxonomy('language', null, $args);
26 + register_taxonomy('term_language', 'term', $args);
27 + register_taxonomy('term_translations', 'term', $args);
28 + $args['update_count_callback'] = '_update_generic_term_count'; // count *all* posts to avoid deleting in clean_translations_terms
29 + register_taxonomy('post_translations', null, $args);
28 30
29 - // We need to clean languages cache when editing a language and when modifying the permalink structure
30 - add_action( 'edited_term_taxonomy', array( $this, 'clean_languages_cache' ), 10, 2 );
31 - add_action( 'update_option_permalink_structure', array( $this, 'clean_languages_cache' ) );
32 - add_action( 'update_option_siteurl', array( $this, 'clean_languages_cache' ) );
33 - add_action( 'update_option_home', array( $this, 'clean_languages_cache' ) );
31 + add_filter('get_terms', array(&$this, '_prime_terms_cache'), 10, 2);
32 + add_filter('wp_get_object_terms', array(&$this, 'wp_get_object_terms'), 10, 3);
34 33
35 - add_filter( 'get_terms_args', array( $this, 'get_terms_args' ) );
34 + // we need to clean languages cache when editing a language,
35 + // when editing page of front or when modifying the permalink structure
36 + add_action('edited_term_taxonomy', array(&$this, 'clean_languages_cache'), 10, 2);
37 + add_action('update_option_page_on_front', array(&$this, 'clean_languages_cache'));
38 + add_action('update_option_permalink_structure', array(&$this, 'clean_languages_cache'));
36 39
37 - // Just in case someone would like to display the language description ;- )
38 - add_filter( 'language_description', '__return_empty_string' );
40 + // registers completely the language taxonomy
41 + add_action('setup_theme', array(&$this, 'register_taxonomy'), 1);
42 +
43 + // setups post types to translate
44 + add_action('registered_post_type', array(&$this, 'registered_post_type'));
45 +
46 + // just in case someone would like to display the language description ;-)
47 + add_filter('language_description', create_function('$v', "return '';"));
39 48 }
40 49
41 - /**
42 - * Returns the list of available languages
43 - * caches the list in a db transient ( except flags ), unless PLL_CACHE_LANGUAGES is set to false
44 - * caches the list ( with flags ) in the private property $languages
50 + /*
51 + * cache language and translations when terms are queried by get_terms
45 52 *
46 - * List of parameters accepted in $args:
53 + * @since 1.2
47 54 *
48 - * hide_empty => hides languages with no posts if set to true ( defaults to false )
49 - * fields => return only that field if set ( see PLL_Language for a list of fields )
55 + * @param array $terms queried terms
56 + * @param array $taxonomies queried taxonomies
57 + * @return array unmodified $terms
58 + */
59 + public function _prime_terms_cache($terms, $taxonomies) {
60 + if ($this->is_translated_taxonomy($taxonomies)) {
61 + foreach ($terms as $term) {
62 + $term_ids[] = is_object($term) ? $term->term_id : $term;
63 + }
64 + }
65 +
66 + if (!empty($term_ids))
67 + update_object_term_cache(array_unique($term_ids), 'term'); // adds language and translation of terms to cache
68 + return $terms;
69 + }
70 +
71 + /*
72 + * when terms are found for posts, add their language and translations to cache
50 73 *
74 + * @since 1.2
75 + *
76 + * @param array $terms terms found
77 + * @param array $object_ids not used
78 + * @param array $taxonomies terms taxonomies
79 + * @return array unmodified $terms
80 + */
81 + // FIXME is that useful? or even desirable?
82 + public function wp_get_object_terms($terms, $object_ids, $taxonomies) {
83 + $taxonomies = explode("', '", trim($taxonomies, "'"));
84 + if (!in_array('term_translations', $taxonomies))
85 + $this->_prime_terms_cache($terms, $taxonomies);
86 + return $terms;
87 + }
88 +
89 + /*
90 + * wrap wp_get_object_terms to cache it and return only one object
91 + * inspired by the function get_the_terms
92 + *
93 + * @since 1.2
94 + *
95 + * @param int $object_id post_id or term_id
96 + * @param string $taxonomy Polylang taxonomy depending if we are looking for a post (or term) language (or translation)
97 + * @return bool|object the term associated to the object in the requested taxonomy if exists, false otherwise
98 + */
99 + protected function get_object_term($object_id, $taxonomy) {
100 + $term = get_object_term_cache($object_id, $taxonomy);
101 +
102 + if ( false === $term ) {
103 + // query language and translations at the same time
104 + $taxonomies = (false !== strpos($taxonomy, 'term_')) ?
105 + array('term_language', 'term_translations') :
106 + array('language', 'post_translations');
107 +
108 + foreach (wp_get_object_terms($object_id, $taxonomies) as $t) {
109 + wp_cache_add($object_id, array($t), $t->taxonomy . '_relationships'); // store it the way WP wants it
110 + if ($t->taxonomy == $taxonomy)
111 + $term = $t;
112 + }
113 + }
114 + else {
115 + $term = reset($term);
116 + }
117 +
118 + return empty($term) ? false : $term;
119 + }
120 +
121 + /*
122 + * returns the list of available languages
123 + * caches the list in a db transient (except flags)
124 + * caches the list (with flags) in the private property $languages
125 + *
126 + * list of parameters accepted in $args:
127 + *
128 + * hide_empty => hides languages with no posts if set to true (defaults to false)
129 + * fields => return only that field if set (see PLL_Language for a list of fields)
130 + *
51 131 * @since 0.1
52 132 *
53 133 * @param array $args
54 134 * @return array|string|int list of PLL_Language objects or PLL_Language object properties
55 135 */
56 - public function get_languages_list( $args = array() ) {
57 - if ( false === $languages = $this->cache->get( 'languages' ) ) {
136 + public function get_languages_list($args = array()) {
137 + if (empty($this->languages)) {
138 + if (false === ($languages = get_transient('pll_languages_list'))) {
139 + $languages = get_terms('language', array('hide_empty' => false, 'orderby'=> 'term_group'));
140 + $languages = empty($languages) || is_wp_error($languages) ? array() : $languages;
58 141
59 - // Create the languages from taxonomies
60 - if ( ( defined( 'PLL_CACHE_LANGUAGES' ) && ! PLL_CACHE_LANGUAGES ) || false === ( $languages = get_transient( 'pll_languages_list' ) ) ) {
61 - $languages = get_terms( 'language', array( 'hide_empty' => false, 'orderby' => 'term_group' ) );
62 - $languages = empty( $languages ) || is_wp_error( $languages ) ? array() : $languages;
142 + $term_languages = get_terms('term_language', array('hide_empty' => false));
143 + $term_languages = empty($term_languages) || is_wp_error($term_languages) ?
144 + array() : array_combine(wp_list_pluck($term_languages, 'name'), $term_languages);
63 145
64 - $term_languages = get_terms( 'term_language', array( 'hide_empty' => false ) );
65 - $term_languages = empty( $term_languages ) || is_wp_error( $term_languages ) ?
66 - array() : array_combine( wp_list_pluck( $term_languages, 'slug' ), $term_languages );
67 -
68 - if ( ! empty( $languages ) && ! empty( $term_languages ) ) {
69 - // Don't use array_map + create_function to instantiate an autoloaded class as it breaks badly in old versions of PHP
70 - foreach ( $languages as $k => $v ) {
71 - $languages[ $k ] = new PLL_Language( $v, $term_languages[ 'pll_' . $v->slug ] );
146 + if (!empty($languages) && !empty($term_languages)) {
147 + // don't use array_map + create_function to instantiate an autoloaded class as it breaks badly in old versions of PHP
148 + foreach ($languages as $k => $v) {
149 + $languages[$k] = new PLL_Language($v, $term_languages[$v->name]);
72 150 }
73 -
74 - // We will need the languages list to allow its access in the filter below
75 - $this->cache->set( 'languages', $languages );
76 -
77 - /**
78 - * Filter the list of languages *before* it is stored in the persistent cache
79 - * /!\ this filter is fired *before* the $polylang object is available
80 - *
81 - * @since 1.7.5
82 - *
83 - * @param array $languages the list of language objects
84 - * @param object $model PLL_Model object
85 - */
86 - $languages = apply_filters( 'pll_languages_list', $languages, $this );
87 -
88 - // Don't store directly objects as it badly break with some hosts ( GoDaddy ) due to race conditions when using object cache
89 - // Thanks to captin411 for catching this!
90 - // See https://wordpress.org/support/topic/fatal-error-pll_model_languages_list?replies=8#post-6782255;
91 - set_transient( 'pll_languages_list', array_map( 'get_object_vars', $languages ) );
92 151 }
93 152 else {
94 - $languages = array(); // In case something went wrong
153 + $languages = array(); // in case something went wrong
95 154 }
96 155 }
97 156
98 - // Create the languages directly from arrays stored in transients
99 - else {
100 - foreach ( $languages as $k => $v ) {
101 - $languages[ $k ] = new PLL_Language( $v );
102 - }
103 - }
157 + $this->languages = $languages;
104 158
105 - // Custom flags
106 - if ( ! PLL_ADMIN ) {
107 - foreach ( $languages as $language ) {
108 - $language->set_custom_flag();
109 - }
159 + // need to wait for $wp_rewrite availibility to set homepage urls
160 + did_action('setup_theme') ? $this->_languages_list() : add_action('setup_theme', array(&$this, '_languages_list'));
161 + }
162 +
163 + $args = wp_parse_args($args, array('hide_empty' => false));
164 +
165 + // remove empty languages if requested
166 + $languages = array_filter($this->languages, create_function('$v', sprintf('return $v->count || !%d;', $args['hide_empty'])));
167 +
168 + return empty($args['fields']) ? $languages : wp_list_pluck($languages, $args['fields']);
169 + }
170 +
171 + /*
172 + * fills home urls and flags in language list and set transient in db
173 + * delayed to be sure we have access to $wp_rewrite for home urls
174 + * home urls are not cached in db if PLL_CACHE_HOME_URL is set to false
175 + *
176 + * @since 1.4
177 + */
178 + public function _languages_list() {
179 + if (false === get_transient('pll_languages_list')) {
180 + if (!defined('PLL_CACHE_HOME_URL') || PLL_CACHE_HOME_URL) {
181 + foreach ($this->languages as $language)
182 + $language->set_home_url();
110 183 }
111 -
112 - /**
113 - * Filter the list of languages *after* it is stored in the persistent cache
114 - * /!\ this filter is fired *before* the $polylang object is available
115 - *
116 - * @since 1.8
117 - *
118 - * @param array $languages the list of language objects
119 - */
120 - $languages = apply_filters( 'pll_after_languages_cache', $languages );
121 - $this->cache->set( 'languages', $languages );
184 + set_transient('pll_languages_list', $this->languages);
122 185 }
123 186
124 - $args = wp_parse_args( $args, array( 'hide_empty' => false ) );
187 + foreach ($this->languages as $language) {
188 + if (defined('PLL_CACHE_HOME_URL') && !PLL_CACHE_HOME_URL)
189 + $language->set_home_url();
125 190
126 - // Remove empty languages if requested
127 - if ( $args['hide_empty'] ) {
128 - $languages = wp_list_filter( $languages, array( 'count' => 0 ), 'NOT' );
191 + // add flags (not in db cache as they may be different on frontend and admin)
192 + $language->set_flag();
129 193 }
130 -
131 - return empty( $args['fields'] ) ? $languages : wp_list_pluck( $languages, $args['fields'] );
132 194 }
133 195
134 - /**
135 - * Cleans language cache
196 + /*
197 + * cleans language cache
136 198 * can be called directly with no parameter
137 199 * called by the 'edited_term_taxonomy' filter with 2 parameters when count needs to be updated
138 200 *
139 201 * @since 1.2
140 202 *
141 - * @param int $term not used
203 + * @param int $term not used
142 204 * @param string $taxonomy taxonomy name
143 205 */
144 - public function clean_languages_cache( $term = 0, $taxonomy = null ) {
145 - if ( empty( $taxonomy ) || 'language' == $taxonomy ) {
146 - delete_transient( 'pll_languages_list' );
147 - $this->cache->clean();
206 + public function clean_languages_cache($term = 0, $taxonomy = null) {
207 + if (empty($taxonomy->name) || 'language' == $taxonomy->name) {
208 + delete_transient('pll_languages_list');
209 + $this->languages = array();
148 210 }
149 211 }
150 212
151 - /**
152 - * Don't query term metas when only our taxonomies are queried
213 + /*
214 + * returns the language by its term_id, tl_term_id, slug or locale
153 215 *
154 - * @since 2.3
216 + * @since 0.1
155 217 *
156 - * @param array $args WP_Term_Query arguments
157 - * @return array
218 + * @param int|string term_id, tl_term_id, slug or locale of the queried language
219 + * @return object|bool PLL_Language object, false if no language found
158 220 */
159 - public function get_terms_args( $args ) {
160 - if ( isset( $args['taxonomy'] ) && ! array_diff( (array) $args['taxonomy'], array( 'language', 'term_language', 'post_translations', 'term_translations' ) ) ) {
161 - $args['update_term_meta_cache'] = false;
221 + public function get_language($value) {
222 + static $language;
223 +
224 + if (is_object($value))
225 + return $this->get_language($value->term_id); // will force cast to PLL_Language
226 +
227 + if (empty($language[$value])) {
228 + foreach ($this->get_languages_list() as $lang)
229 + $language[$lang->term_id] = $language[$lang->tl_term_id] = $language[$lang->slug] = $language[$lang->locale] = $lang;
162 230 }
163 - return $args;
231 +
232 + return empty($language[$value]) ? false : $language[$value];
164 233 }
165 234
166 - /**
167 - * Returns the language by its term_id, tl_term_id, slug or locale
235 + /*
236 + * removes unused translations terms in database
168 237 *
169 - * @since 0.1
238 + * @since 1.5
170 239 *
171 - * @param int|string $value term_id, tl_term_id, slug or locale of the queried language
172 - * @return object|bool PLL_Language object, false if no language found
240 + * @param string $type either 'post' or 'term'
173 241 */
174 - public function get_language( $value ) {
175 - if ( is_object( $value ) ) {
176 - return $value instanceof PLL_Language ? $value : $this->get_language( $value->term_id ); // will force cast to PLL_Language
242 + protected function clean_translations_terms($type) {
243 + global $wpdb;
244 + $ids = $wpdb->get_col("SELECT term_id FROM $wpdb->term_taxonomy WHERE taxonomy IN ('{$type}_translations') AND count = 0");
245 + foreach ($ids as $id)
246 + wp_delete_term((int) $id, $type . '_translations');
247 + }
248 +
249 + /*
250 + * saves translations for posts or terms
251 + *
252 + * @since 0.5
253 + *
254 + * @param string $type either 'post' or 'term'
255 + * @param int $id post id or term id
256 + * @param array $translations: an associative array of translations with language code as key and translation id as value
257 + */
258 + public function save_translations($type, $id, $translations) {
259 + if (($lang = call_user_func(array(&$this, 'get_'.$type.'_language'), $id)) && isset($translations) && is_array($translations)) {
260 +
261 + $translations = array_merge(array($lang->slug => $id), $translations); // make sure this object is in translations
262 + $translations = array_diff($translations, array(0)); // don't keep non translated languages
263 +
264 + // unlink removed translations
265 + $old_translations = $this->get_translations($type, $id);
266 + foreach (array_diff_assoc($old_translations, $translations) as $object_id)
267 + $this->delete_translation($type, $object_id);
268 +
269 + // don't create a translation group for untranslated posts as it is useless
270 + // but we need one for terms to allow relationships remap when importing from a WXR file
271 + if ('term' == $type || count($translations) > 1) {
272 + $terms = wp_get_object_terms($translations, $type . '_translations');
273 + $term = reset($terms);
274 +
275 + // create a new term if necessary
276 + if (empty($term)) {
277 + wp_insert_term($group = uniqid('pll_'), $type . '_translations', array('description' => serialize($translations)));
278 + }
279 + else {
280 + // take care not to overwrite extra data stored in description field, if any
281 + $d = unserialize($term->description);
282 + $d = is_array($d) ? array_diff_key($d, $old_translations) : array(); // remove old translations
283 + $d = array_merge($d, $translations); // add new one
284 + wp_update_term($group = (int) $term->term_id, $type . '_translations', array('description' => serialize($d)));
285 + }
286 +
287 + // link all translations to the new term
288 + foreach($translations as $p)
289 + wp_set_object_terms($p, $group, $type . '_translations');
290 +
291 + // clean unused terms to avoid orphans in db
292 + $this->clean_translations_terms($type);
293 + }
177 294 }
295 + }
178 296
179 - if ( false === $return = $this->cache->get( 'language:' . $value ) ) {
180 - foreach ( $this->get_languages_list() as $lang ) {
181 - $this->cache->set( 'language:' . $lang->term_id, $lang );
182 - $this->cache->set( 'language:' . $lang->tl_term_id, $lang );
183 - $this->cache->set( 'language:' . $lang->slug, $lang );
184 - $this->cache->set( 'language:' . $lang->locale, $lang );
185 - $this->cache->set( 'language:' . $lang->w3c, $lang );
297 + /*
298 + * deletes a translation of a post or term
299 + *
300 + * @since 0.5
301 + *
302 + * @param string $type either 'post' or 'term'
303 + * @param int $id post id or term id
304 + */
305 + public function delete_translation($type, $id) {
306 + $term = $this->get_object_term($id, $type . '_translations');
307 +
308 + if (!empty($term)) {
309 + $d = unserialize($term->description);
310 + $slug = array_search($id, $this->get_translations($type, $id)); // in case some plugin stores the same value with different key
311 + unset($d[$slug]);
312 +
313 + wp_update_term((int) $term->term_id, $type . '_translations', array('description' => serialize($d)));
314 +
315 + if ('post' == $type)
316 + wp_set_object_terms($id, null, $type . '_translations');
317 + else {
318 + // always keep a group for terms to allow relationships remap when importing from a WXR file
319 + $translations[$slug] = $id;
320 + wp_insert_term($group = uniqid('pll_'), $type . '_translations', array('description' => serialize($translations)));
321 + wp_set_object_terms($id, $group, $type . '_translations');
186 322 }
187 - $return = $this->cache->get( 'language:' . $value );
323 +
324 + // clean unused terms to avoid orphans in db
325 + $this->clean_translations_terms($type);
188 326 }
327 + }
189 328
190 - return $return;
329 + /*
330 + * returns the id of the translation of a post or term
331 + *
332 + * @since 0.5
333 + *
334 + * @param string $type either 'post' or 'term'
335 + * @param int $id post id or term id
336 + * @param object|string $lang object or slug
337 + * @return bool|int post id or term id of the translation, flase if there is none
338 + */
339 + public function get_translation($type, $id, $lang) {
340 + if (!$lang = $this->get_language($lang))
341 + return false;
342 +
343 + $translations = $this->get_translations($type, $id);
344 +
345 + return isset($translations[$lang->slug]) ? (int) $translations[$lang->slug] : false;
191 346 }
192 347
193 - /**
194 - * Adds terms clauses to get_terms to filter them by languages - used in both frontend and admin
348 + /*
349 + * returns an array of translations of a post or term
195 350 *
196 - * @since 1.2
351 + * @since 0.5
197 352 *
198 - * @param array $clauses the list of sql clauses in terms query
199 - * @param object $lang PLL_Language object
200 - * @return array modified list of clauses
353 + * @param string $type either 'post' or 'term'
354 + * @param int $id post id or term id
355 + * @return array an associative array of translations with language code as key and translation id as value
201 356 */
202 - public function terms_clauses( $clauses, $lang ) {
203 - if ( ! empty( $lang ) && false === strpos( $clauses['join'], 'pll_tr' ) ) {
204 - $clauses['join'] .= $this->term->join_clause();
205 - $clauses['where'] .= $this->term->where_clause( $lang );
206 - }
207 - return $clauses;
357 + public function get_translations($type, $id) {
358 + $type = ($type == 'post' || $this->is_translated_post_type($type)) ? 'post' : (($type == 'term' || $this->is_translated_taxonomy($type)) ? 'term' : false);
359 + $translations = $type && ($term = $this->get_object_term($id, $type . '_translations')) && !empty($term) ? unserialize($term->description) : array();
360 +
361 + // make sure we return only translations (thus we allow plugins to store other informations in the array)
362 + return array_intersect_key($translations, array_flip($this->get_languages_list(array('fields' => 'slug'))));
208 363 }
209 364
210 - /**
211 - * Returns post types that need to be translated
212 - * the post types list is cached for better better performance
213 - * wait for 'after_setup_theme' to apply the cache to allow themes adding the filter in functions.php
365 + /*
366 + * store the post language in the database
214 367 *
215 - * @since 1.2
368 + * @since 0.6
216 369 *
217 - * @param bool $filter true if we should return only valid registered post types
218 - * @return array post type names for which Polylang manages languages and translations
370 + * @param int $post_id post id
371 + * @param int|string|object language (term_id or slug or object)
219 372 */
220 - public function get_translated_post_types( $filter = true ) {
221 - if ( false === $post_types = $this->cache->get( 'post_types' ) ) {
222 - $post_types = array( 'post' => 'post', 'page' => 'page', 'wp_block' => 'wp_block' );
373 + public function set_post_language($post_id, $lang) {
374 + wp_set_post_terms($post_id, $lang ? $this->get_language($lang)->slug : '', 'language' );
375 + }
223 376
224 - if ( ! empty( $this->options['media_support'] ) ) {
225 - $post_types['attachment'] = 'attachment';
226 - }
377 + /*
378 + * returns the language of a post
379 + *
380 + * @since 0.1
381 + *
382 + * @param int $post_id post id
383 + * @return bool|object PLL_Language object, false if no language is associated to that post
384 + */
385 + public function get_post_language($post_id) {
386 + $lang = $this->get_object_term($post_id, 'language' );
387 + return ($lang) ? $this->get_language($lang) : false;
388 + }
227 389
228 - if ( ! empty( $this->options['post_types'] ) && is_array( $this->options['post_types'] ) ) {
229 - $post_types = array_merge( $post_types, array_combine( $this->options['post_types'], $this->options['post_types'] ) );
230 - }
390 + /*
391 + * among the post and its translations, returns the id of the post which is in $lang
392 + *
393 + * @since 0.1
394 + *
395 + * @param int $post_id post id
396 + * @param int|string|object language (term_id or slug or object)
397 + * @return bool|int the translation post id if exists, otherwise the post id, false if the post has no language
398 + */
399 + public function get_post($post_id, $lang) {
400 + $post_lang = $this->get_post_language($post_id); // FIXME is this necessary?
401 + if (!$lang || !$post_lang)
402 + return false;
231 403
232 - /**
233 - * Filter the list of post types available for translation.
234 - * The default are post types which have the parameter ‘public’ set to true.
235 - * The filter must be added soon in the WordPress loading process:
236 - * in a function hooked to ‘plugins_loaded’ or directly in functions.php for themes.
237 - *
238 - * @since 0.8
239 - *
240 - * @param array $post_types list of post type names
241 - * @param bool $is_settings true when displaying the list of custom post types in Polylang settings
242 - */
243 - $post_types = apply_filters( 'pll_get_post_types', $post_types, false );
404 + $lang = $this->get_language($lang);
405 + return $post_lang->term_id == $lang->term_id ? $post_id : $this->get_translation('post', $post_id, $lang);
406 + }
244 407
245 - if ( did_action( 'after_setup_theme' ) ) {
246 - $this->cache->set( 'post_types', $post_types );
247 - }
248 - }
408 + /*
409 + * stores the term language in the database
410 + *
411 + * @since 0.6
412 + *
413 + * @param int $term_id term id
414 + * @param int|string|object language (term_id or slug or object)
415 + */
416 + public function set_term_language($term_id, $lang) {
417 + wp_set_object_terms($term_id, $lang ? $this->get_language($lang)->tl_term_id : '', 'term_language');
418 + }
249 419
250 - return $filter ? array_intersect( $post_types, get_post_types() ) : $post_types;
420 + /*
421 + * removes the term language in database
422 + *
423 + * @since 0.5
424 + *
425 + * @param int $term_id term id
426 + */
427 + public function delete_term_language($term_id) {
428 + wp_delete_object_term_relationships($term_id, 'term_language');
251 429 }
252 430
253 - /**
254 - * Returns true if Polylang manages languages and translations for this post type
431 + /*
432 + * returns the language of a term
255 433 *
434 + * @since 0.1
435 + *
436 + * @param int|string $value term id or term slug
437 + * @param string $taxonomy optional taxonomy needed when the term slug is passed as first parameter
438 + * @return bool|object PLL_Language object, false if no language is associated to that term
439 + */
440 + public function get_term_language($value, $taxonomy = '') {
441 + if (is_numeric($value))
442 + $term_id = $value;
443 +
444 + // get_term_by still not cached in WP 3.5.1 but internally, the function is always called by term_id
445 + elseif (is_string($value) && $taxonomy)
446 + $term_id = get_term_by('slug', $value , $taxonomy)->term_id;
447 +
448 + $lang = $this->get_object_term($term_id, 'term_language');
449 +
450 + // switch to PLL_Language
451 + return ($lang) ? $this->get_language($lang->term_id) : false;
452 + }
453 +
454 + /*
455 + * among the term and its translations, returns the id of the term which is in $lang
456 + *
457 + * @since 0.1
458 + *
459 + * @param int $term_id term id
460 + * @param int|string|object language (term_id or slug or object)
461 + * @return bool|int the translation term id if exists, otherwise the term id, false if the term has no language
462 + */
463 + public function get_term($term_id, $lang) {
464 + $lg = $this->get_term_language($term_id); // FIXME is this necessary?
465 + if (!$lang || !$lg)
466 + return false;
467 +
468 + $lang = $this->get_language($lang);
469 + return $lg->term_id == $lang->term_id ? $term_id : $this->get_translation('term', $term_id, $lang);
470 + }
471 +
472 + /*
473 + * a join clause to add to sql queries when filtering by language is needed directly in query
474 + *
256 475 * @since 1.2
257 476 *
258 - * @param string|array $post_type post type name or array of post type names
259 - * @return bool
477 + * @param string $type either 'post' or 'term'
478 + * @return string join clause
260 479 */
261 - public function is_translated_post_type( $post_type ) {
262 - $post_types = $this->get_translated_post_types( false );
263 - return ( is_array( $post_type ) && array_intersect( $post_type, $post_types ) || in_array( $post_type, $post_types ) || 'any' === $post_type && ! empty( $post_types ) );
480 + public function join_clause($type) {
481 + global $wpdb;
482 + return " INNER JOIN $wpdb->term_relationships AS pll_tr ON pll_tr.object_id = " . ('term' == $type ? "t.term_id" : "ID");
264 483 }
265 484
266 - /**
267 - * Return taxonomies that need to be translated
485 + /*
486 + * a where clause to add to sql queries when filtering by language is needed directly in query
268 487 *
269 488 * @since 1.2
270 489 *
271 - * @param bool $filter true if we should return only valid registered taxonomies
272 - * @return array array of registered taxonomy names for which Polylang manages languages and translations
490 + * @param object|array|string $lang a PLL_Language object or a comma separated list of languag slug or an array of language slugs
491 + * @param string $type either 'post' or 'term'
492 + * @return string where clause
273 493 */
274 - public function get_translated_taxonomies( $filter = true ) {
275 - if ( false === $taxonomies = $this->cache->get( 'taxonomies' ) ) {
276 - $taxonomies = array( 'category' => 'category', 'post_tag' => 'post_tag' );
494 + public function where_clause($lang, $type) {
495 + global $wpdb;
496 + $tt_id = 'term' == $type ? 'tl_term_taxonomy_id' : 'term_taxonomy_id';
277 497
278 - if ( ! empty( $this->options['taxonomies'] ) && is_array( $this->options['taxonomies'] ) ) {
279 - $taxonomies = array_merge( $taxonomies, array_combine( $this->options['taxonomies'], $this->options['taxonomies'] ) );
280 - }
498 + // $lang is an object
499 + // generally the case if the query is coming from Polylang
500 + if (is_object($lang))
501 + return $wpdb->prepare(" AND pll_tr.term_taxonomy_id = %d", $lang->$tt_id);
281 502
282 - /**
283 - * Filter the list of taxonomies available for translation.
284 - * The default are taxonomies which have the parameter ‘public’ set to true.
285 - * The filter must be added soon in the WordPress loading process:
286 - * in a function hooked to ‘plugins_loaded’ or directly in functions.php for themes.
287 - *
288 - * @since 0.8
289 - *
290 - * @param array $taxonomies list of taxonomy names
291 - * @param bool $is_settings true when displaying the list of custom taxonomies in Polylang settings
292 - */
293 - $taxonomies = apply_filters( 'pll_get_taxonomies', $taxonomies, false );
294 - if ( did_action( 'after_setup_theme' ) ) {
295 - $this->cache->set( 'taxonomies', $taxonomies );
296 - }
297 - }
503 + // $lang is a comma separated list of slugs (or an array of slugs)
504 + // generally the case is the query is coming from outside with 'lang' parameter
505 + $slugs = is_array($lang) ? $lang : explode(',', $lang);
506 + foreach ($slugs as $slug)
507 + $languages[] = (int) $this->get_language($slug)->$tt_id;
298 508
299 - return $filter ? array_intersect( $taxonomies, get_taxonomies() ) : $taxonomies;
509 + return " AND pll_tr.term_taxonomy_id IN (" . implode(',', $languages) . ")";
300 510 }
301 511
302 - /**
303 - * Returns true if Polylang manages languages and translations for this taxonomy
512 + /*
513 + * adds terms clauses to get_terms to filter them by languages - used in both frontend and admin
304 514 *
305 515 * @since 1.2
306 516 *
307 - * @param string|array $tax taxonomy name or array of taxonomy names
308 - * @return bool
517 + * @param array $clauses the list of sql clauses in terms query
518 + * @param object $lang PLL_Language object
519 + * @return array modifed list of clauses
309 520 */
310 - public function is_translated_taxonomy( $tax ) {
311 - $taxonomies = $this->get_translated_taxonomies( false );
312 - return ( is_array( $tax ) && array_intersect( $tax, $taxonomies ) || in_array( $tax, $taxonomies ) );
521 + public function terms_clauses($clauses, $lang) {
522 + if (!empty($lang)) {
523 + $clauses['join'] .= $this->join_clause('term');
524 + $clauses['where'] .= $this->where_clause($lang, 'term');
525 + }
526 + return $clauses;
313 527 }
314 528
315 - /**
316 - * Return taxonomies that need to be filtered ( post_format like )
529 + /*
530 + * register the language taxonomy
317 531 *
318 - * @since 1.7
532 + * @since 1.2
533 + */
534 + public function register_taxonomy() {
535 + // registers the language taxonomy
536 + register_taxonomy('language', $this->get_translated_post_types(), array(
537 + 'labels' => array(
538 + 'name' => __('Languages', 'polylang'),
539 + 'singular_name' => __('Language', 'polylang'),
540 + 'all_items' => __('All languages', 'polylang'),
541 + ),
542 + 'public' => false, // avoid displaying the 'like post tags text box' in the quick edit
543 + 'query_var' => 'lang',
544 + '_pll' => true // polylang taxonomy
545 + ));
546 + }
547 +
548 + /*
549 + * returns post types that need to be translated
319 550 *
320 - * @param bool $filter true if we should return only valid registered taxonomies
321 - * @return array array of registered taxonomy names
551 + * @since 1.2
552 + *
553 + * @param bool $filter true if we should return only valid registered post types
554 + * @return array post type names for which Polylang manages languages and translations
322 555 */
323 - public function get_filtered_taxonomies( $filter = true ) {
324 - if ( did_action( 'after_setup_theme' ) ) {
325 - static $taxonomies = null;
326 - }
556 + public function get_translated_post_types($filter = true) {
557 + static $post_types = array();
327 558
328 - if ( empty( $taxonomies ) ) {
329 - $taxonomies = array( 'post_format' => 'post_format' );
559 + // the post types list is cached for better better performance
560 + // wait for 'after_setup_theme' to apply the cache to allow themes adding the filter in functions.php
561 + if (!$post_types || !did_action('after_setup_theme')) {
562 + $post_types = array('post' => 'post', 'page' => 'page');
330 563
331 - /**
332 - * Filter the list of taxonomies not translatable but filtered by language.
333 - * Includes only the post format by default
334 - * The filter must be added soon in the WordPress loading process:
335 - * in a function hooked to ‘plugins_loaded’ or directly in functions.php for themes.
336 - *
337 - * @since 1.7
338 - *
339 - * @param array $taxonomies list of taxonomy names
340 - * @param bool $is_settings true when displaying the list of custom taxonomies in Polylang settings
341 - */
342 - $taxonomies = apply_filters( 'pll_filtered_taxonomies', $taxonomies, false );
564 + if (!empty($this->options['media_support']))
565 + $post_types['attachement'] = 'attachment';
566 +
567 + if (is_array($this->options['post_types']))
568 + $post_types = array_merge($post_types, $this->options['post_types']);
569 +
570 + $post_types = apply_filters('pll_get_post_types', $post_types , false);
343 571 }
344 572
345 - return $filter ? array_intersect( $taxonomies, get_taxonomies() ) : $taxonomies;
573 + return $filter ? array_intersect($post_types, get_post_types()) : $post_types;
346 574 }
347 575
348 - /**
349 - * Returns true if Polylang filters this taxonomy per language
576 + /*
577 + * check if registered post type must be translated
350 578 *
351 - * @since 1.7
579 + * @since 1.2
352 580 *
353 - * @param string|array $tax taxonomy name or array of taxonomy names
354 - * @return bool
581 + * @param string $post_type post type name
355 582 */
356 - public function is_filtered_taxonomy( $tax ) {
357 - $taxonomies = $this->get_filtered_taxonomies( false );
358 - return ( is_array( $tax ) && array_intersect( $tax, $taxonomies ) || in_array( $tax, $taxonomies ) );
583 + public function registered_post_type($post_type) {
584 + if ($this->is_translated_post_type($post_type)) {
585 + register_taxonomy_for_object_type('language', $post_type);
586 + register_taxonomy_for_object_type('post_translations', $post_type);
587 + }
359 588 }
360 589
361 - /**
362 - * Returns the query vars of all filtered taxonomies
590 + /*
591 + * returns true if Polylang manages languages and translations for this post type
363 592 *
364 - * @since 1.7
593 + * @since 1.2
365 594 *
366 - * @return array
595 + * @param string|array $post_type post type name or array of post type names
367 596 */
368 - public function get_filtered_taxonomies_query_vars() {
369 - $query_vars = array();
370 - foreach ( $this->get_filtered_taxonomies() as $filtered_tax ) {
371 - $tax = get_taxonomy( $filtered_tax );
372 - $query_vars[] = $tax->query_var;
373 - }
374 - return $query_vars;
597 + public function is_translated_post_type($post_type) {
598 + $post_types = $this->get_translated_post_types(false);
599 + return (is_array($post_type) && array_intersect($post_type, $post_types) || in_array($post_type, $post_types));
375 600 }
376 601
377 - /**
378 - * Create a default category for a language
602 + /*
603 + * return taxonomies that need to be translated
379 604 *
380 605 * @since 1.2
381 606 *
382 - * @param object|string|int $lang language
607 + * @param bool $filter true if we should return only valid registered taxonmies
608 + * @return array array of registered taxonomy names for which Polylang manages languages and translations
383 609 */
384 - public function create_default_category( $lang ) {
385 - $lang = $this->get_language( $lang );
610 + public function get_translated_taxonomies($filter = true) {
611 + static $taxonomies = array();
386 612
387 - // create a new category
388 - // FIXME this is translated in admin language when we would like it in $lang
389 - $cat_name = __( 'Uncategorized', 'polylang' );
390 - $cat_slug = sanitize_title( $cat_name . '-' . $lang->slug );
391 - $cat = wp_insert_term( $cat_name, 'category', array( 'slug' => $cat_slug ) );
613 + if (!$taxonomies || !did_action('after_setup_theme')) {
614 + $taxonomies = array('category' => 'category', 'post_tag' => 'post_tag');
392 615
393 - // check that the category was not previously created ( in case the language was deleted and recreated )
394 - $cat = isset( $cat->error_data['term_exists'] ) ? $cat->error_data['term_exists'] : $cat['term_id'];
616 + if (is_array($this->options['taxonomies']))
617 + $taxonomies = array_merge($taxonomies, $this->options['taxonomies']);
395 618
396 - // set language
397 - $this->term->set_language( (int) $cat, $lang );
619 + $taxonomies = apply_filters('pll_get_taxonomies', $taxonomies, false);
620 + }
398 621
399 - // this is a translation of the default category
400 - $default = (int) get_option( 'default_category' );
401 - $translations = $this->term->get_translations( $default );
402 - if ( empty( $translations ) ) {
403 - if ( $lg = $this->term->get_language( $default ) ) {
404 - $translations[ $lg->slug ] = $default;
405 - }
406 - else {
407 - $translations = array();
408 - }
409 - }
622 + return $filter ? array_intersect($taxonomies, get_taxonomies()) : $taxonomies;
623 + }
410 624
411 - $this->term->save_translations( (int) $cat, $translations );
625 + /*
626 + * returns true if Polylang manages languages and translations for this post type
627 + *
628 + * @since 1.2
629 + *
630 + * @param string|array $tax taxonomy name or array of taxonomy names
631 + */
632 + public function is_translated_taxonomy($tax) {
633 + $taxonomies = $this->get_translated_taxonomies(false);
634 + return (is_array($tax) && array_intersect($tax, $taxonomies) || in_array($tax, $taxonomies));
412 635 }
413 636
414 - /**
415 - * It is possible to have several terms with the same name in the same taxonomy ( one per language )
637 + /*
638 + * it is possible to have several terms with the same name in the same taxonomy (one per language)
416 639 * but the native term_exists will return true even if only one exists
417 640 * so here the function adds the language parameter
418 641 *
419 642 * @since 1.4
420 643 *
421 - * @param string $term_name the term name
422 - * @param string $taxonomy taxonomy name
423 - * @param int $parent parent term id
424 - * @param string|object $language the language slug or object
644 + * @param string $term_name the term name
645 + * @param string $taxonomy taxonomy name
646 + * @param int $parent parent term id
647 + * @param string|object $language the language slug or object
425 648 * @return null|int the term_id of the found term
426 649 */
427 - public function term_exists( $term_name, $taxonomy, $parent, $language ) {
650 + public function term_exists($term_name, $taxonomy, $parent, $language) {
428 651 global $wpdb;
429 652
430 - $term_name = trim( wp_unslash( $term_name ) );
431 - $term_name = _wp_specialchars( $term_name );
432 -
433 653 $select = "SELECT t.term_id FROM $wpdb->terms AS t";
434 654 $join = " INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id";
435 - $join .= $this->term->join_clause();
436 - $where = $wpdb->prepare( ' WHERE tt.taxonomy = %s AND t.name = %s', $taxonomy, $term_name );
437 - $where .= $this->term->where_clause( $this->get_language( $language ) );
655 + $join .= $this->join_clause('term');
656 + $where = $wpdb->prepare(" WHERE tt.taxonomy = %s AND t.name = %s", $taxonomy, $term_name);
657 + $where .= $this->where_clause($this->get_language($language), 'term');
438 658
439 - if ( $parent > 0 ) {
440 - $where .= $wpdb->prepare( ' AND tt.parent = %d', $parent );
441 - }
659 + if ($parent > 0)
660 + $where .= $wpdb->prepare(" AND tt.parent = %d", $parent);
442 661
443 - // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared
444 - return $wpdb->get_var( $select . $join . $where );
662 + return $wpdb->get_var($select . $join . $where);
445 663 }
446 664
447 - /**
448 - * Gets the number of posts per language in a date, author or post type archive.
665 + /*
666 + * gets the number of posts per language in a date, author or post type archive
449 667 *
450 668 * @since 1.2
451 669 *
452 - * @param object $lang PLL_Language instance.
453 - * @param array $q WP_Query arguments ( accepted: post_type, m, year, monthnum, day, author, author_name, post_format, post_status ).
670 + * @param object lang
671 + * @param array $args WP_Query arguments (accepted: post_type, m, year, monthnum, day, author, author_name, post_format)
454 672 * @return int
455 673 */
456 - public function count_posts( $lang, $q = array() ) {
674 + public function count_posts($lang, $args = array()) {
457 675 global $wpdb;
458 676
459 - $q = wp_parse_args( $q, array( 'post_type' => 'post', 'post_status' => 'publish' ) );
677 + $q = wp_parse_args($args, array('post_type' => 'post'));
678 + if (!is_array($q['post_type']))
679 + $q['post_type'] = array($q['post_type']);
460 680
461 - if ( ! is_array( $q['post_type'] ) ) {
462 - $q['post_type'] = array( $q['post_type'] );
463 - }
681 + $cache_key = md5(serialize($q));
682 + $counts = wp_cache_get($cache_key, 'pll_count_posts');
464 683
465 - foreach ( $q['post_type'] as $key => $type ) {
466 - if ( ! post_type_exists( $type ) ) {
467 - unset( $q['post_type'][ $key ] );
468 - }
469 - }
684 + if (false === $counts) {
685 + $select = "SELECT pll_tr.term_taxonomy_id, COUNT(*) AS num_posts FROM {$wpdb->posts} AS p";
686 + $join = $this->join_clause('post');
687 + $where = " WHERE post_status = 'publish'";
688 + $where .= " AND p.post_type IN ('" . join("', '", $q['post_type'] ) . "')";
689 + $where .= $this->where_clause($this->get_languages_list(), 'post');
690 + $groupby = " GROUP BY pll_tr.term_taxonomy_id";
470 691
471 - if ( empty( $q['post_type'] ) ) {
472 - $q['post_type'] = array( 'post' ); // We *need* a post type.
473 - }
474 -
475 - $cache_key = 'pll_count_posts_' . md5( maybe_serialize( $q ) );
476 - $counts = wp_cache_get( $cache_key, 'counts' );
477 -
478 - if ( false === $counts ) {
479 - $select = "SELECT pll_tr.term_taxonomy_id, COUNT( * ) AS num_posts FROM {$wpdb->posts}";
480 - $join = $this->post->join_clause();
481 - $where = sprintf( " WHERE post_status = '%s'", esc_sql( $q['post_status'] ) );
482 - $where .= sprintf( " AND {$wpdb->posts}.post_type IN ( '%s' )", join( "', '", esc_sql( $q['post_type'] ) ) );
483 - $where .= $this->post->where_clause( $this->get_languages_list() );
484 - $groupby = ' GROUP BY pll_tr.term_taxonomy_id';
485 -
486 - if ( ! empty( $q['m'] ) ) {
487 - $q['m'] = '' . preg_replace( '|[^0-9]|', '', $q['m'] );
488 - $where .= $wpdb->prepare( " AND YEAR( {$wpdb->posts}.post_date ) = %d", substr( $q['m'], 0, 4 ) );
489 - if ( strlen( $q['m'] ) > 5 ) {
490 - $where .= $wpdb->prepare( " AND MONTH( {$wpdb->posts}.post_date ) = %d", substr( $q['m'], 4, 2 ) );
491 - }
492 - if ( strlen( $q['m'] ) > 7 ) {
493 - $where .= $wpdb->prepare( " AND DAYOFMONTH( {$wpdb->posts}.post_date ) = %d", substr( $q['m'], 6, 2 ) );
494 - }
692 + if (!empty($q['m'])) {
693 + $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']);
694 + $where .= $wpdb->prepare(" AND YEAR(p.post_date) = %d", substr($q['m'], 0, 4));
695 + if ( strlen($q['m']) > 5 )
696 + $where .= $wpdb->prepare(" AND MONTH(p.post_date) = %d", substr($q['m'], 4, 2));
697 + if ( strlen($q['m']) > 7 )
698 + $where .= $wpdb->prepare(" AND DAYOFMONTH(p.post_date) = %d", substr($q['m'], 6, 2));
495 699 }
496 700
497 - if ( ! empty( $q['year'] ) ) {
498 - $where .= $wpdb->prepare( " AND YEAR( {$wpdb->posts}.post_date ) = %d", $q['year'] );
499 - }
701 + if (!empty($q['year']))
702 + $where .= $wpdb->prepare(" AND YEAR(p.post_date) = %d", $q['year']);
500 703
501 - if ( ! empty( $q['monthnum'] ) ) {
502 - $where .= $wpdb->prepare( " AND MONTH( {$wpdb->posts}.post_date ) = %d", $q['monthnum'] );
503 - }
704 + if (!empty($q['monthnum']))
705 + $where .= $wpdb->prepare(" AND MONTH(p.post_date) = %d", $q['monthnum']);
504 706
505 - if ( ! empty( $q['day'] ) ) {
506 - $where .= $wpdb->prepare( " AND DAYOFMONTH( {$wpdb->posts}.post_date ) = %d", $q['day'] );
507 - }
707 + if (!empty($q['day']))
708 + $where .= $wpdb->prepare(" AND DAYOFMONTH(p.post_date) = %d", $q['day']);
508 709
509 - if ( ! empty( $q['author_name'] ) ) {
510 - $author = get_user_by( 'slug', sanitize_title_for_query( $q['author_name'] ) );
511 - if ( $author ) {
710 + if (!empty($q['author_name'])) {
711 + $author = get_user_by('slug', sanitize_title_for_query($q['author_name']));
712 + if ($author)
512 713 $q['author'] = $author->ID;
513 - }
514 714 }
515 715
516 - if ( ! empty( $q['author'] ) ) {
517 - $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_author = %d", $q['author'] );
518 - }
716 + if (!empty($q['author']))
717 + $where .= $wpdb->prepare(" AND p.post_author = %d", $q['author']);
519 718
520 - // Filtered taxonomies ( post_format ).
521 - foreach ( $this->get_filtered_taxonomies_query_vars() as $tax_qv ) {
522 -
523 - if ( ! empty( $q[ $tax_qv ] ) ) {
524 - $join .= " INNER JOIN {$wpdb->term_relationships} AS tr ON tr.object_id = {$wpdb->posts}.ID";
525 - $join .= " INNER JOIN {$wpdb->term_taxonomy} AS tt ON tt.term_taxonomy_id = tr.term_taxonomy_id";
526 - $join .= " INNER JOIN {$wpdb->terms} AS t ON t.term_id = tt.term_id";
527 - $where .= $wpdb->prepare( ' AND t.slug = %s', $q[ $tax_qv ] );
528 - }
719 + if (!empty($q['post_format'])) {
720 + $join .= " INNER JOIN {$wpdb->term_relationships} AS tr ON tr.object_id = p.ID";
721 + $join .= " INNER JOIN {$wpdb->term_taxonomy} AS tt ON tt.term_taxonomy_id = tr.term_taxonomy_id";
722 + $join .= " INNER JOIN {$wpdb->terms} AS t ON t.term_id = tt.term_id";
723 + $where .= $wpdb->prepare(" AND t.slug = %s", $q['post_format']);
529 724 }
530 725
531 - // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared
532 - $res = $wpdb->get_results( $select . $join . $where . $groupby, ARRAY_A );
533 - foreach ( (array) $res as $row ) {
534 - $counts[ $row['term_taxonomy_id'] ] = $row['num_posts'];
535 - }
726 + $res = $wpdb->get_results($select . $join . $where . $groupby, ARRAY_A);
727 + foreach ((array) $res as $row)
728 + $counts[$row['term_taxonomy_id']] = $row['num_posts'];
536 729
537 - wp_cache_set( $cache_key, $counts, 'counts' );
730 + wp_cache_set($cache_key, $counts, 'pll_count_posts');
538 731 }
539 732
540 - return empty( $counts[ $lang->term_taxonomy_id ] ) ? 0 : $counts[ $lang->term_taxonomy_id ];
733 + return empty($counts[$lang->term_taxonomy_id]) ? 0 : $counts[$lang->term_taxonomy_id];
541 734 }
542 735
543 - /**
544 - * Setup the links model based on options
736 + /*
737 + * returns ids of objects in a language similarly to get_objects_in_term for a taxonomy
738 + * faster than get_objects_in_term as it avoids a JOIN
545 739 *
546 - * @since 1.2
740 + * @since 1.4
547 741 *
548 - * @return object implementing "links_model interface"
742 + * @param object $lang a PLL_Language object
743 + * @param string $type optional, either 'post' or 'term', defaults to 'post'
744 + * @return array
549 745 */
550 - public function get_links_model() {
551 - $c = array( 'Directory', 'Directory', 'Subdomain', 'Domain' );
552 - $class = get_option( 'permalink_structure' ) ? 'PLL_Links_' . $c[ $this->options['force_lang'] ] : 'PLL_Links_Default';
553 -
554 - /**
555 - * Filter the links model class to use
556 - * /!\ this filter is fired *before* the $polylang object is available
557 - *
558 - * @since 2.1.1
559 - *
560 - * @param string $class A class name: PLL_Links_Default, PLL_Links_Directory, PLL_Links_Subdomain, PLL_Links_Domain
561 - */
562 - $class = apply_filters( 'pll_links_model', $class );
563 -
564 - return new $class( $this );
565 - }
566 -
567 - /**
568 - * Some backward compatibility with Polylang < 1.8
569 - * allows for example to call $polylang->model->get_post_languages( $post_id ) instead of $polylang->model->post->get_language( $post_id )
570 - * this works but should be slower than the direct call, thus an error is triggered in debug mode
571 - *
572 - * @since 1.8
573 - *
574 - * @param string $func Function name
575 - * @param array $args Function arguments
576 - */
577 - public function __call( $func, $args ) {
578 - $f = $func;
579 -
580 - switch ( $func ) {
581 - case 'get_object_term':
582 - $o = ( false === strpos( $args[1], 'term' ) ) ? 'post' : 'term';
583 - break;
584 -
585 - case 'save_translations':
586 - case 'delete_translation':
587 - case 'get_translations':
588 - case 'get_translation':
589 - case 'join_clause':
590 - $o = ( 'post' == $args[0] || $this->is_translated_post_type( $args[0] ) ) ? 'post' : ( 'term' == $args[0] || $this->is_translated_taxonomy( $args[0] ) ? 'term' : false );
591 - unset( $args[0] );
592 - break;
593 -
594 - case 'set_post_language':
595 - case 'get_post_language':
596 - case 'set_term_language':
597 - case 'get_term_language':
598 - case 'delete_term_language':
599 - case 'get_post':
600 - case 'get_term':
601 - $str = explode( '_', $func );
602 - $f = empty( $str[2] ) ? $str[0] : $str[0] . '_' . $str[2];
603 - $o = $str[1];
604 - break;
605 -
606 - case 'where_clause':
607 - case 'get_objects_in_language':
608 - $o = $args[1];
609 - unset( $args[1] );
610 - break;
611 - }
612 -
613 - if ( ! empty( $o ) && is_object( $this->$o ) && method_exists( $this->$o, $f ) ) {
614 - if ( WP_DEBUG ) {
615 - $debug = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
616 - $i = 1 + empty( $debug[1]['line'] ); // the file and line are in $debug[2] if the function was called using call_user_func
617 -
618 - trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions
619 - sprintf(
620 - '%1$s was called incorrectly in %4$s on line %5$s: the call to $polylang->model->%1$s() has been deprecated in Polylang 1.8, use PLL()->model->%2$s->%3$s() instead.' . "\nError handler",
621 - esc_html( $func ),
622 - esc_html( $o ),
623 - esc_html( $f ),
624 - esc_html( $debug[ $i ]['file'] ),
625 - absint( $debug[ $i ]['line'] )
626 - )
627 - );
628 - }
629 - return call_user_func_array( array( $this->$o, $f ), $args );
630 - }
631 -
632 - $debug = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
633 - trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions
634 - sprintf(
635 - 'Call to undefined function PLL()->model->%1$s() in %2$s on line %3$s' . "\nError handler",
636 - esc_html( $func ),
637 - esc_html( $debug[0]['file'] ),
638 - absint( $debug[0]['line'] )
639 - ),
640 - E_USER_ERROR
641 - );
746 + public function get_objects_in_language($lang, $type = 'post') {
747 + global $wpdb;
748 + return $wpdb->get_col($wpdb->prepare("
749 + SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d",
750 + 'term' == $type ? $lang->tl_term_taxonomy_id : $lang->term_taxonomy_id
751 + ));
642 752 }
643 753 }