is_plan( 'aikct_pro' ) ) { if (!class_exists('WP_List_Table')) { require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; } if (!class_exists('Aikct_Rewrite_post_List_Table')) { class Aikct_Rewrite_post_List_Table extends WP_List_Table { public function __construct() { parent::__construct(array( 'singular' => __('Rewrite Post', 'ai-seo-translator'), 'plural' => __('Rewrite Posts', 'ai-seo-translator'), 'ajax' => false )); } public function prepare_items() { $per_page = 10; $current_page = $this->get_pagenum(); $all_items = $this->get_items(); $total_items = count($all_items); $this->set_pagination_args(array( 'total_items' => $total_items, 'per_page' => $per_page, 'total_pages' => ceil($total_items / $per_page) )); $this->items = array_slice($all_items, (($current_page - 1) * $per_page), $per_page); } public function get_column_info() { $columns = [ 'stt' => 'STT', 'url' => 'URL', 'prompt' => 'Prompt', 'status' => 'Status', 'date_in' => 'Date', 'actions' => 'Actions' ]; $hidden = []; $sortable = []; return [$columns, $hidden, $sortable]; } public function get_items() { global $wpdb; $table_name = $wpdb->prefix . 'aikct_rewrite_post'; // Retrieve data from the custom table $query = "SELECT * FROM $table_name"; $results = $wpdb->get_results($query, ARRAY_A); $data = []; foreach ($results as $index => $item) { $data[] = [ 'stt' => $index + 1, 'url' => isset($item['url']) ? sprintf('%s', esc_url($item['url']), esc_url($item['url'])) : '', 'prompt' => isset($item['prompt']) ? esc_html($item['prompt']) : '', 'status' => isset($item['status']) ? ($item['status'] == 0 ? ' Pending' : ' Completed') : 'Unknown', 'date_in' => isset($item['date_in']) ? esc_html($item['date_in']) : '', 'actions' => sprintf( 'Delete', esc_attr($item['id']), wp_create_nonce('delete_url_prompt_nonce') ) ]; } return $data; } public function column_default($item, $column_name) { switch ($column_name) { case 'stt': return $item['stt']; case 'url': case 'prompt': case 'status': case 'date_in': return $item[$column_name]; case 'actions': return $item[$column_name]; default: return print_r($item, true); } } } } class aikct_rewrite_post { private $option_name = 'aikct_rewrite_post_options'; private $cron_hook = 'aikct_rewrite_post_cron_hook'; public function __construct() { // Hook to register settings page add_action('admin_menu', array($this, 'register_settings_page')); add_action('admin_init', array($this, 'register_settings')); add_action('init', array($this, 'schedule_cron')); add_action($this->cron_hook, array($this, 'run_cron_job')); add_filter('cron_schedules', array($this, 'add_custom_cron_interval')); } public function add_custom_cron_interval($schedules) { // Lấy giá trị đã lưu từ wp_options (mặc định là 60 phút nếu chưa có giá trị) $cron_time = get_option('aikct_rewrite_post_cron_time', 60); // Tính toán thời gian cron theo giây $interval_seconds = intval($cron_time) * 60; // Thêm vào cron schedule $schedules['aikct_rewrite_post_interval'] = array( 'interval' => $interval_seconds, 'display' => sprintf(__('Every %d Minutes', 'ai-seo-translator'), $cron_time) ); return $schedules; } public function schedule_cron() { // Lấy giá trị thời gian từ wp_options (mặc định 60 phút) $cron_time = get_option('aikct_rewrite_post_cron_time', 60); // Kiểm tra xem cron job đã được lên lịch hay chưa if (!wp_next_scheduled($this->cron_hook)) { wp_schedule_event(time(), 'aikct_rewrite_post_interval', $this->cron_hook); } } public function run_cron_job() { global $wpdb; // Tên bảng của bạn $table_name = $wpdb->prefix . 'aikct_rewrite_post'; // Lấy item đầu tiên có status = 0 từ bảng cơ sở dữ liệu $item = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $table_name WHERE status = %d ORDER BY id ASC LIMIT 1", 0), ARRAY_A ); // Kiểm tra nếu không có item nào if (empty($item)) { return; // Kết thúc nếu không có item để xử lý } // Lấy URL từ item $random_url = esc_url_raw($item['url']); $post_id = url_to_postid($random_url); // Kiểm tra post ID có hợp lệ không if (!$post_id) { return; // Kết thúc nếu không tìm thấy bài viết với URL này } // Lấy thông tin bài viết $post = get_post($post_id); if (!$post || $post->post_status !== 'publish') { return; // Kết thúc nếu bài viết không hợp lệ hoặc không được xuất bản } // Lấy categories và tags của bài viết $categories = get_the_category($post_id); $category_names = wp_list_pluck($categories, 'name'); $tags = get_the_tags($post_id); $tag_names = $tags ? wp_list_pluck($tags, 'name') : []; // Tạo mảng thay thế cho các placeholder $arr_replace_prompt = [ '%TITLE%', '%CONTENT%', '%AUTHOR%', '%CATEGORIES_NAME%', '%TAGS_NAME%' ]; // Các giá trị cần thay thế trong prompt $replace_values = [ $post->post_title, // %TITLE% $post->post_content, // %CONTENT% get_the_author_meta('display_name', $post->post_author), // %AUTHOR% implode(', ', $category_names), // %CATEGORIES_NAME% implode(', ', $tag_names) // %TAGS_NAME% ]; // Lấy prompt từ item $prompt_content = isset($item['prompt']) ? sanitize_textarea_field($item['prompt']) : ''; // Thay thế các placeholder trong prompt $replaced_prompt_content = str_replace($arr_replace_prompt, $replace_values, $prompt_content); // Gửi nội dung đã thay thế tới API qua proxy $proxy = new kctaiproxy(); if ($proxy->status) { $result = $proxy->sendRequest($replaced_prompt_content); $content_new = $result['output']; } else { $content_new = 'Please set API KEY'; } if ($content_new !== 'Please set API KEY') { // Cập nhật nội dung bài viết và trạng thái item update_post_meta($post_id, '_old_content', $post->post_content); wp_update_post([ 'ID' => $post_id, 'post_content' => wp_kses_post($content_new), ]); print_r($item); // Cập nhật trạng thái và ngày vào cơ sở dữ liệu $wpdb->update( $table_name, [ 'status' => 1, 'date_in' => current_time('mysql') ], ['id' => $item['id']], // Điều kiện để xác định bản ghi cần cập nhật ['%s', '%s'], ['%d'] ); } } public function register_settings_page() { add_submenu_page( 'kctai-settings', 'Rewrite Post Settings', 'Rewrite Post Settings', 'manage_options', 'aikct_rewrite_post', array($this, 'render_settings_page') ); } public function register_settings() { register_setting($this->option_name, 'aikct_rewrite_post_cron_time', array( 'type' => 'integer', 'sanitize_callback' => 'absint', )); } public function render_settings_page() { if (isset($_POST['save_rewrite_keys_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['save_rewrite_keys_nonce'])), 'save_rewrite_keys_action')) { if (isset($_POST['timerun'])) { update_option('aikct_rewrite_post_cron_time', sanitize_text_field($_POST['timerun'])); } } $selected_timerun = get_option('aikct_rewrite_post_cron_time', '1'); ?>

Rewrite Post Settings

prepare_items(); $list_table->display(); ?>
option_name); ?>

Config time run crontab

' . esc_html(__('Add Prompt','ai-seo-translator')) . ''; ?>
'URL not provided.')); return; } // Sanitize the input URL $random_url = esc_url_raw($_POST['random_url']); // Get the post ID from the URL $post_id = url_to_postid($random_url); // Check if the post ID is valid if (!$post_id) { wp_send_json_error(array('message' => 'Post ID does not exist for the provided URL.')); return; } // Retrieve the post based on the post ID $post = get_post($post_id); // Check if the post exists and is valid if (!$post || $post->post_status !== 'publish') { wp_send_json_error(array('message' => 'Post not found or is not published.')); return; } // Retrieve post categories and tags $categories = get_the_category($post_id); $category_names = wp_list_pluck($categories, 'name'); $tags = get_the_tags($post_id); $tag_names = $tags ? wp_list_pluck($tags, 'name') : []; // Create a replacement array with post values $replace_values = [ $post->post_title, // %TITLE% $post->post_content, // %CONTENT% get_the_author_meta('display_name', $post->post_author), // %AUTHOR% implode(', ', $category_names), // %CATEGORIES_NAME% implode(', ', $tag_names) // %TAGS_NAME% ]; // Get the prompt content to be replaced $prompt_content = isset($_POST['prompt_content']) ? sanitize_textarea_field($_POST['prompt_content']) : ''; // Replace the placeholders in the prompt content $replaced_prompt_content = str_replace($arr_replace_prompt, $replace_values, $prompt_content); $proxy = new kctaiproxy(); if($proxy->status){ $result = $proxy->sendRequest($replaced_prompt_content); $content_new = $result['output']; }else{ $content_new = 'Please set API KEY'; } wp_send_json_success(array( 'post_id' => $post_id, 'post_title' => $post->post_title, 'post_content' => $post->post_content, 'post_content_new' => $content_new, 'replaced_prompt_content' => $replaced_prompt_content )); } // Đăng ký action cho AJAX request add_action('wp_ajax_load_customer_prompt_properties', 'aikct_load_customer_prompt_properties'); function aikct_load_customer_prompt_properties() { if ( !current_user_can( 'edit_posts' ) ) { wp_send_json_error( 'You do not have permission to perform this action.' ); wp_die(); } $arr_replace_prompt = ['%TITLE%','%CONTENT%','%AUTHOR%','%CATEGORIES_NAME%','%TAGS_NAME%']; $prompt_id = isset($_POST['prompt_id']) ? sanitize_text_field($_POST['prompt_id']) : ''; $postprompt = get_post($prompt_id); if (!empty($prompt_id)) { $params = get_post_meta($prompt_id, '_aikct_prompt_params', true); $arr = array(); $arr['prompt_content'] = $postprompt->post_content; $arr['prompt_params'] = $params; $arr['prompt_arr_replace'] = $arr_replace_prompt; echo wp_send_json_success($arr); wp_die(); } wp_die('Invalid prompt ID'); } add_action('wp_ajax_save_url_prompts', 'aikct_save_url_prompts'); function aikct_save_url_prompts() { if ( !current_user_can( 'edit_posts' ) ) { wp_send_json_error( 'You do not have permission to perform this action.' ); wp_die(); } global $wpdb; $table_name = $wpdb->prefix . 'aikct_rewrite_post'; if (!isset($_POST['items']) || !is_array($_POST['items'])) { wp_send_json_error(array('message' => 'Invalid data.')); return; } // Prepare data for insertion $url_prompt_map = $_POST['items']; // Process new items foreach ($url_prompt_map as $new_item) { if (isset($new_item['url'])) { $url = $new_item['url']; $data = array( 'url' => $url, 'prompt' => isset($new_item['prompt']) ? $new_item['prompt'] : '', 'status' => isset($new_item['status']) ? ($new_item['status'] == 'true' ? 1 : 0) : 0, 'date_in' => isset($new_item['date_in']) ? $new_item['date_in'] : current_time('mysql') ); $format = array('%s', '%s', '%d', '%s'); // Update or insert data $existing_row = $wpdb->get_row($wpdb->prepare("SELECT id FROM $table_name WHERE url = %s", $url)); if ($existing_row) { // Update existing row $wpdb->update($table_name, $data, array('url' => $url), $format); } else { // Insert new row $wpdb->insert($table_name, $data, $format); } } } wp_send_json_success(array('message' => 'Data saved successfully.')); } add_action('wp_ajax_delete_url_prompt', 'aikct_delete_url_prompt'); function aikct_delete_url_prompt() { if ( !current_user_can( 'edit_posts' ) ) { wp_send_json_error( 'You do not have permission to perform this action.' ); wp_die(); } if (!isset($_GET['_wpnonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['_wpnonce'])), 'delete_url_prompt_nonce')) { wp_send_json_error(array('message' => 'Nonce verification failed.')); return; } if (!isset($_GET['id'])) { wp_send_json_error(array('message' => 'No ID provided.')); return; } global $wpdb; $table_name = $wpdb->prefix . 'aikct_rewrite_post'; $id = intval($_GET['id']); $result = $wpdb->delete($table_name, ['id' => $id], ['%d']); if ($result !== false) { wp_send_json_success(array('message' => 'Item deleted successfully.')); } else { wp_send_json_error(array('message' => 'Failed to delete item.')); } } // Hook vào khi plugin được kích hoạt add_action('init', 'aikct_rewrite_post_check_and_create_table'); function aikct_rewrite_post_check_and_create_table() { global $wpdb; $table_name = $wpdb->prefix . 'aikct_rewrite_post'; // Kiểm tra xem bảng đã tồn tại chưa if ($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) { // Nếu bảng chưa tồn tại, tạo bảng $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE $table_name ( id BIGINT(20) UNSIGNED AUTO_INCREMENT PRIMARY KEY, url VARCHAR(255) NOT NULL, prompt TEXT NOT NULL, status BOOLEAN NOT NULL DEFAULT 0, date_in DATETIME NOT NULL, UNIQUE KEY url (url) ) $charset_collate;"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); } } }else{ }