Files
igny8/igny8-wp-plugin/admin/assets/js/post-editor.js
2025-11-30 00:54:44 +05:00

201 lines
8.0 KiB
JavaScript

/**
* Post Editor JavaScript
*
* Handles AJAX interactions for Planner and Optimizer meta boxes
*
* @package Igny8Bridge
*/
(function($) {
'use strict';
$(document).ready(function() {
// Fetch Planner Brief
$('#igny8-fetch-brief').on('click', function() {
var $button = $(this);
var $message = $('#igny8-planner-brief-message');
var postId = $button.data('post-id');
var taskId = $button.data('task-id');
$button.prop('disabled', true).text('Fetching...');
$message.hide().removeClass('notice-success notice-error');
$.ajax({
url: igny8PostEditor.ajaxUrl,
type: 'POST',
data: {
action: 'igny8_fetch_planner_brief',
nonce: igny8PostEditor.nonce,
post_id: postId,
task_id: taskId
},
success: function(response) {
if (response.success) {
$message.addClass('notice notice-success inline')
.html('<p>' + response.data.message + '</p>')
.show();
// Reload page to show updated brief
setTimeout(function() {
location.reload();
}, 1000);
} else {
$message.addClass('notice notice-error inline')
.html('<p>' + (response.data.message || 'Failed to fetch brief') + '</p>')
.show();
$button.prop('disabled', false).text('Fetch Brief');
}
},
error: function() {
$message.addClass('notice notice-error inline')
.html('<p>Request failed</p>')
.show();
$button.prop('disabled', false).text('Fetch Brief');
}
});
});
// Refresh Planner Task
$('#igny8-refresh-task').on('click', function() {
var $button = $(this);
var $message = $('#igny8-planner-brief-message');
var postId = $button.data('post-id');
var taskId = $button.data('task-id');
if (!confirm('Are you sure you want to request a refresh of this task from IGNY8 Planner?')) {
return;
}
$button.prop('disabled', true).text('Requesting...');
$message.hide().removeClass('notice-success notice-error');
$.ajax({
url: igny8PostEditor.ajaxUrl,
type: 'POST',
data: {
action: 'igny8_refresh_planner_task',
nonce: igny8PostEditor.nonce,
post_id: postId,
task_id: taskId
},
success: function(response) {
if (response.success) {
$message.addClass('notice notice-success inline')
.html('<p>' + response.data.message + '</p>')
.show();
} else {
$message.addClass('notice notice-error inline')
.html('<p>' + (response.data.message || 'Failed to request refresh') + '</p>')
.show();
}
$button.prop('disabled', false).text('Request Refresh');
},
error: function() {
$message.addClass('notice notice-error inline')
.html('<p>Request failed</p>')
.show();
$button.prop('disabled', false).text('Request Refresh');
}
});
});
// Create Optimizer Job
$('#igny8-create-optimizer-job').on('click', function() {
var $button = $(this);
var $message = $('#igny8-optimizer-message');
var postId = $button.data('post-id');
var taskId = $button.data('task-id');
if (!confirm('Create a new optimizer job for this post?')) {
return;
}
$button.prop('disabled', true).text('Creating...');
$message.hide().removeClass('notice-success notice-error');
$.ajax({
url: igny8PostEditor.ajaxUrl,
type: 'POST',
data: {
action: 'igny8_create_optimizer_job',
nonce: igny8PostEditor.nonce,
post_id: postId,
task_id: taskId,
job_type: 'audit',
priority: 'normal'
},
success: function(response) {
if (response.success) {
$message.addClass('notice notice-success inline')
.html('<p>' + response.data.message + '</p>')
.show();
// Reload page to show updated status
setTimeout(function() {
location.reload();
}, 1000);
} else {
$message.addClass('notice notice-error inline')
.html('<p>' + (response.data.message || 'Failed to create job') + '</p>')
.show();
$button.prop('disabled', false).text('Request Optimization');
}
},
error: function() {
$message.addClass('notice notice-error inline')
.html('<p>Request failed</p>')
.show();
$button.prop('disabled', false).text('Request Optimization');
}
});
});
// Check Optimizer Status
$('#igny8-check-optimizer-status').on('click', function() {
var $button = $(this);
var $message = $('#igny8-optimizer-message');
var postId = $button.data('post-id');
var jobId = $button.data('job-id');
$button.prop('disabled', true).text('Checking...');
$message.hide().removeClass('notice-success notice-error');
$.ajax({
url: igny8PostEditor.ajaxUrl,
type: 'POST',
data: {
action: 'igny8_get_optimizer_status',
nonce: igny8PostEditor.nonce,
post_id: postId,
job_id: jobId
},
success: function(response) {
if (response.success) {
$message.addClass('notice notice-success inline')
.html('<p>Status: <strong>' + response.data.status + '</strong></p>')
.show();
// Reload page to show updated status
setTimeout(function() {
location.reload();
}, 1000);
} else {
$message.addClass('notice notice-error inline')
.html('<p>' + (response.data.message || 'Failed to get status') + '</p>')
.show();
}
$button.prop('disabled', false).text('Check Status');
},
error: function() {
$message.addClass('notice notice-error inline')
.html('<p>Request failed</p>')
.show();
$button.prop('disabled', false).text('Check Status');
}
});
});
});
})(jQuery);