Skip to content

Commit 58449d7

Browse files
authored
Merge pull request #205 from bcc-code/feature/post-visibility-groups
Feature/post visibility groups
2 parents 7061320 + 13fa45d commit 58449d7

17 files changed

Lines changed: 1734 additions & 579 deletions

plugins/bcc-login/bcc-login.php

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,21 @@
1111
define( 'BCC_LOGIN_PATH', plugin_dir_path( __FILE__ ) );
1212
define( 'BCC_LOGIN_URL', plugin_dir_url( __FILE__ ) );
1313

14-
require_once( 'includes/class-bcc-login-token-utility.php');
14+
require_once( 'includes/class-bcc-login-token-utility.php' );
1515
require_once( 'includes/class-bcc-login-settings.php' );
1616
require_once( 'includes/class-bcc-login-client.php' );
1717
require_once( 'includes/class-bcc-login-endpoints.php' );
1818
require_once( 'includes/class-bcc-login-visibility.php' );
1919
require_once( 'includes/class-bcc-login-users.php' );
2020
require_once( 'includes/class-bcc-login-widgets.php' );
2121
require_once( 'includes/class-bcc-login-feed.php' );
22-
require_once( 'includes/class-bcc-login-updater.php');
23-
require_once( 'includes/class-bcc-coreapi-client.php');
24-
require_once( 'includes/class-bcc-storage.php');
25-
require_once( 'includes/class-bcc-notifications.php');
26-
require_once( 'includes/class-exclusive-lock.php');
22+
require_once( 'includes/class-bcc-login-updater.php' );
23+
require_once( 'includes/class-bcc-coreapi-client.php' );
24+
require_once( 'includes/class-bcc-storage.php' );
25+
require_once( 'includes/class-bcc-notifications.php' );
26+
require_once( 'includes/class-exclusive-lock.php' );
2727

