Skip to content

Commit

Permalink
OneSignal WordPress Plugin 1.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
jkasten2 committed May 15, 2015
0 parents commit 1b59399
Show file tree
Hide file tree
Showing 36 changed files with 1,187 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
OneSignal WordPress Push Notification Plugin
====================================

OneSignal is a free push notification service for web and mobile apps. This plugin makes it easy to integrate your WordPress site with OneSignal Push Notifications.

- Download and install from the [WordPress Plugin Directory](https://wordpress.org/plugins/onesignal-free-web-push-notifications/)
Binary file added images/SampleNotification.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
184 changes: 184 additions & 0 deletions onesignal-admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
<?php

class OneSignal_Admin {
public function __construct() {
}


public static function init() {
$onesignal = new self();
if(current_user_can('update_plugins')) {
add_action( 'admin_menu', array(__CLASS__,'add_admin_page') );
add_action( 'add_meta_boxes_post', array( __CLASS__, 'add_onesignal_post_options' ) );
add_action( 'save_post', array( __CLASS__, 'on_post_save' ) );
}

// Outside is_admin() to catch posts that go from future to published in the background.
add_action( 'transition_post_status', array( __CLASS__, 'notification_on_blog_post' ), 10, 3 );

return $onesignal;
}

public static function add_onesignal_post_options() {

add_meta_box(
'onesignal_notif_on_post',
'OneSignal',
array( __CLASS__, 'onesignal_notif_on_post_html_view' ),
'post',
'side',
'high'
);
}

public static function on_post_save($post_id) {
if ( $_POST['send_onesignal_notification'] === "true" && get_post_status($post_id) !== "publish" ) {
update_post_meta($post_id, 'send_onesignal_notification', "true");
}
else {
update_post_meta($post_id, 'send_onesignal_notification', "false");
}
}

public static function onesignal_notif_on_post_html_view($post) {
$onesignal_wp_settings = OneSignal::get_onesignal_settings();
?>
<input type="checkbox" name="send_onesignal_notification" value="true" <?php if ($onesignal_wp_settings['notification_on_post'] && $post->post_status != "publish") { echo checked; } ?>></input>
<label> <?php if ($post->post_status == "publish") { echo "Send notification on update"; } else { echo "Send notification on publish"; } ?></label>
<?php
}

public static function save_config_page($config) {
if (!current_user_can('update_plugins'))
return;

$sdk_dir = plugin_dir_path( __FILE__ ) . 'sdk_files/';
$onesignal_wp_settings = OneSignal::get_onesignal_settings();
$new_app_id = $config['app_id'];

// Validate the UUID
if( preg_match('/([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})/', $new_app_id, $m))
$onesignal_wp_settings['app_id'] = $new_app_id;

if (is_numeric($config['gcm_sender_id'])) {
$onesignal_wp_settings['gcm_sender_id'] = $config['gcm_sender_id'];
$manifest_json = "{\n"
. " \"start_url\": \"/\",\n"
. " \"gcm_sender_id\": \"" . $onesignal_wp_settings['gcm_sender_id'] . "\",\n"
. " \"gcm_user_visible_only\": true\n"
. "}";
file_put_contents($sdk_dir . 'manifest.json', $manifest_json);
}

if ($config['gcm_sender_id']) {
$onesignal_wp_settings['subdomain'] = $config['subdomain'];
}

if ($config['auto_register'] == "true") {
$onesignal_wp_settings['auto_register'] = true;
}
else {
$onesignal_wp_settings['auto_register'] = false;
}

if ($config['notification_on_post'] == "true") {
$onesignal_wp_settings['notification_on_post'] = true;
}
else {
$onesignal_wp_settings['notification_on_post'] = false;
}

$onesignal_wp_settings['default_title'] = $config['default_title'];
$onesignal_wp_settings['default_icon'] = $config['default_icon'];
$onesignal_wp_settings['default_url'] = $config['default_url'];

$onesignal_wp_settings['app_rest_api_key'] = $config['app_rest_api_key'];

OneSignal::save_onesignal_settings($onesignal_wp_settings);

return $onesignal_wp_settings;
}

public static function add_admin_page() {
$OneSignal_menu = add_menu_page('OneSignal Push',
'OneSignal Push',
'manage_options',
'onesignal-push',
array(__CLASS__, 'admin_menu'),
plugin_dir_url( __FILE__ ) .'views/images/menu_icon.png');

add_action( 'load-' . $OneSignal_menu, array(__CLASS__, 'admin_custom_load') );
}

public static function admin_menu() {
require_once( plugin_dir_path( __FILE__ ) . '/views/config.php' );
}

public static function admin_custom_load() {
add_action( 'admin_enqueue_scripts', array(__CLASS__, 'admin_custom_scripts') );
}

public static function admin_custom_scripts() {
wp_enqueue_style( 'bootstrap.min', plugin_dir_url( __FILE__ ) . 'views/css/bootstrap.min.css');
wp_enqueue_style( 'style_onesignal', plugin_dir_url( __FILE__ ) . 'views/css/style_onesignal.css');

wp_enqueue_script( 'bootstrap.min', plugin_dir_url( __FILE__ ) . 'views/javascript/bootstrap.min.js');
wp_enqueue_script( 'settings', plugin_dir_url( __FILE__ ) . 'views/javascript/settings.js');

wp_enqueue_script( 'intercom', plugin_dir_url( __FILE__ ) . 'views/javascript/intercom.js');

}

public static function notification_on_blog_post( $new_status, $old_status, $post ) {
if (empty( $post )) {
return;
}

$onesignal_wp_settings = OneSignal::get_onesignal_settings();

if (isset($_POST['send_onesignal_notification'])) {
$send_onesignal_notification = $_POST['send_onesignal_notification'];
}
else {
$send_onesignal_notification = get_post_meta( $post->ID, 'send_onesignal_notification', true );
}

if ($send_onesignal_notification === "true") {
if ( get_post_type( $post ) !== 'post' || $post->post_status !== "publish") {
return;
}

update_post_meta($post->ID, 'send_onesignal_notification', "false");

if (empty( $custom_headline ) ) {
$notif_content = get_the_title( $post->ID );
} else {
$notif_content = $custom_headline;
}

$fields = array(
'app_id' => $onesignal_wp_settings['app_id'],
'included_segments' => array('All'),
'isChromeWeb' => true,
'contents' => array("en" => $notif_content)
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
'Authorization: Basic ' . $onesignal_wp_settings['app_rest_api_key'])); // TODO: Get auth key from settings
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

$response = curl_exec($ch);
curl_close($ch);

return $response;
}
}
}

