Skip to content
This repository has been archived by the owner on Dec 12, 2023. It is now read-only.

Commit

Permalink
Firebase Notifications (#521)
Browse files Browse the repository at this point in the history
  • Loading branch information
svenseeberg committed May 11, 2017
1 parent fb8b543 commit 5102909
Show file tree
Hide file tree
Showing 8 changed files with 483 additions and 0 deletions.
57 changes: 57 additions & 0 deletions wp-content/plugins/firebase-notifications/css/send.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
.notification-editor {
padding: 0.6em;
}

div.tabs {
min-height: 13em;
height: 13em;
position: relative;
line-height: 1;
z-index: 0;
}
div.tabs > div {
display: inline
}
div.tabs > div > a {
padding: 0.2em;
border: 0.1em;
}
div.tabs > div:not(:target) > a {
border-bottom: 0.2em solid #BBB;
}
#deflang:target > a {
border-bottom: 0.2em solid #F00;
}
#deflang:not(:target) > a {
border-bottom: 0.2em solid #A00;
}
div.tabs > div:target > a {
border-bottom: 0.2em solid #000;
}
div.tabs > div > div {
z-index: -2;
left: 0;
top: 1.3em;
bottom: 0; right: 0;
overflow: auto;
padding: 0.3em;
border: 0.1em;
background: #f1f1f1;
}
div.tabs > div:not(:target) > div {
position: absolute
}
div.tabs > div:target > div {
position: absolute;
z-index: -1
}

.pn-title {
width: 300px;
}

.pn-message {
width: 300px;
resize: none;
height: 7em;
}
3 changes: 3 additions & 0 deletions wp-content/plugins/firebase-notifications/js/send.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
jQuery( document ).ready(function() {
window.location.href = "#deflang";
});
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
msgid "Please fill in the title and message for the red underlined language. Optionally, translations can be filled in as well."
msgstr "Bitte füllen Sie die Felder Titel und Nachricht für die rot unterstrichene Sprache aus. Optional können auch Übersetzungen eingegeben werden."

msgid "Languages without manual translation receive:"
msgstr "Sprachen ohne manuelle Übersetzung erhalten:"

msgid "Title"
msgstr "Titel"

msgid "Message"
msgstr "Nachricht"

msgid "Automatic translation"
msgstr "Automatische Übersetzung"

msgid "No message"
msgstr "Keine Benachrichtigung"

msgid "Message in original language (marked red)"
msgstr "Benachrichtigung in Originalsprache (rot markiert)"

msgid "Send Notification"
msgstr "Benachrichtigung senden"

msgid "This blog must manage it's own Firebase Cloud Messaging settings."
msgstr "Dieser Blog muss seine Firebase Cloud Messaging Einstellungen selbst verwalten."

msgid "This blog is allowed to use the network wide Firebase Cloud Messaging settings."
msgstr "Dieser Blog darf die netzwerkweiten Firebase Cloud Messaging Einstellungen nutzen."

msgid "This blog must use the network wide Firebase Cloud Messaging settings."
msgstr "Dieser Blog muss die netzwerkweiten Firebase Cloud Messaging Einstellungen nutzen."

msgid "Use network settings"
msgstr "Netzwerkeinstellungen nutzen"

msgid "Network settings for blogs"
msgstr "Netzwerkeinstellungen für Blogs"

msgid "Each blog must manage it's own Firebase Cloud Messaging settings."
msgstr "Jeder Blog muss seine Firebase Cloud Messaging Einstellungen selbst verwalten."

msgid "Each blog is allowed to use the network wide Firebase Cloud Messaging settings."
msgstr "Jeder Blog darf die netzwerkweiten Firebase Cloud Messaging Einstellungen nutzen."

msgid "Each blog must use the network wide Firebase Cloud Messaging settings."
msgstr "Jeder Blog muss die netzwerkweiten Firebase Cloud Messaging Einstellungen nutzen."

msgid "Groups"
msgstr "Gruppen"

msgid "Add blog ID and WPML language to topic name.<br>The topic name is then<br>/topics/[blog_id]-[language_code]-[topic],<br>e.g. /topics/1-en-news"
msgstr "Blog ID fund WPML Sprache zum Topic Namen hinzuf&uuml;gen.<br>Der Topic Name wird dann<br>/topics/[blog_id]-[language_code]-[topic],<br>z.B. /topics/1-en-news"

msgid "Groups (separate with white space)"
msgstr "Gruppen (mit Leerzeichen trennen)"
77 changes: 77 additions & 0 deletions wp-content/plugins/firebase-notifications/notifications.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

