<?php $onesignal_wp_settings = OneSignal::get_onesignal_settings(); if (array_key_exists('app_id', $_POST)) { $onesignal_wp_settings = OneSignal_Admin::save_config_page($_POST); } ?> <header class="onesignal"> <a href="https://onesignal.com" target="_blank"> <div class="onesignal logo" id="logo-onesignal" style="width: 250px; height: 52px; margin: 0 auto;"> </div> </a> </header> <div class="outer site onesignal container"> <div class="ui site onesignal container" id="content-container"> <div class="ui pointing stackable menu"> <a class="item" data-tab="setup">Setup</a> <a class="active item" data-tab="configuration">Configuration</a> </div> <div class="ui tab borderless shadowless segment" data-tab="setup" style="padding-top: 0; padding-bottom: 0;"> <div class="ui special padded segment" style="padding-top: 0 !important;"> <div class="ui top secondary pointing menu"> <div class="ui grid" style="margin: 0 !important; padding: 0 !important;"> <a class="item" data-tab="setup/0">Overview</a> <a class="item" data-tab="setup/1">Google Keys</a> <a class="item" data-tab="setup/2">Chrome & Firefox Push</a> <a class="item" data-tab="setup/3">OneSignal Keys</a> <a class="item" data-tab="setup/4">Modify Site</a> <a class="item" data-tab="setup/5">Safari Push</a> <a class="item" data-tab="setup/7">Results</a> </div>
public static function send_notification_on_wp_post($new_status, $old_status, $post) { $post_types = array_values(get_post_types(array('public' => true))); $attachment = array_search('attachment', $post_types); if (false !== $attachment) { unset($post_types[$attachment]); } if (empty($post) || !in_array(get_post_type($post), $post_types) || $new_status !== "publish") { return $new_status; } $onesignal_wp_settings = \OneSignal::get_onesignal_settings(); if (isset($_POST['has_onesignal_setting'])) { if (array_key_exists('send_onesignal_notification', $_POST)) { $send_onesignal_notification = $_POST['send_onesignal_notification']; } else { $send_onesignal_notification = false; } } elseif ($old_status !== "publish") { $send_onesignal_notification = $onesignal_wp_settings['notification_on_post_from_plugin']; } if ($send_onesignal_notification === true || $send_onesignal_notification === "true") { $notif_content = html_entity_decode(get_the_title($post->ID), ENT_QUOTES, 'UTF-8'); $fields = array('app_id' => $onesignal_wp_settings['app_id'], 'included_segments' => array('All'), 'isAnyWeb' => true, 'url' => get_permalink($post->ID), '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'])); 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; } return $new_status; }
public static function send_notification_on_wp_post($new_status, $old_status, $post) { onesignal_debug('Calling send_notification_on_wp_post(', $new_status, $old_status, $post); if (empty($post)) { return; } $onesignal_wp_settings = OneSignal::get_onesignal_settings(); $send_onesignal_notification = false; if (isset($_POST['has_onesignal_setting'])) { if (array_key_exists('send_onesignal_notification', $_POST)) { $send_onesignal_notification = $_POST['send_onesignal_notification']; } } elseif ($old_status !== "publish" && $post->post_type === "post") { $send_onesignal_notification = $onesignal_wp_settings['notification_on_post_from_plugin']; } // default to not send notifications for page post type or unpublished posts if ($new_status !== "publish" || $post->post_type == 'page') { $send_onesignal_notification = false; } // apply the notification_pre_send_check filter if it is defined. if (has_filter('onesignal_send_notification_pre_send_check')) { // default to send unless overriden by filter $send_onesignal_notification = apply_filters('onesignal_send_notification_pre_send_check', $send_onesignal_notification, $new_status, $old_status, $post); } onesignal_debug('Sending notification: ', $send_onesignal_notification); if ($send_onesignal_notification === true || $send_onesignal_notification === "true") { $notif_content = html_entity_decode(get_the_title($post->ID), ENT_QUOTES, 'UTF-8'); $site_title = ""; if ($onesignal_wp_settings['default_title'] != "") { $site_title = html_entity_decode($onesignal_wp_settings['default_title'], ENT_HTML401 | ENT_QUOTES, 'UTF-8'); } else { $site_title = html_entity_decode(get_bloginfo('name'), ENT_HTML401 | ENT_QUOTES, 'UTF-8'); } if (isset($onesignal_wp_settings['includedSegments'])) { $included_segments = array_filter(explode(',', $onesignal_wp_settings['includedSegments'])); } if (empty($included_segments)) { $included_segments = array('All'); } if (has_filter('onesignal_send_notification_included_segments')) { $included_segments = apply_filters('onesignal_send_notification_included_segments', $included_segments, $new_status, $old_status, $post); } $fields = array('app_id' => $onesignal_wp_settings['app_id'], 'headings' => array("en" => $site_title), 'included_segments' => $included_segments, 'isAnyWeb' => true, 'url' => get_permalink($post->ID), 'contents' => array("en" => $notif_content)); $post_has_featured_image = has_post_thumbnail($post); $config_use_featured_image_as_icon = $onesignal_wp_settings['showNotificationIconFromPostThumbnail'] == "1"; onesignal_debug('Post has featured image: ', $post_has_featured_image); onesignal_debug('Use featured image as notification icon: ', $config_use_featured_image_as_icon); if ($post_has_featured_image == true && $config_use_featured_image_as_icon) { // get the icon image from wordpress if it exists $post_thumbnail_id = get_post_thumbnail_id($post->ID); $thumbnail_array = wp_get_attachment_image_src($post_thumbnail_id, array(80, 80), true); onesignal_debug('Thumbnail array: ', $thumbnail_array); if (!empty($thumbnail_array)) { $thumbnail = $thumbnail_array[0]; // set the icon image for both chrome and firefox-1 $fields['chrome_web_icon'] = $thumbnail; $fields['firefox_icon'] = $thumbnail; } } $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'])); 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; } }
public static function onesignal_header() { $onesignal_wp_settings = OneSignal::get_onesignal_settings(); if ($onesignal_wp_settings["subdomain"] == "") { if (strpos(ONESIGNAL_PLUGIN_URL, "http://localhost") === false && strpos(ONESIGNAL_PLUGIN_URL, "http://127.0.0.1") === false) { $current_plugin_url = preg_replace("/(http:\\/\\/)/i", "https://", ONESIGNAL_PLUGIN_URL); } else { $current_plugin_url = ONESIGNAL_PLUGIN_URL; } ?> <?php $settings = get_option("OneSignalWPSetting"); $key = 'gcm_sender_id'; if ($settings !== false && array_key_exists($key, $settings)) { $gcm_sender_id = $settings[$key]; } else { $gcm_sender_id = 'WORDPRESS_NO_SENDER_ID_ENTERED'; } ?> <link rel="manifest" href="<?php echo $current_plugin_url . 'sdk_files/manifest.json.php?gcm_sender_id=' . $gcm_sender_id; ?> " /> <?php } ?> <?php /* <script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async></script> */ ?> <?php /* <script src="https://192.168.1.206:3000/dev_sdks/OneSignalSDK.js" async></script> */ ?> <script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async></script> <script> var OneSignal = OneSignal || []; OneSignal.push( function() { 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_icon'] != "") { echo "OneSignal.setDefaultIcon(\"" . html_entity_decode($onesignal_wp_settings['default_icon'], ENT_HTML401 | ENT_QUOTES, 'UTF-8') . "\");\n"; } if ($onesignal_wp_settings['default_url'] != "") { echo "OneSignal.setDefaultNotificationUrl(\"" . html_entity_decode($onesignal_wp_settings['default_url'], ENT_HTML401 | ENT_QUOTES, 'UTF-8') . "\");"; } else { echo "OneSignal.setDefaultNotificationUrl(\"" . html_entity_decode(get_site_url(), ENT_HTML401 | ENT_QUOTES, 'UTF-8') . "\");\n"; } ?> var oneSignal_options = {}; <?php echo "oneSignal_options['wordpress'] = true;\n"; echo "oneSignal_options['appId'] = '" . $onesignal_wp_settings["app_id"] . "';\n"; if ($onesignal_wp_settings["prompt_auto_register"] == "1") { echo "oneSignal_options['autoRegister'] = true;\n"; } else { echo "oneSignal_options['autoRegister'] = false;\n"; } if ($onesignal_wp_settings["send_welcome_notification"] == "1") { echo "oneSignal_options['welcomeNotification'] = { };\n"; echo "oneSignal_options['welcomeNotification']['title'] = \"" . $onesignal_wp_settings["welcome_notification_title"] . "\";\n"; echo "oneSignal_options['welcomeNotification']['message'] = \"" . $onesignal_wp_settings["welcome_notification_message"] . "\";\n"; if ($onesignal_wp_settings["welcome_notification_url"] != "") { echo "oneSignal_options['welcomeNotification']['url'] = \"" . $onesignal_wp_settings["welcome_notification_url"] . "\";\n"; } } else { echo "oneSignal_options['welcomeNotification'] = { };\n"; echo "oneSignal_options['welcomeNotification']['disable'] = true;\n"; } if ($onesignal_wp_settings["subdomain"] != "") { echo "oneSignal_options['subdomainName'] = \"" . $onesignal_wp_settings["subdomain"] . "\";\n"; } else { echo "oneSignal_options['path'] = \"" . $current_plugin_url . "sdk_files/\";\n"; } if (@$onesignal_wp_settings["safari_web_id"]) { echo "oneSignal_options['safari_web_id'] = \"" . $onesignal_wp_settings["safari_web_id"] . "\";\n"; } if ($onesignal_wp_settings["chrome_auto_dismiss_notifications"] == "1") { echo "oneSignal_options['persistNotification'] = false;\n"; } if ($onesignal_wp_settings["subdomain"] != "" || $onesignal_wp_settings["use_modal_prompt"] == "1") { echo "oneSignal_options['promptOptions'] = { };\n"; if (array_key_exists('prompt_customize_enable', $onesignal_wp_settings) && $onesignal_wp_settings["prompt_customize_enable"] == "1") { if ($onesignal_wp_settings["prompt_action_message"] != "") { echo "oneSignal_options['promptOptions']['actionMessage'] = '" . $onesignal_wp_settings["prompt_action_message"] . "';\n"; } if ($onesignal_wp_settings["prompt_example_notification_title_desktop"] != "") { echo "oneSignal_options['promptOptions']['exampleNotificationTitleDesktop'] = '" . $onesignal_wp_settings["prompt_example_notification_title_desktop"] . "';\n"; } if ($onesignal_wp_settings["prompt_example_notification_message_desktop"] != "") { echo "oneSignal_options['promptOptions']['exampleNotificationMessageDesktop'] = '" . $onesignal_wp_settings["prompt_example_notification_message_desktop"] . "';\n"; } if ($onesignal_wp_settings["prompt_example_notification_title_mobile"] != "") { echo "oneSignal_options['promptOptions']['exampleNotificationTitleMobile'] = '" . $onesignal_wp_settings["prompt_example_notification_title_mobile"] . "';\n"; } if ($onesignal_wp_settings["prompt_example_notification_message_mobile"] != "") { echo "oneSignal_options['promptOptions']['exampleNotificationMessageMobile'] = '" . $onesignal_wp_settings["prompt_example_notification_message_mobile"] . "';\n"; } if ($onesignal_wp_settings["prompt_example_notification_caption"] != "") { echo "oneSignal_options['promptOptions']['exampleNotificationCaption'] = '" . $onesignal_wp_settings["prompt_example_notification_caption"] . "';\n"; } if ($onesignal_wp_settings["prompt_accept_button_text"] != "") { echo "oneSignal_options['promptOptions']['acceptButtonText'] = '" . $onesignal_wp_settings["prompt_accept_button_text"] . "';\n"; } if ($onesignal_wp_settings["prompt_cancel_button_text"] != "") { echo "oneSignal_options['promptOptions']['cancelButtonText'] = '" . $onesignal_wp_settings["prompt_cancel_button_text"] . "';\n"; } } if (array_key_exists('prompt_showcredit', $onesignal_wp_settings) && $onesignal_wp_settings["prompt_showcredit"] != "1") { echo "oneSignal_options['promptOptions']['showCredit'] = false;\n"; } } if (array_key_exists('notifyButton_enable', $onesignal_wp_settings) && $onesignal_wp_settings["notifyButton_enable"] == "1") { echo "oneSignal_options['notifyButton'] = { };\n"; echo "oneSignal_options['notifyButton']['enable'] = true;\n"; if (array_key_exists('notifyButton_position', $onesignal_wp_settings) && $onesignal_wp_settings['notifyButton_position'] != "") { echo "oneSignal_options['notifyButton']['position'] = '" . $onesignal_wp_settings["notifyButton_position"] . "';\n"; } if (array_key_exists('notifyButton_theme', $onesignal_wp_settings) && $onesignal_wp_settings['notifyButton_theme'] != "") { echo "oneSignal_options['notifyButton']['theme'] = '" . $onesignal_wp_settings["notifyButton_theme"] . "';\n"; } if (array_key_exists('notifyButton_size', $onesignal_wp_settings) && $onesignal_wp_settings['notifyButton_size'] != "") { echo "oneSignal_options['notifyButton']['size'] = '" . $onesignal_wp_settings["notifyButton_size"] . "';\n"; } if ($onesignal_wp_settings["notifyButton_prenotify"] == "1") { echo "oneSignal_options['notifyButton']['prenotify'] = true;\n"; } else { echo "oneSignal_options['notifyButton']['prenotify'] = false;\n"; } if ($onesignal_wp_settings["use_modal_prompt"] == "1") { echo "oneSignal_options['notifyButton']['modalPrompt'] = true;\n"; } if ($onesignal_wp_settings["notifyButton_showcredit"] == "1") { echo "oneSignal_options['notifyButton']['showCredit'] = true;\n"; } else { echo "oneSignal_options['notifyButton']['showCredit'] = false;\n"; } if (array_key_exists('notifyButton_customize_enable', $onesignal_wp_settings) && $onesignal_wp_settings["notifyButton_customize_enable"] == "1") { echo "oneSignal_options['notifyButton']['text'] = {};\n"; if ($onesignal_wp_settings["notifyButton_message_prenotify"] != "") { echo "oneSignal_options['notifyButton']['text']['message.prenotify'] = '" . $onesignal_wp_settings["notifyButton_message_prenotify"] . "';\n"; } if ($onesignal_wp_settings["notifyButton_tip_state_unsubscribed"] != "") { echo "oneSignal_options['notifyButton']['text']['tip.state.unsubscribed'] = '" . $onesignal_wp_settings["notifyButton_tip_state_unsubscribed"] . "';\n"; } if ($onesignal_wp_settings["notifyButton_tip_state_subscribed"] != "") { echo "oneSignal_options['notifyButton']['text']['tip.state.subscribed'] = '" . $onesignal_wp_settings["notifyButton_tip_state_subscribed"] . "';\n"; } if ($onesignal_wp_settings["notifyButton_tip_state_blocked"] != "") { echo "oneSignal_options['notifyButton']['text']['tip.state.blocked'] = '" . $onesignal_wp_settings["notifyButton_tip_state_blocked"] . "';\n"; } if ($onesignal_wp_settings["notifyButton_message_action_subscribed"] != "") { echo "oneSignal_options['notifyButton']['text']['message.action.subscribed'] = '" . $onesignal_wp_settings["notifyButton_message_action_subscribed"] . "';\n"; } if ($onesignal_wp_settings["notifyButton_message_action_resubscribed"] != "") { echo "oneSignal_options['notifyButton']['text']['message.action.resubscribed'] = '" . $onesignal_wp_settings["notifyButton_message_action_resubscribed"] . "';\n"; } if ($onesignal_wp_settings["notifyButton_message_action_unsubscribed"] != "") { echo "oneSignal_options['notifyButton']['text']['message.action.unsubscribed'] = '" . $onesignal_wp_settings["notifyButton_message_action_unsubscribed"] . "';\n"; } if ($onesignal_wp_settings["notifyButton_dialog_main_title"] != "") { echo "oneSignal_options['notifyButton']['text']['dialog.main.title'] = '" . $onesignal_wp_settings["notifyButton_dialog_main_title"] . "';\n"; } if ($onesignal_wp_settings["notifyButton_dialog_main_button_subscribe"] != "") { echo "oneSignal_options['notifyButton']['text']['dialog.main.button.subscribe'] = '" . $onesignal_wp_settings["notifyButton_dialog_main_button_subscribe"] . "';\n"; } if ($onesignal_wp_settings["notifyButton_dialog_main_button_unsubscribe"] != "") { echo "oneSignal_options['notifyButton']['text']['dialog.main.button.unsubscribe'] = '" . $onesignal_wp_settings["notifyButton_dialog_main_button_unsubscribe"] . "';\n"; } if ($onesignal_wp_settings["notifyButton_dialog_blocked_title"] != "") { echo "oneSignal_options['notifyButton']['text']['dialog.blocked.title'] = '" . $onesignal_wp_settings["notifyButton_dialog_blocked_title"] . "';\n"; } if ($onesignal_wp_settings["notifyButton_dialog_blocked_message"] != "") { echo "oneSignal_options['notifyButton']['text']['dialog.blocked.message'] = '" . $onesignal_wp_settings["notifyButton_dialog_blocked_message"] . "';\n"; } } if (array_key_exists('notifyButton_customize_colors_enable', $onesignal_wp_settings) && $onesignal_wp_settings["notifyButton_customize_colors_enable"] == "1") { echo "oneSignal_options['notifyButton']['colors'] = {};\n"; if ($onesignal_wp_settings["notifyButton_color_background"] != "") { echo "oneSignal_options['notifyButton']['colors']['circle.background'] = '" . $onesignal_wp_settings["notifyButton_color_background"] . "';\n"; } if ($onesignal_wp_settings["notifyButton_color_foreground"] != "") { echo "oneSignal_options['notifyButton']['colors']['circle.foreground'] = '" . $onesignal_wp_settings["notifyButton_color_foreground"] . "';\n"; } if ($onesignal_wp_settings["notifyButton_color_badge_background"] != "") { echo "oneSignal_options['notifyButton']['colors']['badge.background'] = '" . $onesignal_wp_settings["notifyButton_color_badge_background"] . "';\n"; } if ($onesignal_wp_settings["notifyButton_color_badge_foreground"] != "") { echo "oneSignal_options['notifyButton']['colors']['badge.foreground'] = '" . $onesignal_wp_settings["notifyButton_color_badge_foreground"] . "';\n"; } if ($onesignal_wp_settings["notifyButton_color_badge_border"] != "") { echo "oneSignal_options['notifyButton']['colors']['badge.bordercolor'] = '" . $onesignal_wp_settings["notifyButton_color_badge_border"] . "';\n"; } if ($onesignal_wp_settings["notifyButton_color_pulse"] != "") { echo "oneSignal_options['notifyButton']['colors']['pulse.color'] = '" . $onesignal_wp_settings["notifyButton_color_pulse"] . "';\n"; } if ($onesignal_wp_settings["notifyButton_color_popup_button_background"] != "") { echo "oneSignal_options['notifyButton']['colors']['dialog.button.background'] = '" . $onesignal_wp_settings["notifyButton_color_popup_button_background"] . "';\n"; } if ($onesignal_wp_settings["notifyButton_color_popup_button_background_hover"] != "") { echo "oneSignal_options['notifyButton']['colors']['dialog.button.background.hovering'] = '" . $onesignal_wp_settings["notifyButton_color_popup_button_background_hover"] . "';\n"; } if ($onesignal_wp_settings["notifyButton_color_popup_button_background_active"] != "") { echo "oneSignal_options['notifyButton']['colors']['dialog.button.background.active'] = '" . $onesignal_wp_settings["notifyButton_color_popup_button_background_active"] . "';\n"; } if ($onesignal_wp_settings["notifyButton_color_popup_button_color"] != "") { echo "oneSignal_options['notifyButton']['colors']['dialog.button.foreground'] = '" . $onesignal_wp_settings["notifyButton_color_popup_button_color"] . "';\n"; } } if (array_key_exists('notifyButton_customize_offset_enable', $onesignal_wp_settings) && $onesignal_wp_settings["notifyButton_customize_offset_enable"] == "1") { echo "oneSignal_options['notifyButton']['offset'] = {};\n"; if ($onesignal_wp_settings["notifyButton_offset_bottom"] != "") { echo "oneSignal_options['notifyButton']['offset']['bottom'] = '" . $onesignal_wp_settings["notifyButton_offset_bottom"] . "';\n"; } if ($onesignal_wp_settings["notifyButton_offset_left"] != "") { echo "oneSignal_options['notifyButton']['offset']['left'] = '" . $onesignal_wp_settings["notifyButton_offset_left"] . "';\n"; } if ($onesignal_wp_settings["notifyButton_offset_right"] != "") { echo "oneSignal_options['notifyButton']['offset']['right'] = '" . $onesignal_wp_settings["notifyButton_offset_right"] . "';\n"; } } } $subscriber_tags = null; $subscriber_tags_json = null; if (array_key_exists('subscriberTags', $onesignal_wp_settings) && $onesignal_wp_settings['subscriberTags'] != "") { $subscriber_tag_string = $onesignal_wp_settings['subscriberTags']; $tagValuePairArray = array_filter(explode(',', $subscriber_tag_string)); foreach ($tagValuePairArray as $tvp) { $tag_value = explode(':', $tvp); if (count($tag_value) == 2) { $subscriber_tags[$tag_value[0]] = $tag_value[1]; } } } if (has_filter('onesignal_subscriber_tags')) { $subscriber_tags = apply_filters('onesignal_subscriber_tags', $subscriber_tags); } if (!empty($subscriber_tags)) { $subscriber_tags_json = json_encode($subscriber_tags); } ?> OneSignal.init(oneSignal_options); }); <?php if (!empty($subscriber_tags_json)) { ?> OneSignal.push(["sendTags", <?php echo $subscriber_tags_json; ?> , function(tagsSent) {}]); <?php } ?> function documentInitOneSignal() { var oneSignal_elements = document.getElementsByClassName("OneSignal-prompt"); <?php if ($onesignal_wp_settings["use_modal_prompt"] == "1") { echo "var oneSignalLinkClickHandler = function(event) { OneSignal.push(['registerForPushNotifications', {modalPrompt: true}]); event.preventDefault(); };"; } else { echo "var oneSignalLinkClickHandler = function(event) { OneSignal.push(['registerForPushNotifications']); event.preventDefault(); };"; } ?> for(var i = 0; i < oneSignal_elements.length; i++) oneSignal_elements[i].addEventListener('click', oneSignalLinkClickHandler, false); } if (document.readyState === 'complete') { documentInitOneSignal(); } else { window.addEventListener("load", function(event){ documentInitOneSignal(); }); } </script> <?php }