-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintelli-content.php
214 lines (185 loc) · 8.49 KB
/
intelli-content.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
/**
* @wordpress-plugin
* Plugin Name: IntelliContent
* Plugin URI: https://wordpress.org/plugins/intelli-content
* Description: Generate high-quality content using OpenAI's API.
* Version: 1.0.0
* Author: Azhar Khan
* Author URI: https://www.azhark.com/
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: intelli-content
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
// Top-Level and Sub-Menu
require_once (dirname(__FILE__) . '/includes/menu.php');
// Settings Page
require_once (dirname(__FILE__) . '/includes/settings.php');
// Helper Functions
require_once (dirname(__FILE__) . '/includes/helpers.php');
// Content generation page callback function
function intelli_content_generation_page() {
?>
<div class="wrap">
<h1>Content Generation</h1>
<div class="content-generation-form">
<form method="post" action="">
<!-- Add nonce field -->
<?php wp_nonce_field( 'generate_content_nonce', 'generate_content_nonce_field' ); ?>
<p><label for="keyword">Enter a keyword or topic:</label></p>
<p><input type="text" id="keyword" name="keyword" value="" class="regular-text"></p>
<p><button type="submit" class="button-primary" name="generate_content">Generate Content</button></p>
</form>
</div>
<?php
// Check if form is submitted
if (isset($_POST['generate_content'])) {
// Verify nonce of the form
if ( ! isset( $_POST['generate_content_nonce_field'] ) || ! wp_verify_nonce( $_POST['generate_content_nonce_field'], 'generate_content_nonce' ) ) {
wp_die( 'Security check failed1!' );
}
$keyword = sanitize_text_field($_POST['keyword']);
$content = intelli_content_generate($keyword);
?>
<div id="generatedContent" class="generated-content">
<?php if (!empty( $_POST['generated_content']) ) { ?> <h2>Generated Content</h2> <?php } ?>
<div class="content-text"><?php echo wp_kses_post( intelli_content_nl2p($content) ); ?></div>
<form method="post" action="">
<!-- Add nonce field -->
<?php wp_nonce_field( 'insert_post_nonce', 'insert_post_nonce_field' ); ?>
<input type="hidden" name="generated_content" value="<?php echo esc_attr($content); ?>">
<input type="hidden" name="generated_title" value="<?php echo esc_attr($keyword); ?>">
<p>
<button type="submit" class="button-primary" name="insert_post">Insert Post</button>
</p>
</form>
</div>
<?php
}
?>
</div>
<?php
}
function intelli_content_handle_form_submission() {
// Check if form is submitted
if (isset($_POST['insert_post'])) {
// Verify nonce of the form
if ( ! isset( $_POST['insert_post_nonce_field'] ) || ! wp_verify_nonce( $_POST['insert_post_nonce_field'], 'insert_post_nonce' ) ) {
wp_die( 'Security check failed2!' );
}
$content = wp_kses_post( $_POST['generated_content'] );
$extracted_data = intelli_content_extract_title_from_content($content);
$title = $extracted_data['title'];
$content = $extracted_data['content'];
// Define an array to map block types to their respective tags
$block_map = array(
'paragraph' => 'wp:paragraph',
'heading' => 'wp:heading',
'list' => 'wp:list',
'list_item' => 'wp:list-item',
'blockquote'=> 'wp:quote',
);
// Split the content into paragraphs and process each paragraph
$paragraphs = explode("\n", $content);
$processed_content = '';
foreach ($paragraphs as $paragraph) {
// Determine the block type based on the content
$block_type = 'paragraph'; // Default to paragraph block
if (strpos($paragraph, '#') === 0) {
$block_type = 'heading'; // Heading block if starts with #
} elseif (strpos($paragraph, '*') === 0) {
$block_type = 'list_item'; // List item block if starts with *
} elseif (strpos($paragraph, '>') === 0) {
$block_type = 'blockquote'; // Blockquote block if starts with >
}
// Get the corresponding Gutenberg block tag
$block_tag = $block_map[$block_type];
// Append the processed paragraph to the content
if (!empty (trim($paragraph))) {
$processed_content .= "<!-- {$block_tag} -->" . wpautop(trim($paragraph, '#*>' . PHP_EOL)) . "<!-- /{$block_tag} -->";
}
}
// Create post object
$post_data = array(
'post_title' => sanitize_text_field($title),
'post_content' => $processed_content,
'post_status' => 'draft',
'post_type' => 'post'
);
// Insert the post into the database
$post_id = wp_insert_post($post_data);
// Check if post was successfully inserted
if ($post_id) {
// Redirect to all post screen using JavaScript
echo "Redirecting to all post screen...";
?>
<script>
window.location.href = '<?php echo esc_url( admin_url( 'post.php' ) ); ?>';
</script>
<?php
} else {
// Display error notice
add_action('admin_notices', function() {
echo '<div class="notice notice-error"><p>Error: Failed to insert post.</p></div>';
});
}
}
}
add_action('admin_notices', 'intelli_content_handle_form_submission');
// Extract title from content and remove it from the content using regex
function intelli_content_extract_title_from_content($content) {
// Match the first <h1> tag and extract its content
if (preg_match('/<h1>(.*?)<\/h1>/', $content, $matches)) {
$title = $matches[1]; // Extracted title
$new_content = preg_replace('/<h1>.*?<\/h1>/', '', $content, 1); // Remove the first occurrence of <h1> tag
} else {
$title = ''; // No title found
$new_content = $content; // Content remains unchanged
}
// Return title and new content
return array('title' => $title, 'content' => $new_content);
}
function intelli_content_generate($keyword) {
$api_key = get_option('intelli_content_api_key');
$model = get_option('intelli_content_model', 'gpt-3.5-turbo-0613');
$temperature = get_option('intelli_content_temperature', 0.7);
$max_tokens = get_option('intelli_content_max_tokens', 1000);
$endpoint = 'https://api.openai.com/v1/chat/completions'; // Use chat completions endpoint for chat models
$headers = array(
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $api_key,
);
$prompt = "Generate content about and also add a h1 heading as title on top of the article : $keyword";
$data = array(
'model' => $model,
'max_tokens' => $max_tokens,
'temperature' => $temperature,
'messages' => array(
array(
'role' => 'user',
'content' => $prompt
)
)
);
$args = array(
'body' => wp_json_encode($data),
'headers' => $headers,
'timeout' => 30,
);
$response = wp_remote_post($endpoint, $args);
if (is_wp_error($response)) {
return "Error: Failed to connect to OpenAI API. Please try again later.";
} else {
$response_code = wp_remote_retrieve_response_code($response);
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
if ($response_code === 200 && isset($data['choices'][0]['message']['content'])) {
return $data['choices'][0]['message']['content'];
} elseif (isset($data['error']['message'])) {
return "<p class='error-message'>". "Error: " . $data['error']['message'] . "</p>";
} else {
return "<p class='error-message'>Error: Failed to generate content. Please try again later.</p>";
}
}
}