-
Notifications
You must be signed in to change notification settings - Fork 104
/
flag.php
215 lines (187 loc) · 5.09 KB
/
flag.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
<?php
/**
* All functions and classes related to flagging
* This file keep all function required by flagging system.
*
* @link https://anspress.net
* @since 2.3.4
* @package AnsPress
**/
// phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* All flag methods.
*/
class AnsPress_Flag {
/**
* Ajax callback to process post flag button
*
* @since 2.0.0
*/
public static function action_flag() {
$post_id = (int) ap_sanitize_unslash( 'post_id', 'r' );
if ( ! anspress_verify_nonce( 'flag_' . $post_id ) || ! is_user_logged_in() ) {
ap_ajax_json( 'something_wrong' );
}
$post = ap_get_post( $post_id );
$userid = get_current_user_id();
$is_flagged = ap_is_user_flagged( $post_id );
// Die if already flagged.
if ( $is_flagged ) {
ap_ajax_json(
array(
'success' => false,
'snackbar' => array(
'message' => sprintf(
/* Translators: %s Question or Answer post type label for already reported question or answer. */
__( 'You have already reported this %s.', 'anspress-question-answer' ),
( 'question' === $post->post_type ) ? esc_html__( 'question', 'anspress-question-answer' ) : esc_html__( 'answer', 'anspress-question-answer' )
),
),
)
);
}
ap_add_flag( $post_id );
$count = ap_update_flags_count( $post_id );
ap_ajax_json(
array(
'success' => true,
'action' => array(
'count' => $count,
'active' => true,
),
'snackbar' => array(
'message' => sprintf(
/* Translators: %s Question or Answer post type label for reported question or answer. */
__( 'Thank you for reporting this %s.', 'anspress-question-answer' ),
( 'question' === $post->post_type ) ? esc_html__( 'question', 'anspress-question-answer' ) : esc_html__( 'answer', 'anspress-question-answer' )
),
),
)
);
}
}
/**
* Add flag vote data to ap_votes table.
*
* @param integer $post_id Post ID.
* @param integer $user_id User ID.
* @return integer|boolean
*/
function ap_add_flag( $post_id, $user_id = false ) {
if ( false === $user_id ) {
$user_id = get_current_user_id();
}
$inserted = ap_vote_insert( $post_id, $user_id, 'flag' );
return $inserted;
}
/**
* Count flag votes.
*
* @param integer $post_id Post ID.
* @return integer
* @since 4.0.0
*/
function ap_count_post_flags( $post_id ) {
$rows = ap_count_votes(
array(
'vote_post_id' => $post_id,
'vote_type' => 'flag',
)
);
if ( false !== $rows ) {
return (int) $rows[0]->count;
}
return 0;
}
/**
* Check if user already flagged a post.
*
* @param bool|integer $post Post.
* @return bool
*/
function ap_is_user_flagged( $post = null ) {
$_post = ap_get_post( $post );
if ( is_user_logged_in() ) {
return ap_is_user_voted( $_post->ID, 'flag' );
}
return false;
}
/**
* Flag button html.
*
* @param mixed $post Post.
* @return string
* @since 0.9
*/
function ap_flag_btn_args( $post = null ) {
if ( ! is_user_logged_in() ) {
return;
}
$_post = ap_get_post( $post );
$flagged = ap_is_user_flagged( $_post );
if ( ! $flagged ) {
$title = sprintf(
/* Translators: %s Question or Answer post type label for flagging question or answer. */
__( 'Flag this %s', 'anspress-question-answer' ),
( 'question' === $_post->post_type ) ? esc_html__( 'question', 'anspress-question-answer' ) : esc_html__( 'answer', 'anspress-question-answer' )
);
} else {
$title = sprintf(
/* Translators: %s Question or Answer post type label for already flagged question or answer. */
__( 'You have flagged this %s', 'anspress-question-answer' ),
( 'question' === $_post->post_type ) ? esc_html__( 'question', 'anspress-question-answer' ) : esc_html__( 'answer', 'anspress-question-answer' )
);
}
$actions['close'] = array(
'cb' => 'flag',
'icon' => 'apicon-check',
'query' => array(
'__nonce' => wp_create_nonce( 'flag_' . $_post->ID ),
'post_id' => $_post->ID,
),
'label' => __( 'Flag', 'anspress-question-answer' ),
'title' => $title,
'count' => $_post->flags,
'active' => $flagged,
);
return $actions['close'];
}
/**
* Delete multiple posts flags.
*
* @param integer $post_id Post id.
* @return boolean
*/
function ap_delete_flags( $post_id ) {
return ap_delete_votes( $post_id, 'flag' );
}
/**
* Update total flagged question and answer count.
*
* @since 4.0.0
*/
function ap_update_total_flags_count() {
$opt = get_option( 'anspress_global', array() );
$opt['flagged_questions'] = ap_total_posts_count( 'question', 'flag' );
$opt['flagged_answers'] = ap_total_posts_count( 'answer', 'flag' );
update_option( 'anspress_global', $opt );
}
/**
* Return total flagged post count.
*
* @return array
* @since 4.0.0
*/
function ap_total_flagged_count() {
$opt['flagged_questions'] = ap_total_posts_count( 'question', 'flag' );
$updated = true;
$opt['flagged_answers'] = ap_total_posts_count( 'answer', 'flag' );
$updated = true;
return array(
'questions' => $opt['flagged_questions'],
'answers' => $opt['flagged_answers'],
);
}