PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 4.0.0
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v4.0.0
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 4.0.0, at features/content-restriction/content-restriction-filtering.php

919 lines 34.1 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_is_post_restricted_arr;
8
9 if( is_null( $post ) ) {
10 global $post;
11 }
12
13 if( empty( $post->ID ) )
14 return $content;
15
16 $user_ID = get_current_user_id();
17
18 /*
19 * Defining this variable:
20 *
21 * $wppb_show_content can have 3 states: null, true and false
22 *
23 * - if the state is "null" the $content is showed, but it did not go through any actual filtering
24 * - if the state is "true" the $content is showed, but it did go through filters that explicitly said the $content should be shown
25 * - 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
26 *
27 */
28 $wppb_show_content = null;
29
30 // Show for administrators
31 if( current_user_can( 'manage_options' ) ) {
32 return $content;
33 }
34
35 // Check if any PMS restriction should take place. PMS restrictions have priority
36 if( isset( $pms_is_post_restricted_arr[$post->ID] ) && $pms_is_post_restricted_arr[$post->ID] === true ) {
37 return $content;
38 }
39
40 $restricted_term = wppb_content_restriction_get_restricted_term_for_post( $post->ID );
41
42 if ( $restricted_term ) {
43 $term_user_status = get_term_meta( $restricted_term->term_id, 'wppb-content-restrict-user-status', true );
44 $term_user_roles = get_term_meta( $restricted_term->term_id, 'wppb-content-restrict-user-role' );
45
46 if ( ! wppb_content_restriction_check_user_access( $term_user_status, $term_user_roles, $user_ID ) ) {
47 $wppb_show_content = false;
48
49 if ( is_user_logged_in() ) {
50 $message = wppb_content_restriction_process_term_content_message( 'logged_in', $user_ID, $restricted_term->term_id );
51
52 return do_shortcode( apply_filters( 'wppb_content_restriction_message_logged_in', $message, $content, $post, $user_ID ) );
53 }
54
55 $message = wppb_content_restriction_process_term_content_message( 'logged_out', $user_ID, $restricted_term->term_id );
56
57 return do_shortcode( apply_filters( 'wppb_content_restriction_message_logged_out', $message, $content, $post, $user_ID ) );
58 }
59 }
60
61 // Get user roles that have access to this post
62 if ( isset( $post ) && isset( $post->ID ) ) {
63 $user_status = get_post_meta($post->ID, 'wppb-content-restrict-user-status', true);
64 $post_user_roles = get_post_meta($post->ID, 'wppb-content-restrict-user-role');
65
66 /**
67 * Filter for disabling/enabling User Role based content restriction for Logged-Out users.
68 * By default, restrictions apply even if logged in users is not selected.
69 * If this filter is set to false, restrictions will apply only if the logged in users option is selected alongside user roles.
70 *
71 * @param bool $user_roles_without_logged_in_option -> Defaults to TRUE.
72 *
73 */
74 $user_roles_without_logged_in_option = apply_filters( 'wppb_content_restriction_enable_user_roles_without_logged_in_option', true );
75 }
76
77 if( empty( $user_status ) && empty( $post_user_roles ) ) {
78 $wppb_show_content = true;
79 return $content;
80 } else if( $user_status == 'loggedin' || ( $user_roles_without_logged_in_option && !empty( $post_user_roles ) ) ) {
81 if( is_user_logged_in() ) {
82 if( ! empty( $post_user_roles ) ) {
83 $user_data = get_userdata( $user_ID );
84
85 foreach( $post_user_roles as $post_user_role ) {
86 foreach( $user_data->roles as $role ) {
87 if( $post_user_role == $role ) {
88 $wppb_show_content = true;
89 return $content;
90 }
91 }
92 }
93
94 $wppb_show_content = false;
95
96 $message = wppb_content_restriction_process_content_message( 'logged_in', $user_ID, $post->ID );
97
98 return do_shortcode( apply_filters( 'wppb_content_restriction_message_logged_in', $message, $content, $post, $user_ID ) );
99 } else {
100 return $content;
101 }
102 } else {
103 // If user is not logged in prompt the correct message
104 $wppb_show_content = false;
105
106 $message = wppb_content_restriction_process_content_message( 'logged_out', $user_ID, $post->ID );
107
108 return do_shortcode( apply_filters( 'wppb_content_restriction_message_logged_out', $message, $content, $post, $user_ID ) );
109 }
110 }
111
112 return $content;
113
114 }
115 add_filter( 'the_content', 'wppb_content_restriction_filter_content', 12, 2 );
116 add_filter( 'wppb_content_restriction_post_check', 'wppb_content_restriction_filter_content', 10, 2 );
117
118 /**
119 * Function that checks if a post id is restricted with profile builder
120 * @param $post_id
121 * @return bool true for when the post is restricted and false for when it's not
122 */
123 function wppb_check_content_restriction_on_post_id( $post_id ){
124 global $user_ID;
125
126 // Get user roles that have access to this post
127 $user_status = get_post_meta( $post_id, 'wppb-content-restrict-user-status', true );
128 $post_user_roles = get_post_meta( $post_id, 'wppb-content-restrict-user-role' );
129
130 /**
131 * Filter for disabling/enabling User Role based content restriction for Logged-Out users.
132 * By default, restrictions apply even if logged in users is not selected.
133 * If this filter is set to false, restrictions will apply only if the logged in users option is selected alongside user roles.
134 *
135 * @param bool $user_roles_without_logged_in_option -> Defaults to TRUE.
136 *
137 */
138 $user_roles_without_logged_in_option = apply_filters( 'wppb_content_restriction_enable_user_roles_without_logged_in_option', true );
139
140 if( empty( $user_status ) && empty( $post_user_roles ) ) {
141 return false;
142 } else if( $user_status == 'loggedin' || ( $user_roles_without_logged_in_option && !empty( $post_user_roles ) ) ) {
143 if( is_user_logged_in() ) {
144 if( ! empty( $post_user_roles ) ) {
145 $user_data = get_userdata( $user_ID );
146 foreach( $post_user_roles as $post_user_role ) {
147 foreach( $user_data->roles as $role ) {
148 if( $post_user_role == $role ) {
149 return false;
150 }
151 }
152 }
153 return true;
154 } else {
155 return false;
156 }
157 } else {
158 return true;
159 }
160 }
161
162 return false;
163 }
164
165
166 /* Checks to see if the attachment image is restricted and returns false instead of the image if it is restricted */
167 function wppb_content_restriction_filter_attachment_image_src( $image, $attachment_id ) {
168
169 if( is_admin() ) {
170 return $image;
171 }
172
173 if( wppb_content_restriction_is_post_restricted( $attachment_id ) ) {
174 return false;
175 }
176
177 return $image;
178
179 }
180 add_filter( 'wp_get_attachment_image_src', 'wppb_content_restriction_filter_attachment_image_src', 10, 2 );
181
182 /* Checks to see if the attachment is restricted and returns false instead of the metadata if it is restricted */
183 function wppb_content_restriction_filter_attachment_metadata( $data, $attachment_id ) {
184
185 if( is_admin() ) {
186 return $data;
187 }
188
189 if( wppb_content_restriction_is_post_restricted( $attachment_id ) ) {
190 return false;
191 }
192
193 return $data;
194
195 }
196 add_filter( 'wp_get_attachment_metadata', 'wppb_content_restriction_filter_attachment_metadata', 10, 2 );
197
198 /* Checks to see if the attachment thumb is restricted and returns false instead of the thumb url if it is restricted */
199 function wppb_content_restriction_filter_attachment_thumb_url( $url, $attachment_id ) {
200
201 if( is_admin() ) {
202 return $url;
203 }
204
205 if( wppb_content_restriction_is_post_restricted( $attachment_id ) ) {
206 return false;
207 }
208
209 return $url;
210
211 }
212 add_filter( 'wp_get_attachment_thumb_url', 'wppb_content_restriction_filter_attachment_thumb_url', 10, 2 );
213
214 /* Checks to see if the attachment is restricted and returns an empty string instead of the attachment url if it is restricted*/
215 function wppb_content_restriction_filter_attachment_url( $url, $attachment_id ) {
216
217 if( is_admin() ) {
218 return $url;
219 }
220
221 if( wppb_content_restriction_is_post_restricted( $attachment_id ) ) {
222 return '';
223 }
224
225 return $url;
226
227 }
228 add_filter( 'wp_get_attachment_url', 'wppb_content_restriction_filter_attachment_url', 10, 2 );
229 add_filter( 'attachment_link', 'wppb_content_restriction_filter_attachment_url', 10, 2 );
230
231 /* Formats the error messages to display accordingly to the WYSIWYG editor */
232 function wppb_content_restriction_message_wpautop( $message = '' ) {
233
234 if( ! empty( $message ) ) {
235 $message = wpautop( $message );
236 }
237
238 return apply_filters( 'wppb_content_restriction_message_wpautop', $message );
239
240 }
241 add_filter( 'wppb_content_restriction_message_logged_in', 'wppb_content_restriction_message_wpautop', 30, 1 );
242 add_filter( 'wppb_content_restriction_message_logged_out', 'wppb_content_restriction_message_wpautop', 30, 1 );
243
244 /* Adds a preview of the restricted post before the default restriction messages */
245 function wppb_content_restriction_add_post_preview( $message, $content, $post, $user_ID ) {
246
247 $preview = '';
248 $settings = get_option( 'wppb_content_restriction_settings' );
249 $preview_option = ( ! empty( $settings['post_preview'] ) ? $settings['post_preview'] : '' );
250
251 if( empty( $preview_option ) || $preview_option == 'none' ) {
252 return $message;
253 }
254
255 $post_content = $content;
256
257 // Trim the content
258 if( $preview_option == 'trim-content' ) {
259 $length = ( ! empty( $settings['post_preview_length'] ) ? (int) $settings['post_preview_length'] : 0 );
260
261 if( $length !== 0 ) {
262 // Do shortcodes on the content
263 $post_content = do_shortcode( $post_content );
264
265 // Trim the preview
266 $preview = wp_trim_words( $post_content, $length, apply_filters( 'wppb_content_restriction_post_preview_more', __( '&hellip;', 'profile-builder' ) ) );
267 }
268 }
269
270 // More tag
271 if( $preview_option == 'more-tag' ) {
272 $content_parts = get_extended( $post->post_content );
273
274 if( ! empty( $content_parts['extended'] ) ) {
275 $preview = $content_parts['main'];
276 }
277 }
278
279 // Return the preview
280 return wpautop( $preview ) . $message;
281
282 }
283 add_filter( 'wppb_content_restriction_message_logged_in', 'wppb_content_restriction_add_post_preview', 30, 4 );
284 add_filter( 'wppb_content_restriction_message_logged_out', 'wppb_content_restriction_add_post_preview', 30, 4 );
285
286 /* if the Static Posts Page has a restriction on it hijack the query */
287 add_action( 'template_redirect', 'wppb_content_restriction_posts_page_handle_query', 1 );
288 function wppb_content_restriction_posts_page_handle_query(){
289 if( is_home() ){
290 $posts_page_id = get_option( 'page_for_posts' );
291 if( $posts_page_id ) {
292 if (wppb_check_content_restriction_on_post_id($posts_page_id)) {
293 wppb_content_restriction_force_page($posts_page_id);
294 }
295 }
296 }
297 }
298
299
300 /* if the Static Posts Page has a restriction on it hijack the template back to the Page Template */
301 add_filter( 'template_include', 'wppb_content_restriction_posts_page_template', 100 );
302 function wppb_content_restriction_posts_page_template( $template ){
303 if( is_home() ){
304 $posts_page_id = get_option( 'page_for_posts' );
305 if( $posts_page_id ) {
306 if (wppb_check_content_restriction_on_post_id($posts_page_id)) {
307 $template = get_page_template();
308 }
309 }
310 }
311 return $template;
312 }
313
314 /* Change the query to a single post */
315 function wppb_content_restriction_force_page( $posts_page_id ){
316 if( $posts_page_id ) {
317 global $wp_query, $post;
318 $post = get_post($posts_page_id);
319 $wp_query->posts = array($post);
320 $wp_query->post_count = 1;
321 $wp_query->is_singular = true;
322 $wp_query->is_singule = true;
323 $wp_query->is_archive = false;
324 }
325 }
326
327 // add callback to function that hides comments if post content is restricted
328 function wppb_comments_hide_callback_function( $args ) {
329 global $post;
330
331 if ( empty( $post->ID ) ) return $args;
332
333 if( wppb_content_restriction_is_post_restricted( $post->ID ) )
334 $args[ 'callback' ] = 'wppb_comments_restrict_view';
335
336 return $args;
337 }
338
339 // display restriction message if post content is restricted
340 function wppb_comments_restrict_view( $comment, $args, $depth ) {
341 static $message_shown = false;
342
343 if ( !$message_shown ) {
344
345 if ( is_user_logged_in() )
346 printf( '<p>%s</p>', esc_html( apply_filters( 'wppb_comments_restriction_message_user_role', __( 'Comments are restricted for your user role.', 'profile-builder' ) ) ) );
347 else
348 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' ) ) ) );
349
350 $message_shown = true;
351 }
352 }
353
354 // restrict replying for restricted posts
355 function wppb_comments_restrict_replying( $open, $post_id ) {
356 // Show for administrators
357 if( current_user_can( 'manage_options' ) && is_admin() )
358 return $open;
359
360 if( wppb_content_restriction_is_post_restricted( $post_id ) )
361 return false;
362
363 return $open;
364 }
365
366 $wppb_cr_settings = get_option( 'wppb_content_restriction_settings' );
367
368 // add filter to hide comments and replies if post content is restricted
369 if ( isset( $wppb_cr_settings[ 'contentRestriction' ] ) && $wppb_cr_settings[ 'contentRestriction' ] == 'yes' && apply_filters( 'wppb_enable_comment_restriction', true ) ) {
370 add_filter( 'comments_open', 'wppb_comments_restrict_replying', 20, 2 );
371 add_filter( 'wp_list_comments_args', 'wppb_comments_hide_callback_function', 999 );
372 }
373
374 if( !function_exists( 'pms_exclude_restricted_comments' ) ){
375 add_filter( 'the_comments', 'wppb_exclude_restricted_comments', 10, 2 );
376 function wppb_exclude_restricted_comments( $comments, $query ){
377 if( !empty( $comments ) && !current_user_can( 'manage_options' ) ){
378 $user_id = get_current_user_id();
379 foreach ( $comments as $key => $comment ){
380 $post = get_post( $comment->comment_post_ID );
381 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 ) ) ){
382 unset( $comments[$key] );
383 }
384 }
385 }
386 return $comments;
387 }
388 }
389
390
391 /**
392 * Clean the LearnDash post type slug before output
393 * - displayed in Content Restriction Meta-box settings description
394 */
395 function wppb_ld_handle_cr_settings_description_cpt( $post_type ) {
396
397 if ( substr( $post_type, 0, 5 ) === "sfwd-" ) {
398 $post_type = substr( $post_type, 5 );
399
400 if ( substr( $post_type, -1 ) === "s" )
401 $post_type = substr( $post_type, 0, -1 );
402
403 }
404
405 return $post_type;
406 }
407 add_filter( 'wppb_content_restrict_settings_description_cpt', 'wppb_ld_handle_cr_settings_description_cpt', 20 );
408
409
410 /**
411 * WooCommerce specific filters
412 */
413 if( function_exists( 'wc_get_page_id' ) ) {
414
415 /**
416 * Function that restricts product content
417 *
418 * @param $output What is returned
419 * @return string
420 */
421 function wppb_woo_restrict_product_content( $output ){
422 global $post, $user_ID;
423
424 if ( strpos( $post->post_password, 'wppb_woo_product_restricted_' ) !== false ) {
425
426 // user does not have access, filter the content
427 $output = '';
428
429 // check if restricted post preview is set
430 $settings = get_option( 'wppb_content_restriction_settings' );
431 $preview_option = ( !empty( $settings['restricted_post_preview']['option'] ) ? $settings['restricted_post_preview']['option'] : '' );
432
433 if ( !empty($preview_option) && ($preview_option != 'none') ) {
434 // display product title
435
436 ob_start();
437
438 echo '<div class="summary entry-summary">';
439 wc_get_template( 'single-product/title.php' );
440 echo '</div>';
441
442 $output = ob_get_clean();
443 }
444
445 if( !is_user_logged_in() )
446 $message = wppb_content_restriction_process_content_message( 'logged_out', $user_ID, $post->ID );
447 else if( !wppb_woo_is_product_purchasable() && !wppb_content_restriction_is_post_restricted() )
448 $message = wppb_content_restriction_process_content_message( 'purchasing_restricted', $user_ID, $post->ID );
449 else
450 $message = wppb_content_restriction_process_content_message( 'logged_in', $user_ID, $post->ID );
451
452 $message = '<div class="woocommerce"><div class="woocommerce-info wppb-woo-restriction-message wppb-woo-restricted-product-purchasing-message">' . $message . '</div></div>';
453
454 $output .= do_shortcode( $message );
455
456 $post->post_password = null;
457
458 }
459
460 return $output;
461 }
462
463 /**
464 * Function that checks if current user can purchase the product
465 *
466 * @param string|WC_Product $product The product
467 * @return bool
468 */
469 function wppb_woo_is_product_purchasable( $product = '' ){
470 global $user_ID, $post;
471
472 if ( empty($product) )
473 $product = wc_get_product( $post->ID );
474
475 if( false == $product )
476 return false;
477
478 /**
479 * Show "buy now" for the `manage_options` and `pms_bypass_content_restriction` capabilities
480 */
481 if( current_user_can( 'manage_options' ) )
482 return true;
483
484 // if is variation, use the id of the parent product
485 if ( $product->is_type( 'variation' ) )
486 $product_id = $product->get_parent_id();
487 else
488 $product_id = $product->get_id();
489
490 // Get subscription plans that can purchase this product
491 $user_status = get_post_meta( $product_id, 'wppb-purchase-restrict-user-status', true );
492 $product_user_roles = get_post_meta( $product_id, 'wppb-purchase-restrict-user-role' );
493
494 if( empty( $user_status ) && empty( $product_user_roles ) ) {
495 //everyone can purchase
496 return true;
497
498 } else if( !empty( $product_user_roles ) && is_user_logged_in() ) {
499
500 $user_data = get_userdata( $user_ID );
501 foreach( $product_user_roles as $product_user_role ) {
502 foreach( $user_data->roles as $role ) {
503 if( $product_user_role == $role ) {
504 return true;
505 }
506 }
507 }
508
509 return false;
510
511 } else if ( !is_user_logged_in() && (
512 ( !empty( $user_status ) && $user_status == 'loggedin' ) || ( !empty( $product_user_roles ) )
513 ) ) {
514
515 return false;
516 }
517
518 return true;
519 }
520
521 /**
522 * Restrict the Shop page
523 *
524 * @param $template The shop page template to return
525 * @return string
526 */
527 function wppb_woo_restrict_shop_page($template){
528
529 // check if we're on the Shop page (set under WooCommerce Settings -> Products -> Display)
530 if (is_post_type_archive('product') || is_page(wc_get_page_id('shop'))) {
531
532 // get the ID of the shop page
533 $post_id = wc_get_page_id('shop');
534
535 if (($post_id != -1) && wppb_check_content_restriction_on_post_id($post_id)) {
536
537 $shop_page = get_post($post_id);
538
539 setup_postdata($shop_page);
540
541 $template = WPPB_PLUGIN_DIR . 'features/content-restriction/templates/archive-product.php';
542
543 wp_reset_postdata();
544 }
545
546 }
547
548 return $template;
549 }
550 add_filter( 'template_include', 'wppb_woo_restrict_shop_page', 40 );
551
552 /**
553 * Restrict single product view by marking restricted products as password-protected
554 *
555 * - Runs on 'template_redirect' so it executes early enough for both classic and block themes
556 * - Applies only to 'message' restriction type. Redirect mode is handled separately
557 */
558 function wppb_woo_maybe_password_protect_product(){
559 global $post;
560
561 if ( ! is_singular( 'product' ) || empty( $post ) || empty( $post->ID ) )
562 return;
563
564 $post_restriction_type = get_post_meta( $post->ID, 'wppb-content-restrict-type', true );
565 $settings = get_option( 'wppb_content_restriction_settings', array() );
566
567 if ( $post_restriction_type == 'default' || empty( $post_restriction_type ) )
568 $post_restriction_type = ( ! empty( $settings['restrict_type'] ) ? $settings['restrict_type'] : 'message' );
569
570 if ( $post_restriction_type !== 'message' )
571 return;
572
573 // if the product is restricted and doesn't already require a password, set a temporary password before Woo renders product templates
574 if ( wppb_content_restriction_is_post_restricted( $post->ID ) && ! post_password_required() ) {
575 $post->post_password = uniqid( 'wppb_woo_product_restricted_' );
576
577 if ( ! has_filter( 'the_password_form', 'wppb_woo_restrict_product_content' ) )
578 add_filter( 'the_password_form', 'wppb_woo_restrict_product_content' );
579 }
580 }
581 add_action( 'template_redirect', 'wppb_woo_maybe_password_protect_product', 0 );
582
583
584 /**
585 * Function that hides the price for view-restricted products
586 *
587 * @param float $price The product price
588 * @param WC_Product $product The product
589 * @return string
590 */
591 function wppb_woo_hide_restricted_product_price( $price, WC_Product $product ){
592 // check if current user can view this product, and if not, remove the price
593 if ( wppb_content_restriction_is_post_restricted( $product->get_id() ) ) {
594
595 $price = '';
596 }
597
598 return $price;
599 }
600 add_filter( 'woocommerce_get_price_html', 'wppb_woo_hide_restricted_product_price', 9, 2);
601
602
603 /**
604 * Function that hides the product image thumbnail for view-restricted products
605 *
606 */
607 function wppb_woo_maybe_remove_product_thumbnail(){
608 global $post, $wppb_woo_product_thumbnail_restricted;
609
610 $wppb_woo_product_thumbnail_restricted = false;
611
612 // skip if the product thumbnail is not shown anyway
613 if ( ! has_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail' ) ) {
614 return;
615 }
616
617 // if product is view restricted, do not display the product thumbnail
618 if ( wppb_content_restriction_is_post_restricted($post->ID) ) {
619
620 // indicate that we removed the product thumbnail
621 $wppb_woo_product_thumbnail_restricted = true;
622
623 // remove the product thumbnail and replace it with the placeholder image
624 remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail' );
625 add_action( 'woocommerce_before_shop_loop_item_title', 'wppb_woo_template_loop_product_thumbnail_placeholder', 10 );
626 }
627
628 }
629 add_action( 'woocommerce_before_shop_loop_item_title', 'wppb_woo_maybe_remove_product_thumbnail', 5 );
630
631
632 // return placeholder thumbnail instead of image for view-restricted products
633 function wppb_woo_template_loop_product_thumbnail_placeholder(){
634 if ( wc_placeholder_img_src() ) {
635
636 echo wp_kses_post( wc_placeholder_img( 'shop_catalog' ) );
637 }
638 }
639
640 // restore product thumbnail for the next product in the loop
641 function wppb_woo_restore_product_thumbnail(){
642 global $wppb_woo_product_thumbnail_restricted;
643
644 if ( $wppb_woo_product_thumbnail_restricted
645 && ! has_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail' ) ) {
646
647 add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
648 remove_action( 'woocommerce_before_shop_loop_item_title', 'wppb_woo_template_loop_product_thumbnail_placeholder' );
649 }
650 }
651 add_action( 'woocommerce_after_shop_loop_item_title', 'wppb_woo_restore_product_thumbnail', 5 );
652
653
654 /**
655 * Function that restricts product purchasing
656 *
657 * @param boolean $purchasable Whether the product is purchasable or not
658 * @param $product The product
659 * @return bool
660 */
661 function wppb_woo_product_is_purchasable( $purchasable, $product ){
662
663 // if the product is a variation, we need to grab the parent product before checking if it is purchasable
664 if( $product->is_type( array( 'variation' ) ) ){
665 $product = wc_get_product( $product->get_parent_id() );
666 }
667
668 if ( wppb_content_restriction_is_post_restricted( $product->get_id() ) || !wppb_woo_is_product_purchasable( $product ) )
669 $purchasable = false;
670
671 // double-check for variations; if parent is not purchasable, then neither should be the variation
672 // NOTE: This was removed because it doesn't make sense for varations. We need to check if the product that we receive is a variation and go directly
673 // to the parent product to check settings
674 // if ( $purchasable && $product->is_type( array( 'variation' ) ) ) {
675
676 // $parent = wc_get_product( $product->get_parent_id() );
677
678 // if( !empty( $parent ) && is_object( $parent ) )
679 // $purchasable = $parent->is_purchasable();
680
681 // }
682
683 return $purchasable;
684
685 }
686 add_filter( 'woocommerce_is_purchasable', 'wppb_woo_product_is_purchasable', 10, 2 );
687 add_filter( 'woocommerce_variation_is_purchasable', 'wppb_woo_product_is_purchasable', 10, 2 );
688
689 /**
690 * Function that shows the product purchasing restricted message
691 *
692 **/
693 function wppb_woo_single_product_purchasing_restricted_message(){
694 global $wppb_show_content, $post;
695
696 if( empty( $post->ID ) )
697 return;
698
699 // View restrictions already render their own message
700 if( wppb_content_restriction_is_post_restricted( $post->ID ) ) {
701 return;
702 }
703
704 if ( !wppb_woo_is_product_purchasable() ) {
705
706 // product purchasing is restricted
707 $wppb_show_content = false;
708
709 if( is_user_logged_in() )
710 $message = wppb_content_restriction_process_content_message( 'purchasing_restricted', get_current_user_id(), $post->ID );
711 else
712 $message = wppb_content_restriction_process_content_message( 'logged_out', get_current_user_id(), $post->ID );
713
714 echo wp_kses_post( $message );
715 }
716 }
717 add_action( 'woocommerce_single_product_summary', 'wppb_woo_single_product_purchasing_restricted_message', 30 );
718
719 // Apply wpautop() to "purchasing restricted" messages as well
720 add_filter( 'wppb_content_restriction_message_purchasing_restricted', 'wppb_content_restriction_message_wpautop', 30, 1 );
721
722 }
723
724 /**
725 * Exclude Restricted Posts From Query
726 */
727 if( isset( $wppb_cr_settings['excludePosts'] ) && $wppb_cr_settings['excludePosts'] == 'yes' ) {
728 add_action( 'pre_get_posts', 'wppb_exclude_post_from_query', 40 );
729 function wppb_exclude_post_from_query( $query ) {
730
731 if( !function_exists( 'wppb_content_restriction_is_post_restricted' ) || is_admin() || is_singular() )
732 return;
733
734 if( isset( $query->query_vars['wc_query'] ) && $query->query_vars['wc_query'] == 'product_query' )
735 return;
736
737 // Skip Ultimate Member queries
738 if( isset( $query->query_vars['um_action'] ) || isset( $query->query_vars['um_user'] ) )
739 return;
740
741 if( $query->is_main_query() || ( $query->is_search() && isset( $query->query_vars['s'] ) ) ) {
742
743 remove_action( 'pre_get_posts', 'wppb_exclude_post_from_query', 40 );
744
745 $args = $query->query_vars;
746 $args['suppress_filters'] = true;
747 $args['fields'] = 'ids';
748
749 // Setup paged arguments so we exclude restricted posts from the current page, not only the first N posts.
750 if( !empty( $query->query_vars['paged'] ) ){
751 $args['paged'] = round( $args['paged'] / 2 );
752 if( !empty( $args['posts_per_page'] ) )
753 $args['posts_per_page'] = $args['posts_per_page'] * 2;
754 else
755 $args['posts_per_page'] = get_option( 'posts_per_page' ) * 2;
756 }
757
758 if( is_search() )
759 $args['post_type'] = 'any';
760
761 $restricted_posts = get_posts( $args );
762
763 $previous_restricted_ids = $query->get( 'post__not_in' );
764 if ( ! is_array( $previous_restricted_ids ) ) {
765 $previous_restricted_ids = array();
766 }
767
768 $restricted_ids = array_filter( $restricted_posts, 'wppb_content_restriction_is_post_restricted' );
769 $query->set( 'post__not_in', array_merge( $previous_restricted_ids, $restricted_ids ) );
770
771 }
772 }
773
774 //Remove restricted forums from the main bbPress query
775 add_filter( 'bbp_after_has_forums_parse_args', 'wppb_exclude_restricted_forums_from_main_query' );
776 function wppb_exclude_restricted_forums_from_main_query( $vars ) {
777 if( !function_exists( 'wppb_content_restriction_is_post_restricted' ) ) return;
778
779 $query = new WP_Query( $vars );
780
781 $forum_ids = array();
782
783 foreach ( $query->posts as $forum ) {
784 $forum_ids[] = $forum->ID;
785 }
786
787 $previous_restricted_forums = $query->get( 'post__not_in' );
788 if ( ! is_array( $previous_restricted_forums ) ) {
789 $previous_restricted_forums = array();
790 }
791
792 $restricted_forums = array_filter( $forum_ids, 'wppb_content_restriction_is_post_restricted' );
793 $updated_restricted_forums = array_merge( $previous_restricted_forums, $restricted_forums );
794
795 $vars['post__not_in'] = $updated_restricted_forums;
796
797 return $vars;
798 }
799
800 //Alters the output of the query that displays the Category Archive (including Shop) pages, filtering the restricted products from the output
801 add_action( 'pre_get_posts', 'wppb_exclude_restricted_products_from_woocoommerce_category_queries', 11 );
802 function wppb_exclude_restricted_products_from_woocoommerce_category_queries( $query ) {
803
804 if( is_admin() || !function_exists('wppb_content_restriction_is_post_restricted') )
805 return;
806
807 if( isset( $query->query_vars['wc_query'] ) && $query->query_vars['wc_query'] == 'product_query' ){
808
809 remove_action( 'pre_get_posts', 'wppb_exclude_restricted_products_from_woocoommerce_category_queries', 11 );
810
811 $args = $query->query_vars;
812 $args['suppress_filters'] = true;
813 $args['posts_per_page'] = -1;
814
815 $previous_restricted_ids = $query->get( 'post__not_in' );
816 if ( ! is_array( $previous_restricted_ids ) ) {
817 $previous_restricted_ids = array();
818 }
819
820 $transient_key = 'wppb_cr_products_' . md5( serialize( $args ) );
821 if ( false === ( $products = get_transient( $transient_key ) ) ) {
822 $products = wc_get_products( $args );
823 set_transient( $transient_key, $products, 2 * HOUR_IN_SECONDS );
824 }
825
826 $product_ids = array();
827
828 foreach ($products as $product) {
829 $product_ids[] = $product->get_id();
830 }
831
832 $restricted_ids = array_filter( $product_ids, 'wppb_content_restriction_is_post_restricted' );
833
834 $updated_restricted_ids = array_merge( $previous_restricted_ids, $restricted_ids );
835 $query->set( 'post__not_in', $updated_restricted_ids );
836
837 }
838
839 }
840
841 //Alters the output of the [products] shortcode from WooCommerce, filtering restricted products from the output
842 add_filter( 'woocommerce_shortcode_products_query', 'wppb_exclude_restricted_products_from_woocoommerce_products_shortcode_queries' );
843 function wppb_exclude_restricted_products_from_woocoommerce_products_shortcode_queries( $query_args ) {
844 if( !is_admin() && function_exists('wppb_content_restriction_is_post_restricted') ) {
845 $posts_per_page = $query_args['posts_per_page'];
846
847 $query_args['suppress_filters'] = true;
848 $query_args['posts_per_page'] = '-1';
849
850 $products = wc_get_products($query_args);
851
852 $query_args['posts_per_page'] = $posts_per_page;
853
854 $product_ids = array();
855
856 foreach ($products as $product) {
857 $product_ids[] = $product->get_id();
858 }
859
860 $previous_restricted_ids = isset($query_args['post__not_in']) ? $query_args['post__not_in'] : array();
861
862 if ( ! is_array( $previous_restricted_ids ) ) {
863 $previous_restricted_ids = array();
864 }
865
866 $restricted_ids = array_filter( $product_ids, 'wppb_content_restriction_is_post_restricted' );
867 $updated_restricted_ids = array_merge( $previous_restricted_ids, $restricted_ids );
868
869 $query_args['post__not_in'] = $updated_restricted_ids;
870 }
871
872 return $query_args;
873 }
874
875 // Exclude restricted posts from Elementor
876 add_filter( 'elementor/query/query_args', 'wppb_exclude_posts_from_elementor' );
877 function wppb_exclude_posts_from_elementor( $query_args ) {
878
879 // check if the form is being displayed in the Elementor editor
880 $is_elementor_edit_mode = false;
881 if( class_exists ( '\Elementor\Plugin' ) ){
882 $is_elementor_edit_mode = \Elementor\Plugin::$instance->editor->is_edit_mode();
883 }
884
885 if ( !$is_elementor_edit_mode && !is_admin() && function_exists( 'wppb_content_restriction_is_post_restricted' ) ) {
886 $args = $query_args;
887 $args['suppress_filters'] = true;
888 $args['fields'] = 'ids';
889
890 if( !empty( $query_args['paged'] ) ){
891
892 $args['paged'] = round( $args['paged'] / 2 );
893
894 if( !empty( $args['posts_per_page'] ) )
895 $args['posts_per_page'] = $args['posts_per_page'] * 2;
896 else
897 $args['posts_per_page'] = get_option( 'posts_per_page' ) * 2;
898
899 }
900
901 if( is_search() )
902 $args['post_type'] = 'any';
903
904 $restricted_posts = get_posts( $args );
905
906 $restricted_ids = array_filter( $restricted_posts, 'wppb_content_restriction_is_post_restricted' );
907
908 if ( ! empty( $restricted_ids ) ) {
909 if ( isset( $query_args['post__not_in'] ) && is_array( $query_args['post__not_in'] ) ) {
910 $query_args['post__not_in'] = array_merge( $query_args['post__not_in'], $restricted_ids );
911 } else {
912 $query_args['post__not_in'] = $restricted_ids;
913 }
914 }
915 }
916 return $query_args;
917 }
918 }
919