active = get_option($this->active_option_name, false); if ($this->active=='true') { $this->init(); } add_action('wp_ajax_update_active_option_canonical_'.$this->active_option_name, array($this, 'update_active_option_callback')); add_action('wp_ajax_nopriv_update_active_option_canonical_'.$this->active_option_name, array($this, 'update_active_option_callback')); } public function update_active_option_callback() { check_ajax_referer('aiautotool_nonce', 'security'); if (isset($_POST['active'])) { $active = sanitize_text_field($_POST['active']); update_option($this->active_option_name, $active,null, 'no'); print_r($active); } wp_die(); } public function init(){ add_action('admin_menu', array($this, 'add_menu')); add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts')); add_action('wp_ajax_import_from_json', array($this, 'import_posts_from_json')); add_action('wp_ajax_fetch_posts_by_urls', array($this, 'fetch_posts_by_urls_ajax')); add_action('wp_ajax_delete_post_not_in', array($this, 'delete_post_not_in_ajax')); add_action('wp_ajax_restore_trashed_posts_action', array($this, 'restore_trashed_posts_callback')); add_action('wp_ajax_get_post_details', array($this, 'get_post_details_ajax')); add_action('wp_ajax_import_from_json_file', array($this, 'import_from_json_file')); add_action('wp_ajax_aiautotool_backup_database', array($this, 'aiautotool_backup_database_ajax_handler')); } public function render_setting() { } public function render_tab_setting() { } public function render_feature(){ $autoToolBox = new AutoToolBox("Post Exporter", "Export & Import Post from list Url", "#", $this->active_option_name, $this->active,plugins_url('../images/logo.svg', __FILE__)); echo ($autoToolBox->generateHTML()); } public function add_menu() { add_submenu_page( MENUSUBPARRENT, ' Post Export', ' Post Export', 'manage_options', 'post_exporter_menu', array($this, 'render_page') ); } public function enqueue_scripts($hook) { wp_enqueue_script('post-exporter-script', plugin_dir_url(__FILE__) . '../js/post-exporter-script.js', array('jquery'), '1.2'.rand(), true); wp_localize_script('post-exporter-script', 'postExporterData', array( 'nonce' => wp_create_nonce('post_exporter_nonce'), )); } public function render_page() { ?>

Post Exporter

Export :

Import JSON:


metadesc,'Aiautotool'));?>

Restore posts from trash:

