';
private $trashed_option_name = 'aiautotool_post_exporter_trashed_posts';
public function __construct() {
$this->active = get_option($this->active_option_name, false);
if ($this->active === 'true' || $this->active === true) {
$this->init();
}
add_action('wp_ajax_update_active_option_canonical_' . $this->active_option_name, array($this, 'update_active_option_callback'));
}
public function init() {
add_action('admin_enqueue_scripts', array($this, 'enqueue_assets'));
add_action('wp_ajax_fetch_posts_by_urls', array($this, 'fetch_posts_by_urls_callback'));
add_action('wp_ajax_import_from_json', array($this, 'import_from_json_callback'));
add_action('wp_ajax_import_from_json_file', array($this, 'import_from_json_file_callback'));
add_action('wp_ajax_delete_post_not_in', array($this, 'delete_post_not_in_callback'));
add_action('wp_ajax_restore_trashed_posts_action', array($this, 'restore_trashed_posts_callback'));
}
public function enqueue_assets($hook) {
if (!$this->is_post_exporter_page($hook)) {
return;
}
$version = defined('AIAUTOTOOL_VERSION_CACHE') ? AIAUTOTOOL_VERSION_CACHE : '1.0';
$base_url = plugin_dir_url(__FILE__);
wp_enqueue_script(
'aiautotool-post-exporter',
$base_url . 'assets/js/post-exporter-script.js',
array('jquery'),
$version,
true
);
wp_localize_script('aiautotool-post-exporter', 'postExporterData', array(
'nonce' => wp_create_nonce('post_exporter_nonce'),
));
}
private function is_post_exporter_page($hook = '') {
if (strpos((string) $hook, 'post-exporter') !== false) {
return true;
}
return isset($_GET['page']) && sanitize_text_field(wp_unslash($_GET['page'])) === 'post-exporter';
}
private function verify_ajax_request() {
check_ajax_referer('post_exporter_nonce', 'security');
if (!current_user_can('manage_options')) {
wp_send_json_error(__('Permission denied', 'ai-auto-tool'), 403);
}
}
public function render_admin_page() {
if (!current_user_can('manage_options')) {
wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'ai-auto-tool'));
}
?>
verify_ajax_request();
$url_list = isset($_POST['urlList']) ? (array) wp_unslash($_POST['urlList']) : array();
$posts = array();
foreach ($url_list as $url) {
$url = esc_url_raw(trim((string) $url));
if ($url === '') {
continue;
}
$post_id = url_to_postid($url);
if (!$post_id) {
$path = wp_parse_url($url, PHP_URL_PATH);
if ($path) {
$post = get_page_by_path(trim($path, '/'), OBJECT, get_post_types(array('public' => true)));
$post_id = $post ? $post->ID : 0;
}
}
$post = $post_id ? get_post($post_id) : null;
if (!$post || $post->post_type === 'attachment') {
continue;
}
$posts[] = $this->format_post_for_export($post);
}
if (empty($posts)) {
wp_send_json_error(__('No posts found for the provided URLs.', 'ai-auto-tool'));
}
wp_send_json_success($posts);
}
public function import_from_json_callback() {
$this->verify_ajax_request();
$json_url = isset($_POST['json_data_url']) ? esc_url_raw(wp_unslash($_POST['json_data_url'])) : '';
if ($json_url === '') {
wp_send_json_error(__('Missing JSON URL.', 'ai-auto-tool'));
}
$response = wp_remote_get($json_url, array('timeout' => 20));
if (is_wp_error($response)) {
wp_send_json_error($response->get_error_message());
}
$this->import_posts_from_json(wp_remote_retrieve_body($response));
}
public function import_from_json_file_callback() {
$this->verify_ajax_request();
$json_data = isset($_POST['json_data']) ? wp_unslash($_POST['json_data']) : null;
if ($json_data === null) {
wp_send_json_error(__('Missing JSON data.', 'ai-auto-tool'));
}
$this->import_posts_from_decoded_data($json_data);
}
private function import_posts_from_json($json) {
$decoded = json_decode((string) $json, true);
if (json_last_error() !== JSON_ERROR_NONE) {
wp_send_json_error(__('Invalid JSON data.', 'ai-auto-tool'));
}
$this->import_posts_from_decoded_data($decoded);
}
private function import_posts_from_decoded_data($posts) {
if (!is_array($posts)) {
wp_send_json_error(__('Invalid JSON payload.', 'ai-auto-tool'));
}
$created = 0;
foreach ($posts as $post_data) {
if (!is_array($post_data) || empty($post_data['title'])) {
continue;
}
$post_id = wp_insert_post(array(
'post_title' => sanitize_text_field($post_data['title']),
'post_content' => isset($post_data['content']) ? wp_kses_post($post_data['content']) : '',
'post_name' => isset($post_data['slug']) ? sanitize_title($post_data['slug']) : '',
'post_status' => 'draft',
'post_type' => 'post',
), true);
if (is_wp_error($post_id)) {
continue;
}
if (!empty($post_data['tags'])) {
wp_set_post_terms($post_id, $this->parse_terms($post_data['tags']), 'post_tag', false);
}
if (!empty($post_data['categories'])) {
wp_set_post_terms($post_id, $this->parse_terms($post_data['categories']), 'category', false);
}
$created++;
}
wp_send_json_success(sprintf(__('Imported %d posts as drafts.', 'ai-auto-tool'), $created));
}
public function delete_post_not_in_callback() {
$this->verify_ajax_request();
$export_data = isset($_POST['exportData']) ? (array) wp_unslash($_POST['exportData']) : array();
$keep_ids = array();
foreach ($export_data as $item) {
if (is_array($item) && !empty($item['id'])) {
$keep_ids[] = absint($item['id']);
}
}
$keep_ids = array_filter(array_unique($keep_ids));
if (empty($keep_ids)) {
wp_send_json_error(__('No posts selected to keep.', 'ai-auto-tool'));
}
$query = new WP_Query(array(
'post_type' => 'post',
'post_status' => array('publish', 'draft', 'pending', 'future', 'private'),
'posts_per_page' => -1,
'fields' => 'ids',
'post__not_in' => $keep_ids,
'no_found_rows' => true,
));
$trashed = array();
foreach ($query->posts as $post_id) {
if (wp_trash_post($post_id)) {
$trashed[] = absint($post_id);
}
}
$existing = get_option($this->trashed_option_name, array());
$existing = is_array($existing) ? array_map('absint', $existing) : array();
update_option($this->trashed_option_name, array_values(array_unique(array_merge($existing, $trashed))), false);
wp_send_json_success(sprintf(__('Moved %d posts to trash.', 'ai-auto-tool'), count($trashed)));
}
public function restore_trashed_posts_callback() {
$this->verify_ajax_request();
$trashed_ids = get_option($this->trashed_option_name, array());
$trashed_ids = is_array($trashed_ids) ? array_map('absint', $trashed_ids) : array();
$restored = 0;
foreach ($trashed_ids as $post_id) {
if (get_post_status($post_id) === 'trash' && wp_untrash_post($post_id)) {
$restored++;
}
}
delete_option($this->trashed_option_name);
wp_send_json_success(sprintf(__('Restored %d posts.', 'ai-auto-tool'), $restored));
}
private function format_post_for_export($post) {
$tags = wp_get_post_terms($post->ID, 'post_tag', array('fields' => 'names'));
$categories = wp_get_post_terms($post->ID, 'category', array('fields' => 'names'));
return array(
'id' => $post->ID,
'title' => get_the_title($post),
'content' => $post->post_content,
'slug' => $post->post_name,
'tags' => is_wp_error($tags) ? array() : $tags,
'categories' => is_wp_error($categories) ? array() : $categories,
'language' => get_locale(),
);
}
private function parse_terms($terms) {
if (is_array($terms)) {
return array_map('sanitize_text_field', $terms);
}
return array_filter(array_map('trim', explode(',', sanitize_text_field((string) $terms))));
}
public function update_active_option_callback() {
check_ajax_referer('aiautotool_nonce', 'security');
if (!current_user_can('manage_options')) {
wp_die(__('Permission denied', 'ai-auto-tool'), '', array('response' => 403));
}
if (isset($_POST['active'])) {
$active = sanitize_text_field(wp_unslash($_POST['active']));
update_option($this->active_option_name, $active, false);
wp_send_json_success(array('active' => $active));
}
wp_send_json_error('Invalid request');
}
public function render_feature() {
$box = new AutoToolBox(
$this->icon . ' Post Exporter',
'Export, import, delete, and restore WordPress posts',
admin_url('admin.php?page=post-exporter'),
$this->active_option_name,
$this->active,
'',
false
);
echo $box->generateHTML();
}
}
}