PluginProbe
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP / 1.0.20
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP v1.0.20
1.2.72 1.2.71 1.2.70 1.2.69 1.2.68 1.2.67 1.2.66 1.2.65 1.2.64 1.2.63 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.23 All 172 releases
userswp / includes / helpers / misc.php

misc.php in UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP 1.0.20, at includes/helpers/misc.php

1,698 lines 53.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Converts string value to options array.
4 * Used in select, multiselect and radio fields.
5 * Wraps inside optgroup if available.
6 *
7 * @since 1.0.0
8 * @package userswp
9 *
10 * @param string $option_values String option values.
11 * @param bool $translated Do you want to translate the output?
12 *
13 * @return array|null Options array.
14 */
15 function uwp_string_values_to_options($option_values = '', $translated = false)
16 {
17 $options = array();
18 if ($option_values == '') {
19 return NULL;
20 }
21
22 if (strpos($option_values, "{/optgroup}") !== false) {
23 $option_values_arr = explode("{/optgroup}", $option_values);
24
25 foreach ($option_values_arr as $optgroup) {
26 if (strpos($optgroup, "{optgroup}") !== false) {
27 $optgroup_arr = explode("{optgroup}", $optgroup);
28
29 $count = 0;
30 foreach ($optgroup_arr as $optgroup_str) {
31 $count++;
32 $optgroup_str = trim($optgroup_str);
33
34 $optgroup_label = '';
35 if (strpos($optgroup_str, "|") !== false) {
36 $optgroup_str_arr = explode("|", $optgroup_str, 2);
37 $optgroup_label = trim($optgroup_str_arr[0]);
38 if ($translated && $optgroup_label != '') {
39 $optgroup_label = __($optgroup_label, 'userswp');
40 }
41 $optgroup_label = ucfirst($optgroup_label);
42 $optgroup_str = $optgroup_str_arr[1];
43 }
44
45 $optgroup3 = uwp_string_to_options($optgroup_str, $translated);
46
47 if ($count > 1 && $optgroup_label != '' && !empty($optgroup3)) {
48 $optgroup_start = array(array('label' => $optgroup_label, 'value' => NULL, 'optgroup' => 'start'));
49 $optgroup_end = array(array('label' => $optgroup_label, 'value' => NULL, 'optgroup' => 'end'));
50 $optgroup3 = array_merge($optgroup_start, $optgroup3, $optgroup_end);
51 }
52 $options = array_merge($options, $optgroup3);
53 }
54 } else {
55 $optgroup1 = uwp_string_to_options($optgroup, $translated);
56 $options = array_merge($options, $optgroup1);
57 }
58 }
59 } else {
60 $options = uwp_string_to_options($option_values, $translated);
61 }
62
63 return $options;
64 }
65
66 /**
67 * Converts string value to options array.
68 * Used in select, multiselect and radio fields.
69 *
70 * @since 1.0.0
71 * @package userswp
72 *
73 * @param string $input Input String
74 * @param bool $translated Do you want to translate the output?
75 *
76 * @return array Options array.
77 */
78 function uwp_string_to_options($input = '', $translated = false)
79 {
80 $return = array();
81 if ($input != '') {
82 $input = trim($input);
83 $input = rtrim($input, ",");
84 $input = ltrim($input, ",");
85 $input = trim($input);
86 }
87
88 $input_arr = explode(',', $input);
89
90 if (!empty($input_arr)) {
91 foreach ($input_arr as $input_str) {
92 $input_str = trim($input_str);
93
94 if (strpos($input_str, "/") !== false) {
95 $input_str = explode("/", $input_str, 2);
96 $label = trim($input_str[0]);
97 if ($translated && $label != '') {
98 $label = __($label, 'userswp');
99 }
100 $label = ucfirst($label);
101 $value = trim($input_str[1]);
102 } else {
103 if ($translated && $input_str != '') {
104 $input_str = __($input_str, 'userswp');
105 }
106 $label = ucfirst($input_str);
107 $value = $input_str;
108 }
109
110 if ($label != '') {
111 $return[] = array('label' => $label, 'value' => $value, 'optgroup' => NULL);
112 }
113 }
114 }
115
116 return $return;
117 }
118
119
120 /**
121 * Resizes the image.
122 *
123 * @since 1.0.0
124 * @package userswp
125 *
126 * @param string $image Reference a local file
127 * @param int $width Image width
128 * @param int $height Image height
129 * @param float $scale Image scale ratio.
130 *
131 * @return mixed Resized image.
132 */
133 function uwp_resizeImage($image,$width,$height,$scale) {
134 /** @noinspection PhpUnusedLocalVariableInspection */
135 list($imagewidth, $imageheight, $imageType) = getimagesize($image);
136 $imageType = image_type_to_mime_type($imageType);
137 $newImageWidth = ceil($width * $scale);
138 $newImageHeight = ceil($height * $scale);
139 $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
140 $source = false;
141 switch($imageType) {
142 case "image/gif":
143 $source=imagecreatefromgif($image);
144 break;
145 case "image/pjpeg":
146 case "image/jpeg":
147 case "image/jpg":
148 $source=imagecreatefromjpeg($image);
149 break;
150 case "image/png":
151 case "image/x-png":
152 $source=imagecreatefrompng($image);
153 break;
154 }
155 imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height);
156
157 switch($imageType) {
158 case "image/gif":
159 imagegif($newImage,$image);
160 break;
161 case "image/pjpeg":
162 case "image/jpeg":
163 case "image/jpg":
164 imagejpeg($newImage,$image,90);
165 break;
166 case "image/png":
167 case "image/x-png":
168 imagepng($newImage,$image);
169 break;
170 }
171
172 chmod($image, 0777);
173 return $image;
174 }
175
176 /**
177 * Resizes thumbnail image.
178 *
179 * @since 1.0.0
180 * @package userswp
181 *
182 * @param string $thumb_image_name
183 * @param string $image
184 * @param int $x x-coordinate of source point.
185 * @param int $y y-coordinate of source point.
186 * @param int $src_w Source width.
187 * @param int $src_h Source height.
188 * @param float $scale Image scale ratio.
189 *
190 * @return mixed Resized image.
191 */
192 function uwp_resizeThumbnailImage($thumb_image_name, $image, $x, $y, $src_w, $src_h, $scale){
193 // ignore image createion warnings
194 @ini_set('gd.jpeg_ignore_warning', 1);
195 /** @noinspection PhpUnusedLocalVariableInspection */
196 list($imagewidth, $imageheight, $imageType) = getimagesize($image);
197 $imageType = image_type_to_mime_type($imageType);
198
199 $newImageWidth = ceil($src_w * $scale);
200 $newImageHeight = ceil($src_h * $scale);
201 $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
202 $source = false;
203 switch($imageType) {
204 case "image/gif":
205 $source=imagecreatefromgif($image);
206 break;
207 case "image/pjpeg":
208 case "image/jpeg":
209 case "image/jpg":
210 $source=imagecreatefromjpeg($image);
211 break;
212 case "image/png":
213 case "image/x-png":
214 $source=imagecreatefrompng($image);
215 break;
216 }
217 imagecopyresampled($newImage,$source,0,0,$x,$y,$newImageWidth, $newImageHeight, $src_w, $src_h);
218 switch($imageType) {
219 case "image/gif":
220 imagegif($newImage, $thumb_image_name);
221 break;
222 case "image/pjpeg":
223 case "image/jpeg":
224 case "image/jpg":
225 imagejpeg($newImage, $thumb_image_name, 100);
226 break;
227 case "image/png":
228 case "image/x-png":
229 imagepng($newImage, $thumb_image_name);
230 break;
231 }
232
233 chmod($thumb_image_name, 0777);
234 return $thumb_image_name;
235 }
236
237
238 /**
239 * Logs the error message.
240 *
241 * @since 1.0.0
242 * @package userswp
243 *
244 * @param array|object|string $log Error message.
245 *
246 * @return void
247 */
248 function uwp_error_log($log){
249 /*
250 * A filter to override the WP_DEBUG setting for function uwp_error_log().
251 */
252 $should_log = apply_filters( 'uwp_log_errors', WP_DEBUG);
253 if ( true === $should_log ) {
254 if ( is_array( $log ) || is_object( $log ) ) {
255 error_log( print_r( $log, true ) );
256 } else {
257 error_log( $log );
258 }
259 }
260 }
261
262
263 /**
264 * Prints the users page main content.
265 *
266 * @since 1.0.0
267 * @package userswp
268 *
269 * @return void
270 */
271 function get_uwp_users_list() {
272
273 global $wpdb;
274
275 $keyword = false;
276 if (isset($_GET['uwps']) && $_GET['uwps'] != '') {
277 $keyword = stripslashes(strip_tags($_GET['uwps']));
278 }
279
280 $sort_by = false;
281 if (isset($_GET['uwp_sort_by']) && $_GET['uwp_sort_by'] != '') {
282 $sort_by = strip_tags(esc_sql($_GET['uwp_sort_by']));
283 }
284
285 $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
286
287 $number = uwp_get_option('profile_no_of_items', 10);
288
289 $where = '';
290 $where = apply_filters('uwp_users_search_where', $where, $keyword);
291
292 $arg = array(
293 'fields' => 'ID',
294 'meta_query' => array(
295 'relation' => 'OR',
296 array(
297 'key' => 'uwp_mod',
298 'value' => 'email_unconfirmed',
299 'compare' => '=='
300 ),
301 array(
302 'key' => 'uwp_hide_from_listing',
303 'value' => 1,
304 'compare' => '=='
305 )
306 )
307 );
308
309 $inactive_users = new WP_User_Query($arg);
310 $exclude_users = $inactive_users->get_results();
311
312 $excluded_globally = uwp_get_option('users_excluded_from_list');
313 if ( $excluded_globally ) {
314 $users = str_replace(' ', '', $excluded_globally );
315 $users_array = explode(',', $users );
316 $exclude_users = array_merge($exclude_users, $users_array);
317 }
318
319 $exclude_users = apply_filters('uwp_excluded_users_from_list', $exclude_users, $where, $keyword);
320
321 if($exclude_users){
322 $exclude_users_list = implode(',', array_unique($exclude_users));
323 $exclude_query = 'AND '. $wpdb->users.'.ID NOT IN ('.$exclude_users_list.')';
324 } else {
325 $exclude_query = ' ';
326 }
327
328 if ($keyword || $where) {
329 if (empty($where)) {
330 $users = $wpdb->get_results($wpdb->prepare(
331 "SELECT DISTINCT SQL_CALC_FOUND_ROWS $wpdb->users.*
332 FROM $wpdb->users
333 INNER JOIN $wpdb->usermeta
334 ON ( $wpdb->users.ID = $wpdb->usermeta.user_id )
335 WHERE 1=1
336 $exclude_query
337 AND (
338 ( $wpdb->usermeta.meta_key = 'first_name' AND $wpdb->usermeta.meta_value LIKE %s )
339 OR
340 ( $wpdb->usermeta.meta_key = 'last_name' AND $wpdb->usermeta.meta_value LIKE %s )
341 OR
342 user_login LIKE %s
343 OR
344 user_nicename LIKE %s
345 OR
346 display_name LIKE %s
347 OR
348 user_email LIKE %s
349 )
350 ORDER BY display_name ASC
351 LIMIT 0, 20",
352 array(
353 '%' . $keyword . '%',
354 '%' . $keyword . '%',
355 '%' . $keyword . '%',
356 '%' . $keyword . '%',
357 '%' . $keyword . '%',
358 '%' . $keyword . '%'
359 )
360 ));
361 } else {
362 $usermeta_table = get_usermeta_table_prefix() . 'uwp_usermeta';
363
364 $users = $wpdb->get_results(
365 "SELECT DISTINCT SQL_CALC_FOUND_ROWS $wpdb->users.*
366 FROM $wpdb->users
367 INNER JOIN $usermeta_table
368 ON ( $wpdb->users.ID = $usermeta_table.user_id )
369 WHERE 1=1
370 $exclude_query
371 $where
372 ORDER BY display_name ASC
373 LIMIT 0, 20");
374 }
375
376 $total_user = count($users);
377
378 } else {
379
380 $args = array(
381 'number' => (int) $number,
382 'paged' => $paged,
383 'exclude' => $exclude_users,
384 );
385
386
387 if ($sort_by) {
388 switch ($sort_by) {
389 case "newer":
390 $args['orderby'] = 'registered';
391 $args['order'] = 'desc';
392 break;
393 case "older":
394 $args['orderby'] = 'registered';
395 $args['order'] = 'asc';
396 break;
397 case "alpha_asc":
398 $args['orderby'] = 'display_name';
399 $args['order'] = 'asc';
400 break;
401 case "alpha_desc":
402 $args['orderby'] = 'display_name';
403 $args['order'] = 'desc';
404 break;
405
406 }
407 }
408
409 $users_query = new WP_User_Query($args);
410 $users = $users_query->get_results();
411 $total_user = $users_query->get_total();
412
413 }
414
415 $total_pages=ceil($total_user/$number);
416
417 $layout_class = uwp_get_layout_class();
418 ?>
419 <ul class="uwp-users-list-wrap <?php echo $layout_class; ?>" id="uwp_user_items_layout">
420 <?php
421 if ($users) {
422 foreach ($users as $user) {
423 $user_obj = get_user_by('id', $user->ID);
424
425 // exclude logged in user
426 $exclude_loggedin_user = apply_filters('uwp_users_list_exclude_loggedin_user', false);
427 if ($exclude_loggedin_user) {
428 if ($user_obj->ID == get_current_user_id()) {
429 continue;
430 }
431 }
432 ?>
433 <li class="uwp-users-list-user">
434 <div class="uwp-users-list-user-left">
435 <?php do_action('uwp_users_profile_header', $user); ?>
436 </div>
437 <div class="uwp-users-list-user-right">
438 <div class="uwp-users-list-user-name">
439 <h3 class="uwp-user-title" data-user="<?php echo $user_obj->ID; ?>">
440 <a href="<?php echo apply_filters('uwp_profile_link', get_author_posts_url($user_obj->ID), $user_obj->ID); ?>">
441 <?php echo apply_filters('uwp_profile_display_name', $user_obj->display_name); ?>
442 </a>
443 <?php do_action('uwp_users_after_title', $user_obj->ID ); ?>
444 </h3>
445 </div>
446 <div class="uwp-users-list-user-btns">
447 <?php do_action('uwp_profile_buttons', $user_obj ); ?>
448 </div>
449 <div class="uwp-users-list-user-social">
450 <?php do_action('uwp_profile_social', $user_obj ); ?>
451 </div>
452 <div class="uwp-users-list-extra">
453 <?php do_action('uwp_users_extra', $user_obj ); ?>
454 </div>
455 <div class="clfx"></div>
456 </div>
457 </li>
458 <?php
459 }
460 } else {
461 // no users found
462 echo '<div class="uwp-alert-error text-center">';
463 echo __('No Users Found', 'userswp');
464 echo '</div>';
465 }
466 ?>
467 </ul>
468
469 <?php
470 if ($total_pages > 1) {
471 do_action('uwp_profile_pagination', $total_pages);
472 }
473 ?>
474 <?php
475 }
476
477
478
479 /**
480 * Loads the font-awesome css files.
481 *
482 * @since 1.0.0
483 * @package userswp
484 *
485 * @return void
486 */
487 function uwp_load_font_awesome() {
488 //load font awesome
489 wp_register_script('font-awesome', 'https://use.fontawesome.com/releases/v5.4.1/js/all.js', array('font-awesome-shim'));
490 wp_register_script('font-awesome-shim', 'https://use.fontawesome.com/releases/v5.4.1/js/v4-shims.js', array());
491 wp_enqueue_script( 'font-awesome' );
492 }
493
494 /**
495 * Gets the custom field info for given key.
496 *
497 * @since 1.0.0
498 * @package userswp
499 *
500 * @param string $htmlvar_name Custom field key.
501 *
502 * @return object Field info.
503 */
504 function uwp_get_custom_field_info($htmlvar_name) {
505 global $wpdb;
506 $table_name = uwp_get_table_prefix() . 'uwp_form_fields';
507 $field = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . $table_name . " WHERE htmlvar_name = %s", array($htmlvar_name)));
508 return $field;
509 }
510
511
512
513 /**
514 * Returns the Users page layout class based on the setting.
515 *
516 * @since 1.0.0
517 * @package userswp
518 *
519 * @return string Layout class.
520 */
521 function uwp_get_layout_class() {
522 $default_layout = uwp_get_option('users_default_layout', 'list');
523 switch ($default_layout) {
524 case "list":
525 $class = "uwp_listview";
526 break;
527 case "2col":
528 $class = "uwp_gridview uwp_gridview_2col";
529 break;
530 case "3col":
531 $class = "uwp_gridview uwp_gridview_3col";
532 break;
533 case "4col":
534 $class = "uwp_gridview uwp_gridview_4col";
535 break;
536 case "5col":
537 $class = "uwp_gridview uwp_gridview_5col";
538 break;
539 default:
540 $class = "uwp_listview";
541 }
542
543 return $class;
544 }
545
546 add_filter( 'get_user_option_metaboxhidden_nav-menus', 'uwp_always_nav_menu_visibility', 10, 3 );
547
548 /**
549 * Filters nav menu visibility option value.
550 *
551 * @since 1.0.0
552 * @package userswp
553 *
554 * @param mixed $result Value for the user's option.
555 * @param string $option Name of the option being retrieved.
556 * @param WP_User $user WP_User object of the user whose option is being retrieved.
557 *
558 * @return array Filtered value.
559 */
560 function uwp_always_nav_menu_visibility( $result, $option, $user )
561 {
562 if( is_array($result) && in_array( 'add-users-wp-nav-menu', $result ) ) {
563 $result = array_diff( $result, array( 'add-users-wp-nav-menu' ) );
564 }
565
566 return $result;
567 }
568
569 add_filter('user_profile_picture_description', 'uwp_admin_user_profile_picture_description');
570
571 /**
572 * Filters the user profile picture description displayed under the Gravatar.
573 *
574 * @since 1.0.0
575 * @package userswp
576 *
577 * @param string $description Profile picture description.
578 *
579 * @return string Modified description.
580 */
581 function uwp_admin_user_profile_picture_description($description) {
582 if (is_admin() && IS_PROFILE_PAGE) {
583 $user_id = get_current_user_id();
584 $avatar = uwp_get_usermeta($user_id, 'uwp_account_avatar_thumb', '');
585
586 if (!empty($avatar)) {
587 $description = sprintf( __( 'You can change your profile picture on your <a href="%s">Profile Page</a>.', 'userswp' ),
588 uwp_build_profile_tab_url( $user_id ));
589 }
590
591 }
592 return $description;
593 }
594
595 /**
596 * Adds avatar and banner fields in admin side.
597 *
598 * @since 1.0.0
599 * @package userswp
600 *
601 * @param object $user User object.
602 *
603 * @return void
604 */
605 function uwp_admin_edit_banner_fields($user) {
606 global $wpdb;
607
608 $file_obj = new UsersWP_Files();
609
610 $table_name = uwp_get_table_prefix() . 'uwp_form_fields';
611 $fields = $wpdb->get_results("SELECT * FROM " . $table_name . " WHERE (form_type = 'avatar' OR form_type = 'banner') ORDER BY sort_order ASC");
612 if ($fields) {
613 ?>
614 <div class="uwp-profile-extra uwp_page">
615 <?php do_action('uwp_admin_profile_edit', $user ); ?>
616 <table class="uwp-profile-extra-table form-table">
617 <?php
618 foreach ($fields as $field) {
619
620 // Icon
621 $icon = uwp_get_field_icon( $field->field_icon );
622
623 if ($field->field_type == 'fieldset') {
624 ?>
625 <tr style="margin: 0; padding: 0">
626 <th class="uwp-profile-extra-key" style="margin: 0; padding: 0"><h3 style="margin: 10px 0;">
627 <?php echo $icon.$field->site_title; ?></h3></th>
628 <td></td>
629 </tr>
630 <?php
631 } else { ?>
632 <tr>
633 <th class="uwp-profile-extra-key"><?php echo $icon.$field->site_title; ?></th>
634 <td class="uwp-profile-extra-value">
635 <?php
636 if ($field->htmlvar_name == "uwp_avatar_file") {
637 $value = uwp_get_usermeta($user->ID, "uwp_account_avatar_thumb", "");
638 } elseif ($field->htmlvar_name == "uwp_banner_file") {
639 $value = uwp_get_usermeta($user->ID, "uwp_account_banner_thumb", "");
640 } else {
641 $value = "";
642 }
643 ?>
644 <?php echo $file_obj->uwp_file_upload_preview($field, $value); ?>
645 <?php
646 if ($field->htmlvar_name == "uwp_avatar_file") {
647 if (!empty($value)) {
648 ?>
649 <a class="uwp-profile-modal-form-trigger" data-type="avatar" href="#">
650 <?php echo __("Change Avatar", "userswp"); ?>
651 </a>
652 <?php
653 } else {
654 ?>
655 <a class="uwp-profile-modal-form-trigger" data-type="avatar" href="#">
656 <?php echo __("Upload Avatar", "userswp"); ?>
657 </a>
658 <?php
659 }
660 } elseif ($field->htmlvar_name == "uwp_banner_file") {
661 if (!empty($value)) {
662 ?>
663 <a class="uwp-profile-modal-form-trigger" data-type="banner" href="#">
664 <?php echo __("Change Banner", "userswp"); ?>
665 </a>
666 <?php
667 } else {
668 ?>
669 <a class="uwp-profile-modal-form-trigger" data-type="banner" href="#">
670 <?php echo __("Upload Banner", "userswp"); ?>
671 </a>
672 <?php
673 }
674 }
675 ?>
676 </td>
677 </tr>
678 <?php
679 }
680 }
681 ?>
682 </table>
683 </div>
684 <?php
685 }
686 }
687 add_action('show_user_profile', 'uwp_admin_edit_banner_fields');
688 add_action('edit_user_profile', 'uwp_admin_edit_banner_fields');
689
690 add_filter('admin_body_class', 'uwp_add_admin_body_class');
691 function uwp_add_admin_body_class($classes) {
692 $screen = get_current_screen();
693 if ( 'profile' == $screen->base || 'user-edit' == $screen->base )
694 $classes .= 'uwp_page';
695 return $classes;
696 }
697
698 // Privacy
699 add_filter('uwp_account_page_title', 'uwp_account_privacy_page_title', 10, 2);
700
701 /**
702 * Adds Privacy tab title in Account page.
703 *
704 * @since 1.0.0
705 * @package userswp
706 *
707 * @param string $title Privacy title.
708 * @param string $type Tab type.
709 *
710 * @return string|void Title.
711 */
712 function uwp_account_privacy_page_title($title, $type) {
713 if ($type == 'privacy') {
714 $title = __( 'Privacy', 'userswp' );
715 }
716
717 return $title;
718 }
719
720 add_action('uwp_account_form_display', 'uwp_account_privacy_edit_form_display');
721
722 /**
723 * Adds form html for privacy fields in account page.
724 *
725 * @since 1.0.0
726 * @package userswp
727 *
728 * @param string $type Form type.
729 *
730 * @return void
731 */
732 function uwp_account_privacy_edit_form_display($type) {
733 if ($type == 'privacy') {
734 $make_profile_private = uwp_can_make_profile_private();
735 echo '<div class="uwp-account-form uwp_wc_form">';
736 $extra_where = "AND is_public='2'";
737 $fields = get_account_form_fields($extra_where);
738 $fields = apply_filters('uwp_account_privacy_fields', $fields);
739 $user_id = get_current_user_id();
740 ?>
741 <div class="uwp-profile-extra">
742 <div class="uwp-profile-extra-div form-table">
743 <form class="uwp-account-form uwp_form" method="post">
744 <?php if ($fields) { ?>
745 <div class="uwp-profile-extra-wrap">
746 <div class="uwp-profile-extra-key" style="font-weight: bold;">
747 <?php echo __("Field", "userswp") ?>
748 </div>
749 <div class="uwp-profile-extra-value" style="font-weight: bold;">
750 <?php echo __("Is Public?", "userswp") ?>
751 </div>
752 </div>
753 <?php foreach ($fields as $field) { ?>
754 <div class="uwp-profile-extra-wrap">
755 <div class="uwp-profile-extra-key"><?php echo $field->site_title; ?>
756 <span class="uwp-profile-extra-sep">:</span></div>
757 <div class="uwp-profile-extra-value">
758 <?php
759 $field_name = $field->htmlvar_name . '_privacy';
760 $value = uwp_get_usermeta($user_id, $field_name, false);
761 if ($value === false) {
762 $value = 'yes';
763 }
764 ?>
765 <select name="<?php echo $field_name; ?>" class="uwp_privacy_field"
766 style="margin: 0;">
767 <option value="no" <?php selected($value, "no"); ?>><?php echo __("No", "userswp") ?></option>
768 <option value="yes" <?php selected($value, "yes"); ?>><?php echo __("Yes", "userswp") ?></option>
769 </select>
770 </div>
771 </div>
772 <?php }
773 }
774 $value = get_user_meta($user_id, 'uwp_hide_from_listing', true); ?>
775 <div class="uwp-profile-extra-wrap">
776 <div id="uwp_hide_from_listing" class="uwp_hide_from_listing">
777 <input name="uwp_hide_from_listing" class="" <?php checked($value, "1", true); ?> type="checkbox" value="1"><?php _e('Hide profile from the users listing page.', 'userswp'); ?>
778 </div>
779 </div>
780 <?php
781 do_action('uwp_after_privacy_form_fields', $fields);
782 if ($make_profile_private) {
783 $field_name = 'uwp_make_profile_private';
784 $value = get_user_meta($user_id, $field_name, true);
785 if ($value === false) {
786 $value = '0';
787 }
788 ?>
789 <div id="uwp_make_profile_private" class=" uwp_make_profile_private_row">
790 <input type="hidden" name="uwp_make_profile_private" value="0">
791 <input name="uwp_make_profile_private" class="" <?php checked( $value, "1", true ); ?> type="checkbox" value="1">
792 <?php _e( 'Make the whole profile private', 'userswp' ); ?>
793 </div>
794 <?php
795 }
796 ?>
797 <input type="hidden" name="uwp_privacy_nonce" value="<?php echo wp_create_nonce( 'uwp-privacy-nonce' ); ?>" />
798 <input name="uwp_privacy_submit" value="<?php echo __( 'Submit', 'userswp' ); ?>" type="submit">
799 </form>
800 </div>
801 </div>
802 <?php
803 echo '</div>';
804 }
805 }
806
807 add_action('uwp_account_menu_display', 'uwp_add_account_menu_links');
808
809 /**
810 * Prints "Edit account" page subtab / submenu links. Ex: Privacy
811 *
812 * @since 1.0.0
813 * @package userswp
814 *
815 * @return void
816 */
817 function uwp_add_account_menu_links() {
818
819 if (isset($_GET['type'])) {
820 $type = strip_tags(esc_sql($_GET['type']));
821 } else {
822 $type = 'account';
823 }
824
825 $account_page = uwp_get_page_id('account_page', false);
826 $account_page_link = get_permalink($account_page);
827
828 $account_available_tabs = uwp_account_get_available_tabs();
829
830 if (!is_array($account_available_tabs) && count($account_available_tabs) > 0) {
831 return;
832 }
833
834 echo '<ul class="uwp_account_menu">';
835
836 foreach( $account_available_tabs as $tab_id => $tab ) {
837
838 if ($tab_id == 'account') {
839 $tab_url = $account_page_link;
840 } else {
841 $tab_url = add_query_arg(array(
842 'type' => $tab_id,
843 ), $account_page_link);
844 }
845
846 if (isset($tab['link'])) {
847 $tab_url = $tab['link'];
848 }
849
850 $active = $type == $tab_id ? ' active' : '';
851 ?>
852 <li id="uwp-account-<?php echo $tab_id; ?>">
853 <a class="<?php echo $active; ?>" href="<?php echo esc_url( $tab_url ); ?>">
854 <i class="<?php echo $tab['icon']; ?>"></i> <?php echo $tab['title']; ?>
855 </a>
856 </li>
857 <?php
858 }
859
860 $template = new UsersWP_Templates();
861 $logout_url = $template->uwp_logout_url();
862 echo '<li id="uwp-account-logout"><a class="uwp-account-logout-link" href="'.$logout_url.'"><i class="fas fa-sign-out-alt"></i>'.__('Logout', 'userswp').'</a></li>';
863 echo '</ul>';
864 }
865
866
867
868
869 /**
870 * Updates extras fields sort order.
871 *
872 * @since 1.0.0
873 * @package userswp
874 *
875 * @param array $field_ids Form extras field ids.
876 * @param string $form_type Form type.
877 *
878 * @return array|bool Sorted field ids.
879 */
880 function uwp_form_extras_field_order($field_ids = array(), $form_type = 'register')
881 {
882 global $wpdb;
883 $extras_table_name = uwp_get_table_prefix() . 'uwp_form_extras';
884
885 $count = 0;
886 if (!empty($field_ids)):
887 foreach ($field_ids as $id) {
888
889 $cf = trim($id, '_');
890
891 $wpdb->query(
892 $wpdb->prepare(
893 "update " . $extras_table_name . " set
894 sort_order=%d
895 where id= %d",
896 array($count, $cf)
897 )
898 );
899 $count++;
900 }
901
902 return $field_ids;
903 else:
904 return false;
905 endif;
906 }
907
908 /**
909 * Uppercase the first character of each word in a string.
910 *
911 * @since 1.0.0
912 * @package userswp
913 *
914 * @param string $string String to convert.
915 * @param string $charset Charset.
916 *
917 * @return string Converted string.
918 */
919 function uwp_ucwords($string, $charset='UTF-8') {
920 if (function_exists('mb_convert_case')) {
921 return mb_convert_case($string, MB_CASE_TITLE, $charset);
922 } else {
923 return ucwords($string);
924 }
925 }
926
927 /**
928 * Checks whether the column exists in the table.
929 *
930 * @since 1.0.0
931 * @package userswp
932 *
933 * @param string $db Table name.
934 * @param string $column Column name.
935 *
936 * @return bool
937 */
938 function uwp_column_exist($db, $column)
939 {
940 $table = new UsersWP_Tables();
941 $table->column_exists($db, $column);
942 }
943
944 /**
945 * Adds column if not exist in the table.
946 *
947 * @since 1.0.0
948 * @package userswp
949 *
950 * @param string $db Table name.
951 * @param string $column Column name.
952 * @param string $column_attr Column attributes.
953 *
954 * @return bool|int True when success.
955 */
956 function uwp_add_column_if_not_exist($db, $column, $column_attr = "VARCHAR( 255 ) NOT NULL")
957 {
958 $table = new UsersWP_Tables();
959 $table->add_column_if_not_exist($db, $column, $column_attr);
960
961 }
962
963 /**
964 * Returns excluded custom fields.
965 *
966 * @since 1.0.0
967 * @package userswp
968 *
969 * @return array Excluded custom fields.
970 */
971 function uwp_get_excluded_fields() {
972 $excluded = array(
973 'uwp_account_password',
974 'uwp_account_confirm_password',
975 );
976 return $excluded;
977 }
978
979 /**
980 * Formats the currency using currency separator.
981 *
982 * @since 1.0.0
983 * @package userswp
984 *
985 * @param string $number Currency number.
986 * @param array|string $cf Custom field info.
987 *
988 * @return string Formatted currency.
989 */
990 function uwp_currency_format_number($number='',$cf=''){
991
992 $cs = isset($cf['extra_fields']) ? maybe_unserialize($cf['extra_fields']) : '';
993
994 $symbol = isset($cs['currency_symbol']) ? $cs['currency_symbol'] : '$';
995 $decimals = isset($cf['decimal_point']) && $cf['decimal_point'] ? $cf['decimal_point'] : 2;
996 $decimal_display = isset($cf['decimal_display']) && $cf['decimal_display'] ? $cf['decimal_display'] : 'if';
997 $decimalpoint = '.';
998
999 if(isset($cs['decimal_separator']) && $cs['decimal_separator']=='comma'){
1000 $decimalpoint = ',';
1001 }
1002
1003 $separator = ',';
1004
1005 if(isset($cs['thousand_separator'])){
1006 if($cs['thousand_separator']=='comma'){$separator = ',';}
1007 if($cs['thousand_separator']=='slash'){$separator = '\\';}
1008 if($cs['thousand_separator']=='period'){$separator = '.';}
1009 if($cs['thousand_separator']=='space'){$separator = ' ';}
1010 if($cs['thousand_separator']=='none'){$separator = '';}
1011 }
1012
1013 $currency_symbol_placement = isset($cs['currency_symbol_placement']) ? $cs['currency_symbol_placement'] : 'left';
1014
1015 if($decimals>0 && $decimal_display=='if'){
1016 if(is_int($number) || floor( $number ) == $number)
1017 $decimals = 0;
1018 }
1019
1020 $number = number_format($number,$decimals,$decimalpoint,$separator);
1021
1022
1023
1024 if($currency_symbol_placement=='left'){
1025 $number = $symbol . $number;
1026 }else{
1027 $number = $number . $symbol;
1028 }
1029
1030
1031 return $number;
1032 }
1033
1034
1035 /**
1036 * Checks whether the user can make his/her own profile private or not.
1037 *
1038 * @since 1.0.0
1039 * @package userswp
1040 *
1041 * @return bool
1042 */
1043 function uwp_can_make_profile_private() {
1044 $make_profile_private = apply_filters('uwp_user_can_make_profile_private', false);
1045 return $make_profile_private;
1046 }
1047
1048 /**
1049 * Returns available registration status options.
1050 *
1051 * @since 1.0.0
1052 * @package userswp
1053 *
1054 * @return array Registration status options.
1055 */
1056 function uwp_registration_status_options() {
1057 $registration_options = array(
1058 'auto_approve' => __('Auto approve', 'userswp'),
1059 'auto_approve_login' => __('Auto approve + Auto Login', 'userswp'),
1060 'require_email_activation' => __('Require Email Activation', 'userswp'),
1061 );
1062
1063 $registration_options = apply_filters('uwp_registration_status_options', $registration_options);
1064
1065 return $registration_options;
1066 }
1067
1068 /**
1069 * Retrieves a user row based on password reset key and login
1070 *
1071 * @since 1.0.0
1072 * @package userswp
1073 *
1074 * @param string $key Hash to validate sending user's password.
1075 * @param string $login The user login.
1076 *
1077 * @return WP_Error|WP_User User object.
1078 */
1079 function uwp_check_activation_key( $key, $login ) {
1080 $user_data = check_password_reset_key( $key, $login );
1081
1082 return $user_data;
1083 }
1084
1085
1086 add_action('uwp_template_fields', 'uwp_template_fields_terms_check', 100, 1);
1087
1088 /**
1089 * Adds "Accept terms and conditions" checkbox in register form.
1090 *
1091 * @since 1.0.0
1092 * @package userswp
1093 *
1094 * @param string $form_type Form type.
1095 *
1096 * @return void
1097 */
1098 function uwp_template_fields_terms_check($form_type) {
1099 if ($form_type == 'register') {
1100 $terms_page = false;
1101 $reg_terms_page_id = uwp_get_page_id('register_terms_page', false);
1102 $reg_terms_page_id = apply_filters('uwp_reg_terms_page_id', $reg_terms_page_id);
1103 if (!empty($reg_terms_page_id)) {
1104 $terms_page = get_permalink($reg_terms_page_id);
1105 }
1106 if ($terms_page) {
1107 ?>
1108 <div class="uwp-remember-me">
1109 <label style="display: inline-block;font-weight: normal" for="agree_terms">
1110 <input name="agree_terms" id="agree_terms" value="yes" type="checkbox">
1111 <?php echo sprintf( __( 'I Accept <a href="%s" target="_blank">Terms and Conditions</a>.', 'userswp' ), $terms_page); ?>
1112 </label>
1113 </div>
1114 <?php
1115 }
1116 }
1117 }
1118
1119
1120
1121 /**
1122 * Redirects the user to login page when email not confirmed.
1123 *
1124 * @since 1.0.0
1125 * @package userswp
1126 *
1127 * @param string $username Username.
1128 * @param object $user User object.
1129 *
1130 * @return void
1131 */
1132 function uwp_unconfirmed_login_redirect( $username, $user ) {
1133 if (!is_wp_error($user)) {
1134 $mod_value = get_user_meta( $user->ID, 'uwp_mod', true );
1135 if ($mod_value == 'email_unconfirmed') {
1136 if ( !in_array( 'administrator', $user->roles ) ) {
1137 $login_page = uwp_get_page_id('login_page', false);
1138 if ($login_page) {
1139 $redirect_to = add_query_arg(array('uwp_err' => 'act_pending'), get_permalink($login_page));
1140 wp_destroy_current_session();
1141 wp_clear_auth_cookie();
1142 wp_redirect($redirect_to);
1143 exit();
1144 }
1145 }
1146 }
1147 }
1148 }
1149 add_filter( 'wp_login', 'uwp_unconfirmed_login_redirect', 10, 2 );
1150
1151 /**
1152 * Adds notification menu in admin toolbar.
1153 *
1154 * @since 1.0.0
1155 * @package userswp
1156 *
1157 * @return void
1158 */
1159 function uwp_notifications_toolbar_menu() {
1160 global $wp_admin_bar;
1161
1162 if ( ! is_user_logged_in() ) {
1163 return;
1164 }
1165
1166 $available_counts = apply_filters('uwp_notifications_available_counts', array());
1167
1168 if (count($available_counts) == 0) {
1169 return;
1170 }
1171
1172 $total_count = 0;
1173 foreach ($available_counts as $key => $value) {
1174 $total_count = $total_count + $value;
1175 }
1176
1177
1178 $alert_class = (int) $total_count > 0 ? 'pending-count' : 'count';
1179 $menu_title = '<span id="uwp-notification-count" class="' . $alert_class . '">'
1180 . number_format_i18n( $total_count ) . '</span>';
1181 $menu_link = '';
1182
1183 // Add the top-level Notifications button.
1184 $wp_admin_bar->add_menu( array(
1185 'parent' => 'top-secondary',
1186 'id' => 'uwp-notifications',
1187 'title' => $menu_title,
1188 'href' => $menu_link,
1189 ) );
1190
1191 do_action('uwp_notifications_items', $wp_admin_bar);
1192
1193 return;
1194 }
1195 add_action( 'admin_bar_menu', 'uwp_notifications_toolbar_menu', 90 );
1196
1197 /**
1198 * Returns the installation type.
1199 *
1200 * @since 1.0.0
1201 * @package userswp
1202 *
1203 * @return string Installation type.
1204 */
1205 function uwp_get_installation_type() {
1206 // *. Single Site
1207 if (!is_multisite()) {
1208 return "single";
1209 } else {
1210 // Multisite
1211 if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
1212 require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
1213 }
1214
1215 // Network active.
1216 if ( is_plugin_active_for_network( 'userswp/userswp.php' ) ) {
1217 if (defined('UWP_ROOT_PAGES')) {
1218 if (UWP_ROOT_PAGES == 'all') {
1219 // *. Multisite - Network Active - Pages on all sites
1220 return "multi_na_all";
1221 } else {
1222 // *. Multisite - Network Active - Pages on specific site
1223 return "multi_na_site_id";
1224 }
1225 } else {
1226 // Multi - network active - default
1227 // *. Multisite - Network Active - Pages on main site
1228 return "multi_na_default";
1229 }
1230 } else {
1231 // * Multisite - Not network active
1232 return "multi_not_na";
1233 }
1234 }
1235 }
1236
1237 /**
1238 * Returns the table prefix based on the installation type.
1239 *
1240 * @since 1.0.0
1241 * @package userswp
1242 *
1243 * @return string Table prefix
1244 */
1245 function uwp_get_table_prefix() {
1246 $tables = new UsersWP_Tables();
1247 return $tables->get_table_prefix();
1248 }
1249
1250 /**
1251 * Returns the table prefix based on the installation type.
1252 *
1253 * @since 1.0.16
1254 * @package userswp
1255 *
1256 * @return string Table prefix
1257 */
1258 function get_usermeta_table_prefix() {
1259 $tables = new UsersWP_Tables();
1260 return $tables->get_usermeta_table_prefix();
1261 }
1262
1263
1264
1265 /**
1266 * Converts array to comma separated string.
1267 *
1268 *
1269 * @since 1.0.0
1270 * @package userswp
1271 *
1272 * @param string $key Custom field key.
1273 * @param string $value Custom field value.
1274 *
1275 * @return string Converted custom field value string.
1276 */
1277 function uwp_maybe_serialize($key, $value) {
1278 $field = uwp_get_custom_field_info($key);
1279 if (isset($field->field_type) && $field->field_type == 'multiselect') {
1280 $value = implode(",", $value);
1281 }
1282 return $value;
1283 }
1284
1285 /**
1286 * Converts comma separated string to array.
1287 *
1288 * @since 1.0.0
1289 * @package userswp
1290 *
1291 * @param string $key Custom field key.
1292 * @param string $value Custom field value.
1293 *
1294 * @return array Converted custom field value array.
1295 */
1296 function uwp_maybe_unserialize($key, $value) {
1297 $field = uwp_get_custom_field_info($key);
1298 if (isset($field->field_type) && $field->field_type == 'multiselect') {
1299 $value = explode(",", $value);
1300 }
1301 return $value;
1302 }
1303
1304 /**
1305 * Creates UsersWP related tables.
1306 *
1307 * @since 1.0.0
1308 * @package userswp
1309 *
1310 * @return void
1311 */
1312 function uwp_create_tables()
1313 {
1314 $tables = new UsersWP_Tables();
1315 $tables->uwp_create_tables();
1316 }
1317
1318 /**
1319 * Creates uwp_usermeta table which introduced in version 1.0.1
1320 *
1321 * @since 1.0.0
1322 * @package userswp
1323 *
1324 * @return void
1325 */
1326 function uwp101_create_tables() {
1327 $tables = new UsersWP_Tables();
1328 $tables->uwp101_create_tables();
1329 }
1330
1331 /**
1332 * Returns tye client IP.
1333 *
1334 * @since 1.0.0
1335 * @package userswp
1336 *
1337 * @return string IP address.
1338 */
1339 function uwp_get_ip() {
1340 if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
1341 //check ip from share internet
1342 $ip = $_SERVER['HTTP_CLIENT_IP'];
1343 } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
1344 //to check ip is pass from proxy
1345 $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
1346 } else {
1347 $ip = $_SERVER['REMOTE_ADDR'];
1348 }
1349
1350 return apply_filters('uwp_get_ip', $ip);
1351 }
1352
1353 /**
1354 * Checks whether the string starts with the given string.
1355 *
1356 * @since 1.0.0
1357 * @package userswp
1358 *
1359 * @param string $haystack String to compare with.
1360 * @param string $needle String to search for.
1361 *
1362 * @return bool True when success. False when failure.
1363 */
1364 function uwp_str_starts_with($haystack, $needle)
1365 {
1366 $length = strlen($needle);
1367 return (substr($haystack, 0, $length) === $needle);
1368 }
1369
1370 /**
1371 * Checks whether the string ends with the given string.
1372 *
1373 * @since 1.0.0
1374 * @package userswp
1375 *
1376 * @param string $haystack String to compare with.
1377 * @param string $needle String to search for.
1378 *
1379 * @return bool True when success. False when failure.
1380 */
1381 function uwp_str_ends_with($haystack, $needle)
1382 {
1383 $length = strlen($needle);
1384 if ($length == 0) {
1385 return true;
1386 }
1387
1388 return (substr($haystack, -$length) === $needle);
1389 }
1390
1391 /**
1392 * Returns the font awesome icon value for field type.
1393 * Displayed in profile tabs.
1394 *
1395 * @since 1.0.0
1396 * @package userswp
1397 *
1398 * @param string $type Field type.
1399 *
1400 * @return string Font awesome icon value.
1401 */
1402 function uwp_field_type_to_fa_icon($type) {
1403 $field_types = array(
1404 'text' => 'fas fa-minus',
1405 'datepicker' => 'fas fa-calendar-alt',
1406 'textarea' => 'fas fa-bars',
1407 'time' =>'far fa-clock',
1408 'checkbox' =>'far fa-check-square',
1409 'phone' =>'far fa-phone',
1410 'radio' =>'far fa-dot-circle',
1411 'email' =>'far fa-envelope',
1412 'select' =>'far fa-caret-square-down',
1413 'multiselect' =>'far fa-caret-square-down',
1414 'url' =>'fas fa-link',
1415 'file' =>'fas fa-file'
1416 );
1417
1418 if (isset($field_types[$type])) {
1419 return $field_types[$type];
1420 } else {
1421 return "";
1422 }
1423
1424 }
1425
1426 /**
1427 * Check wpml active or not.
1428 *
1429 * @since 1.0.7
1430 *
1431 * @return True if WPML is active else False.
1432 */
1433 function uwp_is_wpml() {
1434 if (class_exists('SitePress') && function_exists('icl_object_id')) {
1435 return true;
1436 }
1437
1438 return false;
1439 }
1440
1441 /**
1442 * Get the element in the WPML current language.
1443 *
1444 * @since 1.0.7
1445 *
1446 * @param int $element_id Use term_id for taxonomies, post_id for posts
1447 * @param string $element_type Use post, page, {custom post type name}, nav_menu, nav_menu_item, category, tag, etc.
1448 * You can also pass 'any', to let WPML guess the type, but this will only work for posts.
1449 * @param bool $return_original_if_missing Optional, default is FALSE. If set to true it will always return a value (the original value, if translation is missing).
1450 * @param string|NULL $ulanguage_code Optional, default is NULL. If missing, it will use the current language.
1451 * If set to a language code, it will return a translation for that language code or
1452 * the original if the translation is missing and $return_original_if_missing is set to TRUE.
1453 *
1454 * @return int|NULL
1455 */
1456 function uwp_wpml_object_id( $element_id, $element_type = 'post', $return_original_if_missing = false, $ulanguage_code = null ) {
1457 if ( uwp_is_wpml() ) {
1458 if ( function_exists( 'wpml_object_id_filter' ) ) {
1459 return apply_filters( 'wpml_object_id', $element_id, $element_type, $return_original_if_missing, $ulanguage_code );
1460 } else {
1461 return icl_object_id( $element_id, $element_type, $return_original_if_missing, $ulanguage_code );
1462 }
1463 }
1464
1465 return $element_id;
1466 }
1467
1468 function uwp_get_default_avatar_uri(){
1469 $default = uwp_get_option('profile_default_profile', '');
1470 if(empty($default)){
1471 $default = USERSWP_PLUGIN_URL."public/assets/images/no_profile.png";
1472 } else {
1473 $default = wp_get_attachment_url($default);
1474 }
1475
1476 return $default;
1477 }
1478
1479 function uwp_refresh_permalinks_on_bad_404() {
1480
1481 global $wp;
1482
1483 if( ! is_404() ) {
1484 return;
1485 }
1486
1487 if( isset( $_GET['uwp-flush'] ) ) {
1488 return;
1489 }
1490
1491 if( false === get_transient( 'uwp_refresh_404_permalinks' ) ) {
1492
1493 flush_rewrite_rules( false );
1494
1495 set_transient( 'uwp_refresh_404_permalinks', 1, HOUR_IN_SECONDS * 12 );
1496
1497 wp_redirect( home_url( add_query_arg( array( 'uwp-flush' => 1 ), $wp->request ) ) ); exit;
1498
1499 }
1500 }
1501 add_action( 'template_redirect', 'uwp_refresh_permalinks_on_bad_404' );
1502
1503 add_filter( 'avatar_defaults', 'uwp_avatar_defaults' , 99999 , 6 );
1504 /*
1505 * Remove get_avatar filter applied by UWP for default avatars in settings
1506 * @param array $avatar_defaults default avatars
1507 * @return array $avatar_defaults default avatars
1508 *
1509 */
1510 function uwp_avatar_defaults($avatar_defaults){
1511 remove_filter('get_avatar', 'uwp_modify_get_avatar', 99999, 6);
1512 return $avatar_defaults;
1513 }
1514
1515 remove_all_filters('get_avatar');
1516 add_filter( 'get_avatar', 'uwp_modify_get_avatar' , 99999 , 6 );
1517 /**
1518 * Modifies get_avatar function to use userswp avatar.
1519 *
1520 * @since 1.0.0
1521 * @package userswp
1522 * @param string $avatar img tag value for the user's avatar.
1523 * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
1524 * user email, WP_User object, WP_Post object, or WP_Comment object.
1525 * @param int $size Square avatar width and height in pixels to retrieve.
1526 * @param string $default URL for the default image or a default type. Accepts '404', 'retro', 'monsterid',
1527 * 'wavatar', 'indenticon','mystery' (or 'mm', or 'mysteryman'), 'blank', or 'gravatar_default'.
1528 * Default is the value of the 'avatar_default' option, with a fallback of 'mystery'.
1529 * @param string $alt Alternative text to use in the avatar image tag. Default empty.
1530 * @return string Modified img tag value
1531 */
1532 function uwp_modify_get_avatar( $avatar, $id_or_email, $size, $default, $alt, $args )
1533 {
1534 $user = false;
1535
1536 if (is_numeric($id_or_email)) {
1537
1538 $id = (int)$id_or_email;
1539 $user = get_user_by('id', $id);
1540
1541 } elseif (is_object($id_or_email)) {
1542
1543 if (!empty($id_or_email->user_id)) {
1544 $id = (int)$id_or_email->user_id;
1545 $user = get_user_by('id', $id);
1546 }
1547
1548 } else {
1549 $user = get_user_by('email', $id_or_email);
1550 }
1551
1552 if ($user && is_object($user)) {
1553 $avatar_thumb = uwp_get_usermeta($user->data->ID, 'uwp_account_avatar_thumb', '');
1554 if (!empty($avatar_thumb)) {
1555 $uploads = wp_upload_dir();
1556 $upload_url = $uploads['baseurl'];
1557 if (substr($avatar_thumb, 0, 4) !== "http") {
1558 $avatar_thumb = $upload_url . $avatar_thumb;
1559 }
1560 $avatar = "<img alt='{$alt}' src='{$avatar_thumb}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
1561 } else {
1562 $default = uwp_get_default_avatar_uri();
1563 $args = get_avatar_data($id_or_email, $args);
1564 $url = $args['url'];
1565 $url = remove_query_arg('d', $url);
1566 $url = add_query_arg(array('d' => $default), $url);
1567 if (!$url || is_wp_error($url)) {
1568 return $avatar;
1569 }
1570 $avatar = '<img src="' . $url . '" class="gravatar avatar avatar-' . $size . ' uwp-avatar" width="' . $size . '" height="' . $size . '" alt="' . $alt . '" />';
1571 }
1572
1573 }
1574
1575 return $avatar;
1576 }
1577
1578 /**
1579 * Handles multisite upload dir path
1580 *
1581 * @param $uploads array upload variable array
1582 *
1583 * @return array updated upload variable array.
1584 */
1585 function uwp_handle_multisite_profile_image($uploads){
1586 if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
1587 require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
1588 }
1589
1590 // Network active.
1591 if ( is_plugin_active_for_network( 'userswp/userswp.php' ) ) {
1592 $main_site = get_network()->site_id;
1593 switch_to_blog( $main_site );
1594 remove_filter( 'upload_dir', 'uwp_handle_multisite_profile_image');
1595 $uploads = wp_upload_dir();
1596 restore_current_blog();
1597 }
1598
1599 return $uploads;
1600 }
1601
1602 /**
1603 * let_to_num function.
1604 *
1605 * This function transforms the php.ini notation for numbers (like '2M') to an integer.
1606 *
1607 * @since 2.0.0
1608 * @param $size
1609 * @return int
1610 */
1611 function uwp_let_to_num( $size ) {
1612 $l = substr( $size, -1 );
1613 $ret = substr( $size, 0, -1 );
1614 switch ( strtoupper( $l ) ) {
1615 case 'P':
1616 $ret *= 1024;
1617 case 'T':
1618 $ret *= 1024;
1619 case 'G':
1620 $ret *= 1024;
1621 case 'M':
1622 $ret *= 1024;
1623 case 'K':
1624 $ret *= 1024;
1625 }
1626 return $ret;
1627 }
1628
1629 function uwp_format_decimal($number, $dp = false, $trim_zeros = false){
1630 $locale = localeconv();
1631 $decimals = array( uwp_get_decimal_separator(), $locale['decimal_point'], $locale['mon_decimal_point'] );
1632
1633 // Remove locale from string.
1634 if ( ! is_float( $number ) ) {
1635 $number = str_replace( $decimals, '.', $number );
1636 $number = preg_replace( '/[^0-9\.,-]/', '', uwp_clean( $number ) );
1637 }
1638
1639 if ( false !== $dp ) {
1640 $dp = intval( '' == $dp ? uwp_get_decimal_separator() : $dp );
1641 $number = number_format( floatval( $number ), $dp, '.', '' );
1642 // DP is false - don't use number format, just return a string in our format
1643 } elseif ( is_float( $number ) ) {
1644 // DP is false - don't use number format, just return a string using whatever is given. Remove scientific notation using sprintf.
1645 $number = str_replace( $decimals, '.', sprintf( '%.' . uwp_get_rounding_precision() . 'f', $number ) );
1646 // We already had a float, so trailing zeros are not needed.
1647 $trim_zeros = true;
1648 }
1649
1650 if ( $trim_zeros && strstr( $number, '.' ) ) {
1651 $number = rtrim( rtrim( $number, '0' ), '.' );
1652 }
1653
1654 return $number;
1655 }
1656
1657 /**
1658 * Return the decimal separator.
1659 * @since 1.0.20
1660 * @return string
1661 */
1662 function uwp_get_decimal_separator() {
1663 $separator = apply_filters( 'uwp_decimal_separator', '.' );
1664 return $separator ? stripslashes( $separator ) : '.';
1665 }
1666
1667 /**
1668 * Get rounding precision for internal UWP calculations.
1669 * Will increase the precision of uwp_get_decimal_separator by 2 decimals, unless UWP_ROUNDING_PRECISION is set to a higher number.
1670 *
1671 * @since 1.0.20
1672 * @return int
1673 */
1674 function uwp_get_rounding_precision() {
1675 $precision = uwp_get_decimal_separator() + 2;
1676 if ( absint( UWP_ROUNDING_PRECISION ) > $precision ) {
1677 $precision = absint( UWP_ROUNDING_PRECISION );
1678 }
1679 return $precision;
1680 }
1681
1682 /**
1683 * Clean variables using sanitize_text_field. Arrays are cleaned recursively.
1684 * Non-scalar values are ignored.
1685 *
1686 * @param string|array $var
1687 *
1688 * @return string|array
1689 */
1690 function uwp_clean( $var ) {
1691
1692 if ( is_array( $var ) ) {
1693 return array_map( 'uwp_clean', $var );
1694 } else {
1695 return is_scalar( $var ) ? sanitize_text_field( $var ) : $var;
1696 }
1697
1698 }