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

feat(amp-plus): allow OneSignal script #1076

Merged
merged 5 commits into from
Nov 28, 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
9 changes: 5 additions & 4 deletions includes/class-amp-enhancements.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,16 @@ public static function is_error_attribute_matching_string( $attribute, $string,
*
* @param string[] $strings Strings to look for.
* @param array $amp_error AMP error.
* @param string $attribute Which attribute to look at.
*/
public static function is_script_id_matching_strings( $strings, $amp_error ) {
if ( ! isset( $amp_error, $amp_error['node_attributes'], $amp_error['node_attributes']['id'] ) ) {
public static function is_script_attribute_matching_strings( $strings, $amp_error, $attribute = 'id' ) {
if ( ! isset( $amp_error, $amp_error['node_attributes'], $amp_error['node_attributes'][ $attribute ] ) ) {
return false;
}
return array_reduce(
$strings,
function( $carry, $text ) use ( $amp_error ) {
return $carry || false !== strpos( $amp_error['node_attributes']['id'], $text );
function( $carry, $text ) use ( $amp_error, $attribute ) {
return $carry || false !== strpos( $amp_error['node_attributes'][ $attribute ], $text );
},
false
);
Expand Down
1 change: 1 addition & 0 deletions includes/class-newspack.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ private function includes() {
include_once NEWSPACK_ABSPATH . 'includes/plugins/google-site-kit/class-googlesitekit.php';
include_once NEWSPACK_ABSPATH . 'includes/plugins/class-newspack-newsletters.php';
include_once NEWSPACK_ABSPATH . 'includes/plugins/class-mailchimp-for-woocommerce.php';
include_once NEWSPACK_ABSPATH . 'includes/plugins/class-onesignal.php';
include_once NEWSPACK_ABSPATH . 'includes/plugins/class-organic-profile-block.php';

include_once NEWSPACK_ABSPATH . 'includes/class-patches.php';
Expand Down
2 changes: 1 addition & 1 deletion includes/plugins/class-gravityforms.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ public static function filter_scripts_for_amp_plus( $is_sanitized, $error ) {
return $is_sanitized;
}

if ( AMP_Enhancements::is_script_id_matching_strings(
if ( AMP_Enhancements::is_script_attribute_matching_strings(
[
'gform_',
'gpoll_', // GravityForms Polls (gravityformspolls) plugin.
Expand Down
2 changes: 1 addition & 1 deletion includes/plugins/class-jetpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static function jetpack_modules_amp_plus( $is_sanitized, $error ) {
if ( ! self::should_amp_plus_modules() ) {
return $is_sanitized;
}
if ( AMP_Enhancements::is_script_id_matching_strings( self::$scripts_handles, $error ) ) {
if ( AMP_Enhancements::is_script_attribute_matching_strings( self::$scripts_handles, $error ) ) {
$is_sanitized = false;
}

Expand Down
55 changes: 55 additions & 0 deletions includes/plugins/class-onesignal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* OneSignal integration class.
* https://wordpress.org/plugins/onesignal-free-web-push-notifications
*
* @package Newspack
*/

namespace Newspack;

defined( 'ABSPATH' ) || exit;

/**
* Main class.
*/
class OneSignal {
/**
* Initialize hooks and filters.
*/
public static function init() {
add_filter( 'newspack_amp_plus_sanitized', [ __CLASS__, 'onesignal_amp_plus' ], 10, 2 );
add_filter( 'onesignal_is_amp', [ __CLASS__, 'onesignal_is_amp' ] );
}

/**
* Allow OneSignal modules scripts to be loaded in AMP Plus mode.
*
* @param bool|null $is_sanitized If null, the error will be handled. If false, rejected.
* @param object $error The AMP sanitisation error.
*
* @return bool Whether the error should be rejected.
*/
public static function onesignal_amp_plus( $is_sanitized, $error ) {
if (
AMP_Enhancements::is_script_attribute_matching_strings( [ 'onesignal.com' ], $error, 'src' )
|| AMP_Enhancements::is_script_body_matching_strings( [ 'window.OneSignal' ], $error )
) {
$is_sanitized = false;
}
return $is_sanitized;
}

/**
* Override is-AMP check if AMP Plus is enabled.
*
* @param bool $is_amp Whether the page is AMP.
*/
public static function onesignal_is_amp( $is_amp ) {
if ( AMP_Enhancements::should_use_amp_plus() ) {
return false;
}
return $is_amp;
}
}
OneSignal::init();