PluginProbe
wpForo Forum / 1.0.0
wpForo Forum v1.0.0
3.1.6 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 All 138 releases
wpforo / wpf-includes / class-members.php

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

946 lines 34.5 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 }
14
15 private function add_profile($args){
16 if(empty($args)) return FALSE;
17 if(!isset($args['userid']) || !$args['userid'] || !isset($args['username']) || !$args['username'] ) return FALSE;
18 extract( $args, EXTR_OVERWRITE );
19 $this->reset($userid);
20 return $this->wpforo->db->insert(
21 $this->wpforo->db->prefix . 'wpforo_profiles',
22 array( 'userid' => intval($userid),
23 'username' => sanitize_user($username),
24 'groupid' => intval((isset($groupid) && $groupid ? $groupid : $this->wpforo->default_groupid)),
25 'site' => (isset($site) ? sanitize_text_field($site) : '' ),
26 'timezone' => ( isset($timezone) ? sanitize_text_field($timezone) : 'UTC+0' ),
27 'about' => ( isset($about) ? stripslashes( wpforo_kses(trim($about), 'user_description') ) : '' ),
28 'last_login' => ( isset($last_login) ? $last_login : current_time('mysql', 1) ) ),
29 array( '%d', '%s', '%d', '%s', '%s', '%s', '%s' )
30 );
31 }
32
33 function edit_profile($args){
34 if(empty($args)) return FALSE;
35 if( !isset($args['userid']) || !$args['userid'] ) return FALSE;
36 extract( $args, EXTR_OVERWRITE );
37
38 $fields = array();
39 $fields_types = array();
40
41 if(isset($last_login) && $last_login){
42 $fields['last_login'] = sanitize_text_field($last_login);
43 $fields_types[] = '%s';
44 }
45
46 if(isset($groupid) && $groupid){
47 if( $this->wpforo->current_user_groupid == 1 || current_user_can('administrator') ){
48 $fields['groupid'] = intval($groupid);
49 $fields_types[] = '%d';
50 }
51 }
52
53 if(isset($title) && $title){
54 $fields['title'] = sanitize_text_field(trim($title));
55 $fields_types[] = '%s';
56 }
57 if(isset($site)){
58 $fields['site'] = sanitize_text_field(trim($site));
59 $fields_types[] = '%s';
60 }
61 if(isset($icq)){
62 $fields['icq'] = sanitize_text_field(trim($icq));
63 $fields_types[] = '%s';
64 }
65 if(isset($aim)){
66 $fields['aim'] = sanitize_text_field(trim($aim));
67 $fields_types[] = '%s';
68 }
69 if(isset($yahoo)){
70 $fields['yahoo'] = sanitize_text_field(trim($yahoo));
71 $fields_types[] = '%s';
72 }
73 if(isset($msn)){
74 $fields['msn'] = sanitize_text_field(trim($msn));
75 $fields_types[] = '%s';
76 }
77 if(isset($facebook)){
78 $fields['facebook'] = sanitize_text_field(trim($facebook));
79 $fields_types[] = '%s';
80 }
81 if(isset($twitter)){
82 $fields['twitter'] = sanitize_text_field(trim($twitter));
83 $fields_types[] = '%s';
84 }
85 if(isset($gtalk)){
86 $fields['gtalk'] = sanitize_text_field(trim($gtalk));
87 $fields_types[] = '%s';
88 }
89 if(isset($skype)){
90 $fields['skype'] = sanitize_text_field(trim($skype));
91 $fields_types[] = '%s';
92 }
93 if(isset($signature)){
94 $fields['signature'] = stripslashes(wpforo_kses(trim($signature), 'user_description'));
95 $fields_types[] = '%s';
96 }
97 if(isset($about)){
98 $fields['about'] = stripslashes(wpforo_kses(trim($about), 'user_description'));
99 $fields_types[] = '%s';
100 }
101 if(isset($occupation)){
102 $fields['occupation'] = stripslashes(sanitize_text_field(trim($occupation)));
103 $fields_types[] = '%s';
104 }
105 if(isset($location)){
106 $fields['location'] = stripslashes(sanitize_text_field(trim($location)));
107 $fields_types[] = '%s';
108 }
109 if(isset($timezone)){
110 $fields['timezone'] = sanitize_text_field(trim($timezone));
111 $fields_types[] = '%s';
112 }
113 if(isset($avatar_type) && $avatar_type != 'gravatar' && isset($avatar_url) && $avatar_url){
114 $fields['avatar'] = esc_url(trim($avatar_url));
115 $fields_types[] = '%s';
116 }
117 if(isset($avatar_type) && $avatar_type == 'gravatar'){
118 $fields['avatar'] = '';
119 $fields_types[] = '%s';
120 }
121
122 $this->reset($userid);
123
124 return $this->wpforo->db->update(
125 $this->wpforo->db->prefix.'wpforo_profiles',
126 $fields,
127 array('userid' => intval($userid)),
128 $fields_types,
129 array('%d')
130 );
131 }
132
133 function create($args){
134 if(!wpforo_feature('user-register', $this->wpforo)){
135 $this->wpforo->notice->add('User registration is disabled.', 'error');
136 return FALSE;
137 }
138 if(!empty($args) && is_array($args)){
139 extract($args, EXTR_OVERWRITE);
140 $user_login = sanitize_user( $user_login );
141 $user_email = apply_filters( 'user_registration_email', sanitize_email($user_email) );
142 $user_pass1 = trim(substr($user_pass1, 0, 100));
143 $user_pass2 = trim(substr($user_pass2, 0, 100));
144 if ( $user_login == '' ) {
145 $this->wpforo->notice->add('Username is missed.', 'error');
146 return FALSE;
147 }elseif ( ! validate_username( $user_login ) ) {
148 $this->wpforo->notice->add('Illegal character in username.', 'error');
149 $user_login = '';
150 return FALSE;
151 }elseif( strlen($user_login) < 3 || strlen($user_login) > 15 ){
152 $this->wpforo->notice->add('Username length must be between 3 characters and 15 characters.', 'error');
153 return FALSE;
154 }elseif ( username_exists( $user_login ) ) {
155 $this->wpforo->notice->add('Username exists. Please insert another.', 'error');
156 return FALSE;
157 }elseif ( $user_email == '' ) {
158 $this->wpforo->notice->add('Insert your Email address.', 'error');
159 return FALSE;
160 }elseif ( ! is_email( $user_email ) ) {
161 $this->wpforo->notice->add('Invalid Email address', 'error');
162 $user_email = '';
163 return FALSE;
164 }elseif ( email_exists( $user_email ) ) {
165 $this->wpforo->notice->add('Email address exists. Please insert another.', 'error');
166 return FALSE;
167 }elseif( strlen($user_pass1) < 6 || strlen($user_pass1) > 20 ){
168 $this->wpforo->notice->add('Password length must be between 6 characters and 20 characters.', 'error');
169 return FALSE;
170 }elseif($user_pass1 != $user_pass2){
171 $this->wpforo->notice->add('Password mismatch.', 'error');
172 return FALSE;
173 }else{
174 $user_id = wp_create_user( $user_login, $user_pass1, $user_email );
175 if ( !is_wp_error( $user_id ) && $user_id ) {
176 $creds = array('user_login' => $user_login, 'user_password' => $user_pass1 );
177 wp_signon($creds);
178 $this->wpforo->notice->add('Success! Thank you Dear Friend', 'success');
179 return $user_id;
180 }
181 }
182 }
183 if(!empty($user_id->errors)){
184 $args = array();
185 foreach($user_id->errors as $u_err) $args[] = $u_err[0];
186 $this->wpforo->notice->add($args, 'error');
187 return FALSE;
188 }
189 $this->wpforo->notice->add('Registration Error', 'error');
190 return FALSE;
191 }
192
193 function edit( $args = array() ){
194
195 if( empty($args) && empty($_REQUEST['member']) ) return FALSE;
196 if( empty($args) && !empty($_REQUEST['member']) ) $args = $_REQUEST['member'];
197 extract($args, EXTR_OVERWRITE);
198
199 if( isset($userid) && isset($display_name) && isset($user_email) ){
200 $userid = intval($userid);
201 $display_name = sanitize_text_field($display_name);
202 $user_email = sanitize_email($user_email);
203 if ( ! is_email( $user_email ) ) {
204 $this->wpforo->notice->add('Invalid Email address', 'error');
205 $user_email = '';
206 return FALSE;
207 }elseif ( ( $owner_id = email_exists( $user_email ) ) && ( $owner_id != $userid ) ) {
208 $this->wpforo->notice->add('This email address is already registered. Please insert another.', 'error');
209 return FALSE;
210 }
211
212 if ( is_user_logged_in() ) {
213 $current_user_id = get_current_user_id();
214 if(!$this->wpforo->perm->user_can_manage_user( $current_user_id, $userid )){
215 $this->wpforo->notice->add('Permission denied', 'error');
216 return FALSE;
217 }
218 }
219 else{
220 $this->wpforo->notice->add('Permission denied', 'error');
221 return FALSE;
222 }
223
224 if( $display_name && $user_email ){
225 $this->wpforo->db->update(
226 $this->wpforo->db->prefix.'users',
227 array(
228 'display_name' => sanitize_text_field($display_name),
229 'user_email' => sanitize_email($user_email)
230 ),
231 array('ID' => $userid),
232 array(
233 '%s',
234 '%s'
235 ),
236 array('%d')
237 );
238 $this->reset($userid);
239 }
240
241 if( FALSE !== $this->edit_profile($args) ){
242 $this->wpforo->notice->add('Your profile data have been successfully updated.', 'success');
243 return $userid;
244 }
245 }
246
247 $this->wpforo->notice->add('Something wrong with profile data.', 'error');
248 return FALSE;
249 }
250
251 function upload_avatar(){
252 if( !isset($_POST['member']['userid']) || !$userid = intval($_POST['member']['userid']) ) return;
253 if( !$user = $this->get_member($userid) ) return;
254 $username = $user['user_nicename'];
255 if(isset($_FILES['avatar']) && !empty($_FILES['avatar']) && isset($_FILES['avatar']['name']) && $_FILES['avatar']['name']){
256
257 $name = sanitize_file_name($_FILES['avatar']['name']); //myimg.png
258 $type = sanitize_mime_type($_FILES['avatar']['type']); //image/png
259 $tmp_name = sanitize_text_field($_FILES['avatar']['tmp_name']); //D:\wamp\tmp\php986B.tmp
260 $error = sanitize_text_field($_FILES['avatar']['error']); //0
261 $size = intval($_FILES['avatar']['size']); //6112
262
263 if( $error ){
264 $error = wpforo_file_upload_error($error);
265 $this->wpforo->notice->clear();
266 $this->wpforo->notice->add($error, 'error');
267 return FALSE;
268 }
269
270 $upload_dir = wp_upload_dir();
271 $uplds_dir = $upload_dir['basedir']."/wpforo";
272 $avatar_dir = $upload_dir['basedir']."/wpforo/avatars";
273 if(!is_dir($uplds_dir)){
274 wp_mkdir_p($uplds_dir);
275 }
276 if(!is_dir($avatar_dir)){
277 wp_mkdir_p($avatar_dir);
278 }
279
280 $ext = pathinfo($name, PATHINFO_EXTENSION);
281 if( !wpforo_is_image($ext) ){
282 $this->wpforo->notice->clear();
283 $this->wpforo->notice->add('Incorrect file format. Allowed formats: jpeg, jpg, png, gif.', 'error');
284 return false;
285 }
286 $avatar_fname = $username."_".$userid.".".$ext;
287 $avatar_path = $avatar_dir."/".$avatar_fname;
288
289 if(is_dir($avatar_dir)){
290 if(move_uploaded_file($tmp_name, $avatar_path)) {
291 $image = wp_get_image_editor( $avatar_path );
292 if ( ! is_wp_error( $image ) ) {
293 $image->resize( 150, 150, true );
294 $image->save( $avatar_path );
295 }
296 $blog_url = preg_replace('|^https?\:|is', '', $upload_dir['baseurl']);
297 $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'));
298 $this->reset($userid);
299 }
300 }
301 }
302 }
303
304 function synchronize_user($userid){
305 if(!$userid) return FALSE;
306 if( $user = get_userdata($userid) ){
307 if( in_array('administrator', $user->roles) ){
308 $groupid = 1;
309 }elseif( in_array('editor', $user->roles) ){
310 $groupid = 2;
311 }elseif( in_array('customer', $user->roles) ){
312 $groupid = 5;
313 }else{
314 $groupid = $this->wpforo->default_groupid;
315 }
316 $insert_groupid = (isset($_POST['wpforo_usergroup'])) ? intval($_POST['wpforo_usergroup']) : $groupid;
317 $insert_timezone = (isset($_POST['wpforo_usertimezone'])) ? sanitize_text_field($_POST['wpforo_usertimezone']) : '';
318 $about = get_user_meta( $userid, 'description', true );
319 return $this->add_profile(
320 array( 'userid' => intval($userid),
321 'username' => sanitize_user($user->user_login),
322 'groupid' => intval($insert_groupid),
323 'site' => esc_url($user->user_url),
324 'timezone' => sanitize_text_field($insert_timezone),
325 'about' => stripslashes( wpforo_kses(trim($about), 'user_description') ),
326 'last_login' => sanitize_text_field($user->user_registered) ) );
327 }
328 return FALSE;
329 }
330
331 function synchronize_users(){
332 $sql = "SELECT `ID` FROM `".$this->wpforo->db->prefix."users` WHERE `ID` NOT IN( SELECT `userid` FROM `".$this->wpforo->db->prefix."wpforo_profiles` )";
333 $userids = $this->wpforo->db->get_col($sql);
334 if( !empty($userids) ){
335 foreach($userids as $userid){
336 $this->synchronize_user($userid);
337 }
338 }
339 }
340
341 function get_member($args = array(), $cache = false){
342
343 if(is_array($args)){
344
345 $default = array(
346 'userid' => NULL, // userid
347 'username' => '' // username
348 );
349
350 }else{
351
352 $default = array(
353 'userid' => $args, // userid
354 'username' => '' // username
355 );
356
357 }
358
359 $args = wpforo_parse_args( $args, $default );
360
361 if(isset($args['userid'])){
362 if( $cache && isset(self::$cache['user'][$args['userid']]) ){
363 return self::$cache['user'][$args['userid']];
364 }
365 }
366
367 $user_meta_obj = true;
368
369 if(!empty($args)){
370
371 extract($args, EXTR_OVERWRITE);
372
373 $do_db_cache = wpforo_feature('member_cashe', $this->wpforo);
374
375 $userid = intval($userid);
376 $username = sanitize_user($username);
377
378 if( $do_db_cache ){
379 if( $username != '' ){
380 $user_obj = get_user_by( 'user_nicename', $username );
381 if(!empty($user_obj)) $userid = $user_obj->ID;
382 $member = get_user_meta( $userid, '_wpf_member_obj', true );
383 }
384 elseif( $userid != NULL ){
385 $member = get_user_meta( $userid, '_wpf_member_obj', true );
386 }
387 else{
388 $user_meta_obj = true;
389 }
390 }
391 else{
392 $member = array();
393 }
394
395 if(empty($member)){
396 $user_meta_obj = false;
397 $sql = "SELECT *, ug.name AS groupname FROM `".$this->wpforo->db->prefix."users` u
398 LEFT JOIN `".$this->wpforo->db->prefix."wpforo_profiles` p ON p.`userid` = u.`ID`
399 LEFT JOIN `".$this->wpforo->db->prefix."wpforo_usergroups` ug ON ug.`groupid` = p.`groupid`";
400 $wheres = array();
401 if($userid != NULL) $wheres[] = "`ID` = " . intval($userid);
402 if($username != '') $wheres[] = "`user_nicename` = '" . esc_sql($username) . "'";
403 if( !empty($wheres) ) $sql .= " WHERE " . implode($wheres, " AND ");
404 $member = $this->wpforo->db->get_row($sql, ARRAY_A);
405 }
406
407 if(!empty($member)) {
408 if( $do_db_cache ){
409 if(!$user_meta_obj) {
410 $member['profile_url'] = $this->profile_url( $member );
411 $member['stat'] = $this->get_stat( $member, false, true );
412 update_user_meta( $userid, '_wpf_member_obj', $member );
413 }
414 }
415 else{
416 $member['profile_url'] = $this->profile_url( $member );
417 $member['stat'] = $this->get_stat( $member, false, true );
418 }
419 }
420
421 if($cache && isset($userid)){
422 return self::$cache['user'][$userid] = $member;
423 }
424 else{
425 return $member;
426 }
427 }
428 }
429
430 function get_members($args = array(), &$items_count = 0){
431
432 $default = array(
433 'include' => array(), // array( 2, 10, 25 )
434 'exclude' => array(), // array( 2, 10, 25 )
435 'status' => 'active', // 'active', 'blocked', 'trashed', 'spamer'
436 'groupid' => NULL, // groupid
437 'orderby' => 'userid', //
438 'order' => 'ASC', // ASC DESC
439 'offset' => 0, // OFFSET
440 'row_count' => NULL // ROW COUNT
441 );
442
443 $args = wpforo_parse_args( $args, $default );
444 if(!empty($args)){
445 extract($args, EXTR_OVERWRITE);
446
447 $include = wpforo_parse_args( $include );
448 $exclude = wpforo_parse_args( $exclude );
449
450 $sql = "SELECT *, ug.name AS groupname FROM `".$this->wpforo->db->prefix."users` u
451 LEFT JOIN `".$this->wpforo->db->prefix."wpforo_profiles` p ON p.`userid` = u.`ID`
452 LEFT JOIN `".$this->wpforo->db->prefix."wpforo_usergroups` ug ON ug.`groupid` = p.`groupid`";
453 $wheres = array();
454 if(!empty($include)) $wheres[] = "u.`ID` IN(" . implode(', ', array_map('intval', $include)) . ")";
455 if(!empty($exclude)) $wheres[] = "u.`ID` NOT IN(" . implode(', ', array_map('intval', $exclude)) . ")";
456 if(isset($status)) $wheres[] = " p.`status` = '" . esc_sql(sanitize_text_field($status)) . "' ";
457 if($groupid != NULL) $wheres[] = "p.`groupid` = " . intval($groupid);
458
459 if(!empty($wheres)) $sql .= " WHERE " . implode($wheres, " AND ");
460
461 $item_count_sql = preg_replace('#SELECT.+?FROM#isu', 'SELECT count(*) FROM', $sql);
462 if( $item_count_sql ) $items_count = $this->wpforo->db->get_var($item_count_sql);
463
464 $sql .= esc_sql(" ORDER BY $orderby " . $order);
465 if($row_count) $sql .= esc_sql(" LIMIT $offset,$row_count");
466
467 return $this->wpforo->db->get_results($sql, ARRAY_A);
468 }
469 }
470
471 function search($needle, $fields = array()){
472
473 if($needle != ''){
474 $needle = sanitize_text_field($needle);
475 if(empty($fields)){
476 $fields = array(
477 'title',
478 'user_nicename',
479 'user_email',
480 'signature'
481 );
482 }
483
484 $sql = "SELECT `ID` FROM `".$this->wpforo->db->prefix."users` u LEFT JOIN `".$this->wpforo->db->prefix."wpforo_profiles` p ON p.`userid` = u.`ID`";
485 $wheres = array();
486
487 foreach($fields as $field){
488 $field = sanitize_text_field($field);
489 $wheres[] = "`".esc_sql($field)."` LIKE '%" . esc_sql($needle) ."%'";
490 }
491
492 if(!empty($wheres)){
493 $sql .= " WHERE " . implode($wheres, " OR ");
494 $results = $this->wpforo->db->get_results($sql, ARRAY_A);
495 $userids = array();
496 foreach($results as $result){
497 $userids[] = $result['ID'];
498 }
499 return $userids;
500 }else{
501 return array();
502 }
503 }else{
504 return array();
505 }
506
507 }
508
509 /**
510 * make user trashed
511 * NOTE there is no way to delete user
512 *
513 * @since 1.0.0
514 *
515 * @return true or false
516 */
517
518
519 function delete( $userid, $permanently = FALSE ){
520 if( $permanently ){
521 if( FALSE !== $this->wpforo->db->delete(
522 $this->wpforo->db->prefix.'wpforo_profiles', array( 'userid' => intval( $userid ) ), array( '%d' )
523 )
524 ){
525 $this->wpforo->notice->add('User successfully deleted from wpforo', 'success');
526 return TRUE;
527 }
528 }else{
529 if( FALSE !== $this->wpforo->db->update(
530 $this->wpforo->db->prefix.'wpforo_profiles',
531 array('status' => 'trashed'),
532 array('userid' => intval( $userid )),
533 array('%s'),
534 array('%d')
535 )
536 ){
537 $this->wpforo->notice->add('User successfully deleted from wpforo', 'success');
538 return TRUE;
539 }
540 }
541
542 $this->wpforo->notice->add('User delete error', 'error');
543 return FALSE;
544 }
545
546 function avatar($member, $attr = '', $size = '', $cache = false){
547
548 if(!isset($member['userid'])) return;
549
550 $src = $member['avatar'];
551 $userid = $member['userid'];
552 if(isset(self::$cache['avatar'][$userid])){
553 if(self::$cache['avatar'][$userid]['attr'] == $attr && self::$cache['avatar'][$userid]['size'] == $size){
554 if(isset(self::$cache['avatar'][$userid]['img'])){
555 return self::$cache['avatar'][$userid]['img'];
556 }
557 }
558 }
559 if($src && wpforo_feature('custom-avatars', $this->wpforo)){
560 $attr = ($attr ? $attr : 'height="96" width="96"');
561 $img = '<img class="avatar" src="'.esc_url($src).'" '. $attr .' />';
562 }else{
563 $img = ($size) ? get_avatar($userid, $size) : get_avatar($userid);
564 if($attr) $img = str_replace('<img', '<img ' . $attr, $img);
565 }
566 if($cache){
567 self::$cache['avatar'][$userid]['attr'] = $attr;
568 self::$cache['avatar'][$userid]['size'] = $size;
569 return self::$cache['avatar'][$userid]['img'] = $img;
570 }
571 else{
572 return $img;
573 }
574 }
575
576 function get_avatar($userid, $attr = '', $size = '', $cache = false){
577 if(isset(self::$cache['avatar'][$userid])){
578 if(self::$cache['avatar'][$userid]['attr'] == $attr && self::$cache['avatar'][$userid]['size'] == $size){
579 if(isset(self::$cache['avatar'][$userid]['img'])){
580 return self::$cache['avatar'][$userid]['img'];
581 }
582 }
583 }
584 $src = $this->wpforo->db->get_var("SELECT `avatar` FROM `".$this->wpforo->db->prefix."wpforo_profiles` WHERE `userid` = ".intval($userid));
585 if($src && wpforo_feature('custom-avatars', $this->wpforo)){
586 $attr = ($attr ? $attr : 'height="96" width="96"');
587 $img = '<img class="avatar" src="'.esc_url($src).'" '. $attr .' />';
588 }else{
589 $img = ($size) ? get_avatar($userid, $size) : get_avatar($userid);
590 if($attr) $img = str_replace('<img', '<img ' . $attr, $img);
591 }
592 if($cache){
593 self::$cache['avatar'][$userid]['attr'] = $attr;
594 self::$cache['avatar'][$userid]['size'] = $size;
595 return self::$cache['avatar'][$userid]['img'] = $img;
596 }
597 else{
598 return $img;
599 }
600 }
601
602 public function get_avatar_url($userid){
603 return $this->wpforo->db->get_var("SELECT `avatar` FROM `".$this->wpforo->db->prefix."wpforo_profiles` WHERE `userid` = ".intval($userid));
604 }
605
606 function get_topics_count( $userid ){
607 $count = $this->wpforo->db->get_var("SELECT count(topicid) FROM `".$this->wpforo->db->prefix."wpforo_topics` WHERE `userid` = ".intval($userid));
608 return $count;
609 }
610
611 function get_questions_count( $userid ){
612 $count = $this->wpforo->db->get_var("SELECT count(topicid) FROM `".$this->wpforo->db->prefix."wpforo_topics` WHERE `userid` = ".intval($userid));
613 return $count;
614 }
615
616 function get_answers_count( $userid ){
617 $count = $this->wpforo->db->get_var("SELECT count(postid) FROM `".$this->wpforo->db->prefix."wpforo_posts` WHERE `is_answer` = 1 AND `userid` = ".intval($userid));
618 return $count;
619 }
620
621 function get_question_comments_count( $userid ){
622 $count = $this->wpforo->db->get_var("SELECT count(postid) FROM `".$this->wpforo->db->prefix."wpforo_posts` WHERE `parentid` > 0 AND `userid` = ".intval($userid));
623 return $count;
624 }
625
626 function get_replies_count( $userid ){
627 $count = $this->wpforo->db->get_var("SELECT count(postid) FROM `".$this->wpforo->db->prefix."wpforo_posts` WHERE `userid` = ".intval($userid));
628 return $count;
629 }
630
631 function get_likes_count( $userid ){
632 $count = $this->wpforo->db->get_var("SELECT count(likeid) FROM `".$this->wpforo->db->prefix."wpforo_likes` WHERE `userid` = ".intval($userid));
633 return $count;
634 }
635
636 function get_votes_count( $userid ){
637 $count = $this->wpforo->db->get_var("SELECT count(voteid) FROM `".$this->wpforo->db->prefix."wpforo_votes` WHERE `userid` = ".intval($userid));
638 return $count;
639 }
640
641 // how many times the user like or vote
642 function get_votes_and_likes_count( $userid ){
643 return $this->get_votes_count( intval($userid) ) + $this->get_likes_count( intval($userid) );
644 }
645
646 //getting user's posts votes and likes count
647 function get_user_votes_and_likes_count( $userid ){
648 $votes_count = $this->wpforo->db->get_var("SELECT count(voteid) FROM `".$this->wpforo->db->prefix."wpforo_votes` WHERE `post_userid` = ".intval($userid));
649 $likes_count = $this->wpforo->db->get_var("SELECT count(likeid) FROM `".$this->wpforo->db->prefix."wpforo_likes` WHERE `post_userid` = ".intval($userid));
650 return $votes_count + $likes_count;
651 }
652
653 function get_profile_url( $arg, $template = 'profile' ){
654 if(!$arg) return WPFORO_BASE_URL;
655 $userid = intval( basename($arg) );
656 $member_args = ( $userid ? $userid : array( 'username' => basename($arg) ) );
657 $user = $this->get_member( $member_args );
658 if(empty($user)) return WPFORO_BASE_URL;
659 $user_slug = ( wpfo($this->wpforo->member_options['url_structure'], false) == 'id' ? $user['ID'] : $user['user_nicename'] );
660 return WPFORO_BASE_URL . "$template/$user_slug";
661 }
662
663 function profile_url( $member = array(), $template = 'profile' ){
664 if(isset($member['ID']) || isset($member['user_nicename'])){
665 $user_slug = ( wpfo($this->wpforo->member_options['url_structure'], false) == 'id' ? $member['ID'] : $member['user_nicename'] );
666 $profile_url = WPFORO_BASE_URL . "$template/$user_slug";
667 $profile_url = apply_filters( 'wpforo_profile_url', $profile_url, $member, $template );
668 }
669 else{
670 $profile_url = WPFORO_BASE_URL;
671 $profile_url = apply_filters( 'wpforo_no_profile_url', $profile_url, $template );
672
673 }
674 return $profile_url;
675 }
676
677 //$args = UserID or Member Object
678 //$live_count = TRUE / FALSE
679 function get_stat( $args = array(), $live_count = false, $cache = false ){
680
681 $stat = array( 'points' => 0,
682 'rating' => 0,
683 'rating_procent' => 0,
684 'color' => $this->rating(0, 'color'),
685 'badge' => $this->rating(0, 'icon'),
686 'posts' => 0,
687 'topics' => 0,
688 'questions' => 0,
689 'answers' => 0,
690 'question_comments' => 0,
691 'likes' => 0,
692 'liked' => 0,
693 'title' => $this->rating(0, 'title'));
694
695 $userid = ( isset($args['userid']) && $args['userid'] ) ? $args['userid'] : $args;
696
697 if( $cache && isset(self::$cache['stat'][$userid]) ){
698 return self::$cache['stat'][$userid];
699 }
700
701 if( is_array($args) && isset($args['userid']) ){
702 $userid = $args['userid'];
703 $stat['topics'] = (int)$this->get_topics_count( $userid );
704 if(isset($args['questions'])) $stat['questions'] = intval($args['questions']);
705 if(isset($args['answers'])) $stat['answers'] = intval($args['answers']);
706 if(isset($args['posts'])) $stat['posts'] = intval($args['posts']);
707 if(isset($args['comments'])) $stat['question_comments'] = intval($args['comments']);
708 }
709 elseif($userid = wpforo_bigintval($args)){
710 $stat['topics'] = (int)$this->get_topics_count( $userid );
711 if($live_count){
712 if($questions = $this->get_questions_count( $userid )) $stat['questions'] = $questions;
713 if($answers = $this->get_answers_count( $userid )) $stat['answers'] = $answers;
714 if($posts = $this->get_replies_count( $userid )) $stat['posts'] = $posts;
715 if($question_comments = $this->get_question_comments_count( $userid )) $stat['question_comments'] = $question_comments;
716 }
717 else{
718 $profile = $this->wpforo->db->get_var("SELECT `posts`, `questions`, `answers`, `comments` FROM `".$this->wpforo->db->prefix."wpforo_profiles` WHERE `userid` = ".intval($userid));
719 if(isset($profile['questions'])) $stat['questions'] = intval($profile['questions']);
720 if(isset($profile['answers'])) $stat['answers'] = intval($profile['answers']);
721 if(isset($profile['posts'])) $stat['posts'] = intval($profile['posts']);
722 if(isset($profile['comments'])) $stat['question_comments'] = intval($profile['comments']);
723 }
724 }
725
726 if( $userid ){
727 if($likes = $this->get_votes_and_likes_count( $userid )) $stat['likes'] = $likes;
728 if($liked = $this->get_user_votes_and_likes_count( $userid )) $stat['liked'] = $liked;
729 if($stat['posts']) $stat['points'] = $stat['posts']; //TO-DO: Point counter function based on all stat values.
730 if($stat['points']) $stat['rating'] = $this->rating_level($stat['points'], false);
731 if($stat['rating']) {
732 $stat['rating_procent'] = $stat['rating'] * 10;
733 $stat['title'] = $this->rating(intval($stat['rating']), 'title');
734 $stat['color'] = $this->rating(intval($stat['rating']), 'color');
735 $stat['badge'] = $this->rating(intval($stat['rating']), 'icon');
736 }
737 }
738
739 if($cache && isset($userid)){
740 return self::$cache['stat'][$userid] = $stat;
741 }
742 else{
743 return $stat;
744 }
745 }
746
747 function get_count(){
748 return $this->wpforo->db->get_var( "SELECT COUNT(`userid`) FROM `".$this->wpforo->db->prefix."wpforo_profiles`" );
749 }
750
751
752 function is_online( $userid, $duration = 240, $cache = true ){
753 if(isset(self::$cache['online'][$userid])){
754 if(self::$cache['online'][$userid]['durration'] == $duration ){
755 if(isset(self::$cache['online'][$userid]['status'])){
756 return self::$cache['online'][$userid]['status'];
757 }
758 }
759 }
760 if($duration == 240) $duration = $this->wpforo->member_options['online_status_timeout'];
761 $online_time = intval( get_user_meta($userid, 'wpforo_online_time', TRUE) );
762 $current_time = current_time( 'timestamp', 1 );
763 $online_duration = $current_time - $online_time;
764 if( $online_duration < $duration ) {
765 $status = true;
766 }
767 else{
768 $status = false;
769 }
770 if($cache){
771 self::$cache['online'][$userid]['durration'] = $duration;
772 return self::$cache['online'][$userid]['status'] = $status;
773 }
774 else{
775 return $status;
776 }
777 }
778
779 public function show_online_indicator($userid, $ico = TRUE){
780 if( $this->is_online($userid)) : ?>
781
782 <?php if($ico) : ?>
783 <i class="fa fa-lightbulb-o fa-0x wpfcl-8" title="<?php wpforo_phrase('Online') ?>"></i>
784 <?php else : wpforo_phrase('Online'); endif ?>
785
786 <?php else : ?>
787
788 <?php if($ico) : ?>
789 <i class="fa fa-lightbulb-o fa-0x wpfcl-0" title="<?php wpforo_phrase('Offline') ?>"></i>
790 <?php else : wpforo_phrase('Offline'); endif ?>
791
792 <?php endif;
793 }
794
795 function online_members_count( $duration = 240 ){
796 if($duration == 240) $duration = $this->wpforo->member_options['online_status_timeout'];
797 $current_time = current_time( 'timestamp', 1 );
798 $online_timeframe = $current_time - $duration;
799 return $this->wpforo->db->get_var( "SELECT COUNT(`user_id`) FROM `".$this->wpforo->db->prefix."usermeta` WHERE meta_key = 'wpforo_online_time' AND meta_value > " . wpforo_bigintval($online_timeframe) );
800
801 }
802
803 function get_online_members( $count = 1, $duration = 240 ){
804 if($duration == 240) $duration = $this->wpforo->member_options['online_status_timeout'];
805 $current_time = current_time( 'timestamp', 1 );
806 $online_timeframe = $current_time - $duration;
807 $onlinemembers_ids = $this->wpforo->db->get_col( "SELECT `user_id` FROM `".$this->wpforo->db->prefix."usermeta` WHERE meta_key = 'wpforo_online_time' AND meta_value > " . wpforo_bigintval($online_timeframe) );
808 if(!empty($onlinemembers_ids)){
809 $args = array(
810 'include' => $onlinemembers_ids, // array( 2, 10, 25 )
811 'orderby' => 'userid', // forumid, order, parentid
812 'row_count' => $count,
813 'order' => 'ASC', // ASC DESC
814 );
815 return $this->get_members( $args );
816 }
817 else{
818 return array();
819 }
820 }
821
822 function levels(){
823 $levels = array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
824 return $levels;
825 }
826
827 function rating( $level = false, $var = false, $default = false ){
828
829 $rating = array();
830 $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' );
831 $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 );
832 $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' );
833 $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' );
834
835 if(!empty($this->wpforo->member_options['rating'])){
836
837 if($level === false) return $this->wpforo->member_options['rating'];
838 if(!empty($this->wpforo->member_options['rating'][$level])){
839
840 if(!$var) return $this->wpforo->member_options['rating'][$level];
841 if(!empty($this->wpforo->member_options['rating'][$level][$var])){
842
843 return $this->wpforo->member_options['rating'][$level][$var];
844
845 }
846 }
847 }
848 if( $level !== false && $var ) { return $rating[$var][$level]; }
849 elseif( $level !== false && !$var ){ foreach( $rating as $variable => $values ){ $level_data[$variable] = $values[$level];} return $level_data; }
850 elseif( $level === false && !$var ) return $rating;
851 else return array();
852 }
853
854 function rating_level($member_posts, $percent = TRUE){
855 $bar = 0;
856 if($member_posts < $this->rating(1, 'points')){$bar = 0;}
857 elseif($member_posts < $this->rating(2, 'points')){$bar = 10;}
858 elseif($member_posts < $this->rating(3, 'points')){$bar = 20;}
859 elseif($member_posts < $this->rating(4, 'points')){$bar = 30;}
860 elseif($member_posts < $this->rating(5, 'points')){$bar = 40;}
861 elseif($member_posts < $this->rating(6, 'points')){$bar = 50;}
862 elseif($member_posts < $this->rating(7, 'points')){$bar = 60;}
863 elseif($member_posts < $this->rating(8, 'points')){$bar = 70;}
864 elseif($member_posts < $this->rating(9, 'points')){$bar = 80;}
865 elseif($member_posts < $this->rating(10, 'points')){$bar = 90;}
866 else{$bar = 100;}
867 if($percent){
868 return $bar;
869 }else{
870 return floor($bar/10);
871 }
872 }
873
874 function rating_badge($level = 0, $view = 'short'){
875
876 $level = ( $level > 10 ) ? floor($level/10) : $level;
877
878 if($level == 0){
879 return '<i class="fa '. sanitize_html_class($this->rating($level, 'icon')) .'"></i>';
880 }
881 elseif($level > 0 && $level < 6){
882 if( $view == 'full' ){
883 return str_repeat(' <i class="fa '. sanitize_html_class($this->rating($level, 'icon')) .'"></i> ', $level);
884 }
885 else{
886 return '<span>' . esc_html($level) . '</span> <i class="fa '. sanitize_html_class($this->rating($level, 'icon')) .'"></i>';
887 }
888 }
889 elseif($level > 5 && $level < 9){
890 if( $view == 'full' ){
891 return str_repeat(' <i class="fa '. sanitize_html_class($this->rating($level, 'icon')) .'"></i> ', ($level-5));
892 }
893 else{
894 return '<span>' . esc_html($level-5) . '</span> <i class="fa '. sanitize_html_class($this->rating($level, 'icon')) .'"></i>';
895 }
896 }
897 elseif($level > 8){
898 return '<i class="fa '. sanitize_html_class($this->rating($level, 'icon')) .'"></i>';
899 }
900 else{
901 return;
902 }
903 }
904
905 public function reset( $userid ){
906 if( !$userid ) return;
907 $this->wpforo->db->query( "DELETE FROM `" . $this->wpforo->db->prefix ."usermeta` WHERE `meta_key` = '_wpf_member_obj' AND `user_id` = " . intval($userid) );
908 }
909
910 public function clear_db_cache(){
911 $this->wpforo->db->query( "DELETE FROM `" . $this->wpforo->db->prefix ."usermeta` WHERE `meta_key` = '_wpf_member_obj'" );
912 }
913
914 public function init_current_user(){
915 if(is_user_logged_in()){
916 $current_user = wp_get_current_user();
917 update_user_meta( $current_user->ID, 'wpforo_online_time', current_time( 'timestamp', 1 ) );
918 $this->wpforo->current_user = $this->get_member( $current_user->ID );
919 $this->wpforo->current_user_groupid = $this->wpforo->current_user['groupid'];
920 $this->wpforo->current_userid = $current_user->ID;
921 $this->wpforo->current_username = $current_user->user_login;
922 $this->wpforo->current_user_email = $current_user->user_email;
923 $this->wpforo->current_user_display_name = $current_user->display_name;
924 }else{
925 $this->wpforo->current_user = array();
926 $this->wpforo->current_user_groupid = 4;
927 $this->wpforo->current_userid = 0;
928 $this->wpforo->current_username = '';
929 $this->wpforo->current_user_email = '';
930 $this->wpforo->current_user_display_name = '';
931 }
932 }
933
934 public function blog_posts( $userid ){
935 if( isset($userid) && $userid ) return count_user_posts( $userid , 'post' );
936 }
937
938 public function blog_comments($userid, $user_email){
939 global $wpdb;
940 if( !$userid || !$user_email ) return 0;
941 return (int) $wpdb->get_var("SELECT COUNT(*) FROM " . $wpdb->comments. " WHERE `user_id` = " . intval($userid) . " OR `comment_author_email` = '" . esc_sql($user_email) . "'");
942 }
943
944 }
945
946 ?>