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

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