From 7578f77a0f8b0a960f07b32aa10044d4c7904ec7 Mon Sep 17 00:00:00 2001 From: Davis Shaver Date: Sat, 30 Nov 2019 09:16:19 -0500 Subject: [PATCH 01/12] 217: Wrap signup prompt in textwidget container --- onesignal-widget.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/onesignal-widget.php b/onesignal-widget.php index 296e8d7..e45ca96 100644 --- a/onesignal-widget.php +++ b/onesignal-widget.php @@ -36,9 +36,11 @@ function widget($args, $instance) { if ( ! empty( $instance['title'] ) ) { echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ). $args['after_title']; } - if ( ! empty( $instance['text'] ) ) { + echo '
'; + if ( ! empty( $instance['text'] ) ) { echo '' . $instance['text'] . ''; } + echo '
'; echo $args['after_widget']; } } From 4b5180f5e3c3560ce627492541d82cd9c450e0e1 Mon Sep 17 00:00:00 2001 From: Davis Shaver Date: Sat, 30 Nov 2019 09:16:53 -0500 Subject: [PATCH 02/12] 217: Hide OneSignal signup prompt by default --- onesignal-widget.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/onesignal-widget.php b/onesignal-widget.php index e45ca96..94fc38f 100644 --- a/onesignal-widget.php +++ b/onesignal-widget.php @@ -38,7 +38,7 @@ function widget($args, $instance) { } echo '
'; if ( ! empty( $instance['text'] ) ) { - echo '' . $instance['text'] . ''; + echo ''; } echo '
'; echo $args['after_widget']; From ea8b96b67b4157dc5cc2e34dd387d22fcbefbb0c Mon Sep 17 00:00:00 2001 From: Davis Shaver Date: Sat, 30 Nov 2019 09:17:15 -0500 Subject: [PATCH 03/12] 217: Add no JS text to signup prompt --- onesignal-widget.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/onesignal-widget.php b/onesignal-widget.php index 94fc38f..809c5cf 100644 --- a/onesignal-widget.php +++ b/onesignal-widget.php @@ -38,6 +38,10 @@ function widget($args, $instance) { } echo '
'; if ( ! empty( $instance['text'] ) ) { + printf( + '
%s
', + __( 'Apologies, Javascript is required for this feature.' ) + ); echo ''; } echo '
'; From 25670dd3612d4a543bcdf8af48b53f914a12bd1e Mon Sep 17 00:00:00 2001 From: Davis Shaver Date: Sat, 30 Nov 2019 09:32:50 -0500 Subject: [PATCH 04/12] 217: More generic no-js message --- onesignal-widget.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/onesignal-widget.php b/onesignal-widget.php index 809c5cf..4e0f60a 100644 --- a/onesignal-widget.php +++ b/onesignal-widget.php @@ -40,7 +40,7 @@ function widget($args, $instance) { if ( ! empty( $instance['text'] ) ) { printf( '
%s
', - __( 'Apologies, Javascript is required for this feature.' ) + __( 'Loading notification status...' ) ); echo ''; } From 42f18ee29e6acb388eafc3b2ea3ce44ff01c3648 Mon Sep 17 00:00:00 2001 From: Davis Shaver Date: Sat, 30 Nov 2019 09:38:41 -0500 Subject: [PATCH 05/12] 217: Toggle widget messages during init --- onesignal-public.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/onesignal-public.php b/onesignal-public.php index a9d2094..a159ced 100644 --- a/onesignal-public.php +++ b/onesignal-public.php @@ -362,6 +362,7 @@ public static function onesignal_header() function documentInitOneSignal() { var oneSignal_elements = document.getElementsByClassName("OneSignal-prompt"); + var oneSignal_no_js_elements = document.getElementsByClassName("OneSignal-prompt-no-js"); - for(var i = 0; i < oneSignal_elements.length; i++) + for(var i = 0; i < oneSignal_elements.length; i++) { + oneSignal_elements[i].style.display = 'inherit'; oneSignal_elements[i].addEventListener('click', oneSignalLinkClickHandler, false); + } + for(var i = 0; i < oneSignal_no_js_elements.length; i++) { + oneSignal_no_js_elements[i].style.display = 'none'; + } } - if (document.readyState === 'complete') { documentInitOneSignal(); } From 49c64d6105a74a93b3a550059fe45163c75d48cf Mon Sep 17 00:00:00 2001 From: Davis Shaver Date: Sat, 30 Nov 2019 09:45:07 -0500 Subject: [PATCH 06/12] 217: Rip out legacy widget init --- onesignal-public.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/onesignal-public.php b/onesignal-public.php index a159ced..a58665c 100644 --- a/onesignal-public.php +++ b/onesignal-public.php @@ -364,15 +364,8 @@ function documentInitOneSignal() { var oneSignal_elements = document.getElementsByClassName("OneSignal-prompt"); var oneSignal_no_js_elements = document.getElementsByClassName("OneSignal-prompt-no-js"); - for(var i = 0; i < oneSignal_elements.length; i++) { oneSignal_elements[i].style.display = 'inherit'; - oneSignal_elements[i].addEventListener('click', oneSignalLinkClickHandler, false); } for(var i = 0; i < oneSignal_no_js_elements.length; i++) { oneSignal_no_js_elements[i].style.display = 'none'; From 8e47c7a37b6c44e0b6812307e854af70db19e1e9 Mon Sep 17 00:00:00 2001 From: Davis Shaver Date: Sat, 30 Nov 2019 09:47:28 -0500 Subject: [PATCH 07/12] 217: Cleanup spacing in widget file --- onesignal-widget.php | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/onesignal-widget.php b/onesignal-widget.php index 4e0f60a..f348b77 100644 --- a/onesignal-widget.php +++ b/onesignal-widget.php @@ -3,30 +3,29 @@ defined( 'ABSPATH' ) or die('This page may not be accessed directly.'); class OneSignalWidget extends WP_Widget { - + function __construct() { - parent::__construct('OneSignalWidget', 'OneSignal', array( 'description' => 'Subscribe to notifications')); + parent::__construct('OneSignalWidget', 'OneSignal', array( 'description' => 'Subscribe to notifications')); } - - // Admin editor + + // Admin editor function form($instance) { - $title = ! empty( $instance['title'] ) ? $instance['title'] : 'Follow'; - $text = ! empty( $instance['text'] ) ? $instance['text'] : 'Subscribe to notifications'; + $title = ! empty( $instance['title'] ) ? $instance['title'] : 'Follow'; + $text = ! empty( $instance['text'] ) ? $instance['text'] : 'Subscribe to notifications'; ?>

- +

Date: Sun, 1 Dec 2019 12:05:02 -0500 Subject: [PATCH 08/12] 217: Enhance widget JS based on docs --- onesignal-public.php | 79 +++++++++++++++++++++++++++++++++++++++----- onesignal-widget.php | 49 +++++++++++++++++++-------- 2 files changed, 106 insertions(+), 22 deletions(-) diff --git a/onesignal-public.php b/onesignal-public.php index a58665c..903c9dc 100644 --- a/onesignal-public.php +++ b/onesignal-public.php @@ -360,17 +360,78 @@ public static function onesignal_header() } ?> }); - function documentInitOneSignal() { - var oneSignal_elements = document.getElementsByClassName("OneSignal-prompt"); - var oneSignal_no_js_elements = document.getElementsByClassName("OneSignal-prompt-no-js"); + function getSubscriptionState() { + return Promise.all([ + OneSignal.isPushNotificationsEnabled(), + OneSignal.isOptedOut() + ]).then(function(result) { + var isPushEnabled = result[0]; + var isOptedOut = result[1]; + return { + isPushEnabled: isPushEnabled, + isOptedOut: isOptedOut + }; + }); + } - for(var i = 0; i < oneSignal_elements.length; i++) { - oneSignal_elements[i].style.display = 'inherit'; - } - for(var i = 0; i < oneSignal_no_js_elements.length; i++) { - oneSignal_no_js_elements[i].style.display = 'none'; - } + function onManageWebPushSubscriptionButtonClicked(event) { + getSubscriptionState().then(function(state) { + if (state.isPushEnabled) { + OneSignal.setSubscription(false); + } else { + if (state.isOptedOut) { + OneSignal.setSubscription(true); + } else { + OneSignal.registerForPushNotifications(); + } + } + }); + event.preventDefault(); } + + function updateMangeWebPushSubscriptionButton(buttonSelector) { + getSubscriptionState().then(function(state) { + var elements = document.querySelectorAll(buttonSelector); + if (elements === null) { + return; + } + for(var i = 0; i < elements.length; i++) { + var buttonText = !state.isPushEnabled || state.isOptedOut ? + elements[i].dataset.onesignalSubscribe : + elements[i].dataset.onesignalUnsubscribe; + elements[i].removeEventListener('click', onManageWebPushSubscriptionButtonClicked); + elements[i].addEventListener('click', onManageWebPushSubscriptionButtonClicked); + elements[i].textContent = buttonText; + } + }); + } + + function documentInitOneSignal() { + OneSignal.push(function() { + var buttonSelector = '.OneSignal-prompt'; + var noJsSelector = '.OneSignal-prompt-no-js'; + + if (!OneSignal.isPushNotificationsSupported()) { + return; + } + + updateMangeWebPushSubscriptionButton(buttonSelector); + + var oneSignal_elements = document.querySelectorAll(buttonSelector); + var oneSignal_no_js_elements = document.querySelectorAll(noJsSelector); + for(var i = 0; i < oneSignal_elements.length; i++) { + oneSignal_elements[i].style.display = 'inherit'; + } + for(var i = 0; i < oneSignal_no_js_elements.length; i++) { + oneSignal_no_js_elements[i].style.display = 'none'; + } + + OneSignal.on("subscriptionChange", function(isSubscribed) { + updateMangeWebPushSubscriptionButton(buttonSelector); + }); + }); + } + if (document.readyState === 'complete') { documentInitOneSignal(); } diff --git a/onesignal-widget.php b/onesignal-widget.php index f348b77..162111d 100644 --- a/onesignal-widget.php +++ b/onesignal-widget.php @@ -4,45 +4,68 @@ class OneSignalWidget extends WP_Widget { + /** + * Default strings for widget. + * + * @var array + */ + public $default_strings = array(); + function __construct() { parent::__construct('OneSignalWidget', 'OneSignal', array( 'description' => 'Subscribe to notifications')); + $this->default_strings = array( + 'title' => __( 'Follow' ), + 'text' => __( 'Subscribe to notifications' ), + 'unsubscribe' => __( 'Unsubscribe from notifications' ), + ); } // Admin editor function form($instance) { - $title = ! empty( $instance['title'] ) ? $instance['title'] : 'Follow'; - $text = ! empty( $instance['text'] ) ? $instance['text'] : 'Subscribe to notifications'; + $title = ! empty( $instance['title'] ) ? $instance['title'] : $this->default_strings['title']; + $text = ! empty( $instance['text'] ) ? $instance['text'] : $this->default_strings['subscribe']; + $unsub_text = ! empty( $instance['unsub-text'] ) ? $instance['unsub-text'] : $this->default_strings['unsubscribe']; ?>

- + + +

default_strings + ); echo $args['before_widget']; if ( ! empty( $instance['title'] ) ) { - echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ). $args['after_title']; + echo $args['before_title'] . apply_filters( 'widget_title', $strings['title'] ). $args['after_title']; } echo '
'; - if ( ! empty( $instance['text'] ) ) { - printf( - '
%s
', - __( 'Loading notification status...' ) - ); - echo ''; - } + printf( + '
%s
', + __( 'Loading notification status...' ) + ); + $pattern = ''; + printf( + $pattern, + esc_attr( $strings['text'] ), + esc_attr( $strings['unsubscribe'] ) + ); echo '
'; echo $args['after_widget']; } From 727a946ddcdad18dc26a55ca74af4cebdc1b5060 Mon Sep 17 00:00:00 2001 From: Davis Shaver Date: Sun, 1 Dec 2019 12:19:27 -0500 Subject: [PATCH 09/12] 217: Respect modal prompt setting --- onesignal-public.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/onesignal-public.php b/onesignal-public.php index 903c9dc..4a422c7 100644 --- a/onesignal-public.php +++ b/onesignal-public.php @@ -382,7 +382,12 @@ function onManageWebPushSubscriptionButtonClicked(event) { if (state.isOptedOut) { OneSignal.setSubscription(true); } else { - OneSignal.registerForPushNotifications(); + } } }); From 0d04086a80ba3c7c643f86a2870666ba58627c5a Mon Sep 17 00:00:00 2001 From: Davis Shaver Date: Sun, 1 Dec 2019 12:42:48 -0500 Subject: [PATCH 10/12] 217: Fix default string ref for subscription text --- onesignal-widget.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/onesignal-widget.php b/onesignal-widget.php index 162111d..f870ae0 100644 --- a/onesignal-widget.php +++ b/onesignal-widget.php @@ -23,7 +23,7 @@ function __construct() { // Admin editor function form($instance) { $title = ! empty( $instance['title'] ) ? $instance['title'] : $this->default_strings['title']; - $text = ! empty( $instance['text'] ) ? $instance['text'] : $this->default_strings['subscribe']; + $text = ! empty( $instance['text'] ) ? $instance['text'] : $this->default_strings['text']; $unsub_text = ! empty( $instance['unsub-text'] ) ? $instance['unsub-text'] : $this->default_strings['unsubscribe']; ?>