function write_firebase_notification() {
//send message if nonce is valid
if ( wp_verify_nonce( $_POST['_wpnonce'], 'ig-fb-send-nonce' ) && current_user_can('publish_pages') ) {
$languages = icl_get_languages();
$items = array();
foreach( $languages as $key => $value ) {
$items[$key] = array( 'title' => $_POST['pn-title_'.$key], 'message' => $_POST['pn-message_'.$key], 'lang' => $key, 'translate' => $_POST['pn-translate'], 'group' => $_POST['fbn_groups'] );
}
$myNotification = new FirebaseNotificationsService();
$myNotification->translate_send_notifications( $items );
}

wp_enqueue_style( 'ig-fb-style-send', plugin_dir_url(__FILE__) . '/css/send.css' );
wp_enqueue_script( 'ig-fb-js-send', plugin_dir_url(__FILE__) . '/js/send.js' );
// display form
echo write_firebase_notification_form();
}

function write_firebase_notification_form() {
$header = "<h1>".get_admin_page_title()."</h1>
<form method='post'>
".wp_nonce_field( 'ig-fb-send-nonce' )."
<div class='notification-editor'>
<div class='tabs'>
";

if( get_site_option( 'fbn_force_network_settings' ) == '2' || ( get_site_option( 'fbn_force_network_settings' ) == '1' && get_blog_option( $blog_id, 'fbn_use_network_settings' ) == '1' ) ) {
$groups = get_site_option( 'fbn_groups' );
} else {
$groups = get_blog_option( get_current_blog_id(), 'fbn_groups' );
}

$select = "<select name='fbn_groups'>";
foreach( explode( ' ', $groups ) as $group ) {
$select .= "<option>$group</option>";
}
$select .= "</select>";

$footer = "
</div>
</div>
<p>".__('Please fill in the title and message for the red underlined language. Optionally, translations can be filled in as well.', 'firebase-notifications')."</p>
<p>".__('Languages without manual translation receive:', 'firebase-notifications')."</p>
<fieldset>
<ul>
".( class_exists('TranslationService') ? "<li><input type='radio' id='at' name='pn-translate' value='at' checked='checked'><label for='at'> ".__('Automatic translation', 'firebase-notifications')."</label></li>":"")."
<li><input type='radio' id='no' name='pn-translate' value='no' ".( !class_exists('TranslationService') ? " checked='checked'" : "")."><label for='no'> ".__('No message', 'firebase-notifications')."</label></li>
<li><input type='radio' id='or' name='pn-translate' value='or'><label for='or'> ".__('Message in original language (marked red)', 'firebase-notifications')."</label></li>
</ul>
</fieldset>
<p>".__('Groups', 'firebase-notifications').": $select</p>
<button>".__('Send Notification', 'firebase-notifications')."</button>
</form>
";

$tabs = "";
$languages = icl_get_languages();
foreach( $languages as $key => $value ) {
$default = ($value['active'] == "1" ? "deflang" : $value['code'] );
$tabs .= "
<div id='".$default."'>
<a href='#".$default."'>".$value['translated_name']."</a>
<div>
<table class='tabtable'>
<tr><td>".__('Title', 'firebase-notifications')."</td><td><input name='pn-title_".$value['code']."' type='text' class='pn-title' maxlength='50'></td></tr>
<tr><td>".__('Message', 'firebase-notifications')."</td><td><textarea name='pn-message_".$value['code']."' class='pn-message' maxlength='140'></textarea></td></tr>
</table>
</div>
</div>
";
}
return $header.$tabs.$footer;
}

?>
67 changes: 67 additions & 0 deletions wp-content/plugins/firebase-notifications/plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Plugin Name: FCM Notifications
* Description: Sending FCM Messages to Smartphones with WPML support
* Version: 1.0
* Author: Sven Seeberg
* Author URI: https://github.com/sven15
* License: MIT
* Text Domain: firebase-notifications
*/

require_once __DIR__ . '/service.php';
require_once __DIR__ . '/notifications.php';
require_once __DIR__ . '/settings.php';


function fb_pn_menu() {
load_plugin_textdomain( 'firebase-notifications', false, dirname( plugin_basename( __FILE__ ) ) . '/lang/' );
add_menu_page( 'Push Notifications', 'Push Notifications', 'publish_pages', 'fb-pn', 'write_firebase_notification', 'dashicons-email-alt', $position = 99 );
add_submenu_page( 'fb-pn', 'Firebase Notifications Settings', 'Settings', 'manage_options', 'fb-pn-settings', 'firebase_notification_settings' );
}
add_action( 'admin_menu', 'fb_pn_menu' );


function fb_pn_network_menu() {
load_plugin_textdomain( 'firebase-notifications', false, dirname( plugin_basename( __FILE__ ) ) . '/lang/' );
add_menu_page( "Firebase Notifications Network Settings", "FCM Push Notifications", 'manage_network_options', 'fb-pn-network-settings', 'firebase_notification_network_settings');
}
add_action( 'network_admin_menu', 'fb_pn_network_menu' );


