172 lines
6.7 KiB
JavaScript
172 lines
6.7 KiB
JavaScript
document.addEventListener('DOMContentLoaded', function() {
|
|
// Initialize Sortable for rule containers
|
|
document.querySelectorAll('.lcp-rules-container').forEach(container => {
|
|
new Sortable(container, {
|
|
handle: '.dashicons-menu',
|
|
animation: 150
|
|
});
|
|
});
|
|
|
|
// Add new rule
|
|
document.addEventListener('click', function(e) {
|
|
if (e.target.matches('.add-rule')) {
|
|
const postType = e.target.dataset.postType;
|
|
const template = document.querySelector('.lcp-rule-template').innerHTML;
|
|
const container = e.target.closest('.lcp-post-type-rules').querySelector('.lcp-rules-container');
|
|
container.insertAdjacentHTML('beforeend', template);
|
|
}
|
|
});
|
|
|
|
// Delete rule
|
|
document.addEventListener('click', function(e) {
|
|
// Check if the click was on the delete button or its child icon
|
|
if (e.target.matches('.delete-rule') || e.target.closest('.delete-rule')) {
|
|
const ruleElement = e.target.closest('.lcp-rule');
|
|
if (ruleElement) {
|
|
ruleElement.remove();
|
|
}
|
|
}
|
|
});
|
|
|
|
// Update taxonomy and terms fields visibility
|
|
document.addEventListener('change', function(e) {
|
|
if (e.target.matches('.rule-value-type')) {
|
|
const ruleBody = e.target.closest('.rule-body');
|
|
if (!ruleBody) return; // Exit if rule-body not found
|
|
|
|
const taxonomySelect = ruleBody.querySelector('.rule-taxonomy');
|
|
const termSelect = ruleBody.querySelector('.rule-term');
|
|
|
|
if (!taxonomySelect || !termSelect) return; // Exit if selects not found
|
|
|
|
if (e.target.value === 'taxonomy') {
|
|
taxonomySelect.style.display = 'inline-block';
|
|
termSelect.style.display = 'inline-block';
|
|
} else {
|
|
taxonomySelect.style.display = 'none';
|
|
termSelect.style.display = 'none';
|
|
}
|
|
}
|
|
});
|
|
|
|
// Update terms when taxonomy changes
|
|
document.addEventListener('change', function(e) {
|
|
if (e.target.matches('.rule-taxonomy')) {
|
|
const termSelect = e.target.nextElementSibling;
|
|
if (!termSelect) return; // Exit if term select not found
|
|
|
|
const taxonomy = e.target.value;
|
|
|
|
fetch(lcpPaywall.ajaxurl, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
},
|
|
body: new URLSearchParams({
|
|
action: 'lcp_get_taxonomy_terms',
|
|
nonce: lcpPaywall.nonce,
|
|
taxonomy: taxonomy
|
|
})
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.success) {
|
|
termSelect.innerHTML = '';
|
|
data.data.forEach(term => {
|
|
const option = document.createElement('option');
|
|
option.value = term.term_id;
|
|
option.textContent = term.name;
|
|
termSelect.appendChild(option);
|
|
});
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
// Save individual post type rules
|
|
document.addEventListener('click', function(e) {
|
|
if (e.target.matches('.save-post-type-rules')) {
|
|
const postTypeSection = e.target.closest('.lcp-post-type-rules');
|
|
const postType = postTypeSection.dataset.postType;
|
|
|
|
const rules = {
|
|
[postType]: {
|
|
settings: {
|
|
default_lock_status: postTypeSection.querySelector('.default-lock-status').value,
|
|
is_metered: postTypeSection.querySelector('.is-metered').checked,
|
|
metered_free_posts: parseInt(postTypeSection.querySelector('.metered-free-posts').value),
|
|
metered_interval: parseInt(postTypeSection.querySelector('.metered-interval').value)
|
|
},
|
|
rules: []
|
|
}
|
|
};
|
|
|
|
postTypeSection.querySelectorAll('.lcp-rule').forEach(ruleElement => {
|
|
const rule = {
|
|
action: ruleElement.querySelector('.rule-action').value,
|
|
condition: ruleElement.querySelector('.rule-condition').value,
|
|
operator: ruleElement.querySelector('.rule-operator').value,
|
|
value_type: ruleElement.querySelector('.rule-value-type').value,
|
|
taxonomy: ruleElement.querySelector('.rule-taxonomy')?.value || '',
|
|
term: ruleElement.querySelector('.rule-term')?.value || ''
|
|
};
|
|
rules[postType].rules.push(rule);
|
|
});
|
|
|
|
fetch(lcpPaywall.ajaxurl, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
},
|
|
body: new URLSearchParams({
|
|
action: 'lcp_save_rules',
|
|
nonce: lcpPaywall.nonce,
|
|
rules: JSON.stringify(rules)
|
|
})
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.success) {
|
|
alert(`Rules for ${postType} saved successfully!`);
|
|
} else {
|
|
alert(`Failed to save rules for ${postType}. Please try again.`);
|
|
}
|
|
})
|
|
.catch(() => {
|
|
alert(`Failed to save rules for ${postType}. Please try again.`);
|
|
});
|
|
}
|
|
});
|
|
|
|
// Save settings
|
|
document.addEventListener('click', function(e) {
|
|
if (e.target.matches('#save-settings')) {
|
|
// Get the content from the WordPress editor
|
|
const content = wp.editor.getContent('lcp_nag_message');
|
|
|
|
fetch(lcpPaywall.ajaxurl, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
},
|
|
body: new URLSearchParams({
|
|
action: 'lcp_save_settings',
|
|
nonce: lcpPaywall.nonce,
|
|
nag_message: content
|
|
})
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.success) {
|
|
alert('Settings saved successfully!');
|
|
} else {
|
|
alert('Failed to save settings. Please try again.');
|
|
}
|
|
})
|
|
.catch(() => {
|
|
alert('Failed to save settings. Please try again.');
|
|
});
|
|
}
|
|
});
|
|
});
|