iubenda Cookie Solution for GDPR - Version 2.1.3.1

Version Description

Download this release

Release Info

Developer dfactory
Plugin Icon 128x128 iubenda Cookie Solution for GDPR
Version 2.1.3.1
Comparing to
See all releases

Code changes from version 2.1.3-beta to 2.1.3.1

Files changed (2) hide show
  1. includes/amp.php +347 -0
  2. readme.txt +3 -0
includes/amp.php ADDED
@@ -0,0 +1,347 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ // exit if accessed directly
3
+ if ( ! defined( 'ABSPATH' ) )
4
+ exit;
5
+
6
+ /**
7
+ * iubenda_AMP class.
8
+ *
9
+ * @class iubenda_AMP
10
+ */
11
+ class iubenda_AMP {
12
+
13
+ /**
14
+ * Class constructor.
15
+ */
16
+ public function __construct() {
17
+ // actions
18
+ add_action( 'wp_head', array( $this, 'wp_head_amp' ), 100 );
19
+ add_action( 'wp_footer', array( $this, 'wp_footer_amp' ), 100 );
20
+ add_action( 'amp_post_template_css', array( $this, 'amp_post_template_css' ), 100 );
21
+ add_action( 'amp_post_template_footer', array( $this, 'wp_footer_amp' ), 100 );
22
+ add_action( 'amp_post_template_footer', array( $this, 'fix_analytics_amp_for_wp' ), 1 );
23
+
24
+ // filters
25
+ add_filter( 'amp_post_template_data', array( $this, 'amp_post_template_data' ), 100 );
26
+ add_filter( 'amp_analytics_entries', array( $this, 'fix_analytics_wp_amp' ), 10 );
27
+ }
28
+
29
+ /**
30
+ * Add scripts and CSS to WP AMP plugin.
31
+ *
32
+ * @return mixed
33
+ */
34
+ public function wp_head_amp() {
35
+ if ( iubenda()->options['cs']['amp_support'] === false )
36
+ return;
37
+
38
+ if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() && ! function_exists( 'ampforwp_is_amp_endpoint' ) ) {
39
+ echo '
40
+ <script async custom-element="amp-consent" src="https://cdn.ampproject.org/v0/amp-consent-latest.js"></script>
41
+ <script async custom-element="amp-iframe" src="https://cdn.ampproject.org/v0/amp-iframe-latest.js"></script>';
42
+ /* optional geo support
43
+ echo '
44
+ <script async custom-element="amp-geo" src="https://cdn.ampproject.org/v0/amp-geo-0.1.js"></script>';
45
+ */
46
+ // CSS style
47
+ echo '
48
+ <style amp-custom>
49
+ .popupOverlay {
50
+ position:fixed;
51
+ top: 0;
52
+ bottom: 0;
53
+ left: 0;
54
+ right: 0;
55
+ }
56
+ amp-iframe {
57
+ margin: 0;
58
+ }
59
+ amp-consent.amp-active {
60
+ position:fixed;
61
+ top: 0;
62
+ bottom: 0;
63
+ left: 0;
64
+ right: 0;
65
+ }
66
+ </style>';
67
+ }
68
+ }
69
+
70
+ /**
71
+ * Add AMP consent HTML to footer.
72
+ *
73
+ * @return mixed
74
+ */
75
+ public function wp_footer_amp() {
76
+ if ( iubenda()->options['cs']['amp_support'] === false )
77
+ return;
78
+
79
+ if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() || ( function_exists( 'ampforwp_is_amp_endpoint' ) && ampforwp_is_amp_endpoint() ) ) {
80
+
81
+ $configuration = iubenda()->parse_configuration();
82
+
83
+ if ( empty( $configuration ) )
84
+ return;
85
+
86
+ // local file
87
+ if ( iubenda()->options['cs']['amp_source'] === 'local' ) {
88
+ $template_url = $this->get_amp_template_url();
89
+ // remote file
90
+ } else {
91
+ $template_url = esc_url( iubenda()->options['cs']['amp_template'] );
92
+ }
93
+
94
+ if ( empty( $template_url ) )
95
+ return;
96
+
97
+ echo '
98
+ <amp-consent id="myUserConsent" layout="nodisplay">
99
+ <script type="application/json">
100
+ {
101
+ "consentInstanceId": "consent' . $configuration['siteId'] . '",
102
+ "consentRequired": true,
103
+ "promptUI": "myConsentFlow"
104
+ }
105
+ </script>
106
+ <div id="myConsentFlow" class="popupOverlay">
107
+ <amp-iframe
108
+ layout="fill"
109
+ sandbox="allow-scripts allow-same-origin allow-popups allow-popups-to-escape-sandbox"
110
+ src="' . esc_url( $template_url ) . '">
111
+ <div placeholder>' . __( 'Loading', 'iubenda' ) . '</div>
112
+ </amp-iframe>
113
+ </div>
114
+ </amp-consent>';
115
+ }
116
+ }
117
+
118
+ /**
119
+ * Add scripts to AMP for WP plugin.
120
+ *
121
+ * @return mixed
122
+ */
123
+ public function amp_post_template_data( $data ) {
124
+ if ( iubenda()->options['cs']['amp_support'] === false )
125
+ return $data;
126
+
127
+ if ( function_exists( 'ampforwp_is_amp_endpoint' ) && ampforwp_is_amp_endpoint() ) {
128
+ $data['amp_component_scripts'] = array_merge( $data['amp_component_scripts'],
129
+ array( 'amp-consent' => 'https://cdn.ampproject.org/v0/amp-consent-latest.js' )
130
+ );
131
+ $data['amp_component_scripts'] = array_merge( $data['amp_component_scripts'],
132
+ array( 'amp-iframe' => 'https://cdn.ampproject.org/v0/amp-iframe-latest.js' )
133
+ );
134
+ }
135
+
136
+ return $data;
137
+ }
138
+
139
+ /**
140
+ * Add CSS to AMP for WP plugin.
141
+ *
142
+ * @return mixed
143
+ */
144
+ public function amp_post_template_css( $data ) {
145
+ if ( iubenda()->options['cs']['amp_support'] === false )
146
+ return;
147
+
148
+ if ( function_exists( 'ampforwp_is_amp_endpoint' ) && ampforwp_is_amp_endpoint() ) {
149
+ ?>
150
+ .popupOverlay {
151
+ position:fixed;
152
+ top: 0;
153
+ bottom: 0;
154
+ left: 0;
155
+ right: 0;
156
+ }
157
+ amp-iframe {
158
+ margin: 0;
159
+ }
160
+ amp-consent.amp-active {
161
+ position:fixed;
162
+ top: 0;
163
+ bottom: 0;
164
+ left: 0;
165
+ right: 0;
166
+ }
167
+ <?php
168
+ }
169
+ }
170
+
171
+ /**
172
+ * Block analytics in AMP for WP plugin.
173
+ *
174
+ * @return mixed
175
+ */
176
+ public function fix_analytics_amp_for_wp( $data ) {
177
+ if ( iubenda()->options['cs']['amp_support'] === false )
178
+ return $data;
179
+
180
+ global $redux_builder_amp;
181
+
182
+ if ( $redux_builder_amp == null ) {
183
+ $redux_builder_amp = get_option( 'redux_builder_amp', true );
184
+ }
185
+
186
+ // trick to block the analytics using global $redux_builder_amp variable
187
+ if ( ! iubendaParser::consent_given() )
188
+ $redux_builder_amp = true;
189
+
190
+ return $data;
191
+ }
192
+
193
+ /**
194
+ * Block analytics in WP AMP plugin.
195
+ *
196
+ * @return mixed
197
+ */
198
+ public function fix_analytics_wp_amp( $analytics_entries ) {
199
+ if ( iubenda()->options['cs']['amp_support'] === false )
200
+ return $analytics_entries;
201
+
202
+ // block the analytics using the entries filter hook
203
+ if ( ! iubendaParser::consent_given() && ! empty( $analytics_entries ) && is_array( $analytics_entries ) ) {
204
+ foreach ( $analytics_entries as $id => $entry ) {
205
+ $entry['attributes'] = ! empty( $entry['attributes'] ) ? $entry['attributes'] : array();
206
+
207
+ $analytics_entries[$id]['attributes'] = array_merge( array( 'data-block-on-consent' => '_till_accepted' ), $entry['attributes'] );
208
+ }
209
+ }
210
+
211
+ return $analytics_entries;
212
+ }
213
+
214
+ /**
215
+ * Prepare HTML iframe template for the AMP.
216
+ *
217
+ * @return mixed
218
+ */
219
+ public function prepare_amp_template() {
220
+ $html = '';
221
+ $configuration_raw = iubenda()->parse_configuration( false );
222
+
223
+ if ( ! empty( $configuration_raw ) ) {
224
+ // encode array
225
+ $configuration = json_encode( $configuration_raw );
226
+ // remove quotes
227
+ $configuration = preg_replace( '/"([a-zA-Z]+[a-zA-Z0-9]*)":/', '$1:', $configuration );
228
+ // replace brackets
229
+ $configuration = str_replace( array( '{', '}' ), '', $configuration );
230
+
231
+ $html .= '<!DOCTYPE html>
232
+ <html lang="' . $configuration_raw['lang'] . '">
233
+ <head>
234
+ <meta charset="UTF-8">
235
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
236
+ <meta http-equiv="X-UA-Compatible" content="ie=edge">
237
+ <title>AMP Cookie Consent</title>
238
+ <script type="text/javascript">
239
+ var _iub = _iub || [];
240
+ _iub.csConfiguration = {
241
+ ';
242
+ // print configuration
243
+ $html .= $configuration . ',';
244
+ $html .= '
245
+ banner: {
246
+ position: \'float-bottom-center\',
247
+ acceptButtonDisplay: true,
248
+ customizeButtonDisplay: true,
249
+ rejectButtonDisplay: true,
250
+ backgroundOverlay: true
251
+ },
252
+ callback: {
253
+ onPreferenceExpressed: function(preference) {
254
+ var consentAction = \'reject\';
255
+ if (preference && preference.consent) {
256
+ consentAction = \'accept\';
257
+ }
258
+ console.log(\'send consent-response\', consentAction);
259
+ window.parent.postMessage({
260
+ type: \'consent-response\',
261
+ action: consentAction
262
+ }, \'*\');
263
+ }
264
+ }
265
+ };
266
+ </script>
267
+ <script async src="https://cdn.iubenda.com/cs/beta/iubenda_cs.js"></script>
268
+ </head>
269
+ <body></body>
270
+ </html>';
271
+ }
272
+
273
+ return $html;
274
+ }
275
+
276
+ /**
277
+ * Get local file template url;
278
+ *
279
+ * @return string
280
+ */
281
+ public function get_amp_template_url() {
282
+ // get basic site host and template file data
283
+ $template_url = '';
284
+ $file_url = IUBENDA_PLUGIN_URL . '/templates/amp.html';
285
+ // $file_url = 'https://cdn.iubenda.com/cs/test/cs-for-amp.html'; // debug only
286
+ $parsed_site = parse_url( home_url() );
287
+ $parsed_file = parse_url( $file_url );
288
+ $site_host = $parsed_site['host'] !== 'localhost' ? iubenda()->domain( $parsed_site['host'] ) : 'localhost';
289
+ $file_host = $parsed_file['host'] !== 'localhost' ? iubenda()->domain( $parsed_file['host'] ) : 'localhost';
290
+ $is_localhost = (bool) ( $site_host == 'localhost' );
291
+ $is_subdomain = ! $is_localhost ? (bool) ( $parsed_file['host'] !== $file_host ) : false;
292
+
293
+ // check if file host and server host match
294
+ // if not, we're good to go
295
+ if ( $site_host !== $file_host ) {
296
+ $template_url = $file_url;
297
+ // if are located on same host do additional tweaks
298
+ } else {
299
+ // all ok if we're on different subdomains
300
+ if ( $parsed_site['host'] !== $parsed_file['host'] )
301
+ $template_url = $file_url;
302
+ // same hosts, let's tweak the http/https
303
+ else {
304
+ $has_www = strpos( $parsed_file['host'], 'www.' ) === 0;
305
+
306
+ // add or remove www from url string to make iframe url pass AMP validation
307
+ $tweaked_host = ! $is_localhost && ! $is_subdomain ? ( ! $has_www ? 'www.' . $parsed_file['host'] : preg_replace( '/^www\./i', '', $parsed_file['host'] ) ) : $parsed_file['host'];
308
+
309
+ // generate new url
310
+ $tweaked_url = $parsed_file['scheme'] . '://' . $tweaked_host . ( isset( $parsed_file['port'] ) ? ':' . $parsed_file['port'] : '' ) . $parsed_file['path'] . ( ! empty( $parsed_file['query'] ) ? '?' . $parsed_file['query'] : '' );
311
+
312
+ // check if file url is valid
313
+ if ( $tweaked_url ) {
314
+ $template_url = $tweaked_url;
315
+ }
316
+ }
317
+ }
318
+
319
+ return $template_url;
320
+ }
321
+
322
+ /**
323
+ * Generate HTML iframe template for the AMP.
324
+ *
325
+ * @return mixed
326
+ */
327
+ public function generate_amp_template() {
328
+ $template_file = IUBENDA_PLUGIN_PATH . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'amp.html';
329
+ $html = $this->prepare_amp_template();
330
+
331
+ // bail if the template was not created properly
332
+ if ( empty( $html ) )
333
+ return false;
334
+
335
+ // bail if the file is writable
336
+ if ( ! is_writable( $template_file ) )
337
+ return false;
338
+
339
+ // write the file
340
+ $fh = fopen( $template_file, 'w' );
341
+ fwrite( $fh, $html );
342
+ fclose( $fh );
343
+
344
+ return true;
345
+ }
346
+
347
+ }
readme.txt CHANGED
@@ -134,6 +134,9 @@ We will be very happy to receive feedback here: [Uservoice forum](http://support
134
 
135
  == Changelog ==
136
 
 
 
 
137
  = 2.1.3-beta =
138
  * Tweak: Block tracking code from WP AMP plugins
139
 
134
 
135
  == Changelog ==
136
 
137
+ = 2.1.3.1-beta =
138
+ * Fix: Fix missing amp.php file in the SVN repo
139
+
140
  = 2.1.3-beta =
141
  * Tweak: Block tracking code from WP AMP plugins
142