PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 4.0.3
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v4.0.3
4.0.3 4.0.2 4.0.1 4.0.0 3.16.6 3.16.5 3.16.4 3.16.3 3.16.2 3.16.1 3.16.0 3.15.9 3.9.9 3.9.5 3.9.6 3.9.7 3.9.8 1.1.7 1.1.8 1.1.9 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 341 releases
← All changes | features/content-restriction/content-restriction-functions.php +265 -4 3.9.8 → 4.0.3 View file →
@@ -34,8 +34,97 @@
34 34 return $wppb_is_post_restricted_arr[$post_id];
35 35
36 36 }
37 37
38 +function wppb_content_restriction_check_user_access( $user_status, $user_roles = array(), $user_id = 0 ) {
39 +
40 + $user_roles_without_logged_in_option = apply_filters( 'wppb_content_restriction_enable_user_roles_without_logged_in_option', true );
41 +
42 + if ( empty( $user_status ) && empty( $user_roles ) ) {
43 + return true;
44 + }
45 +
46 + if ( $user_status === 'loggedin' || ( $user_roles_without_logged_in_option && ! empty( $user_roles ) ) ) {
47 + if ( is_user_logged_in() ) {
48 + if ( ! empty( $user_roles ) ) {
49 + $user_data = get_userdata( $user_id );
50 +
51 + if ( empty( $user_data ) || empty( $user_data->roles ) ) {
52 + return false;
53 + }
54 +
55 + foreach ( $user_roles as $restricted_role ) {
56 + if ( in_array( $restricted_role, $user_data->roles, true ) ) {
57 + return true;
58 + }
59 + }
60 +
61 + return false;
62 + }
63 +
64 + return true;
65 + }
66 +
67 + return false;
68 + }
69 +
70 + return true;
71 +}
72 +
73 +function wppb_content_restriction_get_restricted_term_for_post( $post_id ) {
74 +
75 + $post = get_post( $post_id );
76 +
77 + if ( empty( $post ) ) {
78 + return false;
79 + }
80 +
81 + $taxonomies = get_object_taxonomies( $post->post_type, 'names' );
82 +
83 + if ( empty( $taxonomies ) ) {
84 + return false;
85 + }
86 +
87 + $terms = wp_get_post_terms( $post_id, $taxonomies );
88 +
89 + if ( empty( $terms ) || is_wp_error( $terms ) ) {
90 + return false;
91 + }
92 +
93 + foreach ( $terms as $term ) {
94 + $user_status = get_term_meta( $term->term_id, 'wppb-content-restrict-user-status', true );
95 + $user_roles = get_term_meta( $term->term_id, 'wppb-content-restrict-user-role' );
96 +
97 + if ( ! empty( $user_status ) || ! empty( $user_roles ) ) {
98 + return $term;
99 + }
100 + }
101 +
102 + return false;
103 +}
104 +
105 +function wppb_content_restriction_get_restricted_term_from_query() {
106 +
107 + if ( ! is_category() && ! is_tag() && ! is_tax() ) {
108 + return false;
109 + }
110 +
111 + $term = get_queried_object();
112 +
113 + if ( empty( $term ) || empty( $term->term_id ) || empty( $term->taxonomy ) ) {
114 + return false;
115 + }
116 +
117 + $user_status = get_term_meta( $term->term_id, 'wppb-content-restrict-user-status', true );
118 + $user_roles = get_term_meta( $term->term_id, 'wppb-content-restrict-user-role' );
119 +
120 + if ( empty( $user_status ) && empty( $user_roles ) ) {
121 + return false;
122 + }
123 +
124 + return $term;
125 +}
126 +
38 127 /* Returns the restriction message added by the admin in the settings page or a default message if the first one is missing */
39 128 function wppb_get_restriction_content_message( $message_type = '', $post_id = 0 ) {
40 129
41 130 $wppb_content_restriction_settings = get_option( 'wppb_content_restriction_settings', 'not_found' );
@@ -64,8 +153,34 @@
64 153 return wp_kses_post( $wppb_content_restriction_message );
65 154
66 155 }
67 156
157 +function wppb_get_term_restriction_content_message( $message_type = '', $term_id = 0 ) {
158 +
159 + $wppb_content_restriction_settings = get_option( 'wppb_content_restriction_settings', 'not_found' );
160 + $wppb_content_restriction_message = '';
161 +
162 + if( $message_type == 'logged_out' ) {
163 + $wppb_content_restriction_message = ( ( $wppb_content_restriction_settings != 'not_found' && ! empty( $wppb_content_restriction_settings['message_logged_out'] ) ) ? $wppb_content_restriction_settings['message_logged_out'] : __( 'You must be logged in to view this content.', 'profile-builder' ) );
164 + } elseif ( $message_type == 'logged_in' ) {
165 + $wppb_content_restriction_message = ( ( $wppb_content_restriction_settings != 'not_found' && ! empty( $wppb_content_restriction_settings['message_logged_in'] ) ) ? $wppb_content_restriction_settings['message_logged_in'] : __( 'This content is restricted for your user role.', 'profile-builder' ) );
166 + } else {
167 + $wppb_content_restriction_message = apply_filters( 'wppb_get_restriction_content_message_default', $wppb_content_restriction_message, $message_type, $wppb_content_restriction_settings );
168 + }
169 +
170 + $custom_message_enabled = get_term_meta( $term_id, 'wppb-content-restrict-messages-enabled', true );
171 +
172 + if( ! empty( $term_id ) && ! empty( $custom_message_enabled ) ) {
173 + $custom_message = get_term_meta( $term_id, 'wppb-content-restrict-message-' . $message_type, true );
174 +
175 + if( ! empty( $custom_message ) ) {
176 + $wppb_content_restriction_message = $custom_message;
177 + }
178 + }
179 +
180 + return wp_kses_post( $wppb_content_restriction_message );
181 +}
182 +
68 183 /* Returns the restriction message with any tags processed */
69 184 function wppb_content_restriction_process_content_message( $type, $user_ID, $post_id = 0 ) {
70 185
71 186 $message = wppb_get_restriction_content_message( $type, $post_id );
@@ -75,8 +190,17 @@
75 190 return '<span class="wppb-frontend-restriction-message wppb-content-restriction-message">'. $message .'</span>';
76 191
77 192 }
78 193
194 +function wppb_content_restriction_process_term_content_message( $type, $user_ID, $term_id = 0 ) {
195 +
196 + $message = wppb_get_term_restriction_content_message( $type, $term_id );
197 + $user_info = get_userdata( $user_ID );
198 + $message = wppb_content_restriction_merge_term_tags( $message, $user_info, $term_id );
199 +
200 + return '<span class="wppb-frontend-restriction-message wppb-content-restriction-message">'. $message .'</span>';
201 +}
202 +
79 203 /* Return the restriction message to be displayed to the user. If the current post is not restricted / it was not checked to see if it is restricted an empty string is returned */
80 204 function wppb_content_restriction_get_post_message( $post_id = 0 ) {
81 205
82 206 global $post, $user_ID, $wppb_show_content;
@@ -175,11 +299,19 @@
175 299 if( $current_url == $redirect_url ) {
176 300 return;
177 301 }
178 302
303 + // Allow filtering whether to add the 'redirect_to' parameter
304 + $add_redirect_to = apply_filters( 'wppb_add_redirect_to_param', true, $current_url );
305 +
306 + // Build the query arguments
307 + $query_args = array( 'wppb_referer_url' => urlencode( wppb_curpageurl() ) );
308 +
309 + if ( $add_redirect_to ) {
310 + $query_args['redirect_to'] = $current_url;
311 + }
179 312 // Pass the correct referer URL forward
180 - $redirect_url = add_query_arg( array( 'wppb_referer_url' => urlencode( wppb_curpageurl() ) ), wppb_add_missing_http( $redirect_url ) );
181 -
313 + $redirect_url = add_query_arg( $query_args, wppb_add_missing_http( $redirect_url ) );
182 314 // Redirect
183 315 nocache_headers();
184 316 wp_redirect( apply_filters( 'wppb_restricted_post_redirect_url', $redirect_url ) );
185 317 exit;
@@ -186,8 +318,80 @@
186 318
187 319 }
188 320 add_action( 'template_redirect', 'wppb_content_restriction_post_redirect' );
189 321
322 +/* Checks to see if the current taxonomy archive is restricted and if any redirect URLs are in place the user is redirected to the URL with the highest priority */
323 +function wppb_content_restriction_taxonomy_redirect() {
324 +
325 + $restricted_term = wppb_content_restriction_get_restricted_term_from_query();
326 +
327 + if ( ! $restricted_term ) {
328 + return;
329 + }
330 +
331 + $redirect_url = '';
332 + $term_restriction_type = get_term_meta( $restricted_term->term_id, 'wppb-content-restrict-type', true );
333 + $settings = get_option( 'wppb_content_restriction_settings', array() );
334 + $general_restriction_type = ( ! empty( $settings['restrict_type'] ) ? $settings['restrict_type'] : 'message' );
335 +
336 + if ( $term_restriction_type !== 'redirect' && $general_restriction_type !== 'redirect' ) {
337 + return;
338 + }
339 +
340 + if ( ! in_array( $term_restriction_type, array( 'default', 'redirect' ), true ) ) {
341 + return;
342 + }
343 +
344 + $term_user_status = get_term_meta( $restricted_term->term_id, 'wppb-content-restrict-user-status', true );
345 + $term_user_roles = get_term_meta( $restricted_term->term_id, 'wppb-content-restrict-user-role' );
346 +
347 + if ( wppb_content_restriction_check_user_access( $term_user_status, $term_user_roles, get_current_user_id() ) ) {
348 + return;
349 + }
350 +
351 + if ( $term_restriction_type === 'redirect' ) {
352 + $term_redirect_url_enabled = get_term_meta( $restricted_term->term_id, 'wppb-content-restrict-custom-redirect-url-enabled', true );
353 + $term_redirect_url = get_term_meta( $restricted_term->term_id, 'wppb-content-restrict-custom-redirect-url', true );
354 +
355 + $redirect_url = ( ! empty( $term_redirect_url_enabled ) && ! empty( $term_redirect_url ) ? $term_redirect_url : '' );
356 + }
357 +
358 + // If the taxonomy doesn't have a custom redirect URL set, get the default from the Settings page
359 + if ( empty( $redirect_url ) ) {
360 + $redirect_url = ( ! empty( $settings['redirect_url'] ) ? $settings['redirect_url'] : '' );
361 + }
362 +
363 + if ( empty( $redirect_url ) ) {
364 + return;
365 + }
366 +
367 + // To avoid a redirect loop we break in case the redirect URL is the same as the current page URL
368 + $current_url = wppb_curpageurl();
369 +
370 + if ( $current_url == $redirect_url ) {
371 + return;
372 + }
373 +
374 + // Allow filtering whether to add the 'redirect_to' parameter
375 + $add_redirect_to = apply_filters( 'wppb_add_redirect_to_param', true, $current_url );
376 +
377 + // Build the query arguments
378 + $query_args = array( 'wppb_referer_url' => urlencode( wppb_curpageurl() ) );
379 +
380 + if ( $add_redirect_to ) {
381 + $query_args['redirect_to'] = $current_url;
382 + }
383 +
384 + // Pass the correct referer URL forward
385 + $redirect_url = add_query_arg( $query_args, wppb_add_missing_http( $redirect_url ) );
386 +
387 + // Redirect
388 + nocache_headers();
389 + wp_redirect( apply_filters( 'wppb_restricted_term_redirect_url', $redirect_url, $restricted_term ) );
390 + exit;
391 +}
392 +add_action( 'template_redirect', 'wppb_content_restriction_taxonomy_redirect' );
393 +
190 394 /* Function that searches and replaces merge tags with their values */
191 395 function wppb_content_restriction_merge_tags( $text, $user_info, $post_id = 0 ) {
192 396
193 397 $merge_tags = apply_filters( 'wppb_content_restriction_merge_tags', array( 'display_name', 'unrestricted_user_roles', 'current_user_role' ) );
@@ -201,8 +405,23 @@
201 405 return $text;
202 406
203 407 }
204 408
409 +/* Function that searches and replaces merge tags with their values for taxonomy restriction messages */
410 +function wppb_content_restriction_merge_term_tags( $text, $user_info, $term_id = 0 ) {
411 +
412 + $merge_tags = apply_filters( 'wppb_content_restriction_merge_tags', array( 'display_name', 'unrestricted_user_roles', 'current_user_role' ) );
413 +
414 + if( ! empty( $merge_tags ) ) {
415 + foreach( $merge_tags as $merge_tag ) {
416 + $text = str_replace( '{{'. $merge_tag .'}}', apply_filters( 'wppb_term_content_restriction_merge_tag_'. $merge_tag, '', $user_info, $term_id ), $text );
417 + }
418 + }
419 +
420 + return $text;
421 +
422 +}
423 +
205 424 /* Add functionality for display_name tag */
206 425 function wppb_content_restriction_tag_display_name( $value, $user_info, $post_id = 0 ) {
207 426
208 427 if( ! empty( $user_info->display_name ) ) {
@@ -214,8 +433,9 @@
214 433 }
215 434
216 435 }
217 436 add_filter( 'wppb_content_restriction_merge_tag_display_name', 'wppb_content_restriction_tag_display_name', 10, 3 );
437 +add_filter( 'wppb_term_content_restriction_merge_tag_display_name', 'wppb_content_restriction_tag_display_name', 10, 3 );
218 438
219 439 /* Add functionality for unrestricted_user_roles tag */
220 440 function wppb_content_restriction_tag_unrestricted_user_roles( $value, $user_info, $post_id = 0 ) {
221 441
@@ -235,8 +455,28 @@
235 455
236 456 }
237 457 add_filter( 'wppb_content_restriction_merge_tag_unrestricted_user_roles', 'wppb_content_restriction_tag_unrestricted_user_roles', 10, 3 );
238 458
459 +/* Add functionality for unrestricted_user_roles tag on taxonomy restriction messages */
460 +function wppb_content_restriction_term_tag_unrestricted_user_roles( $value, $user_info, $term_id = 0 ) {
461 +
462 + if( $term_id != 0 ) {
463 + $unrestricted_user_roles = get_term_meta( $term_id, 'wppb-content-restrict-user-role' );
464 +
465 + if( ! empty( $unrestricted_user_roles ) ) {
466 + $user_roles = implode( ', ', $unrestricted_user_roles );
467 +
468 + return $user_roles;
469 + } else {
470 + return '';
471 + }
472 + } else {
473 + return '';
474 + }
475 +
476 +}
477 +add_filter( 'wppb_term_content_restriction_merge_tag_unrestricted_user_roles', 'wppb_content_restriction_term_tag_unrestricted_user_roles', 10, 3 );
478 +
239 479 /* Add functionality for current_user_role tag */
240 480 function wppb_content_restriction_tag_current_user_role( $value, $user_info, $post_id = 0 ) {
241 481
242 482 if( ! empty( $user_info ) && ! empty( $user_info->roles ) ) {
@@ -248,8 +488,9 @@
248 488 }
249 489
250 490 }
251 491 add_filter( 'wppb_content_restriction_merge_tag_current_user_role', 'wppb_content_restriction_tag_current_user_role', 10, 3 );
492 +add_filter( 'wppb_term_content_restriction_merge_tag_current_user_role', 'wppb_content_restriction_tag_current_user_role', 10, 3 );
252 493
253 494 /* Content restriction shortcode */
254 495 function wppb_content_restriction_shortcode( $atts, $content = null ) {
255 496
@@ -264,9 +505,9 @@
264 505 );
265 506
266 507 // Message to replace the content of checks do not match
267 508 if( ! empty( $args['message'] ) ) {
268 - $message = '<span class="wppb-shortcode-restriction-message">' . $args['message'] . '</span>';
509 + $message = '<span class="wppb-shortcode-restriction-message">' . wp_kses_post( $args['message'] ) . '</span>';
269 510 } else {
270 511 $type = ( is_user_logged_in() ? 'logged_in' : 'logged_out' );
271 512 $message = '<span class="wppb-content-restriction-message">' . wpautop( wppb_get_restriction_content_message( $type ) ) . '</span>';
272 513 }
@@ -303,9 +544,29 @@
303 544 return $message;
304 545 }
305 546 }
306 547 }
307 - elseif( ! empty( $args['user_roles'] ) ) {
548 + elseif ( $args['display_to'] == 'not_role' ){
549 + if( ! empty( $args['user_roles'] ) ){
550 + $user_roles = array_map( 'trim', explode( ',', $args['user_roles'] ) );
551 + $user_data = get_userdata( get_current_user_id() );
552 +
553 + if( ! empty( $user_data->roles ) ){
554 + $common_user_roles = array_intersect( $user_roles, $user_data->roles );
555 +
556 + if( ! empty( $common_user_roles ) ) {
557 + return $message;
558 + } else {
559 + return do_shortcode( $content );
560 + }
561 + }
562 + }
563 + else{
564 + return $message;
565 + }
566 +
567 + }
568 + elseif( ! empty( $args['user_roles'] ) && ( $args['display_to'] != 'not_role' || !isset( $args['display_to'] ) ) ) {
308 569 $user_roles = array_map( 'trim', explode( ',', $args['user_roles'] ) );
309 570 $user_data = get_userdata( get_current_user_id() );
310 571
311 572 if( ! empty( $user_data->roles ) ) {