From 9fa5d3d105be240bcd71dffebbe131c751ec5e44 Mon Sep 17 00:00:00 2001 From: Davis Shaver Date: Sat, 28 Dec 2019 11:53:18 -0500 Subject: [PATCH 11/12] 217: Add "status" loading to prevent status flash --- onesignal-widget.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/onesignal-widget.php b/onesignal-widget.php index f870ae0..a04bce9 100644 --- a/onesignal-widget.php +++ b/onesignal-widget.php @@ -16,15 +16,15 @@ function __construct() { $this->default_strings = array( 'title' => __( 'Follow' ), 'text' => __( 'Subscribe to notifications' ), - 'unsubscribe' => __( 'Unsubscribe from notifications' ), + 'unsub-text' => __( 'Unsubscribe from notifications' ), ); } // Admin editor function form($instance) { - $title = ! empty( $instance['title'] ) ? $instance['title'] : $this->default_strings['title']; - $text = ! empty( $instance['text'] ) ? $instance['text'] : $this->default_strings['text']; - $unsub_text = ! empty( $instance['unsub-text'] ) ? $instance['unsub-text'] : $this->default_strings['unsubscribe']; + $title = ! empty( $instance['title'] ) ? $instance['title'] : $this->default_strings['title']; + $text = ! empty( $instance['text'] ) ? $instance['text'] : $this->default_strings['text']; + $unsub_text = ! empty( $instance['unsub-text'] ) ? $instance['unsub-text'] : $this->default_strings['unsub-text']; ?>

