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 AMP Support #276

Merged
merged 3 commits into from
Feb 25, 2022
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
83 changes: 83 additions & 0 deletions css/onsignal-amp-style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* The file contains CSS for Subscribe and Unsubscribe button on AMP endpoints which will be enqued only at AMP endpoints.
*
/*
Created on : Jul 14, 2021, 11:57:12 PM
Author : milind
*/

amp-web-push-widget button.subscribe.subscribe, amp-web-push-widget button.unsubscribe {
margin: 0;
padding: 0;
border: 0;
cursor: pointer;
outline: none;
position: fixed;
bottom: 20px;
right: 20px;
z-index: 999;
background-color: transparent;
height: 48px;
width: 48px;
}

amp-web-push-widget button.unsubscribe{
opacity: 0.5;
}

amp-web-push-widget button.unsubscribe:hover {
opacity: 1;
}

amp-web-push-widget button.subscribe svg.onesignal-bell-svg, amp-web-push-widget button.unsubscribe svg.onesignal-bell-svg {
width: 100%;
height: 100%;
overflow: visible;
}

amp-web-push-widget button.subscribe svg.onesignal-bell-svg .background, amp-web-push-widget button.unsubscribe svg.onesignal-bell-svg .background {
fill: #e54b4d;
}

amp-web-push-widget button.subscribe svg.onesignal-bell-svg .foreground, amp-web-push-widget button.unsubscribe svg.onesignal-bell-svg .foreground {
fill: #ffffff;
}

amp-web-push-widget button.subscribe svg.onesignal-bell-svg .stroke, amp-web-push-widget button.unsubscribe svg.onesignal-bell-svg .stroke {
fill: none;
stroke: #ffffff;
stroke-width: 3px;
stroke-miterlimit: 10;
}

/* Tooltip text */
amp-web-push-widget button.subscribe .tooltiptext, amp-web-push-widget button.unsubscribe .tooltiptext {
visibility: hidden;
min-width: 350px;
background-color: black;
color: #fff;
text-align: center;
padding: 5px 0;
border-radius: 6px;
top: 5px;
right: 120%;
position: absolute;
z-index: 1;
font-size: 1em;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}

amp-web-push-widget button.subscribe .tooltiptext::after, amp-web-push-widget button.unsubscribe .tooltiptext::after {
content: "";
position: absolute;
top: 50%;
left: 100%;
margin-top: -5px;
border-width: 5px;
border-style: solid;
border-color: transparent transparent transparent black;
}

amp-web-push-widget button.subscribe:hover .tooltiptext, amp-web-push-widget button.unsubscribe:hover .tooltiptext {
visibility: visible;
}
114 changes: 113 additions & 1 deletion onesignal-public.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function __construct()
public static function init()
{
add_action('wp_head', array(__CLASS__, 'onesignal_header'), 10);
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'onesigal_amp_style' ) );
}

// For easier debugging of sites by identifying them as WordPress
Expand Down Expand Up @@ -51,6 +52,20 @@ private static function getOneSignalPluginPath()

