global.js
4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// TODO: Fix error collecting.
//window.onerror = function(message, url, lineNumber) {
// var data;
//
// data = {
// 'action': 'nf_log_js_error',
// 'security': nfFrontEnd.ajaxNonce,
// 'message': message,
// 'url': url,
// 'lineNumber': lineNumber
// };
//
// jQuery.ajax({
// url: nfFrontEnd.adminAjax,
// type: 'POST',
// data: data,
// cache: false,
// success: function( data, textStatus, jqXHR ) {
// try {
//
// } catch( e ) {
// console.log( e );
// console.log( 'Parse Error' );
// console.log( e );
// }
//
// },
// error: function( jqXHR, textStatus, errorThrown ) {
// // Handle errors here
// console.log('ERRORS: ' + errorThrown);
// console.log( jqXHR );
//
// try {
//
// } catch( e ) {
// console.log( 'Parse Error' );
// }
// }
// });
// return false;
//};
var nfRadio = Backbone.Radio;
nfRadio.channel( 'form' ).on( 'render:view', function() {
jQuery( '.g-recaptcha' ).each( function() {
var callback = jQuery( this ).data( 'callback' );
var fieldID = jQuery( this ).data( 'fieldid' );
if ( typeof window[ callback ] !== 'function' ){
window[ callback ] = function( response ) {
nfRadio.channel( 'recaptcha' ).request( 'update:response', response, fieldID );
};
}
} );
} );
var nfRecaptcha = Marionette.Object.extend( {
initialize: function() {
/*
* If we've already rendered our form view, render our recaptcha fields.
*/
if ( 0 != jQuery( '.g-recaptcha' ).length ) {
this.renderCaptcha();
}
/*
* We haven't rendered our form view, so hook into the view render radio message, and then render.
*/
this.listenTo( nfRadio.channel( 'form' ), 'render:view', this.renderCaptcha );
this.listenTo( nfRadio.channel( 'captcha' ), 'reset', this.renderCaptcha );
},
renderCaptcha: function() {
jQuery( '.g-recaptcha:empty' ).each( function() {
var opts = {
fieldid: jQuery( this ).data( 'fieldid' ),
size: jQuery( this ).data( 'size' ),
theme: jQuery( this ).data( 'theme' ),
sitekey: jQuery( this ).data( 'sitekey' ),
callback: jQuery( this ).data( 'callback' )
};
var grecaptchaID = grecaptcha.render( jQuery( this )[0], opts );
if ( opts.size === 'invisible' ) {
try {
grecaptcha.execute( grecaptchaID );
} catch( e ){
console.log( 'Notice: Error trying to execute grecaptcha.' );
}
}
} );
}
} );
var nfRenderRecaptcha = function() {
new nfRecaptcha();
}
const nf_check_recaptcha_consent = () => {
let stored_responses = [], services = [];
//Cookie check
if(!nf_check_data_for_recaptcha_consent()){
stored_responses.push( false );
services.push("missing_cookie");
}
//Build response with services gathered and print it in global scope
const response = {
"consent_state": stored_responses,
"services" : services
};
nfFrontEnd.nf_consent_status_response = response;
//Display filterable status to add extra consent check
let nf_consent_status_extra_check = new CustomEvent('nf_consent_status_check', {detail: response});
document.dispatchEvent(nf_consent_status_extra_check);
return nfFrontEnd.nf_consent_status_response;
}
//Get specific recaptcha cookie
const nf_check_data_for_recaptcha_consent = () => {
return nf_get_cookie_by_name("_grecaptcha") !== "";
}
//Get a cookie
const nf_get_cookie_by_name = (cname) => {
let name = cname + "=";
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(';');
for(let i = 0; i <ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
const nf_reload_after_cookie_consent = ( submitFieldID, layoutView ) => {
if(typeof submitFieldID !== "undefined" && typeof layoutView !== "undefined"){
nfRadio.channel( 'fields' ).request("remove:error", submitFieldID, "recaptcha-v3-missing");
nfRadio.channel( 'fields' ).request("remove:error", submitFieldID, "recaptcha-v3-consent");
nfRadio.channel( 'form' ).trigger( 'render:view', layoutView );
}
}