0) { $schedules['ai_content_x_cron_minutes'] = array( 'interval' => $cronjob * 60, 'display' => __('Every ' . $cronjob . ' minutes') ); } return $schedules; } add_filter('cron_schedules', 'register_cron_schedules'); function ai_content_x_cronjob_set() { if (!isset($_POST)) { return; } $options = get_option('ai_content_x'); $cronjob = intval($options['cronjob']) ?? 0; // minutes if ($cronjob > 0) { if (!wp_next_scheduled('ai_content_x_cronjob')) { wp_schedule_event( time(), 'ai_content_x_cron_minutes', 'ai_content_x_cronjob' ); // Display notice add_action('admin_notices', 'ai_content_x_cronjob_notice'); } } else { wp_clear_scheduled_hook('ai_content_x_cronjob'); } } add_action('admin_init', 'ai_content_x_cronjob_set'); function ai_content_x_cronjob() { $options = get_option('ai_content_x'); $cronjob_number_post = $options['cronjob_number_post'] ?? ''; $extract_title = $options['ai_content_x_extract_title'] ?? ''; $cronjob_number_post = $cronjob_number_post > 0 ? $cronjob_number_post : 1; $cronjob_post_status = $options['cronjob_post_status'] ?? 'draft'; $new_status_post = $options['new_status_post'] ?? 'publish'; $args = array( 'post_status' => $cronjob_post_status ?? 'draft', 'posts_per_page' => $cronjob_number_post ?? 1, 'orderby' => 'ID', 'order' => 'ASC', 'meta_query' => array( array( 'key' => '_ai_content_x_queue', 'value' => 'on', 'compare' => '=' ) ) ); $query = new WP_Query($args); if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post(); $post_id = get_the_ID(); $post = get_post($post_id); $post->post_status = $new_status_post; // update post content $post_title = $post->post_title; $post_content = ($post->post_content); $post->post_content = ai_content_x_generate_content($post_id, $post_title, $post_content); if ($post->post_content) { if ($extract_title) { // extract h1 tag from content using regex preg_match('/

(.*?)<\/h1>/', $post->post_content, $matches); if (isset($matches[1])) { $post->post_title = $matches[1]; } } // replace all h1 tags to h2 $post->post_content = preg_replace('/

(.*?)<\/h1>/', '

$1

', $post->post_content); $post->post_content = ai_content_x_fixed_content($post->post_content); wp_update_post($post); update_post_meta($post_id, '_ai_content_x_queue', 0); update_post_meta($post_id, '_ai_content_x_queue_date', date('Y-m-d H:i:s')); } } } wp_reset_postdata(); } add_action('ai_content_x_cronjob', 'ai_content_x_cronjob'); function ai_content_x_cronjob_notice() { $options = get_option('ai_content_x'); $cronjob = $options['cronjob'] ?? ''; if ($cronjob > 0) { echo '

Cronjob set every ' . $cronjob . ' minutes

'; } else { echo '

Cronjob disabled

'; } } function ai_content_x_generate_content($post_id, $post_title, $post_content) { $options = get_option('ai_content_x'); $prompt = $options['prompt'] ?? ''; $prompt = str_replace('%post_title%', $post_title, $prompt); $prompt = str_replace('%post_content%', $post_content, $prompt); $openai_options = get_option('ai_content_x_openai'); $openai_api_key = $openai_options['ai_content_x_openai_api_key'] ?? ''; $openai_model = $openai_options['ai_content_x_openai_model'] ?? ''; $openai_max_tokens = $openai_options['ai_content_x_openai_max_tokens'] ?? ''; $openai_temperature = $openai_options['ai_content_x_openai_temperature'] ?? ''; $openai_top_p = $openai_options['ai_content_x_openai_top_p'] ?? ''; $openai_frequency_penalty = $openai_options['ai_content_x_openai_frequency_penalty'] ?? ''; $openai_presence_penalty = $openai_options['ai_content_x_openai_presence_penalty'] ?? ''; $openai_stop = $openai_options['ai_content_x_openai_stop'] ?? ''; if (!$openai_api_key || !$openai_model || !$prompt) { saveLogPlugin('error', $post_id, $prompt, 'Error: Missing required fields'); return false; } $data = array( 'prompt' => $prompt, 'model' => $openai_model, 'max_tokens' => intval($openai_max_tokens), 'temperature' => floatval($openai_temperature), 'top_p' => floatval($openai_top_p), 'frequency_penalty' => floatval($openai_frequency_penalty), 'presence_penalty' => floatval($openai_presence_penalty), 'stop' => $openai_stop, ); // Set headers $headers = array( 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $openai_api_key, ); $args = array( 'method' => 'POST', 'body' => json_encode($data), 'timeout' => '60', 'redirection' => '5', 'httpversion' => '1.0', 'blocking' => true, 'headers' => $headers, 'cookies' => array(), ); $request = wp_remote_post('https://api.openai.com/v1/completions', $args); if (is_wp_error($request)) { saveLogPlugin('error', $post_id, $prompt, 'Error ' . json_encode($request)); return false; } $result = wp_remote_retrieve_body($request); $result = json_decode($result, true); if (isset($result['error'])) { saveLogPlugin('error', $post_id, $prompt, 'Error: ' . json_encode($result)); return false; } $text = $result['choices'][0]['text']; saveLogPlugin('success', $post_id, $prompt, $text); return $text; } function saveLogPlugin($type, $post_id, $promp, $message) { // insert into table ai_content_x_logs global $wpdb; $table_name = $wpdb->prefix . 'ai_content_x_logs'; $wpdb->insert( $table_name, array( 'type' => $type, 'post_id' => $post_id, 'prompt' => $promp, 'message' => $message, 'created_at' => current_time('mysql'), ) ); } function ai_content_x_fixed_content($content) { $last_char = substr($content, -1); if ($last_char == '.') { return $content; } $pg = explode(".", $content); // remove last element array_pop($pg); $content = implode(".", $pg); return $content . "."; }