PluginProbe
Polylang / 1.9.2
Polylang v1.9.2
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.9.2, at include/filters.php

148 lines 4.3 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 $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 // converts the locale to a valid W3C locale
31 add_filter( 'language_attributes', array( &$this, 'language_attributes' ) );
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 // don't filter comments if comment ids or post ids are specified
48 $plucked = wp_array_slice_assoc( $query->query_vars, array( 'comment__in', 'parent', 'post_id', 'post__in', 'post_parent' ) );
49 $fields = array_filter( $plucked );
50 if ( ! empty( $fields ) ) {
51 return $clauses;
52 }
53
54 // don't filter comments if a non translated post type is specified
55 if ( ! empty( $query->query_vars['post_type'] ) && ! $this->model->is_translated_post_type( $query->query_vars['post_type'] ) ) {
56 return $clauses;
57 }
58
59 $lang = empty( $query->query_vars['lang'] ) ? $this->curlang : $this->model->get_language( $query->query_vars['lang'] );
60
61 if ( ! empty( $lang ) ) {
62 // if this clause is not already added by WP
63 if ( ! strpos( $clauses['join'], '.ID' ) ) {
64 $clauses['join'] .= " JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
65 }
66
67 $clauses['join'] .= $this->model->post->join_clause();
68 $clauses['where'] .= $this->model->post->where_clause( $lang );
69 }
70 return $clauses;
71 }
72
73 /**
74 * filters get_pages per language
75 *
76 * @since 1.4
77 *
78 * @param array $pages an array of pages already queried
79 * @param array $args get_pages arguments
80 * @return array modified list of pages
81 */
82 public function get_pages( $pages, $args ) {
83 $language = empty( $args['lang'] ) ? $this->curlang : $this->model->get_language( $args['lang'] );
84
85 if ( empty( $language ) || empty( $pages ) || ! $this->model->is_translated_post_type( $args['post_type'] ) ) {
86 return $pages;
87 }
88
89 static $once = false;
90
91 // obliged to redo the get_pages query if we want to get the right number
92 if ( ! empty( $args['number'] ) && ! $once ) {
93 $once = true; // avoid infinite loop
94
95 $r = array(
96 'lang' => 0, // so this query is not filtered
97 'numberposts' => -1,
98 'nopaging' => true,
99 'post_type' => $args['post_type'],
100 'fields' => 'ids',
101 'tax_query' => array( array(
102 'taxonomy' => 'language',
103 'field' => 'term_taxonomy_id', // since WP 3.5
104 'terms' => $language->term_taxonomy_id,
105 'operator' => 'NOT IN',
106 ) ),
107 );
108
109 $args['exclude'] = array_merge( $args['exclude'], get_posts( $r ) );
110 $pages = get_pages( $args );
111 }
112
113 $ids = wp_list_pluck( $pages, 'ID' );
114
115 // filters the queried list of pages by language
116 if ( ! $once ) {
117 $ids = array_intersect( $ids, $this->model->post->get_objects_in_language( $language ) );
118
119 foreach ( $pages as $key => $page ) {
120 if ( ! in_array( $page->ID, $ids ) ) {
121 unset( $pages[ $key ] );
122 }
123 }
124 }
125
126 // not done by WP but extremely useful for performance when manipulating taxonomies
127 update_object_term_cache( $ids, $args['post_type'] );
128
129 $once = false; // in case get_pages is called another time
130 return $pages;
131 }
132
133 /**
134 * converts WordPress locale to valid W3 locale in html language attributes
135 *
136 * @since 1.8
137 *
138 * @param string $output language attributes
139 * @return string
140 */
141 public function language_attributes( $output ) {
142 if ( $language = $this->model->get_language( get_locale() ) ) {
143 $output = str_replace( '"' . get_bloginfo( 'language' ) . '"', '"' . $language->get_locale( 'display' ) . '"', $output );
144 }
145 return $output;
146 }
147 }
148