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

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