';
// echo '';
echo ''; // This is where the FullCalendar will render
echo '';
}
public function register_settings()
{
register_setting('ai_content_pipelines_oneup_author_personas_settings_group', 'ai_content_pipelines_oneup_openai_api_key');
register_setting('ai_content_pipelines_oneup_author_personas_settings_group', 'ai_content_pipelines_oneup_twitter_api_key');
register_setting('ai_content_pipelines_oneup_author_personas_settings_group', 'ai_content_pipelines_oneup_twitter_api_secret');
register_setting('ai_content_pipelines_oneup_author_personas_settings_group', 'ai_content_pipelines_oneup_twitter_access_token');
register_setting('ai_content_pipelines_oneup_author_personas_settings_group', 'ai_content_pipelines_oneup_twitter_access_token_secret');
register_setting('ai_content_pipelines_oneup_author_personas_settings_group', 'ai_content_pipelines_oneup_author_persona_language_setting');
}
public function settings_page()
{
?>
';
}
}
}
// Avoid appending if the content is already present
if (!empty($generated_content)) {
if (strpos($content, $generated_content) === false) {
$content = $generated_content . $content;
} elseif (strpos($content, $generated_content) !== false) {
// Remove duplicate generated content if already appended
$content = str_replace($generated_content, '', $content);
$content = $generated_content . $content;
}
}
}
return $content;
}
// public function get_related_posts($keywords, $current_post_id)
// {
// $args = [
// 's' => implode(' ', $keywords),
// 'post__not_in' => [$current_post_id],
// 'posts_per_page' => 5,
// ];
// $query = new WP_Query($args);
// return $query->posts;
// }
public function get_related_posts($keywords, $current_post_id)
{
// Using post__not_in can affect performance on large datasets. Consider alternative approaches.
$args = [
's' => implode(' ', $keywords),
'posts_per_page' => 6, // Retrieve one extra post to handle manual exclusion
];
$query = new WP_Query($args);
$posts = $query->posts;
// Manually exclude the current post, if it's in the results.
$filtered_posts = array_filter($posts, function ($post) use ($current_post_id) {
return $post->ID !== $current_post_id;
});
// Return only the number of posts you need
return array_slice($filtered_posts, 0, 5);
}
public function generate_content()
{
ini_set('memory_limit', '256M'); // Increase memory limit
ini_set('max_execution_time', '300');
check_ajax_referer('generate_content_nonce', '_wpnonce');
update_option('ai_content_pipelines_oneup_is_last_error', false);
$this->updateProgressToPoll("Content generation started....");
$post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0;
$persona_name = isset($_POST['persona']) ? sanitize_text_field(wp_unslash($_POST['persona'])) : '';
$content_type = isset($_POST['content_type']) ? sanitize_text_field(wp_unslash($_POST['content_type'])) : '';
$api_type = isset($_POST['api_type']) ? sanitize_text_field(wp_unslash($_POST['api_type'])) : '';
$custom_prompt = isset($_POST['custom_prompt']) ? sanitize_text_field(wp_unslash($_POST['custom_prompt'])) : '';
$author_id = isset($_POST['post_author']) ? intval(wp_unslash($_POST['post_author'])) : 0;
$post_status = isset($_POST['post_status']) ? sanitize_text_field(wp_unslash($_POST['post_status'])) : 'pending';
$schedule_time = isset($_POST['schedule_time']) ? wp_unslash($_POST['schedule_time']) : null;
$post_type = $post_id ? get_post_type($post_id) : (isset($_POST['post_type']) ? sanitize_text_field($_POST['post_type']) : 'post');
// $post_type = isset($_POST['post_type']) ? sanitize_text_field(wp_unslash($_POST['post_type'])) : 'post'; // Get post type
$this->updateProgressToPoll("Generating content for " . $post_type);
if ($schedule_time && strtotime($schedule_time)) {
// Convert the valid date to Unix timestamp
$schedule_time = strtotime($schedule_time);
} else {
// Invalid or missing date, use current time
$schedule_time = time();
$this->updateProgressToPoll("Invalid or no scheduled time provided, using current time.");
}
if (empty($post_id) || empty($persona_name) || empty($content_type) || empty($api_type) || empty($custom_prompt) || empty($author_id)) {
wp_send_json_error(['message' => 'Invalid request parameters']);
return;
}
$persona = $this->get_persona_info($persona_name);
if (!$persona) {
wp_send_json_error(['message' => 'Invalid persona']);
return;
}
$persona_details = sprintf(
"Persona: %s\nWriting Style: %s\nTone: %s\nEmotions: %s\nExperience: %s\nExpertise: %s\nAuthoritativeness: %s\nTrustworthiness: %s",
$persona['persona'],
$persona['writing_style'],
$persona['tone'],
implode(', ', $persona['emotions']),
$persona['E-E-A-T']['Experience'],
$persona['E-E-A-T']['Expertise'],
$persona['E-E-A-T']['Authoritativeness'],
$persona['E-E-A-T']['Trustworthiness']
);
$templates = [
"Review" => "Review Template\n\nIntroduction:\n- Provide a brief overview of the product/service being reviewed.\n- Mention the main purpose or use case.\n\nPros and Cons:\n- List the key advantages and benefits of the product/service.\n- List any drawbacks or disadvantages.\n\nDetailed Review:\n- Describe the product/service in detail, including features, performance, and usability.\n- Compare it with similar products/services in the market.\n- Share any personal experiences or insights.\n\nConclusion:\n- Summarize your overall opinion.\n- Recommend whether the product/service is worth purchasing and for whom.",
"Editorial" => "Editorial Template\n\nIntroduction:\n- Introduce the topic or issue you are addressing.\n- Provide some background information and context.\n\nMain Argument:\n- Present your main argument or viewpoint.\n- Use supporting evidence, examples, and anecdotes.\n\nCounterarguments:\n- Address potential counterarguments or opposing views.\n- Refute them with logical reasoning and evidence.\n\nConclusion:\n- Summarize your main points.\n- Reiterate your stance and suggest any calls to action or next steps.",
"Interview" => "Interview Template\n\nIntroduction:\n- Introduce the interviewee and their background.\n- Mention the purpose of the interview.\n\nQuestions and Answers:\n- Ask a series of questions and provide the interviewee's answers.\n- Include follow-up questions based on the interviewee's responses.\n\nConclusion:\n- Summarize key points discussed in the interview.\n- Provide any final thoughts or reflections.",
"How To" => "How To Template\n\nIntroduction:\n- Introduce the topic and explain the importance of the task.\n\nStep-by-Step Instructions:\n- List each step in a clear and logical order.\n- Provide detailed explanations and tips for each step.\n\nConclusion:\n- Summarize the process.\n- Offer any additional tips or considerations.",
"Topic Introduction" => "Topic Introduction Template\n\nIntroduction:\n- Introduce the topic and its relevance.\n- Provide some background information.\n\nKey Points:\n- Outline the main points related to the topic.\n- Include any important facts or statistics.\n\nConclusion:\n- Summarize the key points.\n- Suggest any further reading or actions.",
"Opinion" => "Opinion Template\n\nIntroduction:\n- Introduce the topic and your viewpoint.\n\nMain Points:\n- Present your main points and arguments.\n- Support your points with evidence and examples.\n\nConclusion:\n- Summarize your viewpoint.\n- Suggest any calls to action or next steps.",
"Research" => "Research Template\n\nIntroduction:\n- Introduce the research topic and its importance.\n\nMethods:\n- Describe the research methods used.\n\nFindings:\n- Present the main findings.\n\nConclusion:\n- Summarize the findings and their implications.",
"Case Study" => "Case Study Template\n\nIntroduction:\n- Introduce the subject of the case study.\n- Provide some background information.\n\nProblem:\n- Describe the problem or challenge faced.\n\nSolution:\n- Explain the solution implemented.\n\nResults:\n- Present the results of the solution.\n\nConclusion:\n- Summarize the key points and lessons learned.",
"Short Report" => "Short Report Template\n\nIntroduction:\n- Introduce the topic of the report.\n\nMain Points:\n- Present the main points of the report.\n\nConclusion:\n- Summarize the main points.",
"Think Piece" => "Think Piece Template\n\nIntroduction:\n- Introduce the topic and its relevance.\n\nMain Argument:\n- Present your main argument or viewpoint.\n\nSupporting Points:\n- Provide supporting points and evidence.\n\nConclusion:\n- Summarize your argument and suggest any calls to action.",
"Hard News" => "Hard News Template\n\nIntroduction:\n- Summarize the main facts of the news story.\n\nDetails:\n- Provide detailed information about the news event.\n\nConclusion:\n- Summarize the key points and any potential implications.",
"First Person" => "First Person Template\n\nIntroduction:\n- Introduce the topic and your personal connection to it.\n\nMain Story:\n- Tell your personal story or experience.\n\nConclusion:\n- Summarize the main points of your story.",
"Service Piece" => "Service Piece Template\n\nIntroduction:\n- Introduce the service and its importance.\n\nDetails:\n- Describe the service and how it works.\n\nBenefits:\n- List the key benefits of the service.\n\nConclusion:\n- Summarize the main points and recommend the service.",
"Informational" => "Informational Template\n\nIntroduction:\n- Introduce the topic and its importance.\n\nMain Points:\n- Present the main points and information.\n\nConclusion:\n- Summarize the main points."
];
if (!isset($templates[$content_type])) {
wp_send_json_error('Invalid content type');
}
$this->updateProgressToPoll("Generating content");
$prompt = 'Generate content for the following prompt:' . "\n";
$prompt .= 'Article Idea: ' . esc_html($custom_prompt) . "\n\n";
$prompt .= 'Based on the author persona details:' . "\n";
$prompt .= esc_html($persona_details) . "\n\n";
$prompt .= esc_html($templates[$content_type]) . "\n";
$prompt .= 'Output the content in HTML format:';
$api_key = get_option('ai_content_pipelines_oneup_openai_api_key');
$site_url = get_site_url();
$request_body = wp_json_encode([
'prompt' => $prompt,
'api_key' => $api_key,
'site_url' => $site_url
]);
$response = wp_remote_post('https://ai.1upmedia.com:443/generate-content-from-prompt', [
'headers' => [
'Content-Type' => 'application/json'
],
'body' => $request_body,
'timeout' => 180
]);
if (is_wp_error($response)) {
wp_send_json_error(['message' => 'API request failed: ' . $response->get_error_message()]);
return;
}
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
if (isset($data['type'])) {
switch ($data['type']) {
case 'QuotaExceeded':
update_option('ai_content_pipelines_oneup_content_generation_in_progress', false);
update_option('ai_content_pipelines_oneup_is_last_error', true);
update_option('ai_content_pipelines_oneup_last_scheduled_timestamp', time());
wp_send_json_error(['type' => 'QuotaExceeded', 'message' => "API key invalid or free limit exhausted"]);
case 'Validation Error':
throw new Exception('Validation Error: ' . esc_html($data['error']));
case 'Content Generation Error':
throw new Exception('Content Generation Error: ' . esc_html($data['error']));
case 'Database Update Error':
throw new Exception('Database Update Error: ' . esc_html($data['error']));
default:
throw new Exception('API request failed: ' . esc_html($data['error']));
}
}
if (!isset($data['content'])) {
wp_send_json_error(['message' => 'Invalid API response at content']);
return;
}
$html_content = trim($data['content']);
$html_content = preg_replace('/^```html/', '', $html_content);
// Remove the block
$html_content = preg_replace('/