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