function firebase_notifications_settings() {
$settings['fbn_auth_key'] = '0';
$settings['fbn_api_url'] = 'https://fcm.googleapis.com/fcm/send';
$settings['fbn_use_network_settings'] = '1'; //0 use blog settings; 1 use network settings
$settings['fbn_groups'] = 'news';
return $settings;
}


function firebase_notifications_network_settings() {
$settings['fbn_auth_key'] = '0';
$settings['fbn_api_url'] = 'https://fcm.googleapis.com/fcm/send';
$settings['fbn_force_network_settings'] = '0'; // 0: allow settings for each blog; 1: blogs CAN use network settings; 2: blogs MUST use network settings
$settings['fbn_per_blog_topic'] = '1'; //add blog id and wpml language to topic, 1 = add, 0 = only group
$settings['fbn_groups'] = 'news';
return $settings;
}


function firebase_notifications_registration() {
$default_network_settings = firebase_notifications_network_settings();
foreach( $default_network_settings as $key => $value ) {
add_site_option( $key, $value );
}

$default_blog_settings = firebase_notifications_settings();
$all_blogs = get_sites();
foreach ( $all_blogs as $blog ) {
foreach ( $default_blog_settings as $key => $value ) {
add_blog_option( $blog->blog_id, $key, $value );
}
}
}
register_activation_hook( __FILE__, 'firebase_notifications_registration' );

?>
103 changes: 103 additions & 0 deletions wp-content/plugins/firebase-notifications/service.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
class FirebaseNotificationsService {

function __construct(){
$this->read_settings();
}


public function translate_send_notifications( $items ) {
$languages = icl_get_languages();
foreach($items as $item) {
if( $item['title'] == '' and $item['message'] == '' ) {
//autotranslate | at
if( $item['translate'] == 'at' ) {
$translation_service = new TranslationService();
$item['title'] = $translation_service->translate_string($items[ICL_LANGUAGE_CODE]['title'], ICL_LANGUAGE_CODE, $item['lang']);
$item['message'] = $translation_service->translate_string($items[ICL_LANGUAGE_CODE]['message'], ICL_LANGUAGE_CODE, $item['lang']);
// no message | no
} elseif( $item['translate'] == 'no' ) {
continue;
// original language | or
} elseif( $item['translate'] == 'or' ) {
$item['title'] = $items[ICL_LANGUAGE_CODE]['title'];
$item['message'] = $items[ICL_LANGUAGE_CODE]['message'];
}
}
echo $this->send_notification( $item['title'],$item['message'],$item['lang'], $item['group'] );
}
}


private function send_notification( $title, $body, $language, $group ) {
$header = $this->build_header( $this->settings['auth_key'] );
$fields = $this->build_json( $title, $body, $language, $this->settings['blog_id'], $group );
$settings = $this->read_settings();
echo $this->execute_curl( $this->settings['api_url'], $header, $fields );
}


private function read_settings() {
// are network settings enforced?
$this->settings['blog_id'] = get_current_blog_id();
$this->settings['force_network_settings'] = get_site_option( 'fbn_force_network_settings' );
$this->settings['per_blog_topic'] = get_site_option( 'fbn_per_blog_topic' );
// use network settings
if ( $this->settings['force_network_settings'] == '2' ) {
$this->settings['api_url'] = get_site_option('fbn_auth_key');
$this->settings['auth_key'] = get_site_option('fbn_api_url');
}
// network or blog settings
elseif ( $this->settings['force_network_settings'] == '1' ) {
if( get_blog_option( $blog_id, 'fbn_use_network_settings' ) == '1' ) {
$this->settings['api_url'] = get_site_option('fbn_auth_key');
$this->settings['auth_key'] = get_site_option('fbn_api_url');
} else {
$this->settings['auth_key'] = get_blog_option( $blog_id, 'fbn_auth_key' );
$this->settings['api_url'] = get_blog_option( $blog_id, 'fbn_api_url' );
}
}
// blog settings
elseif ( $this->settings['force_network_settings'] == '0' ) {
$this->settings['auth_key'] = get_blog_option( $blog_id, 'fbn_auth_key' );
$this->settings['api_url'] = get_blog_option( $blog_id, 'fbn_api_url' );
}
}


private function execute_curl( $url, $headers, $fields ) {
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );
$result = curl_exec ( $ch );
curl_close ( $ch );
return $result;
}


private function build_json( $title, $body, $language, $blog_id, $group ) {
$fields = array (
'to' => '/topics/' . ($this->settings['per_blog_topic'] == '1' ? (string)$blog_id . "-" . $language . "-" : "") . $group,
'notification' => array (
'title' => $title,
'body' => $body
)
);
return json_encode ( $fields );
}


private function build_header( $authKey ) {
$headers = array (
'Authorization: key=' . $authKey,
'Content-Type: application/json'
);
return $headers;
}

}

?>
Loading

0 comments on commit 5102909

Please sign in to comment.