PluginProbe
Polylang / 2.1
Polylang v2.1
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 2.1, at include/filters.php

206 lines 6.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_action( 'parse_comment_query', array( $this, 'parse_comment_query' ) );
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 // Converts the locale to a valid W3C locale
32 add_filter( 'language_attributes', array( $this, 'language_attributes' ) );
33
34 // Prevents deleting all the translations of the default category
35 add_filter( 'map_meta_cap', array( $this, 'fix_delete_default_category' ), 10, 4 );
36 }
37
38 /**
39 * Get the language to filter a comments query
40 *
41 * @since 2.0
42 *
43 * @param object $query
44 * @return object|bool the language(s) to use in the filter, false otherwise
45 */
46 protected function get_comments_queried_language( $query ) {
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 false;
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 false;
57 }
58
59 return empty( $query->query_vars['lang'] ) ? $this->curlang : $this->model->get_language( $query->query_vars['lang'] );
60 }
61
62 /**
63 * Adds language dependent cache domain when querying comments
64 * Useful as the 'lang' parameter is not included in cache key by WordPress
65 * Needed since WP 4.6 as comments have been added to persistent cache. See #36906, #37419
66 *
67 * @since 2.0
68 *
69 * @param object $query
70 */
71 public function parse_comment_query( $query ) {
72 if ( $lang = $this->get_comments_queried_language( $query ) ) {
73 $key = '_' . ( is_array( $lang ) ? implode( ',', $lang ) : $this->model->get_language( $lang )->slug );
74 $query->query_vars['cache_domain'] = empty( $query->query_vars['cache_domain'] ) ? 'pll' . $key : $query->query_vars['cache_domain'] . $key;
75 }
76 }
77
78 /**
79 * Filters the comments according to the current language
80 * Used by the recent comments widget and admin language filter
81 *
82 * @since 0.2
83 *
84 * @param array $clauses sql clauses
85 * @param object $query WP_Comment_Query object
86 * @return array modified $clauses
87 */
88 public function comments_clauses( $clauses, $query ) {
89 global $wpdb;
90
91 $lang = $this->get_comments_queried_language( $query );
92
93 if ( ! empty( $lang ) ) {
94 // If this clause is not already added by WP
95 if ( ! strpos( $clauses['join'], '.ID' ) ) {
96 $clauses['join'] .= " JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
97 }
98
99 $clauses['join'] .= $this->model->post->join_clause();
100 $clauses['where'] .= $this->model->post->where_clause( $lang );
101 }
102 return $clauses;
103 }
104
105 /**
106 * Filters get_pages per language
107 *
108 * @since 1.4
109 *
110 * @param array $pages an array of pages already queried
111 * @param array $args get_pages arguments
112 * @return array modified list of pages
113 */
114 public function get_pages( $pages, $args ) {
115 if ( isset( $args['lang'] ) && empty( $args['lang'] ) ) {
116 return $pages;
117 }
118
119 $language = empty( $args['lang'] ) ? $this->curlang : $this->model->get_language( $args['lang'] );
120
121 if ( empty( $language ) || empty( $pages ) || ! $this->model->is_translated_post_type( $args['post_type'] ) ) {
122 return $pages;
123 }
124
125 static $once = false;
126
127 // Obliged to redo the get_pages query if we want to get the right number
128 if ( ! empty( $args['number'] ) && ! $once ) {
129 $once = true; // avoid infinite loop
130
131 $r = array(
132 'lang' => 0, // So this query is not filtered
133 'numberposts' => -1,
134 'nopaging' => true,
135 'post_type' => $args['post_type'],
136 'fields' => 'ids',
137 'tax_query' => array( array(
138 'taxonomy' => 'language',
139 'field' => 'term_taxonomy_id', // Since WP 3.5
140 'terms' => $language->term_taxonomy_id,
141 'operator' => 'NOT IN',
142 ) ),
143 );
144
145 $args['exclude'] = array_merge( $args['exclude'], get_posts( $r ) );
146 $pages = get_pages( $args );
147 }
148
149 $ids = wp_list_pluck( $pages, 'ID' );
150
151 // Filters the queried list of pages by language
152 if ( ! $once ) {
153 $ids = array_intersect( $ids, $this->model->post->get_objects_in_language( $language ) );
154
155 foreach ( $pages as $key => $page ) {
156 if ( ! in_array( $page->ID, $ids ) ) {
157 unset( $pages[ $key ] );
158 }
159 }
160
161 $pages = array_values( $pages ); // In case 3rd parties suppose the existence of $pages[0]
162 }
163
164 // Not done by WP but extremely useful for performance when manipulating taxonomies
165 update_object_term_cache( $ids, $args['post_type'] );
166
167 $once = false; // In case get_pages is called another time
168 return $pages;
169 }
170
171 /**
172 * Converts WordPress locale to valid W3 locale in html language attributes
173 *
174 * @since 1.8
175 *
176 * @param string $output language attributes
177 * @return string
178 */
179 public function language_attributes( $output ) {
180 if ( $language = $this->model->get_language( get_locale() ) ) {
181 $output = str_replace( '"' . get_bloginfo( 'language' ) . '"', '"' . $language->get_locale( 'display' ) . '"', $output );
182 }
183 return $output;
184 }
185
186
187 /**
188 * Prevents deleting all the translations of the default category
189 *
190 * @since 2.1
191 *
192 * @param array $caps The user's actual capabilities.
193 * @param string $cap Capability name.
194 * @param int $user_id The user ID.
195 * @param array $args Adds the context to the cap. The category id.
196 * @return array
197 */
198 public function fix_delete_default_category( $caps, $cap, $user_id, $args ) {
199 if ( 'delete_term' === $cap && array_intersect( $args, $this->model->term->get_translations( get_option( 'default_category' ) ) ) ) {
200 $caps[] = 'do_not_allow';
201 }
202
203 return $caps;
204 }
205 }
206