/
query.php
432 lines (365 loc) · 10.2 KB
/
query.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
<?php
/**
* AnsPress notification query class.
*
* @package AnsPress
* @subpackage Notifications Addon
* @author Rahul Aryan <rah12@live.com>
* @license GPL-3.0+
* @link https://anspress.net
* @copyright 2014 Rahul Aryan
* @since 4.0.0
*/
namespace AnsPress;
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* User notification query
* Query wrapper for fetching notifications.
*
* @param array|string $args arguments passed to class.
* @since 1.0.0
*/
class Notifications extends \AnsPress_Query {
/**
* Verbs
*
* @var array
*/
public $verbs = array();
/**
* Initialize the class.
*
* @param array $args Arguments.
*/
public function __construct( $args = array() ) {
$this->ids['reputation'] = array();
$this->pos['reputation'] = array();
$this->verbs = ap_notification_verbs();
parent::__construct( $args );
}
/**
* Prepare and fetch notifications from database.
*/
public function query() {
global $wpdb;
$ref_id_q = '';
if ( isset( $this->args['ref_id'] ) ) {
$ref_id_q = $wpdb->prepare( 'AND noti_ref_id = %d', (int) $this->args['ref_id'] );
}
$ref_type_q = '';
if ( isset( $this->args['ref_type'] ) ) {
$ref_type_q = $wpdb->prepare( 'AND noti_ref_type = %s', sanitize_title( $this->args['ref_type'] ) );
}
$verb_q = '';
if ( isset( $this->args['verb'] ) ) {
$verb_q = $wpdb->prepare( 'AND noti_verb = %s', $this->args['verb'] );
}
$seen_q = '';
if ( isset( $this->args['seen'] ) ) {
$seen_q = $wpdb->prepare( 'AND noti_seen = %d', (bool) $this->args['seen'] );
}
$order = 'DESC' === $this->args['order'] ? 'DESC' : 'ASC';
$query = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}ap_notifications WHERE noti_user_id = %d {$ref_id_q} {$ref_type_q} {$verb_q} {$seen_q} ORDER BY noti_date {$order} LIMIT {$this->offset},{$this->per_page}", $this->args['user_id'] ); // phpcs:ignore WordPress.DB
$this->objects = $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB
$count_query = $wpdb->prepare( "SELECT count(noti_id) FROM {$wpdb->prefix}ap_notifications WHERE noti_user_id = %d {$ref_id_q} {$ref_type_q} {$verb_q} {$seen_q}", $this->args['user_id'] ); // phpcs:ignore WordPress.DB
$this->total_count = $wpdb->get_var( apply_filters( 'ap_notifications_found_rows', $count_query, $this ) ); // phpcs:ignore WordPress.DB
$this->prefetch();
parent::query();
}
/**
* Prefetch posts, comments and other data.
*/
public function prefetch() {
foreach ( (array) $this->objects as $key => $noti ) {
if ( ! empty( $noti->noti_ref_id ) ) {
$current_verb = $this->verb_args( $noti->noti_verb );
if ( in_array( $current_verb['ref_type'], array( 'question', 'answer', 'post' ), true ) ) {
$this->add_prefetch_id( 'post', $noti->noti_ref_id, $key );
}
if ( 'comment' === $current_verb['ref_type'] ) {
$this->add_prefetch_id( 'comment', $noti->noti_ref_id, $key );
}
if ( 'reputation' === $current_verb['ref_type'] ) {
$this->add_prefetch_id( 'reputation', $noti->noti_ref_id, $key );
}
}
if ( ! empty( $noti->noti_actor ) ) {
$this->add_prefetch_id( 'user', $noti->noti_actor );
}
}
$this->prefetch_posts();
$this->prefetch_comments();
$this->prefetch_actors();
$this->prefetch_reputations();
}
/**
* Pre fetch post contents and append to object.
*/
public function prefetch_posts() {
if ( empty( $this->ids['post'] ) ) {
return;
}
global $wpdb;
$ids_str = esc_sql( sanitize_comma_delimited( $this->ids['post'] ) );
$posts = $wpdb->get_results( "SELECT * FROM {$wpdb->posts} WHERE ID in ({$ids_str})" ); // phpcs:ignore WordPress.DB
foreach ( (array) $posts as $_post ) {
$this->append_ref_data( 'post', $_post->ID, $_post );
}
}
/**
* Pre fetch comments and append data to object.
*/
public function prefetch_comments() {
global $wpdb;
if ( empty( $this->ids['comment'] ) ) {
return;
}
$ids = esc_sql( sanitize_comma_delimited( $this->ids['comment'] ) );
$comments = $wpdb->get_results( "SELECT c.*, p.post_type, p.post_title FROM {$wpdb->comments} c LEFT JOIN $wpdb->posts p ON c.comment_post_ID = p.ID WHERE comment_ID in ({$ids})" ); // phpcs:ignore WordPress.DB
foreach ( (array) $comments as $_comment ) {
$this->append_ref_data( 'comment', $_comment->comment_ID, $_comment );
}
}
/**
* Prefetch actors user object.
*/
public function prefetch_actors() {
if ( empty( $this->ids['user'] ) ) {
return;
}
ap_post_author_pre_fetch( $this->ids['user'] );
}
/**
* Prefetch notification reputations.
*/
public function prefetch_reputations() {
global $wpdb;
if ( empty( $this->ids['reputation'] ) ) {
return;
}
$ids = esc_sql( sanitize_comma_delimited( $this->ids['reputation'] ) );
$reputations = $wpdb->get_results( "SELECT rep_id, rep_event FROM {$wpdb->ap_reputations} WHERE rep_id in ({$ids})" ); // phpcs:ignore WordPress.DB
foreach ( (array) $reputations as $rep ) {
$rep->points = ap_get_reputation_event_points( $rep->rep_event );
$this->append_ref_data( 'reputation', $rep->rep_id, $rep );
}
}
/**
* Return verb arguments.
*
* @param string $key Verb key.
* @return array|null
*/
public function verb_args( $key ) {
if ( isset( $this->verbs[ $key ] ) ) {
return $this->verbs[ $key ];
}
}
/**
* Include current item template.
*/
public function item_template() {
$verb = $this->verb_args( $this->object->noti_verb );
$file = ap_get_theme_location( 'addons/notification/' . sanitize_file_name( $verb['ref_type'] . '.php' ) );
if ( file_exists( $file ) ) {
include $file;
}
}
/**
* Set up the current notification inside the loop.
*/
public function the_notification() {
parent::the_object();
}
/**
* Get notification reference id.
*
* @return int
*/
public function get_ref_id() {
return (int) $this->object->noti_ref_id;
}
/**
* Get notification reference type.
*
* @return string
*/
public function get_ref_type() {
return $this->object->noti_ref_type;
}
/**
* Get the permalink of notification based on ref_id and ref_type.
*
* @return string
*/
public function get_permalink() {
if ( in_array( $this->get_ref_type(), array( 'question', 'answer', 'post' ), true ) ) {
return ap_get_short_link( array( 'ap_p' => $this->get_ref_id() ) );
} elseif ( 'comment' === $this->get_ref_type() ) {
return ap_get_short_link( array( 'ap_c' => $this->get_ref_id() ) );
} elseif ( 'reputation' === $this->get_ref_type() ) {
return ap_get_short_link(
array(
'ap_u' => $this->object->noti_user_id,
'sub' => 'reputations',
)
);
}
}
/**
* Echo permalink, Alice of `get_permalink`.
*/
public function the_permalink() {
echo esc_url( $this->get_permalink() );
}
/**
* Get actor ID.
*
* @return integer
*/
public function get_actor() {
if ( ! $this->hide_actor() ) {
return ap_user_display_name( $this->object->noti_actor ); // xss okay.
} else {
return __( 'Someone', 'anspress-question-answer' );
}
}
/**
* Echo actor display name.
*/
public function the_actor() {
echo esc_html( $this->get_actor() );
}
/**
* Return avatar of notification actor.
*
* @param integer|string $size Size of the avatar.
* @return string
*/
public function actor_avatar( $size = 35 ) {
if ( ! $this->hide_actor() ) {
return get_avatar( $this->object->noti_actor, $size );
}
}
/**
* Echo actor avatar.
*
* @param integer|string $size Size of the avatar.
*/
public function the_actor_avatar( $size = 40 ) {
if ( ! $this->hide_actor() ) {
echo wp_kses_post( $this->actor_avatar( $size ) );
}
}
/**
* Get the verb of a notification.
*/
public function get_verb() {
$key = $this->object->noti_verb;
$verb_text = '';
if ( isset( $this->verbs[ $key ] ) ) {
$args = $this->verbs[ $key ];
$verb_text = $args['label'];
$args = array(
'%cpt%' => __( 'post', 'anspress-question-answer' ),
'%points%' => number_format_i18n( 0 ),
);
if ( isset( $this->object->ref ) ) {
if ( isset( $this->object->ref->post_type ) ) {
if ( 'question' === $this->object->ref->post_type ) {
$args['%cpt%'] = __( 'question', 'anspress-question-answer' );
} elseif ( 'answer' === $this->object->ref->post_type ) {
$args['%cpt%'] = __( 'answer', 'anspress-question-answer' );
}
}
if ( isset( $this->object->ref->points ) ) {
$args['%points%'] = number_format_i18n( $this->object->ref->points );
}
}
$verb_text = strtr( $verb_text, $args );
}
return $verb_text;
}
/**
* Echo verb.
*/
public function the_verb() {
echo wp_kses_post( $this->get_verb() );
}
/**
* Return date of a notification.
*
* @return string
*/
public function get_date() {
return $this->object->noti_date;
}
/**
* Echo human readble date.
*/
public function the_date() {
echo wp_kses_post( ap_human_time( $this->get_date(), false ) );
}
/**
* Return ref title.
*
* @return string
*/
public function get_ref_title() {
if ( isset( $this->object->ref ) ) {
$verb_args = $this->verb_args( $this->object->noti_verb );
if ( in_array( $verb_args['ref_type'], array( 'post', 'comment' ), true ) && isset( $this->object->ref->post_title ) ) {
return ap_truncate_chars( $this->object->ref->post_title, 80 );
}
}
}
/**
* Print ref title.
*/
public function the_ref_title() {
echo esc_html( $this->get_ref_title() );
}
/**
* Return notification points if ref_type is reputation.
*
* @return integer
*/
public function get_reputation_points() {
if ( isset( $this->object->ref ) && isset( $this->object->ref->points ) ) {
return $this->object->ref->points;
}
}
/**
* Print reputtaion points.
*/
public function the_reputation_points() {
echo esc_attr( $this->get_reputation_points() );
}
/**
* Hide actor.
*
* @return boolean
*/
public function hide_actor() {
$args = $this->verb_args( $this->object->noti_verb );
return (bool) $args['hide_actor'];
}
/**
* Return icon of notification.
*
* @return string
*/
public function get_icon() {
$args = $this->verb_args( $this->object->noti_verb );
return $args['icon'];
}
/**
* Print icon of notification if defined.
*/
public function the_icon() {
echo esc_attr( $this->get_icon() );
}
}