@@ -6,6 +6,7 @@ class BCC_Login_Visibility {
66 private BCC_Login_Client $ _client ;
77 private BCC_Coreapi_Client $ _coreapi ;
88
9+ public const VISIBILITY_PUBLIC_ONLY = -1 ;
910 public const VISIBILITY_DEFAULT = 0 ;
1011 public const VISIBILITY_PUBLIC = 1 ;
1112 public const VISIBILITY_SUBSCRIBER = 2 ;
@@ -15,14 +16,16 @@ class BCC_Login_Visibility {
1516 private $ levels = array (
1617 'bcc-login-member ' => self ::VISIBILITY_MEMBER ,
1718 'subscriber ' => self ::VISIBILITY_SUBSCRIBER ,
18- 'public ' => self ::VISIBILITY_PUBLIC
19+ 'public ' => self ::VISIBILITY_PUBLIC ,
20+ 'public-only ' => self ::VISIBILITY_PUBLIC_ONLY
1921 );
2022
2123 // A mapping of level -> title.
2224 private $ titles = array (
2325 self ::VISIBILITY_PUBLIC => 'Public ' ,
24- self ::VISIBILITY_SUBSCRIBER => 'Authenticated Users ' ,
25- self ::VISIBILITY_MEMBER => 'Members '
26+ self ::VISIBILITY_SUBSCRIBER => 'Logged In ' ,
27+ self ::VISIBILITY_MEMBER => 'Members ' ,
28+ self ::VISIBILITY_PUBLIC_ONLY => 'Not Logged In ' ,
2629 );
2730
2831 private $ visibility_post_types = array ( 'post ' , 'page ' , 'attachment ' , 'nav_menu_item ' );
@@ -353,6 +356,19 @@ function filter_pre_get_posts( $query ) {
353356 'value ' => $ this ->_client ->get_current_user_level ()
354357 );
355358
359+ // If user is logged in, exclude posts where visiblity is set to public-only
360+ if ( is_user_logged_in () ) {
361+ $ rules = array (
362+ 'relation ' => 'AND ' ,
363+ $ rules ,
364+ array (
365+ 'key ' => 'bcc_login_visibility ' ,
366+ 'compare ' => '!= ' ,
367+ 'value ' => self ::VISIBILITY_PUBLIC_ONLY
368+ )
369+ );
370+ }
371+
356372 // Include also posts where visibility isn't specified based on the Default Content Access
357373 if ( $ this ->_client ->get_current_user_level () >= $ this ->_settings ->default_visibility ) {
358374 $ rules = array (
@@ -455,9 +471,7 @@ function filter_by_queried_target_groups($query) {
455471 * @return WP_Post[]
456472 */
457473 function filter_menu_items ( $ items ) {
458- if ( current_user_can ( 'edit_posts ' ) || $ this ->_settings ->show_protected_menu_items ) {
459- return $ items ;
460- }
474+
461475
462476 $ level = $ this ->_client ->get_current_user_level ();
463477 $ removed = array ();
@@ -476,6 +490,18 @@ function filter_menu_items( $items ) {
476490 $ visibility = $ this ->_settings ->default_visibility ;
477491 }
478492
493+ // Hide public-only menu items for users who are logged in (including editors)
494+ if ( $ visibility == self ::VISIBILITY_PUBLIC_ONLY && is_user_logged_in () ) {
495+ $ removed [] = $ item ->ID ;
496+ unset( $ items [ $ key ] );
497+ continue ;
498+ }
499+
500+ // Otherwise, show everything to editors
501+ if ( current_user_can ( 'edit_posts ' ) || $ this ->_settings ->show_protected_menu_items ) {
502+ continue ;
503+ }
504+
479505 if ( $ visibility && $ visibility > $ level ) {
480506 $ removed [] = $ item ->ID ;
481507 unset( $ items [ $ key ] );
@@ -555,12 +581,24 @@ public function get_number_of_user_groups() {
555581 * @return string
556582 */
557583 function on_render_block ( $ block_content , $ block ) {
584+
585+ $ visibility_set = false ;
586+ if ( isset ( $ block ['attrs ' ]['bccLoginVisibility ' ] ) ) {
587+ $ visibility = (int ) $ block ['attrs ' ]['bccLoginVisibility ' ];
588+ $ visibility_set = true ;
589+ }
590+
591+ // Hide public-only blocks for users who are logged in (including editors)
592+ if ( $ visibility_set && $ visibility == self ::VISIBILITY_PUBLIC_ONLY && is_user_logged_in () ) {
593+ return '' ;
594+ }
595+
596+ // Editors can see all other blocks
558597 if ( current_user_can ( 'edit_posts ' ) ) {
559598 return $ block_content ;
560599 }
561600
562- if ( isset ( $ block ['attrs ' ]['bccLoginVisibility ' ] ) ) {
563- $ visibility = (int ) $ block ['attrs ' ]['bccLoginVisibility ' ];
601+ if ( $ visibility_set ) {
564602 if (!$ visibility ) {
565603 $ visibility = $ this ->_settings ->default_visibility ;
566604 }
0 commit comments