Skip to content

Commit 73d904f

Browse files
authored
AMP Support for Cookies and Consent (#15722)
1 parent 69e4e3e commit 73d904f

File tree

4 files changed

+68
-11
lines changed

4 files changed

+68
-11
lines changed

bin/phpcs-whitelist.js

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ module.exports = [
5454
'modules/theme-tools/devicepx.php',
5555
'modules/verification-tools.php',
5656
'modules/widgets/contact-info.php',
57+
'modules/widgets/eu-cookie-law/widget-amp.php',
5758
'modules/widgets/social-icons.php',
5859
'modules/woocommerce-analytics.php',
5960
'modules/wpcom-block-editor/class-jetpack-wpcom-block-editor.php',

modules/widgets/eu-cookie-law.php

+19-11
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,20 @@ function __construct() {
9898
* Enqueue scripts and styles.
9999
*/
100100
function enqueue_frontend_scripts() {
101-
wp_enqueue_style( 'eu-cookie-law-style', plugins_url( 'eu-cookie-law/style.css', __FILE__ ), array(), '20170403' );
102-
wp_enqueue_script(
103-
'eu-cookie-law-script',
104-
Assets::get_file_url_for_environment(
105-
'_inc/build/widgets/eu-cookie-law/eu-cookie-law.min.js',
106-
'modules/widgets/eu-cookie-law/eu-cookie-law.js'
107-
),
108-
array(),
109-
'20180522',
110-
true
111-
);
101+
wp_enqueue_style( 'eu-cookie-law-style', plugins_url( 'eu-cookie-law/style.css', __FILE__ ), array(), JETPACK__VERSION );
102+
103+
if ( ! class_exists( 'Jetpack_AMP_Support' ) || ! Jetpack_AMP_Support::is_amp_request() ) {
104+
wp_enqueue_script(
105+
'eu-cookie-law-script',
106+
Assets::get_file_url_for_environment(
107+
'_inc/build/widgets/eu-cookie-law/eu-cookie-law.min.js',
108+
'modules/widgets/eu-cookie-law/eu-cookie-law.js'
109+
),
110+
array(),
111+
'20180522',
112+
true
113+
);
114+
}
112115
}
113116

114117
/**
@@ -156,6 +159,11 @@ public function widget( $args, $instance ) {
156159

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

162+
if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
163+
require dirname( __FILE__ ) . '/eu-cookie-law/widget-amp.php';
164+
return;
165+
}
166+
159167
$classes = array();
160168
$classes['hide'] = 'hide-on-' . esc_attr( $instance['hide'] );
161169
if ( 'negative' === $instance['color-scheme'] ) {

modules/widgets/eu-cookie-law/style.css

+10
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@
2525
top: 3em;
2626
}
2727

28+
amp-consent.widget_eu_cookie_law_widget.widget.top {
29+
top: 1em;
30+
margin: 0;
31+
}
32+
33+
.admin-bar amp-consent.widget_eu_cookie_law_widget.widget.top {
34+
top: 0;
35+
margin-top: 3em;
36+
}
37+
2838
#eu-cookie-law {
2939
background-color: #fff;
3040
border: 1px solid #dedede;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php // phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
2+
/**
3+
* AMP Widget for Cookies and Consent.
4+
*
5+
* @package Jetpack
6+
*/
7+
?>
8+
9+
<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' : '' ); ?>">
10+
<script type="application/json">
11+
{
12+
"consentInstanceId": "eu-cookie-consent",
13+
"consentRequired": true,
14+
"promptUI": "eu-cookie-consent-prompt"
15+
}
16+
</script>
17+
<div class="popupOverlay" id="eu-cookie-consent-prompt">
18+
<div class="consentPopup<?php echo esc_attr( ! empty( $instance['color-scheme'] ) && 'negative' === $instance['color-scheme'] ? ' negative' : '' ); ?>" id="eu-cookie-law">
19+
<form>
20+
<input type="button" on="tap:eu-cookie-consent.accept" class="accept" value="<?php echo esc_attr( $instance['button'] ); ?>" />
21+
</form>
22+
<?php
23+
if ( 'default' === $instance['text'] || empty( $instance['customtext'] ) ) {
24+
echo wp_kses_post( nl2br( $instance['default-text'] ) );
25+
} else {
26+
echo esc_html( $instance['customtext'] );
27+
}
28+
29+
$policy_link_text = 'default' === $instance['policy-url'] || empty( $instance['custom-policy-url'] )
30+
? $instance['default-policy-url']
31+
: $instance['custom-policy-url'];
32+
?>
33+
<a href="<?php echo esc_url( $policy_link_text ); ?>">
34+
<?php echo esc_html( $instance['policy-link-text'] ); ?>
35+
</a>
36+
</div>
37+
</div>
38+
</amp-consent>

0 commit comments

Comments
 (0)