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

AMP Support for Cookies and Consent #15722

Merged
merged 4 commits into from
May 22, 2020
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
1 change: 1 addition & 0 deletions bin/phpcs-whitelist.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ module.exports = [
'modules/theme-tools/devicepx.php',
'modules/verification-tools.php',
'modules/widgets/contact-info.php',
'modules/widgets/eu-cookie-law/widget-amp.php',
'modules/widgets/social-icons.php',
'modules/woocommerce-analytics.php',
'modules/wpcom-block-editor/class-jetpack-wpcom-block-editor.php',
Expand Down
30 changes: 19 additions & 11 deletions modules/widgets/eu-cookie-law.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,20 @@ function __construct() {
* Enqueue scripts and styles.
*/
function enqueue_frontend_scripts() {
wp_enqueue_style( 'eu-cookie-law-style', plugins_url( 'eu-cookie-law/style.css', __FILE__ ), array(), '20170403' );
wp_enqueue_script(
'eu-cookie-law-script',
Assets::get_file_url_for_environment(
'_inc/build/widgets/eu-cookie-law/eu-cookie-law.min.js',
'modules/widgets/eu-cookie-law/eu-cookie-law.js'
),
array(),
'20180522',
true
);
wp_enqueue_style( 'eu-cookie-law-style', plugins_url( 'eu-cookie-law/style.css', __FILE__ ), array(), JETPACK__VERSION );

if ( ! class_exists( 'Jetpack_AMP_Support' ) || ! Jetpack_AMP_Support::is_amp_request() ) {
wp_enqueue_script(
'eu-cookie-law-script',
Assets::get_file_url_for_environment(
'_inc/build/widgets/eu-cookie-law/eu-cookie-law.min.js',
'modules/widgets/eu-cookie-law/eu-cookie-law.js'
),
array(),
'20180522',
true
);
}
}

/**
Expand Down Expand Up @@ -156,6 +159,11 @@ public function widget( $args, $instance ) {

$instance = wp_parse_args( $instance, $this->defaults() );

if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
require dirname( __FILE__ ) . '/eu-cookie-law/widget-amp.php';
return;
}

$classes = array();
$classes['hide'] = 'hide-on-' . esc_attr( $instance['hide'] );
if ( 'negative' === $instance['color-scheme'] ) {
Expand Down
10 changes: 10 additions & 0 deletions modules/widgets/eu-cookie-law/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@
top: 3em;
}

amp-consent.widget_eu_cookie_law_widget.widget.top {
top: 1em;
margin: 0;
}

.admin-bar amp-consent.widget_eu_cookie_law_widget.widget.top {
top: 0;
margin-top: 3em;
}

#eu-cookie-law {
background-color: #fff;
border: 1px solid #dedede;
Expand Down
38 changes: 38 additions & 0 deletions modules/widgets/eu-cookie-law/widget-amp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php // phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
/**
* AMP Widget for Cookies and Consent.
*
* @package Jetpack
*/
?>

<amp-consent id="eu-cookie-consent" layout="nodisplay" class="widget widget_eu_cookie_law_widget<?php echo esc_attr( ! empty( $instance['position'] ) && 'top' === $instance['position'] ? ' top' : '' ); ?>">
<script type="application/json">
{
"consentInstanceId": "eu-cookie-consent",
"consentRequired": true,
"promptUI": "eu-cookie-consent-prompt"
}
</script>
<div class="popupOverlay" id="eu-cookie-consent-prompt">
<div class="consentPopup<?php echo esc_attr( ! empty( $instance['color-scheme'] ) && 'negative' === $instance['color-scheme'] ? ' negative' : '' ); ?>" id="eu-cookie-law">
<form>
<input type="button" on="tap:eu-cookie-consent.accept" class="accept" value="<?php echo esc_attr( $instance['button'] ); ?>" />
</form>
<?php
if ( 'default' === $instance['text'] || empty( $instance['customtext'] ) ) {
echo wp_kses_post( nl2br( $instance['default-text'] ) );
} else {
echo esc_html( $instance['customtext'] );
}

$policy_link_text = 'default' === $instance['policy-url'] || empty( $instance['custom-policy-url'] )
? $instance['default-policy-url']
: $instance['custom-policy-url'];
?>
<a href="<?php echo esc_url( $policy_link_text ); ?>">
<?php echo esc_html( $instance['policy-link-text'] ); ?>
</a>
</div>
</div>
</amp-consent>