PluginProbe
wpForo Forum / 1.2.0
wpForo Forum v1.2.0
3.1.5 3.1.4 3.1.2 3.1.1 3.1.0 3.0.9 3.0.8 3.0.7 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 1.4.2 All 137 releases
wpforo / wpf-includes / class-members.php

class-members.php in wpForo Forum 1.2.0, at wpf-includes/class-members.php

1,188 lines 46.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // Exit if accessed directly
3 if( !defined( 'ABSPATH' ) ) exit;
4
5
6 class wpForoMember{
7
8 private $wpforo;
9 private static $cache = array();
10
11 function __construct( $wpForo ){
12 if(!isset($this->wpforo)) $this->wpforo = $wpForo;
13 add_action('delete_user_form', array(&$this, 'show_delete_form'), 10, 2);
14 }
15
16 public function send_new_user_notifications($user_id, $notify = 'admin'){
17 wp_send_new_user_notifications( $user_id, $notify );
18 }
19
20 private function add_profile($args){
21 if(empty($args)) return FALSE;
22 if(!isset($args['userid']) || !$args['userid'] || !isset($args['username']) || !$args['username'] ) return FALSE;
23 extract( $args, EXTR_OVERWRITE );
24 $this->reset($userid);
25 return $this->wpforo->db->insert(
26 $this->wpforo->db->prefix . 'wpforo_profiles',
27 array( 'userid' => intval($userid),
28 'username' => sanitize_user($username),
29 'groupid' => intval((isset($groupid) && $groupid ? $groupid : $this->wpforo->default_groupid)),
30 'site' => (isset($site) ? sanitize_text_field($site) : '' ),
31 'timezone' => ( isset($timezone) ? sanitize_text_field($timezone) : 'UTC+0' ),
32 'about' => ( isset($about) ? stripslashes( wpforo_kses(trim($about), 'user_description') ) : '' ),
33 'last_login' => ( isset($last_login) ? $last_login : current_time('mysql', 1) ) ),
34 array( '%d', '%s', '%d', '%s', '%s', '%s', '%s' )
35 );
36 }
37
38 function edit_profile($args){
39 if(empty($args)) return FALSE;
40 if( !isset($args['userid']) || !$args['userid'] ) return FALSE;
41 extract( $args, EXTR_OVERWRITE );
42
43 $fields = array();
44 $fields_types = array();
45
46 if(isset($last_login) && $last_login){
47 $fields['last_login'] = sanitize_text_field($last_login);
48 $fields_types[] = '%s';
49 }
50
51 if(isset($groupid) && $groupid){
52 if( $this->wpforo->current_user_groupid == 1 || current_user_can('administrator') ){
53 $fields['groupid'] = intval($groupid);
54 $fields_types[] = '%d';
55 }
56 }
57
58 if(isset($title) && $title){
59 $fields['title'] = sanitize_text_field(trim($title));
60 $fields_types[] = '%s';
61 }
62 if(isset($site)){
63 $fields['site'] = sanitize_text_field(trim($site));
64 $fields_types[] = '%s';
65 }
66 if(isset($icq)){
67 $fields['icq'] = sanitize_text_field(trim($icq));
68 $fields_types[] = '%s';
69 }
70 if(isset($aim)){
71 $fields['aim'] = sanitize_text_field(trim($aim));
72 $fields_types[] = '%s';
73 }
74 if(isset($yahoo)){
75 $fields['yahoo'] = sanitize_text_field(trim($yahoo));
76 $fields_types[] = '%s';
77 }
78 if(isset($msn)){
79 $fields['msn'] = sanitize_text_field(trim($msn));
80 $fields_types[] = '%s';
81 }
82 if(isset($facebook)){
83 $fields['facebook'] = sanitize_text_field(trim($facebook));
84 $fields_types[] = '%s';
85 }
86 if(isset($twitter)){
87 $fields['twitter'] = sanitize_text_field(trim($twitter));
88 $fields_types[] = '%s';
89 }
90 if(isset($gtalk)){
91 $fields['gtalk'] = sanitize_text_field(trim($gtalk));
92 $fields_types[] = '%s';
93 }
94 if(isset($skype)){
95 $fields['skype'] = sanitize_text_field(trim($skype));
96 $fields_types[] = '%s';
97 }
98 if(isset($signature)){
99 $fields['signature'] = stripslashes(wpforo_kses(trim($signature), 'user_description'));
100 $fields_types[] = '%s';
101 }
102 if(isset($about)){
103 $fields['about'] = stripslashes(wpforo_kses(trim($about), 'user_description'));
104 $fields_types[] = '%s';
105 }
106 if(isset($occupation)){
107 $fields['occupation'] = stripslashes(sanitize_text_field(trim($occupation)));
108 $fields_types[] = '%s';
109 }
110 if(isset($location)){
111 $fields['location'] = stripslashes(sanitize_text_field(trim($location)));
112 $fields_types[] = '%s';
113 }
114 if(isset($timezone)){
115 $fields['timezone'] = sanitize_text_field(trim($timezone));
116 $fields_types[] = '%s';
117 }
118 if(isset($avatar_type) && $avatar_type != 'gravatar' && isset($avatar_url) && $avatar_url){
119 $fields['avatar'] = esc_url(trim($avatar_url));
120 $fields_types[] = '%s';
121 }
122 if(isset($avatar_type) && $avatar_type == 'gravatar'){
123 $fields['avatar'] = '';
124 $fields_types[] = '%s';
125 }
126
127 $this->reset($userid);
128
129 return $this->wpforo->db->update(
130 $this->wpforo->db->prefix.'wpforo_profiles',
131 $fields,
132 array('userid' => intval($userid)),
133 $fields_types,
134 array('%d')
135 );
136 }
137
138 function create($args){
139 if(!wpforo_feature('user-register', $this->wpforo)){
140 $this->wpforo->notice->add('User registration is disabled.', 'error');
141 return FALSE;
142 }
143 if( !empty($args) && is_array($args) && !empty($args['user_pass1']) ){
144 remove_action('register_new_user', 'wp_send_new_user_notifications');
145 add_action('register_new_user', array($this, 'send_new_user_notifications'));
146
147 $errors = new WP_Error();
148
149 extract($args, EXTR_OVERWRITE);
150 $sanitized_user_login = sanitize_user( $user_login );
151 $user_email = apply_filters( 'user_registration_email', $user_email );
152 $user_pass1 = trim(substr($user_pass1, 0, 100));
153 $user_pass2 = trim(substr($user_pass2, 0, 100));
154 $illegal_user_logins = array_map( 'strtolower', (array) apply_filters( 'illegal_user_logins', array() ) );
155 if ( $sanitized_user_login == '' ) {
156 $errors->add( 'empty_username', __( '<strong>ERROR</strong>: Please enter a username.' ) );
157 $this->wpforo->notice->add('Username is missed.', 'error');
158 return FALSE;
159 }elseif ( ! validate_username( $user_login ) ) {
160 $errors->add( 'invalid_username', __( '<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.' ) );
161 $sanitized_user_login = '';
162 $this->wpforo->notice->add('Illegal character in username.', 'error');
163 $user_login = '';
164 return FALSE;
165 }elseif( strlen($user_login) < 3 || strlen($user_login) > 15 ){
166 $this->wpforo->notice->add('Username length must be between 3 characters and 15 characters.', 'error');
167 return FALSE;
168 }elseif ( username_exists( $sanitized_user_login ) ) {
169 $errors->add( 'username_exists', __( '<strong>ERROR</strong>: This username is already registered. Please choose another one.' ) );
170 $this->wpforo->notice->add('Username exists. Please insert another.', 'error');
171 return FALSE;
172 }elseif ( in_array( strtolower( $sanitized_user_login ), $illegal_user_logins ) ) {
173 $errors->add( 'invalid_username', __( '<strong>ERROR</strong>: Sorry, that username is not allowed.' ) );
174 $this->wpforo->notice->add('ERROR: invalid_username. Sorry, that username is not allowed. Please insert another.', 'error');
175 return FALSE;
176 }elseif ( $user_email == '' ) {
177 $errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please type your email address.' ) );
178 $this->wpforo->notice->add('Insert your Email address.', 'error');
179 return FALSE;
180 }elseif ( ! is_email( $user_email ) ) {
181 $errors->add( 'invalid_email', __( '<strong>ERROR</strong>: The email address isn&#8217;t correct.' ) );
182 $this->wpforo->notice->add('Invalid Email address', 'error');
183 $user_email = '';
184 return FALSE;
185 }elseif ( email_exists( $user_email ) ) {
186 $errors->add( 'email_exists', __( '<strong>ERROR</strong>: This email is already registered, please choose another one.' ) );
187 $this->wpforo->notice->add('Email address exists. Please insert another.', 'error');
188 return FALSE;
189 }elseif( strlen($user_pass1) < 6 || strlen($user_pass1) > 20 ){
190 $this->wpforo->notice->add('Password length must be between 6 characters and 20 characters.', 'error');
191 return FALSE;
192 }elseif($user_pass1 != $user_pass2){
193 $this->wpforo->notice->add('Password mismatch.', 'error');
194 return FALSE;
195 }else{
196 do_action( 'register_post', $sanitized_user_login, $user_email, $errors );
197 $errors = apply_filters( 'registration_errors', $errors, $sanitized_user_login, $user_email );
198 if ( $errors->get_error_code() ){
199 $args = array();
200 foreach($errors->errors as $u_err) $args[] = $u_err[0];
201 $this->wpforo->notice->add($args, 'error');
202 return FALSE;
203 }
204 $user_id = wp_create_user( $sanitized_user_login, $user_pass1, $user_email );
205 if ( !is_wp_error( $user_id ) && $user_id ) {
206 $creds = array('user_login' => $sanitized_user_login, 'user_password' => $user_pass1 );
207 wp_signon($creds);
208 $this->wpforo->notice->add('Success! Thank you Dear Friend', 'success');
209 do_action( 'register_new_user', $user_id );
210 return $user_id;
211 }
212 }
213 }elseif( !empty($args['user_login']) && !empty($args['user_email']) ){
214 $user_id = register_new_user( $args['user_login'], $args['user_email'] );
215 if ( !is_wp_error( $user_id ) && $user_id ) {
216 $this->wpforo->notice->add('Success! Please check your mail for confirmation.', 'success');
217 return $user_id;
218 }
219 }
220 if(!empty($user_id->errors)){
221 $args = array();
222 foreach($user_id->errors as $u_err) $args[] = $u_err[0];
223 $this->wpforo->notice->add($args, 'error');
224 return FALSE;
225 }
226 $this->wpforo->notice->add('Registration Error', 'error');
227 return FALSE;
228 }
229
230 function edit( $args = array() ){
231 if( empty($args) && empty($_REQUEST['member']) ) return FALSE;
232 if( empty($args) && !empty($_REQUEST['member']) ) $args = $_REQUEST['member'];
233 extract($args, EXTR_OVERWRITE);
234
235 if( isset($userid) && isset($display_name) && isset($user_email) && isset($user_nickname) ){
236 $userid = intval($userid);
237 $user_email = sanitize_email($user_email);
238 if ( ! is_email( $user_email ) ) {
239 $this->wpforo->notice->add('Invalid Email address', 'error');
240 $user_email = '';
241 return FALSE;
242 }elseif ( ( $owner_id = email_exists( $user_email ) ) && ( $owner_id != $userid ) ) {
243 $this->wpforo->notice->add('This email address is already registered. Please insert another.', 'error');
244 return FALSE;
245 }
246
247 $user_nickname = sanitize_title($user_nickname);
248 if( is_numeric($user_nickname) ){
249 $this->wpforo->notice->add('Numerical nicknames are not allowed. Please insert another.', 'error');
250 return FALSE;
251 }
252 $sql = "SELECT `ID` FROM `".$this->wpforo->db->base_prefix."users` WHERE `ID` != ". intval($userid) ." AND ( `user_nicename` LIKE '".esc_sql($user_nickname)."' OR `ID` LIKE '".esc_sql($user_nickname)."' )";
253 if( $this->wpforo->db->get_var($sql)){
254 $this->wpforo->notice->add('This nickname is already registered. Please insert another.', 'error');
255 return FALSE;
256 }
257
258 if ( !is_user_logged_in() || !$this->wpforo->perm->user_can_manage_user( $this->wpforo->current_userid, $userid ) ) {
259 $this->wpforo->notice->add('Permission denied', 'error');
260 return FALSE;
261 }
262
263 if( $display_name && $user_email && $user_nickname ){
264 $this->wpforo->db->update(
265 $this->wpforo->db->base_prefix."users",
266 array(
267 'display_name' => sanitize_text_field($display_name),
268 'user_nicename' => sanitize_title($user_nickname),
269 'user_email' => sanitize_email($user_email)
270 ),
271 array('ID' => $userid),
272 array('%s','%s','%s'),
273 array('%d')
274 );
275 $this->wpforo->db->update(
276 $this->wpforo->db->base_prefix."usermeta",
277 array('meta_value' => sanitize_title($user_nickname)),
278 array('user_id' => $userid, 'meta_key' => 'nickname'),
279 array('%s'),
280 array('%d', '%s')
281 );
282 $this->reset($userid);
283 }
284
285 if( FALSE !== $this->edit_profile($args) ){
286 $this->wpforo->notice->add('Your profile data have been successfully updated.', 'success');
287 return $userid;
288 }
289 }
290
291 $this->wpforo->notice->add('Something wrong with profile data.', 'error');
292 return FALSE;
293 }
294
295 function upload_avatar(){
296 if( !isset($_POST['member']['userid']) || !$userid = intval($_POST['member']['userid']) ) return;
297 if( !$user = $this->get_member($userid) ) return;
298 $username = urldecode($user['user_nicename']);
299 if(isset($_FILES['avatar']) && !empty($_FILES['avatar']) && isset($_FILES['avatar']['name']) && $_FILES['avatar']['name']){
300
301 $name = sanitize_file_name($_FILES['avatar']['name']); //myimg.png
302 $type = sanitize_mime_type($_FILES['avatar']['type']); //image/png
303 $tmp_name = sanitize_text_field($_FILES['avatar']['tmp_name']); //D:\wamp\tmp\php986B.tmp
304 $error = sanitize_text_field($_FILES['avatar']['error']); //0
305 $size = intval($_FILES['avatar']['size']); //6112
306
307 if( $size > 2*1048576 ){
308 $this->wpforo->notice->clear();
309 $this->wpforo->notice->add('Avatar image is too big maximum allowed size is 2MB', 'error');
310 return FALSE;
311 }
312
313 if( $error ){
314 $error = wpforo_file_upload_error($error);
315 $this->wpforo->notice->clear();
316 $this->wpforo->notice->add($error, 'error');
317 return FALSE;
318 }
319
320 $upload_dir = wp_upload_dir();
321 $uplds_dir = $upload_dir['basedir']."/wpforo";
322 $avatar_dir = $upload_dir['basedir']."/wpforo/avatars";
323 if(!is_dir($uplds_dir)) wp_mkdir_p($uplds_dir);
324 if(!is_dir($avatar_dir)) wp_mkdir_p($avatar_dir);
325
326 $ext = pathinfo($name, PATHINFO_EXTENSION);
327 if( !wpforo_is_image($ext) ){
328 $this->wpforo->notice->clear();
329 $this->wpforo->notice->add('Incorrect file format. Allowed formats: jpeg, jpg, png, gif.', 'error');
330 return FALSE;
331 }
332
333 $fnm = pathinfo($username, PATHINFO_FILENAME);
334 $fnm = str_replace(' ', '-', $fnm);
335 while(strpos($fnm, '--') !== FALSE) $fnm = str_replace('--', '-', $fnm);
336 $fnm = preg_replace("/[^-a-zA-Z0-9]/", "", $fnm);
337 $fnm = trim($fnm, "-");
338
339 $avatar_fname = $fnm.( $fnm ? '_' : '' ).$userid.".".$ext;
340 $avatar_path = $avatar_dir."/".$avatar_fname;
341
342 if(is_dir($avatar_dir)){
343 if(move_uploaded_file($tmp_name, $avatar_path)) {
344 $image = wp_get_image_editor( $avatar_path );
345 if ( ! is_wp_error( $image ) ) {
346 $image->resize( 150, 150, true );
347 $image->save( $avatar_path );
348 }
349 $blog_url = preg_replace('#^https?\:#is', '', $upload_dir['baseurl']);
350 $this->wpforo->db->update($this->wpforo->db->prefix.'wpforo_profiles', array('avatar' => $blog_url . "/wpforo/avatars/" . $avatar_fname), array('userid' => intval($userid)), array('%s'), array('%d'));
351 $this->reset($userid);
352 }
353 }
354 }
355 }
356
357 function synchronize_user($userid){
358 if(!$userid) return FALSE;
359 if( $user = get_userdata($userid) ){
360 if( is_super_admin( $userid ) || in_array('administrator', $user->roles) ){
361 $groupid = 1;
362 }elseif( in_array('editor', $user->roles) ){
363 $groupid = 2;
364 }elseif( in_array('customer', $user->roles) ){
365 $groupid = 5;
366 }else{
367 $groupid = $this->wpforo->default_groupid;
368 }
369 $insert_groupid = (isset($_POST['wpforo_usergroup'])) ? intval($_POST['wpforo_usergroup']) : $groupid;
370 $insert_timezone = (isset($_POST['wpforo_usertimezone'])) ? sanitize_text_field($_POST['wpforo_usertimezone']) : '';
371 $about = get_user_meta( $userid, 'description', true );
372 return $this->add_profile(
373 array( 'userid' => intval($userid),
374 'username' => sanitize_user($user->user_login),
375 'groupid' => intval($insert_groupid),
376 'site' => esc_url($user->user_url),
377 'timezone' => sanitize_text_field($insert_timezone),
378 'about' => stripslashes( wpforo_kses(trim($about), 'user_description') ),
379 'last_login' => sanitize_text_field($user->user_registered) ) );
380 }
381 return FALSE;
382 }
383
384 function synchronize_users(){
385 $sql = "SELECT `ID` FROM `".$this->wpforo->db->base_prefix."users` WHERE `ID` NOT IN( SELECT `userid` FROM `".$this->wpforo->db->prefix."wpforo_profiles` )";
386 $userids = $this->wpforo->db->get_col($sql);
387 if( !empty($userids) ){
388 foreach($userids as $userid){
389 $this->synchronize_user($userid);
390 }
391 }
392 }
393
394 function get_member($args = array(), $cache = false){
395
396 if(is_array($args)){
397
398 $default = array(
399 'userid' => NULL, // userid
400 'username' => '' // username
401 );
402
403 }else{
404
405 $default = array(
406 'userid' => $args, // userid
407 'username' => '' // username
408 );
409
410 }
411
412 $args = wpforo_parse_args( $args, $default );
413
414 if(isset($args['userid'])){
415 if( $cache && isset(self::$cache['user'][$args['userid']]) ){
416 return self::$cache['user'][$args['userid']];
417 }
418 }
419
420 $user_meta_obj = true;
421
422 if(!empty($args)){
423
424 extract($args, EXTR_OVERWRITE);
425
426 $do_db_cache = wpforo_feature('member_cashe', $this->wpforo);
427
428 $userid = intval($userid);
429 $username = sanitize_user($username);
430
431 if( $do_db_cache ){
432 if( $username ){
433 $user_obj = get_user_by( 'user_nicename', $username );
434 if(!empty($user_obj)) $userid = $user_obj->ID;
435 $member = get_user_meta( $userid, '_wpf_member_obj', true );
436 }elseif( $userid ){
437 $member = get_user_meta( $userid, '_wpf_member_obj', true );
438 }else{
439 $user_meta_obj = true;
440 }
441 }else{
442 $member = array();
443 }
444
445 if(empty($member)){
446 $user_meta_obj = false;
447 $sql = "SELECT *, ug.name AS groupname FROM `".$this->wpforo->db->base_prefix."users` u
448 LEFT JOIN `".$this->wpforo->db->prefix."wpforo_profiles` p ON p.`userid` = u.`ID`
449 LEFT JOIN `".$this->wpforo->db->prefix."wpforo_usergroups` ug ON ug.`groupid` = p.`groupid`";
450 $wheres = array();
451 if($userid) $wheres[] = "`ID` = " . intval($userid);
452 if($username) $wheres[] = "`user_nicename` = '" . esc_sql($username) . "'";
453 if( !empty($wheres) ) $sql .= " WHERE " . implode($wheres, " AND ");
454 $member = $this->wpforo->db->get_row($sql, ARRAY_A);
455 }
456
457 if(!empty($member)) {
458 if( $do_db_cache ){
459 if(!$user_meta_obj) {
460 $member['profile_url'] = $this->profile_url( $member );
461 $member['stat'] = $this->get_stat( $member, false, true );
462 update_user_meta( $userid, '_wpf_member_obj', $member );
463 }
464 }else{
465 $member['profile_url'] = $this->profile_url( $member );
466 $member['stat'] = $this->get_stat( $member, false, true );
467 }
468 }
469
470 if($cache && isset($userid)){
471 return self::$cache['user'][$userid] = $member;
472 }else{
473 return $member;
474 }
475 }
476 }
477
478 function get_members($args = array(), &$items_count = 0){
479
480 $default = array(
481 'include' => array(), // array( 2, 10, 25 )
482 'exclude' => array(), // array( 2, 10, 25 )
483 'status' => array('active', 'banned'), // 'active', 'blocked', 'trashed', 'spamer'
484 'groupid' => NULL, // groupid
485 'orderby' => 'userid', //
486 'order' => 'ASC', // ASC DESC
487 'offset' => 0, // OFFSET
488 'row_count' => NULL // ROW COUNT
489 );
490
491 $args = wpforo_parse_args( $args, $default );
492 if(!empty($args)){
493 extract($args, EXTR_OVERWRITE);
494
495 $include = wpforo_parse_args( $include );
496 $exclude = wpforo_parse_args( $exclude );
497
498 $sql = "SELECT *, ug.name AS groupname FROM `".$this->wpforo->db->base_prefix."users` u
499 LEFT JOIN `".$this->wpforo->db->prefix."wpforo_profiles` p ON p.`userid` = u.`ID`
500 LEFT JOIN `".$this->wpforo->db->prefix."wpforo_usergroups` ug ON ug.`groupid` = p.`groupid`";
501 $wheres = array();
502 if(!empty($include)) $wheres[] = "u.`ID` IN(" . implode(', ', array_map('intval', $include)) . ")";
503 if(!empty($exclude)) $wheres[] = "u.`ID` NOT IN(" . implode(', ', array_map('intval', $exclude)) . ")";
504 if(!empty($status)) $wheres[] = " p.`status` IN('" . implode("','", array_map('esc_sql', array_map('sanitize_text_field', $status)) ) . "')";
505 if($groupid != NULL) $wheres[] = "p.`groupid` = " . intval($groupid);
506
507 if(!empty($wheres)) $sql .= " WHERE " . implode($wheres, " AND ");
508
509 $item_count_sql = preg_replace('#SELECT.+?FROM#isu', 'SELECT count(*) FROM', $sql);
510 if( $item_count_sql ) $items_count = $this->wpforo->db->get_var($item_count_sql);
511
512 if( $orderby == 'groupid' ) $orderby = 'p.`groupid`';
513 $sql .= esc_sql(" ORDER BY $orderby " . $order);
514 if($row_count) $sql .= esc_sql(" LIMIT $offset,$row_count");
515
516 return $this->wpforo->db->get_results($sql, ARRAY_A);
517 }
518 }
519
520 function search($needle, $fields = array(), $limit = NULL){
521
522 if($needle != ''){
523 $needle = sanitize_text_field($needle);
524 if(empty($fields)){
525 $fields = array(
526 'title',
527 'user_nicename',
528 'user_email',
529 'signature'
530 );
531 }
532
533 $sql = "SELECT `ID` FROM `".$this->wpforo->db->base_prefix."users` u LEFT JOIN `".$this->wpforo->db->prefix."wpforo_profiles` p ON p.`userid` = u.`ID`";
534 $wheres = array();
535
536 foreach($fields as $field){
537 $field = sanitize_text_field($field);
538 $wheres[] = "`".esc_sql($field)."` LIKE '%" . esc_sql($needle) ."%'";
539 }
540
541 if(!empty($wheres)){
542 $sql .= " WHERE " . implode($wheres, " OR ");
543 if( $limit ) $sql .= " LIMIT " . intval($limit);
544
545 return $this->wpforo->db->get_col($sql);
546 }else{
547 return array();
548 }
549 }else{
550 return array();
551 }
552
553 }
554
555 function ban($userid){
556 if( $userid == $this->wpforo->current_userid ){
557 $this->wpforo->notice->add('You can\'t make yourself banned user', 'error');
558 return FALSE;
559 }
560 if( !$this->wpforo->perm->usergroup_can('bm') || !$this->wpforo->perm->user_can_manage_user( $this->wpforo->current_userid, intval( $userid ) )){
561 $this->wpforo->notice->add('Permission denied for this action', 'error');
562 return FALSE;
563 }
564 if( FALSE !== $this->wpforo->db->update(
565 $this->wpforo->db->prefix.'wpforo_profiles',
566 array('status' => 'banned'),
567 array('userid' => intval( $userid )),
568 array('%s'),
569 array('%d')
570 )
571 ){
572 $this->reset($userid);
573 $this->wpforo->notice->add('User successfully banned from wpforo', 'success');
574 return TRUE;
575 }
576
577 $this->wpforo->notice->add('User ban action error', 'error');
578 return FALSE;
579 }
580
581 function unban($userid){
582 if( !$this->wpforo->perm->usergroup_can('bm') || !$this->wpforo->perm->user_can_manage_user( $this->wpforo->current_userid, intval( $userid ) )){
583 $this->wpforo->notice->add('Permission denied for this action', 'error');
584 return FALSE;
585 }
586 if( FALSE !== $this->wpforo->db->update(
587 $this->wpforo->db->prefix.'wpforo_profiles',
588 array('status' => 'active'),
589 array('userid' => intval( $userid )),
590 array('%s'),
591 array('%d')
592 )
593 ){
594 $this->reset($userid);
595 $this->wpforo->notice->add('User successfully unbanned from wpforo', 'success');
596 return TRUE;
597 }
598
599 $this->wpforo->notice->add('User unban action error', 'error');
600 return FALSE;
601 }
602
603 /**
604 *
605 * @param int $userid
606 * @param int $reassign
607 *
608 * @return bool true | false if user successfully deleted
609 */
610 public function delete( $userid, $reassign = NULL ){
611 if( !($userid = intval($userid)) ) return FALSE;
612 if( !$this->wpforo->perm->usergroup_can('dm') || !$this->wpforo->perm->user_can_manage_user( $this->wpforo->current_userid, intval( $userid ) )){
613 $this->wpforo->notice->add('Permission denied for this action', 'error');
614 return FALSE;
615 }
616
617 if( !($reassign = intval($reassign)) ){
618 if( $postids = $this->wpforo->db->get_col( $this->wpforo->db->prepare( "SELECT postid FROM {$this->wpforo->db->prefix}wpforo_posts WHERE userid = %d", $userid ) ) ){
619 foreach( $postids as $postid ) $this->wpforo->post->delete($postid);
620 }
621
622 if( $topicids = $this->wpforo->db->get_col( $this->wpforo->db->prepare( "SELECT topicid FROM {$this->wpforo->db->prefix}wpforo_topics WHERE userid = %d", $userid ) ) ){
623 foreach( $topicids as $topicid ) $this->wpforo->topic->delete($topicid);
624 }
625 }else{
626 $this->wpforo->db->update( $this->wpforo->db->prefix."wpforo_topics", array('userid' => $reassign), array('userid' => $userid) );
627 $this->wpforo->db->update( $this->wpforo->db->prefix."wpforo_posts", array('userid' => $reassign), array('userid' => $userid) );
628 $this->wpforo->db->update( $this->wpforo->db->prefix."wpforo_likes", array('post_userid' => $reassign), array('post_userid' => $userid) );
629 $this->wpforo->db->update( $this->wpforo->db->prefix."wpforo_votes", array('post_userid' => $reassign), array('post_userid' => $userid) );
630 if( $user_stats = $this->wpforo->db->get_row(
631 $this->wpforo->db->prepare( "SELECT
632 SUM(`posts`) AS posts,
633 SUM(`questions`) AS questions,
634 SUM(`answers`) AS answers,
635 SUM(`comments`) AS comments
636 FROM {$this->wpforo->db->prefix}wpforo_profiles
637 WHERE userid IN( %d , %d )", $userid, $reassign
638 ),
639 ARRAY_A
640 )
641 ){
642 $this->wpforo->db->update(
643 $this->wpforo->db->prefix.'wpforo_profiles',
644 array(
645 'posts' => $user_stats['posts'],
646 'questions' => $user_stats['questions'],
647 'answers' => $user_stats['answers'],
648 'comments' => $user_stats['comments']
649 ),
650 array('userid' => $reassign),
651 array('%d','%d','%d','%d'),
652 array('%d')
653 );
654 }
655 }
656
657 $this->wpforo->db->delete(
658 $this->wpforo->db->prefix.'wpforo_subscribes', array( 'userid' => $userid ), array( '%d' )
659 );
660
661 $this->wpforo->db->delete(
662 $this->wpforo->db->prefix.'wpforo_views', array( 'userid' => $userid ), array( '%d' )
663 );
664
665 $this->wpforo->db->delete(
666 $this->wpforo->db->prefix.'wpforo_likes', array( 'userid' => $userid ), array( '%d' )
667 );
668
669 $this->wpforo->db->delete(
670 $this->wpforo->db->prefix.'wpforo_votes', array( 'userid' => $userid ), array( '%d' )
671 );
672
673 if( FALSE !== $this->wpforo->db->delete(
674 $this->wpforo->db->prefix.'wpforo_profiles', array( 'userid' => $userid ), array( '%d' )
675 )
676 ){
677 $this->clear_db_cache();
678 $this->wpforo->notice->add('User successfully deleted from wpforo', 'success');
679 return TRUE;
680 }
681
682 $this->wpforo->notice->add('User delete error', 'error');
683 return FALSE;
684 }
685
686 public function avatar($member, $attr = '', $size = '', $cache = false){
687
688 if(!isset($member['userid'])) return;
689
690 $src = $member['avatar'];
691 $userid = $member['userid'];
692 if(isset(self::$cache['avatar'][$userid])){
693 if(self::$cache['avatar'][$userid]['attr'] == $attr && self::$cache['avatar'][$userid]['size'] == $size){
694 if(isset(self::$cache['avatar'][$userid]['img'])){
695 return self::$cache['avatar'][$userid]['img'];
696 }
697 }
698 }
699 if($src && wpforo_feature('custom-avatars', $this->wpforo)){
700 $attr = ($attr ? $attr : 'height="96" width="96"');
701 $img = '<img class="avatar" src="'.esc_url($src).'" '. $attr .' />';
702 }else{
703 $img = ($size) ? get_avatar($userid, $size) : get_avatar($userid);
704 if($attr) $img = str_replace('<img', '<img ' . $attr, $img);
705 }
706 if($cache){
707 self::$cache['avatar'][$userid]['attr'] = $attr;
708 self::$cache['avatar'][$userid]['size'] = $size;
709 return self::$cache['avatar'][$userid]['img'] = $img;
710 }
711 else{
712 return $img;
713 }
714 }
715
716 function get_avatar($userid, $attr = '', $size = '', $cache = false){
717 if(isset(self::$cache['avatar'][$userid])){
718 if(self::$cache['avatar'][$userid]['attr'] == $attr && self::$cache['avatar'][$userid]['size'] == $size){
719 if(isset(self::$cache['avatar'][$userid]['img'])){
720 return self::$cache['avatar'][$userid]['img'];
721 }
722 }
723 }
724 $src = $this->wpforo->db->get_var("SELECT `avatar` FROM `".$this->wpforo->db->prefix."wpforo_profiles` WHERE `userid` = ".intval($userid));
725 if($src && wpforo_feature('custom-avatars', $this->wpforo)){
726 $attr = ($attr ? $attr : 'height="96" width="96"');
727 $img = '<img class="avatar" src="'.esc_url($src).'" '. $attr .' />';
728 }else{
729 $img = ($size) ? get_avatar($userid, $size) : get_avatar($userid);
730 if($attr) $img = str_replace('<img', '<img ' . $attr, $img);
731 }
732 if($cache){
733 self::$cache['avatar'][$userid]['attr'] = $attr;
734 self::$cache['avatar'][$userid]['size'] = $size;
735 return self::$cache['avatar'][$userid]['img'] = $img;
736 }
737 else{
738 return $img;
739 }
740 }
741
742 public function get_avatar_url($userid){
743 return $this->wpforo->db->get_var("SELECT `avatar` FROM `".$this->wpforo->db->prefix."wpforo_profiles` WHERE `userid` = ".intval($userid));
744 }
745
746 function get_topics_count( $userid ){
747 $count = $this->wpforo->db->get_var("SELECT count(topicid) FROM `".$this->wpforo->db->prefix."wpforo_topics` WHERE `userid` = ".intval($userid));
748 return $count;
749 }
750
751 function get_questions_count( $userid ){
752 $count = $this->wpforo->db->get_var("SELECT count(topicid) FROM `".$this->wpforo->db->prefix."wpforo_topics` WHERE `userid` = ".intval($userid));
753 return $count;
754 }
755
756 function get_answers_count( $userid ){
757 $count = $this->wpforo->db->get_var("SELECT count(postid) FROM `".$this->wpforo->db->prefix."wpforo_posts` WHERE `is_answer` = 1 AND `userid` = ".intval($userid));
758 return $count;
759 }
760
761 function get_question_comments_count( $userid ){
762 $count = $this->wpforo->db->get_var("SELECT count(postid) FROM `".$this->wpforo->db->prefix."wpforo_posts` WHERE `parentid` > 0 AND `userid` = ".intval($userid));
763 return $count;
764 }
765
766 function get_replies_count( $userid ){
767 $count = $this->wpforo->db->get_var("SELECT count(postid) FROM `".$this->wpforo->db->prefix."wpforo_posts` WHERE `userid` = ".intval($userid));
768 return $count;
769 }
770
771 function get_likes_count( $userid ){
772 $count = $this->wpforo->db->get_var("SELECT count(likeid) FROM `".$this->wpforo->db->prefix."wpforo_likes` WHERE `userid` = ".intval($userid));
773 return $count;
774 }
775
776 function get_votes_count( $userid ){
777 $count = $this->wpforo->db->get_var("SELECT count(voteid) FROM `".$this->wpforo->db->prefix."wpforo_votes` WHERE `userid` = ".intval($userid));
778 return $count;
779 }
780
781 // how many times the user like or vote
782 function get_votes_and_likes_count( $userid ){
783 return $this->get_votes_count( intval($userid) ) + $this->get_likes_count( intval($userid) );
784 }
785
786 //getting user's posts votes and likes count
787 function get_user_votes_and_likes_count( $userid ){
788 $votes_count = $this->wpforo->db->get_var("SELECT count(voteid) FROM `".$this->wpforo->db->prefix."wpforo_votes` WHERE `post_userid` = ".intval($userid));
789 $likes_count = $this->wpforo->db->get_var("SELECT count(likeid) FROM `".$this->wpforo->db->prefix."wpforo_likes` WHERE `post_userid` = ".intval($userid));
790 return $votes_count + $likes_count;
791 }
792
793 function get_profile_url( $arg, $template = 'profile' ){
794 if(!$arg) return WPFORO_BASE_URL;
795 $userid = intval( basename($arg) );
796 $member_args = ( $userid ? $userid : array( 'username' => basename($arg) ) );
797 $user = $this->get_member( $member_args );
798 if(empty($user)) return WPFORO_BASE_URL;
799 $user_slug = ( wpfo($this->wpforo->member_options['url_structure'], false) == 'id' ? $user['ID'] : $user['user_nicename'] );
800 return WPFORO_BASE_URL . "$template/$user_slug";
801 }
802
803 function profile_url( $member = array(), $template = 'profile' ){
804 if(isset($member['ID']) || isset($member['user_nicename'])){
805 $user_slug = ( wpfo($this->wpforo->member_options['url_structure'], false) == 'id' ? $member['ID'] : $member['user_nicename'] );
806 $profile_url = WPFORO_BASE_URL . "$template/$user_slug";
807 $profile_url = apply_filters( 'wpforo_profile_url', $profile_url, $member, $template );
808 }
809 else{
810 $profile_url = WPFORO_BASE_URL;
811 $profile_url = apply_filters( 'wpforo_no_profile_url', $profile_url, $template );
812
813 }
814 return $profile_url;
815 }
816
817 //$args = UserID or Member Object
818 //$live_count = TRUE / FALSE
819 function get_stat( $args = array(), $live_count = false, $cache = false ){
820
821 $stat = array( 'points' => 0,
822 'rating' => 0,
823 'rating_procent' => 0,
824 'color' => $this->rating(0, 'color'),
825 'badge' => $this->rating(0, 'icon'),
826 'posts' => 0,
827 'topics' => 0,
828 'questions' => 0,
829 'answers' => 0,
830 'question_comments' => 0,
831 'likes' => 0,
832 'liked' => 0,
833 'title' => $this->rating(0, 'title'));
834
835 $userid = ( isset($args['userid']) && $args['userid'] ) ? $args['userid'] : $args;
836
837 if( $cache && isset(self::$cache['stat'][$userid]) ){
838 return self::$cache['stat'][$userid];
839 }
840
841 if( is_array($args) && isset($args['userid']) ){
842 $userid = $args['userid'];
843 $stat['topics'] = (int)$this->get_topics_count( $userid );
844 if(isset($args['questions'])) $stat['questions'] = intval($args['questions']);
845 if(isset($args['answers'])) $stat['answers'] = intval($args['answers']);
846 if(isset($args['posts'])) $stat['posts'] = intval($args['posts']);
847 if(isset($args['comments'])) $stat['question_comments'] = intval($args['comments']);
848 }
849 elseif($userid = wpforo_bigintval($args)){
850 $stat['topics'] = (int)$this->get_topics_count( $userid );
851 if($live_count){
852 if($questions = $this->get_questions_count( $userid )) $stat['questions'] = $questions;
853 if($answers = $this->get_answers_count( $userid )) $stat['answers'] = $answers;
854 if($posts = $this->get_replies_count( $userid )) $stat['posts'] = $posts;
855 if($question_comments = $this->get_question_comments_count( $userid )) $stat['question_comments'] = $question_comments;
856 }
857 else{
858 $profile = $this->wpforo->db->get_var("SELECT `posts`, `questions`, `answers`, `comments` FROM `".$this->wpforo->db->prefix."wpforo_profiles` WHERE `userid` = ".intval($userid));
859 if(isset($profile['questions'])) $stat['questions'] = intval($profile['questions']);
860 if(isset($profile['answers'])) $stat['answers'] = intval($profile['answers']);
861 if(isset($profile['posts'])) $stat['posts'] = intval($profile['posts']);
862 if(isset($profile['comments'])) $stat['question_comments'] = intval($profile['comments']);
863 }
864 }
865
866 if( $userid ){
867 if($likes = $this->get_votes_and_likes_count( $userid )) $stat['likes'] = $likes;
868 if($liked = $this->get_user_votes_and_likes_count( $userid )) $stat['liked'] = $liked;
869 if($stat['posts']) $stat['points'] = $stat['posts']; //TO-DO: Point counter function based on all stat values.
870 if($stat['points']) $stat['rating'] = $this->rating_level($stat['points'], false);
871 if($stat['rating']) {
872 $stat['rating_procent'] = $stat['rating'] * 10;
873 $stat['title'] = $this->rating(intval($stat['rating']), 'title');
874 $stat['color'] = $this->rating(intval($stat['rating']), 'color');
875 $stat['badge'] = $this->rating(intval($stat['rating']), 'icon');
876 }
877 }
878
879 if($cache && isset($userid)){
880 return self::$cache['stat'][$userid] = $stat;
881 }
882 else{
883 return $stat;
884 }
885 }
886
887 function get_count(){
888 return $this->wpforo->db->get_var( "SELECT COUNT(p.`userid`) FROM `".$this->wpforo->db->prefix."wpforo_profiles` p
889 INNER JOIN `".$this->wpforo->db->base_prefix."users` u ON u.`ID` = p.`userid` WHERE p.`status` NOT LIKE 'trashed'" );
890 }
891
892
893 function is_online( $userid, $duration = 240, $cache = true ){
894 if(isset(self::$cache['online'][$userid])){
895 if(self::$cache['online'][$userid]['durration'] == $duration ){
896 if(isset(self::$cache['online'][$userid]['status'])){
897 return self::$cache['online'][$userid]['status'];
898 }
899 }
900 }
901 if($duration == 240) $duration = $this->wpforo->member_options['online_status_timeout'];
902 $online_time = intval( get_user_meta($userid, 'wpforo_online_time', TRUE) );
903 $current_time = current_time( 'timestamp', 1 );
904 $online_duration = $current_time - $online_time;
905 if( $online_duration < $duration ) {
906 $status = true;
907 }
908 else{
909 $status = false;
910 }
911 if($cache){
912 self::$cache['online'][$userid]['durration'] = $duration;
913 return self::$cache['online'][$userid]['status'] = $status;
914 }
915 else{
916 return $status;
917 }
918 }
919
920 public function show_online_indicator($userid, $ico = TRUE){
921 if( $this->is_online($userid)) : ?>
922
923 <?php if($ico) : ?>
924 <i class="fa fa-lightbulb-o fa-0x wpfcl-8" title="<?php wpforo_phrase('Online') ?>"></i>
925 <?php else : wpforo_phrase('Online'); endif ?>
926
927 <?php else : ?>
928
929 <?php if($ico) : ?>
930 <i class="fa fa-lightbulb-o fa-0x wpfcl-0" title="<?php wpforo_phrase('Offline') ?>"></i>
931 <?php else : wpforo_phrase('Offline'); endif ?>
932
933 <?php endif;
934 }
935
936 function online_members_count( $duration = 240 ){
937 if($duration == 240) $duration = $this->wpforo->member_options['online_status_timeout'];
938 $current_time = current_time( 'timestamp', 1 );
939 $online_timeframe = $current_time - $duration;
940 return $this->wpforo->db->get_var( "SELECT COUNT(`user_id`) FROM `".$this->wpforo->db->base_prefix."usermeta` WHERE meta_key = 'wpforo_online_time' AND meta_value > " . wpforo_bigintval($online_timeframe) );
941
942 }
943
944 function get_online_members( $count = 1, $duration = 240 ){
945 if($duration == 240) $duration = $this->wpforo->member_options['online_status_timeout'];
946 $current_time = current_time( 'timestamp', 1 );
947 $online_timeframe = $current_time - $duration;
948 $onlinemembers_ids = $this->wpforo->db->get_col( "SELECT `user_id` FROM `".$this->wpforo->db->base_prefix."usermeta` WHERE meta_key = 'wpforo_online_time' AND meta_value > " . wpforo_bigintval($online_timeframe) );
949 if(!empty($onlinemembers_ids)){
950 $args = array(
951 'include' => $onlinemembers_ids, // array( 2, 10, 25 )
952 'orderby' => 'userid', // forumid, order, parentid
953 'row_count' => $count,
954 'order' => 'ASC', // ASC DESC
955 );
956 return $this->get_members( $args );
957 }
958 else{
959 return array();
960 }
961 }
962
963 function levels(){
964 $levels = array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
965 return $levels;
966 }
967
968 function rating( $level = false, $var = false, $default = false ){
969
970 $rating = array();
971 $rating['color'] = array( 0 => '#d2d2d2', 1 => '#4dca5c', 2 => '#4dca5c', 3 => '#4dca5c', 4 => '#4dca5c', 5 => '#4dca5c', 6 => '#E5D600', 7 => '#E5D600', 8 => '#E5D600', 9 => '#FF812D', 10 => '#E04A47' );
972 $rating['points'] = array( 0 => 0, 1 => 5, 2 => 20, 3 => 50, 4 => 100, 5 => 250, 6 => 500, 7 => 750, 8 => 1000, 9 => 2500, 10 => 5000 );
973 $rating['title'] = array( 0 => 'New Member', 1 => 'Active Member', 2 => 'Eminent Member', 3 => 'Trusted Member', 4 => 'Estimable Member', 5 => 'Reputable Member', 6 => 'Honorable Member', 7 => 'Prominent Member', 8 => 'Noble Member', 9 => 'Famed Member', 10 => 'Illustrious Member' );
974 $rating['icon'] = array( 0 => 'fa-star-half-o', 1 => 'fa-star', 2 => 'fa-star', 3 => 'fa-star', 4 => 'fa-star', 5 => 'fa-star', 6 => 'fa-certificate', 7 => 'fa-certificate', 8 => 'fa-certificate', 9 => 'fa-shield', 10 => 'fa-trophy' );
975
976 if(!empty($this->wpforo->member_options['rating'])){
977
978 if($level === false) return $this->wpforo->member_options['rating'];
979 if(!empty($this->wpforo->member_options['rating'][$level])){
980
981 if(!$var) return $this->wpforo->member_options['rating'][$level];
982 if(!empty($this->wpforo->member_options['rating'][$level][$var])){
983
984 return $this->wpforo->member_options['rating'][$level][$var];
985
986 }
987 }
988 }
989 if( $level !== false && $var ) { return $rating[$var][$level]; }
990 elseif( $level !== false && !$var ){ foreach( $rating as $variable => $values ){ $level_data[$variable] = $values[$level];} return $level_data; }
991 elseif( $level === false && !$var ) return $rating;
992 else return array();
993 }
994
995 function rating_level($member_posts, $percent = TRUE){
996 $bar = 0;
997 if($member_posts < $this->rating(1, 'points')){$bar = 0;}
998 elseif($member_posts < $this->rating(2, 'points')){$bar = 10;}
999 elseif($member_posts < $this->rating(3, 'points')){$bar = 20;}
1000 elseif($member_posts < $this->rating(4, 'points')){$bar = 30;}
1001 elseif($member_posts < $this->rating(5, 'points')){$bar = 40;}
1002 elseif($member_posts < $this->rating(6, 'points')){$bar = 50;}
1003 elseif($member_posts < $this->rating(7, 'points')){$bar = 60;}
1004 elseif($member_posts < $this->rating(8, 'points')){$bar = 70;}
1005 elseif($member_posts < $this->rating(9, 'points')){$bar = 80;}
1006 elseif($member_posts < $this->rating(10, 'points')){$bar = 90;}
1007 else{$bar = 100;}
1008 if($percent){
1009 return $bar;
1010 }else{
1011 return floor($bar/10);
1012 }
1013 }
1014
1015 function rating_badge($level = 0, $view = 'short'){
1016
1017 $level = ( $level > 10 ) ? floor($level/10) : $level;
1018
1019 if($level == 0){
1020 return '<i class="fa '. sanitize_html_class($this->rating($level, 'icon')) .'"></i>';
1021 }
1022 elseif($level > 0 && $level < 6){
1023 if( $view == 'full' ){
1024 return str_repeat(' <i class="fa '. sanitize_html_class($this->rating($level, 'icon')) .'"></i> ', $level);
1025 }
1026 else{
1027 return '<span>' . esc_html($level) . '</span> <i class="fa '. sanitize_html_class($this->rating($level, 'icon')) .'"></i>';
1028 }
1029 }
1030 elseif($level > 5 && $level < 9){
1031 if( $view == 'full' ){
1032 return str_repeat(' <i class="fa '. sanitize_html_class($this->rating($level, 'icon')) .'"></i> ', ($level-5));
1033 }
1034 else{
1035 return '<span>' . esc_html($level-5) . '</span> <i class="fa '. sanitize_html_class($this->rating($level, 'icon')) .'"></i>';
1036 }
1037 }
1038 elseif($level > 8){
1039 return '<i class="fa '. sanitize_html_class($this->rating($level, 'icon')) .'"></i>';
1040 }
1041 else{
1042 return;
1043 }
1044 }
1045
1046 public function reset($userid){
1047 if( !$userid ) return;
1048 $this->wpforo->db->query( "DELETE FROM `" . $this->wpforo->db->base_prefix ."usermeta` WHERE `meta_key` = '_wpf_member_obj' AND `user_id` = " . intval($userid) );
1049 }
1050
1051 public function clear_db_cache(){
1052 $this->wpforo->db->query( "DELETE FROM `" . $this->wpforo->db->base_prefix ."usermeta` WHERE `meta_key` = '_wpf_member_obj'" );
1053 }
1054
1055 public function init_current_user(){
1056 $current_user = wp_get_current_user();
1057 if( $current_user->exists() ){
1058 $user = $this->get_member( $current_user->ID );
1059 $status = ( isset($user['status']) ? $user['status'] : '' );
1060 if( $status == 'active' ){
1061 update_user_meta( $current_user->ID, 'wpforo_online_time', current_time( 'timestamp', 1 ) );
1062 $this->wpforo->current_user = $user;
1063 $this->wpforo->current_user_groupid = $this->wpforo->current_user['groupid'];
1064 $this->wpforo->current_userid = $current_user->ID;
1065 $this->wpforo->current_username = $current_user->user_login;
1066 $this->wpforo->current_user_email = $current_user->user_email;
1067 $this->wpforo->current_user_display_name = $current_user->display_name;
1068 }else{
1069 $this->wpforo->current_user = array();
1070 $this->wpforo->current_user_groupid = 4;
1071 $this->wpforo->current_userid = 0;
1072 $this->wpforo->current_username = '';
1073 $this->wpforo->current_user_email = '';
1074 $this->wpforo->current_user_display_name = '';
1075 }
1076 $this->wpforo->current_user_status = $status;
1077 }else{
1078 $this->wpforo->current_user = array();
1079 $this->wpforo->current_user_groupid = 4;
1080 $this->wpforo->current_userid = 0;
1081 $this->wpforo->current_username = '';
1082 $this->wpforo->current_user_email = '';
1083 $this->wpforo->current_user_display_name = '';
1084 $this->wpforo->current_user_status = '';
1085 }
1086 }
1087
1088 public function blog_posts( $userid ){
1089 if( isset($userid) && $userid ) return count_user_posts( $userid , 'post' );
1090 }
1091
1092 public function blog_comments($userid, $user_email){
1093 global $wpdb;
1094 if( !$userid || !$user_email ) return 0;
1095 return (int) $wpdb->get_var("SELECT COUNT(*) FROM " . $wpdb->comments. " WHERE `user_id` = " . intval($userid) . " OR `comment_author_email` = '" . esc_sql($user_email) . "'");
1096 }
1097
1098 public function show_delete_form($current_user, $userids){
1099 if( empty($current_user) || empty($userids) ) return;
1100
1101 $userids = array_diff( $userids, array( $current_user->ID ) );
1102 $users_have_content = false;
1103 if ( $this->wpforo->db->get_var( "SELECT postid FROM {$this->wpforo->db->prefix}wpforo_posts WHERE userid IN( " . implode( ',', array_map('intval', $userids) ) . " ) LIMIT 1" ) ) {
1104 $users_have_content = true;
1105 }
1106 ?>
1107 <hr /><strong>#wpForo</strong>
1108 <?php if ( ! $users_have_content ) : ?>
1109 <input type="hidden" name="wpforo_user_delete_option" value="delete" />
1110 <?php else: ?>
1111 <?php if ( 1 == count($userids) ) : ?>
1112 <fieldset><p><legend><?php _e( 'What should be done with wpForo content owned by this user?', 'wpforo' ); ?></legend></p>
1113 <?php else : ?>
1114 <fieldset><p><legend><?php _e( 'What should be done with wpForo content owned by these users?', 'wpforo' ); ?></legend></p>
1115 <?php endif; ?>
1116 <ul style="list-style:none;">
1117 <li><label><input type="radio" id="wpforo_delete_option0" name="wpforo_user_delete_option" value="delete" />
1118 <?php _e('Delete all wpForo content.', 'wpforo'); ?></label></li>
1119 <li><input type="radio" id="wpforo_delete_option1" name="wpforo_user_delete_option" value="reassign" />
1120 <?php echo '<label for="wpforo_delete_option1">' . __( 'Attribute all content to:' ) . '</label> ';
1121 wp_dropdown_users( array(
1122 'name' => 'wpforo_reassign_user',
1123 'exclude' => $userids,
1124 'show' => 'display_name_with_login',
1125 ) ); ?></li>
1126 </ul></fieldset>
1127 <script type="text/javascript">
1128 jQuery(document).ready( function($) {
1129 $('#wpforo_reassign_user').focus( function() {
1130 $('#wpforo_delete_option1').prop('checked', true).trigger('change');
1131 });
1132 });
1133 </script>
1134 <?php endif;
1135 }
1136
1137
1138
1139 public function autoban($userid){
1140 if( !$this->wpforo->perm->usergroup_can( 'em' ) ){
1141 $this->wpforo->db->update(
1142 $this->wpforo->db->prefix.'wpforo_profiles',
1143 array('status' => 'banned'),
1144 array('userid' => intval( $userid )),
1145 array('%s'),
1146 array('%d')
1147 );
1148 }
1149 }
1150
1151 public function member_approved_posts( $member = array() ){
1152 if(is_numeric($member)){
1153 if( isset($this->wpforo->current_user['posts']) && $this->wpforo->current_user['posts'] && $member == $this->wpforo->current_userid ){
1154 return $this->wpforo->current_user['posts'];
1155 }
1156 else{
1157 return $this->wpforo->db->get_var( "SELECT COUNT(*) as posts FROM `".$this->wpforo->db->prefix."wpforo_posts` WHERE `status` = 0 AND `userid` = " . intval($member) );
1158 }
1159 }
1160 elseif(is_array($member) && !empty($member)){
1161 return intval($member['posts']);
1162 }
1163 else{
1164 return 0;
1165 }
1166 }
1167
1168 public function current_user_is_new(){
1169 if( $this->wpforo->perm->usergroup_can( 'em' ) ){
1170 //This is an admin or moderator. The number of posts doesn't matter.
1171 return false;
1172 }
1173 else{
1174 $posts = $this->member_approved_posts( $this->wpforo->current_userid );
1175 if ( $posts < $this->wpforo->tools_antispam['new_user_max_posts'] ) {
1176 return true;
1177 }
1178 }
1179 }
1180
1181 function banned_count(){
1182 $count = $this->wpforo->db->get_var("SELECT count(*) FROM `".$this->wpforo->db->prefix."wpforo_profiles` WHERE `status` = 'banned' " );
1183 return $count;
1184 }
1185
1186 }
1187
1188 ?>