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

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