PluginProbe
Polylang / 1.4
Polylang v1.4
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
polylang / include / filters.php

filters.php in Polylang 1.4, at include/filters.php

116 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $links_model
17 * @param object $curlang
18 */
19 public function __construct(&$links_model, &$curlang) {
20 $this->links_model = &$links_model;
21 $this->model = &$links_model->model;
22 $this->options = &$this->model->options;
23 $this->curlang = $curlang;
24
25 // filters the comments according to the current language
26 add_filter('comments_clauses', array(&$this, 'comments_clauses'), 10, 2);
27
28 // filters the get_pages function according to the current language
29 add_filter('get_pages', array(&$this, 'get_pages'), 10, 2);
30 }
31
32 /*
33 * filters the comments according to the current language
34 * used by the recent comments widget and admin language filter
35 *
36 * @since 0.2
37 *
38 * @param array $clauses sql clauses
39 * @param object $query WP_Comment_Query object
40 * @return array modified $clauses
41 */
42 public function comments_clauses($clauses, $query) {
43 global $wpdb;
44
45 $lang = empty($query->query_vars['lang']) ? $this->curlang : $this->model->get_language($query->query_vars['lang']);
46
47 if (!empty($lang)) {
48 // if this clause is not already added by WP
49 if (!strpos($clauses['join'], '.ID'))
50 $clauses['join'] .= " JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
51
52 $clauses['join'] .= $this->model->join_clause('post');
53 $clauses['where'] .= $this->model->where_clause($lang, 'post');
54 }
55 return $clauses;
56 }
57
58 /*
59 * filters get_pages per language
60 *
61 * @since 1.4
62 *
63 * @param array $pages an array of pages already queried
64 * @param array $args get_pages arguments
65 * @return array modified list of pages
66 */
67 public function get_pages($pages, $args) {
68 $language = empty($args['lang']) ? $this->curlang : $this->model->get_language($args['lang']);
69
70 if (empty($language) || empty($pages) || !$this->model->is_translated_post_type($args['post_type']))
71 return $pages;
72
73 static $once = false;
74
75 // obliged to redo the get_pages query if we want to get the right number
76 if (!empty($args['number']) && !$once) {
77 $once = true; // avoid infinite loop
78
79 $r = array(
80 'lang' => 0, // so this query is not filtered
81 'numberposts' => -1,
82 'nopaging' => true,
83 'post_type' => $args['post_type'],
84 'fields' => 'ids',
85 'tax_query' => array(array(
86 'taxonomy' => 'language',
87 'field' => 'term_taxonomy_id', // since WP 3.5
88 'terms' => $language->term_taxonomy_id,
89 'operator' => 'NOT IN'
90 ))
91 );
92
93 $args['exclude'] = array_merge($args['exclude'], get_posts($r));
94 $pages = get_pages($args);
95 }
96
97 $ids = wp_list_pluck($pages, 'ID');
98
99 // filters the queried list of pages by language
100 if (!$once) {
101 $ids = array_intersect($ids, $this->model->get_objects_in_language($language));
102
103 foreach ($pages as $key => $page) {
104 if (!in_array($page->ID, $ids))
105 unset($pages[$key]);
106 }
107 }
108
109 // not done by WP but extremely useful for performance when manipulating taxonomies
110 update_object_term_cache($ids, $args['post_type']);
111
112 $once = false; // in case get_pages is called another time
113 return $pages;
114 }
115 }
116