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