← All changes
|
features/content-restriction/content-restriction-filtering.php
+543
-21
3.9.9
→
4.0.3
View file →
| @@ -3,14 +3,19 @@ | ||
| 3 | 3 | |
| 4 | 4 | /* Hijack the content when restrictions are set on a single post */ |
| 5 | 5 | function wppb_content_restriction_filter_content( $content, $post = null ) { |
| 6 | 6 | |
| 7 | - global $user_ID, $wppb_show_content, $pms_show_content; | |
| 7 | + global $user_ID, $wppb_show_content, $pms_is_post_restricted_arr; | |
| 8 | 8 | |
| 9 | 9 | if( is_null( $post ) ) { |
| 10 | 10 | global $post; |
| 11 | 11 | } |
| 12 | 12 | |
| 13 | + if( empty( $post->ID ) ) | |
| 14 | + return $content; | |
| 15 | + | |
| 16 | + $user_ID = get_current_user_id(); | |
| 17 | + | |
| 13 | 18 | /* |
| 14 | 19 | * Defining this variable: |
| 15 | 20 | * |
| 16 | 21 | * $wppb_show_content can have 3 states: null, true and false |
| @@ -27,21 +32,53 @@ | ||
| 27 | 32 | return $content; |
| 28 | 33 | } |
| 29 | 34 | |
| 30 | 35 | // Check if any PMS restriction should take place. PMS restrictions have priority |
| 31 | - if( $pms_show_content === false ) { | |
| 36 | + if( isset( $pms_is_post_restricted_arr[$post->ID] ) && $pms_is_post_restricted_arr[$post->ID] === true ) { | |
| 32 | 37 | return $content; |
| 33 | 38 | } |
| 34 | 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 | + | |
| 35 | 61 | // Get user roles that have access to this post |
| 36 | 62 | if ( isset( $post ) && isset( $post->ID ) ) { |
| 37 | 63 | $user_status = get_post_meta($post->ID, 'wppb-content-restrict-user-status', true); |
| 38 | 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 ); | |
| 39 | 75 | } |
| 40 | 76 | |
| 41 | 77 | if( empty( $user_status ) && empty( $post_user_roles ) ) { |
| 78 | + $wppb_show_content = true; | |
| 42 | 79 | return $content; |
| 43 | - } else if( $user_status == 'loggedin' ) { | |
| 80 | + } else if( $user_status == 'loggedin' || ( $user_roles_without_logged_in_option && !empty( $post_user_roles ) ) ) { | |
| 44 | 81 | if( is_user_logged_in() ) { |
| 45 | 82 | if( ! empty( $post_user_roles ) ) { |
| 46 | 83 | $user_data = get_userdata( $user_ID ); |
| 47 | 84 | |
| @@ -89,11 +126,21 @@ | ||
| 89 | 126 | // Get user roles that have access to this post |
| 90 | 127 | $user_status = get_post_meta( $post_id, 'wppb-content-restrict-user-status', true ); |
| 91 | 128 | $post_user_roles = get_post_meta( $post_id, 'wppb-content-restrict-user-role' ); |
| 92 | 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 | + | |
| 93 | 140 | if( empty( $user_status ) && empty( $post_user_roles ) ) { |
| 94 | 141 | return false; |
| 95 | - } else if( $user_status == 'loggedin' ) { | |
| 142 | + } else if( $user_status == 'loggedin' || ( $user_roles_without_logged_in_option && !empty( $post_user_roles ) ) ) { | |
| 96 | 143 | if( is_user_logged_in() ) { |
| 97 | 144 | if( ! empty( $post_user_roles ) ) { |
| 98 | 145 | $user_data = get_userdata( $user_ID ); |
| 99 | 146 | foreach( $post_user_roles as $post_user_role ) { |
| @@ -193,8 +240,242 @@ | ||
| 193 | 240 | } |
| 194 | 241 | add_filter( 'wppb_content_restriction_message_logged_in', 'wppb_content_restriction_message_wpautop', 30, 1 ); |
| 195 | 242 | add_filter( 'wppb_content_restriction_message_logged_out', 'wppb_content_restriction_message_wpautop', 30, 1 ); |
| 196 | 243 | |
| 244 | +function wppb_content_restriction_get_post_preview_allowed_html() { | |
| 245 | + $allowed_post_html = wp_kses_allowed_html( 'post' ); | |
| 246 | + $allowed_tags = array( | |
| 247 | + 'a', | |
| 248 | + 'abbr', | |
| 249 | + 'acronym', | |
| 250 | + 'b', | |
| 251 | + 'blockquote', | |
| 252 | + 'br', | |
| 253 | + 'cite', | |
| 254 | + 'code', | |
| 255 | + 'dd', | |
| 256 | + 'del', | |
| 257 | + 'details', | |
| 258 | + 'div', | |
| 259 | + 'dl', | |
| 260 | + 'dt', | |
| 261 | + 'em', | |
| 262 | + 'h1', | |
| 263 | + 'h2', | |
| 264 | + 'h3', | |
| 265 | + 'h4', | |
| 266 | + 'h5', | |
| 267 | + 'h6', | |
| 268 | + 'hr', | |
| 269 | + 'i', | |
| 270 | + 'ins', | |
| 271 | + 'kbd', | |
| 272 | + 'li', | |
| 273 | + 'mark', | |
| 274 | + 'ol', | |
| 275 | + 'p', | |
| 276 | + 'pre', | |
| 277 | + 'q', | |
| 278 | + 's', | |
| 279 | + 'samp', | |
| 280 | + 'small', | |
| 281 | + 'span', | |
| 282 | + 'strike', | |
| 283 | + 'strong', | |
| 284 | + 'sub', | |
| 285 | + 'summary', | |
| 286 | + 'sup', | |
| 287 | + 'table', | |
| 288 | + 'tbody', | |
| 289 | + 'td', | |
| 290 | + 'tfoot', | |
| 291 | + 'th', | |
| 292 | + 'thead', | |
| 293 | + 'tr', | |
| 294 | + 'u', | |
| 295 | + 'ul', | |
| 296 | + 'var', | |
| 297 | + ); | |
| 298 | + $allowed_html = array(); | |
| 299 | + | |
| 300 | + foreach( $allowed_tags as $tag ) { | |
| 301 | + if( isset( $allowed_post_html[ $tag ] ) ) { | |
| 302 | + $allowed_html[ $tag ] = $allowed_post_html[ $tag ]; | |
| 303 | + } | |
| 304 | + } | |
| 305 | + | |
| 306 | + return apply_filters( 'wppb_content_restriction_post_preview_allowed_html', $allowed_html ); | |
| 307 | +} | |
| 308 | + | |
| 309 | +function wppb_content_restriction_sanitize_post_preview_html( $html ) { | |
| 310 | + $html = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $html ); | |
| 311 | + | |
| 312 | + return wp_kses( $html, wppb_content_restriction_get_post_preview_allowed_html() ); | |
| 313 | +} | |
| 314 | + | |
| 315 | +function wppb_content_restriction_get_text_count_units( $text, $offsets = false ) { | |
| 316 | + if( str_starts_with( wp_get_word_count_type(), 'characters' ) && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) { | |
| 317 | + $pattern = ( wp_get_word_count_type() === 'characters_excluding_spaces' ) ? '/[^\n\r\t ]/u' : '/./u'; | |
| 318 | + } else { | |
| 319 | + $pattern = '/[^\n\r\t ]+/u'; | |
| 320 | + } | |
| 321 | + | |
| 322 | + preg_match_all( $pattern, $text, $units, ( $offsets ? PREG_OFFSET_CAPTURE : 0 ) ); | |
| 323 | + | |
| 324 | + return $units[0]; | |
| 325 | +} | |
| 326 | + | |
| 327 | +function wppb_content_restriction_count_dom_node_words( $node ) { | |
| 328 | + $count = 0; | |
| 329 | + | |
| 330 | + if( ! $node->hasChildNodes() ) { | |
| 331 | + return $count; | |
| 332 | + } | |
| 333 | + | |
| 334 | + foreach( $node->childNodes as $child ) { | |
| 335 | + if( $child->nodeType === XML_TEXT_NODE || $child->nodeType === XML_CDATA_SECTION_NODE ) { | |
| 336 | + $count += count( wppb_content_restriction_get_text_count_units( $child->nodeValue ) ); | |
| 337 | + } else { | |
| 338 | + $count += wppb_content_restriction_count_dom_node_words( $child ); | |
| 339 | + } | |
| 340 | + } | |
| 341 | + | |
| 342 | + return $count; | |
| 343 | +} | |
| 344 | + | |
| 345 | +function wppb_content_restriction_trim_text_node_words( $text, &$remaining_words, $more, &$trimmed, &$last_text_node ) { | |
| 346 | + $words = wppb_content_restriction_get_text_count_units( $text, true ); | |
| 347 | + | |
| 348 | + if( empty( $words ) ) { | |
| 349 | + return $text; | |
| 350 | + } | |
| 351 | + | |
| 352 | + if( $remaining_words <= 0 ) { | |
| 353 | + if( ! empty( $last_text_node ) ) { | |
| 354 | + $last_text_node->nodeValue = rtrim( $last_text_node->nodeValue ) . $more; | |
| 355 | + } | |
| 356 | + | |
| 357 | + $trimmed = true; | |
| 358 | + | |
| 359 | + return ''; | |
| 360 | + } | |
| 361 | + | |
| 362 | + $word_count = count( $words ); | |
| 363 | + | |
| 364 | + if( $word_count < $remaining_words ) { | |
| 365 | + $remaining_words -= $word_count; | |
| 366 | + | |
| 367 | + return $text; | |
| 368 | + } | |
| 369 | + | |
| 370 | + $last_text_node = null; | |
| 371 | + | |
| 372 | + if( $word_count === $remaining_words ) { | |
| 373 | + $remaining_words = 0; | |
| 374 | + | |
| 375 | + return $text; | |
| 376 | + } | |
| 377 | + | |
| 378 | + $last_word = $words[ $remaining_words - 1 ]; | |
| 379 | + $cut_position = $last_word[1] + strlen( $last_word[0] ); | |
| 380 | + $remaining_words = 0; | |
| 381 | + $trimmed = true; | |
| 382 | + | |
| 383 | + return rtrim( substr( $text, 0, $cut_position ) ) . $more; | |
| 384 | +} | |
| 385 | + | |
| 386 | +function wppb_content_restriction_trim_dom_node_words( $node, &$remaining_words, $more, &$trimmed, &$last_text_node ) { | |
| 387 | + if( ! $node->hasChildNodes() ) { | |
| 388 | + return; | |
| 389 | + } | |
| 390 | + | |
| 391 | + $children = array(); | |
| 392 | + foreach( $node->childNodes as $child ) { | |
| 393 | + $children[] = $child; | |
| 394 | + } | |
| 395 | + | |
| 396 | + foreach( $children as $child ) { | |
| 397 | + if( $trimmed ) { | |
| 398 | + $node->removeChild( $child ); | |
| 399 | + continue; | |
| 400 | + } | |
| 401 | + | |
| 402 | + if( $child->nodeType === XML_TEXT_NODE || $child->nodeType === XML_CDATA_SECTION_NODE ) { | |
| 403 | + $child->nodeValue = wppb_content_restriction_trim_text_node_words( $child->nodeValue, $remaining_words, $more, $trimmed, $last_text_node ); | |
| 404 | + | |
| 405 | + if( ! $trimmed && ! empty( wppb_content_restriction_get_text_count_units( $child->nodeValue ) ) ) { | |
| 406 | + $last_text_node = $child; | |
| 407 | + } | |
| 408 | + | |
| 409 | + if( $trimmed && $child->nodeValue === '' ) { | |
| 410 | + $node->removeChild( $child ); | |
| 411 | + } | |
| 412 | + } else { | |
| 413 | + wppb_content_restriction_trim_dom_node_words( $child, $remaining_words, $more, $trimmed, $last_text_node ); | |
| 414 | + | |
| 415 | + if( $trimmed && ! $child->hasChildNodes() ) { | |
| 416 | + $node->removeChild( $child ); | |
| 417 | + } | |
| 418 | + } | |
| 419 | + } | |
| 420 | +} | |
| 421 | + | |
| 422 | +function wppb_content_restriction_trim_html_words( $html, $num_words, $more = null ) { | |
| 423 | + if( $more === null ) { | |
| 424 | + $more = __( '…', 'profile-builder' ); | |
| 425 | + } | |
| 426 | + | |
| 427 | + $num_words = (int) $num_words; | |
| 428 | + | |
| 429 | + if( $num_words <= 0 || $html === '' ) { | |
| 430 | + return ''; | |
| 431 | + } | |
| 432 | + | |
| 433 | + $html = wppb_content_restriction_sanitize_post_preview_html( $html ); | |
| 434 | + | |
| 435 | + if( ! class_exists( 'DOMDocument' ) ) { | |
| 436 | + return wpautop( wp_trim_words( $html, $num_words, $more ) ); | |
| 437 | + } | |
| 438 | + | |
| 439 | + $dom_flags = 0; | |
| 440 | + if( defined( 'LIBXML_HTML_NOIMPLIED' ) ) { | |
| 441 | + $dom_flags |= LIBXML_HTML_NOIMPLIED; | |
| 442 | + } | |
| 443 | + if( defined( 'LIBXML_HTML_NODEFDTD' ) ) { | |
| 444 | + $dom_flags |= LIBXML_HTML_NODEFDTD; | |
| 445 | + } | |
| 446 | + | |
| 447 | + $document = new DOMDocument(); | |
| 448 | + $previous_errors = libxml_use_internal_errors( true ); | |
| 449 | + $document->loadHTML( '<?xml encoding="UTF-8"><div id="wppb-content-restriction-preview-wrapper">' . $html . '</div>', $dom_flags ); | |
| 450 | + libxml_clear_errors(); | |
| 451 | + libxml_use_internal_errors( $previous_errors ); | |
| 452 | + | |
| 453 | + $wrapper = $document->getElementById( 'wppb-content-restriction-preview-wrapper' ); | |
| 454 | + | |
| 455 | + if( empty( $wrapper ) ) { | |
| 456 | + return wpautop( wp_trim_words( $html, $num_words, $more ) ); | |
| 457 | + } | |
| 458 | + | |
| 459 | + $remaining_words = $num_words; | |
| 460 | + $trimmed = false; | |
| 461 | + $last_text_node = null; | |
| 462 | + $more_text = html_entity_decode( $more, ENT_QUOTES | ENT_HTML5, get_option( 'blog_charset' ) ); | |
| 463 | + | |
| 464 | + if( wppb_content_restriction_count_dom_node_words( $wrapper ) <= $num_words ) { | |
| 465 | + return $html; | |
| 466 | + } | |
| 467 | + | |
| 468 | + wppb_content_restriction_trim_dom_node_words( $wrapper, $remaining_words, $more_text, $trimmed, $last_text_node ); | |
| 469 | + | |
| 470 | + $preview = ''; | |
| 471 | + foreach( $wrapper->childNodes as $child ) { | |
| 472 | + $preview .= $document->saveHTML( $child ); | |
| 473 | + } | |
| 474 | + | |
| 475 | + return $preview; | |
| 476 | +} | |
| 477 | + | |
| 197 | 478 | /* Adds a preview of the restricted post before the default restriction messages */ |
| 198 | 479 | function wppb_content_restriction_add_post_preview( $message, $content, $post, $user_ID ) { |
| 199 | 480 | |
| 200 | 481 | $preview = ''; |
| @@ -214,11 +495,13 @@ | ||
| 214 | 495 | if( $length !== 0 ) { |
| 215 | 496 | // Do shortcodes on the content |
| 216 | 497 | $post_content = do_shortcode( $post_content ); |
| 217 | 498 | |
| 218 | - // Trim the preview | |
| 219 | - $preview = wp_trim_words( $post_content, $length, apply_filters( 'wppb_content_restriction_post_preview_more', __( '…', 'profile-builder' ) ) ); | |
| 499 | + // Trim the preview while preserving the post's HTML structure. | |
| 500 | + $preview = wppb_content_restriction_trim_html_words( $post_content, $length, apply_filters( 'wppb_content_restriction_post_preview_more', __( '…', 'profile-builder' ) ) ); | |
| 220 | 501 | } |
| 502 | + | |
| 503 | + return $preview . $message; | |
| 221 | 504 | } |
| 222 | 505 | |
| 223 | 506 | // More tag |
| 224 | 507 | if( $preview_option == 'more-tag' ) { |
| @@ -339,9 +622,29 @@ | ||
| 339 | 622 | return $comments; |
| 340 | 623 | } |
| 341 | 624 | } |
| 342 | 625 | |
| 626 | + | |
| 343 | 627 | /** |
| 628 | + * Clean the LearnDash post type slug before output | |
| 629 | + * - displayed in Content Restriction Meta-box settings description | |
| 630 | + */ | |
| 631 | +function wppb_ld_handle_cr_settings_description_cpt( $post_type ) { | |
| 632 | + | |
| 633 | + if ( substr( $post_type, 0, 5 ) === "sfwd-" ) { | |
| 634 | + $post_type = substr( $post_type, 5 ); | |
| 635 | + | |
| 636 | + if ( substr( $post_type, -1 ) === "s" ) | |
| 637 | + $post_type = substr( $post_type, 0, -1 ); | |
| 638 | + | |
| 639 | + } | |
| 640 | + | |
| 641 | + return $post_type; | |
| 642 | +} | |
| 643 | +add_filter( 'wppb_content_restrict_settings_description_cpt', 'wppb_ld_handle_cr_settings_description_cpt', 20 ); | |
| 644 | + | |
| 645 | + | |
| 646 | +/** | |
| 344 | 647 | * WooCommerce specific filters |
| 345 | 648 | */ |
| 346 | 649 | if( function_exists( 'wc_get_page_id' ) ) { |
| 347 | 650 | |
| @@ -383,9 +686,9 @@ | ||
| 383 | 686 | $message = wppb_content_restriction_process_content_message( 'logged_in', $user_ID, $post->ID ); |
| 384 | 687 | |
| 385 | 688 | $message = '<div class="woocommerce"><div class="woocommerce-info wppb-woo-restriction-message wppb-woo-restricted-product-purchasing-message">' . $message . '</div></div>'; |
| 386 | 689 | |
| 387 | - $output .= $message; | |
| 690 | + $output .= do_shortcode( $message ); | |
| 388 | 691 | |
| 389 | 692 | $post->post_password = null; |
| 390 | 693 | |
| 391 | 694 | } |
| @@ -482,25 +785,37 @@ | ||
| 482 | 785 | } |
| 483 | 786 | add_filter( 'template_include', 'wppb_woo_restrict_shop_page', 40 ); |
| 484 | 787 | |
| 485 | 788 | /** |
| 486 | - * Function that restricts product viewing by hijacking WooCommerce product password protection (hide_content restriction mode) | |
| 789 | + * Restrict single product view by marking restricted products as password-protected | |
| 487 | 790 | * |
| 791 | + * - Runs on 'template_redirect' so it executes early enough for both classic and block themes | |
| 792 | + * - Applies only to 'message' restriction type. Redirect mode is handled separately | |
| 488 | 793 | */ |
| 489 | 794 | function wppb_woo_maybe_password_protect_product(){ |
| 490 | 795 | global $post; |
| 491 | 796 | |
| 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() ) { | |
| 797 | + if ( ! is_singular( 'product' ) || empty( $post ) || empty( $post->ID ) ) | |
| 798 | + return; | |
| 495 | 799 | |
| 800 | + $post_restriction_type = get_post_meta( $post->ID, 'wppb-content-restrict-type', true ); | |
| 801 | + $settings = get_option( 'wppb_content_restriction_settings', array() ); | |
| 802 | + | |
| 803 | + if ( $post_restriction_type == 'default' || empty( $post_restriction_type ) ) | |
| 804 | + $post_restriction_type = ( ! empty( $settings['restrict_type'] ) ? $settings['restrict_type'] : 'message' ); | |
| 805 | + | |
| 806 | + if ( $post_restriction_type !== 'message' ) | |
| 807 | + return; | |
| 808 | + | |
| 809 | + // if the product is restricted and doesn't already require a password, set a temporary password before Woo renders product templates | |
| 810 | + if ( wppb_content_restriction_is_post_restricted( $post->ID ) && ! post_password_required() ) { | |
| 496 | 811 | $post->post_password = uniqid( 'wppb_woo_product_restricted_' ); |
| 497 | 812 | |
| 498 | - add_filter( 'the_password_form', 'wppb_woo_restrict_product_content' ); | |
| 499 | - | |
| 813 | + if ( ! has_filter( 'the_password_form', 'wppb_woo_restrict_product_content' ) ) | |
| 814 | + add_filter( 'the_password_form', 'wppb_woo_restrict_product_content' ); | |
| 500 | 815 | } |
| 501 | 816 | } |
| 502 | - add_action( 'woocommerce_before_single_product', 'wppb_woo_maybe_password_protect_product' ); | |
| 817 | + add_action( 'template_redirect', 'wppb_woo_maybe_password_protect_product', 0 ); | |
| 503 | 818 | |
| 504 | 819 | |
| 505 | 820 | /** |
| 506 | 821 | * Function that hides the price for view-restricted products |
| @@ -580,21 +895,27 @@ | ||
| 580 | 895 | * @return bool |
| 581 | 896 | */ |
| 582 | 897 | function wppb_woo_product_is_purchasable( $purchasable, $product ){ |
| 583 | 898 | |
| 584 | - // if the product is view-restricted or purchase-restricted it cannot be purchased | |
| 899 | + // if the product is a variation, we need to grab the parent product before checking if it is purchasable | |
| 900 | + if( $product->is_type( array( 'variation' ) ) ){ | |
| 901 | + $product = wc_get_product( $product->get_parent_id() ); | |
| 902 | + } | |
| 903 | + | |
| 585 | 904 | if ( wppb_content_restriction_is_post_restricted( $product->get_id() ) || !wppb_woo_is_product_purchasable( $product ) ) |
| 586 | 905 | $purchasable = false; |
| 587 | 906 | |
| 588 | 907 | // double-check for variations; if parent is not purchasable, then neither should be the variation |
| 589 | - if ( $purchasable && $product->is_type( array( 'variation' ) ) ) { | |
| 908 | + // 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 | |
| 909 | + // to the parent product to check settings | |
| 910 | + // if ( $purchasable && $product->is_type( array( 'variation' ) ) ) { | |
| 590 | 911 | |
| 591 | - $parent = wc_get_product( $product->get_parent_id() ); | |
| 912 | + // $parent = wc_get_product( $product->get_parent_id() ); | |
| 592 | 913 | |
| 593 | - if( !empty( $parent ) && is_object( $parent ) ) | |
| 594 | - $purchasable = $parent->is_purchasable(); | |
| 914 | + // if( !empty( $parent ) && is_object( $parent ) ) | |
| 915 | + // $purchasable = $parent->is_purchasable(); | |
| 595 | 916 | |
| 596 | - } | |
| 917 | + // } | |
| 597 | 918 | |
| 598 | 919 | return $purchasable; |
| 599 | 920 | |
| 600 | 921 | } |
| @@ -610,8 +931,13 @@ | ||
| 610 | 931 | |
| 611 | 932 | if( empty( $post->ID ) ) |
| 612 | 933 | return; |
| 613 | 934 | |
| 935 | + // View restrictions already render their own message | |
| 936 | + if( wppb_content_restriction_is_post_restricted( $post->ID ) ) { | |
| 937 | + return; | |
| 938 | + } | |
| 939 | + | |
| 614 | 940 | if ( !wppb_woo_is_product_purchasable() ) { |
| 615 | 941 | |
| 616 | 942 | // product purchasing is restricted |
| 617 | 943 | $wppb_show_content = false; |
| @@ -628,5 +954,201 @@ | ||
| 628 | 954 | |
| 629 | 955 | // Apply wpautop() to "purchasing restricted" messages as well |
| 630 | 956 | add_filter( 'wppb_content_restriction_message_purchasing_restricted', 'wppb_content_restriction_message_wpautop', 30, 1 ); |
| 631 | 957 | |
| 632 | -} | |
| 958 | +} | |
| 959 | + | |
| 960 | +/** | |
| 961 | + * Exclude Restricted Posts From Query | |
| 962 | + */ | |
| 963 | + if( isset( $wppb_cr_settings['excludePosts'] ) && $wppb_cr_settings['excludePosts'] == 'yes' ) { | |
| 964 | + add_action( 'pre_get_posts', 'wppb_exclude_post_from_query', 40 ); | |
| 965 | + function wppb_exclude_post_from_query( $query ) { | |
| 966 | + | |
| 967 | + if( !function_exists( 'wppb_content_restriction_is_post_restricted' ) || is_admin() || is_singular() ) | |
| 968 | + return; | |
| 969 | + | |
| 970 | + if( isset( $query->query_vars['wc_query'] ) && $query->query_vars['wc_query'] == 'product_query' ) | |
| 971 | + return; | |
| 972 | + | |
| 973 | + // Skip Ultimate Member queries | |
| 974 | + if( isset( $query->query_vars['um_action'] ) || isset( $query->query_vars['um_user'] ) ) | |
| 975 | + return; | |
| 976 | + | |
| 977 | + if( $query->is_main_query() || ( $query->is_search() && isset( $query->query_vars['s'] ) ) ) { | |
| 978 | + | |
| 979 | + remove_action( 'pre_get_posts', 'wppb_exclude_post_from_query', 40 ); | |
| 980 | + | |
| 981 | + $args = $query->query_vars; | |
| 982 | + $args['suppress_filters'] = true; | |
| 983 | + $args['fields'] = 'ids'; | |
| 984 | + | |
| 985 | + // Setup paged arguments so we exclude restricted posts from the current page, not only the first N posts. | |
| 986 | + if( !empty( $query->query_vars['paged'] ) ){ | |
| 987 | + $args['paged'] = round( $args['paged'] / 2 ); | |
| 988 | + if( !empty( $args['posts_per_page'] ) ) | |
| 989 | + $args['posts_per_page'] = $args['posts_per_page'] * 2; | |
| 990 | + else | |
| 991 | + $args['posts_per_page'] = get_option( 'posts_per_page' ) * 2; | |
| 992 | + } | |
| 993 | + | |
| 994 | + if( is_search() ) | |
| 995 | + $args['post_type'] = 'any'; | |
| 996 | + | |
| 997 | + $restricted_posts = get_posts( $args ); | |
| 998 | + | |
| 999 | + $previous_restricted_ids = $query->get( 'post__not_in' ); | |
| 1000 | + if ( ! is_array( $previous_restricted_ids ) ) { | |
| 1001 | + $previous_restricted_ids = array(); | |
| 1002 | + } | |
| 1003 | + | |
| 1004 | + $restricted_ids = array_filter( $restricted_posts, 'wppb_content_restriction_is_post_restricted' ); | |
| 1005 | + $query->set( 'post__not_in', array_merge( $previous_restricted_ids, $restricted_ids ) ); | |
| 1006 | + | |
| 1007 | + } | |
| 1008 | + } | |
| 1009 | + | |
| 1010 | + //Remove restricted forums from the main bbPress query | |
| 1011 | + add_filter( 'bbp_after_has_forums_parse_args', 'wppb_exclude_restricted_forums_from_main_query' ); | |
| 1012 | + function wppb_exclude_restricted_forums_from_main_query( $vars ) { | |
| 1013 | + if( !function_exists( 'wppb_content_restriction_is_post_restricted' ) ) return; | |
| 1014 | + | |
| 1015 | + $query = new WP_Query( $vars ); | |
| 1016 | + | |
| 1017 | + $forum_ids = array(); | |
| 1018 | + | |
| 1019 | + foreach ( $query->posts as $forum ) { | |
| 1020 | + $forum_ids[] = $forum->ID; | |
| 1021 | + } | |
| 1022 | + | |
| 1023 | + $previous_restricted_forums = $query->get( 'post__not_in' ); | |
| 1024 | + if ( ! is_array( $previous_restricted_forums ) ) { | |
| 1025 | + $previous_restricted_forums = array(); | |
| 1026 | + } | |
| 1027 | + | |
| 1028 | + $restricted_forums = array_filter( $forum_ids, 'wppb_content_restriction_is_post_restricted' ); | |
| 1029 | + $updated_restricted_forums = array_merge( $previous_restricted_forums, $restricted_forums ); | |
| 1030 | + | |
| 1031 | + $vars['post__not_in'] = $updated_restricted_forums; | |
| 1032 | + | |
| 1033 | + return $vars; | |
| 1034 | + } | |
| 1035 | + | |
| 1036 | + //Alters the output of the query that displays the Category Archive (including Shop) pages, filtering the restricted products from the output | |
| 1037 | + add_action( 'pre_get_posts', 'wppb_exclude_restricted_products_from_woocoommerce_category_queries', 11 ); | |
| 1038 | + function wppb_exclude_restricted_products_from_woocoommerce_category_queries( $query ) { | |
| 1039 | + | |
| 1040 | + if( is_admin() || !function_exists('wppb_content_restriction_is_post_restricted') ) | |
| 1041 | + return; | |
| 1042 | + | |
| 1043 | + if( isset( $query->query_vars['wc_query'] ) && $query->query_vars['wc_query'] == 'product_query' ){ | |
| 1044 | + | |
| 1045 | + remove_action( 'pre_get_posts', 'wppb_exclude_restricted_products_from_woocoommerce_category_queries', 11 ); | |
| 1046 | + | |
| 1047 | + $args = $query->query_vars; | |
| 1048 | + $args['suppress_filters'] = true; | |
| 1049 | + $args['posts_per_page'] = -1; | |
| 1050 | + | |
| 1051 | + $previous_restricted_ids = $query->get( 'post__not_in' ); | |
| 1052 | + if ( ! is_array( $previous_restricted_ids ) ) { | |
| 1053 | + $previous_restricted_ids = array(); | |
| 1054 | + } | |
| 1055 | + | |
| 1056 | + $transient_key = 'wppb_cr_products_' . md5( serialize( $args ) ); | |
| 1057 | + if ( false === ( $products = get_transient( $transient_key ) ) ) { | |
| 1058 | + $products = wc_get_products( $args ); | |
| 1059 | + set_transient( $transient_key, $products, 2 * HOUR_IN_SECONDS ); | |
| 1060 | + } | |
| 1061 | + | |
| 1062 | + $product_ids = array(); | |
| 1063 | + | |
| 1064 | + foreach ($products as $product) { | |
| 1065 | + $product_ids[] = $product->get_id(); | |
| 1066 | + } | |
| 1067 | + | |
| 1068 | + $restricted_ids = array_filter( $product_ids, 'wppb_content_restriction_is_post_restricted' ); | |
| 1069 | + | |
| 1070 | + $updated_restricted_ids = array_merge( $previous_restricted_ids, $restricted_ids ); | |
| 1071 | + $query->set( 'post__not_in', $updated_restricted_ids ); | |
| 1072 | + | |
| 1073 | + } | |
| 1074 | + | |
| 1075 | + } | |
| 1076 | + | |
| 1077 | + //Alters the output of the [products] shortcode from WooCommerce, filtering restricted products from the output | |
| 1078 | + add_filter( 'woocommerce_shortcode_products_query', 'wppb_exclude_restricted_products_from_woocoommerce_products_shortcode_queries' ); | |
| 1079 | + function wppb_exclude_restricted_products_from_woocoommerce_products_shortcode_queries( $query_args ) { | |
| 1080 | + if( !is_admin() && function_exists('wppb_content_restriction_is_post_restricted') ) { | |
| 1081 | + $posts_per_page = $query_args['posts_per_page']; | |
| 1082 | + | |
| 1083 | + $query_args['suppress_filters'] = true; | |
| 1084 | + $query_args['posts_per_page'] = '-1'; | |
| 1085 | + | |
| 1086 | + $products = wc_get_products($query_args); | |
| 1087 | + | |
| 1088 | + $query_args['posts_per_page'] = $posts_per_page; | |
| 1089 | + | |
| 1090 | + $product_ids = array(); | |
| 1091 | + | |
| 1092 | + foreach ($products as $product) { | |
| 1093 | + $product_ids[] = $product->get_id(); | |
| 1094 | + } | |
| 1095 | + | |
| 1096 | + $previous_restricted_ids = isset($query_args['post__not_in']) ? $query_args['post__not_in'] : array(); | |
| 1097 | + | |
| 1098 | + if ( ! is_array( $previous_restricted_ids ) ) { | |
| 1099 | + $previous_restricted_ids = array(); | |
| 1100 | + } | |
| 1101 | + | |
| 1102 | + $restricted_ids = array_filter( $product_ids, 'wppb_content_restriction_is_post_restricted' ); | |
| 1103 | + $updated_restricted_ids = array_merge( $previous_restricted_ids, $restricted_ids ); | |
| 1104 | + | |
| 1105 | + $query_args['post__not_in'] = $updated_restricted_ids; | |
| 1106 | + } | |
| 1107 | + | |
| 1108 | + return $query_args; | |
| 1109 | + } | |
| 1110 | + | |
| 1111 | + // Exclude restricted posts from Elementor | |
| 1112 | + add_filter( 'elementor/query/query_args', 'wppb_exclude_posts_from_elementor' ); | |
| 1113 | + function wppb_exclude_posts_from_elementor( $query_args ) { | |
| 1114 | + | |
| 1115 | + // check if the form is being displayed in the Elementor editor | |
| 1116 | + $is_elementor_edit_mode = false; | |
| 1117 | + if( class_exists ( '\Elementor\Plugin' ) ){ | |
| 1118 | + $is_elementor_edit_mode = \Elementor\Plugin::$instance->editor->is_edit_mode(); | |
| 1119 | + } | |
| 1120 | + | |
| 1121 | + if ( !$is_elementor_edit_mode && !is_admin() && function_exists( 'wppb_content_restriction_is_post_restricted' ) ) { | |
| 1122 | + $args = $query_args; | |
| 1123 | + $args['suppress_filters'] = true; | |
| 1124 | + $args['fields'] = 'ids'; | |
| 1125 | + | |
| 1126 | + if( !empty( $query_args['paged'] ) ){ | |
| 1127 | + | |
| 1128 | + $args['paged'] = round( $args['paged'] / 2 ); | |
| 1129 | + | |
| 1130 | + if( !empty( $args['posts_per_page'] ) ) | |
| 1131 | + $args['posts_per_page'] = $args['posts_per_page'] * 2; | |
| 1132 | + else | |
| 1133 | + $args['posts_per_page'] = get_option( 'posts_per_page' ) * 2; | |
| 1134 | + | |
| 1135 | + } | |
| 1136 | + | |
| 1137 | + if( is_search() ) | |
| 1138 | + $args['post_type'] = 'any'; | |
| 1139 | + | |
| 1140 | + $restricted_posts = get_posts( $args ); | |
| 1141 | + | |
| 1142 | + $restricted_ids = array_filter( $restricted_posts, 'wppb_content_restriction_is_post_restricted' ); | |
| 1143 | + | |
| 1144 | + if ( ! empty( $restricted_ids ) ) { | |
| 1145 | + if ( isset( $query_args['post__not_in'] ) && is_array( $query_args['post__not_in'] ) ) { | |
| 1146 | + $query_args['post__not_in'] = array_merge( $query_args['post__not_in'], $restricted_ids ); | |
| 1147 | + } else { | |
| 1148 | + $query_args['post__not_in'] = $restricted_ids; | |
| 1149 | + } | |
| 1150 | + } | |
| 1151 | + } | |
| 1152 | + return $query_args; | |
| 1153 | + } | |
| 1154 | + } | |