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