Skip to content

Commit 1c1e68c

Browse files
authored
Merge pull request #182 from bcc-code:feature/public-only-content
Add support for showing content explicitly to public users only
2 parents 2cf8148 + f29ecc8 commit 1c1e68c

File tree

4 files changed

+56
-12
lines changed

4 files changed

+56
-12
lines changed

docs/signon/configuration.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ It is strongly discouraged to enable the *OIDC_CREATE_USERS* setting. This funct
2626
This setting controls the default visability of new pages and posts.
2727

2828
1. **Public** - visible to all users, without logging in
29-
2. **Authenticated Users** - only visible to logged in users
29+
2. **Logged In** - only visible to logged in users
3030
3. **Members** - only visible to members of the specified *Member Organization* (see below)
31+
4. **Not Logged In** - only visible to users who are not logged in
3132

3233
## Feed Key
3334

plugins/bcc-login/includes/class-bcc-login-settings.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,9 @@ function register_settings() {
295295
'value' => $this->_settings->default_visibility,
296296
'values' => array(
297297
1 => __( 'Public', 'bcc-login' ),
298-
2 => __( 'Authenticated Users', 'bcc-login' ),
299-
3 => __( 'Members', 'bcc-login' ),
298+
2 => __( 'Logged In', 'bcc-login' ),
299+
3 => __( 'Members', 'bcc-login' ),
300+
-1 => __( 'Not Logged In', 'bcc-login' ),
300301
)
301302
)
302303
);

plugins/bcc-login/includes/class-bcc-login-visibility.php

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

plugins/bcc-login/src/visibility.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,16 @@ const visibilityOptions = [
2929
},
3030
{
3131
value: levels.subscriber,
32-
label: __("Authenticated Users"),
32+
label: __("Logged In"),
3333
},
3434
{
3535
value: levels["bcc-login-member"],
3636
label: sprintf(__("%s Members"), localName),
3737
},
38+
{
39+
value: levels["public-only"],
40+
label: __("Not Logged In"),
41+
},
3842
];
3943

4044
function VisibilityOptions({

0 commit comments

Comments
 (0)