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

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