Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Polylang displays all posts
- Version: 0.2
- Author: F. Demarle
- Description: Adds the possibility to display posts in all languages with Polylang. Requires Polylang v0.9.2
- */
- /*
- This plugin is no more maintained and is not compatible with recent versions of Polylang.
- I keep it here in case someone wants to use it at a base for his own needs
- */
- /* Copyright 2012 F. Demarle
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License, published by
- the Free Software Foundation, either version 2 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- */
- class pll_no_lang {
- protected $curlang;
- function __construct() {
- add_filter('pll_dropdown_language_args', array(&$this, 'pll_dropdown_language_args'));
- add_filter('pll_get_default_language', create_function('','return 0;'));
- add_filter('pll_get_objects_with_no_lang', create_function('','return false;'));
- // filters posts according to the language
- add_filter('pre_get_posts', array(&$this, 'pre_get_posts'), 20); // after polylang
- add_action('wp', array(&$this, 'wp'), 1); // before polylang
- // rewrites archives, next and previous post links to filter them by language
- foreach (array('getarchives', 'get_previous_post', 'get_next_post') as $filter)
- add_filter($filter.'_where', array(&$this, 'posts_where'), 1); // before polylang
- add_filter('terms_clauses', array(&$this, 'terms_clauses'), 20); // after polylang
- }
- // adds the item 'No language' for posts, media and terms
- function pll_dropdown_language_args($args) {
- if ((isset($args['name']) && $args['name'] == 'post_lang_choice') ||
- (isset($args['class']) && $args['class'] == 'media_lang_choice') ||
- (isset($args['name']) && $args['name'] == 'term_lang_choice'))
- $args['add_option'] = __('No language', 'polylang');
- return $args;
- }
- // return the array of all languages ids which are not $languages
- function invert_languages_ids($languages) {
- global $polylang;
- // get an array of ids for languages
- $languages = is_array($languages) ? $languages : array($languages);
- foreach ($languages as $key=>$lang)
- $languages[$key] = $polylang->get_language($lang)->term_id;
- // invert the array
- return array_diff($polylang->get_languages_list(array('fields' => 'ids')), $languages);
- }
- function pre_get_posts($query) {
- $qvars = $query->query_vars;
- if (is_admin() || !isset($qvars['lang']) /*|| (isset($qvars['pagename']) && $qvars['pagename'])*/)
- return;
- $query->set('tax_query', array(array(
- 'taxonomy' => 'language',
- 'fields' => 'ids',
- 'terms' => $this->invert_languages_ids($qvars['lang']),
- 'operator' => 'NOT IN'
- )));
- $this->curlang = $qvars['lang']; // remember the queried language
- unset ($query->query_vars['lang']); // to allow querying posts with no language
- $query->is_tax = false; // to avoid lot of errors
- }
- // so that Polylang knows the language to load
- function wp() {
- if (isset($this->curlang))
- set_query_var('lang', $this->curlang);
- }
- function posts_where($sql) {
- global $polylang, $wpdb;
- foreach (array('getarchives', 'get_previous_post', 'get_next_post') as $filter)
- foreach (array('_join', '_where') as $clause)
- remove_filter($filter.$clause, array(&$polylang, 'posts'.$clause));
- $p = current_filter() == 'getarchives_where' ? $wpdb->posts : 'p';
- $ids = implode(',', $this->invert_languages_ids($polylang->get_current_language()));
- return $sql . $wpdb->prepare(" AND ($p.ID NOT IN (
- SELECT object_id
- FROM $wpdb->term_relationships
- WHERE term_taxonomy_id IN ($ids)
- ))");
- }
- function terms_clauses($clauses) {
- preg_match('#pll_tm.meta_value IN \(([0-9]+)\)#', $clauses['where'], $matches);
- if (!empty($matches)) {
- global $wpdb;
- $where_lang = $wpdb->prepare("pll_tm.meta_key = '_language' AND pll_tm.meta_value IN (%d)", $matches[1]); // terms in the right language
- $where_none = $wpdb->prepare("t.term_id NOT IN (SELECT term_id FROM $wpdb->termmeta WHERE meta_key IN ('_language'))"); // terms with no language set
- $clauses['where'] = str_replace($where_lang, "(($where_lang) OR ($where_none))", $clauses['where']);
- }
- return $clauses;
- }
- }
- new pll_no_lang();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement