/**
* RSS AI Rewriter Admin JavaScript
*/
(function($) {
'use strict';
let currentArticleId = null;
let rewrittenData = {};
$(document).ready(function() {
// Tab navigation
$('.nav-tab').on('click', function(e) {
e.preventDefault();
const target = $(this).attr('href').substring(1);
$('.nav-tab').removeClass('nav-tab-active');
$(this).addClass('nav-tab-active');
$('.tab-content').hide();
$('#' + target).show();
});
// Fetch all feeds
$('#fetch-all-feeds').on('click', function() {
const $button = $(this);
$button.prop('disabled', true).text('Fetching...');
$.ajax({
url: rssAiRewriter.ajaxUrl,
type: 'POST',
data: {
action: 'fetch_rss_articles',
nonce: rssAiRewriter.nonce
},
success: function(response) {
if (response.success) {
let totalArticles = 0;
response.data.forEach(feed => {
if (feed.articles.count) {
totalArticles += feed.articles.count;
}
});
alert(`Successfully fetched ${totalArticles} articles!`);
location.reload(); // Reload to show new articles
} else {
alert('Error fetching feeds');
}
},
complete: function() {
$button.prop('disabled', false).text('Fetch All Feeds');
}
});
});
// Rewrite article
$('.rewrite-article').on('click', function() {
currentArticleId = $(this).data('id');
const $row = $(this).closest('tr');
const title = $row.find('td:eq(0) strong').text();
$('#preview-title').text(title);
$('#rewrite-modal').show();
// Reset modal
$('.result-section').hide();
rewrittenData = {};
});
// Rewrite buttons
$('.rewrite-controls button[data-rewrite]').on('click', function() {
const type = $(this).data('rewrite');
const $button = $(this);
$button.prop('disabled', true).text('Processing...');
$.ajax({
url: rssAiRewriter.ajaxUrl,
type: 'POST',
data: {
action: 'rewrite_content',
article_id: currentArticleId,
type: type,
nonce: rssAiRewriter.nonce
},
success: function(response) {
if (response.success) {
if (type === 'all') {
// Handle all rewrites
rewrittenData = response.data;
$('#new-title').val(response.data.title);
$('#new-body').val(response.data.body);
$('#new-excerpt').val(response.data.excerpt);
// Handle highlights
const highlights = response.data.highlights.split('\n');
const $highlightsList = $('#highlights-list').empty();
highlights.forEach(highlight => {
if (highlight.trim()) {
$highlightsList.append(`
• ${highlight}
`);
}
});
$('#new-tags').val(response.data.tags);
// Show all sections
$('.result-section').show();
} else {
rewrittenData[type] = response.data;
// Show specific result
switch(type) {
case 'title':
$('#new-title').val(response.data);
$('#result-title').show();
break;
case 'body':
$('#new-body').val(response.data);
$('#result-body').show();
break;
case 'excerpt':
$('#new-excerpt').val(response.data);
$('#result-excerpt').show();
break;
case 'highlights':
const highlights = response.data.split('\n');
const $highlightsList = $('#highlights-list').empty();
highlights.forEach(highlight => {
if (highlight.trim()) {
$highlightsList.append(`• ${highlight}
`);
}
});
$('#result-highlights').show();
break;
case 'tags':
$('#new-tags').val(response.data);
$('#result-tags').show();
break;
}
}
} else {
alert('Error: ' + response.data);
}
},
error: function() {
alert('Error connecting to OpenAI');
},
complete: function() {
$button.prop('disabled', false).text($button.text().replace('Processing...', ''));
}
});
});
// Create post
$('#create-post').on('click', function() {
const $button = $(this);
$button.prop('disabled', true).text('Creating...');
$.ajax({
url: rssAiRewriter.ajaxUrl,
type: 'POST',
data: {
action: 'create_post',
article_id: currentArticleId,
nonce: rssAiRewriter.nonce
},
success: function(response) {
if (response.success) {
alert('Post created successfully!');
window.open(response.data.edit_link, '_blank');
$('#rewrite-modal').hide();
location.reload();
} else {
alert('Error creating post: ' + response.data);
}
},
complete: function() {
$button.prop('disabled', false).text('Create Post');
}
});
});
// Close modal
$('#close-modal').on('click', function() {
$('#rewrite-modal').hide();
});
// Save settings
$('#save-settings').on('click', function(e) {
e.preventDefault();
const $button = $(this);
$button.prop('disabled', true).text('Saving...');
const formData = $('#rss-ai-settings').serializeArray();
formData.push({name: 'action', value: 'save_settings'});
formData.push({name: 'nonce', value: rssAiRewriter.nonce});
$.ajax({
url: rssAiRewriter.ajaxUrl,
type: 'POST',
data: formData,
success: function(response) {
if (response.success) {
alert('Settings saved successfully!');
} else {
alert('Error saving settings');
}
},
complete: function() {
$button.prop('disabled', false).text('Save Settings');
}
});
});
// Add feed
$('#add-feed').on('click', function() {
const feedHtml = `
`;
$('#feeds-list').append(feedHtml);
});
// Remove feed
$(document).on('click', '.remove-feed', function() {
$(this).closest('.feed-item').remove();
});
// Test extraction
$('#test-extraction').on('click', function() {
const url = $('#test-url').val();
if (!url) {
alert('Please enter a URL');
return;
}
const $button = $(this);
$button.prop('disabled', true).text('Testing...');
$.ajax({
url: rssAiRewriter.ajaxUrl,
type: 'POST',
data: {
action: 'test_extraction',
url: url,
nonce: rssAiRewriter.nonce
},
success: function(response) {
if (response.success && response.data) {
const content = response.data.content || 'No content extracted';
$('#extraction-preview').html(`
`);
} else {
let errorMsg = 'Failed to extract content';
if (response.data && response.data.debug) {
errorMsg += ': ' + response.data.debug;
}
$('#extraction-preview').html(`
Error: ${errorMsg}
URL tested: ${url}
Suggestions:
- Check if the URL is accessible
- Try using the Node.js extractor (Option B)
- Add a custom extraction rule for this domain
`);
}
},
complete: function() {
$button.prop('disabled', false).text('Test Extraction');
}
});
});
// Bulk operations
$('#select-all').on('change', function() {
$('input[name="articles[]"]').prop('checked', $(this).is(':checked'));
});
$('#bulk-rewrite').on('click', function() {
const selected = $('input[name="articles[]"]:checked');
if (selected.length === 0) {
alert('Please select articles to rewrite');
return;
}
if (!confirm(`Rewrite ${selected.length} articles? This may take a while and use API credits.`)) {
return;
}
const $button = $(this);
$button.prop('disabled', true);
let processed = 0;
selected.each(function(index) {
const articleId = $(this).val();
setTimeout(() => {
$.ajax({
url: rssAiRewriter.ajaxUrl,
type: 'POST',
data: {
action: 'rewrite_content',
article_id: articleId,
type: 'all',
nonce: rssAiRewriter.nonce
},
success: function() {
processed++;
$button.text(`Processing... ${processed}/${selected.length}`);
if (processed === selected.length) {
alert('Bulk rewrite completed!');
location.reload();
}
}
});
}, index * 2000); // 2 second delay between requests
});
});
});
})(jQuery);