Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions plugins/bcc-login/includes/class-bcc-coreapi-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,33 @@ public function send_notification($group_uids, $type, $workflow, $payload, $test
if ($response['response']['code'] != 200) {
wp_die("Could not send notification: " . print_r($response, true));
}

$response_body = json_decode( wp_remote_retrieve_body( $response ) );
return $response_body;
}

public function get_notification($notification_id) {
$token = $this->get_coreapi_token();

$notifications_base = str_replace("https://", "https://notifications.", $this->_settings->coreapi_base_url);
$request_url = $notifications_base . "/notifications/notification/" . $notification_id;

$response = wp_remote_get($request_url, array(
"headers" => array(
"Authorization" => "Bearer " . $token,
)
));

if ( is_wp_error( $response ) ) {
wp_die( $response->get_error_message() );
}

if ($response['response']['code'] != 200) {
wp_die("Could not get notification: " . print_r($response, true));
}

$notification = json_decode( wp_remote_retrieve_body( $response ) );
return $notification;
}
Comment thread
jkbcz marked this conversation as resolved.

public function get_coreapi_token() {
Expand Down
44 changes: 13 additions & 31 deletions plugins/bcc-login/includes/class-bcc-login-visibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,25 +120,36 @@ function on_init() {
'items' => [
'type' => 'object',
'properties' => [
'id' => ['type' => 'string'],
'type' => ['type' => 'string'],

'date' => [
'type' => 'string',
'format' => 'date-time', // ISO-8601 like 2025-12-12T09:15:00Z
],

'refresh_date' => [
'type' => 'string',
'format' => 'date-time', // ISO-8601 like 2025-12-12T09:15:00Z
],
'notification_groups' => [
'type' => 'array',
'items' => [
'type' => 'string',
],
],
'total' => ['type' => 'number'],
'sent' => ['type' => 'number'],
'delivered' => ['type' => 'number'],
'error' => ['type' => 'number'],
],
'required' => [ 'date', 'notification_groups' ],
'required' => [ 'date' ],
],
],
],
'single' => true,
'type' => 'array',
'default' => [],
'sanitize_callback' => array( $this, 'sanitize_sent_notifications_meta' ),
) );
Comment thread
jkbcz marked this conversation as resolved.
}
}
Expand Down Expand Up @@ -1378,35 +1389,6 @@ static function on_uninstall() {
delete_metadata( 'post', 0, 'bcc_login_visibility', '', true );
}

public static function sanitize_sent_notifications_meta( $value, $meta_key, $object_type ) {
// Normalize to array
if ( ! is_array( $value ) ) {
return [];
}

$out = [];
foreach ( $value as $item ) {
if ( ! is_array( $item ) ) {
continue;
}

$date = isset( $item['date'] ) && is_string( $item['date'] ) ? $item['date'] : null;
$groups = isset( $item['notification_groups'] ) && is_array( $item['notification_groups'] )
? array_values( array_filter( $item['notification_groups'], fn( $uid ) => is_string( $uid ) && $uid !== '' ) )
: [];

if ( $date ) {
$out[] = array(
'date' => $date,
'notification_groups' => $groups,
);
}
}

// Reindex
return array_values( $out );
}

/**
* Magic token functions
*/
Expand Down
89 changes: 69 additions & 20 deletions plugins/bcc-login/includes/class-bcc-notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function __construct(BCC_Login_Settings $settings, BCC_Coreapi_Client $core_api)

add_action('admin_init', array($this, 'register_wpml_strings'));
add_action('rest_api_init', array($this, 'register_send_notifications_endpoint'));
add_action('rest_api_init', array($this, 'register_refresh_notification_statistics_endpoint'));
add_action('rest_api_init', array($this, 'register_wpml_translations_endpoint'));
}

Expand All @@ -32,9 +33,9 @@ public function register_send_notifications_endpoint() {
}

$test_email_address = $request->get_param('testEmailAddress') ?: null;
$this->send_notification($post_id, $test_email_address);
$updated_notifications = $this->send_notification($post_id, $test_email_address);

return new WP_REST_Response(null, 200);
return new WP_REST_Response($updated_notifications, 200);
Comment thread
jkbcz marked this conversation as resolved.
} else {
return new WP_REST_Response(array('error' => 'postId parameter is required'), 400);
}
Expand All @@ -45,6 +46,32 @@ public function register_send_notifications_endpoint() {
));
}

public function register_refresh_notification_statistics_endpoint() {
register_rest_route('bcc-login/v1', '/refresh-notification-statistics', array(
'methods' => 'POST',
'callback' => function (WP_REST_Request $request) {
$post_id = $request->get_param('postId');
$notification_id = $request->get_param('notificationId');

if (!$post_id || !$notification_id) {
return new WP_REST_Response(array('error' => 'postId or notificationId parameter is required'), 400);
}

if (!in_array(get_post_type($post_id), $this->settings->notification_post_types)) {
return new WP_REST_Response(array('error' => 'This post type is not allowed to sent notifications'), 400);
}

$notification = $this->core_api->get_notification($notification_id);
$updated_notifications = $this->update_notification_statistics($post_id, $notification);

return new WP_REST_Response($updated_notifications, 200);
},
'permission_callback' => function () {
return current_user_can('edit_posts');
},
));
}

