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.72 1.2.71 1.2.70 1.2.69 1.2.68 1.2.67 1.2.66 1.2.65 1.2.64 1.2.63 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.23 All 172 releases
userswp / includes / class-profile.php

class-profile.php in UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP 1.2.3.4, at includes/class-profile.php

1,937 lines 59.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Profile Template related functions
5 *
6 * This class defines all code necessary for Profile template.
7 *
8 * @since 1.0.0
9 * @author GeoDirectory Team <info@wpgeodirectory.com>
10 */
11 class UsersWP_Profile {
12
13 public static function get_author_link( $link, $user_id ) {
14
15 if ( 1 == uwp_get_option( 'uwp_disable_author_link' ) && ! ( is_uwp_profile_page() || is_uwp_users_page() ) ) {
16 return $link;
17 }
18
19 return self::get_profile_link( $link, $user_id );
20 }
21
22 /**
23 * Returns user profile link based on user id.
24 *
25 * @since 1.0.0
26 * @package userswp
27 *
28 * @param string $link Unmodified link.
29 * @param int $user_id User id.
30 *
31 * @return string Modified link.
32 */
33 public static function get_profile_link( $link, $user_id ) {
34
35 $page_id = uwp_get_page_id( 'profile_page', false );
36
37 if ( ! $page_id ) {
38 return $link;
39 }
40
41 $link = get_permalink( $page_id );
42
43 if ( $link != '' ) {
44
45 if ( isset( $_REQUEST['page_id'] ) ) {
46 $permalink_structure = 'DEFAULT';
47 } else {
48 $permalink_structure = 'CUSTOM';
49 // Add forward slash if not available
50 $link = trailingslashit( $link );
51 }
52
53 $url_type = apply_filters( 'uwp_profile_url_type', 'slug' );
54
55 if ( $url_type && 'id' == $url_type ) {
56 if ( 'DEFAULT' == $permalink_structure ) {
57 return add_query_arg( array( 'viewuser' => $user_id ), $link );
58 } else {
59 return user_trailingslashit( $link . $user_id );
60 }
61 } else {
62 $user = get_userdata( $user_id );
63 if ( $user ) {
64 if ( ! empty( $user->user_nicename ) ) {
65 $username = $user->user_nicename;
66 } else {
67 $username = $user->user_login;
68 }
69 } else {
70 $username = '';
71 }
72
73 if ( 'DEFAULT' == $permalink_structure ) {
74 return add_query_arg( array( 'username' => $username ), $link );
75 } else {
76 return user_trailingslashit( $link . $username );
77 }
78 }
79 } else {
80 return $link;
81 }
82 }
83
84 /**
85 * Prints the profile page title section.
86 *
87 * @since 1.0.0
88 * @package userswp
89 *
90 * @param object $user The User ID.
91 * @param string $tag Tag
92 * @param string $title_class Title Class
93 * @param string $link_class Link Class
94 * @param bool $link The User ID.
95 */
96 public function get_profile_title( $user, $tag = 'h2', $title_class = '', $link_class = '', $link = true ) {
97 if ( ! $user ) {
98 return;
99 }
100 ?>
101 <div class="uwp-profile-name">
102 <<?php echo esc_attr( $tag ); ?> class="uwp-user-title <?php echo esc_attr( $title_class ); ?>"
103 data-user="<?php echo absint( $user->ID ); ?>">
104 <?php if ( $link ){ ?><a href="<?php echo esc_url( uwp_build_profile_tab_url( $user->ID ) ); ?>"
105 class="<?php echo esc_attr( $link_class ); ?>"><?php } ?>
106 <?php echo apply_filters( 'uwp_profile_display_name', esc_attr( $user->display_name ) ); ?>
107 <?php if ( $link ){ ?></a><?php } ?>
108 <?php do_action( 'uwp_profile_after_title', $user->ID ); ?>
109 </<?php echo esc_attr( $tag ); ?>>
110 </div>
111 <?php
112 }
113
114 /**
115 * Displays edit account button
116 */
117 public function edit_profile_button( $user_id ) {
118 global $uwp_in_user_loop;
119 $account_page = uwp_get_page_id( 'account_page', false );
120 if ( $user_id ) {
121 $user = get_userdata( $user_id );
122 } else {
123 $user = uwp_get_displayed_user();
124 }
125
126 $can_user_edit_account = apply_filters( 'uwp_user_can_edit_own_profile', true, $user->ID );
127
128 if ( ! $uwp_in_user_loop && $account_page && is_user_logged_in() && ( get_current_user_id() == $user->ID ) && $can_user_edit_account ) { ?>
129 <a href="<?php echo get_permalink( $account_page ); ?>"
130 class="btn btn-sm btn-outline-dark btn-circle mt-n1" data-toggle="tooltip"
131 title="<?php esc_attr_e( 'Edit Account', 'userswp' ); ?>"><i
132 class="fas fa-pencil-alt fa-fw fa-lg"></i></a>
133 <?php }
134 }
135
136 /**
137 * Prints the profile page social links section.
138 *
139 * @since 1.0.0
140 * @package userswp
141 *
142 * @param object $user The User ID.
143 * @param string , array $exclude Fields to exclude from displaying.
144 */
145 public function get_profile_social( $user, $exclude = '' ) {
146
147 if ( is_uwp_profile_page() ) {
148 $show_type = '[profile_side]';
149 } elseif ( is_uwp_users_page() ) {
150 $show_type = '[users]';
151 } else {
152 $show_type = false;
153 }
154
155 if ( ! $show_type ) {
156 return;
157 }
158
159 global $wpdb;
160 $table_name = uwp_get_table_prefix() . 'uwp_form_fields';
161 $form_id = uwp_get_register_form_id( $user->ID );
162 $fields = $wpdb->get_results( $wpdb->prepare("SELECT * FROM " . $table_name . " WHERE ( form_type = 'register' OR form_type = 'account' ) AND field_type = 'url' AND css_class LIKE '%uwp_social%' AND form_id = %d ORDER BY sort_order ASC", $form_id ) );
163
164 $usermeta = isset( $user->ID ) ? uwp_get_usermeta_row( $user->ID ) : array();
165 $privacy = ! empty( $usermeta ) && ! empty( $usermeta->user_privacy ) ? explode( ',', $usermeta->user_privacy ) : array();
166
167 if ( ! empty( $exclude ) && ! is_array( $exclude ) ) {
168 $exclude = explode( ',', $exclude );
169 $exclude = array_map( 'trim', $exclude );
170 }
171 ?>
172 <div class="uwp-profile-social">
173 <ul class="uwp-profile-social-ul">
174 <?php
175 foreach ( $fields as $field ) {
176 $show_in = explode( ',', $field->show_in );
177
178 if ( ! in_array( $show_type, $show_in ) ) {
179 continue;
180 }
181
182 $display = false;
183 if ( $field->is_public == '0' ) {
184 $display = false;
185 } else if ( $field->is_public == '1' ) {
186 $display = true;
187 } else {
188 if ( ! in_array( $field->htmlvar_name . '_privacy', $privacy ) ) {
189 $display = true;
190 }
191 }
192 if ( ! $display ) {
193 continue;
194 }
195
196 $key = $field->htmlvar_name;
197 // see UsersWP_Forms -> save_user_extra_fields reason for replacing key
198 $key = str_replace( 'uwp_register_', '', $key );
199 $exclude_key = str_replace( 'uwp_account_', '', $key );
200 $value = uwp_get_usermeta( $user->ID, $key, false );
201 $icon = uwp_get_field_icon( $field->field_icon );
202
203 if ( isset( $icon ) && ! empty( $icon ) ) {
204 $title = $icon;
205 } else {
206 $title = $field->site_title;
207 }
208
209 if ( ! empty( $exclude ) && in_array( $exclude_key, $exclude ) ) {
210 continue;
211 }
212
213 if ( $value ) {
214 echo '<li><a target="_blank" rel="nofollow" href="' . esc_url($value) . '">' . esc_atrr($title) . '</a></li>';
215 }
216 }
217 ?>
218 </ul>
219 </div>
220 <?php
221 }
222
223 /**
224 * Returns the custom fields content of profile page more info tab.
225 *
226 * @since 1.0.0
227 * @package userswp
228 *
229 * @param object $user The User ID.
230 * @param string $show_type Location of field to display.
231 */
232 public function show_output_location_data( $user, $show_type ) {
233 if ( ! $show_type ) {
234 return;
235 }
236 echo $this->get_extra_fields( $user, '[' . $show_type . ']' );
237 }
238
239 /**
240 * Returns the custom fields content based on type.
241 *
242 * @since 1.0.0
243 * @package userswp
244 *
245 * @param object $user The User ID.
246 * @param string $show_type Filter type.
247 *
248 * @return string Custom fields content.
249 */
250 public function get_extra_fields( $user, $show_type ) {
251
252 ob_start();
253 global $wpdb;
254 $table_name = uwp_get_table_prefix() . 'uwp_form_fields';
255 $form_id = uwp_get_register_form_id( $user->ID );
256 $fields = $wpdb->get_results( $wpdb->prepare("SELECT * FROM " . $table_name . " WHERE form_type = 'account' AND css_class NOT LIKE '%uwp_social%' AND form_id = %d ORDER BY sort_order ASC", $form_id ));
257 $wrap_html = false;
258 if ( $fields ) {
259 $usermeta = isset( $user->ID ) ? uwp_get_usermeta_row( $user->ID ) : array();
260 $privacy = ! empty( $usermeta ) && ! empty( $usermeta->user_privacy ) ? explode( ',', $usermeta->user_privacy ) : array();
261
262 foreach ( $fields as $field ) {
263 $show_in = explode( ',', $field->show_in );
264 if ( ! in_array( $show_type, $show_in ) ) {
265 continue;
266 }
267
268 $display = false;
269 if ( $field->is_public == '0' ) {
270 $display = false;
271 } else if ( $field->is_public == '1' ) {
272 $display = true;
273 } else {
274 if ( ! in_array( $field->htmlvar_name . '_privacy', $privacy ) ) {
275 $display = true;
276 }
277 }
278 if ( ! $display ) {
279 continue;
280 }
281
282 $value = $this->get_field_value( $field, $user );
283 $class = isset( $field->css_class ) ? $field->css_class : '';
284
285 // Icon
286 $icon = uwp_get_field_icon( $field->field_icon );
287 $site_title = isset( $field->site_title ) ? __( wp_unslash($field->site_title), 'userswp' ) : '';
288
289 if ( $field->field_type == 'fieldset' ) {
290 $icon = '';
291 ?>
292 <div class="uwp-profile-extra-wrap m-0 p-0">
293 <div class="uwp-profile-extra-key uwp-profile-extra-full m-0 p-0"><h3
294 style="margin: 10px 0;"><?php echo $icon . $site_title; ?></h3></div>
295 </div>
296 <?php
297 } else {
298 if ( $value ) {
299 $wrap_html = true;
300 ?>
301 <div class="uwp-profile-extra-wrap <?php echo esc_attr( $class ); ?>">
302 <div class="uwp-profile-extra-key d-inline-block"><?php echo $icon . " " . $site_title; ?>
303 <span class="uwp-profile-extra-sep">:</span></div>
304 <div class="uwp-profile-extra-value d-inline-block">
305 <?php
306 if ( $field->htmlvar_name == 'bio' ) {
307 $show_read_more = apply_filters( 'uwp_profile_bio_show_read_more', false );
308 $value = get_user_meta( $user->ID, 'description', true );
309 $value = stripslashes( $value );
310 $limit_words = apply_filters( 'uwp_profile_bio_content_limit', 50 );
311 if ( $value ) {
312 ?>
313 <div class="uwp-profile-bio <?php if ( $show_read_more ) {
314 echo "uwp_more";
315 } ?>">
316 <?php
317 if ( is_uwp_profile_page() ) {
318 echo nl2br( $value );
319 } else {
320 echo wp_trim_words( $value, $limit_words, '...' );
321 }
322 ?>
323 </div>
324 <?php
325 }
326 } else {
327 echo $value;
328 }
329 ?>
330 </div>
331 </div>
332 <?php
333 }
334 }
335 }
336 }
337 $output = ob_get_contents();
338 $wrapped_output = '';
339 if ( $wrap_html ) {
340 $wrapped_output .= '<div class="uwp-profile-extra"><div class="uwp-profile-extra-div form-table">';
341 $wrapped_output .= $output;
342 $wrapped_output .= '</div></div>';
343 }
344 ob_end_clean();
345
346 return trim( $wrapped_output );
347 }
348
349 /**
350 * Gets custom field value based on key.
351 *
352 * @since 1.0.0
353 * @package userswp
354 *
355 * @param object $field Field info object.
356 * @param object $user The User.
357 *
358 * @return string Custom field value.
359 */
360 public function get_field_value( $field, $user ) {
361
362 $user_data = get_userdata( $user->ID );
363
364 if ( $field->htmlvar_name == 'email' ) {
365 $value = $user_data->user_email;
366 } elseif ( $field->htmlvar_name == 'password' ) {
367 $value = '';
368 $field->is_required = 0;
369 } elseif ( $field->htmlvar_name == 'confirm_password' ) {
370 $value = '';
371 $field->is_required = 0;
372 } else {
373 $value = uwp_get_usermeta( $user->ID, $field->htmlvar_name, "" );
374 }
375
376 // Select and Multiselect needs Value to be converted
377 if ( $field->field_type == 'select' || $field->field_type == 'multiselect' ) {
378 $option_values_arr = uwp_string_values_to_options( $field->option_values, true );
379
380 // Select
381 if ( $field->field_type == 'select' ) {
382
383 if ( $field->field_type_key != 'country' && $field->field_type_key != 'uwp_country' ) {
384 if ( ! empty( $value ) ) {
385 $data = $this->uwp_array_search( $option_values_arr, 'value', $value );
386 $value = $data[0]['label'];
387 } else {
388 $value = '';
389 }
390 }
391 }
392
393 //Multiselect
394 if ( $field->field_type == 'multiselect' && ! empty( $value ) ) {
395 if ( ! empty( $value ) && is_array( $value ) ) {
396 $array_value = array();
397 foreach ( $value as $v ) {
398 if ( ! empty( $v ) ) {
399 $data = $this->uwp_array_search( $option_values_arr, 'value', $v );
400 $array_value[] = $data[0]['label'];
401 }
402
403 }
404 if ( ! empty( $array_value ) ) {
405 $value = implode( ', ', $array_value );
406 } else {
407 $value = '';
408 }
409
410 } else {
411 $value = '';
412 }
413 }
414 }
415
416 // Date
417 if ( $field->field_type == 'datepicker' ) {
418 $date_format = get_option( 'date_format' );
419
420 if ( isset( $field->extra_fields ) && ! empty( $field->extra_fields ) ) {
421 $extra_fields = unserialize( $field->extra_fields );
422 if ( isset( $extra_fields['date_format'] ) && ! empty( $extra_fields['date_format'] ) ) {
423 $date_format = $extra_fields['date_format'];
424 }
425 }
426
427 if ( ! empty( $value ) ) {
428 $value = date( $date_format, $value );
429 } else {
430 $value = '';
431 }
432
433 }
434
435 // Time
436 if ( $field->field_type == 'time' ) {
437 if ( ! empty( $value ) ) {
438 $value = date( get_option( 'time_format' ), strtotime( $value ) );
439 } else {
440 $value = '';
441 }
442 }
443
444 // URL
445 if ( $field->field_type == 'url' && ! empty( $value ) ) {
446 $link_text = $value;
447 // if default_value is not url then it will be used as link text.
448 if ( $field->default_value && ! empty( $field->default_value ) ) {
449 if ( substr( $field->default_value, 0, 4 ) === "http" ) {
450 $link_text = $value;
451 } else {
452 $link_text = $field->default_value;
453 }
454 }
455 if ( substr( $link_text, 0, 4 ) === "http" ) {
456 $link_text = esc_url( $link_text );
457 }
458
459 $value = '<a href="' . esc_url( $value ) . '" target="_blank" rel="nofollow">' . esc_attr( $link_text ) . '</a>';
460 }
461
462 // Checkbox
463 if ( $field->field_type == 'checkbox' ) {
464 if ( $value == '1' ) {
465 $value = __( 'Yes', 'userswp' );
466 } else {
467 $value = __( 'No', 'userswp' );
468 }
469 }
470
471 // File
472 if ( $field->field_type == 'file' ) {
473 $file_obj = new UsersWP_Files();
474 $value = $file_obj->file_upload_preview( $field, $value, false );
475 }
476
477 // Sanitize
478 switch ( $field->field_type ) {
479 case 'url':
480 // already escaped
481 break;
482 case 'file':
483 // already escaped
484 break;
485 case 'textarea':
486 $value = esc_textarea( $value );
487 $value = nl2br( $value );
488 break;
489 case 'editor':
490 $value = wp_kses_post( $value );
491 $value = nl2br( $value );
492 break;
493 default:
494 $value = esc_html( $value );
495 }
496
497 if ( isset( $field->field_type_key ) && $field->field_type_key == 'country' || $field->field_type_key == 'uwp_country' ) {
498 $value = uwp_output_country_html( $value );
499 }
500
501 return apply_filters('uwp_get_field_value', $value, $field, $user);
502 }
503
504 /**
505 * Array search by value
506 *
507 * @since 1.0.0
508 * @package userswp
509 *
510 * @param array $array Original array.
511 * @param mixed $key Array key
512 * @param mixed $value Array value.
513 *
514 * @return array Result array.
515 */
516 public function uwp_array_search( $array, $key, $value ) {
517 $results = array();
518
519 if ( is_array( $array ) ) {
520 if ( isset( $array[ $key ] ) && $array[ $key ] == $value ) {
521 $results[] = $array;
522 }
523
524 foreach ( $array as $subarray ) {
525 $results = array_merge( $results, $this->uwp_array_search( $subarray, $key, $value ) );
526 }
527 }
528
529 return $results;
530 }
531
532 /**
533 * Returns the custom fields content of profile page sidebar.
534 *
535 * @since 1.0.0
536 * @package userswp
537 *
538 * @param object $user The User ID.
539 */
540 public function get_profile_side_extra( $user ) {
541 echo $this->get_extra_fields( $user, '[profile_side]' );
542 }
543
544 /**
545 * Returns the custom fields content of users page.
546 *
547 * @since 1.0.0
548 * @package userswp
549 *
550 * @param object $user The User ID.
551 */
552 public function get_users_extra( $user ) {
553 echo $this->get_extra_fields( $user, '[users]' );
554 }
555
556 /**
557 *
558 * Returns profile page tabs
559 *
560 * @return array
561 */
562 public function get_tabs() {
563
564 // get the settings
565 global $uwp_profile_tabs_array;
566 // check if we have been here before
567 $tabs = ! empty( $uwp_profile_tabs_array ) ? $uwp_profile_tabs_array : $this->get_tab_settings();
568
569 $tabs_array = array();
570
571 if ( ! empty( $tabs ) ) {
572 // get the tab contents first so we can decide to output the tab head
573 $tabs_content = array();
574 foreach ( $tabs as $tab ) {
575 $tabs_content[ $tab->id . "tab" ] = $this->tab_content( $tab );
576 }
577
578 // setup the array
579 foreach ( $tabs as $tab ) {
580 if ( isset( $tab->tab_level ) && $tab->tab_level > 0 ) {
581 continue;
582 }
583
584 $hide_empty = apply_filters('uwp_hide_empty_tabs', true, $tab, $tabs);
585
586 if ( $hide_empty && empty( $tabs_content[ $tab->id . "tab" ] ) ) {
587 continue;
588 }
589
590 $tab->tab_content_rendered = $tabs_content[ $tab->id . "tab" ];
591 $tabs_array[] = (array) $tab;
592 }
593
594 }
595
596 /**
597 * Get the tabs output settings.
598 *
599 * @param array $tabs_array The array of tabs.
600 */
601 return apply_filters( 'uwp_get_profile_tabs', $tabs_array );
602
603 }
604
605 /**
606 *
607 * Returns profile tabs
608 *
609 * @return array $tabs Profile tabs
610 */
611 public function get_tab_settings() {
612 global $wpdb, $uwp_profile_tabs_array;
613
614 if ( $uwp_profile_tabs_array ) {
615 $tabs = $uwp_profile_tabs_array;
616 } else {
617 $tabs_table_name = uwp_get_table_prefix() . 'uwp_profile_tabs';
618 $uwp_profile_tabs_array = array();
619 $displayed_user = uwp_get_user_by_author_slug();
620
621 if ( $displayed_user ) {
622 $tabs_privacy = uwp_get_tabs_privacy_by_user( $displayed_user );
623 }
624
625 $form_id = uwp_get_register_form_id( $displayed_user->ID );
626 $tabs_result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM " . $tabs_table_name . " WHERE form_type = %s and form_id = %s ORDER BY sort_order ASC", 'profile-tabs', $form_id ) );
627 foreach ( $tabs_result as $tab ) {
628 if ( isset( $tab->user_decided ) && 1 == $tab->user_decided && ! empty( $tabs_privacy ) ) {
629
630 $field_name = $tab->tab_key . '_tab_privacy';
631 $public_fields_keys = is_array( $tabs_privacy ) ? array_keys( $tabs_privacy ) : $tabs_privacy;
632 if ( in_array( $field_name, $public_fields_keys ) ) {
633 if ( 1 == $tabs_privacy[ $field_name ] && is_user_logged_in() ) { // 1 for logged in users
634 $uwp_profile_tabs_array[] = $tab;
635 } elseif ( 2 == $tabs_privacy[ $field_name ] && isset( $displayed_user->ID ) && ( $displayed_user->ID == get_current_user_id() || current_user_can( 'administrator' ) ) ) { // 2 for author and admin only
636 $uwp_profile_tabs_array[] = $tab;
637 } elseif ( 0 == $tabs_privacy[ $field_name ] ) { // for all users
638 $uwp_profile_tabs_array[] = $tab;
639 } else {
640 // Skip tab
641 }
642 }
643
644 } elseif ( isset( $tab->tab_privacy ) ) {
645
646 if ( 1 == $tab->tab_privacy && is_user_logged_in() ) { // 1 for logged in users
647 $uwp_profile_tabs_array[] = $tab;
648 } elseif ( 2 == $tab->tab_privacy && isset( $displayed_user->ID ) && ( $displayed_user->ID == get_current_user_id() || current_user_can( 'administrator' ) ) ) { // 2 for author and admin only
649 $uwp_profile_tabs_array[] = $tab;
650 } elseif ( 0 == $tab->tab_privacy ) { // for all users
651 $uwp_profile_tabs_array[] = $tab;
652 } else {
653 // Skip tab
654 }
655
656 } else {
657 // Skip tab
658 }
659 }
660
661 $tabs = $uwp_profile_tabs_array;
662 }
663
664 /**
665 * Get the tabs output settings.
666 *
667 * @param array $tabs The array of stdClass settings for the tabs output.
668 */
669 return apply_filters( 'uwp_profile_tab_settings', $tabs );
670 }
671
672 /**
673 * Returns profile tab content
674 *
675 * @since 2.0.0
676 *
677 * @param array $tab Tab array
678 *
679 * @return string
680 */
681 public function tab_content( $tab ) {
682
683 ob_start();
684 // main content
685 if ( ! empty( $tab->tab_content ) ) { // override content
686 $content = stripslashes( $tab->tab_content );
687 echo do_shortcode( $content );
688 } elseif ( $tab->tab_type == 'standard' ) {
689 $user = uwp_get_displayed_user();
690 do_action( 'uwp_profile_tab_content', $user, $tab );
691 do_action( 'uwp_profile_' . $tab->tab_key . '_tab_content', $user, $tab );
692 }
693
694 echo self::tab_content_child( $tab ); // child elements
695
696 return ob_get_clean();
697 }
698
699 /**
700 * Returns profile child tab content
701 *
702 * @since 2.0.0
703 *
704 * @param array $tab Child tab array
705 *
706 * @return string
707 */
708 public function tab_content_child( $tab ) {
709 ob_start();
710 $tabs = self::get_tab_settings();
711 $parent_id = $tab->id;
712
713 foreach ( $tabs as $child_tab ) {
714 if ( isset( $child_tab->tab_parent ) && $child_tab->tab_parent == $parent_id ) {
715 if ( ! empty( $child_tab->tab_content ) ) { // override content
716 echo do_shortcode( stripslashes( $child_tab->tab_content ) );
717 } elseif ( $child_tab->tab_type == 'fieldset' ) {
718 self::output_fieldset( $child_tab );
719 } elseif ( $child_tab->tab_type == 'standard' ) {
720 $user = uwp_get_displayed_user();
721 do_action( 'uwp_profile_tab_content', $user, $child_tab );
722 do_action( 'uwp_profile_' . $child_tab->tab_key . '_tab_content', $user, $child_tab );
723 if ( $child_tab->tab_key == 'reviews' ) {
724 comments_template();
725 }
726 }
727 }
728 }
729
730 return ob_get_clean();
731
732 }
733
734 /**
735 *
736 * Displays fieldset content
737 *
738 * @param array $tab Tab array
739 *
740 * @return string
741 */
742 public function output_fieldset( $tab ) {
743 ob_start();
744 echo '<div class="uwp_post_meta uwp-fieldset">';
745 echo "<h4>";
746 if ( $tab->tab_icon ) {
747 echo '<i class="fas ' . esc_attr( $tab->tab_icon ) . '" aria-hidden="true"></i>';
748 }
749 if ( $tab->tab_name ) {
750 esc_attr_e( $tab->tab_name, 'userswp' );
751 }
752 echo "</h4>";
753 echo "</div>";
754
755 return ob_get_clean();
756 }
757
758 /**
759 * Prints the tab content pagination section.
760 *
761 * @since 1.0.0
762 * @package userswp
763 *
764 * @param int $total Total items.
765 *
766 * @return void
767 */
768 public function get_profile_pagination( $total ) {
769 ?>
770 <div class="uwp-pagination">
771 <?php
772 $design_style = uwp_get_option( "design_style", 'bootstrap' );
773 if ( $design_style == 'bootstrap' ) {
774 self::get_bootstrap_pagination( $total );
775 } else {
776 $big = 999999999; // need an unlikely integer
777 $translated = __( 'Page', 'userswp' ); // Supply translatable string
778
779 $args = array(
780 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
781 'format' => '?paged=%#%',
782 'current' => max( 1, get_query_var( 'paged' ) ),
783 'total' => $total,
784 'before_page_number' => '<span class="screen-reader-text">' . $translated . ' </span>',
785 'type' => 'list'
786 );
787
788 echo paginate_links( $args );
789 }
790
791
792 ?>
793 </div>
794 <?php
795 }
796
797 /**
798 * Displays boostrap pagination
799 *
800 * @since 2.0.0
801 *
802 * @param int $total Total count
803 */
804 public function get_bootstrap_pagination( $total ) {
805
806 $navigation = '';
807 $args = array(
808 'mid_size' => 2,
809 'type' => 'array',
810 'prev_text' => sprintf(
811 '%s <span class="nav-prev-text sr-only">%s</span>',
812 '<i class="fas fa-chevron-left"></i>',
813 __( 'Newer posts', 'ayetheme' )
814 ),
815 'next_text' => sprintf(
816 '<span class="nav-next-text sr-only">%s</span> %s',
817 __( 'Older posts', 'ayetheme' ),
818 '<i class="fas fa-chevron-right"></i>'
819 ),
820 );
821
822 // Don't print empty markup if there's only one page.
823 if ( $total > 1 ) {
824 $args = wp_parse_args( $args, array(
825 'mid_size' => 1,
826 'prev_text' => _x( 'Previous', 'previous set of posts' ),
827 'next_text' => _x( 'Next', 'next set of posts' ),
828 'screen_reader_text' => __( 'Posts navigation' ),
829 'total' => $total,
830 ) );
831
832 // Set up paginated links.
833 $links = paginate_links( $args );
834
835 // make the output bootstrap ready
836 $links_html = "<ul class='pagination m-0 p-0'>";
837 if ( ! empty( $links ) ) {
838 foreach ( $links as $link ) {
839 $active = strpos( $link, 'current' ) !== false ? 'active' : '';
840 $links_html .= "<li class='page-item $active'>";
841 $links_html .= str_replace( "page-numbers", "page-link", $link );
842 $links_html .= "</li>";
843 }
844 }
845 $links_html .= "</ul>";
846
847 if ( $links ) {
848 $navigation .= '<section class="px-0 py-2 w-100">';
849 $navigation .= _navigation_markup( $links_html, 'pagination', $args['screen_reader_text'] );
850 $navigation .= '</section>';
851 }
852
853 $navigation = str_replace( "nav-links", "uwp-nav-links", $navigation );
854 }
855
856 echo $navigation;
857
858 }
859
860 /**
861 * Prints the profile page more info tab content.
862 *
863 * @since 1.0.0
864 * @package userswp
865 *
866 * @param object $user The User ID.
867 *
868 * @return void
869 */
870 public function get_profile_more_info( $user ) {
871 $extra = $this->get_profile_extra( $user );
872 echo $extra;
873 }
874
875 /**
876 * Returns the custom fields content of profile page more info tab.
877 *
878 * @since 1.0.0
879 * @package userswp
880 *
881 * @param object $user The User ID.
882 *
883 * @return string More info tab content.
884 */
885 public function get_profile_extra( $user ) {
886 return $this->get_extra_fields( $user, '[more_info]' );
887 }
888
889 /**
890 * Prints the profile page "posts" tab content.
891 *
892 * @since 1.0.0
893 * @package userswp
894 *
895 * @param object $user The User ID.
896 *
897 * @return void
898 */
899 public function get_profile_posts( $user ) {
900 uwp_generic_tab_content( $user, 'post', __( 'Posts', 'userswp' ) );
901 }
902
903 /**
904 * Prints the profile page "comments" tab content.
905 *
906 * @since 1.0.0
907 * @package userswp
908 *
909 * @param object $user The User ID.
910 * @param string $post_type Post type.
911 * @param array $args Extra arguments.
912 *
913 * @return void
914 */
915 public function get_profile_comments( $user, $post_type = 'post', $args = array() ) {
916
917 $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
918 $number = uwp_get_option( 'profile_no_of_items', 10 );
919 $offset = ( $paged - 1 ) * $number;
920
921 $total_comments = $this->get_comment_count_by_user( $user->ID, $post_type );
922 $maximum_pages = ceil( $total_comments / $number );
923
924 $query_args = array(
925 'number' => $number,
926 'offset' => $offset,
927 'user_id' => $user->ID,
928 'paged' => $paged,
929 'post_type' => $post_type,
930 );
931 // The Query
932 $the_query = new WP_Comment_Query();
933 $comments = $the_query->query( $query_args );
934
935 if(!$comments){
936 return;
937 }
938
939 $args['template_args']['the_query'] = $comments;
940 $args['template_args']['user'] = $user;
941 $args['template_args']['title'] = ! empty( $args['template_args']['title'] ) ? $args['template_args']['title'] : __( "Comments", 'userswp' );
942 $args['template_args']['maximum_pages'] = $maximum_pages;
943
944 $design_style = ! empty( $args['design_style'] ) ? esc_attr( $args['design_style'] ) : uwp_get_option( "design_style", 'bootstrap' );
945 $template = $design_style ? $design_style . "/loop-comments.php" : "loop-comments.php";
946 uwp_get_template( $template, $args );
947 }
948
949 /**
950 * Displays the profile page "user comments" tab content.
951 *
952 * @since 1.2.2.36
953 * @package userswp
954 *
955 * @param object $user The User ID.
956 * @param string $post_type Post type.
957 * @param array $args Extra arguments.
958 *
959 * @return void
960 */
961 public function get_profile_user_comments( $user, $post_type = 'post', $args = array() ) {
962
963 $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
964 $number = uwp_get_option( 'profile_no_of_items', 10 );
965 $offset = ( $paged - 1 ) * $number;
966
967 $args = array(
968 'post_type' => $post_type,
969 'post_status' => 'published',
970 'posts_per_page' => -1,
971 'author' => $user->ID,
972 'fields' => 'ids',
973 );
974
975 $wp_query = new WP_Query($args);
976 $post_ids = $wp_query->posts;
977
978 if(isset( $the_query->found_posts ) && $wp_query->found_posts == 0 || empty($post_ids) ){
979 return;
980 }
981
982 $query_args = array(
983 'count' => true,
984 'post_type' => $post_type,
985 'post__in' => $post_ids,
986 'author__not_in' => $user->ID,
987 );
988 // The Query
989 $the_query = new WP_Comment_Query();
990 $comments_count = $the_query->query( $query_args );
991
992 $total_comments = $comments_count;
993 $maximum_pages = ceil( $total_comments / $number );
994
995 $query_args = array(
996 'number' => $number,
997 'offset' => $offset,
998 'paged' => $paged,
999 'post_type' => $post_type,
1000 'post__in' => $post_ids,
1001 'author__not_in' => $user->ID,
1002 );
1003
1004 // The Query
1005 $the_query = new WP_Comment_Query();
1006 $comments = $the_query->query( $query_args );
1007
1008 if(!$comments){
1009 return;
1010 }
1011
1012 $args['template_args']['the_query'] = $comments;
1013 $args['template_args']['user'] = $user;
1014 $args['template_args']['title'] = ! empty( $args['template_args']['title'] ) ? $args['template_args']['title'] : __( "User Comments", 'userswp' );
1015 $args['template_args']['maximum_pages'] = $maximum_pages;
1016
1017 $design_style = ! empty( $args['design_style'] ) ? esc_attr( $args['design_style'] ) : uwp_get_option( "design_style", 'bootstrap' );
1018 $template = $design_style ? $design_style . "/loop-comments.php" : "loop-comments.php";
1019 uwp_get_template( $template, $args );
1020 }
1021
1022 /**
1023 * Gets the comment count.
1024 *
1025 * @since 1.0.0
1026 * @package userswp
1027 *
1028 * @param int $user_id User ID.
1029 *
1030 * @return int Comment count.
1031 */
1032 public function get_comment_count_by_user( $user_id, $post_type = 'post' ) {
1033 global $wpdb;
1034
1035 $count = $wpdb->get_var(
1036 "SELECT COUNT(comment_ID)
1037 FROM " . $wpdb->comments . "
1038 WHERE comment_post_ID in (
1039 SELECT ID
1040 FROM " . $wpdb->posts . "
1041 WHERE post_type = '".$post_type."'
1042 AND post_status = 'publish')
1043 AND user_id = " . $user_id . "
1044 AND comment_approved = '1'
1045 AND comment_type NOT IN ('pingback', 'trackback' )"
1046 );
1047
1048 return $count;
1049 }
1050
1051 /**
1052 * Rewrites profile page links
1053 *
1054 * @since 1.0.0
1055 * @package userswp
1056 * @return void
1057 */
1058 public function rewrite_profile_link() {
1059
1060 $page_id = uwp_get_page_id( 'profile_page', false );
1061 if ( $page_id && ! isset( $_REQUEST['page_id'] ) ) {
1062 $link = get_page_link( $page_id );
1063 $uwp_profile_link = trailingslashit( $this->profile_slug() );
1064 $uwp_profile_page_id = $page_id;
1065
1066 // {home_url}/profile/1
1067 $uwp_profile_link_empty_slash = '^' . $uwp_profile_link . '([^/]+)?$';
1068 add_rewrite_rule( $uwp_profile_link_empty_slash, 'index.php?page_id=' . $uwp_profile_page_id . '&uwp_profile=$matches[1]', 'top' );
1069
1070 // {home_url}/profile/1/
1071 $uwp_profile_link_with_slash = '^' . $uwp_profile_link . '([^/]+)/?$';
1072 add_rewrite_rule( $uwp_profile_link_with_slash, 'index.php?page_id=' . $uwp_profile_page_id . '&uwp_profile=$matches[1]', 'top' );
1073
1074 // {home_url}/profile/1/page/1
1075 $uwp_profile_link_empty_slash_paged = '^' . $uwp_profile_link . '([^/]+)/page/([0-9]+)?$';
1076 add_rewrite_rule( $uwp_profile_link_empty_slash_paged, 'index.php?page_id=' . $uwp_profile_page_id . '&uwp_profile=$matches[1]&paged=$matches[2]', 'top' );
1077
1078 // {home_url}/profile/1/page/1/
1079 $uwp_profile_link_with_slash_paged = '^' . $uwp_profile_link . '([^/]+)/page/([0-9]+)/?$';
1080 add_rewrite_rule( $uwp_profile_link_with_slash_paged, 'index.php?page_id=' . $uwp_profile_page_id . '&uwp_profile=$matches[1]&paged=$matches[2]', 'top' );
1081
1082 // {home_url}/profile/1/tab-slug
1083 $uwp_profile_tab_empty_slash = '^' . $uwp_profile_link . '([^/]+)/([^/]+)?$';
1084 add_rewrite_rule( $uwp_profile_tab_empty_slash, 'index.php?page_id=' . $uwp_profile_page_id . '&uwp_profile=$matches[1]&uwp_tab=$matches[2]', 'top' );
1085
1086 // {home_url}/profile/1/tab-slug/
1087 $uwp_profile_tab_with_slash = '^' . $uwp_profile_link . '([^/]+)/([^/]+)/?$';
1088 add_rewrite_rule( $uwp_profile_tab_with_slash, 'index.php?page_id=' . $uwp_profile_page_id . '&uwp_profile=$matches[1]&uwp_tab=$matches[2]', 'top' );
1089
1090 // {home_url}/profile/1/tab-slug/page/1
1091 $uwp_profile_tab_empty_slash_paged = '^' . $uwp_profile_link . '([^/]+)/([^/]+)/page/([0-9]+)?$';
1092 add_rewrite_rule( $uwp_profile_tab_empty_slash_paged, 'index.php?page_id=' . $uwp_profile_page_id . '&uwp_profile=$matches[1]&uwp_tab=$matches[2]&paged=$matches[3]', 'top' );
1093
1094 // {home_url}/profile/1/tab-slug/page/1/
1095 $uwp_profile_tab_with_slash_paged = '^' . $uwp_profile_link . '([^/]+)/([^/]+)/page/([0-9]+)/?$';
1096 add_rewrite_rule( $uwp_profile_tab_with_slash_paged, 'index.php?page_id=' . $uwp_profile_page_id . '&uwp_profile=$matches[1]&uwp_tab=$matches[2]&paged=$matches[3]', 'top' );
1097
1098 // {home_url}/profile/1/tab-slug/subtab-slug
1099 $uwp_profile_tab_empty_slash = '^' . $uwp_profile_link . '([^/]+)/([^/]+)/([^/]+)?$';
1100 add_rewrite_rule( $uwp_profile_tab_empty_slash, 'index.php?page_id=' . $uwp_profile_page_id . '&uwp_profile=$matches[1]&uwp_tab=$matches[2]&uwp_subtab=$matches[3]', 'top' );
1101
1102 // {home_url}/profile/1/tab-slug/subtab-slug/
1103 $uwp_profile_tab_with_slash = '^' . $uwp_profile_link . '([^/]+)/([^/]+)/([^/]+)/?$';
1104 add_rewrite_rule( $uwp_profile_tab_with_slash, 'index.php?page_id=' . $uwp_profile_page_id . '&uwp_profile=$matches[1]&uwp_tab=$matches[2]&uwp_subtab=$matches[3]', 'top' );
1105
1106 // {home_url}/profile/1/tab-slug/subtab-slug/page/1
1107 $uwp_profile_tab_empty_slash_paged = '^' . $uwp_profile_link . '([^/]+)/([^/]+)/([^/]+)/page/([0-9]+)?$';
1108 add_rewrite_rule( $uwp_profile_tab_empty_slash_paged, 'index.php?page_id=' . $uwp_profile_page_id . '&uwp_profile=$matches[1]&uwp_tab=$matches[2]&uwp_subtab=$matches[3]&paged=$matches[4]', 'top' );
1109
1110 // {home_url}/profile/1/tab-slug/subtab-slug/page/1/
1111 $uwp_profile_tab_with_slash_paged = '^' . $uwp_profile_link . '([^/]+)/([^/]+)/([^/]+)/page/([0-9]+)/?$';
1112 add_rewrite_rule( $uwp_profile_tab_with_slash_paged, 'index.php?page_id=' . $uwp_profile_page_id . '&uwp_profile=$matches[1]&uwp_tab=$matches[2]&uwp_subtab=$matches[3]&paged=$matches[4]', 'top' );
1113
1114 if ( get_option( 'uwp_flush_rewrite' ) ) {
1115 //Ensure the $wp_rewrite global is loaded
1116 global $wp_rewrite;
1117 //Call flush_rules() as a method of the $wp_rewrite object
1118 $wp_rewrite->flush_rules( false );
1119 delete_option( 'uwp_flush_rewrite' );
1120 }
1121
1122 if ( function_exists('pll_current_language') ) {
1123 $previous_lang = ! empty( $_COOKIE[ PLL_COOKIE ] ) ? $_COOKIE[ PLL_COOKIE ] : '';
1124 $current_lang = pll_current_language();
1125 if ( $current_lang != $previous_lang ) {
1126
1127
1128 flush_rewrite_rules( true );
1129 }
1130 }
1131 }
1132 }
1133
1134 /**
1135 * Get the slug for the profile page.
1136 *
1137 * @since 1.2.2.18
1138 *
1139 * @param string $slug Profile slug. Default profile.
1140 *
1141 * @return string
1142 */
1143 public function profile_slug( $slug = 'profile' ) {
1144 if ( $page_id = uwp_get_page_id( 'profile_page', false ) ) {
1145 if ( $_slug = get_post_field( 'post_name', absint( $page_id ) ) ) {
1146 $slug = $_slug;
1147 }
1148 }
1149 return apply_filters( 'uwp_rewrite_profile_slug', $slug );
1150 }
1151
1152 /**
1153 * Adds profile page query variables.
1154 *
1155 * @since 1.0.0
1156 * @package userswp
1157 *
1158 * @param array $query_vars Query variables.
1159 *
1160 * @return array Modified Query variables array.
1161 */
1162 public function profile_query_vars( $query_vars ) {
1163 $query_vars[] = 'uwp_profile';
1164 $query_vars[] = 'uwp_tab';
1165 $query_vars[] = 'uwp_subtab';
1166
1167 return $query_vars;
1168 }
1169
1170 /**
1171 * Modifies profile page title to include username.
1172 *
1173 * @since 1.0.0
1174 * @package userswp
1175 *
1176 * @param string $title Original title
1177 * @param int|null $id Page id.
1178 *
1179 * @return string Modified page title.
1180 */
1181 public function modify_profile_page_title( $title, $id = null ) {
1182
1183 global $wp_query;
1184 $page_id = uwp_get_page_id( 'profile_page', false );
1185
1186 if ( $page_id == $id && isset( $wp_query->query_vars['uwp_profile'] ) && in_the_loop() ) {
1187
1188 $url_type = apply_filters( 'uwp_profile_url_type', 'slug' );
1189
1190 $author_slug = $wp_query->query_vars['uwp_profile'];
1191
1192 if ( $url_type == 'id' ) {
1193 $user = get_user_by( 'id', $author_slug );
1194 } else {
1195 $user = get_user_by( 'slug', $author_slug );
1196 }
1197 $title = esc_attr( $user->display_name );
1198 }
1199
1200 return $title;
1201 }
1202
1203 /**
1204 * Initializes image crop js.
1205 *
1206 * @since 1.0.0
1207 * @package userswp
1208 *
1209 * @param object $user The User ID.
1210 */
1211 public function image_crop_init( $user ) {
1212 if ( is_user_logged_in() ) {
1213 add_action( 'wp_footer', array( $this, 'modal_loading_html' ) );
1214 add_action( 'wp_footer', array( $this, 'modal_close_js' ) );
1215 if ( is_admin() ) {
1216 add_action( 'admin_footer', array( $this, 'modal_loading_html' ) );
1217 add_action( 'admin_footer', array( $this, 'modal_close_js' ) );
1218 }
1219 }
1220 }
1221
1222 /**
1223 * Adds modal close js.
1224 *
1225 * @since 1.0.0
1226 * @package userswp
1227 * @return void
1228 */
1229 public function modal_close_js() {
1230 ?>
1231 <script type="text/javascript">
1232 (function ($, window, undefined) {
1233 $(document).ready(function () {
1234 $('.uwp-modal-close').click(function (e) {
1235 e.preventDefault();
1236 var uwp_popup_type = $(this).data('type');
1237 // $('#uwp-'+uwp_popup_type+'-modal').hide();
1238 var mod_shadow = jQuery('#uwp-modal-backdrop');
1239 var container = jQuery('#uwp-popup-modal-wrap');
1240 container.hide();
1241 container.replaceWith('<?php echo $this->modal_loading_html(); ?>');
1242 mod_shadow.remove();
1243 });
1244 });
1245 }(jQuery, window));
1246 </script>
1247 <?php
1248 }
1249
1250 /*
1251 * Modifies get_avatar_url function to use UWP avatar URL.
1252 *
1253 * @param string $url Default avatar URL
1254 * @param int|string $id_or_email User ID or email
1255 *
1256 * @return string $url New avatar URL
1257 *
1258 */
1259 function get_avatar_url( $url, $id_or_email ) {
1260
1261 // don't filter on admin side.
1262 if ( is_admin() && ! wp_doing_ajax() ) {
1263 return $url;
1264 }
1265
1266 $user = false;
1267
1268 if ( 1 == uwp_get_option( 'disable_avatar_override' ) ) {
1269 return $url;
1270 }
1271
1272 if ( is_numeric( $id_or_email ) ) {
1273
1274 $id = (int) $id_or_email;
1275 $user = get_user_by( 'id', $id );
1276
1277 } elseif ( is_object( $id_or_email ) ) {
1278
1279 if ( ! empty( $id_or_email->user_id ) ) {
1280 $id = (int) $id_or_email->user_id;
1281 $user = get_user_by( 'id', $id );
1282 }
1283
1284 } else {
1285 $user = get_user_by( 'email', $id_or_email );
1286 }
1287
1288 if ( $user && is_object( $user ) ) {
1289 $avatar_thumb = uwp_get_usermeta( $user->data->ID, 'avatar_thumb', '' );
1290 if ( ! empty( $avatar_thumb ) ) {
1291 add_filter( 'upload_dir', 'uwp_handle_multisite_profile_image', 10, 1 );
1292 $uploads = wp_upload_dir();
1293 remove_filter( 'upload_dir', 'uwp_handle_multisite_profile_image' );
1294 $upload_url = $uploads['baseurl'];
1295 if ( substr( $avatar_thumb, 0, 4 ) !== "http" ) {
1296 $url = $upload_url . $avatar_thumb;
1297 } else {
1298 $url = $avatar_thumb;
1299 }
1300 } else {
1301 $default = uwp_get_default_avatar_uri();
1302 if ( uwp_is_localhost() ) { // if localhost then default gravatar won't work.
1303 $url = $default;
1304 } else {
1305 $url = remove_query_arg( 'd', $url );
1306 $url = add_query_arg( array( 'd' => $default ), $url );
1307 }
1308
1309 }
1310 }
1311
1312 return $url;
1313 }
1314
1315 /**
1316 * Modified the comment author url to profile page link for loggedin users.
1317 *
1318 * @since 1.0.0
1319 * @package userswp
1320 *
1321 * @param string $link Original author link.
1322 *
1323 * @return string Modified author link.
1324 */
1325 public function get_comment_author_link( $link ) {
1326 global $comment;
1327 if ( ! empty( $comment->user_id ) && ! empty( get_userdata( $comment->user_id )->ID ) ) {
1328 $user = get_userdata( $comment->user_id );
1329 $link = sprintf(
1330 '<a href="%s" rel="external nofollow" class="url">%s</a>',
1331 uwp_build_profile_tab_url( $comment->user_id ),
1332 strip_tags( $user->display_name )
1333 );
1334 }
1335
1336 return $link;
1337 }
1338
1339 /**
1340 * Redirects /author page to /profile page.
1341 *
1342 * @since 1.0.0
1343 * @package userswp
1344 * @return void
1345 */
1346 public function redirect_author_page() {
1347
1348 if ( uwp_is_page_builder() ) {
1349 return;
1350 }
1351
1352 if ( is_author() && 1 != uwp_get_option( 'uwp_disable_author_link' ) && apply_filters( 'uwp_check_redirect_author_page', true ) ) {
1353 $id = get_query_var( 'author' );
1354 $link = uwp_build_profile_tab_url( $id );
1355 $link = apply_filters( 'uwp_redirect_author_page', $link, $id );
1356 wp_redirect( $link );
1357 exit;
1358 }
1359 }
1360
1361 /**
1362 * Modifies "edit my profile" link in admin bar
1363 *
1364 * @since 1.0.0
1365 * @package userswp
1366 *
1367 * @param string $url The complete URL including scheme and path.
1368 * @param int $user_id The user ID.
1369 * @param string $scheme Scheme to give the URL context. Accepts 'http', 'https', 'login',
1370 * 'login_post', 'admin', 'relative' or null.
1371 *
1372 * @return false|string Filtered url.
1373 */
1374 public function modify_admin_bar_edit_profile_url( $url, $user_id, $scheme ) {
1375 // Makes the link to http://example.com/account
1376 if ( ! is_admin() && ! user_can( $user_id, 'administrator' ) ) {
1377 $account_page = uwp_get_page_id( 'account_page', false );
1378 if ( $account_page ) {
1379 $account_page_link = get_permalink( $account_page );
1380 $url = $account_page_link;
1381 }
1382 }
1383
1384 return $url;
1385 }
1386
1387 /**
1388 * Restrict the files display only to current users in media popup.
1389 *
1390 * @since 1.0.0
1391 * @package userswp
1392 *
1393 * @param object $wp_query Unmodified wp_query.
1394 *
1395 * @return object Modified wp_query.
1396 */
1397 public function restrict_attachment_display( $wp_query ) {
1398 if ( ! is_admin() ) {
1399 if ( ! current_user_can( 'manage_options' ) ) {
1400 $wp_query->set( 'author', get_current_user_id() );
1401 }
1402 }
1403
1404 return $wp_query;
1405 }
1406
1407 /**
1408 * Allow users to upload files who has upload capability.
1409 *
1410 * @since 1.0.0
1411 * @package userswp
1412 *
1413 * @param array $llcaps An array of all the user's capabilities.
1414 * @param array $caps Actual capabilities for meta capability.
1415 * @param array $args Optional parameters passed to has_cap(), typically object ID.
1416 * @param object $user The user object.
1417 *
1418 * @return array User capabilities.
1419 */
1420 public function allow_all_users_profile_uploads( $llcaps, $caps, $args, $user ) {
1421
1422 $files = new UsersWP_Files();
1423
1424 if ( isset( $caps[0] ) && $caps[0] == 'upload_files' && $files->doing_upload() ) {
1425 $llcaps['upload_files'] = true;
1426 }
1427
1428 return $llcaps;
1429 }
1430
1431 /**
1432 * Validates file uploads and returns errors if found.
1433 *
1434 * @since 1.0.0
1435 * @package userswp
1436 *
1437 * @param string|bool $value Original value.
1438 * @param object $field Field info.
1439 * @param string $file_key Field key.
1440 * @param array $file_to_upload File data to upload.
1441 *
1442 * @return string|WP_Error Returns original value if no errors. Else returns errors.
1443 */
1444 public function handle_file_upload_error_checks( $value, $field, $file_key, $file_to_upload ) {
1445
1446 if ( in_array( $field->htmlvar_name, array( 'avatar', 'banner' ) ) ) {
1447
1448 if ( $field->htmlvar_name == 'avatar' ) {
1449 $avatar_size = uwp_get_upload_image_size();
1450 $min_width = $avatar_size['width'];
1451 $min_height = $avatar_size['height'];
1452 } else {
1453 $banner_size = uwp_get_upload_image_size('banner');
1454 $min_width = $banner_size['width'];
1455 $min_height = $banner_size['height'];
1456 }
1457
1458 $imagedetails = getimagesize( $file_to_upload['tmp_name'] );
1459 $width = $imagedetails[0];
1460 $height = $imagedetails[1];
1461
1462 if ( $width < $min_width ) {
1463 return new WP_Error( 'image-too-small', sprintf( __( 'The uploaded file is too small. Minimum image width should be %s px', 'userswp' ), $min_width ) );
1464 }
1465 if ( $height < $min_height ) {
1466 return new WP_Error( 'image-too-small', sprintf( __( 'The uploaded file is too small. Minimum image height should be %s px', 'userswp' ), $min_height ) );
1467 }
1468 }
1469
1470 return $value;
1471
1472 }
1473
1474 /**
1475 * Sets uwp_profile_upload to true on profile page.
1476 *
1477 * @since 1.0.0
1478 * @package userswp
1479 *
1480 * @param array $params Plupload params
1481 *
1482 * @return array Plupload params
1483 */
1484 public function add_uwp_plupload_param( $params ) {
1485
1486 if ( ! is_admin() && get_the_ID() == uwp_get_page_id( 'profile_page', false ) ) {
1487 $params['uwp_profile_upload'] = true;
1488 }
1489
1490 return $params;
1491 }
1492
1493 /**
1494 * Handles avatar and banner file upload.
1495 *
1496 * @since 1.0.0
1497 * @package userswp
1498 * @return void
1499 */
1500 public function ajax_avatar_banner_upload() {
1501 // Image upload handler
1502 // todo: security checks
1503 $type = strip_tags( esc_sql( $_POST['uwp_popup_type'] ) );
1504 $result = array();
1505
1506 if ( ! in_array( $type, array( 'banner', 'avatar' ) ) ) {
1507 $result['error'] = aui()->alert( array(
1508 'type' => 'danger',
1509 'content' => __( "Invalid request!", "userswp" )
1510 ) );
1511 $return = json_encode( $result );
1512 echo $return;
1513 die();
1514 }
1515
1516 global $wpdb;
1517 $table_name = uwp_get_table_prefix() . 'uwp_form_fields';
1518 $fields = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM " . $table_name . " WHERE form_type = %s AND field_type = 'file' AND is_active = '1' ORDER BY sort_order ASC", array( $type ) ) );
1519
1520 $field = false;
1521 if ( $fields ) {
1522 $field = $fields[0];
1523 }
1524
1525 $result = array();
1526
1527 if ( ! $field ) {
1528 $result['error'] = aui()->alert( array(
1529 'type' => 'danger',
1530 'content' => __( "No fields available", "userswp" )
1531 ) );
1532 $return = json_encode( $result );
1533 echo $return;
1534 die();
1535 }
1536
1537 $files = new UsersWP_Files();
1538 $errors = $files->handle_file_upload( $field, $_FILES );
1539
1540 if ( is_wp_error( $errors ) ) {
1541 $result['error'] = aui()->alert( array(
1542 'type' => 'danger',
1543 'content' => $errors->get_error_message()
1544 ) );
1545 $return = json_encode( $result );
1546 echo $return;
1547 } else {
1548 $return = $this->ajax_image_crop_popup( $errors['url'], $type );
1549 echo $return;
1550 }
1551
1552 die();
1553 }
1554
1555 /**
1556 * Returns the avatar and banner crop popup html and js.
1557 *
1558 * @since 1.0.0
1559 * @package userswp
1560 *
1561 * @param string $image_url Image url to crop.
1562 * @param string $type Crop type. Avatar or Banner
1563 *
1564 * @return string|null Html and js content.
1565 */
1566 public function ajax_image_crop_popup( $image_url, $type ) {
1567 wp_enqueue_style( 'jcrop' );
1568 wp_enqueue_script( 'jcrop', array( 'jquery' ) );
1569
1570 $output = null;
1571 if ( $image_url && $type ) {
1572 $output = $this->image_crop_popup( $image_url, $type );
1573 }
1574
1575 return $output;
1576 }
1577
1578 /**
1579 * Returns json content for image crop.
1580 *
1581 * @since 1.0.0
1582 * @package userswp
1583 *
1584 * @param string $image_url Image url
1585 * @param string $type popup type. Avatar or Banner
1586 *
1587 * @return string Json
1588 */
1589 public function image_crop_popup( $image_url, $type ) {
1590
1591 add_filter( 'upload_dir', 'uwp_handle_multisite_profile_image', 10, 1 );
1592 $uploads = wp_upload_dir();
1593 remove_filter( 'upload_dir', 'uwp_handle_multisite_profile_image' );
1594 $upload_url = $uploads['baseurl'];
1595 $upload_path = $uploads['basedir'];
1596 $image_path = str_replace( $upload_url, $upload_path, $image_url );
1597
1598 $image = apply_filters( 'uwp_' . $type . '_cropper_image', getimagesize( $image_path ) );
1599 if ( empty( $image ) ) {
1600 return "";
1601 }
1602
1603 if ( $type == 'avatar' ) {
1604 $avatar_size = uwp_get_upload_image_size();
1605 $full_width = $avatar_size['width'];
1606 $full_height = $avatar_size['height'];
1607 } else {
1608 $banner_size = uwp_get_upload_image_size('banner');
1609 $full_width = $banner_size['width'];
1610 $full_height = $banner_size['height'];
1611 }
1612
1613 $values = array(
1614 'error' => '',
1615 'image_url' => $image_url,
1616 'uwp_popup_type' => $type,
1617 'uwp_full_width' => $full_width,
1618 'uwp_full_height' => $full_height,
1619 'uwp_true_width' => $image[0],
1620 'uwp_true_height' => $image[1],
1621 'uwp_popup_content' => $this->image_crop_modal_html( $type, $image_url, $full_width, $full_height ),
1622 );
1623
1624 $json = json_encode( $values );
1625
1626
1627 return $json;
1628 }
1629
1630 /**
1631 * Returns avatar and banner crop modal html.
1632 *
1633 * @since 1.0.0
1634 * @package userswp
1635 *
1636 * @param string $type Avatar or Banner
1637 * @param string $image_url Image url to crop
1638 * @param int $full_width Full image width
1639 * @param int $full_height Full image height
1640 *
1641 * @return string Html.
1642 */
1643 public function image_crop_modal_html( $type, $image_url, $full_width, $full_height ) {
1644 $args = array(
1645 'type' => $type,
1646 'image_url' => $image_url,
1647 'full_width' => $full_width,
1648 'full_height' => $full_height,
1649 );
1650 ob_start();
1651
1652 $design_style = uwp_get_option( "design_style", 'bootstrap' );
1653 $template = $design_style ? $design_style . "/modal-profile-image-crop.php" : "modal-profile-image-crop.php";
1654
1655 uwp_get_template( $template, $args );
1656 ?>
1657 <script type="text/javascript">
1658 (function ($, window, undefined) {
1659 $(document).ready(function () {
1660 $('.uwp-modal-close').click(function (e) {
1661 e.preventDefault();
1662 var uwp_popup_type = $(this).data('type');
1663 var mod_shadow = jQuery('#uwp-modal-backdrop');
1664 var container = jQuery('#uwp-popup-modal-wrap');
1665 container.hide();
1666 container.replaceWith('<?php echo $this->modal_loading_html(); ?>');
1667 mod_shadow.remove();
1668 });
1669 });
1670 }(jQuery, window));
1671 </script>
1672 <?php
1673 $output = ob_get_contents();
1674 ob_end_clean();
1675
1676 return trim( $output );
1677 }
1678
1679 /**
1680 * Returns modal content loading html.
1681 *
1682 * @since 1.0.0
1683 * @package userswp
1684 */
1685 public function modal_loading_html() {
1686 ob_start();
1687 ?>
1688 <div id="uwp-popup-modal-wrap" style="display: none;">
1689 <div class="uwp-bs-modal uwp_fade uwp_show">
1690 <div class="uwp-bs-modal-dialog">
1691 <div class="uwp-bs-modal-content">
1692 <div class="uwp-bs-modal-header">
1693 <h4 class="uwp-bs-modal-title">
1694 <?php echo __( "Loading Form ...", "userswp" ); ?>
1695 </h4>
1696 </div>
1697 <div class="uwp-bs-modal-body">
1698 <div class="uwp-bs-modal-loading-icon-wrap">
1699 <div class="uwp-bs-modal-loading-icon"></div>
1700 </div>
1701 </div>
1702 </div>
1703 </div>
1704 </div>
1705 </div>
1706 <?php
1707 $output = ob_get_contents();
1708 ob_end_clean();
1709 echo trim( preg_replace( "/\s+|\n+|\r/", ' ', $output ) );
1710 }
1711
1712 /**
1713 * Handles crop popup form ajax request.
1714 *
1715 * @since 1.0.0
1716 * @package userswp
1717 * @return void
1718 */
1719 public function ajax_image_crop_popup_form() {
1720 $type = isset( $_POST['type'] ) ? strip_tags( esc_sql( $_POST['type'] ) ) : '';
1721
1722 $output = null;
1723
1724
1725 if ( $type && in_array( $type, array( 'banner', 'avatar' ) ) ) {
1726 $output = $this->crop_submit_form( $type );
1727 }
1728 echo $output;
1729 exit();
1730 }
1731
1732 /**
1733 * Returns avatar and banner crop submit form.
1734 *
1735 * @since 1.0.0
1736 * @package userswp
1737 *
1738 * @param string $type Avatar or Banner
1739 *
1740 * @return string Html.
1741 */
1742 public function crop_submit_form( $type = 'avatar' ) {
1743
1744 ob_start();
1745
1746 // get file sizes
1747 $files = new UsersWP_Files();
1748 $max_file_size = $files->uwp_get_max_upload_size( $type );
1749
1750 $design_style = uwp_get_option( "design_style", 'bootstrap' );
1751 $template = $design_style ? $design_style . "/modal-profile-image.php" : "modal-profile-image.php";
1752 uwp_get_template( $template );
1753
1754 $content_wrap = $design_style == 'bootstrap' ? '.uwp-profile-image-change-modal .modal-content' : '#uwp-popup-modal-wrap';
1755 $bg_color = apply_filters('uwp_crop_image_bg_color', '', $type);
1756 ?>
1757
1758 <script type="text/javascript">
1759 (function ($, window, undefined) {
1760
1761 var uwp_popup_type = '<?php echo $type; ?>';
1762
1763 $(document).ready(function () {
1764 $("#progressbar").progressbar();
1765 $('.uwp-modal-close').click(function (e) {
1766 e.preventDefault();
1767 var uwp_popup_type = $(this).data('type');
1768 var mod_shadow = jQuery('#uwp-modal-backdrop');
1769 var container = jQuery('#uwp-popup-modal-wrap');
1770 container.hide();
1771 container.replaceWith('<?php $this->modal_loading_html(); ?>');
1772 mod_shadow.remove();
1773 });
1774
1775 $('#uwp_upload_<?php echo $type; ?>').on('change', function (e) {
1776 e.preventDefault();
1777
1778 var container = jQuery('<?php echo esc_attr( $content_wrap );?>');
1779 var err_container = jQuery('#uwp-bs-modal-notice');
1780 err_container.html('');// clear errors on retry
1781
1782 var fd = new FormData();
1783 var files_data = $(this); // The <input type="file" /> field
1784 var file = files_data[0].files[0];
1785 var file_size = file.size;
1786
1787 // file size check
1788 if (file_size && <?php echo absint( $max_file_size );?> && file_size > <?php echo absint( $max_file_size );?>) {
1789 err_container.html('<div class="text-center alert alert-danger"><?php _e( 'File too big.', 'userswp' );?></div>');
1790 return;
1791 }
1792
1793 fd.append('<?php echo $type; ?>', file);
1794 // our AJAX identifier
1795 fd.append('action', 'uwp_avatar_banner_upload');
1796 fd.append('uwp_popup_type', '<?php echo $type; ?>');
1797
1798 $("#progressBar").show().removeClass('d-none');
1799
1800 $.ajax({
1801 // Your server script to process the upload
1802 url: '<?php echo admin_url( 'admin-ajax.php' ); ?>',
1803 type: 'POST',
1804 data: fd,
1805 cache: false,
1806 contentType: false,
1807 processData: false,
1808
1809 xhr: function () {
1810 myXhr = $.ajaxSettings.xhr();
1811 if (myXhr.upload) {
1812 myXhr.upload.addEventListener('progress', showProgress, false);
1813 } else {
1814 console.log("Upload progress is not supported.");
1815 }
1816 return myXhr;
1817 },
1818
1819 success: function (response) {
1820 $("#progressBar").hide();
1821 resp = JSON.parse(response);
1822 if (resp['error'] != "") {
1823 err_container.html(resp['error']);
1824 } else {
1825 resp = JSON.parse(response);
1826 uwp_full_width = resp['uwp_full_width'];
1827 uwp_full_height = resp['uwp_full_height'];
1828 uwp_true_width = resp['uwp_true_width'];
1829 uwp_true_height = resp['uwp_true_height'];
1830
1831 container.html(resp['uwp_popup_content']).find('#uwp-' + uwp_popup_type + '-to-crop').Jcrop({
1832 // onChange: showPreview,
1833 onSelect: updateCoords,
1834 allowResize: true,
1835 allowSelect: false,
1836 bgColor: '<?php echo $bg_color; ?>',
1837 boxWidth: 650, //Maximum width you want for your bigger images
1838 boxHeight: 400, //Maximum Height for your bigger images
1839 setSelect: [0, 0, uwp_full_width, uwp_full_height],
1840 aspectRatio: uwp_full_width / uwp_full_height,
1841 trueSize: [uwp_true_width, uwp_true_height],
1842 minSize: [uwp_full_width, uwp_full_height]
1843 });
1844 }
1845 }
1846 });
1847 });
1848
1849 function showProgress(evt) {
1850 if (evt.lengthComputable) {
1851 var percentComplete = (evt.loaded / evt.total) * 100;
1852 progress(percentComplete, $('#progressBar'));
1853 }
1854 }
1855
1856 function progress(percent, $element) {
1857 percent = Math.round(percent);
1858 var progressBarWidth = percent * ( $element.width() / 100 );
1859 $element.find('div').width(progressBarWidth).html(percent + "%");
1860 }
1861
1862 function updateCoords(c) {
1863 jQuery('#' + uwp_popup_type + '-x').val(c.x);
1864 jQuery('#' + uwp_popup_type + '-y').val(c.y);
1865 jQuery('#' + uwp_popup_type + '-w').val(c.w);
1866 jQuery('#' + uwp_popup_type + '-h').val(c.h);
1867 }
1868
1869 });
1870 }(jQuery, window));
1871 </script>
1872
1873 <?php
1874 $output = ob_get_contents();
1875 ob_end_clean();
1876
1877 return trim( $output );
1878 }
1879
1880 public function ajax_profile_image_remove() {
1881 $type = isset( $_POST['type'] ) ? strip_tags( esc_sql( $_POST['type'] ) ) : '';
1882 if ( $type && in_array( $type, array( 'banner', 'avatar' ) ) ) {
1883 $user_id = get_current_user_id();
1884 uwp_update_usermeta( $user_id, 'banner_thumb', '' );
1885 }
1886 exit();
1887 }
1888
1889 /**
1890 * Defines javascript ajaxurl variable.
1891 *
1892 * @since 1.0.0
1893 * @package userswp
1894 * @return void
1895 */
1896 public function define_ajaxurl() {
1897
1898 echo '<script type="text/javascript">
1899 var ajaxurl = "' . admin_url( 'admin-ajax.php' ) . '";
1900 </script>';
1901 }
1902
1903 public function wpdiscuz_profile_url( $profile_url, $user ) {
1904 if(!$user){
1905 return $profile_url;
1906 }
1907
1908 $allowed = apply_filters( 'uwp_wpdiscuz_profile_url_change', true, $profile_url, $user );
1909 if ( $allowed && isset($user->user_login) ) {
1910 return $this->get_profile_link( $profile_url, $user->ID );
1911 }
1912
1913 return $profile_url;
1914 }
1915
1916 /**
1917 * Display lightbox modal
1918 */
1919 public function lightbox_modals() {
1920 ?>
1921 <div class="modal fade uwp-profile-image-change-modal" tabindex="-1" role="dialog"
1922 aria-labelledby="uwp-profile-modal-title" aria-hidden="true">
1923 <div class="modal-dialog modal-xl">
1924 <div class="modal-content">
1925 <div class="modal-header">
1926 <h5 class="modal-title" id="uwp-profile-modal-title"></h5>
1927 </div>
1928 <div class="modal-body">
1929 <i class="fas fa-circle-notch fa-spin"></i>
1930 </div>
1931 </div>
1932 </div>
1933 </div>
1934 <?php
1935 }
1936
1937 }