PluginProbe
Bogo / trunk
Bogo vtrunk
3.9.3 trunk 1.0 1.1 2.0 2.0.1 2.1 2.1.1 2.1.2 2.1.3 2.2 2.3 2.4 2.4.1 2.4.2 2.4.3 2.5 2.6 2.6.1 2.7 2.8 2.8.1 2.9 3.0 3.0.1 All 52 releases
bogo / includes / query.php

query.php in Bogo trunk, at includes/query.php

286 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 add_action( 'parse_query', 'bogo_parse_query', 10, 1 );
4
5 function bogo_parse_query( $query ) {
6 $qv = &$query->query_vars;
7
8 if ( ! empty( $qv['bogo_suppress_locale_query'] ) ) {
9 return;
10 }
11
12 if ( $query->is_preview() and ( $qv['page_id'] or $qv['p'] ) ) {
13 $qv['bogo_suppress_locale_query'] = true;
14 return;
15 }
16
17 if ( isset( $qv['post_type'] ) and 'any' !== $qv['post_type'] ) {
18 $localizable = array_filter(
19 (array) $qv['post_type'],
20 'bogo_is_localizable_post_type'
21 );
22
23 if ( empty( $localizable ) ) {
24 $qv['bogo_suppress_locale_query'] = true;
25 return;
26 }
27 }
28
29 $lang = $qv['lang'] ?? '';
30
31 if ( is_admin() ) {
32 $locale = $lang;
33 } else {
34 if ( $lang ) {
35 $locale = bogo_get_closest_locale( $lang );
36 } else {
37 $locale = get_locale();
38 }
39
40 if ( empty( $locale ) ) {
41 $locale = bogo_get_default_locale();
42 }
43 }
44
45 if ( empty( $locale ) or ! bogo_is_available_locale( $locale ) ) {
46 $qv['bogo_suppress_locale_query'] = true;
47 return;
48 }
49
50 $qv['lang'] = $locale;
51
52 if ( is_admin() ) {
53 return;
54 }
55
56 if (
57 $query->is_home and
58 'page' === get_option( 'show_on_front' ) and
59 get_option( 'page_on_front' )
60 ) {
61 $query_keys = array_keys( wp_parse_args( $query->query ) );
62 $query_keys = array_diff(
63 $query_keys,
64 array( 'preview', 'page', 'paged', 'cpage', 'lang' )
65 );
66
67 if ( empty( $query_keys ) ) {
68 $query->is_page = true;
69 $query->is_home = false;
70 $qv['page_id'] = get_option( 'page_on_front' );
71
72 if ( ! empty( $qv['paged'] ) ) {
73 $qv['page'] = $qv['paged'];
74 unset( $qv['paged'] );
75 }
76 }
77 }
78
79 if ( '' != $qv['pagename'] ) {
80 $query->queried_object = bogo_get_page_by_path( $qv['pagename'], $locale );
81
82 if ( ! empty( $query->queried_object ) ) {
83 $query->queried_object_id = (int) $query->queried_object->ID;
84 } else {
85 unset( $query->queried_object );
86 unset( $query->queried_object_id );
87 }
88
89 if (
90 'page' === get_option( 'show_on_front' ) and
91 isset( $query->queried_object_id ) and
92 $query->queried_object_id == get_option( 'page_for_posts' )
93 ) {
94 $query->is_page = false;
95 $query->is_home = true;
96 $query->is_posts_page = true;
97 }
98 }
99
100 if (
101 isset( $qv['post_type'] ) and
102 'any' !== $qv['post_type'] and
103 ! is_array( $qv['post_type'] ) and
104 ! empty( $qv['name'] )
105 ) {
106 $query->queried_object = bogo_get_page_by_path(
107 $qv['name'], $locale, $qv['post_type']
108 );
109
110 if ( ! empty( $query->queried_object ) ) {
111 $query->queried_object_id = (int) $query->queried_object->ID;
112 } else {
113 unset( $query->queried_object );
114 unset( $query->queried_object_id );
115 }
116 }
117
118 if (
119 $query->is_posts_page and
120 ( ! isset( $qv['withcomments'] ) or ! $qv['withcomments'] )
121 ) {
122 $query->is_comment_feed = false;
123 }
124
125 $query->is_singular =
126 ( $query->is_single || $query->is_page || $query->is_attachment );
127
128 $query->is_embed =
129 $query->is_embed && ( $query->is_singular || $query->is_404 );
130 }
131
132
133 add_filter( 'posts_join', 'bogo_posts_join', 10, 2 );
134
135 function bogo_posts_join( $join, $query ) {
136 global $wpdb;
137
138 $qv = &$query->query_vars;
139
140 if ( ! empty( $qv['bogo_suppress_locale_query'] ) ) {
141 return $join;
142 }
143
144 $locale = $qv['lang'] ?? '';
145
146 if ( ! bogo_is_available_locale( $locale ) ) {
147 return $join;
148 }
149
150 if ( ! $meta_table = _get_meta_table( 'post' ) ) {
151 return $join;
152 }
153
154 $join .= " LEFT JOIN $meta_table AS postmeta_bogo ON ($wpdb->posts.ID = postmeta_bogo.post_id AND postmeta_bogo.meta_key = '_locale')";
155
156 return $join;
157 }
158
159
160 add_filter( 'posts_where', 'bogo_posts_where', 10, 2 );
161
162 function bogo_posts_where( $where, $query ) {
163 global $wpdb;
164
165 $qv = &$query->query_vars;
166
167 if ( ! empty( $qv['bogo_suppress_locale_query'] ) ) {
168 return $where;
169 }
170
171 $locale = $qv['lang'] ?? '';
172
173 if ( ! bogo_is_available_locale( $locale ) ) {
174 return $where;
175 }
176
177 if ( ! $meta_table = _get_meta_table( 'post' ) ) {
178 return $where;
179 }
180
181 $where .= " AND (1=0";
182
183 $where .= $wpdb->prepare( " OR postmeta_bogo.meta_value LIKE %s", $locale );
184
185 if ( bogo_is_default_locale( $locale ) ) {
186 $where .= " OR postmeta_bogo.meta_id IS NULL";
187 }
188
189 $where .= ")";
190
191 return $where;
192 }
193
194
195 add_filter( 'option_sticky_posts', 'bogo_option_sticky_posts', 10, 1 );
196
197 function bogo_option_sticky_posts( $posts ) {
198 if ( is_home() ) {
199 $locale = get_locale();
200
201 $posts = array_filter( $posts,
202 static function ( $post_id ) use ( $locale ) {
203 return bogo_get_post_locale( $post_id ) === $locale;
204 }
205 );
206 }
207
208 return $posts;
209 }
210
211
212 add_filter( 'option_page_on_front', 'bogo_get_local_post', 10, 1 );
213 add_filter( 'option_page_for_posts', 'bogo_get_local_post', 10, 1 );
214
215 function bogo_get_local_post( $post_id ) {
216 global $wpdb;
217
218 if ( is_admin() or empty( $post_id ) ) {
219 return $post_id;
220 }
221
222 $post_type = get_post_type( $post_id );
223
224 if (
225 ! post_type_exists( $post_type ) or
226 ! bogo_is_localizable_post_type( $post_type )
227 ) {
228 return $post_id;
229 }
230
231 $locale = get_locale();
232
233 if ( bogo_get_post_locale( $post_id ) === $locale ) {
234 return $post_id;
235 }
236
237 $locale_query = $wpdb->prepare(
238 "pm2.meta_value LIKE %s",
239 $locale
240 );
241
242 if ( bogo_is_default_locale( $locale ) ) {
243 $locale_query = $wpdb->prepare(
244 "(pm2.meta_value LIKE %s OR pm2.meta_id IS NULL)",
245 $locale
246 );
247 }
248
249 $original_post = get_post_meta( $post_id, '_original_post', true );
250
251 // For back-compat
252 if ( empty( $original_post ) ) {
253 $original_post = $post_id;
254 }
255
256 $original_post_query = $wpdb->prepare(
257 "pm1.meta_value = %s",
258 $original_post
259 );
260
261 // For back-compat
262 if ( is_int( $original_post ) ) {
263 $original_post_query = $wpdb->prepare(
264 "(ID = %d OR pm1.meta_value = %d)",
265 $original_post,
266 $original_post
267 );
268 }
269
270 $translation = $wpdb->get_var( $wpdb->prepare(
271 "SELECT ID FROM %i AS posts LEFT JOIN %i AS pm1 ON posts.ID = pm1.post_id AND pm1.meta_key = '_original_post' LEFT JOIN %i AS pm2 ON posts.ID = pm2.post_id AND pm2.meta_key = '_locale' WHERE post_status = 'publish' AND post_type = %s AND $locale_query AND $original_post_query",
272 $wpdb->posts,
273 $wpdb->postmeta,
274 $wpdb->postmeta,
275 $post_type,
276 ) );
277
278 $translation = absint( $translation );
279
280 if ( $translation ) {
281 return $translation;
282 }
283
284 return $post_id;
285 }
286