public function register_wpml_translations_endpoint() {
register_rest_route('bcc-login/v1', '/wpml-translations/(?P<id>\d+)', array(
'methods' => 'GET',
Expand Down Expand Up @@ -96,6 +123,8 @@ public function send_notification($post_id, $test_email_address = null) {
$notification_groups = $this->settings->array_union($notification_groups, $post_visibility_groups);
}

$updated_notifications = null;

// Notification logic goes here
if (!empty($notification_groups)) {
// 2. Get default language and url for site
Expand Down Expand Up @@ -287,8 +316,9 @@ public function send_notification($post_id, $test_email_address = null) {
$requires_action_inapp_payload[] = $inapp_item;
}

$this->core_api->send_notification($post_target_groups, 'email', 'simpleemail', $requires_action_email_payload, $test_email_address);
$sent_notification = $this->core_api->send_notification($post_target_groups, 'email', 'simpleemail', $requires_action_email_payload, $test_email_address);
if (!$test_email_address) {
$updated_notifications = $this->save_notification_data($post_id, $sent_notification, "target_groups");
$this->core_api->send_notification($post_target_groups, 'inapp', 'simpleinapp', $requires_action_inapp_payload);
}
Comment thread
jkbcz marked this conversation as resolved.

Expand Down Expand Up @@ -318,19 +348,15 @@ public function send_notification($post_id, $test_email_address = null) {
$for_information_inapp_payload[] = $inapp_item;
}

$this->core_api->send_notification($post_visibility_groups, 'email', 'simpleemail', $for_information_email_payload, $test_email_address);
$sent_notification = $this->core_api->send_notification($post_visibility_groups, 'email', 'simpleemail', $for_information_email_payload, $test_email_address);
if (!$test_email_address) {
$updated_notifications = $this->save_notification_data($post_id, $sent_notification, "visibility_groups");
$this->core_api->send_notification($post_visibility_groups, 'inapp', 'simpleinapp', $for_information_inapp_payload);
}
Comment thread
jkbcz marked this conversation as resolved.

error_log('DEBUG: ' . __METHOD__ . ' - Sent notifications for ' . count($post_visibility_groups) . ' visibility groups.');
}

// Store sent notification data (skipped for test sends)
if (!$test_email_address) {
$this->save_notification_data($post_id, $notification_groups);
}

error_log('DEBUG: ' . __METHOD__ . ' - Sent notifications for ' . count($email_payload) . ' languages.');

} else {
Expand All @@ -340,30 +366,53 @@ public function send_notification($post_id, $test_email_address = null) {
else {
error_log('DEBUG: ' . __METHOD__ . ' - No notification groups found for post: ' . $post_id);
}

return $updated_notifications;
}

// Save the date and the notification groups of the notification
public function save_notification_data($post_id, $notification_groups = array()) {
// Ensure array of strings (UIDs)
$group_uids = array_values(array_filter((array) $notification_groups, function($uid) {
return is_string($uid) && $uid !== '';
}));

// Load current meta; normalize to array
public function save_notification_data($post_id, $response, $type) {
$sent_notifications = get_post_meta($post_id, 'sent_notifications', true);
if (!is_array($sent_notifications)) {
$sent_notifications = [];
}

// Append new record that matches the REST schema
$sent_notifications[] = array(
'date' => gmdate('c'), // ISO-8601 UTC
'notification_groups' => $group_uids,
'id' => $response->id,
'type' => $type,
'date' => gmdate('c'),
'total' => $response->deliveryStatistics->total,
'sent' => $response->deliveryStatistics->sent,
'delivered' => $response->deliveryStatistics->delivered,
'error' => $response->deliveryStatistics->error
);
Comment thread
jkbcz marked this conversation as resolved.

// Reindex and save
$sent_notifications = array_values($sent_notifications);
update_post_meta($post_id, 'sent_notifications', $sent_notifications);

return $sent_notifications;
}

public function update_notification_statistics($post_id, $notification_response) {
$sent_notifications = get_post_meta($post_id, 'sent_notifications', true);
if (!is_array($sent_notifications)) {
$sent_notifications = [];
}

foreach($sent_notifications as $key => $notification ) {
if (array_key_exists('id', $notification) && $notification['id'] == $notification_response->id) {
$notification['total'] = $notification_response->deliveryStatistics->total;
$notification['sent'] = $notification_response->deliveryStatistics->sent;
$notification['delivered'] = $notification_response->deliveryStatistics->delivered;
$notification['error'] = $notification_response->deliveryStatistics->error;
$notification['refresh_date'] = gmdate('c');
$sent_notifications[$key] = $notification;
}
}

$sent_notifications = array_values($sent_notifications);
update_post_meta($post_id, 'sent_notifications', $sent_notifications);
return $sent_notifications;
}

public function bcc_get_wpml_post_translations($post_id) {
Expand Down
12 changes: 6 additions & 6 deletions plugins/bcc-login/src/components/send-notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ const SendNotifications = ({ label, postId, postType, status, targetGroupsCount,
throw new Error(`Request failed (${response.status}): ${text}`);
}

window.dispatchEvent(new CustomEvent('bcc:notificationSent', {
detail: {
date: new Date().toLocaleString(),
no_of_groups: targetGroupsCount + visibilityGroupsCount
}
}));
const responseBody = await response.json()
if(responseBody) {
window.dispatchEvent(new CustomEvent('bcc:notificationsUpdated', {
detail: responseBody
}));
}

showToast('success');
} catch (error) {
Expand Down
Loading
Loading