-
Notifications
You must be signed in to change notification settings - Fork 1
/
votes-creds.php
427 lines (385 loc) · 13.8 KB
/
votes-creds.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
<?php
/*
Plugin Name: Votes Creds
Plugin URI:
Description: Allow users to vote up or down to topics and replies and add or remove creds for this.
Author: Alex Dyakonov and Ilya Kovalyov
Version: 1.0.2
*/
// Hook for adding admin menus
add_action( 'bbp_forum_metabox', 'add_pay_topic' );
add_action( 'bbp_topic_metabox', 'add_pay_reply' );
// Hook for adding attributes
add_action( 'save_post', 'add_forum_attributes' );
add_action( 'save_post', 'add_reply_attributes' );
// Hook for add custom fields to bbpress topics and replies on front end
add_action( 'bbp_theme_before_reply_form_notices', 'theme_before_reply' );
add_action( 'bbp_theme_before_topic_form_notices', 'theme_before_topic' );
// Hook for remove creds
add_action( 'init', 'pay_for_topic' );
add_action( 'init', 'pay_for_reply' );
//Hook for load language
add_action( 'plugins_loaded', 'votes_creds_load_lang' );
// Hook for add creds for vote
add_action( 'bbpvotes_do_post_vote', 'do_post_vote_up', 10, 3 );
add_action( 'bbpvotes_can_user_vote_up_for_post', 'votes_fix', 10, 2);
add_action( 'bbpvotes_can_user_vote_down_for_post', 'votes_fix', 10, 2);
/**
* function to load languages
*/
function votes_creds_load_lang() {
load_plugin_textdomain( "votes-creds", false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* function to add checkbox and select in forums properties
*/
function add_pay_topic() {
$post_meta = get_post_custom();
$pay_forum = $post_meta['_pay_forum'][0];
$cost_forum = $post_meta['_cost_forum'][0];
if ( $post_meta['_pay_vote'][0] == "" ) {
$pay_vote = 1;
} else {
$pay_vote = $post_meta['_pay_vote'][0];
}
if ( $post_meta['_cost_vote'][0] == "" ) {
$cost_vote = 1;
} else {
$cost_vote = $post_meta['_cost_vote'][0];
}
$obj_roles = new WP_Roles();
$roles['no_one'] = 'no one';
$roles += $obj_roles->role_names;
if ( ! isset( $post_meta['_group'][0] ) ) {
$ar_status[0] = 'administrator';
} else {
$ar_status = unserialize( $post_meta['_group'][0] );
}
//get myCRED_Settings
$mycred = mycred();
include( "votes-creds-pay-forum.phtml" );
}
/**
* function to add checkbox in topics properties
*/
function add_pay_reply() {
$post_meta = get_post_custom();
$pay_reply = $post_meta['_pay_reply'][0];
$cost_reply = $post_meta['_cost_reply'][0];
$obj_roles = new WP_Roles();
$roles['no_one'] = 'no one';
$roles += $obj_roles->role_names;
if ( ! isset( $post_meta['_group'][0] ) ) {
$ar_status[0] = 'administrator';
} else {
$ar_status = unserialize( $post_meta['_group'][0] );
}
$mycred = mycred();
include( "votes-creds-pay-reply.phtml" );
}
/**
* function to add forums attribute in wp_postmeta
*/
function add_forum_attributes( $post_id ) {
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
return;
}
// Make sure that it is set.
if ( ! isset( $_POST['pay_forum_cred'] ) ) {
return;
}
if ( isset( $_REQUEST['pay_topic'] ) ) {
$pay_forum_cred = sanitize_text_field( $_POST['pay_forum_cred'] );
} else {
$pay_forum_cred = 0;
}
if ( ! isset( $_POST['voting_up'] ) ) {
return;
}
if ( isset( $_REQUEST['vote_up'] ) ) {
$pay_vote_cred = sanitize_text_field($_POST['voting_up']);
} else {
$pay_vote_cred = 0;
}
$status_array = array();
if ( isset( $_POST['group'] ) ) {
foreach ( $_POST["group"] as $keys => $values ) {
array_push( $status_array, $values );
}
} else {
return;
}
// Update the meta field in the database.
update_post_meta( $post_id, '_pay_forum', ( isset( $_REQUEST['pay_topic'] ) ) ? 1 : 0 );
update_post_meta( $post_id, '_cost_forum', $pay_forum_cred );
update_post_meta( $post_id, '_pay_vote', ( isset( $_REQUEST['vote_up'] ) ) ? 1 : 0 );
update_post_meta( $post_id, '_cost_vote', $pay_vote_cred );
update_post_meta( $post_id, '_group', $status_array );
}
/**
* function to add replyes attribute in wp_postmeta
*/
function add_reply_attributes( $post_id ) {
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Make sure that it is set.
if ( ! isset( $_POST['pay_reply_cred'] ) ) {
return;
}
if ( isset( $_REQUEST['pay_reply'] ) ) {
$pay_reply_cred = sanitize_text_field( $_POST['pay_reply_cred'] );
} else {
$pay_reply_cred = 0;
}
$status_array = array();
if ( isset( $_POST['group'] ) ) {
foreach ( $_POST['group'] as $keys => $values ) {
array_push( $status_array, $values );
}
}
// Update the meta field in the database.
update_post_meta( $post_id, '_pay_reply', ( isset( $_REQUEST['pay_reply'] ) ) ? 1 : 0 );
update_post_meta( $post_id, '_cost_reply', $pay_reply_cred );
update_post_meta( $post_id, '_group', $status_array );
}
/**
* function get meta names
* Returns meta name
*/
function get_mycred_meta_name() {
$mycred = mycred();
global $current_site;
// Not a multisite
if ( ! is_multisite() ) {
return $mycred->get_cred_id();
} else {
$settings_network = mycred_get_settings_network();
if ( $settings_network['master'] ) {
return $mycred->get_cred_id() . '_' . $current_site->id;
} elseif ( $settings_network['central'] ) {
return $mycred->get_cred_id();
}
}
}
/**
* function to add creds for voting
*
* @param type $post_id
* @param type $vote MUST BE defined, MUST BE a boolean
* @param type $user_id
*
*/
function do_post_vote_up( $post_id, $user_id, $vote ) {
$voteplus = $vote;
if ( ! $post = get_post( $post_id ) ) {
return;
}
if ( mycred_exclude_user( $user_id ) ) {
return;
}
$post_type = $post->post_type;
if ( $post_type == 'topic' ) {
$author_id = $post->post_author;
$forum_id = $post->post_parent;
$forum = get_post_meta( $forum_id );
} elseif ( $post_type == 'reply' ) {
$author_id = $post->post_author;
$topic_id = $post->post_parent;
$post = get_post( $topic_id );
$forum_id = $post->post_parent;
$forum = get_post_meta( $forum_id );
}
$pay_vote = $forum['_pay_vote'][0];
if ( $pay_vote == 0 ) {
return;
} else if ( $pay_vote == 1 ) {
$cost_vote = $forum['_cost_vote'][0];
}
$post_meta = get_post_meta( $post_id );
$author_mycred_default = get_user_meta( $author_id, get_mycred_meta_name(), true );
global $wpdb;
if ( array_key_exists( '_toggle_vote', $post_meta ) && in_array( $user_id, $post_meta['_toggle_vote'] ) ) {
if ( $voteplus ) {
$wpdb->delete( $wpdb->prefix . 'myCRED_log', array( 'ref_id' => $user_id, 'creds' => -$cost_vote ), array( '%d' ) );
$author_creds = $author_mycred_default + $cost_vote;
update_user_meta( $author_id, get_mycred_meta_name(), $author_creds );
mycred_add( 'vote_up', $author_id, $cost_vote, 'Vote_up', $user_id );
} else {
$wpdb->delete( $wpdb->prefix . 'myCRED_log', array( 'ref_id' => $user_id, 'creds' => $cost_vote ), array( '%d' ) );
$author_creds = $author_mycred_default - $cost_vote;
update_user_meta( $author_id, get_mycred_meta_name(), $author_creds );
mycred_subtract( 'vote_down', $author_id, $cost_vote, 'Vote_down', $user_id );
}
} else {
if ( $voteplus ) {
// add points
mycred_add( 'vote_up', $author_id, $cost_vote, 'Vote_up', $user_id );
} else {
// remove points
mycred_subtract( 'vote_down', $author_id, $cost_vote, 'Vote_down', $user_id );
}
add_post_meta( $post_id, '_toggle_vote', $user_id );
}
}
/**
* function to add inscription before the topic
*/
function theme_before_topic() {
$post_meta = get_post_custom();
$pay_forum = $post_meta['_pay_forum'][0];
$cost_forum = $post_meta['_cost_forum'][0];
$mycred = mycred();
if ( $cost_forum == 1 ) {
$message_pay = __( 'Creating topics in this forum pay. It is worth ', 'votes-creds' ) . $cost_forum . ' ' . $mycred->singular();
} else {
$message_pay = __( 'Creating topics in this forum pay. It is worth ', 'votes-creds' ) . $cost_forum . ' ' . $mycred->plural();
}
//add fields if admin
$current_user = wp_get_current_user();
if ( current_user_can( 'manage_options' ) ) {
$obj_roles = new WP_Roles();
$roles['no_one'] = 'no one';
$roles += $obj_roles->role_names;
$ar_status[0] = 'administrator';
include( 'votes-creds-pay-reply.phtml' );
}
if ( $pay_forum == 1 && $cost_forum > 0 ) {
include( 'votes-creds-message-pay.phtml' );
} else {
return;
}
}
/**
* function to remove creds for the creation of topic in the selected forum
*/
function pay_for_topic() {
if ( isset( $_POST ) ) {
if ( empty( $_POST['bbp_topic_content'] ) ) {
return;
}
$forum_id = $_POST['bbp_forum_id'];
$user_id = get_current_user_id();
$mycred = mycred();
$user_creds = get_user_meta( $user_id, 'mycred_default', true );
$post_meta = get_post_meta( $forum_id );
$pay_forum = $post_meta['_pay_forum'][0];
$cost_forum = $post_meta['_cost_forum'][0];
$ar_status = unserialize( $post_meta['_group'][0] );
$current_user = wp_get_current_user();
if ( ! ( $current_user instanceof WP_User ) ) {
return;
}
if( ! $ar_status ){
return;
} else {
$roles = $current_user->roles;
}
if ( $pay_forum != 0 ) {
if ( in_array( $roles[0], $ar_status ) ) {
return;
} else if ( $user_creds < $cost_forum ) {
bbp_add_error( 'bbp_new_reply_nonce', sprintf( __( '<strong>ERROR</strong>: You have not enough %s to post a topic!', 'votes-creds' ), $mycred->plural() ) );
return;
} else {
add_action( 'bbp_new_topic', 'remove_creds_for_topic' );
}
} else {
add_action( 'bbp_new_topic', 'remove_creds_for_topic' );
}
}
}
function remove_creds_for_topic() {
$user_id = get_current_user_id();
if ( mycred_exclude_user( $user_id ) ) {
return;
}
$forum_id = $_POST['bbp_forum_id'];
$post_meta = get_post_meta( $forum_id );
$pay_forum = $post_meta['_pay_forum'][0];
$cost_forum = $post_meta['_cost_forum'][0];
if ( $pay_forum == 0 ) {
return;
} else {
// remove points
mycred_subtract( 'vote_down', $user_id, $cost_forum, 'Pay for topic', date( 'y' ) );
}
}
/**
* function to add inscription before the reply
*/
function theme_before_reply() {
$post_meta = get_post_custom();
$pay_reply = $post_meta['_pay_reply'][0];
$cost_reply = $post_meta['_cost_reply'][0];
$mycred = mycred();
if ( $cost_reply == 1 ) {
$reply_pay = __( 'Reply in this forum paid. It is worth ', 'votes-creds' ) . $cost_reply . ' ' . $mycred->singular();
} else {
$reply_pay = __( 'Reply in this forum paid. It is worth ', 'votes-creds' ) . $cost_reply . ' ' . $mycred->plural();
}
if ( $pay_reply == 1 && $cost_reply > 0 ) {
include( 'votes-creds-reply-pay.phtml' );
} else {
return;
}
}
/**
* function to remove creds for the creation of reply
*/
function pay_for_reply() {
if ( isset( $_POST ) ) {
if ( empty( $_POST['bbp_reply_content'] ) ) {
return;
}
$topic_id = $_POST['bbp_topic_id'];
$user_id = get_current_user_id();
$mycred = mycred();
$user_creds = get_user_meta( $user_id, 'mycred_default', true );
$post_meta = get_post_meta( $topic_id );
$pay_reply = $post_meta['_pay_reply'][0];
$cost_reply = $post_meta['_cost_reply'][0];
$ar_status = unserialize( $post_meta['_group'][0] );
$current_user = wp_get_current_user();
if ( ! ($current_user instanceof WP_User ) ) {
return;
}
if( ! $ar_status ){
return;
} else {
$roles = $current_user->roles;
}
if ( in_array( $roles[0], $ar_status ) ) {
return;
} else if ( $user_creds < $cost_reply && $pay_reply != 0 ) {
bbp_add_error( 'bbp_new_reply_nonce', sprintf( __( '<strong>ERROR</strong>: You have not enough %s to reply!', 'votes-creds' ), $mycred->plural() ) );
return;
} else {
add_action( 'bbp_new_reply', 'remove_creds_for_reply' );
}
}
}
function remove_creds_for_reply() {
$user_id = get_current_user_id();
if ( mycred_exclude_user( $user_id ) ) {
return;
}
$topic_id = $_POST['bbp_topic_id'];
$post_meta = get_post_meta( $topic_id );
$pay_reply = $post_meta['_pay_reply'][0];
$cost_reply = $post_meta['_cost_reply'][0];
if ( $pay_reply != 0 ) {
// remove points
mycred_subtract( 'vote_down', $user_id, $cost_reply, 'Pay for reply', date( 'y' ) );
} else {
return;
}
}
function votes_fix( $can, $post_id) {
if ($can) {
return $can;
}
return current_user_can( 'participate', $post_id );
}