PluginProbe
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP / 1.2.43
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP v1.2.43
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.2.43, at includes/helpers/misc.php

1,898 lines 51.6 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( stripslashes( $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 * Resizes thumbnail image.
121 *
122 * @since 1.0.0
123 * @package userswp
124 *
125 * @param string $thumb_image_name
126 * @param string $image
127 * @param int $x x-coordinate of source point.
128 * @param int $y y-coordinate of source point.
129 * @param int $src_w Source width.
130 * @param int $src_h Source height.
131 * @param float $scale Image scale ratio.
132 *
133 * @return mixed Resized image.
134 */
135 function uwp_resizeThumbnailImage( $thumb_image_name, $image, $x, $y, $src_w, $src_h, $scale, $wp_error = false ) {
136 uwp_set_php_limits();
137
138 // Ignore image creation warnings
139 @ini_set( 'gd.jpeg_ignore_warning', 1 );
140
141 $newImageWidth = ceil( $src_w * $scale );
142 $newImageHeight = ceil( $src_h * $scale );
143
144 /** @noinspection PhpUnusedLocalVariableInspection */
145 list( $imagewidth, $imageheight, $imageType ) = getimagesize( $image );
146 $imageType = image_type_to_mime_type( $imageType );
147
148 if ( function_exists( 'wp_crop_image' ) ) {
149 $wp_crop = ! in_array( $imageType, array( 'image/gif', 'image/pjpeg', 'image/jpeg', 'image/jpg', 'image/png', 'image/x-png', 'image/webp' ) ) ? true : false;
150 $wp_crop = apply_filters( 'uwp_resize_image_use_wp_crop_image', $wp_crop, $imageType, $image, $thumb_image_name, $x, $y, $src_w, $src_h, $scale );
151
152 if ( $wp_crop ) {
153 $cropped = wp_crop_image( $image, $x, $y, $src_w, $src_h, $newImageWidth, $newImageHeight, false, $thumb_image_name );
154
155 if ( $wp_error && is_wp_error( $cropped ) ) {
156 return $cropped;
157 }
158
159 return $thumb_image_name;
160 }
161 }
162
163 $newImage = imagecreatetruecolor( $newImageWidth, $newImageHeight );
164 $source = false;
165
166 switch( $imageType ) {
167 case "image/gif":
168 $source = imagecreatefromgif( $image );
169 break;
170 case "image/pjpeg":
171 case "image/jpeg":
172 case "image/jpg":
173 $source = imagecreatefromjpeg( $image );
174 break;
175 case "image/png":
176 case "image/x-png":
177 $source = imagecreatefrompng( $image );
178 if ( apply_filters( 'uwp_keep_png_transperent', true, $thumb_image_name, $image, $x, $y, $src_w, $src_h ) ) {
179 $background = imagecolorallocate( $newImage, 0, 0, 0 );
180
181 imagecolortransparent( $newImage, $background );
182 imagealphablending( $newImage, false );
183 imagesavealpha( $newImage, true );
184 }
185 break;
186 case "image/webp":
187 if (function_exists('imagecreatefromwebp')) {
188 $source = imagecreatefromwebp($image);
189 imagealphablending($newImage, false);
190 imagesavealpha($newImage, true);
191 }
192 break;
193 }
194
195 imagecopyresampled( $newImage, $source, 0, 0, $x, $y, $newImageWidth, $newImageHeight, $src_w, $src_h );
196
197 $quality = apply_filters( 'uwp_resize_thumb_quality', 100 );
198
199 switch( $imageType ) {
200 case "image/gif":
201 imagegif( $newImage, $thumb_image_name );
202 break;
203 case "image/pjpeg":
204 case "image/jpeg":
205 case "image/jpg":
206 imagejpeg( $newImage, $thumb_image_name, $quality );
207 break;
208 case "image/png":
209 case "image/x-png":
210 imagepng( $newImage, $thumb_image_name );
211 break;
212 case "image/webp":
213 if (function_exists('imagewebp')) {
214 imagewebp( $newImage, $thumb_image_name, $quality );
215 } else {
216 imagepng( $newImage, $thumb_image_name );
217 }
218 break;
219 }
220
221 chmod( $thumb_image_name, 0777 );
222 imagedestroy($newImage);
223 imagedestroy($source);
224
225 return $thumb_image_name;
226 }
227
228 /**
229 * Try to set higher limits on the fly
230 */
231 function uwp_set_php_limits() {
232 error_reporting( 0 );
233
234 // try to set higher limits for import
235 $max_input_time = ini_get( 'max_input_time' );
236 $max_execution_time = ini_get( 'max_execution_time' );
237 $memory_limit = ini_get( 'memory_limit' );
238
239 if ( $max_input_time !== 0 && $max_input_time != -1 && ( ! $max_input_time || $max_input_time < 3000 ) ) {
240 ini_set( 'max_input_time', 3000 );
241 }
242
243 if ( $max_execution_time !== 0 && ( ! $max_execution_time || $max_execution_time < 3000 ) ) {
244 ini_set( 'max_execution_time', 3000 );
245 }
246
247 if ( $memory_limit && str_replace( 'M', '', $memory_limit ) ) {
248 if ( str_replace( 'M', '', $memory_limit ) < 256 ) {
249 ini_set( 'memory_limit', '256M' );
250 }
251 }
252
253 ini_set( 'auto_detect_line_endings', true );
254 }
255
256 /**
257 * Logs the error message.
258 *
259 * @since 1.0.0
260 * @package userswp
261 *
262 * @param array|object|string $log Error message.
263 *
264 * @return void
265 */
266 function uwp_error_log($log){
267 /*
268 * A filter to override the debugging setting for function uwp_error_log().
269 */
270 $should_log = apply_filters( 'uwp_log_errors', uwp_get_option('enable_uwp_error_log', 0));
271 if ( 1 == $should_log ) {
272 if ( is_array( $log ) || is_object( $log ) ) {
273 error_log( print_r( $log, true ) );
274 } else {
275 error_log( $log );
276 }
277 }
278 }
279
280 function uwp_get_excluded_users_list() {
281
282 $args = array(
283 'fields' => 'ID',
284 'meta_query' => array(
285 'relation' => 'OR',
286 array(
287 'key' => 'uwp_mod',
288 'value' => 'email_unconfirmed',
289 'compare' => '=='
290 ),
291 array(
292 'key' => 'uwp_hide_from_listing',
293 'value' => 1,
294 'compare' => '=='
295 )
296 )
297 );
298
299 $inactive_users = new WP_User_Query($args);
300 $exclude_users = $inactive_users->get_results();
301
302 $excluded_globally = uwp_get_option('users_excluded_from_list');
303 if ( !empty($excluded_globally) ) {
304
305 if(is_array($excluded_globally)) {
306 $exclude_users = array_merge($exclude_users, $excluded_globally);
307 } else {
308 $excluded_users = str_replace(' ', '', $excluded_globally);
309 $users_array = explode(',', $excluded_users);
310 $exclude_users = array_merge($exclude_users, $users_array);
311 }
312 }
313
314 return $exclude_users;
315 }
316
317 /**
318 * Prints the users page main content.
319 *
320 * @since 1.0.0
321 * @package userswp
322 *
323 * @return array $users array of users
324 */
325 function get_uwp_users_list($roles = array()) {
326
327 global $wpdb;
328
329 $keyword = false;
330 if (isset($_GET['uwps']) && $_GET['uwps'] != '') {
331 $keyword = stripslashes(strip_tags($_GET['uwps']));
332 }
333
334 if ( is_front_page() ) {
335 $paged = ( get_query_var( 'page' ) ) ? absint( get_query_var( 'page' ) ) : 1;
336 } else {
337 $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
338 }
339
340 $number = uwp_get_option('users_no_of_items', 10);
341 $number = !empty($number) ? $number : 10;
342
343 $where = '';
344 $where = apply_filters('uwp_users_search_where', $where, $keyword);
345
346 $exclude_users = uwp_get_excluded_users_list();
347 $exclude_users = apply_filters('uwp_excluded_users_from_list', $exclude_users, $where, $keyword);
348 $exclude_users = !empty($exclude_users) ? array_unique($exclude_users): array();
349
350 $exclude_query = ' ';$order_by = 'uwp_meta_value'; $order = 'ASC';
351
352 if (isset($_GET['uwp_sort_by']) && $_GET['uwp_sort_by'] != '') {
353 $sort_by = strip_tags(esc_sql($_GET['uwp_sort_by']));
354 } else {
355 $sort_by = '';
356 }
357
358 if ($sort_by) {
359 switch ( $sort_by ) {
360 case "newer":
361 $order_by = 'registered';
362 $order = 'DESC';
363 break;
364 case "older":
365 $order_by = 'registered';
366 $order = 'ASC';
367 break;
368 }
369 }
370
371 if(!empty($exclude_users)) {
372 $exclude_users_list = implode(',', $exclude_users);
373 $exclude_query = 'AND '. $wpdb->users.'.ID NOT IN ('.$exclude_users_list.')';
374 }
375
376 $users = array();
377
378 if($keyword || $where ) {
379
380 if (empty($where)) {
381 $user_query = $wpdb->prepare("SELECT DISTINCT SQL_CALC_FOUND_ROWS $wpdb->users.*
382 FROM $wpdb->users
383 INNER JOIN $wpdb->usermeta
384 ON ( $wpdb->users.ID = $wpdb->usermeta.user_id )
385 WHERE 1=1
386 $exclude_query
387 AND (
388 ( $wpdb->usermeta.meta_key = 'first_name' AND $wpdb->usermeta.meta_value LIKE %s )
389 OR
390 ( $wpdb->usermeta.meta_key = 'last_name' AND $wpdb->usermeta.meta_value LIKE %s )
391 OR user_login LIKE %s OR user_nicename LIKE %s OR display_name LIKE %s
392 )
393 ORDER BY display_name ASC",
394 array(
395 '%' . $keyword . '%',
396 '%' . $keyword . '%',
397 '%' . $keyword . '%',
398 '%' . $keyword . '%',
399 '%' . $keyword . '%',
400 )
401 );
402 } else{
403 $usermeta_table = get_usermeta_table_prefix() . 'uwp_usermeta';
404 $keyword_query = '';
405
406 if($keyword) {
407 $keyword_query = " AND (( $wpdb->usermeta.meta_key = 'first_name' AND $wpdb->usermeta.meta_value LIKE '$keyword' )
408 OR ( $wpdb->usermeta.meta_key = 'last_name' AND $wpdb->usermeta.meta_value LIKE '$keyword' )
409 OR 'user_login' LIKE '$keyword' OR 'user_nicename' LIKE '$keyword' OR 'display_name' LIKE '$keyword')";
410 }
411
412 $user_query = "SELECT DISTINCT SQL_CALC_FOUND_ROWS $wpdb->users.* FROM $wpdb->users
413 INNER JOIN $wpdb->usermeta ON ( $wpdb->users.ID = $wpdb->usermeta.user_id )
414 INNER JOIN $usermeta_table ON ( $wpdb->users.ID = $usermeta_table.user_id )
415 WHERE 1=1 $keyword_query $exclude_query $where ORDER BY display_name ASC";
416 }
417
418 $user_results = $wpdb->get_results($user_query); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
419 $get_users = wp_list_pluck($user_results, 'ID');
420
421 if(isset($roles) && is_array($roles) && count($roles) > 0){
422 $users = get_users( array( 'role__in' => $roles, 'fields' => array('ID') ) );
423 $users = wp_list_pluck( $users, 'ID' );
424 if($get_users && count($get_users) > 0 && $users && count($users) > 0){
425 foreach ($get_users as $key => $get_user){
426 if(!in_array($get_user, $users)){
427 unset($get_users[$key]);
428 }
429 }
430 }
431 }
432
433 if(!empty($get_users) && is_array($get_users) && count($get_users) > 0){
434
435 $args = array(
436 'include' => $get_users,
437 'number' => (int) $number,
438 'paged' => (int) $paged,
439 );
440
441 if(!empty($exclude_users)) {
442 $args['exclude'] = $exclude_users;
443 }
444
445 if(!empty($meta_key)) {
446 $args['meta_key'] = $meta_key;
447 }
448
449 if(!empty($order_by) && !empty($order) ) {
450 $args['orderby'] = $order_by;
451 $args['order'] = $order;
452 }
453
454 $uwp_users_query = new WP_User_Query($args);
455 $users['users'] = $uwp_users_query->get_results();
456 $users['total_users'] = $uwp_users_query->get_total();
457
458 } else {
459 $users['users'] = array();
460 $users['total_users'] = 0;
461 }
462
463 } else {
464 $args = array(
465 'number' => (int) $number,
466 'paged' => (int) $paged,
467 );
468
469 $roles_args = array(
470 'fields' => array('ID'),
471 );
472
473 if(!empty($exclude_users)) {
474 $args['exclude'] = $exclude_users;
475 $roles_args['exclude'] = $exclude_users;
476 }
477
478 if(isset($roles) && is_array($roles) && count($roles) > 0){
479 $include_users = array();
480 $roles_args['role__in'] = $roles;
481 $users = get_users( $roles_args );
482 $users = wp_list_pluck( $users, 'ID' );
483 if($users && count($users) > 0){
484 $include_users = array_merge($include_users, $users);
485 $args['include'] = $include_users;
486 }
487 }
488
489 if(!empty($meta_key)) {
490 $args['meta_key'] = $meta_key;
491 }
492
493 if(!empty($order_by) && !empty($order) ) {
494 $args['orderby'] = $order_by;
495 $args['order'] = $order;
496 }
497
498 $uwp_users_query = new WP_User_Query($args);
499 $users['users'] = $uwp_users_query->get_results();
500 $users['total_users'] = $uwp_users_query->get_total();
501 }
502
503 return $users;
504
505 }
506
507 /**
508 * Returns the Users page layout class based on the setting.
509 *
510 * @since 1.0.0
511 * @package userswp
512 *
513 * @return string Layout class.
514 */
515 function uwp_get_layout_class($layout, $count_only = false) {
516 if(!$layout){
517 if(uwp_get_option("design_style",'bootstrap')){
518 $value = '3col';
519 } else {
520 $value = 'list';
521 }
522 $layout = uwp_get_option('users_default_layout', $value);
523 }
524
525 switch ($layout) {
526 case "list":
527 $class = "uwp_listview";
528 $bs_class = "row-cols-md-1";
529 $col_count = 1;
530 break;
531 case "2col":
532 $class = "uwp_gridview uwp_gridview_2col";
533 $bs_class = "row-cols-md-2";
534 $col_count = 2;
535 break;
536 case "3col":
537 $class = "uwp_gridview uwp_gridview_3col";
538 $bs_class = "row-cols-md-3";
539 $col_count = 3;
540 break;
541 case "4col":
542 $class = "uwp_gridview uwp_gridview_4col";
543 $bs_class = "row-cols-md-4";
544 $col_count = 4;
545 break;
546 case "5col":
547 $class = "uwp_gridview uwp_gridview_5col";
548 $bs_class = "row-cols-md-5";
549 $col_count = 5;
550 break;
551 default:
552 $class = "uwp_listview";
553 $bs_class = "row-cols-md-3";
554 $col_count = 1;
555 }
556
557 if($count_only){
558 return $col_count;
559 }
560
561 if(uwp_get_option("design_style",'bootstrap')){
562 return $bs_class;
563 }
564
565 return $class;
566 }
567
568 add_filter( 'uwp_users_list_ul_extra_class', 'uwp_get_layout_class', 10, 1 );
569
570 add_filter( 'get_user_option_metaboxhidden_nav-menus', 'uwp_always_nav_menu_visibility', 10, 3 );
571
572 /**
573 * Filters nav menu visibility option value.
574 *
575 * @since 1.0.0
576 * @package userswp
577 *
578 * @param mixed $result Value for the user's option.
579 * @param string $option Name of the option being retrieved.
580 * @param WP_User $user WP_User object of the user whose option is being retrieved.
581 *
582 * @return array Filtered value.
583 */
584 function uwp_always_nav_menu_visibility( $result, $option, $user )
585 {
586 if( is_array($result) && in_array( 'add-users-wp-nav-menu', $result ) ) {
587 $result = array_diff( $result, array( 'add-users-wp-nav-menu' ) );
588 }
589
590 return $result;
591 }
592
593 // Privacy
594 add_filter('uwp_account_page_title', 'uwp_account_privacy_page_title', 10, 2);
595
596 /**
597 * Adds Privacy tab title in Account page.
598 *
599 * @since 1.0.0
600 * @package userswp
601 *
602 * @param string $title Privacy title.
603 * @param string $type Tab type.
604 *
605 * @return string Title.
606 */
607 function uwp_account_privacy_page_title($title, $type) {
608
609 if ($type == 'privacy') {
610 $title = __( 'Privacy', 'userswp' );
611 } elseif ($type == 'notifications') {
612 $title = __( 'E-Mail Notifications', 'userswp' );
613 } elseif ($type == 'delete-account') {
614 $title = __( 'Delete Account', 'userswp' );
615 } elseif ($type == 'change-password') {
616 $title = __( 'Change Password', 'userswp' );
617 } elseif ($type == 'wp2fa') {
618 $title = __( 'Two-factor Authentication Settings', 'userswp' );
619 }
620
621 return $title;
622 }
623
624 add_action('uwp_account_menu_display', 'uwp_add_account_menu_links');
625
626 /**
627 * Prints "Edit account" page subtab / submenu links. Ex: Privacy
628 *
629 * @since 1.0.0
630 * @package userswp
631 *
632 * @return void
633 */
634 function uwp_add_account_menu_links() {
635 global $aui_bs5;
636
637 if (isset($_GET['type'])) {
638 $type = strip_tags(esc_sql($_GET['type']));
639 } else {
640 $type = 'account';
641 }
642
643 $account_page = uwp_get_page_id('account_page', false);
644 $account_page_link = get_permalink($account_page);
645
646 $account_available_tabs = uwp_account_get_available_tabs();
647
648 if (!is_array($account_available_tabs) && count($account_available_tabs) > 0) {
649 return;
650 }
651
652 $legacy = '<ul class="uwp_account_menu">';
653 ob_start();
654 ?>
655 <ul class="<?php echo $aui_bs5 ? 'nav' : 'navbar-nav'; ?> m-0 p-0 mt-3 list-unstyled flex-lg-column flex-row flex-wrap" aria-labelledby="account_settings">
656 <?php
657 foreach( $account_available_tabs as $tab_id => $tab ) {
658
659 if ($tab_id == 'account') {
660 $tab_url = $account_page_link;
661 } else {
662 $tab_url = add_query_arg(array(
663 'type' => $tab_id,
664 ), $account_page_link);
665 }
666
667 if (isset($tab['link'])) {
668 $tab_url = $tab['link'];
669 }
670
671 $active = $type == $tab_id ? ' active' : '';
672
673 ?>
674 <li class="nav-item m-0 p-0 list-unstyled mx-md-2 mx-2">
675 <a class="nav-link text-decoration-none uwp-account-<?php echo esc_attr( $tab_id.' '.$active ); ?>" href="<?php echo esc_url( $tab_url ); ?>"><?php echo '<i class="'.esc_attr($tab["icon"]).' mr-1 fa-fw"></i>'.esc_html($tab['title']); ?></a>
676 </li>
677 <?php
678
679 $legacy .= '<li id="uwp-account-'.$tab_id.'">';
680 $legacy .= '<a class="'.$active.'" href="'.esc_url( $tab_url ).'">';
681 $legacy .= '<i class="'.esc_attr($tab["icon"]).'"></i>'.esc_html($tab["title"]);
682 $legacy .= '</a></li>';
683 }
684 ?>
685 </ul>
686 <?php
687 $legacy .= '</ul>';
688 $bs_output = ob_get_clean();
689 $style = uwp_get_option('design_style', 'bootstrap');
690 if(!empty($style)){
691 echo $bs_output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
692 } else {
693 echo $legacy; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
694 }
695 }
696
697 /**
698 * Updates extras fields sort order.
699 *
700 * @since 1.0.0
701 * @package userswp
702 *
703 * @param array $field_ids Form extras field ids.
704 * @param string $form_type Form type.
705 * @param int $form_id Form ID.
706 *
707 * @return array|bool Sorted field ids.
708 */
709 function uwp_form_extras_field_order($field_ids = array(), $form_type = 'register', $form_id = 1)
710 {
711 global $wpdb;
712 $extras_table_name = uwp_get_table_prefix() . 'uwp_form_extras';
713
714 $count = 0;
715 if (!empty($field_ids)):
716 foreach ($field_ids as $id) {
717
718 $cf = trim($id, '_');
719
720 $wpdb->update( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
721 $extras_table_name,
722 array(
723 'sort_order' => $count,
724 ),
725 array( 'id' => $cf, 'form_id' => $form_id )
726 );
727
728 $count++;
729 }
730
731 return $field_ids;
732 else:
733 return false;
734 endif;
735 }
736
737 /**
738 * Uppercase the first character of each word in a string.
739 *
740 * @since 1.0.0
741 * @package userswp
742 *
743 * @param string $string String to convert.
744 * @param string $charset Charset.
745 *
746 * @return string Converted string.
747 */
748 function uwp_ucwords($string, $charset='UTF-8') {
749 if (function_exists('mb_convert_case')) {
750 return mb_convert_case($string, MB_CASE_TITLE, $charset);
751 } else {
752 return ucwords($string);
753 }
754 }
755
756 /**
757 * Checks whether the column exists in the table.
758 *
759 * @since 1.0.0
760 * @package userswp
761 *
762 * @param string $db Table name.
763 * @param string $column Column name.
764 *
765 * @return bool
766 */
767 function uwp_column_exist($db, $column)
768 {
769 $table = new UsersWP_Tables();
770 return $table->column_exists($db, $column);
771 }
772
773 /**
774 * Adds column if not exist in the table.
775 *
776 * @since 1.0.0
777 * @package userswp
778 *
779 * @param string $db Table name.
780 * @param string $column Column name.
781 * @param string $column_attr Column attributes.
782 *
783 * @return bool|int True when success.
784 */
785 function uwp_add_column_if_not_exist($db, $column, $column_attr = "VARCHAR( 255 ) NOT NULL")
786 {
787 $table = new UsersWP_Tables();
788 return $table->add_column_if_not_exist($db, $column, $column_attr);
789
790 }
791
792 /**
793 * Returns excluded custom fields.
794 *
795 * @since 1.0.0
796 * @package userswp
797 *
798 * @return array Excluded custom fields.
799 */
800 function uwp_get_excluded_fields() {
801 $excluded = array(
802 'password',
803 'confirm_password',
804 'user_privacy',
805 );
806 return apply_filters('uwp_excluded_fields',$excluded);
807 }
808
809 /**
810 * Formats the currency using currency separator.
811 *
812 * @since 1.0.0
813 * @package userswp
814 *
815 * @param string $number Currency number.
816 * @param array|string $cf Custom field info.
817 *
818 * @return string Formatted currency.
819 */
820 function uwp_currency_format_number($number='',$cf=''){
821
822 $cs = isset($cf['extra_fields']) ? maybe_unserialize($cf['extra_fields']) : '';
823
824 $symbol = isset($cs['currency_symbol']) ? $cs['currency_symbol'] : '$';
825 $decimals = isset($cf['decimal_point']) && $cf['decimal_point'] ? $cf['decimal_point'] : 2;
826 $decimal_display = isset($cf['decimal_display']) && $cf['decimal_display'] ? $cf['decimal_display'] : 'if';
827 $decimalpoint = '.';
828
829 if(isset($cs['decimal_separator']) && $cs['decimal_separator']=='comma'){
830 $decimalpoint = ',';
831 }
832
833 $separator = ',';
834
835 if(isset($cs['thousand_separator'])){
836 if($cs['thousand_separator']=='comma'){$separator = ',';}
837 if($cs['thousand_separator']=='slash'){$separator = '\\';}
838 if($cs['thousand_separator']=='period'){$separator = '.';}
839 if($cs['thousand_separator']=='space'){$separator = ' ';}
840 if($cs['thousand_separator']=='none'){$separator = '';}
841 }
842
843 $currency_symbol_placement = isset($cs['currency_symbol_placement']) ? $cs['currency_symbol_placement'] : 'left';
844
845 if($decimals>0 && $decimal_display=='if'){
846 if(is_int($number) || floor( $number ) == $number)
847 $decimals = 0;
848 }
849
850 $number = number_format($number,$decimals,$decimalpoint,$separator);
851
852
853
854 if($currency_symbol_placement=='left'){
855 $number = $symbol . $number;
856 }else{
857 $number = $number . $symbol;
858 }
859
860
861 return $number;
862 }
863
864
865 /**
866 * Checks whether the user can make his/her own profile private or not.
867 *
868 * @since 1.0.0
869 * @package userswp
870 *
871 * @return bool
872 */
873 function uwp_can_make_profile_private() {
874 $make_profile_private = apply_filters('uwp_user_can_make_profile_private', false);
875 return $make_profile_private;
876 }
877
878 /**
879 * Returns the installation type.
880 *
881 * @since 1.0.0
882 * @package userswp
883 *
884 * @return string Installation type.
885 */
886 function uwp_get_installation_type() {
887 // *. Single Site
888 if (!is_multisite()) {
889 return "single";
890 } else {
891 // Multisite
892 if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
893 require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
894 }
895
896 // Network active.
897 if ( is_plugin_active_for_network( 'userswp/userswp.php' ) ) {
898 if (defined('UWP_ROOT_PAGES')) {
899 if (UWP_ROOT_PAGES == 'all') {
900 // *. Multisite - Network Active - Pages on all sites
901 return "multi_na_all";
902 } else {
903 // *. Multisite - Network Active - Pages on specific site
904 return "multi_na_site_id";
905 }
906 } else {
907 // Multi - network active - default
908 // *. Multisite - Network Active - Pages on main site
909 return "multi_na_default";
910 }
911 } else {
912 // * Multisite - Not network active
913 return "multi_not_na";
914 }
915 }
916 }
917
918 /**
919 * Returns the table prefix based on the installation type.
920 *
921 * @since 1.0.0
922 * @package userswp
923 *
924 * @return string Table prefix
925 */
926 function uwp_get_table_prefix() {
927 $tables = new UsersWP_Tables();
928 return $tables->get_table_prefix();
929 }
930
931 /**
932 * Returns the table prefix based on the installation type.
933 *
934 * @since 1.0.16
935 * @package userswp
936 *
937 * @return string Table prefix
938 */
939 function get_usermeta_table_prefix() {
940 $tables = new UsersWP_Tables();
941 return $tables->get_usermeta_table_prefix();
942 }
943
944 /**
945 * Converts array to comma separated string.
946 *
947 *
948 * @since 1.0.0
949 * @package userswp
950 *
951 * @param string $key Custom field key.
952 * @param string $value Custom field value.
953 *
954 * @return string Converted custom field value string.
955 */
956 function uwp_maybe_serialize($key, $value) {
957 $field = uwp_get_custom_field_info($key);
958 if (isset($field->field_type) && $field->field_type == 'multiselect' && is_array($value)) {
959 $value = implode(",", $value);
960 }
961 return $value;
962 }
963
964 /**
965 * Converts comma separated string to array.
966 *
967 * @since 1.0.0
968 * @package userswp
969 *
970 * @param string $key Custom field key.
971 * @param string $value Custom field value.
972 *
973 * @return array Converted custom field value array.
974 */
975 function uwp_maybe_unserialize($key, $value) {
976 $field = uwp_get_custom_field_info($key);
977 if (isset($field->field_type) && $field->field_type == 'multiselect' && $value) {
978 $value = explode(",", $value);
979 }
980 return $value;
981 }
982
983 /**
984 * Creates UsersWP related tables.
985 *
986 * @since 1.0.0
987 * @package userswp
988 *
989 * @return void
990 */
991 function uwp_create_tables()
992 {
993 $tables = new UsersWP_Tables();
994 $tables->create_tables();
995 }
996
997 /**
998 * Returns tye client IP.
999 *
1000 * @since 1.0.0
1001 * @package userswp
1002 *
1003 * @return string IP address.
1004 */
1005 function uwp_get_ip() {
1006 if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
1007 //check ip from share internet
1008 $ip = $_SERVER['HTTP_CLIENT_IP'];
1009 } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
1010 //to check ip is pass from proxy
1011 $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
1012 } else {
1013 $ip = $_SERVER['REMOTE_ADDR'];
1014 }
1015
1016 return apply_filters('uwp_get_ip', $ip);
1017 }
1018
1019 /**
1020 * Checks whether the string starts with the given string.
1021 *
1022 * @since 1.0.0
1023 * @package userswp
1024 *
1025 * @param string $haystack String to compare with.
1026 * @param string $needle String to search for.
1027 *
1028 * @return bool True when success. False when failure.
1029 */
1030 function uwp_str_starts_with($haystack, $needle)
1031 {
1032 $length = strlen($needle);
1033 return (substr($haystack, 0, $length) === $needle);
1034 }
1035
1036 /**
1037 * Checks whether the string ends with the given string.
1038 *
1039 * @since 1.0.0
1040 * @package userswp
1041 *
1042 * @param string $haystack String to compare with.
1043 * @param string $needle String to search for.
1044 *
1045 * @return bool True when success. False when failure.
1046 */
1047 function uwp_str_ends_with($haystack, $needle)
1048 {
1049 $length = strlen($needle);
1050 if ($length == 0) {
1051 return true;
1052 }
1053
1054 return (substr($haystack, -$length) === $needle);
1055 }
1056
1057 /**
1058 * Returns the font awesome icon value for field type.
1059 * Displayed in profile tabs.
1060 *
1061 * @since 1.0.0
1062 * @package userswp
1063 *
1064 * @param string $type Field type.
1065 *
1066 * @return string Font awesome icon value.
1067 */
1068 function uwp_field_type_to_fa_icon($type) {
1069 $field_types = array(
1070 'text' => 'fas fa-minus',
1071 'datepicker' => 'fas fa-calendar-alt',
1072 'textarea' => 'fas fa-bars',
1073 'time' =>'far fa-clock',
1074 'checkbox' =>'far fa-check-square',
1075 'phone' =>'far fa-phone',
1076 'radio' =>'far fa-dot-circle',
1077 'email' =>'far fa-envelope',
1078 'select' =>'far fa-caret-square-down',
1079 'multiselect' =>'far fa-caret-square-down',
1080 'url' =>'fas fa-link',
1081 'file' =>'fas fa-file'
1082 );
1083
1084 if (isset($field_types[$type])) {
1085 return $field_types[$type];
1086 } else {
1087 return "";
1088 }
1089
1090 }
1091
1092 /**
1093 * Check wpml active or not.
1094 *
1095 * @since 1.0.7
1096 *
1097 * @return True if WPML is active else False.
1098 */
1099 function uwp_is_wpml() {
1100 if ( defined( 'ICL_SITEPRESS_VERSION' ) && ! ICL_PLUGIN_INACTIVE && class_exists( 'SitePress' ) && function_exists( 'icl_object_id' ) ) {
1101 return true;
1102 }
1103
1104 return false;
1105 }
1106
1107 /**
1108 * Get the element in the WPML current language.
1109 *
1110 * @since 1.0.7
1111 *
1112 * @param int $element_id Use term_id for taxonomies, post_id for posts
1113 * @param string $element_type Use post, page, {custom post type name}, nav_menu, nav_menu_item, category, tag, etc.
1114 * You can also pass 'any', to let WPML guess the type, but this will only work for posts.
1115 * @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).
1116 * @param string|NULL $ulanguage_code Optional, default is NULL. If missing, it will use the current language.
1117 * If set to a language code, it will return a translation for that language code or
1118 * the original if the translation is missing and $return_original_if_missing is set to TRUE.
1119 *
1120 * @return int|NULL
1121 */
1122 function uwp_wpml_object_id( $element_id, $element_type = 'post', $return_original_if_missing = false, $ulanguage_code = null ) {
1123 if ( uwp_is_wpml() ) {
1124 if ( function_exists( 'wpml_object_id_filter' ) ) {
1125 return apply_filters( 'wpml_object_id', $element_id, $element_type, $return_original_if_missing, $ulanguage_code );
1126 } else {
1127 return icl_object_id( $element_id, $element_type, $return_original_if_missing, $ulanguage_code );
1128 }
1129 }
1130
1131 return $element_id;
1132 }
1133
1134 /**
1135 * Check if we might be on localhost.
1136 *
1137 * @return bool
1138 */
1139 function uwp_is_localhost(){
1140 $localhost = false;
1141
1142 if( isset($_SERVER['SERVER_NAME']) && $_SERVER['SERVER_NAME']=='localhost' ){
1143 $localhost = true;
1144 }elseif(isset($_SERVER['SERVER_ADDR']) && ( $_SERVER['SERVER_ADDR'] == '127.0.0.1' || $_SERVER['SERVER_ADDR'] == '::1' ) ){
1145 $localhost = true;
1146 }
1147
1148 return $localhost;
1149 }
1150
1151 function uwp_get_default_avatar_uri(){
1152 $default = uwp_get_option('profile_default_profile', '');
1153 if(empty($default)){
1154 $default = USERSWP_PLUGIN_URL."assets/images/no_profile.png";
1155 } else {
1156 $default = wp_get_attachment_url($default);
1157 }
1158
1159 return apply_filters('uwp_default_avatar_uri', $default);
1160 }
1161
1162 function uwp_get_default_thumb_uri(){
1163 $thumb_url = USERSWP_PLUGIN_URL."assets/images/no_thumb.png";
1164 return apply_filters('uwp_default_thumb_uri', $thumb_url);
1165 }
1166
1167 function uwp_get_default_banner_uri(){
1168 $banner = uwp_get_option('profile_default_banner', '');
1169 if(empty($banner)) {
1170 $banner_url = USERSWP_PLUGIN_URL."assets/images/banner.png";
1171 } else {
1172 $banner_url = wp_get_attachment_url($banner);
1173 }
1174 return apply_filters('uwp_default_banner_uri', $banner_url);
1175 }
1176
1177 /**
1178 * Handles multisite upload dir path
1179 *
1180 * @param $uploads array upload variable array
1181 *
1182 * @return array updated upload variable array.
1183 */
1184 function uwp_handle_multisite_profile_image($uploads){
1185 if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
1186 require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
1187 }
1188
1189 // Network active.
1190 if ( is_plugin_active_for_network( 'userswp/userswp.php' ) ) {
1191 $main_site = get_network()->site_id;
1192 switch_to_blog( $main_site );
1193 remove_filter( 'upload_dir', 'uwp_handle_multisite_profile_image');
1194 $uploads = wp_upload_dir();
1195 restore_current_blog();
1196 }
1197
1198 return $uploads;
1199 }
1200
1201 /**
1202 * let_to_num function.
1203 *
1204 * This function transforms the php.ini notation for numbers (like '2M') to an integer.
1205 *
1206 * @since 2.0.0
1207 * @param $size
1208 * @return int
1209 */
1210 function uwp_let_to_num( $size ) {
1211 $l = substr( $size, -1 );
1212 $ret = substr( $size, 0, -1 );
1213 switch ( strtoupper( $l ) ) {
1214 case 'P':
1215 $ret *= 1024;
1216 case 'T':
1217 $ret *= 1024;
1218 case 'G':
1219 $ret *= 1024;
1220 case 'M':
1221 $ret *= 1024;
1222 case 'K':
1223 $ret *= 1024;
1224 }
1225 return $ret;
1226 }
1227
1228 function uwp_format_decimal($number, $dp = false, $trim_zeros = false){
1229 $locale = localeconv();
1230 $decimals = array( uwp_get_decimal_separator(), $locale['decimal_point'], $locale['mon_decimal_point'] );
1231
1232 // Remove locale from string.
1233 if ( ! is_float( $number ) ) {
1234 $number = str_replace( $decimals, '.', $number );
1235 $number = preg_replace( '/[^0-9\.,-]/', '', uwp_clean( $number ) );
1236 }
1237
1238 if ( false !== $dp ) {
1239 $dp = intval( '' == $dp ? uwp_get_decimal_separator() : $dp );
1240 $number = number_format( floatval( $number ), $dp, '.', '' );
1241 // DP is false - don't use number format, just return a string in our format
1242 } elseif ( is_float( $number ) ) {
1243 // DP is false - don't use number format, just return a string using whatever is given. Remove scientific notation using sprintf.
1244 $number = str_replace( $decimals, '.', sprintf( '%.' . uwp_get_rounding_precision() . 'f', $number ) );
1245 // We already had a float, so trailing zeros are not needed.
1246 $trim_zeros = true;
1247 }
1248
1249 if ( $trim_zeros && strstr( $number, '.' ) ) {
1250 $number = rtrim( rtrim( $number, '0' ), '.' );
1251 }
1252
1253 return $number;
1254 }
1255
1256 /**
1257 * Return the decimal separator.
1258 * @since 1.0.20
1259 * @return string
1260 */
1261 function uwp_get_decimal_separator() {
1262 $separator = apply_filters( 'uwp_decimal_separator', '.' );
1263 return $separator ? stripslashes( $separator ) : '.';
1264 }
1265
1266 /**
1267 * Get rounding precision for internal UWP calculations.
1268 * Will increase the precision of uwp_get_decimal_separator by 2 decimals, unless UWP_ROUNDING_PRECISION is set to a higher number.
1269 *
1270 * @since 1.0.20
1271 * @return int
1272 */
1273 function uwp_get_rounding_precision() {
1274 $precision = uwp_get_decimal_separator() + 2;
1275 if ( defined(UWP_ROUNDING_PRECISION) && absint( UWP_ROUNDING_PRECISION ) > $precision ) {
1276 $precision = absint( UWP_ROUNDING_PRECISION );
1277 }
1278 return $precision;
1279 }
1280
1281 /**
1282 * Clean variables using sanitize_text_field. Arrays are cleaned recursively.
1283 * Non-scalar values are ignored.
1284 *
1285 * @param string|array $var
1286 *
1287 * @return string|array
1288 */
1289 function uwp_clean( $var ) {
1290
1291 if ( is_array( $var ) ) {
1292 return array_map( 'uwp_clean', $var );
1293 } else {
1294 return is_scalar( $var ) ? sanitize_text_field( $var ) : $var;
1295 }
1296
1297 }
1298
1299 /**
1300 * Define a constant if it is not already defined.
1301 *
1302 * @since 1.0.21
1303 *
1304 * @param string $name Constant name.
1305 * @param string $value Value.
1306 */
1307 function uwp_maybe_define( $name, $value ) {
1308 if ( ! defined( $name ) ) {
1309 define( $name, $value );
1310 }
1311 }
1312
1313 function uwp_insert_usermeta(){
1314 global $wpdb;
1315 $sort= "user_registered";
1316
1317 $all_users_id = $wpdb->get_col( $wpdb->prepare( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
1318 "SELECT $wpdb->users.ID FROM $wpdb->users ORDER BY %s ASC"
1319 , $sort ));
1320
1321 //we got all the IDs, now loop through them to get individual IDs
1322 foreach ( $all_users_id as $user_id ) {
1323 $user_data = get_userdata($user_id);
1324
1325 $meta_table = get_usermeta_table_prefix() . 'uwp_usermeta';
1326 $user_meta = array(
1327 'username' => $user_data->user_login,
1328 'email' => sanitize_email( $user_data->user_email ),
1329 'first_name' => $user_data->first_name,
1330 'last_name' => $user_data->last_name,
1331 'display_name' => $user_data->display_name,
1332 );
1333
1334 $users = $wpdb->get_var($wpdb->prepare("SELECT COUNT(user_id) FROM {$meta_table} WHERE user_id = %d", $user_id)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
1335
1336 if(!empty($users)) {
1337 $wpdb->update( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
1338 $meta_table,
1339 $user_meta,
1340 array('user_id' => $user_id)
1341 );
1342 } else {
1343 $user_meta['user_id'] = $user_id;
1344 $wpdb->insert( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
1345 $meta_table,
1346 $user_meta
1347 );
1348 }
1349 }
1350 }
1351
1352 function uwp_get_localize_data(){
1353 $uwp_localize_data = array(
1354 'uwp_more_char_limit' => 100,
1355 'uwp_more_text' => __('more','userswp'),
1356 'uwp_less_text' => __('less','userswp'),
1357 'error' => __('Something went wrong.','userswp'),
1358 'error_retry' => __('Something went wrong, please retry.','userswp'),
1359 'uwp_more_ellipses_text' => '...',
1360 'ajaxurl' => admin_url('admin-ajax.php'),
1361 'login_modal' => uwp_get_option("design_style",'bootstrap')=='bootstrap' && uwp_get_option("login_modal",1) ? 1 : '',
1362 'register_modal' => uwp_get_option("design_style",'bootstrap')=='bootstrap' && uwp_get_option("register_modal",1) ? 1 : '',
1363 'forgot_modal' => uwp_get_option("design_style",'bootstrap')=='bootstrap' && uwp_get_option("forgot_modal",1) ? 1 : '',
1364 'uwp_pass_strength' => uwp_get_option("register_min_password_strength",0),
1365 'uwp_strong_pass_msg' => uwp_get_option("register_uwp_strong_pass_msg",__("Please enter valid strong password.", "userswp")),
1366 'default_banner' => uwp_get_default_banner_uri(),
1367 'basicNonce' => esc_attr( wp_create_nonce( 'uwp_basic_nonce' ) )
1368 );
1369
1370 return apply_filters('uwp_localize_data', $uwp_localize_data);
1371 }
1372
1373 function uwp_is_page_builder(){
1374 if(
1375 (isset($_GET['elementor-preview']) && $_GET['elementor-preview'] > 0) // elementor
1376 || isset( $_REQUEST['et_fb'] ) || isset( $_REQUEST['et_pb_preview'] ) // divi
1377 || isset( $_REQUEST['fl_builder'] ) // beaver
1378 || ! empty( $_REQUEST['siteorigin_panels_live_editor'] ) // siteorigin
1379 || ! empty( $_REQUEST['cornerstone_preview'] ) // cornerstone
1380 || ! empty( $_REQUEST['fb-edit'] ) || ! empty( $_REQUEST['fusion_load_nonce'] ) // fusion builder
1381 || ! empty( $_REQUEST['ct_builder'] ) || ( ! empty( $_REQUEST['action'] ) && ( substr( $_REQUEST['action'], 0, 11 ) === "oxy_render_" || substr( $_REQUEST['action'], 0, 10 ) === "ct_render_" ) ) // oxygen
1382 ){
1383 return true; // builder.
1384 }
1385
1386 return false;
1387 }
1388
1389 /**
1390 * Display a help tip for settings.
1391 *
1392 * @param string $tip Help tip text
1393 * @param bool $allow_html Allow sanitized HTML if true or escape
1394 *
1395 * @return string
1396 */
1397 function uwp_help_tip( $tip, $allow_html = false ) {
1398 global $aui_bs5;
1399 if ( $allow_html ) {
1400 $tip = uwp_sanitize_tooltip( $tip );
1401 } else {
1402 $tip = esc_attr( $tip );
1403 }
1404
1405 $ml = $aui_bs5 ? 'ms-2 float-end' : 'ml-2 float-right';
1406
1407 return '<span class="uwp-help-tip dashicons dashicons-editor-help text-muted ' . $ml . '" title="' . $tip . '" data-bs-toggle="tooltip" data-bs-html="true"></span>';
1408 }
1409
1410 /**
1411 * Sanitize a string destined to be a tooltip.
1412 *
1413 * Tooltips are encoded with htmlspecialchars to prevent XSS. Should not be used in conjunction with esc_attr()
1414 *
1415 * @param string $var
1416 * @return string
1417 */
1418 function uwp_sanitize_tooltip( $var ) {
1419 return htmlspecialchars( wp_kses( html_entity_decode( $var ), array(
1420 'br' => array(),
1421 'em' => array(),
1422 'strong' => array(),
1423 'small' => array(),
1424 'span' => array(),
1425 'ul' => array(),
1426 'li' => array(),
1427 'ol' => array(),
1428 'p' => array(),
1429 ) ) );
1430 }
1431
1432 function uwp_all_email_tags( $inline = true, $extra_tags = array() ){
1433 $tags = array( '[#site_name#]', '[#site_name_url#]', '[#to_name#]', '[#from_name#]', '[#from_email#]', '[#user_name#]', '[#username#]', '[#user_email#]', '[#first_name#]', '[#last_name#]','[#login_details#]', '[#date_time#]', '[#current_date#]', '[#login_url#]', '[#user_login#]', '[#profile_link#]' );
1434
1435 if(is_array($extra_tags) && count($extra_tags) > 0){
1436 $tags = array_merge($extra_tags, $tags);
1437 }
1438
1439 $tags = apply_filters( 'uwp_all_email_tags', $tags );
1440
1441 if ( $inline ) {
1442 $tags = '<code>' . implode( '</code> <code>', $tags ) . '</code>';
1443 }
1444
1445 return $tags;
1446 }
1447
1448 function uwp_wp_new_user_notification_tags( $inline = true, $extra_tags = array() ){
1449 $tags = array( '[#site_name#]', '[#site_name_url#]', '[#to_name#]', '[#from_name#]', '[#from_email#]', '[#user_name#]', '[#username#]', '[#user_email#]', '[#date_time#]', '[#current_date#]', '[#login_url#]', '[#user_login#]', );
1450
1451 if(is_array($extra_tags) && count($extra_tags) > 0){
1452 $tags = array_merge($tags, $extra_tags);
1453 }
1454
1455 $tags = apply_filters( 'uwp_wp_new_user_notification_email_tags', $tags );
1456
1457 if ( $inline ) {
1458 $tags = '<code>' . implode( '</code> <code>', $tags ) . '</code>';
1459 }
1460
1461 return $tags;
1462 }
1463
1464
1465 function uwp_delete_account_email_tags( $inline = true ){
1466 $tags = array( '[#site_name#]', '[#site_name_url#]', '[#from_name#]', '[#from_email#]', '[#date_time#]', '[#current_date#]', '[#login_url#]', '[#user_login#]' );
1467
1468 $tags = apply_filters( 'uwp_delete_account_email_tags', $tags );
1469
1470 if ( $inline ) {
1471 $tags = '<code>' . implode( '</code> <code>', $tags ) . '</code>';
1472 }
1473
1474 return $tags;
1475 }
1476
1477 function uwp_authbox_tags( $inline = true ){
1478 global $wpdb;
1479
1480 $tags = array( '[#post_id#]', '[#author_id#]', '[#author_display_name#]', '[#author_name#]', '[#author_link#]', '[#author_bio#]', '[#author_image#]', '[#author_image_url#]', '[#post_modified#]', '[#post_date#]', '[#author_nicename#]', '[#author_registered#]', '[#author_website#]' );
1481
1482 $tags = apply_filters('uwp_author_box_default_tags', $tags, $inline);
1483
1484 $table_name = uwp_get_table_prefix() . 'uwp_usermeta';
1485
1486 $excluded = uwp_get_excluded_fields();
1487
1488 $columns = $wpdb->get_col("show columns from $table_name"); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
1489
1490 $extra_tags = array_diff($columns,$excluded);
1491
1492 if( !empty( $extra_tags ) && '' != $extra_tags ) {
1493
1494 foreach ( $extra_tags as $tag_val ) {
1495 $tags[] = '[#'.$tag_val.'#]';
1496 }
1497
1498 }
1499
1500 $tags = apply_filters( 'uwp_all_author_box_tags', $tags );
1501
1502 if ( $inline ) {
1503 $tags = '<code>' . implode( '</code> <code>', $tags ) . '</code>';
1504 }
1505
1506 return $tags;
1507 }
1508
1509 function uwp_get_posttypes() {
1510
1511 $exclude_posts = array('attachment','revision','nav_menu_item','custom_css','uwp-post');
1512 $exclude_posttype = apply_filters('uwp_exclude_register_posttype', $exclude_posts);
1513
1514 $all_posttyps = get_post_types(array('public' => true,),'objects');
1515
1516 $display_posttypes = array();
1517
1518 if( !empty( $all_posttyps ) && '' != $all_posttyps ) {
1519 foreach ( $all_posttyps as $pt_keys => $pt_values ) {
1520
1521 if( !in_array($pt_values->name,$exclude_posttype) ) {
1522 $display_posttypes[$pt_values->name] = $pt_values->label;
1523 }
1524
1525 }
1526 }
1527
1528 return $display_posttypes;
1529 }
1530
1531 function uwp_get_user_by_author_slug(){
1532 $url_type = apply_filters('uwp_profile_url_type', 'slug');
1533 $author_slug = get_query_var('uwp_profile');
1534 if ($url_type == 'id') {
1535 $user = get_user_by('id', $author_slug);
1536 } else {
1537 $user = get_user_by('slug', $author_slug);
1538 }
1539
1540 return $user;
1541 }
1542
1543 function uwp_get_show_in_locations(){
1544 $show_in_locations = array(
1545 "[users]" => __("Users Page", 'userswp'),
1546 "[more_info]" => __("More info tab", 'userswp'),
1547 "[profile_side]" => __("Profile side (non bootstrap)", 'userswp'),
1548 "[fieldset]" => __("Fieldset", 'userswp'),
1549 );
1550
1551 $show_in_locations = apply_filters('uwp_show_in_locations', $show_in_locations);
1552
1553 return $show_in_locations;
1554 }
1555
1556 function uwp_get_displayed_user(){
1557 global $uwp_user;
1558 $user = uwp_get_user_by_author_slug(); // for user displayed in profile
1559
1560 if(!$user && is_user_logged_in()){
1561 $user = get_userdata(get_current_user_id()); // for user currently logged in
1562 }
1563
1564 if(isset($uwp_user) && !empty($uwp_user) && $uwp_user instanceof WP_User){ // for user displaying in loop
1565 $user = $uwp_user;
1566 }
1567
1568 return apply_filters('uwp_get_displayed_user', $user);
1569 }
1570
1571 function uwp_is_gdv2(){
1572
1573 if(defined('GEODIRECTORY_VERSION') && version_compare(GEODIRECTORY_VERSION,'2.0.0.0', '>=') ) {
1574 return true;
1575 }
1576
1577 return false;
1578 }
1579
1580 function uwp_get_blogname() {
1581 $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
1582
1583 return apply_filters( 'uwp_get_blogname', $blogname );
1584 }
1585
1586 /**
1587 * RGB from hex.
1588 *
1589 * @since 1.2.1.3
1590 *
1591 * @param string $color Color.
1592 * @return array $rgb.
1593 */
1594 function uwp_rgb_from_hex( $color ) {
1595 $color = str_replace( '#', '', $color );
1596
1597 // Convert shorthand colors to full format, e.g. "FFF" -> "FFFFFF"
1598 $color = preg_replace( '~^(.)(.)(.)$~', '$1$1$2$2$3$3', $color );
1599 if ( empty( $color ) ) {
1600 return NULL;
1601 }
1602
1603 $color = str_split( $color );
1604
1605 $rgb = array();
1606 $rgb['R'] = hexdec( $color[0].$color[1] );
1607 $rgb['G'] = hexdec( $color[2].$color[3] );
1608 $rgb['B'] = hexdec( $color[4].$color[5] );
1609
1610 return $rgb;
1611 }
1612
1613 /**
1614 * HEX darker.
1615 *
1616 * @since 1.2.1.3
1617 *
1618 * @param string $color Color.
1619 * @param int $factor Optional. Factor. Default 30.
1620 * @return string $color.
1621 */
1622 function uwp_hex_darker( $color, $factor = 30 ) {
1623 $base = uwp_rgb_from_hex( $color );
1624 if ( empty( $base ) ) {
1625 return $color;
1626 }
1627
1628 $color = '#';
1629 foreach ( $base as $k => $v ) {
1630 $amount = $v / 100;
1631 $amount = round( $amount * $factor );
1632 $new_decimal = $v - $amount;
1633
1634 $new_hex_component = dechex( $new_decimal );
1635 if ( strlen( $new_hex_component ) < 2 ) {
1636 $new_hex_component = "0" . $new_hex_component;
1637 }
1638 $color .= $new_hex_component;
1639 }
1640
1641 return $color;
1642 }
1643
1644 /**
1645 * Hex lighter.
1646 *
1647 * @since 1.2.1.3
1648 *
1649 * @param string $color Color.
1650 * @param int $factor Optional. factor. Default 30.
1651 * @return string $color.
1652 */
1653 function uwp_hex_lighter( $color, $factor = 30 ) {
1654 $base = uwp_rgb_from_hex( $color );
1655 if ( empty( $base ) ) {
1656 return $color;
1657 }
1658
1659 $color = '#';
1660
1661 foreach ( $base as $k => $v ) {
1662 $amount = 255 - $v;
1663 $amount = $amount / 100;
1664 $amount = round( $amount * $factor );
1665 $new_decimal = $v + $amount;
1666
1667 $new_hex_component = dechex( $new_decimal );
1668 if ( strlen( $new_hex_component ) < 2 ) {
1669 $new_hex_component = "0" . $new_hex_component;
1670 }
1671 $color .= $new_hex_component;
1672 }
1673
1674 return $color;
1675 }
1676
1677 /**
1678 * Get Light or dark.
1679 *
1680 * @since 1.2.1.3
1681 *
1682 * @param string $color color.
1683 * @param string $dark Optional. Dark. Default #000000.
1684 * @param string $light Optional. Light. Default #FFFFFF.
1685 * @return string
1686 */
1687 function uwp_light_or_dark( $color, $dark = '#000000', $light = '#FFFFFF' ) {
1688 $hex = str_replace( '#', '', $color );
1689 if ( empty( $hex ) ) {
1690 return $color;
1691 }
1692
1693 $c_r = hexdec( substr( $hex, 0, 2 ) );
1694 $c_g = hexdec( substr( $hex, 2, 2 ) );
1695 $c_b = hexdec( substr( $hex, 4, 2 ) );
1696
1697 $brightness = ( ( $c_r * 299 ) + ( $c_g * 587 ) + ( $c_b * 114 ) ) / 1000;
1698
1699 return $brightness > 155 ? $dark : $light;
1700 }
1701
1702 /**
1703 * Format hex.
1704 *
1705 * @since 1.2.1.3
1706 *
1707 * @param string $hex hex.
1708 * @return string
1709 */
1710 function uwp_format_hex( $hex ) {
1711 $hex = trim( str_replace( '#', '', $hex ) );
1712 if ( empty( $hex ) ) {
1713 return NULL;
1714 }
1715
1716 if ( strlen( $hex ) == 3 ) {
1717 $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
1718 }
1719
1720 return $hex ? '#' . $hex : null;
1721 }
1722
1723 /**
1724 * Returns activation link for user.
1725 *
1726 * @since 1.2.1.3
1727 *
1728 * @param int $user_id User ID.
1729 *
1730 * @return string $activation_link
1731 */
1732 function uwp_get_activation_link($user_id){
1733
1734 global $wpdb, $wp_hasher;
1735
1736 if(!$user_id){
1737 return false;
1738 }
1739
1740 $user_data = get_userdata($user_id);
1741
1742 $key = wp_generate_password( 20, false );
1743
1744 do_action( 'uwp_activation_key', $user_data->user_login, $key );
1745
1746 if ( empty( $wp_hasher ) ) {
1747 require_once ABSPATH . 'wp-includes/class-phpass.php';
1748 $wp_hasher = new PasswordHash( 8, true );
1749 }
1750 $hashed = $wp_hasher->HashPassword( $key );
1751 $wpdb->update( $wpdb->users, array( 'user_activation_key' => time().":".$hashed ), array( 'user_login' => $user_data->user_login ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
1752 update_user_meta( $user_id, 'uwp_mod', 'email_unconfirmed' );
1753
1754 $activation_link = add_query_arg(
1755 array(
1756 'uwp_activate' => 'yes',
1757 'key' => $key,
1758 'login' => $user_data->user_login
1759 ),
1760 home_url('/login/')
1761 );
1762
1763 return $activation_link;
1764 }
1765
1766 /**
1767 * Checks a version number against the core version and adds a admin notice if requirements are not met.
1768 *
1769 * @param $name
1770 * @param $version
1771 *
1772 * @return bool
1773 */
1774 function uwp_min_version_check( $name, $version ) {
1775 if ( version_compare( USERSWP_VERSION, $version, '<' ) ) {
1776 add_action( 'admin_notices', function () use ( &$name ) {
1777 ?>
1778 <div class="notice notice-error is-dismissible">
1779 <p><?php echo esc_html( wp_sprintf( __( "%s requires a newer version of UsersWP and will not run until the UsersWP plugin is updated.", "userswp" ), $name ) ); ?></p>
1780 </div>
1781 <?php
1782 } );
1783
1784 return false;
1785 }
1786
1787 return true;
1788 }
1789
1790 function uwp_get_user_roles($exclude = array()) {
1791 $user_roles = array();
1792 if ( !function_exists('get_editable_roles') ) {
1793 require_once( ABSPATH . '/wp-admin/includes/user.php' );
1794 }
1795
1796 $wp_roles = get_editable_roles();
1797 if(!empty($wp_roles) && is_array($wp_roles)) {
1798 foreach ( $wp_roles as $role => $details ) {
1799 if ( in_array( $role, $exclude ) ) {
1800 } else {
1801 $user_roles[ esc_attr( $role ) ] = !empty($details['name']) ? translate_user_role( $details['name'] ): $role;
1802 }
1803
1804 }
1805 }
1806
1807 return $user_roles;
1808 }
1809
1810 function uwp_get_sort_by_order_list(){
1811
1812 $cache = wp_cache_get("uwp_get_sort_options");
1813 if($cache !== false){
1814 return $cache;
1815 }
1816
1817 global $wpdb;
1818 $table_name = uwp_get_table_prefix() . 'uwp_user_sorting';
1819
1820 $sort_options_raw = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM " . $table_name . " WHERE is_active = %d AND field_type != 'address' AND tab_parent = '0' ORDER BY sort_order ASC", array( 1 ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
1821
1822 $sort_options = array();
1823
1824 if ( ! empty( $sort_options_raw ) && count( $sort_options_raw ) > 1 ) {
1825 foreach ( $sort_options_raw as $sort ) {
1826 $sort = stripslashes_deep( $sort );
1827
1828 $sort->site_title = __( wp_unslash( $sort->site_title ), 'userswp' );
1829
1830 if ( $sort->htmlvar_name == 'comment_count' ) {
1831 $sort->htmlvar_name = 'rating_count';
1832 }
1833
1834 $key = $sort->htmlvar_name;
1835 if ( !in_array($key, array('newer', 'older')) ) {
1836 if($sort->sort == 'asc'){$key = esc_attr($sort->htmlvar_name."_asc");}
1837 elseif($sort->sort == 'desc'){$key = esc_attr($sort->htmlvar_name."_desc");}
1838 }
1839
1840 $sort_options[$key] = wp_unslash( $sort->site_title );
1841 }
1842 }
1843
1844 /**
1845 * Filter post sort options.
1846 *
1847 * @param array $sort_options Unfiltered sort field array.
1848 */
1849 $sort_options = apply_filters( 'uwp_available_users_layout', $sort_options );
1850
1851 wp_cache_set("uwp_get_sort_options", $sort_options );
1852
1853 return $sort_options;
1854 }
1855
1856 function uwp_get_default_sort(){
1857
1858 $cache = wp_cache_get("uwp_get_default_sort");
1859
1860 if($cache !== false){
1861 return $cache;
1862 }
1863
1864 $default_sort = 'newer_asc';
1865
1866 global $wpdb;
1867 $table_name = uwp_get_table_prefix() . 'uwp_user_sorting';
1868
1869 $field = $wpdb->get_row("SELECT htmlvar_name, sort, field_type FROM " . $table_name . " WHERE is_active = 1 AND is_default = 1 ORDER BY sort_order ASC" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
1870 if ( ! empty( $field ) ) {
1871 if ( $field->field_type == 'random' ) {
1872 $default_sort = 'random';
1873 } elseif ( 'newer' == $field->htmlvar_name) {
1874 $default_sort = 'user_registered_desc';
1875 } elseif ( 'older' == $field->htmlvar_name ) {
1876 $default_sort = 'user_registered_asc';
1877 }else {
1878 $default_sort = $field->htmlvar_name . '_' . $field->sort;
1879 }
1880 }
1881
1882 wp_cache_set("uwp_get_default_sort", $default_sort );
1883
1884 return $default_sort;
1885
1886 }
1887
1888 function uwp_get_username( $user_id ) {
1889 $user_data = get_userdata( $user_id );
1890
1891 if ( ! $user_data ) {
1892 return '';
1893 }
1894
1895 $display_name = ! empty( $user_data->display_name ) ? $user_data->display_name : $user_data->user_login;
1896
1897 return apply_filters( 'uwp_get_username', $display_name, $user_id, $user_data );
1898 }