Don't like ads? PRO users don't see any ads ;-)
Guest

Plugin Polylang displays all posts

By: Chouby on Aug 6th, 2012  |  syntax: PHP  |  size: 4.55 KB  |  hits: 329  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. /*
  3. Plugin Name: Polylang displays all posts
  4. Version: 0.2
  5. Author: F. Demarle
  6. Description: Adds the possibility to display posts in all languages with Polylang. Requires Polylang v0.9.2
  7. */
  8.  
  9. /*  Copyright 2012 F. Demarle
  10.  
  11.     This program is free software; you can redistribute it and/or modify
  12.     it under the terms of the GNU General Public License, published by
  13.     the Free Software Foundation, either version 2 of the License, or
  14.     (at your option) any later version.
  15.  
  16.     This program is distributed in the hope that it will be useful,
  17.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.     GNU General Public License for more details.
  20.  
  21.     You should have received a copy of the GNU General Public License
  22.     along with this program; if not, write to the Free Software
  23.     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  24. */
  25.  
  26. class pll_no_lang {
  27.         protected $curlang;
  28.  
  29.         function __construct() {
  30.                 add_filter('pll_dropdown_language_args', array(&$this, 'pll_dropdown_language_args'));
  31.                 add_filter('pll_get_default_language', create_function('','return 0;'));
  32.                 add_filter('pll_get_objects_with_no_lang', create_function('','return false;'));
  33.  
  34.                 // filters posts according to the language
  35.                 add_filter('pre_get_posts', array(&$this, 'pre_get_posts'), 20); // after polylang
  36.  
  37.                 add_action('wp', array(&$this, 'wp'), 1); // before polylang
  38.  
  39.                 // rewrites archives, next and previous post links to filter them by language
  40.                 foreach (array('getarchives', 'get_previous_post', 'get_next_post') as $filter)
  41.                         add_filter($filter.'_where', array(&$this, 'posts_where'), 1); // before polylang
  42.  
  43.                 add_filter('terms_clauses', array(&$this, 'terms_clauses'), 20); // after polylang
  44.         }
  45.  
  46.         // adds the item 'No language' for posts, media and terms
  47.         function pll_dropdown_language_args($args) {
  48.                 if ((isset($args['name']) && $args['name'] == 'post_lang_choice') ||
  49.                         (isset($args['class']) && $args['class'] == 'media_lang_choice') ||
  50.                         (isset($args['name']) && $args['name'] == 'term_lang_choice'))
  51.                         $args['add_option'] = __('No language', 'polylang');
  52.                 return $args;
  53.         }
  54.  
  55.         // return the array of all languages ids which are not $languages
  56.         function invert_languages_ids($languages) {
  57.                 global $polylang;
  58.                 // get an array of ids for languages
  59.                 $languages = is_array($languages) ? $languages : array($languages);
  60.                 foreach ($languages as $key=>$lang)
  61.                         $languages[$key] = $polylang->get_language($lang)->term_id;
  62.  
  63.                 // invert the array
  64.                 return array_diff($polylang->get_languages_list(array('fields' => 'ids')), $languages);
  65.         }
  66.  
  67.         function pre_get_posts($query) {
  68.                 $qvars = $query->query_vars;
  69.                 if (is_admin() || !isset($qvars['lang']) /*|| (isset($qvars['pagename']) && $qvars['pagename'])*/)
  70.                         return;
  71.  
  72.                 $query->set('tax_query', array(array(
  73.                         'taxonomy' => 'language',
  74.                         'fields'   => 'ids',
  75.                         'terms'    =>  $this->invert_languages_ids($qvars['lang']),
  76.                         'operator' => 'NOT IN'
  77.                 )));
  78.  
  79.                 $this->curlang = $qvars['lang']; // remember the queried language
  80.                 unset ($query->query_vars['lang']); // to allow querying posts with no language
  81.                 $query->is_tax = false; // to avoid lot of errors
  82.         }
  83.  
  84.         // so that Polylang knows the language to load
  85.         function wp() {
  86.                 if (isset($this->curlang))
  87.                         set_query_var('lang', $this->curlang);
  88.         }
  89.  
  90.         function posts_where($sql) {
  91.                 global $polylang, $wpdb;
  92.                 foreach (array('getarchives', 'get_previous_post', 'get_next_post') as $filter)
  93.                         foreach (array('_join', '_where') as $clause)
  94.                                 remove_filter($filter.$clause, array(&$polylang, 'posts'.$clause));
  95.  
  96.                 $p = current_filter() == 'getarchives_where' ? $wpdb->posts : 'p';
  97.                 $ids = implode(',', $this->invert_languages_ids($polylang->get_current_language()));
  98.  
  99.                 return $sql . $wpdb->prepare(" AND ($p.ID NOT IN (
  100.                         SELECT object_id
  101.                         FROM $wpdb->term_relationships
  102.                         WHERE term_taxonomy_id IN ($ids)                       
  103.                 ))");
  104.         }
  105.  
  106.         function terms_clauses($clauses) {
  107.                 preg_match('#pll_tm.meta_value IN \(([0-9]+)\)#', $clauses['where'], $matches);
  108.                 if (!empty($matches)) {
  109.                         global $wpdb;
  110.                         $where_lang = $wpdb->prepare("pll_tm.meta_key = '_language' AND pll_tm.meta_value IN (%d)", $matches[1]); // terms in the right language
  111.                         $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
  112.                         $clauses['where'] = str_replace($where_lang, "(($where_lang) OR ($where_none))", $clauses['where']);
  113.                 }
  114.                 return $clauses;
  115.         }
  116. }
  117.  
  118. new pll_no_lang();
  119.  
  120. ?>