PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 3.9.9
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v3.9.9
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
profile-builder / features / content-restriction / content-restriction-filtering.php

content-restriction-filtering.php in User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor 3.9.9, at features/content-restriction/content-restriction-filtering.php

632 lines 22.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
3
4 /* Hijack the content when restrictions are set on a single post */
5 function wppb_content_restriction_filter_content( $content, $post = null ) {
6
7 global $user_ID, $wppb_show_content, $pms_show_content;
8
9 if( is_null( $post ) ) {
10 global $post;
11 }
12
13 /*
14 * Defining this variable:
15 *
16 * $wppb_show_content can have 3 states: null, true and false
17 *
18 * - if the state is "null" the $content is showed, but it did not go through any actual filtering
19 * - if the state is "true" the $content is showed, but it did go through filters that explicitly said the $content should be shown
20 * - if the state is "false" the $content is not showed, it is replaced with a restriction message, thus it explicitly says that it was filtered and access is denied to it
21 *
22 */
23 $wppb_show_content = null;
24
25 // Show for administrators
26 if( current_user_can( 'manage_options' ) ) {
27 return $content;
28 }
29
30 // Check if any PMS restriction should take place. PMS restrictions have priority
31 if( $pms_show_content === false ) {
32 return $content;
33 }
34
35 // Get user roles that have access to this post
36 if ( isset( $post ) && isset( $post->ID ) ) {
37 $user_status = get_post_meta($post->ID, 'wppb-content-restrict-user-status', true);
38 $post_user_roles = get_post_meta($post->ID, 'wppb-content-restrict-user-role');
39 }
40
41 if( empty( $user_status ) && empty( $post_user_roles ) ) {
42 return $content;
43 } else if( $user_status == 'loggedin' ) {
44 if( is_user_logged_in() ) {
45 if( ! empty( $post_user_roles ) ) {
46 $user_data = get_userdata( $user_ID );
47
48 foreach( $post_user_roles as $post_user_role ) {
49 foreach( $user_data->roles as $role ) {
50 if( $post_user_role == $role ) {
51 $wppb_show_content = true;
52 return $content;
53 }
54 }
55 }
56
57 $wppb_show_content = false;
58
59 $message = wppb_content_restriction_process_content_message( 'logged_in', $user_ID, $post->ID );
60
61 return do_shortcode( apply_filters( 'wppb_content_restriction_message_logged_in', $message, $content, $post, $user_ID ) );
62 } else {
63 return $content;
64 }
65 } else {
66 // If user is not logged in prompt the correct message
67 $wppb_show_content = false;
68
69 $message = wppb_content_restriction_process_content_message( 'logged_out', $user_ID, $post->ID );
70
71 return do_shortcode( apply_filters( 'wppb_content_restriction_message_logged_out', $message, $content, $post, $user_ID ) );
72 }
73 }
74
75 return $content;
76
77 }
78 add_filter( 'the_content', 'wppb_content_restriction_filter_content', 12, 2 );
79 add_filter( 'wppb_content_restriction_post_check', 'wppb_content_restriction_filter_content', 10, 2 );
80
81 /**
82 * Function that checks if a post id is restricted with profile builder
83 * @param $post_id
84 * @return bool true for when the post is restricted and false for when it's not
85 */
86 function wppb_check_content_restriction_on_post_id( $post_id ){
87 global $user_ID;
88
89 // Get user roles that have access to this post
90 $user_status = get_post_meta( $post_id, 'wppb-content-restrict-user-status', true );
91 $post_user_roles = get_post_meta( $post_id, 'wppb-content-restrict-user-role' );
92
93 if( empty( $user_status ) && empty( $post_user_roles ) ) {
94 return false;
95 } else if( $user_status == 'loggedin' ) {
96 if( is_user_logged_in() ) {
97 if( ! empty( $post_user_roles ) ) {
98 $user_data = get_userdata( $user_ID );
99 foreach( $post_user_roles as $post_user_role ) {
100 foreach( $user_data->roles as $role ) {
101 if( $post_user_role == $role ) {
102 return false;
103 }
104 }
105 }
106 return true;
107 } else {
108 return false;
109 }
110 } else {
111 return true;
112 }
113 }
114
115 return false;
116 }
117
118
119 /* Checks to see if the attachment image is restricted and returns false instead of the image if it is restricted */
120 function wppb_content_restriction_filter_attachment_image_src( $image, $attachment_id ) {
121
122 if( is_admin() ) {
123 return $image;
124 }
125
126 if( wppb_content_restriction_is_post_restricted( $attachment_id ) ) {
127 return false;
128 }
129
130 return $image;
131
132 }
133 add_filter( 'wp_get_attachment_image_src', 'wppb_content_restriction_filter_attachment_image_src', 10, 2 );
134
135 /* Checks to see if the attachment is restricted and returns false instead of the metadata if it is restricted */
136 function wppb_content_restriction_filter_attachment_metadata( $data, $attachment_id ) {
137
138 if( is_admin() ) {
139 return $data;
140 }
141
142 if( wppb_content_restriction_is_post_restricted( $attachment_id ) ) {
143 return false;
144 }
145
146 return $data;
147
148 }
149 add_filter( 'wp_get_attachment_metadata', 'wppb_content_restriction_filter_attachment_metadata', 10, 2 );
150
151 /* Checks to see if the attachment thumb is restricted and returns false instead of the thumb url if it is restricted */
152 function wppb_content_restriction_filter_attachment_thumb_url( $url, $attachment_id ) {
153
154 if( is_admin() ) {
155 return $url;
156 }
157
158 if( wppb_content_restriction_is_post_restricted( $attachment_id ) ) {
159 return false;
160 }
161
162 return $url;
163
164 }
165 add_filter( 'wp_get_attachment_thumb_url', 'wppb_content_restriction_filter_attachment_thumb_url', 10, 2 );
166
167 /* Checks to see if the attachment is restricted and returns an empty string instead of the attachment url if it is restricted*/
168 function wppb_content_restriction_filter_attachment_url( $url, $attachment_id ) {
169
170 if( is_admin() ) {
171 return $url;
172 }
173
174 if( wppb_content_restriction_is_post_restricted( $attachment_id ) ) {
175 return '';
176 }
177
178 return $url;
179
180 }
181 add_filter( 'wp_get_attachment_url', 'wppb_content_restriction_filter_attachment_url', 10, 2 );
182 add_filter( 'attachment_link', 'wppb_content_restriction_filter_attachment_url', 10, 2 );
183
184 /* Formats the error messages to display accordingly to the WYSIWYG editor */
185 function wppb_content_restriction_message_wpautop( $message = '' ) {
186
187 if( ! empty( $message ) ) {
188 $message = wpautop( $message );
189 }
190
191 return apply_filters( 'wppb_content_restriction_message_wpautop', $message );
192
193 }
194 add_filter( 'wppb_content_restriction_message_logged_in', 'wppb_content_restriction_message_wpautop', 30, 1 );
195 add_filter( 'wppb_content_restriction_message_logged_out', 'wppb_content_restriction_message_wpautop', 30, 1 );
196
197 /* Adds a preview of the restricted post before the default restriction messages */
198 function wppb_content_restriction_add_post_preview( $message, $content, $post, $user_ID ) {
199
200 $preview = '';
201 $settings = get_option( 'wppb_content_restriction_settings' );
202 $preview_option = ( ! empty( $settings['post_preview'] ) ? $settings['post_preview'] : '' );
203
204 if( empty( $preview_option ) || $preview_option == 'none' ) {
205 return $message;
206 }
207
208 $post_content = $content;
209
210 // Trim the content
211 if( $preview_option == 'trim-content' ) {
212 $length = ( ! empty( $settings['post_preview_length'] ) ? (int) $settings['post_preview_length'] : 0 );
213
214 if( $length !== 0 ) {
215 // Do shortcodes on the content
216 $post_content = do_shortcode( $post_content );
217
218 // Trim the preview
219 $preview = wp_trim_words( $post_content, $length, apply_filters( 'wppb_content_restriction_post_preview_more', __( '&hellip;', 'profile-builder' ) ) );
220 }
221 }
222
223 // More tag
224 if( $preview_option == 'more-tag' ) {
225 $content_parts = get_extended( $post->post_content );
226
227 if( ! empty( $content_parts['extended'] ) ) {
228 $preview = $content_parts['main'];
229 }
230 }
231
232 // Return the preview
233 return wpautop( $preview ) . $message;
234
235 }
236 add_filter( 'wppb_content_restriction_message_logged_in', 'wppb_content_restriction_add_post_preview', 30, 4 );
237 add_filter( 'wppb_content_restriction_message_logged_out', 'wppb_content_restriction_add_post_preview', 30, 4 );
238
239 /* if the Static Posts Page has a restriction on it hijack the query */
240 add_action( 'template_redirect', 'wppb_content_restriction_posts_page_handle_query', 1 );
241 function wppb_content_restriction_posts_page_handle_query(){
242 if( is_home() ){
243 $posts_page_id = get_option( 'page_for_posts' );
244 if( $posts_page_id ) {
245 if (wppb_check_content_restriction_on_post_id($posts_page_id)) {
246 wppb_content_restriction_force_page($posts_page_id);
247 }
248 }
249 }
250 }
251
252
253 /* if the Static Posts Page has a restriction on it hijack the template back to the Page Template */
254 add_filter( 'template_include', 'wppb_content_restriction_posts_page_template', 100 );
255 function wppb_content_restriction_posts_page_template( $template ){
256 if( is_home() ){
257 $posts_page_id = get_option( 'page_for_posts' );
258 if( $posts_page_id ) {
259 if (wppb_check_content_restriction_on_post_id($posts_page_id)) {
260 $template = get_page_template();
261 }
262 }
263 }
264 return $template;
265 }
266
267 /* Change the query to a single post */
268 function wppb_content_restriction_force_page( $posts_page_id ){
269 if( $posts_page_id ) {
270 global $wp_query, $post;
271 $post = get_post($posts_page_id);
272 $wp_query->posts = array($post);
273 $wp_query->post_count = 1;
274 $wp_query->is_singular = true;
275 $wp_query->is_singule = true;
276 $wp_query->is_archive = false;
277 }
278 }
279
280 // add callback to function that hides comments if post content is restricted
281 function wppb_comments_hide_callback_function( $args ) {
282 global $post;
283
284 if ( empty( $post->ID ) ) return $args;
285
286 if( wppb_content_restriction_is_post_restricted( $post->ID ) )
287 $args[ 'callback' ] = 'wppb_comments_restrict_view';
288
289 return $args;
290 }
291
292 // display restriction message if post content is restricted
293 function wppb_comments_restrict_view( $comment, $args, $depth ) {
294 static $message_shown = false;
295
296 if ( !$message_shown ) {
297
298 if ( is_user_logged_in() )
299 printf( '<p>%s</p>', esc_html( apply_filters( 'wppb_comments_restriction_message_user_role', __( 'Comments are restricted for your user role.', 'profile-builder' ) ) ) );
300 else
301 printf( '<p>%s</p>', esc_html( apply_filters( 'wppb_comments_restriction_message_logged_out', __( 'You must be logged in to view the comments.', 'profile-builder' ) ) ) );
302
303 $message_shown = true;
304 }
305 }
306
307 // restrict replying for restricted posts
308 function wppb_comments_restrict_replying( $open, $post_id ) {
309 // Show for administrators
310 if( current_user_can( 'manage_options' ) && is_admin() )
311 return $open;
312
313 if( wppb_content_restriction_is_post_restricted( $post_id ) )
314 return false;
315
316 return $open;
317 }
318
319 $wppb_cr_settings = get_option( 'wppb_content_restriction_settings' );
320
321 // add filter to hide comments and replies if post content is restricted
322 if ( isset( $wppb_cr_settings[ 'contentRestriction' ] ) && $wppb_cr_settings[ 'contentRestriction' ] == 'yes' && apply_filters( 'wppb_enable_comment_restriction', true ) ) {
323 add_filter( 'comments_open', 'wppb_comments_restrict_replying', 20, 2 );
324 add_filter( 'wp_list_comments_args', 'wppb_comments_hide_callback_function', 999 );
325 }
326
327 if( !function_exists( 'pms_exclude_restricted_comments' ) ){
328 add_filter( 'the_comments', 'wppb_exclude_restricted_comments', 10, 2 );
329 function wppb_exclude_restricted_comments( $comments, $query ){
330 if( !empty( $comments ) && !current_user_can( 'manage_options' ) ){
331 $user_id = get_current_user_id();
332 foreach ( $comments as $key => $comment ){
333 $post = get_post( $comment->comment_post_ID );
334 if( ( $post->post_type == 'private-page' && $user_id != (int)$post->post_author ) || ( function_exists( 'wppb_content_restriction_is_post_restricted' ) && wppb_content_restriction_is_post_restricted( $comment->comment_post_ID ) ) || ( function_exists( 'pms_is_post_restricted' ) && pms_is_post_restricted( $comment->comment_post_ID ) ) ){
335 unset( $comments[$key] );
336 }
337 }
338 }
339 return $comments;
340 }
341 }
342
343 /**
344 * WooCommerce specific filters
345 */
346 if( function_exists( 'wc_get_page_id' ) ) {
347
348 /**
349 * Function that restricts product content
350 *
351 * @param $output What is returned
352 * @return string
353 */
354 function wppb_woo_restrict_product_content( $output ){
355 global $post, $user_ID;
356
357 if ( strpos( $post->post_password, 'wppb_woo_product_restricted_' ) !== false ) {
358
359 // user does not have access, filter the content
360 $output = '';
361
362 // check if restricted post preview is set
363 $settings = get_option( 'wppb_content_restriction_settings' );
364 $preview_option = ( !empty( $settings['restricted_post_preview']['option'] ) ? $settings['restricted_post_preview']['option'] : '' );
365
366 if ( !empty($preview_option) && ($preview_option != 'none') ) {
367 // display product title
368
369 ob_start();
370
371 echo '<div class="summary entry-summary">';
372 wc_get_template( 'single-product/title.php' );
373 echo '</div>';
374
375 $output = ob_get_clean();
376 }
377
378 if( !is_user_logged_in() )
379 $message = wppb_content_restriction_process_content_message( 'logged_out', $user_ID, $post->ID );
380 else if( !wppb_woo_is_product_purchasable() && !wppb_content_restriction_is_post_restricted() )
381 $message = wppb_content_restriction_process_content_message( 'purchasing_restricted', $user_ID, $post->ID );
382 else
383 $message = wppb_content_restriction_process_content_message( 'logged_in', $user_ID, $post->ID );
384
385 $message = '<div class="woocommerce"><div class="woocommerce-info wppb-woo-restriction-message wppb-woo-restricted-product-purchasing-message">' . $message . '</div></div>';
386
387 $output .= $message;
388
389 $post->post_password = null;
390
391 }
392
393 return $output;
394 }
395
396 /**
397 * Function that checks if current user can purchase the product
398 *
399 * @param string|WC_Product $product The product
400 * @return bool
401 */
402 function wppb_woo_is_product_purchasable( $product = '' ){
403 global $user_ID, $post;
404
405 if ( empty($product) )
406 $product = wc_get_product( $post->ID );
407
408 if( false == $product )
409 return false;
410
411 /**
412 * Show "buy now" for the `manage_options` and `pms_bypass_content_restriction` capabilities
413 */
414 if( current_user_can( 'manage_options' ) )
415 return true;
416
417 // if is variation, use the id of the parent product
418 if ( $product->is_type( 'variation' ) )
419 $product_id = $product->get_parent_id();
420 else
421 $product_id = $product->get_id();
422
423 // Get subscription plans that can purchase this product
424 $user_status = get_post_meta( $product_id, 'wppb-purchase-restrict-user-status', true );
425 $product_user_roles = get_post_meta( $product_id, 'wppb-purchase-restrict-user-role' );
426
427 if( empty( $user_status ) && empty( $product_user_roles ) ) {
428 //everyone can purchase
429 return true;
430
431 } else if( !empty( $product_user_roles ) && is_user_logged_in() ) {
432
433 $user_data = get_userdata( $user_ID );
434 foreach( $product_user_roles as $product_user_role ) {
435 foreach( $user_data->roles as $role ) {
436 if( $product_user_role == $role ) {
437 return true;
438 }
439 }
440 }
441
442 return false;
443
444 } else if ( !is_user_logged_in() && (
445 ( !empty( $user_status ) && $user_status == 'loggedin' ) || ( !empty( $product_user_roles ) )
446 ) ) {
447
448 return false;
449 }
450
451 return true;
452 }
453
454 /**
455 * Restrict the Shop page
456 *
457 * @param $template The shop page template to return
458 * @return string
459 */
460 function wppb_woo_restrict_shop_page($template){
461
462 // check if we're on the Shop page (set under WooCommerce Settings -> Products -> Display)
463 if (is_post_type_archive('product') || is_page(wc_get_page_id('shop'))) {
464
465 // get the ID of the shop page
466 $post_id = wc_get_page_id('shop');
467
468 if (($post_id != -1) && wppb_check_content_restriction_on_post_id($post_id)) {
469
470 $shop_page = get_post($post_id);
471
472 setup_postdata($shop_page);
473
474 $template = WPPB_PLUGIN_DIR . 'features/content-restriction/templates/archive-product.php';
475
476 wp_reset_postdata();
477 }
478
479 }
480
481 return $template;
482 }
483 add_filter( 'template_include', 'wppb_woo_restrict_shop_page', 40 );
484
485 /**
486 * Function that restricts product viewing by hijacking WooCommerce product password protection (hide_content restriction mode)
487 *
488 */
489 function wppb_woo_maybe_password_protect_product(){
490 global $post;
491
492 // if the product is to be restricted, and doesn't already have a password,
493 // set a password so as to perform the actions we want
494 if ( wppb_content_restriction_is_post_restricted() && ! post_password_required() ) {
495
496 $post->post_password = uniqid( 'wppb_woo_product_restricted_' );
497
498 add_filter( 'the_password_form', 'wppb_woo_restrict_product_content' );
499
500 }
501 }
502 add_action( 'woocommerce_before_single_product', 'wppb_woo_maybe_password_protect_product' );
503
504
505 /**
506 * Function that hides the price for view-restricted products
507 *
508 * @param float $price The product price
509 * @param WC_Product $product The product
510 * @return string
511 */
512 function wppb_woo_hide_restricted_product_price( $price, WC_Product $product ){
513 // check if current user can view this product, and if not, remove the price
514 if ( wppb_content_restriction_is_post_restricted( $product->get_id() ) ) {
515
516 $price = '';
517 }
518
519 return $price;
520 }
521 add_filter( 'woocommerce_get_price_html', 'wppb_woo_hide_restricted_product_price', 9, 2);
522
523
524 /**
525 * Function that hides the product image thumbnail for view-restricted products
526 *
527 */
528 function wppb_woo_maybe_remove_product_thumbnail(){
529 global $post, $wppb_woo_product_thumbnail_restricted;
530
531 $wppb_woo_product_thumbnail_restricted = false;
532
533 // skip if the product thumbnail is not shown anyway
534 if ( ! has_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail' ) ) {
535 return;
536 }
537
538 // if product is view restricted, do not display the product thumbnail
539 if ( wppb_content_restriction_is_post_restricted($post->ID) ) {
540
541 // indicate that we removed the product thumbnail
542 $wppb_woo_product_thumbnail_restricted = true;
543
544 // remove the product thumbnail and replace it with the placeholder image
545 remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail' );
546 add_action( 'woocommerce_before_shop_loop_item_title', 'wppb_woo_template_loop_product_thumbnail_placeholder', 10 );
547 }
548
549 }
550 add_action( 'woocommerce_before_shop_loop_item_title', 'wppb_woo_maybe_remove_product_thumbnail', 5 );
551
552
553 // return placeholder thumbnail instead of image for view-restricted products
554 function wppb_woo_template_loop_product_thumbnail_placeholder(){
555 if ( wc_placeholder_img_src() ) {
556
557 echo wp_kses_post( wc_placeholder_img( 'shop_catalog' ) );
558 }
559 }
560
561 // restore product thumbnail for the next product in the loop
562 function wppb_woo_restore_product_thumbnail(){
563 global $wppb_woo_product_thumbnail_restricted;
564
565 if ( $wppb_woo_product_thumbnail_restricted
566 && ! has_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail' ) ) {
567
568 add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
569 remove_action( 'woocommerce_before_shop_loop_item_title', 'wppb_woo_template_loop_product_thumbnail_placeholder' );
570 }
571 }
572 add_action( 'woocommerce_after_shop_loop_item_title', 'wppb_woo_restore_product_thumbnail', 5 );
573
574
575 /**
576 * Function that restricts product purchasing
577 *
578 * @param boolean $purchasable Whether the product is purchasable or not
579 * @param $product The product
580 * @return bool
581 */
582 function wppb_woo_product_is_purchasable( $purchasable, $product ){
583
584 // if the product is view-restricted or purchase-restricted it cannot be purchased
585 if ( wppb_content_restriction_is_post_restricted( $product->get_id() ) || !wppb_woo_is_product_purchasable( $product ) )
586 $purchasable = false;
587
588 // double-check for variations; if parent is not purchasable, then neither should be the variation
589 if ( $purchasable && $product->is_type( array( 'variation' ) ) ) {
590
591 $parent = wc_get_product( $product->get_parent_id() );
592
593 if( !empty( $parent ) && is_object( $parent ) )
594 $purchasable = $parent->is_purchasable();
595
596 }
597
598 return $purchasable;
599
600 }
601 add_filter( 'woocommerce_is_purchasable', 'wppb_woo_product_is_purchasable', 10, 2 );
602 add_filter( 'woocommerce_variation_is_purchasable', 'wppb_woo_product_is_purchasable', 10, 2 );
603
604 /**
605 * Function that shows the product purchasing restricted message
606 *
607 **/
608 function wppb_woo_single_product_purchasing_restricted_message(){
609 global $wppb_show_content, $post;
610
611 if( empty( $post->ID ) )
612 return;
613
614 if ( !wppb_woo_is_product_purchasable() ) {
615
616 // product purchasing is restricted
617 $wppb_show_content = false;
618
619 if( is_user_logged_in() )
620 $message = wppb_content_restriction_process_content_message( 'purchasing_restricted', get_current_user_id(), $post->ID );
621 else
622 $message = wppb_content_restriction_process_content_message( 'logged_out', get_current_user_id(), $post->ID );
623
624 echo wp_kses_post( $message );
625 }
626 }
627 add_action( 'woocommerce_single_product_summary', 'wppb_woo_single_product_purchasing_restricted_message', 30 );
628
629 // Apply wpautop() to "purchasing restricted" messages as well
630 add_filter( 'wppb_content_restriction_message_purchasing_restricted', 'wppb_content_restriction_message_wpautop', 30, 1 );
631
632 }