')
.addClass('siteseo-toast')
.addClass(type)
.html(` ${message}`);
$('body').append(toast);
// 3 seconds
toast.fadeIn(300).delay(3000).fadeOut(300, function () {
toast.remove();
});
}
// error hadeler
function toggleError($toggle, $input, $stateText) {
$toggle.toggleClass('active');
$input.val($toggle.hasClass('active') ? '1' : '0');
$stateText.text($toggle.hasClass('active') ? 'Disable' : 'Enable');
}
// media uploader for image logo
$('#siteseopro_structured_data_upload_img').click(function(e) {
var mediaUploader;
e.preventDefault();
if (mediaUploader) {
mediaUploader.open();
return;
}
mediaUploader = wp.media.frames.file_frame = wp.media({
title: 'Media',
button: {
text: 'Select'
},
multiple: false
});
mediaUploader.on('select', function() {
var attachment = mediaUploader.state().get('selection').first().toJSON();
$('#structured_data_image_url').val(attachment.url);
});
mediaUploader.open();
});
$('#siteseo-sidebar-refersh-tokens').on('click', function(e){
e.preventDefault();
let token_container = $(this).closest(".siteseo-ai-token-count");
$('.siteseo-sidebar-ai-tokens').fadeOut(200).fadeIn(200);
$.ajax({
url: siteseo_pro.ajax_url,
method: "POST",
data: {
action: "siteseo_pro_refresh_tokens",
nonce: siteseo_pro.nonce,
},
success: function(res){
if(!res.success){
if(res.data){
alert(res.data);
return;
}
alert("Something went wrong fetching token data");
return;
}
update_dashbord_ai_tokens(res.data);
},
error: function(){
alert("Error refreshing tokens");
}
});
});
function update_dashbord_ai_tokens(remaining_tokens){
let formatted_tokens = remaining_tokens < 0 ? 0 : parseInt(remaining_tokens).toLocaleString('en'),
token_badge = $('.siteseo-ai-token-badge');
// Update the tokens count text
$('.siteseo-sidebar-ai-tokens').text('Tokens Remaining ' + formatted_tokens);
// If you want to add a badge (though it's not in your original HTML)
if(token_badge.length === 0){
$('.siteseo-ai-token-count').prepend(
'' + formatted_tokens + ''
);
} else{
token_badge.text(formatted_tokens);
}
}
/** global schema modal **/
$('#siteseo-auto-schema-modal').dialog({
autoOpen: false,
modal: true,
width: '40%',
minWidth: 500,
maxWidth: 600,
closeOnEscape: false,
dialogClass: 'siteseo-modal',
draggable: false,
resizable: false,
position: { my: "center", at: "center", of: window },
buttons: {
"submit": {
text: "Save",
class: "save-schema-btn",
click: function(){
save_schema();
}
}
},
open: function(){
// add spinner in footer
let $btn = $(this).parent().find('.save-schema-btn');
if(!$btn.next('.spinner').length){
$btn.after('');
}
}
});
/** Add events **/
$('#siteseo-auto-schema-modal').on('dialogopen', function() {
// Add schema rule event
$('.siteseo-add-schema-rule').off('click').on('click', function(e){
e.preventDefault();
let $row = $(this).closest('tr'),
$container = $row.find('.siteseo-schema-rule-container'),
$first = $container.find('select').first();
let $clone = $first.clone();
let $wrapper = $('');
$wrapper.append($clone);
$wrapper.append('');
$container.append($wrapper);
// delete rule event as new elements are added
bind_remove_rule();
});
// Delete rule event
bind_remove_rule();
// Specific targets change event
bind_specific_targets_change();
});
$('#siteseo-auto-schema').on('click', function(e){
e.preventDefault();
// Open modal
$('#siteseo-auto-schema-modal').dialog('open');
// Clear all inputs and selects
$('input[name="schema_id"]').val('');
$('input[name="schema_name"]').val('');
$('select[name="schema_type"]').val('None').trigger('change');
// Clear dynamically added rules
$('.siteseo-schema-rule-container').each(function(){
let $first = $(this).find('select').first().clone();
$(this).empty().append($first);
});
$('.siteseo-specific-input').remove();
// Clear schema properties container
$('#siteseo-schema-properties-show').empty();
// Set modal title
$('#siteseo-auto-schema-modal').dialog('option', 'title', 'Add Schema');
});
/**loads global schema **/
$('#siteseo_schema_type').change(function(e){
let schema_type = $(this).val();
let container = $('#siteseo-schema-properties-show');
container.empty(); // Clear previous properties every time
if(schema_type && siteseo_pro.schema[schema_type]){
if(schema_type === 'BreadcrumbList' || schema_type === 'SearchAction'){
$('#siteseo-schema-properties-container').hide();
} else {
load_schema_properties(schema_type);
$('#siteseo-schema-properties-container').show();
}
} else{
$('#siteseo-schema-properties-container').hide();
}
});
function load_schema_properties(schema_type, existing_properties){
let properties = existing_properties && Object.keys(existing_properties).length ? existing_properties : siteseo_pro.schema[schema_type];
let container = $('#siteseo-schema-properties-show');
container.empty();
function process_properties(propObj, parent_key = '', depth = 0) {
$.each(propObj, function(label, variable) {
let full_key = parent_key ? `${parent_key}[${label}]` : label;
if(typeof variable === 'object' && !Array.isArray(variable)){
// nested object (sub-field group)
let group_row = `
${label}
`;
container.append(group_row);
// Process nested properties
process_properties(variable, full_key, depth + 1);
} else {
// This is a regular property
let value = variable;
// Try to get existing value from nested structure
if(existing_properties){
let keys = full_key.split('[').map(k => k.replace(']', ''));
let current = existing_properties;
for(let i = 0; i < keys.length; i++){
if(current && current[keys[i]] !== undefined){
current = current[keys[i]];
} else {
current = '';
break;
}
}
if(current !== undefined && current !== ''){
value = current;
} else if(existing_properties[label] !== undefined){
// Fallback to flat structure
value = existing_properties[label];
}
}
let row = `