Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added basic support for notifications #142

Merged
merged 2 commits into from
Feb 26, 2024
Merged
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
3 changes: 3 additions & 0 deletions plugins/bcc-login/bcc-login.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
require_once( 'includes/class-bcc-login-updater.php');
require_once( 'includes/class-bcc-coreapi-client.php');
require_once( 'includes/class-bcc-storage.php');
require_once( 'includes/class-bcc-notifications.php');
require_once( 'includes/class-exclusive-lock.php');

class BCC_Login {
Expand All @@ -45,6 +46,7 @@ class BCC_Login {
private BCC_Login_Feed $_feed;
private BCC_Login_Updater $_updater;
private BCC_Coreapi_Client $_coreapi;
private BCC_Notifications $_notifications;
private BCC_Storage $_storage;


Expand All @@ -69,6 +71,7 @@ private function __construct(){
$this->_widgets = new BCC_Login_Widgets( $this->_settings, $this->_client );
$this->_feed = new BCC_Login_Feed( $this->_settings, $this->_client );
$this->_updater = new BCC_Login_Updater( $this->plugin, $this->plugin_slug, $this->plugin_version, $this->plugin_name );
$this->_notifications = new BCC_Notifications( $this->_settings, $this->_coreapi );

if (!empty($this->_settings->site_groups)) {
$this->_coreapi->ensure_subscription_to_person_updates();
Expand Down
45 changes: 45 additions & 0 deletions plugins/bcc-login/includes/class-bcc-coreapi-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function __construct(BCC_Login_Settings $login_settings, BCC_Storage $storage)
{
$this->_settings = $login_settings;
$this->_storage = $storage;

}

function get_site_groups() {
Expand Down Expand Up @@ -164,6 +165,50 @@ private function subscribe_to_person_updates() {
}
}

// {
// "workflowId": "simpleinapp",
// "personUid": "fcfc4132-1aaa-464f-9080-f49a06287f80",
// "notificationPayload": [
// {
// "language": "en-US",
// "notification": "You need a new app for live broadcasts [cta text=\"Download here\" link=\"https://portal.bcc.no/en/bcc-connect-en/important-new-app-for-viewing-live-brunstad-transmissions/\"]"
// },
// {
// "language": "no-NO",
// "notification": "Du trenger en ny app for livesendinger [cta text=\"Last ned her\" link=\"https://portal.bcc.no/bcc-connect/viktig-informasjon-ny-app-vil-sende-live-fra-brunstad/\"]"
// }
// ]
// }

public function send_notification($group_uids, $workflow, $payload) {
$token = $this->get_coreapi_token();


//$request_url = $this->_settings->coreapi_base_url . "/notifications/notification?createSubscribers=false&pushNotifications=true";
$request_url = "https://ca-notifications-api-prod.salmonmoss-1fccea79.westeurope.azurecontainerapps.io" . "/notifications/notification?createSubscribers=false&pushNotifications=true";
$request_body = array(
"workflowId" => $workflow,
"groupUid" => $group_uids[0], //First group only for now
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a better solution before we go live, indeed! Taking only the first group isn't a good idea.

"notificationPayload" => $payload
);

$response = wp_remote_post($request_url, array(
"body" => wp_json_encode( $request_body ),
"headers" => array(
"Authorization" => "Bearer " . $token,
"Content-Type" => "application/json"
)
));

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

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

public function get_coreapi_token() {
$cached_token = $this->_storage->get('coreapi_token');
if ($cached_token !== null) {
Expand Down
159 changes: 159 additions & 0 deletions plugins/bcc-login/includes/class-bcc-notifications.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php

class BCC_Notifications {

private BCC_Login_Settings $settings;
private BCC_Coreapi_Client $core_api;

function __construct( BCC_Login_Settings $settings, BCC_Coreapi_Client $core_api) {
$this->settings = $settings;
$this->core_api = $core_api;

add_action( 'transition_post_status', array( $this, 'on_post_status_transition' ), 10, 3 );
add_action( 'send_scheduled_notification', array( $this, 'send_notification' ), 10, 1 );

}

public function on_post_status_transition( $new_status, $old_status, $post ) {
if ('publish' === $new_status && 'publish' !== $old_status) {
wp_schedule_single_event( time() + 180, 'send_scheduled_notification', array( $post->ID ) );
// $this->send_notification($post->ID);
}
}

public function send_notification( $post_id ) {
// Fetch the post object since only the ID is passed through scheduling.

$post = get_post( $post_id );
if ( ! $post ) {
return; // Exit if the post doesn't exist.
}
$post_type = $post->post_type;

// 1. Get groups for post
$post_groups = get_post_meta( $post->ID, 'bcc_groups', false );

// 2. Get default language and url for site
$site_language = get_bloginfo('language'); //E.g. "en-US"
$site_url = get_site_url();

// 3. Define array of content to send
$payload = [];

$is_multilinguage_post = false;

// 4. Handle multilingual posts
if (defined('ICL_SITEPRESS_VERSION')) {
// WPML is installed and active.

// Check if post has been translated
$has_translations = apply_filters( 'wpml_element_has_translations', '', $post_id, $post_type );
if ($has_translations) {
$trid = apply_filters( 'wpml_element_trid', NULL, $post_id, 'post_' . $post_type);
$translations = apply_filters( 'wpml_get_element_translations', NULL, $trid, 'post_' . $post_type );

// Determine if current post is the original
$is_orginal = false;
foreach ($translations as $lang_code => $details) {
if ($details->element_id == $post_id) {
$is_orginal = $details->original == "1";
break;
}
}

$is_multilinguage_post = true;

if ($is_orginal) {
foreach ( $translations as $lang => $details ) {
$translation = get_post($details->element_id);
$language_details = apply_filters( 'wpml_post_language_details', NULL, $translation->ID );
$language_code = str_replace('_', '-', $language_details["locale"]);
$excerpt = get_the_excerpt($translation);
if ($translation->post_status == 'publish') {
$payload[] = [
'title' => $translation->post_title,
'language' => $language_code,
'excerpt' => $excerpt,
'url' => get_permalink( $translation ) ?? ($site_url . '/?p=' . $translation->ID . '&lang=' . $language_code),
'image_url' => get_the_post_thumbnail_url($translation->ID,'thumbnail'),
'date' => str_replace(' ','T',$translation->post_date_gmt) . 'Z'
];
}
}
} else {
// Don't process non-default languages of posts that have translations
// This is to avoid sending duplicate notifications
return;
}

}
}

if (!$is_multilinguage_post){
$excerpt = get_the_excerpt($post);
$payload[] = [
'title' => $post->post_title,
'language' => $site_language,
'excerpt' => $excerpt,
'url' => get_permalink( $post ) ?? ($site_url . '/?p=' . $post->ID),
'image_url' => get_the_post_thumbnail_url($post->ID,'thumbnail'),
'date' => str_replace(' ','T',$post->post_date_gmt) . 'Z'
];
}

// Notification logic goes here.
if (isset($post_groups) && !empty($post_groups) && !empty($payload)) {

$inapp_payload = [];
$email_payload = [];
foreach ($payload as $item) {
$inapp_payload[] = [
"language" => $item["language"],
"notification" => $item["title"] . '<br><small>' . $item["excerpt"] . '</small> [cta text="' . $this->get_read_more_text($item["language"]) . '" link="' . $item["url"] . '"]'
];
$email_payload[] = [
"language" => $item["language"],
"subject" => $item["title"],
"banner" => $item["image_url"] !== false ? $item["image_url"] : null,
"title" => $item["title"],
"body" => $item["excerpt"] . '<br> [cta text="' . $this->get_read_more_text($item["language"]) . '" link="' . $item["url"] . '"]'
];
}
// // Set subtitle to title from other language (should probably be fixed in template...)
// foreach ($email_payload as $email) {
// foreach ($email_payload as $other_email) {
// if ($email["language"] != $other_email["language"]) {
// $email["sub_title"] = $other_email["title"];
// }
// }
// }
$this->core_api->send_notification($post_groups, 'simpleinapp', $inapp_payload);
$this->core_api->send_notification($post_groups, 'simpleemail', $email_payload);
}

}


function get_read_more_text($lang) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could instead use translation in WordPress for that: https://codex.wordpress.org/I18n_for_WordPress_Developers#Strings_for_Translation

if ($lang && strlen($lang) >= 2) {
$lang_code = substr($lang, 0, 2);
switch ($lang_code) {
case "no":
return 'Les mer';
break;
case "nb":
return 'Les mer';
break;
case "en":
return 'Read more';
break;
default:
return 'Read more';
break;
}
}
return 'Read more';

}

}