2828
class BCC_Login {
29-
3029
/**
3130
* The plugin instance.
3231
*/
@@ -49,8 +48,6 @@ class BCC_Login {
4948
private BCC_Notifications $_notifications;
5049
private BCC_Storage $_storage;
5150

52-
53-
5451
/**
5552
* Initialize the plugin.
5653
*/
@@ -101,8 +98,6 @@ function plugin_settings_link( $links ) {
10198
return $links;
10299
}
103100

104-
105-
106101
function redirect_login() {
107102
global $pagenow;
108103

@@ -123,14 +118,12 @@ function redirect_login() {
123118
}
124119
}
125120

126-
127121
function hide_admin_bar() {
128122
if (!is_user_logged_in()) {
129123
echo '<style>#wpadminbar { display: none !important; }</style>';
130124
}
131125
}
132126

133-
134127
function add_auto_login_script() {
135128
if ( is_front_page() && !is_user_logged_in() ) {
136129

@@ -163,9 +156,7 @@ function add_auto_login_script() {
163156
}
164157
}
165158

166-
167159
function should_auto_login() {
168-
169160
// Don't log in user if they are already logged in
170161
if (is_user_logged_in() && $this->_client->is_session_valid()) {
171162
return false;
@@ -196,7 +187,6 @@ function should_auto_login() {
196187
return false;
197188
}
198189

199-
200190
/**
201191
* End PHP session (e.g. after logout)
202192
*/
@@ -216,27 +206,42 @@ function get_logout_url() {
216206
return home_url();
217207
}
218208

219-
220209
/**
221210
* Action hook for bcc_site_groups option update.
222211
*/
223212
function on_site_groups_option_update($old_value, $value, $option) {
224213
delete_transient('coreapi_groups');
225214
delete_transient('coreapi_all_groups');
215+
216+
$this->register_selected_site_groups($value);
217+
}
218+
219+
/**
220+
* Register selected site groups for WPML String translation.
221+
*/
222+
function register_selected_site_groups($selected_groups) {
223+
$all_groups = $this->_coreapi->get_all_groups();
224+
225+
foreach ($all_groups as $group) {
226+
if ( !in_array($group->uid, explode(',', $selected_groups)) ) {
227+
continue;
228+
}
229+
230+
do_action( 'wpml_register_single_string', 'bcc-login', '', $group->name );
231+
}
226232
}
227233

228234
/**
229235
* Action hook for bcc_disable_pubsub option update.
230236
*/
231237
function on_disable_pubsub_option_update($old_value, $value, $option) {
232-
if($value == true) {
238+
if ( $value == true ) {
233239
$this->_coreapi->unsubscribe_to_person_updates();
234240
} else {
235241
$this->_coreapi->subscribe_to_person_updates();
236242
}
237243
}
238244

239-
240245
/**
241246
* Activate plugin hook
242247
* Called when plugin is activated

plugins/bcc-login/includes/class-bcc-coreapi-client.php

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,9 @@ public function get_all_groups() {
4646
foreach ($result as $group) {
4747
$groups[$group->uid] = $group;
4848
}
49-
50-
// $group_uids = $this->_settings->filtering_groups;
51-
// $result = $this->fetch_groups($group_uids);
52-
// foreach ($result as $group) {
53-
// $groups[$group->uid] = $group;
54-
// }
55-
56-
// $group_uids = $this->_settings->notification_groups;
57-
// $result = $this->fetch_groups($group_uids);
58-
// foreach ($result as $group) {
59-
// $groups[$group->uid] = $group;
60-
// }
6149

6250
$this->_all_groups = array_values($groups);
63-
51+
6452
$expiration_duration = 60 * 60 * 24; // 1 day
6553
set_transient($cache_key, $this->_all_groups, $expiration_duration);
6654

@@ -98,8 +86,7 @@ function get_translated_site_groups() {
9886
return $site_groups;
9987
}
10088

101-
function fetch_groups($group_uids)
102-
{
89+
function fetch_groups($group_uids) {
10390
$token = $this->get_coreapi_token();
10491

10592
$qry = array(
@@ -138,11 +125,11 @@ function fetch_groups_by_tags($tags) {
138125
$all_groups[$group->uid] = $group;
139126
}
140127
}
128+
141129
return array_values($all_groups);
142130
}
143131

144-
function fetch_groups_by_tag($tag)
145-
{
132+
function fetch_groups_by_tag($tag) {
146133
$token = $this->get_coreapi_token();
147134

148135
$qry = array(
@@ -167,7 +154,6 @@ function fetch_groups_by_tag($tag)
167154

168155
return $body->data;
169156
}
170-
171157

172158
function get_groups_for_user($user_uid) {
173159
$cache_key = 'coreapi_user_groups_'.$user_uid;
@@ -186,7 +172,9 @@ function get_groups_for_user($user_uid) {
186172
}
187173

188174
function fetch_groups_for_user($user_uid) {
189-
if (empty($this->_settings->site_groups)) return array();
175+
if (empty($this->_settings->site_groups)) {
176+
return array();
177+
}
190178

191179
$token = $this->get_coreapi_token();
192180

@@ -196,7 +184,6 @@ function fetch_groups_for_user($user_uid) {
196184
$user_groups = [];
197185

198186
for ($i = 0; $i < $total_groups; $i += $batch_size) {
199-
200187
$batch = array_slice($this->_settings->site_groups, $i, $batch_size);
201188
$request_body = array(
202189
"groupUids" => $batch
@@ -226,11 +213,10 @@ function fetch_groups_for_user($user_uid) {
226213

227214
$user_groups = array_merge($user_groups, $body->data->groupUids);
228215
}
216+
229217
return $user_groups;
230218
}
231219

232-
233-
234220
public function ensure_subscription_to_person_updates() {
235221
if ($this->_settings->disable_pubsub) {
236222
return;
@@ -241,9 +227,9 @@ public function ensure_subscription_to_person_updates() {
241227
return;
242228
}
243229

244-
$lock = new ExclusiveLock( "subscribe_person_updates" );
230+
$lock = new ExclusiveLock("subscribe_person_updates");
245231

246-
if( $lock->lock( ) == FALSE ){
232+
if ($lock->lock() == FALSE) {
247233
return;
248234
}
249235

@@ -331,14 +317,10 @@ public function subscribe_to_person_updates() {
331317
// ]
332318
// }
333319

334-
335-
336-
337320
// Type = email, sms, inapp
338321
public function send_notification($group_uids, $type, $workflow, $payload) {
339322
$token = $this->get_coreapi_token();
340323

341-
342324
//$request_url = $this->_settings->coreapi_base_url . "/notifications/notification?createSubscribers=false&pushNotifications=true";
343325
$request_url = str_replace("https://", "https://notifications.", $this->_settings->coreapi_base_url) . "/notifications/notification/". $type ."?createSubscribers=true&pushNotifications=" . ($this->_settings->notification_dry_run ? "false" : "true");
344326
$request_body = array(

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

Lines changed: 37 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -108,111 +108,86 @@ function add_internal_category_to_member_items($the_list, $type) {
108108
return $result;
109109
}
110110

111-
function add_custom_elements_to_items($the_list){
111+
function add_custom_elements_to_items($the_list) {
112112
$this->add_visibility_and_groups_to_items($the_list);
113113
$this->add_post_type_to_items($the_list);
114114
$this->add_original_language_to_items($the_list);
115115
}
116116

117-
118-
119-
function array_union($x, $y)
120-
{
121-
if (empty($x) && empty($y)){
122-
return [];
123-
}
124-
if (empty($x)){
125-
return $y;
126-
}
127-
if (empty($y)){
128-
return $x;
129-
}
130-
// Use array_merge to combine three arrays:
131-
// 1. Intersection of $x and $y
132-
// 2. Elements in $x that are not in $y
133-
// 3. Elements in $y that are not in $x
134-
$aunion = array_merge(
135-
array_intersect($x, $y), // Intersection of $x and $y
136-
array_diff($x, $y), // Elements in $x but not in $y
137-
array_diff($y, $x) // Elements in $y but not in $x
138-
);
139-
140-
// Return the resulting array representing the union
141-
return $aunion;
142-
}
143-
144-
function add_visibility_and_groups_to_items($the_list){
117+
function add_visibility_and_groups_to_items($the_list) {
145118
global $post;
146119
$visibility = $this->_settings->default_visibility;
120+
147121
if ( $bcc_login_visibility = (int) get_post_meta( $post->ID, 'bcc_login_visibility', true ) ) {
148122
$visibility = $bcc_login_visibility;
149123
}
124+
150125
if ( !empty($this->_settings->site_groups)) {
151126
// Get groups that are checked on post
152-
$post_groups = get_post_meta($post->ID, 'bcc_groups', false);
127+
$post_target_groups = get_post_meta($post->ID, 'bcc_groups', false);
128+
$post_visibility_groups = get_post_meta($post->ID, 'bcc_visibility_groups', false);
153129

154-
// Make sure posts with a group set don't have public visiblity
155-
if (!empty($post_groups))
130+
// Make sure posts with a group set don't have public visibility
131+
if ( !empty($post_target_groups) || !empty($post_visibility_groups) )
156132
{
157133
if ($visibility == BCC_Login_Visibility::VISIBILITY_PUBLIC) {
158134
$visibility = BCC_Login_Visibility::VISIBILITY_SUBSCRIBER;
159135
}
160136
}
161-
162137
}
163-
$this->add_visibility_to_items($the_list,$visibility);
164-
$this->add_groups_to_items($the_list, $visibility);
165138

139+
$this->add_visibility_to_items($the_list, $visibility);
140+
$this->add_groups_to_items($the_list, $visibility);
166141
}
167142

168143
// Include basic visibility settings in feed:
169144
// - public (no authentication required)
170145
// - user (requires authentication)
171146
// - internal:{district name} (requires affiliation with organization in specified district)
172-
function add_visibility_to_items ($the_list, $visibility) {
173-
174-
if ($visibility == BCC_Login_Visibility::VISIBILITY_PUBLIC){
147+
function add_visibility_to_items($the_list, $visibility) {
148+
if ($visibility == BCC_Login_Visibility::VISIBILITY_PUBLIC) {
175149
echo "<bcc:visibility>public</bcc:visibility>\n";
176-
177-
} else if ($visibility == BCC_Login_Visibility::VISIBILITY_SUBSCRIBER){
150+
} else if ($visibility == BCC_Login_Visibility::VISIBILITY_SUBSCRIBER) {
178151
echo "<bcc:visibility>user</bcc:visibility>\n";
179-
} else if ($visibility == BCC_Login_Visibility::VISIBILITY_MEMBER){
152+
} else if ($visibility == BCC_Login_Visibility::VISIBILITY_MEMBER) {
180153
echo "<bcc:visibility>internal:" . $this->_settings->member_organization_name . "</bcc:visibility>\n";
181154
}
182155
}
183-
184156

185-
// Include group uid for each group that the post is visible for or targetted at (notification group)
186-
// E.g.
187-
// <bcc:visiblityGroup>d4c434a7-504a-4246-9a10-def7dbfa982c</bcc:visiblityGroup>
188-
// <bcc:visiblityGroup>25f5bc4d-48e0-4a6e-bf05-6b2a15d70861</bcc:visiblityGroup>
189-
// <bcc:notificationGroup>d4c434a7-504a-4246-9a10-def7dbfa982c</bcc:notificationGroup>
157+
// Include group uid for each group that the post is targeted at or visible for
158+
// E.g.:
159+
// <bcc:targetGroup>d4c434a7-504a-4246-9a10-def7dbfa982c</bcc:targetGroup>
160+
// <bcc:visibilityGroup>d4c434a7-504a-4246-9a10-def7dbfa982c</bcc:visibilityGroup>
161+
// <bcc:visibilityGroup>25f5bc4d-48e0-4a6e-bf05-6b2a15d70861</bcc:visibilityGroup>
190162
function add_groups_to_items($the_list, $visibility) {
191163
global $post;
192164
$result = '';
165+
193166
if ( !empty($this->_settings->site_groups) || !empty($this->_settings->full_content_access_groups) ) {
194-
// Get groups that are checked on post
195-
$post_groups = get_post_meta($post->ID, 'bcc_groups', false);
167+
$post_target_groups = get_post_meta($post->ID, 'bcc_groups', false);
168+
$post_visibility_groups = get_post_meta($post->ID, 'bcc_visibility_groups', false);
196169

197-
// Visiblity Groups: Groups that are checked on post
198-
// + groups with access to all posts (only if there are groups checked on post)
199-
if ($visibility != BCC_Login_Visibility::VISIBILITY_PUBLIC && is_array($post_groups) && count($post_groups))
200-
{
201-
$visibility_post_groups = $this->array_union($post_groups, $this->_settings->full_content_access_groups);
202-
foreach ($visibility_post_groups as $group){
203-
$result = $result . "\t\t<bcc:visibilityGroup>" . $group . "</bcc:visibilityGroup>\n";
204-
}
170+
if ($visibility == BCC_Login_Visibility::VISIBILITY_PUBLIC) {
171+
return; // No groups for public posts
205172
}
206173

207-
if (in_array($post->post_type, $this->_settings->notification_post_types)){
208-
// Notification Groups: Groups that are checked on posts + are eligable for notification
209-
$notification_groups = array_intersect($post_groups, $this->_settings->notification_groups);
210-
foreach ($notification_groups as $group){
211-
$result = $result . "\t\t<bcc:notificationGroup>" . $group . "</bcc:notificationGroup>\n";
174+
// Target Groups: the primary groups that the KPIs are measured for
175+
if (is_array($post_target_groups)) {
176+
foreach ($post_target_groups as $group) {
177+
$result .= "\t\t<bcc:targetGroup>" . $group . "</bcc:targetGroup>\n";
212178
}
213179
}
214180

181+
// Visibility Groups: the secondary groups which can view the post
182+
if (is_array($post_visibility_groups) && count($post_visibility_groups)) {
183+
$visibility_groups = $this->_settings->array_union($post_visibility_groups, $this->_settings->full_content_access_groups);
184+
185+
foreach ($visibility_groups as $group) {
186+
$result .= "\t\t<bcc:visibilityGroup>" . $group . "</bcc:visibilityGroup>\n";
187+
}
188+
}
215189
}
190+
216191
echo $result;
217192
}
218193

0 commit comments

Comments
 (0)