PluginProbe
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP / 1.0.20
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP v1.0.20
1.2.73 1.2.72 1.2.71 1.2.70 1.2.69 1.2.68 1.2.67 1.2.66 1.2.65 1.2.64 1.2.63 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 All 173 releases
userswp / includes / class-profile.php

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

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