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'); ?>