-
Notifications
You must be signed in to change notification settings - Fork 50
/
miscellaneous.php
387 lines (334 loc) · 11.8 KB
/
miscellaneous.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?php
/**
* Functions and (mainly) hooks which don't fit in the various
* classes for whatever reason. Consider these various things
* Private access, for this plugin only, please.
*
* Will try to keep the functions in here to an absolute minumum.
*
* @package Babble
* @since Alpha 1.1
*/
if ( !is_admin() ) {
foreach ( array( 'admin_bar_init', 'admin_bar_menu' ) as $hook ) {
add_action( $hook, 'bbl_load_interface_textdomain', -9999 );
}
foreach ( array( 'add_admin_bar_menus', 'wp_after_admin_bar_render' ) as $hook ) {
add_action( $hook, 'bbl_load_content_textdomain', 9999 );
}
}
/**
* Load the textdomain for Babble's interface language.
*
* This is used to attempt to ensure the interface language is used for the admin toolbar. Effective for core, but not themes or plugins which add items to the admin toolbar.
*/
function bbl_load_interface_textdomain() {
load_default_textdomain( bbl_get_current_interface_lang_code() );
$GLOBALS['wp_locale'] = new WP_Locale();
}
/**
* Load the textdomain for Babble's content language.
*
*/
function bbl_load_content_textdomain() {
load_default_textdomain( bbl_get_current_content_lang_code() );
$GLOBALS['wp_locale'] = new WP_Locale();
}
/**
* Hooks the WP admin_init action to redirect any requests accessing
* content which is not in the current language.
*
* @return void
**/
function bbl_admin_init() {
global $pagenow;
$taxonomy = isset( $_GET[ 'taxonomy' ] ) ? $_GET[ 'taxonomy' ] : false;
$post_type = isset( $_GET[ 'post_type' ] ) ? $_GET[ 'post_type' ] : false;
// Deal with the special URL case of the listing screens for vanilla posts
if ( ! $post_type && 'edit.php' == $pagenow )
$post_type = 'post';
$cur_lang_code = bbl_get_current_lang_code();
if ( $taxonomy ) {
$new_taxonomy = bbl_get_taxonomy_in_lang( $taxonomy, $cur_lang_code );
if ( $taxonomy != $new_taxonomy ) {
$url = add_query_arg( array( 'taxonomy' => $new_taxonomy ) );
wp_redirect( $url );
exit;
}
}
if ( $post_type ) {
$new_post_type = bbl_get_post_type_in_lang( $post_type, $cur_lang_code );
if ( $post_type != $new_post_type ) {
$url = add_query_arg( array( 'post_type' => $new_post_type ) );
wp_redirect( $url );
exit;
}
}
}
add_action( 'admin_init', 'bbl_admin_init' );
/**
* Replicates the core comments_template function, but uses the API
* to fetch the comments and includes more filters.
*
* Loads the comment template specified in $file.
*
* Will not display the comments template if not on single post or page, or if
* the post does not have comments.
*
* Uses the WordPress database object to query for the comments. The comments
* are passed through the 'comments_array' filter hook with the list of comments
* and the post ID respectively.
*
* The $file path is passed through a filter hook called, 'comments_template'
* which includes the TEMPLATEPATH and $file combined. Tries the $filtered path
* first and if it fails it will require the default comment template from the
* default theme. If either does not exist, then the WordPress process will be
* halted. It is advised for that reason, that the default theme is not deleted.
*
* @since 1.5.0
* @global array $comment List of comment objects for the current post
* @uses $wpdb
* @uses $post
* @uses $withcomments Will not try to get the comments if the post has none.
*
* @see comments_template()
*
* @param string $file Optional, default '/comments.php'. The file to load
* @param bool $separate_comments Optional, whether to separate the comments by comment type. Default is false.
* @return null Returns null if no comments appear
*/
function bbl_comments_template( $file = '/comments.php', $separate_comments = false ) {
global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_ID, $user_identity, $overridden_cpage;
if ( !(is_single() || is_page() || $withcomments) || empty($post) )
return;
if ( empty($file) )
$file = '/comments.php';
$req = get_option('require_name_email');
/**
* Comment author information fetched from the comment cookies.
*
* @uses wp_get_current_commenter()
*/
$commenter = wp_get_current_commenter();
/**
* The name of the current comment author escaped for use in attributes.
*/
$comment_author = $commenter['comment_author']; // Escaped by sanitize_comment_cookies()
/**
* The email address of the current comment author escaped for use in attributes.
*/
$comment_author_email = $commenter['comment_author_email']; // Escaped by sanitize_comment_cookies()
/**
* The url of the current comment author escaped for use in attributes.
*/
$comment_author_url = esc_url($commenter['comment_author_url']);
$query = new Bbl_Comment_Query;
$args = array(
'order' => 'ASC',
'post_id' => $post->ID,
'status' => 'approve',
'status' => 'approve',
);
if ( $user_ID) {
$args[ 'unapproved_user_id' ] = $user_ID;
} else if ( ! empty($comment_author) ) {
$args[ 'unapproved_author' ] = wp_specialchars_decode($comment_author,ENT_QUOTES);
$args[ 'unapproved_author_email' ] = $comment_author_email;
}
$args = apply_filters( 'comments_template_args', $args );
$comments = $query->query( $args );
// keep $comments for legacy's sake
$wp_query->comments = apply_filters( 'comments_array', $comments, $post->ID );
$comments = &$wp_query->comments;
$wp_query->comment_count = count($wp_query->comments);
update_comment_cache($wp_query->comments);
if ( $separate_comments ) {
$wp_query->comments_by_type = &separate_comments($comments);
$comments_by_type = &$wp_query->comments_by_type;
}
$overridden_cpage = FALSE;
if ( '' == get_query_var('cpage') && get_option('page_comments') ) {
set_query_var( 'cpage', 'newest' == get_option('default_comments_page') ? get_comment_pages_count() : 1 );
$overridden_cpage = TRUE;
}
if ( !defined('COMMENTS_TEMPLATE') || !COMMENTS_TEMPLATE)
define('COMMENTS_TEMPLATE', true);
$include = apply_filters('comments_template', STYLESHEETPATH . $file );
if ( file_exists( $include ) )
require $include;
elseif ( file_exists( TEMPLATEPATH . $file ) )
require TEMPLATEPATH . $file;
else // Backward compat code will be removed in a future release
require ABSPATH . WPINC . '/theme-compat/comments.php';
}
/**
* WordPress Comment Query class.
*
* See Trac: http://core.trac.wordpress.org/ticket/19623
*
* @since 3.1.0
*/
class Bbl_Comment_Query {
/**
* Execute the query
*
* @since 3.1.0
*
* @param string|array $query_vars
* @return int|array
*/
function query( $query_vars ) {
global $wpdb;
$defaults = array(
'author_email' => '',
'ID' => '',
'karma' => '',
'number' => '',
'offset' => '',
'orderby' => '',
'order' => 'DESC',
'parent' => '',
'post_ID' => '',
'post_id' => '',
'post__in' => '',
'post_author' => '',
'post_name' => '',
'post_parent' => '',
'post_status' => '',
'post_type' => '',
'status' => '',
'type' => '',
'unapproved_author' => '',
'unapproved_author_email' => '',
'unapproved_user_id' => '',
'user_id' => '',
'search' => '',
'count' => false,
);
$this->query_vars = wp_parse_args( $query_vars, $defaults );
do_action_ref_array( 'pre_get_comments', array( $this ) );
extract( $this->query_vars, EXTR_SKIP );
// $args can be whatever, only use the args defined in defaults to compute the key
$key = md5( serialize( compact(array_keys($defaults)) ) );
$last_changed = wp_cache_get('last_changed', 'comment');
if ( !$last_changed ) {
$last_changed = time();
wp_cache_set('last_changed', $last_changed, 'comment');
}
$cache_key = "get_comments:$key:$last_changed";
if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) {
return $cache;
}
if ( empty( $post_id ) && empty( $post__in ) )
$post_id = 0;
$post_id = absint($post_id);
$where = '';
$show_unapproved = ( '' != $unapproved_user_id || '' !== $unapproved_author || '' != $unapproved_author_email );
if ( $show_unapproved ) {
$where .= ' ( ';
}
if ( 'hold' == $status )
$where .= "comment_approved = '0'";
elseif ( 'approve' == $status )
$where .= "comment_approved = '1'";
elseif ( 'spam' == $status )
$where .= "comment_approved = 'spam'";
elseif ( 'trash' == $status )
$where .= "comment_approved = 'trash'";
else
$where .= "( comment_approved = '0' OR comment_approved = '1' )";
if ( $show_unapproved ) {
$where .= ' OR ( comment_approved = 0 ';
if ( '' !== $unapproved_author )
$where .= $wpdb->prepare( ' AND comment_author = %s', $unapproved_author );
if ( '' !== $unapproved_author_email )
$where .= $wpdb->prepare( ' AND comment_author_email = %s', $unapproved_author_email );
if ( '' !== $unapproved_user_id )
$where .= $wpdb->prepare( ' AND user_id = %d', $unapproved_user_id );
$where .= ' ) ) ';
}
$order = ( 'ASC' == strtoupper($order) ) ? 'ASC' : 'DESC';
if ( ! empty( $orderby ) ) {
$ordersby = is_array($orderby) ? $orderby : preg_split('/[,\s]/', $orderby);
$ordersby = array_intersect(
$ordersby,
array(
'comment_agent',
'comment_approved',
'comment_author',
'comment_author_email',
'comment_author_IP',
'comment_author_url',
'comment_content',
'comment_date',
'comment_date_gmt',
'comment_ID',
'comment_karma',
'comment_parent',
'comment_post_ID',
'comment_type',
'user_id',
)
);
$orderby = empty( $ordersby ) ? 'comment_date_gmt' : implode(', ', $ordersby);
} else {
$orderby = 'comment_date_gmt';
}
$number = absint($number);
$offset = absint($offset);
if ( !empty($number) ) {
if ( $offset )
$limits = 'LIMIT ' . $offset . ',' . $number;
else
$limits = 'LIMIT ' . $number;
} else {
$limits = '';
}
if ( $count )
$fields = 'COUNT(*)';
else
$fields = '*';
$join = '';
if ( ! empty($post_id) ) {
$where .= $wpdb->prepare( ' AND comment_post_ID = %d', $post_id );
} else if ( '' != $post__in ) {
$_post__in = implode(',', array_map( 'absint', $post__in ));
$where .= " AND comment_post_ID IN ($_post__in)";
}
if ( '' !== $author_email )
$where .= $wpdb->prepare( ' AND comment_author_email = %s', $author_email );
if ( '' !== $karma )
$where .= $wpdb->prepare( ' AND comment_karma = %d', $karma );
if ( 'comment' == $type ) {
$where .= " AND comment_type = ''";
} elseif( 'pings' == $type ) {
$where .= ' AND comment_type IN ("pingback", "trackback")';
} elseif ( ! empty( $type ) ) {
$where .= $wpdb->prepare( ' AND comment_type = %s', $type );
}
if ( '' !== $parent )
$where .= $wpdb->prepare( ' AND comment_parent = %d', $parent );
if ( '' !== $user_id )
$where .= $wpdb->prepare( ' AND user_id = %d', $user_id );
if ( '' !== $search )
$where .= $this->get_search_sql( $search, array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content' ) );
$post_fields = array_filter( compact( array( 'post_author', 'post_name', 'post_parent', 'post_status', 'post_type', ) ) );
if ( ! empty( $post_fields ) ) {
$join = "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
foreach( $post_fields as $field_name => $field_value )
$where .= $wpdb->prepare( " AND {$wpdb->posts}.{$field_name} = %s", $field_value );
}
$pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits' );
$clauses = apply_filters_ref_array( 'comments_clauses', array( compact( $pieces ), $this ) );
foreach ( $pieces as $piece )
$$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
$query = "SELECT $fields FROM $wpdb->comments $join WHERE $where ORDER BY $orderby $order $limits";
if ( $count )
return $wpdb->get_var( $query );
$comments = $wpdb->get_results( $query );
$comments = apply_filters_ref_array( 'the_comments', array( $comments, $this ) );
wp_cache_add( $cache_key, $comments, 'comment' );
return $comments;
}
}
?>