?>
64 changes: 64 additions & 0 deletions onesignal-public.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

class OneSignal_Public {

public function __construct() {}

public static function init() {
define( 'ONESIGNAL_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
add_action( 'wp_head', array( __CLASS__, 'onesignal_header' ), 1 );
}

public static function onesignal_header() {
$onesignal_wp_settings = OneSignal::get_onesignal_settings();
?>
<script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async></script>
<?php if ($onesignal_wp_settings["subdomain"] == "") { ?>
<link rel="manifest" href="<?php echo( ONESIGNAL_PLUGIN_URL . 'sdk_files/manifest.json' ) ?>" />
<?php } ?>
<script>
var OneSignal = OneSignal || [];

function initOneSignal() {
OneSignal.SERVICE_WORKER_UPDATER_PATH = "OneSignalSDKUpdaterWorker.js.php";
OneSignal.SERVICE_WORKER_PATH = "OneSignalSDKWorker.js.php";
OneSignal.SERVICE_WORKER_PARAM = { scope: '/' };

<?php if ($onesignal_wp_settings['default_title'] != "") {
echo "OneSignal.setDefaultTitle(\"" . $onesignal_wp_settings['default_title'] . "\");\n";
}
else {
echo "OneSignal.setDefaultTitle(\"" . get_bloginfo( 'name' ) . "\");\n";
}
?>
<?php if ($onesignal_wp_settings['default_icon'] != "") {
echo "OneSignal.setDefaultIcon(\"" . $onesignal_wp_settings['default_icon'] . "\");\n";
} ?>
<?php
if ($onesignal_wp_settings['default_url'] != "") {
echo "OneSignal.setDefaultNotificationUrl(\"" . $onesignal_wp_settings['default_url'] . "\");";
}
else {
echo "OneSignal.setDefaultNotificationUrl(\"" . get_site_url() . "\");";
}
?>

OneSignal.init({appId: "<?php echo $onesignal_wp_settings["app_id"] ?>",
<?php if ($onesignal_wp_settings["subdomain"] != "") { echo "subdomainName: \"" . $onesignal_wp_settings["subdomain"] . "\",\n"; } ?>
path: "<?php echo ONESIGNAL_PLUGIN_URL . 'sdk_files' ?>/"});
}

window.addEventListener("load", function(event){
OneSignal.push(initOneSignal);

var oneSignal_elements = document.getElementsByClassName("OneSignal-prompt");
var oneSignalLinkClickHandler = function(event) { OneSignal.push(['registerForPushNotifications', {modalPrompt: true}]); event.preventDefault(); };
for(var i = 0; i < oneSignal_elements.length; i++)
oneSignal_elements[i].addEventListener('click', oneSignalLinkClickHandler, false);
});
</script>

<?php
}
}
?>
31 changes: 31 additions & 0 deletions onesignal-settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
class OneSignal {
public static function get_onesignal_settings() {
if (!defined($onesignal_wp_settings)) {
$onesignal_wp_settings = get_option("OneSignalWPSetting");
if (empty( $onesignal_wp_settings )) {
$onesignal_wp_settings = array(
'app_id' => '',
'gcm_sender_id' => '',
'auto_register' => true,
'notification_on_post' => true,
'use_http' => false,
'subdomain' => "",
'origin' => "",
'default_title' => "",
'default_icon' => "",
'default_url' => "",
'app_rest_api_key' => ""
);
}
}

return $onesignal_wp_settings;
}

public static function save_onesignal_settings($settings) {
$onesignal_wp_settings = $settings;
update_option("OneSignalWPSetting", $onesignal_wp_settings);
}
}
?>
46 changes: 46 additions & 0 deletions onesignal-widget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

class OneSignalWidget extends WP_Widget {

function __construct() {
parent::__construct('OneSignalWidget', 'OneSignal', array( 'description' => 'Subscribe to notifications'));
}

// Admin editor
function form($instance) {
$title = ! empty( $instance['title'] ) ? $instance['title'] : 'Follow';
$text = ! empty( $instance['text'] ) ? $instance['text'] : 'Subscribe to notifications';
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
<label for="<?php echo $this->get_field_id( 'text' ); ?>"><?php _e( 'Body:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'text' ); ?>" name="<?php echo $this->get_field_name( 'text' ); ?>" type="text" value="<?php echo esc_attr( $text ); ?>">
</p>
<?php
}

function update($new_instance, $old_instance) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
$instance['text'] = ( ! empty( $new_instance['text'] ) ) ? strip_tags( $new_instance['text'] ) : '';

return $instance;
}

// Public display
function widget($args, $instance) {
echo $args['before_widget'];
if ( ! empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ). $args['after_title'];
}
if ( ! empty( $instance['text'] ) ) {
echo '<a href="#" class="OneSignal-prompt">' . $instance['text'] . '</a>';
}
echo $args['after_widget'];
}
}

add_action('widgets_init', create_function('', 'return register_widget("OneSignalWidget");'));

?>
22 changes: 22 additions & 0 deletions onesignal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Plugin Name: OneSignal Push Notifications
* Plugin URI: https://onesignal.com/
* Description:
* Version: 1.0.3
* Author: OneSignal
* Author URI: https://onesignal.com
* License: MIT
*/

define( 'ONESIGNAL_PLUGIN_URL', plugin_dir_url( __FILE__ ) );

require_once( plugin_dir_path( __FILE__ ) . 'onesignal-admin.php' );
require_once( plugin_dir_path( __FILE__ ) . 'onesignal-public.php' );
require_once( plugin_dir_path( __FILE__ ) . 'onesignal-settings.php' );
require_once( plugin_dir_path( __FILE__ ) . 'onesignal-widget.php' );

add_action( 'init', array( 'OneSignal_Admin', 'init' ) );
add_action( 'init', array( 'OneSignal_Public', 'init' ) );

?>
Loading

0 comments on commit 1b59399

Please sign in to comment.