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

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