get_json_data($json_data_url); foreach ($json_data as $post_data) { $post_id = wp_insert_post(array( 'post_title' => $post_data['title'], 'post_content' => $post_data['content'], 'post_status' => 'publish', 'post_type' => 'post', )); if ($post_id) { // Thêm tags và chuyên mục cho bài viết wp_set_post_terms($post_id, $post_data['tags'], 'post_tag'); wp_set_post_categories($post_id, $post_data['categories']); // Thêm xử lý bổ sung tùy theo nhu cầu của bạn } } wp_die(); } public function import_from_json_file() { check_ajax_referer('post_exporter_nonce', 'security'); $json_data = $_POST['json_data']; $imported_posts = array(); foreach ($json_data as $post_data) { $post_id = wp_insert_post(array( 'post_title' => $post_data['title'], 'post_name' => $post_data['slug'], 'post_content' => $post_data['content'], 'post_status' => 'publish', 'post_type' => 'post', )); if ($post_id) { // Thêm tags và chuyên mục cho bài viết wp_set_post_terms($post_id, $post_data['tags'], 'post_tag'); wp_set_post_categories($post_id, $post_data['categories']); // Thêm xử lý bổ sung tùy theo nhu cầu của bạn $imported_posts[] = $post_id; } } if (!empty($imported_posts)) { wp_send_json_success('Import thành công. Bài viết đã được thêm vào với ID: ' . implode(', ', $imported_posts)); } else { wp_send_json_error('Không có bài viết nào được import.'); } wp_die(); } public function fetch_posts_by_urls_ajax() { check_ajax_referer('post_exporter_nonce', 'security'); $url_list = $_POST['urlList']; $posts = array(); foreach ($url_list as $url) { $post = $this->search_posts_by_url($url); if ($post) { if (function_exists('pll_get_post_language')) { $language = pll_get_post_language($post->ID); } else { $language = get_bloginfo("language"); } $posts[] = array( 'title' => $post->post_title, 'id' => $post->ID, 'content' => $post->post_content, 'slug' => $post->post_name, 'tags' => wp_get_post_tags($post_id, array('fields' => 'names')), 'categories' => wp_get_post_categories($post_id, array('fields' => 'names')), 'language' => $language, ); } } if (!empty($posts)) { wp_send_json_success($posts); } else { wp_send_json_error('Không có bài viết nào được tìm thấy.'); } wp_die(); } public function delete_post_not_in_ajax() { check_ajax_referer('post_exporter_nonce', 'security'); global $wpdb; // $exportData = $_POST['exportData']; // $exportData = isset($_POST['exportData']) ? array_map('absint', array_keys($_POST['exportData'])) : array(); $exportData = isset($_POST['exportData']) ? $_POST['exportData'] : array(); $posts = array(); if (!empty($exportData)) { $tmpid = array(); foreach($exportData as $item){ if (!isset($item['id']) || !is_numeric($item['id']) || $item['id'] <= 0) { wp_send_json_error('Invalid post ID.'); wp_die(); } $tmpid[] = absint($item['id']); } if (empty($tmpid)) { wp_send_json_error('No valid post IDs provided.'); wp_die(); } $previouslyTrashedIDs = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'trash'" ); // Xây dựng danh sách ID cần giữ $keepPostIDs = implode(',', $tmpid); // // Sử dụng SQL để đặt bài viết không nằm trong danh sách vào thùng rác $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_status = 'trash' WHERE post_type = 'post' AND ID NOT IN ($keepPostIDs)" ) ); $currentlyTrashedIDs = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'trash'" ); $newlyTrashedIDs = array_diff($currentlyTrashedIDs, $previouslyTrashedIDs); $trashedLog = get_option('aiautotool_trashed_log', array()); $trashedLog = array_merge($trashedLog, $newlyTrashedIDs); // Lưu lại danh sách ID vào biến option update_option('aiautotool_trashed_log', $trashedLog,null, 'no'); wp_send_json_success('The posts not included in the export list have been successfully deleted.'.$keepPostIDs); } else { wp_send_json_error('Error'); } wp_die(); } public function get_post_details_ajax() { check_ajax_referer('post_exporter_nonce', 'security'); $post_id = $_POST['post_id']; $post = get_post($post_id); if ($post) { $post_details = array( 'title' => $post->post_title, 'content' => $post->post_content, 'slug' => $post->post_name, 'tags' => wp_get_post_tags($post_id, array('fields' => 'names')), 'categories' => wp_get_post_categories($post_id, array('fields' => 'names')), // Thêm các thông tin khác nếu cần ); wp_send_json_success($post_details); } else { wp_send_json_error('Không thể lấy thông tin chi tiết của bài viết.'); } wp_die(); } private function search_posts_by_url($url) { $post_id = url_to_postid($url); if ($post_id) { $post = get_post($post_id); return $post; } else { return null; } } private function get_json_data($json_url) { $response = wp_remote_get($json_url); $body = wp_remote_retrieve_body($response); $json_data = json_decode($body, true); return $json_data; } function restore_trashed_posts_callback() { check_ajax_referer('post_exporter_nonce', 'security'); // Lấy danh sách ID từ biến option trashed_log $trashedLog = get_option('aiautotool_trashed_log', array()); if (!empty($trashedLog)) { global $wpdb; $postIDsString = implode(',', array_map('absint', $trashedLog)); // Sử dụng SQL để cập nhật trạng thái của bài viết thành "publish" $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_status = 'publish' WHERE ID IN ($postIDsString)" ) ); wp_send_json_success('Posts restored successfully.'); } else { wp_send_json_error('No posts to restore.'); } wp_die(); } public function aiautotool_backup_database_ajax_handler() { if ( isset($_POST['action']) && $_POST['action'] == 'aiautotool_backup_database' && isset($_POST['_wpnonce']) && wp_verify_nonce($_POST['_wpnonce'], 'backup_database_nonce') ) { ob_start(); global $wpdb; $query = "SHOW TABLES"; $tables = $wpdb->get_results($query, ARRAY_N); foreach ($tables as $table) { $table = $table[0]; // Giả sử $table là một mảng chứa tên bảng $escaped_table = esc_sql($table); // Thoát tên bảng // Xuất câu lệnh DROP TABLE với tên bảng đã được thoát echo "DROP TABLE IF EXISTS `".esc_sql($escaped_table)."`;\n"; // Bảo vệ tên bảng $result = $wpdb->get_results("SELECT * FROM `$escaped_table`", ARRAY_A); $create_table = $wpdb->get_row("SHOW CREATE TABLE `".esc_sql($escaped_table)."`", ARRAY_N); echo esc_sql($create_table[1]) . ";\n\n"; echo "-- Data for table `".esc_sql($escaped_table)."`\n\n"; foreach ($result as $row) { // Thoát các giá trị trong mảng $row trước khi đưa vào câu lệnh SQL $escaped_row = array_map('esc_sql', $row); // Thoát từng giá trị trong mảng $row echo "INSERT INTO `".esc_sql($escaped_table)."` VALUES ('" . implode("','", esc_sql($escaped_row)) . "');\n"; // Sử dụng biến đã thoát } echo "\n\n"; } $backup_content = ob_get_clean(); // mở ra khi cần sendmail // $filename = WP_CONTENT_DIR.'/uploads/backup-' . date('YmdHis') . '.sql'; // file_put_contents($filename, $backup_content); // sendmail($filename); echo esc_html($backup_content); } else { echo esc_html('Invalid nonce or action.'); } exit(); } }