public static function onesignal_header()
{

if ( function_exists( 'amp_is_request' ) && amp_is_request() ) {

if ( function_exists( 'amp_is_legacy' ) && amp_is_legacy() ) {
add_action( 'amp_post_template_body_open', array( __CLASS__, 'insert_amp_web_push' ) );
add_action( 'amp_post_template_footer', array( __CLASS__, 'insert_amp_one_signal_widget' ) );
} else {
add_action( 'wp_body_open', array( __CLASS__, 'insert_amp_web_push' ) );
add_action( 'wp_footer', array( __CLASS__, 'insert_amp_one_signal_widget' ) );
}

return;
}

$onesignal_wp_settings = OneSignal::get_onesignal_settings();

if (array_key_exists('subdomain', $onesignal_wp_settings) && $onesignal_wp_settings['subdomain'] === '') {
Expand Down Expand Up @@ -346,5 +361,102 @@ function documentInitOneSignal() {
</script>
<?php
}

/**
* Enqueues style for onesignal push notification button.
*
* @return void
*/
public static function onesigal_amp_style() {
if ( ! self::onesignal_is_amp() ) {
return;
}

wp_enqueue_style( 'onesignal-amp', plugin_dir_url( __FILE__ ) . 'css/onsignal-amp-style.css', '', '0.1' );
}

/**
* OneSignal AMP endpoint.
*
* @return boolean
*/
public static function onesignal_is_amp() {

$is_amp = false;

if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) {
$is_amp = true;
}

/**
* Filter to modify AMP check.
*/
return apply_filters( 'onesignal_is_amp', $is_amp );
}

/**
* Returns One Signal App ID
*
* @return int | false return one singal app ID false otherwise.
*/
public static function get_onesignal_app_id() {
$onesignal_wp_settings = OneSignal::get_onesignal_settings();
$onesignal_app_id = $onesignal_wp_settings['app_id'];
return $onesignal_app_id;
}

/**
* Add amp-web-push component.
*
* @return html
*/
public static function insert_amp_web_push() {

$onesignal_app_id = self::get_onesignal_app_id();

if ( empty( $onesignal_app_id ) ) {
return;
}

$one_signal_sdk_files_url = plugin_dir_url( __FILE__ ) . 'sdk_files/';

$helper_iframe_url = $one_signal_sdk_files_url . 'amp-helper-frame.html?appId=' . $onesignal_app_id;
$permission_dialog_url = $one_signal_sdk_files_url . 'amp-permission-dialog.html?appId=' . $onesignal_app_id;
$service_worker_url = $one_signal_sdk_files_url . 'OneSignalSDKWorker.js.php?appId=' . $onesignal_app_id;

echo sprintf(
'<amp-web-push id="amp-web-push" layout="nodisplay" helper-iframe-url="%1$s" permission-dialog-url="%2$s" service-worker-url="%3$s"></amp-web-push>',
esc_url( $helper_iframe_url ),
esc_url( $permission_dialog_url ),
esc_url( $service_worker_url )
);
}

/**
* Add AMP webpush widget.
*/
public static function insert_amp_one_signal_widget() {
$onesignal_app_id = self::get_onesignal_app_id();

if ( empty( $onesignal_app_id ) ) {
return;
}
?>
<!-- A subscription widget -->
<amp-web-push-widget visibility="unsubscribed" layout="fixed" width="245" height="45">
<button class="subscribe has-background has-text-color" on="tap:amp-web-push.subscribe">
<svg class="onesignal-bell-svg" xmlns="http://www.w3.org/2000/svg" width="99.7" height="99.7" viewBox="0 0 99.7 99.7" style="filter: drop-shadow(0 2px 4px rgba(34,36,38,0.35));; -webkit-filter: drop-shadow(0 2px 4px rgba(34,36,38,0.35));;"><circle class="background" cx="49.9" cy="49.9" r="49.9" style=""></circle><path class="foreground" d="M50.1 66.2H27.7s-2-.2-2-2.1c0-1.9 1.7-2 1.7-2s6.7-3.2 6.7-5.5S33 52.7 33 43.3s6-16.6 13.2-16.6c0 0 1-2.4 3.9-2.4 2.8 0 3.8 2.4 3.8 2.4 7.2 0 13.2 7.2 13.2 16.6s-1 11-1 13.3c0 2.3 6.7 5.5 6.7 5.5s1.7.1 1.7 2c0 1.8-2.1 2.1-2.1 2.1H50.1zm-7.2 2.3h14.5s-1 6.3-7.2 6.3-7.3-6.3-7.3-6.3z" style=""></path><ellipse class="stroke" cx="49.9" cy="49.9" rx="37.4" ry="36.9" style=""></ellipse></svg>
<span class="tooltiptext"><?php esc_html_e( 'Subscribe to notifications' ); ?></span>
</button>
</amp-web-push-widget>

<!-- An unsubscription widget -->
<amp-web-push-widget visibility="subscribed" layout="fixed" width="230" height="45">
<button class="unsubscribe has-background has-text-color" on="tap:amp-web-push.unsubscribe">
<svg class="onesignal-bell-svg" xmlns="http://www.w3.org/2000/svg" width="99.7" height="99.7" viewBox="0 0 99.7 99.7" style="filter: drop-shadow(0 2px 4px rgba(34,36,38,0.35));; -webkit-filter: drop-shadow(0 2px 4px rgba(34,36,38,0.35));;"><circle class="background" cx="49.9" cy="49.9" r="49.9" style=""></circle><path class="foreground" d="M50.1 66.2H27.7s-2-.2-2-2.1c0-1.9 1.7-2 1.7-2s6.7-3.2 6.7-5.5S33 52.7 33 43.3s6-16.6 13.2-16.6c0 0 1-2.4 3.9-2.4 2.8 0 3.8 2.4 3.8 2.4 7.2 0 13.2 7.2 13.2 16.6s-1 11-1 13.3c0 2.3 6.7 5.5 6.7 5.5s1.7.1 1.7 2c0 1.8-2.1 2.1-2.1 2.1H50.1zm-7.2 2.3h14.5s-1 6.3-7.2 6.3-7.3-6.3-7.3-6.3z" style=""></path><ellipse class="stroke" cx="49.9" cy="49.9" rx="37.4" ry="36.9" style=""></ellipse></svg>
<span class="tooltiptext"><?php esc_html_e( 'Your\'e subscribed to notifications' ); ?></span>
</button>
</amp-web-push-widget>
<?php
}
}
?>