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

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