PluginProbe
wpForo Forum / beta-4
wpForo Forum vbeta-4
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 beta-4, at wpf-includes/class-members.php

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