@@ -51,6 +51,8 @@ function widget($args, $instance) { $instance, $this->default_strings ); + $loading_service = __( 'Loading notification service...' ); + $loading_status = __( 'Loading notification status...' ); echo $args['before_widget']; if ( ! empty( $instance['title'] ) ) { echo $args['before_title'] . apply_filters( 'widget_title', $strings['title'] ). $args['after_title']; @@ -58,13 +60,14 @@ function widget($args, $instance) { echo '

'; printf( '
%s
', - __( 'Loading notification status...' ) + esc_html( $loading_service ) ); - $pattern = ''; + $pattern = ''; printf( $pattern, esc_attr( $strings['text'] ), - esc_attr( $strings['unsubscribe'] ) + esc_attr( $strings['unsub-text'] ), + esc_html( $loading_status ) ); echo '
'; echo $args['after_widget']; From 9d523628a8c6acb2a9f4c8d293a191d729fde8a1 Mon Sep 17 00:00:00 2001 From: Davis Shaver Date: Sun, 19 Jan 2020 05:53:27 -0500 Subject: [PATCH 12/12] 217: Align array for default strings --- onesignal-widget.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/onesignal-widget.php b/onesignal-widget.php index 065716c..9771c18 100644 --- a/onesignal-widget.php +++ b/onesignal-widget.php @@ -16,7 +16,7 @@ function __construct() { $this->default_strings = array( 'title' => __( 'Follow' ), 'text' => __( 'Subscribe to notifications' ), - 'unsub-text' => __( 'Unsubscribe from notifications' ), + 'unsub-text' => __( 'Unsubscribe from notifications' ), ); }