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