PluginProbe
Booking Calendar / 11.6.1
Booking Calendar v11.6.1
11.8.3 11.8.2 11.8.1 11.8 11.7 11.6.1 11.6 11.5 11.4.3 11.4.2 11.4.1 11.4 11.3 11.2.1 11.2 11.1 11.0 10.15.7 10.15.6 10.1.3 10.10 10.10.1 10.10.2 10.11 10.11.2 All 203 releases
booking / includes / save-user-meta / _src / save-user-meta.js

save-user-meta.js in Booking Calendar 11.6.1, at includes/save-user-meta/_src/save-user-meta.js

141 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 function wpbc_user_data_saver__read_attr_or_data( $el, attr_name, data_name ) {
2 var attr_value = $el.attr( attr_name );
3
4 if ( attr_value !== undefined ) {
5 return attr_value;
6 }
7
8 return $el.data( data_name );
9 }
10
11 function wpbc_save_custom_user_data_from_element(el) {
12
13 if ( typeof WPBC_UserDataSaver === 'undefined' ) {
14 console.error( 'WPBC | Global AJAX object is missing.' );
15 return false;
16 }
17
18 const $el = jQuery( el );
19
20 const user_id = wpbc_user_data_saver__read_attr_or_data( $el, 'data-wpbc-u-save-user-id', 'wpbc-u-save-user-id' );
21 const nonce = wpbc_user_data_saver__read_attr_or_data( $el, 'data-wpbc-u-save-nonce', 'wpbc-u-save-nonce' );
22 const nonce_action = wpbc_user_data_saver__read_attr_or_data( $el, 'data-wpbc-u-save-action', 'wpbc-u-save-action' );
23 const data_name = wpbc_user_data_saver__read_attr_or_data( $el, 'data-wpbc-u-save-name', 'wpbc-u-save-name' );
24 const fields_raw = wpbc_user_data_saver__read_attr_or_data( $el, 'data-wpbc-u-save-fields', 'wpbc-u-save-fields' ) || '';
25 const inline_value = wpbc_user_data_saver__read_attr_or_data( $el, 'data-wpbc-u-save-value', 'wpbc-u-save-value' );
26 const json = wpbc_user_data_saver__read_attr_or_data( $el, 'data-wpbc-u-save-value-json', 'wpbc-u-save-value-json' );
27
28 const callbackFnName = wpbc_user_data_saver__read_attr_or_data( $el, 'data-wpbc-u-save-callback', 'wpbc-u-save-callback' );
29 const callbackFn = typeof window[callbackFnName] === 'function' ? window[callbackFnName] : null;
30 if ( callbackFnName && !callbackFn ) {
31 console.warn( 'WPBC | Callback not found:', callbackFnName );
32 }
33
34 if ( !user_id || !nonce || !nonce_action || !data_name ) {
35 console.error( 'WPBC | Missing required data attributes.' );
36 return false;
37 }
38
39 let serialized = '';
40
41 if ( typeof json === 'string' && json.trim() !== '' ) {
42 try {
43 serialized = jQuery.param( JSON.parse( json ) );
44 } catch ( e ) {
45 console.error( 'WPBC | Invalid JSON in data-wpbc-u-save-value-json' );
46 return false;
47 }
48 } else if ( inline_value !== undefined ) {
49 // Save simple direct value as single param.
50 serialized = jQuery.param( { value: inline_value } );
51 } else if ( fields_raw ) {
52 const fields = fields_raw.split( ',' ).map( s => s.trim() ).filter( Boolean );
53 const data = {};
54
55 fields.forEach( function (selector) {
56 const $field = jQuery( selector );
57 if ( $field.length ) {
58 const key = $field.attr( 'name' ) || $field.attr( 'id' );
59 if ( key ) {
60 data[key] = $field.val();
61 } else {
62 console.warn( 'WPBC | Field missing name/id:', $field );
63 }
64 }
65 } );
66
67 serialized = jQuery.param( data );
68 } else {
69 console.error( 'WPBC | Missing data-wpbc-u-save-fields or data-wpbc-u-save-value.' );
70 return false;
71 }
72
73 jQuery( document ).trigger( 'wpbc:userdata:beforeSave', [ $el, serialized ] );
74
75 return jQuery.ajax( {
76 url : WPBC_UserDataSaver.ajax_url,
77 type : 'POST',
78 data : {
79 action : WPBC_UserDataSaver.action,
80 user_id : user_id,
81 nonce : nonce,
82 nonce_action: nonce_action,
83 data_name : data_name,
84 data_value : serialized
85 },
86 success: function (response) {
87
88 jQuery( document ).trigger( 'wpbc:userdata:afterSave', [ response ] );
89
90 if ( response.success ) {
91 // console.log( 'WPBC | ' + (response.data.message || 'Saved successfully.') );
92 if ( callbackFn ) {
93 callbackFn( response );
94 }
95 } else {
96 console.error( 'WPBC | ' + ( response.data.message || 'Save error.' ) );
97 }
98 },
99 error : function ( xhr ) {
100 console.error( 'WPBC | AJAX error: ' + xhr.status + ' ' + xhr.statusText );
101 }
102 } );
103 }
104
105 /**
106 * Note:
107 // This binds a handler *to event 'click'*, labeled 'myFeature'
108 $(document).on('click.myFeature', function () { ... });
109
110 // This binds another *to 'click'*, labeled 'debug'
111 $(document).on('click.debug', function () { ... });
112
113 // This fires the event 'click' — both will run:
114 $(document).trigger('click');
115
116 // This removes only handlers with .myFeature
117 $(document).off('click.myFeature');
118 */
119
120
121 // jQuery( document ).on( 'wpbc:userdata:beforeSave.customLogger', function (e, $el, data) {
122 // // $el.prop('disabled', true); // Example: disable the clicked element
123 // } );
124
125 // jQuery( document ).on( 'wpbc:userdata:afterSave.customLogger', function (e, response) {
126 //
127 // console.log( 'Save finished. Server responded with:', response );
128 //
129 // if ( response.success ) {
130 // console.log( 'Saved!' );
131 // } else {
132 // console.log( 'Error: ' + ( response.data?.message || 'Unknown error' ) );
133 // }
134 //
135 // // Optional: re-enable all save buttons
136 // // jQuery('[data-wpbc-u-save-name]').prop('disabled', false);
137 // } );
138
139 // To remove it:
140 // jQuery(document).off('wpbc:userdata:afterSave.customLogger');
141