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