test

Posted on

cron_hook, [$this,’generate_and_insert_post’]);
add_action(‘admin_post_apag_generate_now’, [$this,’handle_generate_now’]);
}

/* Admin menu */
public function admin_menu(){
add_options_page(‘Auto Pro Article’, ‘Auto Pro Article’, ‘manage_options’, ‘apag-settings’, [$this,’settings_page’]);
}

/* Register settings */
public function register_settings(){
register_setting($this->option_name, $this->option_name, [$this,’sanitize’]);
}

public function sanitize($input){
$output = [];
$output[‘api_key’] = sanitize_text_field($input[‘api_key’] ?? ”);
$output[‘category’] = intval($input[‘category’] ?? 0);
$output[‘interval’] = sanitize_text_field($input[‘interval’] ?? ‘daily’);
$output[‘prompt_template’] = wp_kses_post($input[‘prompt_template’] ?? $this->default_prompt());
$output[‘post_status’] = in_array($input[‘post_status’] ?? ‘draft’, [‘draft’,’publish’]) ? $input[‘post_status’] : ‘draft’;
$output[‘max_tokens’] = intval($input[‘max_tokens’] ?? 800);
return $output;
}

private function default_prompt(){
return “Write a professional, well-structured article in Indonesian about the topic: {topic}. Length: about 600-800 words. Include an engaging introduction, 3-5 informative subheadings, and a concise conclusion. Use an authoritative, friendly tone. Add suggested SEO title and meta description.”;
}

/* Settings page */
public function settings_page(){
if (!current_user_can(‘manage_options’)) wp_die(‘Unauthorized’);
$opts = get_option($this->option_name, []);
?>

Auto Pro Article Settings

option_name); do_settings_sections($this->option_name); ?>

OpenAI API Key option_name); ?>[interval]”>
Post Status
Max tokens / length option_name); ?>[prompt_template]” rows=”6″ cols=”80″>default_prompt()); ?>

Use {topic} placeholder for topic. Keep instructions clear for professional tone.


Manual generation




generate_and_insert_post($topic);
wp_redirect(admin_url(‘options-general.php?page=apag-settings’));
exit;
}

/* Setup WP-Cron */
public function setup_cron(){
$opts = get_option($this->option_name, []);
$interval = $opts[‘interval’] ?? ‘daily’;
// clear existing then schedule
wp_clear_scheduled_hook($this->cron_hook);
if (!wp_next_scheduled($this->cron_hook)) {
wp_schedule_event(time()+60, $interval, $this->cron_hook);
}
}

/* Core: generate content and insert post */
public function generate_and_insert_post($manual_topic = null){
$opts = get_option($this->option_name, []);
if (empty($opts[‘api_key’])) return;

// Choose topic: either manual param or simple helper (you can expand this)
$topic = $manual_topic ? sanitize_text_field($manual_topic) : $this->pick_topic();

if (!$topic) return;

$prompt = str_replace(‘{topic}’, $topic, $opts[‘prompt_template’]);

$generated = $this->call_openai($opts[‘api_key’], $prompt, $opts[‘max_tokens’] ?? 800);
if (!$generated) return;

// Parse for SEO suggestion: simple split. If response includes “Suggested SEO title:” we’ll try to extract – otherwise fallback.
$title = $this->extract_between($generated, “Suggested SEO title:”, “\n”) ?: wp_trim_words($topic, 8, ”);
$content = $generated;

$post = [
‘post_title’ => wp_strip_all_tags($title),
‘post_content’ => wp_kses_post($content),
‘post_status’ => $opts[‘post_status’] ?? ‘draft’,
‘post_category’=> $opts[‘category’] ? [$opts[‘category’]] : [],
‘post_author’ => get_current_user_id(),
];

wp_insert_post($post);
}

private function pick_topic(){
// Simple placeholder: pick recent tag/topic or random. You should replace with your topic source or keywords list.
$topics = [
‘Tren teknologi 2025 untuk UMKM’,
‘Cara mengoptimalkan SEO on-page 2025’,
‘Tips memilih laptop untuk content creator’,
‘Panduan dasar monetisasi blog’,
];
return $topics[array_rand($topics)];
}

private function extract_between($text, $start, $end){
$s = strpos($text, $start);
if ($s === false) return null;
$s += strlen($start);
$e = strpos($text, $end, $s);
if ($e === false) $e = strlen($text);
return trim(substr($text, $s, $e-$s));
}

/* Call OpenAI using wp_remote_post */
private function call_openai($api_key, $prompt, $max_tokens=800){
$endpoint = ‘https://api.openai.com/v1/chat/completions’;
$body = [
‘model’ => ‘gpt-4o-mini’, // change if you prefer another
‘messages’ => [
[‘role’ => ‘system’, ‘content’ => ‘You are an expert content writer. Produce long-form articles in Indonesian with a professional tone.’],
[‘role’ => ‘user’, ‘content’ => $prompt]
],
‘max_tokens’ => intval($max_tokens),
‘temperature’ => 0.2,
‘top_p’ => 1,
];

$response = wp_remote_post($endpoint, [
‘headers’ => [
‘Authorization’ => ‘Bearer ‘ . $api_key,
‘Content-Type’ => ‘application/json’,
],
‘body’ => wp_json_encode($body),
‘timeout’ => 60,
]);

if (is_wp_error($response)) {
error_log(‘APAG OpenAI error: ‘ . $response->get_error_message());
return false;
}

$code = wp_remote_retrieve_response_code($response);
$body = wp_remote_retrieve_body($response);
$json = json_decode($body, true);
if ($code !== 200 || empty($json[‘choices’][0][‘message’][‘content’])) {
error_log(‘APAG OpenAI bad response: ‘ . $body);
return false;
}
return $json[‘choices’][0][‘message’][‘content’];
}
}

new AutoProArticleGenerator();

Leave a Reply

Your email address will not be published. Required fields are marked *