iubenda Cookie Solution for GDPR - Version 2.3.3

Version Description

  • Fix: AddThis purpose category
Download this release

Release Info

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

Code changes from version 2.3.2 to 2.3.3

includes/amp.php CHANGED
@@ -1,399 +1,399 @@
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 in Transitional mode.
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
-
43
- // optional geo support
44
- if ( iubenda()->multilang && ! empty( iubenda()->lang_current ) ) {
45
- $code = iubenda()->options['cs']['code_' . iubenda()->lang_current];
46
- } else {
47
- $code = iubenda()->options['cs']['code_default'];
48
- }
49
-
50
- $configuration_raw = iubenda()->parse_configuration( $code );
51
-
52
- if ( isset( $configuration_raw['gdprAppliesGlobally'] ) && ! $configuration_raw['gdprAppliesGlobally'] ) {
53
- echo '
54
- <script async custom-element="amp-geo" src="https://cdn.ampproject.org/v0/amp-geo-0.1.js"></script>';
55
- }
56
-
57
- // CSS style
58
- echo '
59
- <style amp-custom>
60
- .popupOverlay {
61
- position:fixed;
62
- top: 0;
63
- bottom: 0;
64
- left: 0;
65
- right: 0;
66
- }
67
- amp-iframe {
68
- margin: 0;
69
- }
70
- amp-consent.amp-active {
71
- position:fixed;
72
- top: 0;
73
- bottom: 0;
74
- left: 0;
75
- right: 0;
76
- }
77
- </style>';
78
- }
79
- }
80
-
81
- /**
82
- * Add AMP consent HTML to WP AMP plugin in Transitional mode.
83
- *
84
- * @return mixed
85
- */
86
- public function wp_footer_amp() {
87
- if ( iubenda()->options['cs']['amp_support'] === false )
88
- return;
89
-
90
- if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() || ( function_exists( 'ampforwp_is_amp_endpoint' ) && ampforwp_is_amp_endpoint() ) ) {
91
-
92
- // get code
93
- if ( iubenda()->multilang && ! empty( iubenda()->lang_current ) ) {
94
- $code = iubenda()->options['cs']['code_' . iubenda()->lang_current];
95
- } else {
96
- $code = iubenda()->options['cs']['code_default'];
97
- }
98
-
99
- $configuration = iubenda()->parse_configuration( $code );
100
-
101
- if ( empty( $configuration ) )
102
- return;
103
-
104
- // local file
105
- if ( iubenda()->options['cs']['amp_source'] === 'local' ) {
106
- // multilang support
107
- if ( iubenda()->multilang && ! empty( iubenda()->lang_current ) )
108
- $template_url = $this->get_amp_template_url( iubenda()->lang_current );
109
- else
110
- $template_url = $this->get_amp_template_url();
111
- // remote file
112
- } else {
113
- // multilang support
114
- if ( iubenda()->multilang && ! empty( iubenda()->lang_current ) )
115
- $template_url = esc_url( isset( iubenda()->options['cs']['amp_template'][iubenda()->lang_current] ) ? iubenda()->options['cs']['amp_template'][iubenda()->lang_current] : '' );
116
- else
117
- $template_url = esc_url( iubenda()->options['cs']['amp_template'] );
118
- }
119
-
120
- if ( empty( $template_url ) )
121
- return;
122
-
123
- echo '
124
- <amp-consent id="myUserConsent" layout="nodisplay">
125
- <script type="application/json">
126
- {
127
- "consentInstanceId": "consent' . $configuration['siteId'] . '",
128
- "consentRequired": true,
129
- "promptUI": "myConsentFlow"
130
- }
131
- </script>
132
- <div id="myConsentFlow" class="popupOverlay">
133
- <amp-iframe
134
- layout="fill"
135
- sandbox="allow-scripts allow-same-origin allow-popups allow-popups-to-escape-sandbox"
136
- src="' . esc_url( $template_url ) . '">
137
- <div placeholder>' . __( 'Loading', 'iubenda' ) . '</div>
138
- </amp-iframe>
139
- </div>
140
- </amp-consent>';
141
- }
142
- }
143
-
144
- /**
145
- * Add scripts to AMP for WP plugin and WP AMP plugin in Standard mode.
146
- *
147
- * @return mixed
148
- */
149
- public function amp_post_template_data( $data ) {
150
- if ( iubenda()->options['cs']['amp_support'] === false )
151
- return $data;
152
-
153
- if ( function_exists( 'ampforwp_is_amp_endpoint' ) && ampforwp_is_amp_endpoint() ) {
154
- $data['amp_component_scripts'] = array_merge( $data['amp_component_scripts'],
155
- array( 'amp-consent' => 'https://cdn.ampproject.org/v0/amp-consent-latest.js' )
156
- );
157
- $data['amp_component_scripts'] = array_merge( $data['amp_component_scripts'],
158
- array( 'amp-iframe' => 'https://cdn.ampproject.org/v0/amp-iframe-latest.js' )
159
- );
160
- }
161
-
162
- return $data;
163
- }
164
-
165
- /**
166
- * Add CSS to AMP for WP plugin and WP AMP plugin in Standard mode.
167
- *
168
- * @return mixed
169
- */
170
- public function amp_post_template_css( $data ) {
171
- if ( iubenda()->options['cs']['amp_support'] === false )
172
- return;
173
-
174
- echo '
175
- .popupOverlay {
176
- position:fixed;
177
- top: 0;
178
- bottom: 0;
179
- left: 0;
180
- right: 0;
181
- }
182
- amp-iframe {
183
- margin: 0;
184
- }
185
- amp-consent.amp-active {
186
- position:fixed;
187
- top: 0;
188
- bottom: 0;
189
- left: 0;
190
- right: 0;
191
- }';
192
- }
193
-
194
- /**
195
- * Block analytics in AMP for WP plugin.
196
- *
197
- * @return mixed
198
- */
199
- public function fix_analytics_amp_for_wp( $data ) {
200
- if ( iubenda()->options['cs']['amp_support'] === false )
201
- return $data;
202
-
203
- global $redux_builder_amp;
204
-
205
- if ( $redux_builder_amp == null ) {
206
- $redux_builder_amp = get_option( 'redux_builder_amp', true );
207
- }
208
-
209
- // trick to block the analytics using global $redux_builder_amp variable
210
- if ( ! iubendaParser::consent_given() )
211
- $redux_builder_amp = true;
212
-
213
- return $data;
214
- }
215
-
216
- /**
217
- * Block analytics in WP AMP plugin.
218
- *
219
- * @return mixed
220
- */
221
- public function fix_analytics_wp_amp( $analytics_entries ) {
222
- if ( iubenda()->options['cs']['amp_support'] === false )
223
- return $analytics_entries;
224
-
225
- // block the analytics using the entries filter hook
226
- if ( ! iubendaParser::consent_given() && ! empty( $analytics_entries ) && is_array( $analytics_entries ) ) {
227
- foreach ( $analytics_entries as $id => $entry ) {
228
- $entry['attributes'] = ! empty( $entry['attributes'] ) ? $entry['attributes'] : array();
229
-
230
- $analytics_entries[$id]['attributes'] = array_merge( array( 'data-block-on-consent' => '_till_accepted' ), $entry['attributes'] );
231
- }
232
- }
233
-
234
- return $analytics_entries;
235
- }
236
-
237
- /**
238
- * Prepare HTML iframe template for the AMP.
239
- *
240
- * @return mixed
241
- */
242
- public function prepare_amp_template( $code ) {
243
- $html = '';
244
-
245
- $configuration_raw = iubenda()->parse_configuration( $code );
246
-
247
- if ( ! empty( $configuration_raw ) ) {
248
- // get script
249
- $script_src = ! empty( $configuration_raw['script'] ) ? $configuration_raw['script'] : '//cdn.iubenda.com/cs/iubenda_cs.js';
250
-
251
- // remove from configuration
252
- if ( isset( $configuration_raw['script'] ) )
253
- unset( $configuration_raw['script'] );
254
-
255
- // encode array
256
- $configuration = json_encode( $configuration_raw );
257
-
258
- // remove quotes
259
- $configuration = preg_replace( '/"([a-zA-Z]+[a-zA-Z0-9]*)":/', '$1:', $configuration );
260
- // replace brackets
261
- $configuration = str_replace( array( '{', '}' ), '', $configuration );
262
-
263
- $html .= '<!DOCTYPE html>
264
- <html lang="' . $configuration_raw['lang'] . '">
265
- <head>
266
- <meta charset="UTF-8">
267
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
268
- <meta name="robots" content="noindex">
269
- <meta http-equiv="X-UA-Compatible" content="ie=edge">
270
- <title>' . __( 'AMP Cookie Consent', 'iubenda' ) . '</title>
271
- <script type="text/javascript">
272
- var _iub = _iub || [];
273
- _iub.csConfiguration = {
274
- ';
275
- // print configuration
276
- $html .= $configuration . ',';
277
- $html .= '
278
- banner: {
279
- position: \'float-bottom-center\',
280
- acceptButtonDisplay: true,
281
- customizeButtonDisplay: true,
282
- rejectButtonDisplay: true,
283
- backgroundOverlay: true
284
- },
285
- callback: { // Mandatory
286
- onPreferenceExpressed: function(preference) {
287
- var consentAction = \'reject\';
288
- var consentObject = {
289
- type: \'consent-response\'
290
- };
291
-
292
- if (preference && preference.consent) {
293
- consentAction = \'accept\';
294
- }
295
-
296
- consentObject.action = consentAction;
297
-
298
- if (_iub.cs.options.enableCMP) {
299
- __cmp(\'getConsentData\', null, function(res) {
300
- var consentString = res.consentData;
301
-
302
- if (consentString.length <= 200) {
303
- consentObject.info = consentString;
304
- }
305
-
306
- console.log(\'send consent-response\', consentAction, \'with CMP consent string\', consentString);
307
-
308
- window.parent.postMessage(consentObject, \'*\');
309
- });
310
- }
311
- else {
312
- console.log(\'send consent-response\', consentAction);
313
-
314
- window.parent.postMessage(consentObject, \'*\');
315
- }
316
- }
317
- }
318
- };
319
- </script>
320
- <script async src="' . $script_src . '"></script>
321
- </head>
322
- <body></body>
323
- </html>';
324
- }
325
-
326
- return $html;
327
- }
328
-
329
- /**
330
- * Get local file template url;
331
- *
332
- * @return string
333
- */
334
- public function get_amp_template_url( $template_lang = '' ) {
335
- $template_url = '';
336
- $template_lang = ! empty( $template_lang ) && is_string( $template_lang ) ? $template_lang : '';
337
-
338
- // get basic site host and template file data
339
- $file_url = ! empty( $template_lang ) ? IUBENDA_PLUGIN_URL . '/templates/amp' . '-' . $template_lang . '.html' : IUBENDA_PLUGIN_URL . '/templates/amp.html';
340
- // $file_url = 'https://cdn.iubenda.com/cs/test/cs-for-amp.html'; // debug only
341
- $parsed_site = parse_url( home_url() );
342
- $parsed_file = parse_url( $file_url );
343
- $site_host = $parsed_site['host'] !== 'localhost' ? iubenda()->domain( $parsed_site['host'] ) : 'localhost';
344
- $file_host = $parsed_file['host'] !== 'localhost' ? iubenda()->domain( $parsed_file['host'] ) : 'localhost';
345
- $is_localhost = (bool) ( $site_host == 'localhost' );
346
- $is_subdomain = ! $is_localhost ? (bool) ( $parsed_file['host'] !== $file_host ) : false;
347
-
348
- // check if file host and server host match
349
- // if not, we're good to go
350
- if ( $site_host !== $file_host ) {
351
- $template_url = $file_url;
352
- // if are located on same host do additional tweaks
353
- } else {
354
- // all ok if we're on different subdomains
355
- if ( $parsed_site['host'] !== $parsed_file['host'] )
356
- $template_url = $file_url;
357
- // same hosts, let's tweak the http/https
358
- else {
359
- $has_www = strpos( $parsed_file['host'], 'www.' ) === 0;
360
-
361
- // add or remove www from url string to make iframe url pass AMP validation
362
- $tweaked_host = ! $is_localhost && ( ! $is_subdomain || $has_www ) ? ( ! $has_www ? 'www.' . $parsed_file['host'] : preg_replace( '/^www\./i', '', $parsed_file['host'] ) ) : $parsed_file['host'];
363
-
364
- // generate new url
365
- $tweaked_url = $parsed_file['scheme'] . '://' . $tweaked_host . ( isset( $parsed_file['port'] ) ? ':' . $parsed_file['port'] : '' ) . $parsed_file['path'] . ( ! empty( $parsed_file['query'] ) ? '?' . $parsed_file['query'] : '' );
366
-
367
- // check if file url is valid
368
- if ( $tweaked_url ) {
369
- $template_url = $tweaked_url;
370
- }
371
- }
372
- }
373
-
374
- return $template_url;
375
- }
376
-
377
- /**
378
- * Generate HTML iframe template for the AMP.
379
- *
380
- * @return mixed
381
- */
382
- public function generate_amp_template( $code = '', $lang = '' ) {
383
- if ( empty( $code ) )
384
- return false;
385
-
386
- $template_dir = IUBENDA_PLUGIN_PATH . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR;
387
- $template_file = $template_dir . ( ! empty( $lang ) && in_array( $lang, array_keys( iubenda()->languages ) ) ? 'amp' . '-' . $lang . '.html' : 'amp.html' );
388
- $html = $this->prepare_amp_template( $code );
389
-
390
- // bail if the template was not created properly
391
- if ( empty( $html ) )
392
- return false;
393
-
394
- $result = file_put_contents( $template_file, $html );
395
-
396
- return (bool) $result;
397
- }
398
-
399
  }
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 in Transitional mode.
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
+
43
+ // optional geo support
44
+ if ( iubenda()->multilang && ! empty( iubenda()->lang_current ) ) {
45
+ $code = iubenda()->options['cs']['code_' . iubenda()->lang_current];
46
+ } else {
47
+ $code = iubenda()->options['cs']['code_default'];
48
+ }
49
+
50
+ $configuration_raw = iubenda()->parse_configuration( $code );
51
+
52
+ if ( isset( $configuration_raw['gdprAppliesGlobally'] ) && ! $configuration_raw['gdprAppliesGlobally'] ) {
53
+ echo '
54
+ <script async custom-element="amp-geo" src="https://cdn.ampproject.org/v0/amp-geo-0.1.js"></script>';
55
+ }
56
+
57
+ // CSS style
58
+ echo '
59
+ <style amp-custom>
60
+ .popupOverlay {
61
+ position:fixed;
62
+ top: 0;
63
+ bottom: 0;
64
+ left: 0;
65
+ right: 0;
66
+ }
67
+ amp-iframe {
68
+ margin: 0;
69
+ }
70
+ amp-consent.amp-active {
71
+ position:fixed;
72
+ top: 0;
73
+ bottom: 0;
74
+ left: 0;
75
+ right: 0;
76
+ }
77
+ </style>';
78
+ }
79
+ }
80
+
81
+ /**
82
+ * Add AMP consent HTML to WP AMP plugin in Transitional mode.
83
+ *
84
+ * @return mixed
85
+ */
86
+ public function wp_footer_amp() {
87
+ if ( iubenda()->options['cs']['amp_support'] === false )
88
+ return;
89
+
90
+ if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() || ( function_exists( 'ampforwp_is_amp_endpoint' ) && ampforwp_is_amp_endpoint() ) ) {
91
+
92
+ // get code
93
+ if ( iubenda()->multilang && ! empty( iubenda()->lang_current ) ) {
94
+ $code = iubenda()->options['cs']['code_' . iubenda()->lang_current];
95
+ } else {
96
+ $code = iubenda()->options['cs']['code_default'];
97
+ }
98
+
99
+ $configuration = iubenda()->parse_configuration( $code );
100
+
101
+ if ( empty( $configuration ) )
102
+ return;
103
+
104
+ // local file
105
+ if ( iubenda()->options['cs']['amp_source'] === 'local' ) {
106
+ // multilang support
107
+ if ( iubenda()->multilang && ! empty( iubenda()->lang_current ) )
108
+ $template_url = $this->get_amp_template_url( iubenda()->lang_current );
109
+ else
110
+ $template_url = $this->get_amp_template_url();
111
+ // remote file
112
+ } else {
113
+ // multilang support
114
+ if ( iubenda()->multilang && ! empty( iubenda()->lang_current ) )
115
+ $template_url = esc_url( isset( iubenda()->options['cs']['amp_template'][iubenda()->lang_current] ) ? iubenda()->options['cs']['amp_template'][iubenda()->lang_current] : '' );
116
+ else
117
+ $template_url = esc_url( iubenda()->options['cs']['amp_template'] );
118
+ }
119
+
120
+ if ( empty( $template_url ) )
121
+ return;
122
+
123
+ echo '
124
+ <amp-consent id="myUserConsent" layout="nodisplay">
125
+ <script type="application/json">
126
+ {
127
+ "consentInstanceId": "consent' . $configuration['siteId'] . '",
128
+ "consentRequired": true,
129
+ "promptUI": "myConsentFlow"
130
+ }
131
+ </script>
132
+ <div id="myConsentFlow" class="popupOverlay">
133
+ <amp-iframe
134
+ layout="fill"
135
+ sandbox="allow-scripts allow-same-origin allow-popups allow-popups-to-escape-sandbox"
136
+ src="' . esc_url( $template_url ) . '">
137
+ <div placeholder>' . __( 'Loading', 'iubenda' ) . '</div>
138
+ </amp-iframe>
139
+ </div>
140
+ </amp-consent>';
141
+ }
142
+ }
143
+
144
+ /**
145
+ * Add scripts to AMP for WP plugin and WP AMP plugin in Standard mode.
146
+ *
147
+ * @return mixed
148
+ */
149
+ public function amp_post_template_data( $data ) {
150
+ if ( iubenda()->options['cs']['amp_support'] === false )
151
+ return $data;
152
+
153
+ if ( function_exists( 'ampforwp_is_amp_endpoint' ) && ampforwp_is_amp_endpoint() ) {
154
+ $data['amp_component_scripts'] = array_merge( $data['amp_component_scripts'],
155
+ array( 'amp-consent' => 'https://cdn.ampproject.org/v0/amp-consent-latest.js' )
156
+ );
157
+ $data['amp_component_scripts'] = array_merge( $data['amp_component_scripts'],
158
+ array( 'amp-iframe' => 'https://cdn.ampproject.org/v0/amp-iframe-latest.js' )
159
+ );
160
+ }
161
+
162
+ return $data;
163
+ }
164
+
165
+ /**
166
+ * Add CSS to AMP for WP plugin and WP AMP plugin in Standard mode.
167
+ *
168
+ * @return mixed
169
+ */
170
+ public function amp_post_template_css( $data ) {
171
+ if ( iubenda()->options['cs']['amp_support'] === false )
172
+ return;
173
+
174
+ echo '
175
+ .popupOverlay {
176
+ position:fixed;
177
+ top: 0;
178
+ bottom: 0;
179
+ left: 0;
180
+ right: 0;
181
+ }
182
+ amp-iframe {
183
+ margin: 0;
184
+ }
185
+ amp-consent.amp-active {
186
+ position:fixed;
187
+ top: 0;
188
+ bottom: 0;
189
+ left: 0;
190
+ right: 0;
191
+ }';
192
+ }
193
+
194
+ /**
195
+ * Block analytics in AMP for WP plugin.
196
+ *
197
+ * @return mixed
198
+ */
199
+ public function fix_analytics_amp_for_wp( $data ) {
200
+ if ( iubenda()->options['cs']['amp_support'] === false )
201
+ return $data;
202
+
203
+ global $redux_builder_amp;
204
+
205
+ if ( $redux_builder_amp == null ) {
206
+ $redux_builder_amp = get_option( 'redux_builder_amp', true );
207
+ }
208
+
209
+ // trick to block the analytics using global $redux_builder_amp variable
210
+ if ( ! iubendaParser::consent_given() )
211
+ $redux_builder_amp = true;
212
+
213
+ return $data;
214
+ }
215
+
216
+ /**
217
+ * Block analytics in WP AMP plugin.
218
+ *
219
+ * @return mixed
220
+ */
221
+ public function fix_analytics_wp_amp( $analytics_entries ) {
222
+ if ( iubenda()->options['cs']['amp_support'] === false )
223
+ return $analytics_entries;
224
+
225
+ // block the analytics using the entries filter hook
226
+ if ( ! iubendaParser::consent_given() && ! empty( $analytics_entries ) && is_array( $analytics_entries ) ) {
227
+ foreach ( $analytics_entries as $id => $entry ) {
228
+ $entry['attributes'] = ! empty( $entry['attributes'] ) ? $entry['attributes'] : array();
229
+
230
+ $analytics_entries[$id]['attributes'] = array_merge( array( 'data-block-on-consent' => '_till_accepted' ), $entry['attributes'] );
231
+ }
232
+ }
233
+
234
+ return $analytics_entries;
235
+ }
236
+
237
+ /**
238
+ * Prepare HTML iframe template for the AMP.
239
+ *
240
+ * @return mixed
241
+ */
242
+ public function prepare_amp_template( $code ) {
243
+ $html = '';
244
+
245
+ $configuration_raw = iubenda()->parse_configuration( $code );
246
+
247
+ if ( ! empty( $configuration_raw ) ) {
248
+ // get script
249
+ $script_src = ! empty( $configuration_raw['script'] ) ? $configuration_raw['script'] : '//cdn.iubenda.com/cs/iubenda_cs.js';
250
+
251
+ // remove from configuration
252
+ if ( isset( $configuration_raw['script'] ) )
253
+ unset( $configuration_raw['script'] );
254
+
255
+ // encode array
256
+ $configuration = json_encode( $configuration_raw );
257
+
258
+ // remove quotes
259
+ $configuration = preg_replace( '/"([a-zA-Z]+[a-zA-Z0-9]*)":/', '$1:', $configuration );
260
+ // replace brackets
261
+ $configuration = str_replace( array( '{', '}' ), '', $configuration );
262
+
263
+ $html .= '<!DOCTYPE html>
264
+ <html lang="' . $configuration_raw['lang'] . '">
265
+ <head>
266
+ <meta charset="UTF-8">
267
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
268
+ <meta name="robots" content="noindex">
269
+ <meta http-equiv="X-UA-Compatible" content="ie=edge">
270
+ <title>' . __( 'AMP Cookie Consent', 'iubenda' ) . '</title>
271
+ <script type="text/javascript">
272
+ var _iub = _iub || [];
273
+ _iub.csConfiguration = {
274
+ ';
275
+ // print configuration
276
+ $html .= $configuration . ',';
277
+ $html .= '
278
+ banner: {
279
+ position: \'float-bottom-center\',
280
+ acceptButtonDisplay: true,
281
+ customizeButtonDisplay: true,
282
+ rejectButtonDisplay: true,
283
+ backgroundOverlay: true
284
+ },
285
+ callback: { // Mandatory
286
+ onPreferenceExpressed: function(preference) {
287
+ var consentAction = \'reject\';
288
+ var consentObject = {
289
+ type: \'consent-response\'
290
+ };
291
+
292
+ if (preference && preference.consent) {
293
+ consentAction = \'accept\';
294
+ }
295
+
296
+ consentObject.action = consentAction;
297
+
298
+ if (_iub.cs.options.enableCMP) {
299
+ __cmp(\'getConsentData\', null, function(res) {
300
+ var consentString = res.consentData;
301
+
302
+ if (consentString.length <= 200) {
303
+ consentObject.info = consentString;
304
+ }
305
+
306
+ console.log(\'send consent-response\', consentAction, \'with CMP consent string\', consentString);
307
+
308
+ window.parent.postMessage(consentObject, \'*\');
309
+ });
310
+ }
311
+ else {
312
+ console.log(\'send consent-response\', consentAction);
313
+
314
+ window.parent.postMessage(consentObject, \'*\');
315
+ }
316
+ }
317
+ }
318
+ };
319
+ </script>
320
+ <script async src="' . $script_src . '"></script>
321
+ </head>
322
+ <body></body>
323
+ </html>';
324
+ }
325
+
326
+ return $html;
327
+ }
328
+
329
+ /**
330
+ * Get local file template url;
331
+ *
332
+ * @return string
333
+ */
334
+ public function get_amp_template_url( $template_lang = '' ) {
335
+ $template_url = '';
336
+ $template_lang = ! empty( $template_lang ) && is_string( $template_lang ) ? $template_lang : '';
337
+
338
+ // get basic site host and template file data
339
+ $file_url = ! empty( $template_lang ) ? IUBENDA_PLUGIN_URL . '/templates/amp' . '-' . $template_lang . '.html' : IUBENDA_PLUGIN_URL . '/templates/amp.html';
340
+ // $file_url = 'https://cdn.iubenda.com/cs/test/cs-for-amp.html'; // debug only
341
+ $parsed_site = parse_url( home_url() );
342
+ $parsed_file = parse_url( $file_url );
343
+ $site_host = $parsed_site['host'] !== 'localhost' ? iubenda()->domain( $parsed_site['host'] ) : 'localhost';
344
+ $file_host = $parsed_file['host'] !== 'localhost' ? iubenda()->domain( $parsed_file['host'] ) : 'localhost';
345
+ $is_localhost = (bool) ( $site_host == 'localhost' );
346
+ $is_subdomain = ! $is_localhost ? (bool) ( $parsed_file['host'] !== $file_host ) : false;
347
+
348
+ // check if file host and server host match
349
+ // if not, we're good to go
350
+ if ( $site_host !== $file_host ) {
351
+ $template_url = $file_url;
352
+ // if are located on same host do additional tweaks
353
+ } else {
354
+ // all ok if we're on different subdomains
355
+ if ( $parsed_site['host'] !== $parsed_file['host'] )
356
+ $template_url = $file_url;
357
+ // same hosts, let's tweak the http/https
358
+ else {
359
+ $has_www = strpos( $parsed_file['host'], 'www.' ) === 0;
360
+
361
+ // add or remove www from url string to make iframe url pass AMP validation
362
+ $tweaked_host = ! $is_localhost && ( ! $is_subdomain || $has_www ) ? ( ! $has_www ? 'www.' . $parsed_file['host'] : preg_replace( '/^www\./i', '', $parsed_file['host'] ) ) : $parsed_file['host'];
363
+
364
+ // generate new url
365
+ $tweaked_url = $parsed_file['scheme'] . '://' . $tweaked_host . ( isset( $parsed_file['port'] ) ? ':' . $parsed_file['port'] : '' ) . $parsed_file['path'] . ( ! empty( $parsed_file['query'] ) ? '?' . $parsed_file['query'] : '' );
366
+
367
+ // check if file url is valid
368
+ if ( $tweaked_url ) {
369
+ $template_url = $tweaked_url;
370
+ }
371
+ }
372
+ }
373
+
374
+ return $template_url;
375
+ }
376
+
377
+ /**
378
+ * Generate HTML iframe template for the AMP.
379
+ *
380
+ * @return mixed
381
+ */
382
+ public function generate_amp_template( $code = '', $lang = '' ) {
383
+ if ( empty( $code ) )
384
+ return false;
385
+
386
+ $template_dir = IUBENDA_PLUGIN_PATH . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR;
387
+ $template_file = $template_dir . ( ! empty( $lang ) && in_array( $lang, array_keys( iubenda()->languages ) ) ? 'amp' . '-' . $lang . '.html' : 'amp.html' );
388
+ $html = $this->prepare_amp_template( $code );
389
+
390
+ // bail if the template was not created properly
391
+ if ( empty( $html ) )
392
+ return false;
393
+
394
+ $result = file_put_contents( $template_file, $html );
395
+
396
+ return (bool) $result;
397
+ }
398
+
399
  }
includes/forms-list-table.php CHANGED
@@ -1,367 +1,367 @@
1
- <?php
2
- // exit if accessed directly
3
- if ( ! defined( 'ABSPATH' ) )
4
- exit;
5
-
6
- /**
7
- * iubenda_List_Table_Forms class.
8
- *
9
- * @class iubenda_List_Table_Forms
10
- */
11
- class iubenda_List_Table_Forms extends WP_List_Table {
12
-
13
- public $items;
14
- public $extra_items;
15
- public $base_url;
16
-
17
- /**
18
- * Class constructor.
19
- */
20
- public function __construct() {
21
- global $status, $page;
22
-
23
- // set parent defaults
24
- parent::__construct( array(
25
- 'ajax' => false
26
- ) );
27
-
28
- $this->base_url = esc_url_raw( add_query_arg( array( 'tab' => 'cons' ), iubenda()->base_url ) );
29
- }
30
-
31
-
32
-
33
- /**
34
- * Prepare the items for the table to process.
35
- */
36
- public function prepare_items() {
37
- if ( ! empty( $_GET['status'] ) && array_key_exists( $_GET['status'], iubenda()->forms->statuses ) )
38
- $status = $_GET['status'];
39
- else
40
- $status = '';
41
-
42
- $orderby = ( isset( $_GET['orderby'] ) ) ? esc_attr( $_GET['orderby'] ) : 'date';
43
- $order = ( isset( $_GET['order'] ) ) && in_array( $_GET['order'], array( 'asc', 'desc' ) ) ? esc_attr( $_GET['order'] ) : 'desc';
44
-
45
- $per_page = 20;
46
-
47
- if ( isset( $_GET['number'] ) ) {
48
- $number = (int) $_GET['number'];
49
- } else {
50
- $number = $per_page + min( 8, $per_page ); // grab a few extra
51
- }
52
- $page = $this->get_pagenum();
53
-
54
- $args = array(
55
- 'orderby' => $orderby,
56
- 'order' => $order,
57
- 'offset' => 0,
58
- 'number' => 0,
59
- 'post_status' => $status
60
- );
61
-
62
- $items = iubenda()->forms->get_forms( $args );
63
-
64
- // echo '<pre>'; print_r( $items ); echo '</pre>';
65
-
66
- if ( is_array( $items ) ) {
67
- $this->items = array_slice( $items, 0, $per_page );
68
- $this->extra_items = array_slice( $items, $per_page );
69
- }
70
-
71
- // echo '<pre>'; print_r( $this->extra_items ); echo '</pre>';
72
-
73
- $this->set_pagination_args( array(
74
- 'total_items' => count( $items ),
75
- 'per_page' => $per_page,
76
- ) );
77
-
78
- $columns = $this->get_columns();
79
- $hidden = array();
80
- $sortable = $this->get_sortable_columns();
81
- $this->_column_headers = array( $columns, $hidden, $sortable );
82
- }
83
-
84
- /**
85
- * Override the parent columns method. Defines the columns to use in your listing table.
86
- *
87
- * @return array
88
- */
89
- public function get_columns() {
90
- $columns = array(
91
- 'cb' => '<input type="checkbox"/>',
92
- 'title' => __( 'Form Title', 'iubenda' ),
93
- 'ID' => __( 'Form ID', 'iubenda' ),
94
- 'source' => __( 'Form Source', 'iubenda' ),
95
- 'fields' => __( 'Fields', 'iubenda' ),
96
- 'date' => __( 'Date', 'iubenda' ),
97
- );
98
-
99
- return $columns;
100
- }
101
-
102
- /**
103
- * Define the sortable columns.
104
- *
105
- * @return array
106
- */
107
- public function get_sortable_columns() {
108
- $columns = array(
109
- 'title' => array( 'name', true )
110
- );
111
- return $columns;
112
- }
113
-
114
- /**
115
- * Handle single row content.
116
- *
117
- * @param array $item
118
- * @return mixed
119
- */
120
- public function single_row( $item ) {
121
- $classes = array();
122
- $classes[] = 'item-' . $item->ID;
123
- ?>
124
- <tr id="item-<?php echo $item->ID; ?>" class="<?php echo implode( ' ', $classes ); ?>">
125
- <?php $this->single_row_columns( $item ); ?>
126
- </tr>
127
- <?php
128
- }
129
-
130
- /**
131
- * Get the name of the default primary column.
132
- *
133
- * @return string Name of the default primary column, in this case, 'title'.
134
- */
135
- public function get_default_primary_column_name() {
136
- return 'title';
137
- }
138
-
139
- /**
140
- * Generate and display row actions links.
141
- *
142
- * @param object $item
143
- * @param string $column_name
144
- * @param string $primary
145
- * @return string|void
146
- */
147
- public function handle_row_actions( $item, $column_name, $primary ) {
148
- if ( $column_name !== 'title' ) {
149
- return '';
150
- }
151
-
152
- $output = '';
153
-
154
- $del_nonce = esc_html( '_wpnonce=' . wp_create_nonce( "delete-form_{$item->ID}" ) );
155
- $url = add_query_arg( array( 'form_id' => $item->ID ), $this->base_url );
156
- $edit_url = add_query_arg( array( 'action' => 'edit' ), $url );
157
- $delete_url = add_query_arg( array( 'action' => 'delete' ), $url ) . "&$del_nonce";
158
-
159
- // preorder it: View | Approve | Unapprove | Delete
160
- $actions = array(
161
- 'view' => '',
162
- 'delete' => ''
163
- );
164
-
165
- $actions['view'] = "<a href='$edit_url' aria-label='" . esc_attr__( 'Edit this form', 'iubenda' ) . "'>" . __( 'Edit' ) . '</a>';
166
- $actions['delete'] = "<a href='$delete_url' aria-label='" . esc_attr__( 'Delete this form', 'iubenda' ) . "'>" . __( 'Delete', 'iubenda' ) . '</a>';
167
-
168
- $i = 0;
169
- $output .= '<div class="row-actions">';
170
- foreach ( $actions as $action => $link ) {
171
- ++ $i;
172
- $sep = ( 1 === $i ) ? $sep = '' : $sep = ' | ';
173
- $output .= '<span class="' . ( $action === 'delete' ? 'delete delete-form' : $action ) . '">' . $sep . $link . '</span>';
174
- }
175
- $output .= '</div>';
176
-
177
- return $output;
178
- }
179
-
180
- /**
181
- * Define what data to show on each column of the table.
182
- *
183
- * @param array $item
184
- * @param string $column_name
185
- * @return mixed
186
- */
187
- public function column_default( $item, $column_name ) {
188
- $output = '';
189
-
190
- if ( ! empty( $_GET['status'] ) && array_key_exists( $_GET['status'], iubenda()->forms->statuses ) )
191
- $status = $_GET['status'];
192
- else
193
- $status = '';
194
-
195
- // print_r( $item );
196
-
197
- // get columns content
198
- switch ( $column_name ) {
199
- case 'ID':
200
- $output = $item->ID;
201
- break;
202
- case 'title':
203
- $output = '<strong>' . ( current_user_can( 'edit_post', $item->ID ) ? '<a href="' . esc_url_raw( add_query_arg( array( 'form_id' => $item->ID, 'action' => 'edit' ), $this->base_url ) ) . '">' . $item->post_title . '</a>' : $item->post_title );
204
-
205
- if ( ! $status ) {
206
- if ( in_array( $item->post_status, array( 'publish', 'needs_update' ) ) ) {
207
- $output .= ' &mdash; ';
208
- $output .= '<span class="post-state to-map-state">' . iubenda()->forms->statuses[$item->post_status] . '</span>';
209
- }
210
- }
211
-
212
- $output .= '</strong>';
213
-
214
- break;
215
- case 'source':
216
- $output = array_key_exists( $item->form_source, iubenda()->forms->sources ) ? iubenda()->forms->sources[$item->form_source] : '&#8212;';
217
- break;
218
- case 'fields':
219
- $output = count( $item->form_fields );
220
- break;
221
- case 'date':
222
- $output = date_i18n( $item->post_date );
223
- break;
224
- default:
225
- break;
226
- }
227
-
228
- return $output;
229
- }
230
-
231
- /**
232
- * Display checkboxex callback.
233
- *
234
- * @return string
235
- */
236
- public function column_cb( $item ) {
237
- return sprintf(
238
- '<input type="checkbox" name="%1$s[]" value="%2$s" />', 'form', $item->ID
239
- );
240
- }
241
-
242
- /**
243
- * Generate the table navigation above or below the table
244
- *
245
- * @since 3.1.0
246
- * @param string $which
247
- */
248
- protected function display_tablenav( $which ) {
249
- ?>
250
- <div class="tablenav <?php echo esc_attr( $which ); ?>">
251
- <?php
252
- // $this->extra_tablenav( $which );
253
- $this->pagination( $which );
254
- ?>
255
-
256
- <br class="clear" />
257
- </div>
258
- <?php
259
- }
260
-
261
- /**
262
- * @param string $which
263
- */
264
- protected function extra_tablenav( $which ) {
265
- ?>
266
- <div class="alignleft actions">
267
- <?php
268
- if ( 'top' === $which ) {
269
- ob_start();
270
-
271
- $this->sources_dropdown();
272
-
273
- $output = ob_get_clean();
274
-
275
- if ( ! empty( $output ) ) {
276
- echo $output;
277
- submit_button( __( 'Filter' ), '', 'filter_action', false, array( 'id' => 'post-query-submit' ) );
278
- }
279
- }
280
- ?>
281
- </div>
282
- <?php
283
- }
284
-
285
- /**
286
- * Displays a sources drop-down for filtering on the list table.
287
- *
288
- * @return mixed
289
- */
290
- protected function sources_dropdown() {
291
- if ( ! empty( iubenda()->forms->sources ) ) {
292
- $current = ! empty( $_GET['source'] ) && in_array( $_GET['source'], iubenda()->forms->sources ) ? esc_attr( $_GET['source'] ) : '';
293
- echo '
294
- <label class="screen-reader-text" for="cat">' . __( 'Filter by source', 'iubenda' ) . '</label>
295
- <select name="source" id="filter-by-source">
296
- <option ' . selected( '', $current, false ) . 'value="">' . __( 'All form sources', 'iubenda' ) . '</option>';
297
- foreach ( iubenda()->forms->sources as $key => $label ) {
298
- echo '
299
- <option ' . selected( $key, $current, false ) . 'value="' . $key . '">' . $label . '</option>';
300
- }
301
-
302
- echo '</select>';
303
- }
304
- }
305
-
306
- /**
307
- * Display views.
308
- *
309
- * @return array
310
- */
311
- public function get_views() {
312
- if ( ! empty( $_GET['status'] ) && array_key_exists( $_GET['status'], iubenda()->forms->statuses ) )
313
- $status = $_GET['status'];
314
- else
315
- $status = '';
316
-
317
- $orderby = ( isset( $_GET['orderby'] ) ) ? esc_attr( $_GET['orderby'] ) : '';
318
- $order = ( isset( $_GET['order'] ) ) ? esc_attr( $_GET['order'] ) : '';
319
-
320
- $per_page = 20;
321
-
322
- if ( isset( $_GET['number'] ) ) {
323
- $number = (int) $_GET['number'];
324
- } else {
325
- $number = $per_page + min( 8, $per_page ); // grab a few extra
326
- }
327
- $page = $this->get_pagenum();
328
- $items_total = 0;
329
-
330
- $args = array(
331
- 'orderby' => $orderby,
332
- 'order' => $order,
333
- 'offset' => 0,
334
- 'number' => 0
335
- );
336
-
337
- foreach ( iubenda()->forms->statuses as $key => $view ) {
338
- $args['post_status'] = $key;
339
-
340
- $items = iubenda()->forms->get_forms( $args );
341
-
342
- $items_count[$key] = count( $items );
343
- $items_total = $items_total + $items_count[$key];
344
- }
345
-
346
- $views = $items_total > 0 ? array(
347
- 'all' => '<a href="' . $this->base_url . '"' . ($status === '' ? ' class="current"' : '') . '>' . esc_html__( 'All' ) . ' <span class="count">(' . $items_total . ')</span></a>'
348
- ) : '';
349
-
350
- foreach ( iubenda()->forms->statuses as $key => $view ) {
351
- if ( (int) $items_count[$key] > 0 )
352
- $views[$key] = '<a href="' . esc_url_raw( add_query_arg( array( 'status' => $key ), $this->base_url ) ) . '" ' . ($status === $key ? ' class="current"' : '') . '>' . sprintf( _n( '%1$s <span class="count">(%2$s)</span>', '%1$s <span class="count">(%2$s)</span>', $items_count[$key], 'iubenda' ), $view, $items_count[$key] ) . '</a>';
353
- }
354
-
355
- return $views;
356
- }
357
-
358
- /**
359
- * Display empty result.
360
- *
361
- * @return string
362
- */
363
- public function no_items() {
364
- echo __( 'No forms found.', 'iubenda' );
365
- }
366
-
367
  }
1
+ <?php
2
+ // exit if accessed directly
3
+ if ( ! defined( 'ABSPATH' ) )
4
+ exit;
5
+
6
+ /**
7
+ * iubenda_List_Table_Forms class.
8
+ *
9
+ * @class iubenda_List_Table_Forms
10
+ */
11
+ class iubenda_List_Table_Forms extends WP_List_Table {
12
+
13
+ public $items;
14
+ public $extra_items;
15
+ public $base_url;
16
+
17
+ /**
18
+ * Class constructor.
19
+ */
20
+ public function __construct() {
21
+ global $status, $page;
22
+
23
+ // set parent defaults
24
+ parent::__construct( array(
25
+ 'ajax' => false
26
+ ) );
27
+
28
+ $this->base_url = esc_url_raw( add_query_arg( array( 'tab' => 'cons' ), iubenda()->base_url ) );
29
+ }
30
+
31
+
32
+
33
+ /**
34
+ * Prepare the items for the table to process.
35
+ */
36
+ public function prepare_items() {
37
+ if ( ! empty( $_GET['status'] ) && array_key_exists( $_GET['status'], iubenda()->forms->statuses ) )
38
+ $status = $_GET['status'];
39
+ else
40
+ $status = '';
41
+
42
+ $orderby = ( isset( $_GET['orderby'] ) ) ? esc_attr( $_GET['orderby'] ) : 'date';
43
+ $order = ( isset( $_GET['order'] ) ) && in_array( $_GET['order'], array( 'asc', 'desc' ) ) ? esc_attr( $_GET['order'] ) : 'desc';
44
+
45
+ $per_page = 20;
46
+
47
+ if ( isset( $_GET['number'] ) ) {
48
+ $number = (int) $_GET['number'];
49
+ } else {
50
+ $number = $per_page + min( 8, $per_page ); // grab a few extra
51
+ }
52
+ $page = $this->get_pagenum();
53
+
54
+ $args = array(
55
+ 'orderby' => $orderby,
56
+ 'order' => $order,
57
+ 'offset' => 0,
58
+ 'number' => 0,
59
+ 'post_status' => $status
60
+ );
61
+
62
+ $items = iubenda()->forms->get_forms( $args );
63
+
64
+ // echo '<pre>'; print_r( $items ); echo '</pre>';
65
+
66
+ if ( is_array( $items ) ) {
67
+ $this->items = array_slice( $items, 0, $per_page );
68
+ $this->extra_items = array_slice( $items, $per_page );
69
+ }
70
+
71
+ // echo '<pre>'; print_r( $this->extra_items ); echo '</pre>';
72
+
73
+ $this->set_pagination_args( array(
74
+ 'total_items' => count( $items ),
75
+ 'per_page' => $per_page,
76
+ ) );
77
+
78
+ $columns = $this->get_columns();
79
+ $hidden = array();
80
+ $sortable = $this->get_sortable_columns();
81
+ $this->_column_headers = array( $columns, $hidden, $sortable );
82
+ }
83
+
84
+ /**
85
+ * Override the parent columns method. Defines the columns to use in your listing table.
86
+ *
87
+ * @return array
88
+ */
89
+ public function get_columns() {
90
+ $columns = array(
91
+ 'cb' => '<input type="checkbox"/>',
92
+ 'title' => __( 'Form Title', 'iubenda' ),
93
+ 'ID' => __( 'Form ID', 'iubenda' ),
94
+ 'source' => __( 'Form Source', 'iubenda' ),
95
+ 'fields' => __( 'Fields', 'iubenda' ),
96
+ 'date' => __( 'Date', 'iubenda' ),
97
+ );
98
+
99
+ return $columns;
100
+ }
101
+
102
+ /**
103
+ * Define the sortable columns.
104
+ *
105
+ * @return array
106
+ */
107
+ public function get_sortable_columns() {
108
+ $columns = array(
109
+ 'title' => array( 'name', true )
110
+ );
111
+ return $columns;
112
+ }
113
+
114
+ /**
115
+ * Handle single row content.
116
+ *
117
+ * @param array $item
118
+ * @return mixed
119
+ */
120
+ public function single_row( $item ) {
121
+ $classes = array();
122
+ $classes[] = 'item-' . $item->ID;
123
+ ?>
124
+ <tr id="item-<?php echo $item->ID; ?>" class="<?php echo implode( ' ', $classes ); ?>">
125
+ <?php $this->single_row_columns( $item ); ?>
126
+ </tr>
127
+ <?php
128
+ }
129
+
130
+ /**
131
+ * Get the name of the default primary column.
132
+ *
133
+ * @return string Name of the default primary column, in this case, 'title'.
134
+ */
135
+ public function get_default_primary_column_name() {
136
+ return 'title';
137
+ }
138
+
139
+ /**
140
+ * Generate and display row actions links.
141
+ *
142
+ * @param object $item
143
+ * @param string $column_name
144
+ * @param string $primary
145
+ * @return string|void
146
+ */
147
+ public function handle_row_actions( $item, $column_name, $primary ) {
148
+ if ( $column_name !== 'title' ) {
149
+ return '';
150
+ }
151
+
152
+ $output = '';
153
+
154
+ $del_nonce = esc_html( '_wpnonce=' . wp_create_nonce( "delete-form_{$item->ID}" ) );
155
+ $url = add_query_arg( array( 'form_id' => $item->ID ), $this->base_url );
156
+ $edit_url = add_query_arg( array( 'action' => 'edit' ), $url );
157
+ $delete_url = add_query_arg( array( 'action' => 'delete' ), $url ) . "&$del_nonce";
158
+
159
+ // preorder it: View | Approve | Unapprove | Delete
160
+ $actions = array(
161
+ 'view' => '',
162
+ 'delete' => ''
163
+ );
164
+
165
+ $actions['view'] = "<a href='$edit_url' aria-label='" . esc_attr__( 'Edit this form', 'iubenda' ) . "'>" . __( 'Edit' ) . '</a>';
166
+ $actions['delete'] = "<a href='$delete_url' aria-label='" . esc_attr__( 'Delete this form', 'iubenda' ) . "'>" . __( 'Delete', 'iubenda' ) . '</a>';
167
+
168
+ $i = 0;
169
+ $output .= '<div class="row-actions">';
170
+ foreach ( $actions as $action => $link ) {
171
+ ++ $i;
172
+ $sep = ( 1 === $i ) ? $sep = '' : $sep = ' | ';
173
+ $output .= '<span class="' . ( $action === 'delete' ? 'delete delete-form' : $action ) . '">' . $sep . $link . '</span>';
174
+ }
175
+ $output .= '</div>';
176
+
177
+ return $output;
178
+ }
179
+
180
+ /**
181
+ * Define what data to show on each column of the table.
182
+ *
183
+ * @param array $item
184
+ * @param string $column_name
185
+ * @return mixed
186
+ */
187
+ public function column_default( $item, $column_name ) {
188
+ $output = '';
189
+
190
+ if ( ! empty( $_GET['status'] ) && array_key_exists( $_GET['status'], iubenda()->forms->statuses ) )
191
+ $status = $_GET['status'];
192
+ else
193
+ $status = '';
194
+
195
+ // print_r( $item );
196
+
197
+ // get columns content
198
+ switch ( $column_name ) {
199
+ case 'ID':
200
+ $output = $item->ID;
201
+ break;
202
+ case 'title':
203
+ $output = '<strong>' . ( current_user_can( 'edit_post', $item->ID ) ? '<a href="' . esc_url_raw( add_query_arg( array( 'form_id' => $item->ID, 'action' => 'edit' ), $this->base_url ) ) . '">' . $item->post_title . '</a>' : $item->post_title );
204
+
205
+ if ( ! $status ) {
206
+ if ( in_array( $item->post_status, array( 'publish', 'needs_update' ) ) ) {
207
+ $output .= ' &mdash; ';
208
+ $output .= '<span class="post-state to-map-state">' . iubenda()->forms->statuses[$item->post_status] . '</span>';
209
+ }
210
+ }
211
+
212
+ $output .= '</strong>';
213
+
214
+ break;
215
+ case 'source':
216
+ $output = array_key_exists( $item->form_source, iubenda()->forms->sources ) ? iubenda()->forms->sources[$item->form_source] : '&#8212;';
217
+ break;
218
+ case 'fields':
219
+ $output = count( $item->form_fields );
220
+ break;
221
+ case 'date':
222
+ $output = date_i18n( $item->post_date );
223
+ break;
224
+ default:
225
+ break;
226
+ }
227
+
228
+ return $output;
229
+ }
230
+
231
+ /**
232
+ * Display checkboxex callback.
233
+ *
234
+ * @return string
235
+ */
236
+ public function column_cb( $item ) {
237
+ return sprintf(
238
+ '<input type="checkbox" name="%1$s[]" value="%2$s" />', 'form', $item->ID
239
+ );
240
+ }
241
+
242
+ /**
243
+ * Generate the table navigation above or below the table
244
+ *
245
+ * @since 3.1.0
246
+ * @param string $which
247
+ */
248
+ protected function display_tablenav( $which ) {
249
+ ?>
250
+ <div class="tablenav <?php echo esc_attr( $which ); ?>">
251
+ <?php
252
+ // $this->extra_tablenav( $which );
253
+ $this->pagination( $which );
254
+ ?>
255
+
256
+ <br class="clear" />
257
+ </div>
258
+ <?php
259
+ }
260
+
261
+ /**
262
+ * @param string $which
263
+ */
264
+ protected function extra_tablenav( $which ) {
265
+ ?>
266
+ <div class="alignleft actions">
267
+ <?php
268
+ if ( 'top' === $which ) {
269
+ ob_start();
270
+
271
+ $this->sources_dropdown();
272
+
273
+ $output = ob_get_clean();
274
+
275
+ if ( ! empty( $output ) ) {
276
+ echo $output;
277
+ submit_button( __( 'Filter' ), '', 'filter_action', false, array( 'id' => 'post-query-submit' ) );
278
+ }
279
+ }
280
+ ?>
281
+ </div>
282
+ <?php
283
+ }
284
+
285
+ /**
286
+ * Displays a sources drop-down for filtering on the list table.
287
+ *
288
+ * @return mixed
289
+ */
290
+ protected function sources_dropdown() {
291
+ if ( ! empty( iubenda()->forms->sources ) ) {
292
+ $current = ! empty( $_GET['source'] ) && in_array( $_GET['source'], iubenda()->forms->sources ) ? esc_attr( $_GET['source'] ) : '';
293
+ echo '
294
+ <label class="screen-reader-text" for="cat">' . __( 'Filter by source', 'iubenda' ) . '</label>
295
+ <select name="source" id="filter-by-source">
296
+ <option ' . selected( '', $current, false ) . 'value="">' . __( 'All form sources', 'iubenda' ) . '</option>';
297
+ foreach ( iubenda()->forms->sources as $key => $label ) {
298
+ echo '
299
+ <option ' . selected( $key, $current, false ) . 'value="' . $key . '">' . $label . '</option>';
300
+ }
301
+
302
+ echo '</select>';
303
+ }
304
+ }
305
+
306
+ /**
307
+ * Display views.
308
+ *
309
+ * @return array
310
+ */
311
+ public function get_views() {
312
+ if ( ! empty( $_GET['status'] ) && array_key_exists( $_GET['status'], iubenda()->forms->statuses ) )
313
+ $status = $_GET['status'];
314
+ else
315
+ $status = '';
316
+
317
+ $orderby = ( isset( $_GET['orderby'] ) ) ? esc_attr( $_GET['orderby'] ) : '';
318
+ $order = ( isset( $_GET['order'] ) ) ? esc_attr( $_GET['order'] ) : '';
319
+
320
+ $per_page = 20;
321
+
322
+ if ( isset( $_GET['number'] ) ) {
323
+ $number = (int) $_GET['number'];
324
+ } else {
325
+ $number = $per_page + min( 8, $per_page ); // grab a few extra
326
+ }
327
+ $page = $this->get_pagenum();
328
+ $items_total = 0;
329
+
330
+ $args = array(
331
+ 'orderby' => $orderby,
332
+ 'order' => $order,
333
+ 'offset' => 0,
334
+ 'number' => 0
335
+ );
336
+
337
+ foreach ( iubenda()->forms->statuses as $key => $view ) {
338
+ $args['post_status'] = $key;
339
+
340
+ $items = iubenda()->forms->get_forms( $args );
341
+
342
+ $items_count[$key] = count( $items );
343
+ $items_total = $items_total + $items_count[$key];
344
+ }
345
+
346
+ $views = $items_total > 0 ? array(
347
+ 'all' => '<a href="' . $this->base_url . '"' . ($status === '' ? ' class="current"' : '') . '>' . esc_html__( 'All' ) . ' <span class="count">(' . $items_total . ')</span></a>'
348
+ ) : '';
349
+
350
+ foreach ( iubenda()->forms->statuses as $key => $view ) {
351
+ if ( (int) $items_count[$key] > 0 )
352
+ $views[$key] = '<a href="' . esc_url_raw( add_query_arg( array( 'status' => $key ), $this->base_url ) ) . '" ' . ($status === $key ? ' class="current"' : '') . '>' . sprintf( _n( '%1$s <span class="count">(%2$s)</span>', '%1$s <span class="count">(%2$s)</span>', $items_count[$key], 'iubenda' ), $view, $items_count[$key] ) . '</a>';
353
+ }
354
+
355
+ return $views;
356
+ }
357
+
358
+ /**
359
+ * Display empty result.
360
+ *
361
+ * @return string
362
+ */
363
+ public function no_items() {
364
+ echo __( 'No forms found.', 'iubenda' );
365
+ }
366
+
367
  }
includes/forms.php CHANGED
@@ -1,969 +1,969 @@
1
- <?php
2
- // exit if accessed directly
3
- if ( ! defined( 'ABSPATH' ) )
4
- exit;
5
-
6
- /**
7
- * iubenda_Forms class.
8
- *
9
- * @class iubenda_Forms
10
- */
11
- class iubenda_Forms {
12
-
13
- public $sources = array();
14
- public $statuses = array();
15
-
16
- /**
17
- * Class constructor.
18
- */
19
- public function __construct() {
20
- // actions
21
- add_action( 'plugins_loaded', array( $this, 'init' ) );
22
- add_action( 'init', array( $this, 'register_post_type' ) );
23
- add_action( 'init', array( $this, 'register_post_status' ) );
24
- add_action( 'wp_enqueue_scripts', array( $this, 'wp_enqueue_scripts' ) );
25
- }
26
-
27
- /**
28
- * Initialize forms data.
29
- *
30
- * @return void
31
- */
32
- public function init() {
33
- // WOrdPress commenting form
34
- $this->sources['wp_comment_form'] = 'WordPress Comment';
35
-
36
- // check if Contact Form 7 is active
37
- if ( class_exists( 'WPCF7' ) ) {
38
- $this->sources['wpcf7'] = 'Contact Form 7';
39
- }
40
-
41
- // check if WP Forms is active
42
- if ( function_exists( 'wpforms' ) ) {
43
- $this->sources['wpforms'] = 'WP Forms';
44
- }
45
-
46
- // check if EooCommerce is active
47
- if ( function_exists( 'WC' ) ) {
48
- $this->sources['woocommerce'] = 'WooCommerce Checkout';
49
- }
50
-
51
- $this->sources = apply_filters( 'iub_supported_form_sources', $this->sources );
52
-
53
- $this->statuses = array(
54
- 'publish' => _x( 'To Map', 'post status', 'iubenda' ),
55
- 'mapped' => _x( 'Mapped', 'post status', 'iubenda' ),
56
- 'needs_update' => _x( 'Needs Update', 'post status', 'iubenda' ),
57
- // 'trash' => _x( 'Ignored', 'post status', 'iubenda' )
58
- );
59
- }
60
-
61
- /**
62
- * Enqueue frontend script.
63
- */
64
- public function wp_enqueue_scripts() {
65
- if ( ! empty( iubenda()->options['cons']['public_api_key'] ) ) {
66
- wp_register_script( 'iubenda-forms', IUBENDA_PLUGIN_URL . '/js/frontend.js', array( 'jquery' ), iubenda()->version, true );
67
-
68
- $args = array();
69
-
70
- $form_args = array(
71
- 'post_status' => array( 'mapped', 'needs_update' )
72
- );
73
-
74
- $forms = $this->get_forms( $form_args );
75
-
76
- // echo '<pre>'; print_r( $forms ); echo '</pre>';
77
-
78
- if ( ! empty( $forms ) ) {
79
- // required form parameters
80
- $form_parameters = array(
81
- 'subject',
82
- 'preferences',
83
- 'exclude',
84
- 'legal_notices'
85
- );
86
- // loop through forms
87
- foreach ( $forms as $form ) {
88
- // bail if user is logged in and source is WP comment form
89
- if ( $form->form_source == 'wp_comment_form' && is_user_logged_in() )
90
- continue;
91
-
92
- // we need unique identifier for the html form
93
- // by default it's object id, used in form html id
94
- $args[$form->form_source][$form->object_id] = array();
95
-
96
- foreach ( $form_parameters as $parameter ) {
97
- $parameter_name = 'form_' . $parameter;
98
- $parameter_value = ! empty( $form->$parameter_name ) ? $form->$parameter_name : '';
99
-
100
- // echo '<pre>'; print_r( $parameter_value ); echo '</pre>';
101
-
102
- switch ( $parameter ) {
103
- case 'legal_notices' :
104
- if ( $parameter_value && is_array( $parameter_value ) ) {
105
- foreach( $parameter_value as $value ) {
106
- $args[$form->form_source][$form->object_id]['consent']['legal_notices'][] = array( 'identifier' => $value );
107
- }
108
- }
109
- break;
110
- default :
111
- if ( $parameter_value ) {
112
- switch ( $form->form_source ) {
113
- case 'wpforms' :
114
- // replace integers with field names
115
- foreach ( $parameter_value as $index => $parameter_item ) {
116
- $parameter_value[$index] = $form->form_fields[$parameter_item]['name'];
117
- }
118
- $args[$form->form_source][$form->object_id]['form']['map'][$parameter] = $parameter_value;
119
- break;
120
- default :
121
- $args[$form->form_source][$form->object_id]['form']['map'][$parameter] = $parameter_value;
122
- break;
123
- }
124
- }
125
- break;
126
- }
127
- }
128
- }
129
- }
130
-
131
- // echo '<pre>'; print_r( $args ); echo '</pre>'; exit;
132
-
133
- wp_localize_script(
134
- 'iubenda-forms',
135
- 'iubForms',
136
- json_encode( $args )
137
- );
138
-
139
- wp_enqueue_script( 'iubenda-forms' );
140
- }
141
- }
142
-
143
- /**
144
- * Register iubenda form post type.
145
- */
146
- public function register_post_type() {
147
- register_post_type( 'iubenda_form', array(
148
- 'labels' => array(
149
- 'name' => __( 'Forms', 'iubenda' ),
150
- 'singular_name' => __( 'Form', 'iubenda' ),
151
- ),
152
- 'rewrite' => false,
153
- 'query_var' => false,
154
- 'public' => false,
155
- 'capability_type' => 'page'
156
- ) );
157
- }
158
-
159
- /**
160
- * Register iubenda form post status.
161
- */
162
- public function register_post_status() {
163
- foreach ( $this->statuses as $name => $label ) {
164
- if ( $name === 'publish' )
165
- continue;
166
-
167
- register_post_status( $name, array(
168
- 'label' => $label,
169
- 'public' => true,
170
- 'exclude_from_search' => false,
171
- 'show_in_admin_all_list' => true,
172
- 'show_in_admin_status_list' => true,
173
- 'post_type' => array( 'iubenda_form' ),
174
- // 'label_count' => _n_noop( 'Mapped <span class="count">(%s)</span>', 'Mapped <span class="count">(%s)</span>', 'iubenda' ),
175
- ) );
176
- }
177
-
178
- }
179
-
180
- /**
181
- * Get iubenda forms function.
182
- *
183
- * @param type $args
184
- * @return array
185
- */
186
- public function get_forms( $args = array() ) {
187
- $defaults = array(
188
- 'post_status' => 'any',
189
- 'posts_per_page' => -1,
190
- 'offset' => 0,
191
- 'orderby' => 'ID',
192
- 'order' => 'ASC',
193
- 'form_source' => 'any'
194
- );
195
-
196
- $args = wp_parse_args( $args, $defaults );
197
-
198
- $args['post_type'] = 'iubenda_form';
199
-
200
- // specific sources only
201
- if ( $args['form_source'] != 'any' && ( is_string( $args['form_source'] ) || is_array( $args['form_source'] ) ) ) {
202
- $args['meta_query'] = array(
203
- array(
204
- 'key' => '_iub_form_source',
205
- 'value' => $args['form_source'],
206
- 'compare' => 'IN',
207
- ),
208
- );
209
- }
210
-
211
- $q = new WP_Query();
212
-
213
- $posts = $q->query( $args );
214
-
215
- $metakeys = array(
216
- 'form_source',
217
- 'object_type',
218
- 'object_id',
219
- 'form_fields',
220
- 'form_subject',
221
- 'form_preferences',
222
- 'form_exclude',
223
- 'form_legal_notices'
224
- );
225
-
226
- if ( ! empty( $posts ) ) {
227
- foreach ( $posts as $index => $post ) {
228
- // get form data
229
- $metadata_raw = get_metadata( 'post', $post->ID );
230
-
231
- foreach ( $metakeys as $metakey ) {
232
- $metadata = ! empty( $metadata_raw['_iub_' . $metakey][0] ) ? maybe_unserialize( $metadata_raw['_iub_' . $metakey][0] ) : '';
233
-
234
- if ( ! empty( $metadata ) ) {
235
- // unset empty values
236
- if ( is_array( $metadata ) ) {
237
- foreach ( $metadata as $metadata_key => $metadata_value ) {
238
- if ( $metadata_value == '' && ! in_array( $metakey, array( 'form_legal_notices' ) ) )
239
- unset( $metadata[$metadata_key] );
240
- }
241
- }
242
-
243
- $posts[$index]->{$metakey} = $metadata;
244
- }
245
- /* object id needs to ba an integer
246
- } elseif ( in_array( $metakey, array( 'object_id' ) ) ) {
247
- $posts[$index]->{$metakey} = ! empty( $metadata ) ? absint( $metadata ) : 0;
248
- }
249
- */
250
- }
251
- }
252
- }
253
-
254
- return $posts;
255
- }
256
-
257
- /**
258
- * Get form function.
259
- *
260
- * @param int
261
- * @return object
262
- */
263
- public function get_form( $id ) {
264
- $form_id = ! empty( $id ) ? absint( $id ) : 0;
265
-
266
- if ( ! $form_id )
267
- return false;
268
-
269
- $form = get_post( $form_id );
270
-
271
- if ( ! $form )
272
- return false;
273
-
274
- $metakeys = array(
275
- 'form_source',
276
- 'object_type',
277
- 'object_id',
278
- 'form_fields',
279
- 'form_subject',
280
- 'form_preferences',
281
- 'form_exclude',
282
- 'form_legal_notices'
283
- );
284
-
285
- // get form data
286
- $metadata = get_metadata( 'post', $form->ID );
287
-
288
- foreach ( $metakeys as $metakey ) {
289
- $form->{$metakey} = ! empty( $metadata['_iub_' . $metakey][0] ) ? maybe_unserialize( $metadata['_iub_' . $metakey][0] ) : '';
290
- }
291
-
292
- return $form;
293
- }
294
-
295
- /**
296
- * Delete form function.
297
- *
298
- * @param int
299
- * @return int
300
- */
301
- public function delete_form( $id ) {
302
- $form_id = ! empty( $id ) ? absint( $id ) : 0;
303
-
304
- if ( ! $form_id )
305
- return false;
306
-
307
- $form = get_post( $form_id );
308
-
309
- if ( ! $form )
310
- return false;
311
-
312
- $result = wp_delete_post( $id, true );
313
-
314
- return $result;
315
- }
316
-
317
- /**
318
- * Insert form function.
319
- *
320
- * @param array $args
321
- * @return int
322
- */
323
- public function save_form( $args = array() ) {
324
- $defaults = array(
325
- 'ID' => 0,
326
- 'status' => 'publish',
327
- 'object_type' => 'post', // object type where the form data is stored
328
- 'object_id' => 0, // unique object id
329
- 'form_source' => '', // source slug
330
- 'form_title' => '', // form title
331
- 'form_date' => current_time( 'mysql' ), // form last modified date
332
- 'form_fields' => array(), // form field names array
333
- 'form_subject' => array(), // mapped form with iubenda consent subject param
334
- 'form_preferences' => array(), // mapped form with iubenda consent preferences param
335
- 'form_exclude' => array(), // mapped form with iubenda consent exclude param
336
- 'form_legal_notices' => array() // form legal notices
337
- );
338
-
339
- $args = wp_parse_args( $args, $defaults );
340
-
341
- // sanitize args
342
- $args['ID'] = ! empty( $args['ID'] ) ? absint( $args['ID'] ) : 0;
343
- $args['status'] = ! empty( $args['status'] ) && in_array( $args['status'], array_keys( $this->statuses ) ) ? $args['status'] : 'publish';
344
- $args['object_type'] = 'post';
345
- $args['object_id'] = ! empty( $args['object_id'] ) ? (int) $args['object_id'] : 0;
346
- $args['form_source'] = ! empty( $args['form_source'] ) && in_array( $args['form_source'], array_keys( $this->sources ) ) ? $args['form_source'] : '';
347
- $args['form_title'] = ! empty( $args['form_title'] ) ? esc_html( $args['form_title'] ) : '';
348
- $args['form_date'] = date( 'Y-m-d H:i:s', ( ! empty( $args['form_date'] ) ? strtotime( $args['form_date'] ) : current_time( 'mysql' ) ) );
349
- $args['form_fields'] = ! empty( $args['form_fields'] ) && is_array( $args['form_fields'] ) ? $args['form_fields'] : array();
350
- $args['form_subject'] = ! empty( $args['form_subject'] ) && is_array( $args['form_subject'] ) ? array_map( 'esc_attr', $args['form_subject'] ) : array();
351
- $args['form_preferences'] = ! empty( $args['form_preferences'] ) && is_array( $args['form_preferences'] ) ? array_map( 'esc_attr', $args['form_preferences'] ) : array();
352
- $args['form_exclude'] = ! empty( $args['form_exclude'] ) && is_array( $args['form_exclude'] ) ? array_map( 'esc_attr', $args['form_exclude'] ) : array();
353
- $args['form_legal_notices'] = ! empty( $args['form_legal_notices'] ) && is_array( $args['form_legal_notices'] ) ? array_map( 'esc_attr', $args['form_legal_notices'] ) : array();
354
-
355
- $form_fields = array();
356
-
357
- // sanitize form fields
358
- if ( ! empty( $args['form_fields'] ) && is_array( $args['form_fields'] ) ) {
359
- foreach ( $args['form_fields'] as $form_field ) {
360
- if ( ! empty( $form_field ) && is_array( $form_field ) ) {
361
- $form_fields[] = array_map( 'esc_attr', $form_field );
362
- } else {
363
- $form_fields[] = esc_attr( $form_field );
364
- }
365
- }
366
- }
367
-
368
- // echo '<pre>'; print_r( $args ); echo '</pre>'; exit;
369
-
370
- // bail if any issues
371
- if ( ! $args['form_source'] || ! $args['form_fields'] )
372
- return false;
373
-
374
- $post = $args['ID'] !== 0 ? get_post( $args['ID'] ) : false;
375
- $update = empty( $post ) ? false : true;
376
-
377
- // insert new form
378
- if ( ! $update ) {
379
- $post_id = wp_insert_post( array(
380
- 'post_type' => 'iubenda_form',
381
- 'post_status' => $args['status'],
382
- 'post_title' => $args['form_title'],
383
- 'post_content' => '',
384
- 'post_date' => $args['form_date'],
385
- 'post_modified' => $args['form_date']
386
- ) );
387
- // update form
388
- } else {
389
- $post_id = wp_update_post( array(
390
- 'ID' => $args['ID'],
391
- 'post_status' => $args['status'],
392
- 'post_modified' => $args['form_date']
393
- ) );
394
- }
395
-
396
- // save form source
397
- if ( isset( $args['form_source'] ) )
398
- update_post_meta( $post_id, '_iub_form_source', $args['form_source'] );
399
-
400
- // save object type
401
- if ( isset( $args['object_type'] ) )
402
- update_post_meta( $post_id, '_iub_object_type', $args['object_type'] );
403
-
404
- // save object id
405
- if ( isset( $args['object_id'] ) )
406
- update_post_meta( $post_id, '_iub_object_id', absint( $args['object_id'] ) );
407
-
408
- // save form fields
409
- if ( isset( $args['form_fields'] ) )
410
- update_post_meta( $post_id, '_iub_form_fields', $form_fields );
411
-
412
- // save form subject
413
- if ( isset( $args['form_subject'] ) )
414
- update_post_meta( $post_id, '_iub_form_subject', $args['form_subject'] );
415
-
416
- // save form preferences
417
- if ( isset( $args['form_preferences'] ) )
418
- update_post_meta( $post_id, '_iub_form_preferences', $args['form_preferences'] );
419
-
420
- // save form exclude
421
- if ( isset( $args['form_exclude'] ) )
422
- update_post_meta( $post_id, '_iub_form_exclude', $args['form_exclude'] );
423
-
424
- // save legal notices
425
- if ( isset( $args['form_legal_notices'] ) )
426
- update_post_meta( $post_id, '_iub_form_legal_notices', $args['form_legal_notices'] );
427
-
428
- return $post_id;
429
- }
430
-
431
- /**
432
- * Autodetect forms action.
433
- *
434
- * @return bool
435
- */
436
- public function autodetect_forms() {
437
- $found_forms = $new_forms = array();
438
-
439
- // get forms from active sources
440
- if ( ! empty( $this->sources ) ) {
441
- foreach ( $this->sources as $source => $source_name ) {
442
- $found_forms[$source] = call_user_func( array( $this, 'get_source_forms' ), $source );
443
- }
444
- }
445
-
446
- // insert forms
447
- if ( ! empty( $found_forms ) ) {
448
- foreach ( $found_forms as $source => $source_forms ) {
449
- if ( ! empty( $source_forms ) ) {
450
-
451
- foreach ( $source_forms as $formdata ) {
452
-
453
- $exists = $this->get_form_by_object_id( array(
454
- 'id' => $formdata['object_id'],
455
- 'source' => $formdata['form_source']
456
- ) );
457
-
458
- // form does not exist
459
- if ( ! $exists ) {
460
- $result = $this->save_form( $formdata );
461
-
462
- if ( $result )
463
- $new_forms['new'] = $result;
464
- } else {
465
- // check for fields changes
466
- $new_fields = array_merge( array_diff( $formdata['form_fields'], $exists->form_fields ), array_diff( $exists->form_fields, $formdata['form_fields'] ) );
467
-
468
- if ( $new_fields ) {
469
- $new_forms['updated'] = $exists->ID;
470
-
471
- // update form
472
- $formdata['ID'] = $exists->ID;
473
-
474
- // update to need status if form is already mapped
475
- if ( $exists->post_status == 'mapped' )
476
- $formdata['status'] = 'needs_update';
477
-
478
- // echo '<pre>'; print_r( $formdata ); echo '</pre>'; exit;
479
-
480
- $result = $this->save_form( $formdata );
481
- }
482
- }
483
- }
484
- }
485
- }
486
- }
487
-
488
- // echo '<pre>'; print_r( $found_forms ); echo '</pre>'; exit;
489
-
490
- return ! empty( $new_forms ) ? $new_forms : array();
491
- }
492
-
493
- /**
494
- * Get source forms.
495
- *
496
- * @param string
497
- * @return array
498
- */
499
- public function get_source_forms( $source = '' ) {
500
- $source = ! empty( $source ) && in_array( $source, array_keys( $this->sources ) ) ? $source : '';
501
- $forms = array();
502
-
503
- $restricted_fields = apply_filters( "iub_{$source}_restricted_fields", array(
504
- 'submit',
505
- 'file',
506
- 'quiz'
507
- ) );
508
-
509
- switch ( $source ) {
510
- case 'wpforms' :
511
- $args = array(
512
- 'post_type' => 'wpforms',
513
- 'no_found_rows' => true,
514
- 'nopaging' => true,
515
- );
516
- $posts = get_posts( $args );
517
-
518
- // echo '<pre>'; print_r( $posts ); echo '</pre>'; exit;
519
-
520
- if ( ! empty( $posts ) ) {
521
- foreach ( $posts as $post ) {
522
- // get form data
523
- $formdata = array(
524
- 'object_type' => 'post', // object type where the form data is stored
525
- 'object_id' => $post->ID, // unique object id
526
- 'form_source' => $source, // source slug
527
- 'form_title' => $post->post_title, // form title
528
- 'form_date' => $post->post_modified, // form last modified date
529
- 'form_fields' => array() // form field names array
530
- );
531
-
532
- $input_fields = array(
533
- 'text',
534
- 'textarea',
535
- 'select',
536
- 'radio',
537
- 'checkbox',
538
- 'gdpr-checkbox',
539
- 'email',
540
- 'address',
541
- 'url',
542
- 'name',
543
- 'hidden',
544
- 'date-time',
545
- 'phone',
546
- 'number',
547
- );
548
-
549
- $fields_raw = function_exists( 'wpforms_get_form_fields' ) ? wpforms_get_form_fields( $post->ID ) : false;
550
-
551
- // echo '<pre>'; print_r( $fields_raw ); echo '</pre>'; exit;
552
-
553
- if ( ! empty( $fields_raw ) ) {
554
- foreach ( $fields_raw as $index => $field ) {
555
- // specific field types only
556
- if ( ! empty( $field['type'] ) && in_array( $field['type'], $input_fields ) ) {
557
- switch ( $field['type'] ) {
558
- case 'name' :
559
- if ( ! empty( $field['format'] ) ) {
560
- switch ( $field['format'] ) {
561
- case 'first-last' :
562
- $formdata['form_fields'][] = array(
563
- 'id' => $field['id'],
564
- 'name' => 'wpforms[fields][' . $index . '][first]',
565
- 'type' => $field['type'],
566
- 'label' => __( 'First name', 'iubenda' )
567
- );
568
- $formdata['form_fields'][] = array(
569
- 'id' => $field['id'],
570
- 'name' => 'wpforms[fields][' . $index . '][last]',
571
- 'type' => $field['type'],
572
- 'label' => __( 'Last name', 'iubenda' )
573
- );
574
- break;
575
- case 'first-middle-last' :
576
- $formdata['form_fields'][] = array(
577
- 'id' => $field['id'],
578
- 'name' => 'wpforms[fields][' . $index . '][first]',
579
- 'type' => $field['type'],
580
- 'label' => __( 'First name', 'iubenda' )
581
- );
582
- $formdata['form_fields'][] = array(
583
- 'id' => $field['id'],
584
- 'name' => 'wpforms[fields][' . $index . '][middle]',
585
- 'type' => $field['type'],
586
- 'label' => __( 'Middle name', 'iubenda' )
587
- );
588
- $formdata['form_fields'][] = array(
589
- 'id' => $field['id'],
590
- 'name' => 'wpforms[fields][' . $index . '][last]',
591
- 'type' => $field['type'],
592
- 'label' => __( 'Last name', 'iubenda' )
593
- );
594
- break;
595
- default :
596
- $formdata['form_fields'][] = array(
597
- 'id' => $field['id'],
598
- 'name' => 'wpforms[fields][' . $index . ']',
599
- 'type' => $field['type'],
600
- 'label' => $field['label']
601
- );
602
- break;
603
- }
604
- } else {
605
- $formdata['form_fields'][] = array(
606
- 'id' => $field['id'],
607
- 'name' => 'wpforms[fields][' . $index . ']',
608
- 'type' => $field['type'],
609
- 'label' => $field['label']
610
- );
611
- }
612
- break;
613
- // fix multiple choice checkbox
614
- case 'checkbox' :
615
- $formdata['form_fields'][] = array(
616
- 'id' => $field['id'],
617
- 'name' => 'wpforms[fields][' . $index . '][]',
618
- 'type' => $field['type'],
619
- 'label' => $field['label']
620
- );
621
- break;
622
- default :
623
- $formdata['form_fields'][] = array(
624
- 'id' => $field['id'],
625
- 'name' => 'wpforms[fields][' . $index . ']',
626
- 'type' => $field['type'],
627
- 'label' => $field['label']
628
- );
629
- }
630
-
631
- }
632
- }
633
- }
634
-
635
- $forms[] = $formdata;
636
- }
637
-
638
- // echo '<pre>'; print_r( $forms ); echo '</pre>'; exit;
639
- }
640
-
641
- break;
642
-
643
- case 'wpcf7' :
644
- $args = array(
645
- 'post_type' => 'wpcf7_contact_form',
646
- 'posts_per_page' => -1
647
- );
648
- $posts = get_posts( $args );
649
-
650
- if ( ! empty( $posts ) ) {
651
- foreach ( $posts as $post ) {
652
- // get form data
653
- $contact_form = class_exists( 'WPCF7_ContactForm' ) ? WPCF7_ContactForm::get_instance( $post->ID ) : false;
654
-
655
- if ( ! empty( $contact_form ) ) {
656
- $formdata = array(
657
- 'object_type' => 'post', // object type where the form data is stored
658
- 'object_id' => $post->ID, // unique object id
659
- 'form_source' => $source, // source slug
660
- 'form_title' => $post->post_title, // form title
661
- 'form_date' => $post->post_modified, // form last modified date
662
- 'form_fields' => array() // form field names array
663
- );
664
-
665
- $fields_raw = $contact_form->scan_form_tags();
666
-
667
- // echo '<pre>'; print_r( $fields_raw ); echo '</pre>'; exit;
668
-
669
- if ( ! empty( $fields_raw ) ) {
670
- foreach ( $fields_raw as $field ) {
671
- // specific field types only
672
- if ( ! empty( $field['basetype'] ) && ! in_array( $field['basetype'], $restricted_fields ) ) {
673
- $formdata['form_fields'][] = $field['name'];
674
- }
675
- }
676
- }
677
-
678
- $forms[] = $formdata;
679
- }
680
-
681
- // echo '<pre>'; print_r( $contact_form ); echo '</pre>'; exit;
682
- }
683
- }
684
-
685
- break;
686
-
687
- case 'woocommerce' :
688
- $checkout_form = '';
689
-
690
- ob_start();
691
-
692
- // Ensure gateways and shipping methods are loaded early.
693
- WC()->payment_gateways();
694
- WC()->shipping();
695
-
696
- /*
697
- * First lets start the session. You cant use here WC_Session directly
698
- * because it's an abstract class. But you can use WC_Session_Handler which
699
- * extends WC_Session
700
- */
701
- WC()->session = new WC_Session_Handler;
702
-
703
- /*
704
- * Next lets create a customer so we can access checkout fields
705
- * If you will check a constructor for WC_Customer class you will see
706
- * that if you will not provide user to create customer it will use some
707
- * default one. Magic.
708
- */
709
- WC()->customer = new WC_Customer;
710
-
711
- // Create a cart contents
712
- WC()->cart = new WC_Cart;
713
-
714
- // Create an abstract order
715
- WC()->order = new WC_Order;
716
-
717
- wc_get_template(
718
- 'checkout/form-checkout.php', array(
719
- 'checkout' => WC()->checkout()
720
- )
721
- );
722
-
723
- wc_get_template(
724
- 'checkout/form-pay.php', array(
725
- 'order' => WC()->order
726
- )
727
- );
728
-
729
- $checkout_form = ob_get_contents();
730
- ob_end_clean();
731
-
732
- if ( ! empty( $checkout_form ) ) {
733
- $formdata = array(
734
- 'object_type' => 'custom', // object type where the form data is stored
735
- 'object_id' => 0, // unique object id
736
- 'form_source' => $source, // source slug
737
- 'form_title' => $this->sources[$source], // form title
738
- 'form_date' => current_time( 'mysql' ), // form last modified date
739
- 'form_fields' => array() // form field names array
740
- );
741
-
742
- $input_fields = array(
743
- 'input',
744
- 'textarea',
745
- 'select'
746
- );
747
-
748
- // DOMDoc parser
749
- if ( iubenda()->options['cs']['parser_engine'] == 'new' ) {
750
- libxml_use_internal_errors( true );
751
-
752
- $document = new DOMDocument();
753
-
754
- // set document arguments
755
- $document->formatOutput = true;
756
- $document->preserveWhiteSpace = false;
757
-
758
- // load HTML
759
- $document->loadHTML( $checkout_form );
760
-
761
- // search for nodes
762
- foreach ( $input_fields as $input_field ) {
763
- $fields_raw = $document->getElementsByTagName( $input_field );
764
-
765
- if ( ! empty( $fields_raw ) && is_object( $fields_raw ) ) {
766
- foreach ( $fields_raw as $field ) {
767
- $field_name = $field->getAttribute( 'name' );
768
- $field_type = $field->getAttribute( 'type' );
769
-
770
- // exclude submit
771
- if ( ! empty( $field_type ) && ! in_array( $field_type, array( 'submit', 'hidden' ) ) )
772
- $formdata['form_fields'][] = $field->getAttribute( 'name' );
773
- }
774
- }
775
- }
776
-
777
- $forms[] = $formdata;
778
-
779
- libxml_use_internal_errors( false );
780
-
781
- // Simple HTML Dom parser
782
- } else {
783
- $html = str_get_html( $checkout_form, $lowercase = true, $force_tags_closed = true, $strip = false );
784
-
785
- if ( is_object( $html ) ) {
786
- // search for nodes
787
- foreach ( $input_fields as $input_field ) {
788
- $fields_raw = $html->find( $input_field );
789
-
790
- if ( is_array( $fields_raw ) ) {
791
- foreach ( $fields_raw as $field ) {
792
- $field_name = $field->name;
793
- $field_type = $field->type;
794
-
795
- // exclude submit
796
- if ( ! empty( $field_type ) && ! in_array( $field_type, array( 'submit', 'hidden' ) ) )
797
- $formdata['form_fields'][] = $field->getAttribute( 'name' );
798
- }
799
- }
800
- }
801
-
802
- $forms[] = $formdata;
803
-
804
- }
805
- }
806
-
807
- }
808
-
809
- /*
810
- echo '<pre>';
811
- print_r( $checkout_form );
812
- echo '</pre>';
813
- exit;
814
- */
815
- break;
816
-
817
- case 'wp_comment_form' :
818
- $comment_form = '';
819
-
820
- // get comment form for logged out user
821
- $current_user_id = get_current_user_id();
822
-
823
- // get first post
824
- $post_args = array(
825
- 'numberposts' => 1,
826
- 'orderby' => 'ID',
827
- 'order' => 'ASC',
828
- 'fields' => 'ids'
829
- );
830
-
831
- $posts = get_posts( $post_args );
832
-
833
- // get comment form
834
- if ( ! empty( $posts ) ) {
835
- wp_set_current_user( 0 );
836
-
837
- ob_start();
838
-
839
- comment_form( array(), $posts[0] );
840
-
841
- $comment_form = ob_get_contents();
842
- ob_end_clean();
843
-
844
- wp_set_current_user( $current_user_id );
845
- }
846
-
847
- if ( ! empty( $comment_form ) ) {
848
- $formdata = array(
849
- 'object_type' => 'custom', // object type where the form data is stored
850
- 'object_id' => 0, // unique object id
851
- 'form_source' => $source, // source slug
852
- 'form_title' => $this->sources[$source], // form title
853
- 'form_date' => current_time( 'mysql' ), // form last modified date
854
- 'form_fields' => array() // form field names array
855
- );
856
-
857
- $input_fields = array(
858
- 'input',
859
- 'textarea',
860
- 'select'
861
- );
862
-
863
- // DOMDoc parser
864
- if ( iubenda()->options['cs']['parser_engine'] == 'new' ) {
865
- libxml_use_internal_errors( true );
866
-
867
- $document = new DOMDocument();
868
-
869
- // set document arguments
870
- $document->formatOutput = true;
871
- $document->preserveWhiteSpace = false;
872
-
873
- // load HTML
874
- $document->loadHTML( $comment_form );
875
-
876
- // search for nodes
877
- foreach ( $input_fields as $input_field ) {
878
- $fields_raw = $document->getElementsByTagName( $input_field );
879
-
880
- if ( ! empty( $fields_raw ) && is_object( $fields_raw ) ) {
881
- foreach ( $fields_raw as $field ) {
882
- $field_name = $field->getAttribute( 'name' );
883
- $field_type = $field->getAttribute( 'type' );
884
-
885
- // exclude submit
886
- if ( ! empty( $field_type ) && ! in_array( $field_type, array( 'submit' ) ) )
887
- $formdata['form_fields'][] = $field->getAttribute( 'name' );
888
- }
889
- }
890
- }
891
-
892
- $forms[] = $formdata;
893
-
894
- libxml_use_internal_errors( false );
895
-
896
- // Simple HTML Dom parser
897
- } else {
898
- $html = str_get_html( $comment_form, $lowercase = true, $force_tags_closed = true, $strip = false );
899
-
900
- if ( is_object( $html ) ) {
901
- // search for nodes
902
- foreach ( $input_fields as $input_field ) {
903
- $fields_raw = $html->find( $input_field );
904
-
905
- if ( is_array( $fields_raw ) ) {
906
- foreach ( $fields_raw as $field ) {
907
- $field_name = $field->name;
908
- $field_type = $field->type;
909
-
910
- // exclude submit
911
- if ( ! empty( $field_type ) && ! in_array( $field_type, array( 'submit' ) ) )
912
- $formdata['form_fields'][] = $field->getAttribute( 'name' );
913
- }
914
- }
915
- }
916
-
917
- $forms[] = $formdata;
918
-
919
- }
920
- }
921
- }
922
-
923
- break;
924
- }
925
-
926
- return $forms;
927
- }
928
-
929
- /**
930
- * Get Post object by post_meta query
931
- *
932
- * @return array
933
- */
934
- public function get_form_by_object_id( $args = array() ) {
935
- // parse incoming $args into an array and merge it with $defaults
936
- $args = wp_parse_args( $args );
937
-
938
- // grab page
939
- $args = array(
940
- 'meta_query' => array(
941
- array(
942
- 'key' => '_iub_object_id',
943
- 'value' => $args['id'],
944
- ),
945
- array(
946
- 'key' => '_iub_form_source',
947
- 'value' => $args['source'],
948
- )
949
- ),
950
- 'post_type' => 'iubenda_form',
951
- 'post_status' => 'any',
952
- 'posts_per_page' => '1',
953
- 'fields' => 'ids'
954
- );
955
-
956
- // run query
957
- $posts = get_posts( $args );
958
-
959
- // check result
960
- if ( ! $posts || is_wp_error( $posts ) )
961
- return false;
962
-
963
- $form = $this->get_form( $posts[0] );
964
-
965
- // kick back results
966
- return $form;
967
- }
968
-
969
- }
1
+ <?php
2
+ // exit if accessed directly
3
+ if ( ! defined( 'ABSPATH' ) )
4
+ exit;
5
+
6
+ /**
7
+ * iubenda_Forms class.
8
+ *
9
+ * @class iubenda_Forms
10
+ */
11
+ class iubenda_Forms {
12
+
13
+ public $sources = array();
14
+ public $statuses = array();
15
+
16
+ /**
17
+ * Class constructor.
18
+ */
19
+ public function __construct() {
20
+ // actions
21
+ add_action( 'plugins_loaded', array( $this, 'init' ) );
22
+ add_action( 'init', array( $this, 'register_post_type' ) );
23
+ add_action( 'init', array( $this, 'register_post_status' ) );
24
+ add_action( 'wp_enqueue_scripts', array( $this, 'wp_enqueue_scripts' ) );
25
+ }
26
+
27
+ /**
28
+ * Initialize forms data.
29
+ *
30
+ * @return void
31
+ */
32
+ public function init() {
33
+ // WOrdPress commenting form
34
+ $this->sources['wp_comment_form'] = 'WordPress Comment';
35
+
36
+ // check if Contact Form 7 is active
37
+ if ( class_exists( 'WPCF7' ) ) {
38
+ $this->sources['wpcf7'] = 'Contact Form 7';
39
+ }
40
+
41
+ // check if WP Forms is active
42
+ if ( function_exists( 'wpforms' ) ) {
43
+ $this->sources['wpforms'] = 'WP Forms';
44
+ }
45
+
46
+ // check if EooCommerce is active
47
+ if ( function_exists( 'WC' ) ) {
48
+ $this->sources['woocommerce'] = 'WooCommerce Checkout';
49
+ }
50
+
51
+ $this->sources = apply_filters( 'iub_supported_form_sources', $this->sources );
52
+
53
+ $this->statuses = array(
54
+ 'publish' => _x( 'To Map', 'post status', 'iubenda' ),
55
+ 'mapped' => _x( 'Mapped', 'post status', 'iubenda' ),
56
+ 'needs_update' => _x( 'Needs Update', 'post status', 'iubenda' ),
57
+ // 'trash' => _x( 'Ignored', 'post status', 'iubenda' )
58
+ );
59
+ }
60
+
61
+ /**
62
+ * Enqueue frontend script.
63
+ */
64
+ public function wp_enqueue_scripts() {
65
+ if ( ! empty( iubenda()->options['cons']['public_api_key'] ) ) {
66
+ wp_register_script( 'iubenda-forms', IUBENDA_PLUGIN_URL . '/js/frontend.js', array( 'jquery' ), iubenda()->version, true );
67
+
68
+ $args = array();
69
+
70
+ $form_args = array(
71
+ 'post_status' => array( 'mapped', 'needs_update' )
72
+ );
73
+
74
+ $forms = $this->get_forms( $form_args );
75
+
76
+ // echo '<pre>'; print_r( $forms ); echo '</pre>';
77
+
78
+ if ( ! empty( $forms ) ) {
79
+ // required form parameters
80
+ $form_parameters = array(
81
+ 'subject',
82
+ 'preferences',
83
+ 'exclude',
84
+ 'legal_notices'
85
+ );
86
+ // loop through forms
87
+ foreach ( $forms as $form ) {
88
+ // bail if user is logged in and source is WP comment form
89
+ if ( $form->form_source == 'wp_comment_form' && is_user_logged_in() )
90
+ continue;
91
+
92
+ // we need unique identifier for the html form
93
+ // by default it's object id, used in form html id
94
+ $args[$form->form_source][$form->object_id] = array();
95
+
96
+ foreach ( $form_parameters as $parameter ) {
97
+ $parameter_name = 'form_' . $parameter;
98
+ $parameter_value = ! empty( $form->$parameter_name ) ? $form->$parameter_name : '';
99
+
100
+ // echo '<pre>'; print_r( $parameter_value ); echo '</pre>';
101
+
102
+ switch ( $parameter ) {
103
+ case 'legal_notices' :
104
+ if ( $parameter_value && is_array( $parameter_value ) ) {
105
+ foreach( $parameter_value as $value ) {
106
+ $args[$form->form_source][$form->object_id]['consent']['legal_notices'][] = array( 'identifier' => $value );
107
+ }
108
+ }
109
+ break;
110
+ default :
111
+ if ( $parameter_value ) {
112
+ switch ( $form->form_source ) {
113
+ case 'wpforms' :
114
+ // replace integers with field names
115
+ foreach ( $parameter_value as $index => $parameter_item ) {
116
+ $parameter_value[$index] = $form->form_fields[$parameter_item]['name'];
117
+ }
118
+ $args[$form->form_source][$form->object_id]['form']['map'][$parameter] = $parameter_value;
119
+ break;
120
+ default :
121
+ $args[$form->form_source][$form->object_id]['form']['map'][$parameter] = $parameter_value;
122
+ break;
123
+ }
124
+ }
125
+ break;
126
+ }
127
+ }
128
+ }
129
+ }
130
+
131
+ // echo '<pre>'; print_r( $args ); echo '</pre>'; exit;
132
+
133
+ wp_localize_script(
134
+ 'iubenda-forms',
135
+ 'iubForms',
136
+ json_encode( $args )
137
+ );
138
+
139
+ wp_enqueue_script( 'iubenda-forms' );
140
+ }
141
+ }
142
+
143
+ /**
144
+ * Register iubenda form post type.
145
+ */
146
+ public function register_post_type() {
147
+ register_post_type( 'iubenda_form', array(
148
+ 'labels' => array(
149
+ 'name' => __( 'Forms', 'iubenda' ),
150
+ 'singular_name' => __( 'Form', 'iubenda' ),
151
+ ),
152
+ 'rewrite' => false,
153
+ 'query_var' => false,
154
+ 'public' => false,
155
+ 'capability_type' => 'page'
156
+ ) );
157
+ }
158
+
159
+ /**
160
+ * Register iubenda form post status.
161
+ */
162
+ public function register_post_status() {
163
+ foreach ( $this->statuses as $name => $label ) {
164
+ if ( $name === 'publish' )
165
+ continue;
166
+
167
+ register_post_status( $name, array(
168
+ 'label' => $label,
169
+ 'public' => true,
170
+ 'exclude_from_search' => false,
171
+ 'show_in_admin_all_list' => true,
172
+ 'show_in_admin_status_list' => true,
173
+ 'post_type' => array( 'iubenda_form' ),
174
+ // 'label_count' => _n_noop( 'Mapped <span class="count">(%s)</span>', 'Mapped <span class="count">(%s)</span>', 'iubenda' ),
175
+ ) );
176
+ }
177
+
178
+ }
179
+
180
+ /**
181
+ * Get iubenda forms function.
182
+ *
183
+ * @param type $args
184
+ * @return array
185
+ */
186
+ public function get_forms( $args = array() ) {
187
+ $defaults = array(
188
+ 'post_status' => 'any',
189
+ 'posts_per_page' => -1,
190
+ 'offset' => 0,
191
+ 'orderby' => 'ID',
192
+ 'order' => 'ASC',
193
+ 'form_source' => 'any'
194
+ );
195
+
196
+ $args = wp_parse_args( $args, $defaults );
197
+
198
+ $args['post_type'] = 'iubenda_form';
199
+
200
+ // specific sources only
201
+ if ( $args['form_source'] != 'any' && ( is_string( $args['form_source'] ) || is_array( $args['form_source'] ) ) ) {
202
+ $args['meta_query'] = array(
203
+ array(
204
+ 'key' => '_iub_form_source',
205
+ 'value' => $args['form_source'],
206
+ 'compare' => 'IN',
207
+ ),
208
+ );
209
+ }
210
+
211
+ $q = new WP_Query();
212
+
213
+ $posts = $q->query( $args );
214
+
215
+ $metakeys = array(
216
+ 'form_source',
217
+ 'object_type',
218
+ 'object_id',
219
+ 'form_fields',
220
+ 'form_subject',
221
+ 'form_preferences',
222
+ 'form_exclude',
223
+ 'form_legal_notices'
224
+ );
225
+
226
+ if ( ! empty( $posts ) ) {
227
+ foreach ( $posts as $index => $post ) {
228
+ // get form data
229
+ $metadata_raw = get_metadata( 'post', $post->ID );
230
+
231
+ foreach ( $metakeys as $metakey ) {
232
+ $metadata = ! empty( $metadata_raw['_iub_' . $metakey][0] ) ? maybe_unserialize( $metadata_raw['_iub_' . $metakey][0] ) : '';
233
+
234
+ if ( ! empty( $metadata ) ) {
235
+ // unset empty values
236
+ if ( is_array( $metadata ) ) {
237
+ foreach ( $metadata as $metadata_key => $metadata_value ) {
238
+ if ( $metadata_value == '' && ! in_array( $metakey, array( 'form_legal_notices' ) ) )
239
+ unset( $metadata[$metadata_key] );
240
+ }
241
+ }
242
+
243
+ $posts[$index]->{$metakey} = $metadata;
244
+ }
245
+ /* object id needs to ba an integer
246
+ } elseif ( in_array( $metakey, array( 'object_id' ) ) ) {
247
+ $posts[$index]->{$metakey} = ! empty( $metadata ) ? absint( $metadata ) : 0;
248
+ }
249
+ */
250
+ }
251
+ }
252
+ }
253
+
254
+ return $posts;
255
+ }
256
+
257
+ /**
258
+ * Get form function.
259
+ *
260
+ * @param int
261
+ * @return object
262
+ */
263
+ public function get_form( $id ) {
264
+ $form_id = ! empty( $id ) ? absint( $id ) : 0;
265
+
266
+ if ( ! $form_id )
267
+ return false;
268
+
269
+ $form = get_post( $form_id );
270
+
271
+ if ( ! $form )
272
+ return false;
273
+
274
+ $metakeys = array(
275
+ 'form_source',
276
+ 'object_type',
277
+ 'object_id',
278
+ 'form_fields',
279
+ 'form_subject',
280
+ 'form_preferences',
281
+ 'form_exclude',
282
+ 'form_legal_notices'
283
+ );
284
+
285
+ // get form data
286
+ $metadata = get_metadata( 'post', $form->ID );
287
+
288
+ foreach ( $metakeys as $metakey ) {
289
+ $form->{$metakey} = ! empty( $metadata['_iub_' . $metakey][0] ) ? maybe_unserialize( $metadata['_iub_' . $metakey][0] ) : '';
290
+ }
291
+
292
+ return $form;
293
+ }
294
+
295
+ /**
296
+ * Delete form function.
297
+ *
298
+ * @param int
299
+ * @return int
300
+ */
301
+ public function delete_form( $id ) {
302
+ $form_id = ! empty( $id ) ? absint( $id ) : 0;
303
+
304
+ if ( ! $form_id )
305
+ return false;
306
+
307
+ $form = get_post( $form_id );
308
+
309
+ if ( ! $form )
310
+ return false;
311
+
312
+ $result = wp_delete_post( $id, true );
313
+
314
+ return $result;
315
+ }
316
+
317
+ /**
318
+ * Insert form function.
319
+ *
320
+ * @param array $args
321
+ * @return int
322
+ */
323
+ public function save_form( $args = array() ) {
324
+ $defaults = array(
325
+ 'ID' => 0,
326
+ 'status' => 'publish',
327
+ 'object_type' => 'post', // object type where the form data is stored
328
+ 'object_id' => 0, // unique object id
329
+ 'form_source' => '', // source slug
330
+ 'form_title' => '', // form title
331
+ 'form_date' => current_time( 'mysql' ), // form last modified date
332
+ 'form_fields' => array(), // form field names array
333
+ 'form_subject' => array(), // mapped form with iubenda consent subject param
334
+ 'form_preferences' => array(), // mapped form with iubenda consent preferences param
335
+ 'form_exclude' => array(), // mapped form with iubenda consent exclude param
336
+ 'form_legal_notices' => array() // form legal notices
337
+ );
338
+
339
+ $args = wp_parse_args( $args, $defaults );
340
+
341
+ // sanitize args
342
+ $args['ID'] = ! empty( $args['ID'] ) ? absint( $args['ID'] ) : 0;
343
+ $args['status'] = ! empty( $args['status'] ) && in_array( $args['status'], array_keys( $this->statuses ) ) ? $args['status'] : 'publish';
344
+ $args['object_type'] = 'post';
345
+ $args['object_id'] = ! empty( $args['object_id'] ) ? (int) $args['object_id'] : 0;
346
+ $args['form_source'] = ! empty( $args['form_source'] ) && in_array( $args['form_source'], array_keys( $this->sources ) ) ? $args['form_source'] : '';
347
+ $args['form_title'] = ! empty( $args['form_title'] ) ? esc_html( $args['form_title'] ) : '';
348
+ $args['form_date'] = date( 'Y-m-d H:i:s', ( ! empty( $args['form_date'] ) ? strtotime( $args['form_date'] ) : current_time( 'mysql' ) ) );
349
+ $args['form_fields'] = ! empty( $args['form_fields'] ) && is_array( $args['form_fields'] ) ? $args['form_fields'] : array();
350
+ $args['form_subject'] = ! empty( $args['form_subject'] ) && is_array( $args['form_subject'] ) ? array_map( 'esc_attr', $args['form_subject'] ) : array();
351
+ $args['form_preferences'] = ! empty( $args['form_preferences'] ) && is_array( $args['form_preferences'] ) ? array_map( 'esc_attr', $args['form_preferences'] ) : array();
352
+ $args['form_exclude'] = ! empty( $args['form_exclude'] ) && is_array( $args['form_exclude'] ) ? array_map( 'esc_attr', $args['form_exclude'] ) : array();
353
+ $args['form_legal_notices'] = ! empty( $args['form_legal_notices'] ) && is_array( $args['form_legal_notices'] ) ? array_map( 'esc_attr', $args['form_legal_notices'] ) : array();
354
+
355
+ $form_fields = array();
356
+
357
+ // sanitize form fields
358
+ if ( ! empty( $args['form_fields'] ) && is_array( $args['form_fields'] ) ) {
359
+ foreach ( $args['form_fields'] as $form_field ) {
360
+ if ( ! empty( $form_field ) && is_array( $form_field ) ) {
361
+ $form_fields[] = array_map( 'esc_attr', $form_field );
362
+ } else {
363
+ $form_fields[] = esc_attr( $form_field );
364
+ }
365
+ }
366
+ }
367
+
368
+ // echo '<pre>'; print_r( $args ); echo '</pre>'; exit;
369
+
370
+ // bail if any issues
371
+ if ( ! $args['form_source'] || ! $args['form_fields'] )
372
+ return false;
373
+
374
+ $post = $args['ID'] !== 0 ? get_post( $args['ID'] ) : false;
375
+ $update = empty( $post ) ? false : true;
376
+
377
+ // insert new form
378
+ if ( ! $update ) {
379
+ $post_id = wp_insert_post( array(
380
+ 'post_type' => 'iubenda_form',
381
+ 'post_status' => $args['status'],
382
+ 'post_title' => $args['form_title'],
383
+ 'post_content' => '',
384
+ 'post_date' => $args['form_date'],
385
+ 'post_modified' => $args['form_date']
386
+ ) );
387
+ // update form
388
+ } else {
389
+ $post_id = wp_update_post( array(
390
+ 'ID' => $args['ID'],
391
+ 'post_status' => $args['status'],
392
+ 'post_modified' => $args['form_date']
393
+ ) );
394
+ }
395
+
396
+ // save form source
397
+ if ( isset( $args['form_source'] ) )
398
+ update_post_meta( $post_id, '_iub_form_source', $args['form_source'] );
399
+
400
+ // save object type
401
+ if ( isset( $args['object_type'] ) )
402
+ update_post_meta( $post_id, '_iub_object_type', $args['object_type'] );
403
+
404
+ // save object id
405
+ if ( isset( $args['object_id'] ) )
406
+ update_post_meta( $post_id, '_iub_object_id', absint( $args['object_id'] ) );
407
+
408
+ // save form fields
409
+ if ( isset( $args['form_fields'] ) )
410