| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* setup filters common to admin and frontend |
| 5 |
* |
| 6 |
* @since 1.4 |
| 7 |
*/ |
| 8 |
class PLL_Filters { |
| 9 |
public $links_model, $model, $options, $curlang; |
| 10 |
|
| 11 |
/* |
| 12 |
* constructor: setups filters |
| 13 |
* |
| 14 |
* @since 1.4 |
| 15 |
* |
| 16 |
* @param object $polylang |
| 17 |
*/ |
| 18 |
public function __construct(&$polylang) { |
| 19 |
$this->links_model = &$polylang->links_model; |
| 20 |
$this->model = &$polylang->model; |
| 21 |
$this->options = &$polylang->options; |
| 22 |
$this->curlang = &$polylang->curlang; |
| 23 |
|
| 24 |
// filters the comments according to the current language |
| 25 |
add_filter('comments_clauses', array(&$this, 'comments_clauses'), 10, 2); |
| 26 |
|
| 27 |
// filters the get_pages function according to the current language |
| 28 |
add_filter('get_pages', array(&$this, 'get_pages'), 10, 2); |
| 29 |
|
| 30 |
// adds cache domain when querying terms |
| 31 |
add_filter('get_terms_args', array(&$this, 'get_terms_args')); |
| 32 |
} |
| 33 |
|
| 34 |
/* |
| 35 |
* filters the comments according to the current language |
| 36 |
* used by the recent comments widget and admin language filter |
| 37 |
* |
| 38 |
* @since 0.2 |
| 39 |
* |
| 40 |
* @param array $clauses sql clauses |
| 41 |
* @param object $query WP_Comment_Query object |
| 42 |
* @return array modified $clauses |
| 43 |
*/ |
| 44 |
public function comments_clauses($clauses, $query) { |
| 45 |
global $wpdb; |
| 46 |
|
| 47 |
$lang = empty($query->query_vars['lang']) ? $this->curlang : $this->model->get_language($query->query_vars['lang']); |
| 48 |
|
| 49 |
if (!empty($lang)) { |
| 50 |
// if this clause is not already added by WP |
| 51 |
if (!strpos($clauses['join'], '.ID')) |
| 52 |
$clauses['join'] .= " JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID"; |
| 53 |
|
| 54 |
$clauses['join'] .= $this->model->join_clause('post'); |
| 55 |
$clauses['where'] .= $this->model->where_clause($lang, 'post'); |
| 56 |
} |
| 57 |
return $clauses; |
| 58 |
} |
| 59 |
|
| 60 |
/* |
| 61 |
* filters get_pages per language |
| 62 |
* |
| 63 |
* @since 1.4 |
| 64 |
* |
| 65 |
* @param array $pages an array of pages already queried |
| 66 |
* @param array $args get_pages arguments |
| 67 |
* @return array modified list of pages |
| 68 |
*/ |
| 69 |
public function get_pages($pages, $args) { |
| 70 |
$language = empty($args['lang']) ? $this->curlang : $this->model->get_language($args['lang']); |
| 71 |
|
| 72 |
if (empty($language) || empty($pages) || !$this->model->is_translated_post_type($args['post_type'])) |
| 73 |
return $pages; |
| 74 |
|
| 75 |
static $once = false; |
| 76 |
|
| 77 |
// obliged to redo the get_pages query if we want to get the right number |
| 78 |
if (!empty($args['number']) && !$once) { |
| 79 |
$once = true; // avoid infinite loop |
| 80 |
|
| 81 |
$r = array( |
| 82 |
'lang' => 0, // so this query is not filtered |
| 83 |
'numberposts' => -1, |
| 84 |
'nopaging' => true, |
| 85 |
'post_type' => $args['post_type'], |
| 86 |
'fields' => 'ids', |
| 87 |
'tax_query' => array(array( |
| 88 |
'taxonomy' => 'language', |
| 89 |
'field' => 'term_taxonomy_id', // since WP 3.5 |
| 90 |
'terms' => $language->term_taxonomy_id, |
| 91 |
'operator' => 'NOT IN' |
| 92 |
)) |
| 93 |
); |
| 94 |
|
| 95 |
$args['exclude'] = array_merge($args['exclude'], get_posts($r)); |
| 96 |
$pages = get_pages($args); |
| 97 |
} |
| 98 |
|
| 99 |
$ids = wp_list_pluck($pages, 'ID'); |
| 100 |
|
| 101 |
// filters the queried list of pages by language |
| 102 |
if (!$once) { |
| 103 |
$ids = array_intersect($ids, $this->model->get_objects_in_language($language)); |
| 104 |
|
| 105 |
foreach ($pages as $key => $page) { |
| 106 |
if (!in_array($page->ID, $ids)) |
| 107 |
unset($pages[$key]); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
// not done by WP but extremely useful for performance when manipulating taxonomies |
| 112 |
update_object_term_cache($ids, $args['post_type']); |
| 113 |
|
| 114 |
$once = false; // in case get_pages is called another time |
| 115 |
return $pages; |
| 116 |
} |
| 117 |
|
| 118 |
/* |
| 119 |
* adds language dependent cache domain when querying terms |
| 120 |
* useful as the 'lang' parameter is not included in cache key by WordPress |
| 121 |
* |
| 122 |
* @since 1.3 |
| 123 |
*/ |
| 124 |
public function get_terms_args($args) { |
| 125 |
if (!empty($args['lang'])) |
| 126 |
$lang = $args['lang']; |
| 127 |
elseif (!empty($this->curlang)) |
| 128 |
$lang = $this->curlang->slug; |
| 129 |
|
| 130 |
if (isset($lang)) { |
| 131 |
$key = '_' . (is_array($lang) ? implode(',', $lang) : $lang); |
| 132 |
$args['cache_domain'] = empty($args['cache_domain']) ? 'pll' . $key : $args['cache_domain'] . $key; |
| 133 |
} |
| 134 |
|
| 135 |
return $args; |
| 136 |
} |
| 137 |
} |
| 138 |
|