PluginProbe
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP / 1.0.23
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP v1.0.23
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.0.23, at includes/class-profile.php

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