-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathplugin_core.php
261 lines (242 loc) · 12.3 KB
/
plugin_core.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
/*
* Plugin Name: A.I Comments Reply for GPT
* Plugin URI: https://aicodecraft.io
* Description: Effortlessly manage and respond to comments on your WordPress site with the power of AI using the ChatGPT Comments Reply Plugin
* Version: 1.2
* Requires at least: 5.2
* Requires PHP: 7.4
* Author: S. Volkan Kücükbudak
* Author URI: https://aicodecraft.io
* License: CC BY 4.0
* License URI: https://creativecommons.org/licenses/by/4.0/
* Update URI: https://github.com/VolkanSah/ChatGPT-Comments-Reply-WordPress-Plugin/latest.zip
* Text Domain: aicc-aicr
* Domain Path: /languages
*/
if ( ! defined( 'WPINC' ) ) {
die;
}
function aicc_openai_settings_init() {
register_setting('aicc_openai_settings', 'openai_api_key');
register_setting('aicc_openai_settings', 'model');
register_setting('aicc_openai_settings', 'temperature');
register_setting('aicc_openai_settings', 'max_tokens');
register_setting('aicc_openai_settings', 'top_p');
register_setting('aicc_openai_settings', 'frequency_penalty');
register_setting('aicc_openai_settings', 'presence_penalty');
}
add_action('admin_init', 'aicc_openai_settings_init');
function aicc_openai_settings() {
add_menu_page(
'AICC OpenAI Comment Reply Settings',
'AICC OpenAI Settings',
'manage_options',
'aicc-openai-comment-reply-settings',
'aicc_openai_settings_page',
'dashicons-format-chat'
);
}
add_action('admin_menu', 'aicc_openai_settings');
function aicc_openai_settings_page() {
$openai_api_key = get_option('openai_api_key');
$model = get_option('model');
$temperature = get_option('temperature');
$max_tokens = get_option('max_tokens');
$top_p = get_option('top_p');
$frequency_penalty = get_option('frequency_penalty');
$presence_penalty = get_option('presence_penalty');
?>
<div class="wrap">
<h1>AICC OpenAI Comment Reply Settings</h1>
<div class="aicc-openai-info">
<h2>About AICC OpenAI Comment Reply Plugin</h2>
<p>This plugin uses OpenAI's ChatGPT to automatically reply to comments on your WordPress website. It leverages AI technology to generate relevant and helpful responses to your visitors' comments.</p>
<h3>How to use the plugin:</h3>
<ol>
<li>Enter your OpenAI API key in the "OpenAI API Key" field and save the settings.</li>
<li>Go to the comments management in your WordPress admin area.</li>
<li>In the comment list, you'll see the option "Reply with AICC OpenAI" under each comment. Click on this option to generate an AI-generated response to the comment.</li>
</ol>
<h3>Recommended settings:</h3>
<p>These are the recommended settings for the plugin:</p>
<ul>
<li>Model: gpt-3.5-turbo</li>
<li>Temperature: 0.6</li>
<li>Max Tokens: 500</li>
<li>Top P: 1</li>
<li>Frequency Penalty: 0.0</li>
<li>Presence Penalty: 0.0</li>
</ul>
<p>These settings have been tested to provide good results with the plugin. However, feel free to adjust them according to your needs and preferences.</p>
</div>
<form method="post" action="options.php">
<?php settings_fields('aicc_openai_settings'); ?>
<?php do_settings_sections('aicc_openai_settings'); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">OpenAI API Key</th>
<td>
<input type="password" required name="openai_api_key" class="regular-text" value="<?php echo esc_attr($openai_api_key); ?>" />
</td>
</tr>
<tr valign="top">
<th scope="row">Model</th>
<td>
<select name="model">
<option <?php selected($model, 'gpt-3.5-turbo'); ?> value="gpt-3.5-turbo">gpt-3.5-turbo</option>
<option <?php selected($model, 'gpt-3.5-turbo-16k'); ?> value="gpt-3.5-turbo-16k">gpt-3.5-turbo-16k</option>
<option <?php selected($model, 'gpt-4'); ?> value="gpt-4">gpt-4</option>
<option <?php selected($model, 'gpt-4-turbo'); ?> value="gpt-4-turbo">gpt-4-turbo</option>
<option <?php selected($model, 'gpt-4-0613'); ?> value="gpt-4-0613">gpt-4-0613</option>
<option <?php selected($model, 'gpt-4-0125-preview'); ?> value="gpt-4-0125-preview">gpt-4-0125-preview</option>
<option <?php selected($model, 'gpt-4-turbo-preview'); ?> value="gpt-4-turbo-preview">gpt-4-turbo-preview</option>
<option <?php selected($model, 'gpt-4o-mini'); ?> value="gpt-4o-mini">gpt-4o-mini</option>
<option <?php selected($model, 'gpt-4o-mini-2024-07-18'); ?> value="gpt-4o-mini-2024-07-18">gpt-4o-mini-2024-07-18</option>
<option <?php selected($model, 'gpt-4-1106-preview'); ?> value="gpt-4-1106-preview">gpt-4-1106-preview</option>
<option <?php selected($model, 'gpt-4o'); ?> value="gpt-4o">gpt-4o</option>
<option <?php selected($model, 'gpt-4o-2024-05-13'); ?> value="gpt-4o-2024-05-13">gpt-4o-2024-05-13</option>
<option <?php selected($model, 'gpt-4o-2024-08-06'); ?> value="gpt-4o-2024-08-06">gpt-4o-2024-08-06</option>
<option <?php selected($model, 'chatgpt-4o-latest'); ?> value="chatgpt-4o-latest">chatgpt-4o-latest</option>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row">Temperature</th>
<td><input type="number" step="0.01" min="0" max="1" name="temperature" value="<?php echo esc_attr($temperature); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">Max Tokens</th>
<td><input type="number" min="1" name="max_tokens" value="<?php echo esc_attr($max_tokens); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">Top P</th>
<td><input type="number" step="0.01" min="0" max="1" name="top_p" value="<?php echo esc_attr($top_p); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">Frequency Penalty</th>
<td><input type="number" step="0.01" min="0" max="1" name="frequency_penalty" value="<?php echo esc_attr($frequency_penalty); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">Presence Penalty</th>
<td><input type="number" step="0.01" min="0" max="1" name="presence_penalty" value="<?php echo esc_attr($presence_penalty); ?>" /></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
// Adds button to comment row actions
function aicc_add_button_to_comment_row_actions($actions, $comment) {
$actions['aicc_reply'] = '<a href="#" class="openai-reply">Reply with A.I</a>';
return $actions;
}
add_filter('comment_row_actions', 'aicc_add_button_to_comment_row_actions', 10, 2);
function aicc_add_js_to_comment_page() {
?>
<script>
jQuery(document).ready(function ($) {
$('.openai-reply').click(function () {
var commentId = $(this).closest('tr').attr('id').replace('comment-', '');
$('#comment-' + commentId + ' .row-actions .reply button').click();
aicc_openai_reply(commentId);
return false;
});
function aicc_openai_reply(comment_id) {
var rowData = $('#inline-' + comment_id);
var comment_text = $('textarea.comment', rowData).val();
var editRow = $('#replyrow');
$('#replysubmit .spinner').addClass('is-active');
$.ajax({
type: "POST",
url: ajaxurl,
data: {
action: 'aicc_generate_reply',
comment_text: comment_text,
comment_id: comment_id,
},
success: function (response) {
if(response.success) {
$('#replycontent', editRow).val(response.data.reply);
$('#replysubmit .spinner').removeClass('is-active');
} else {
console.error('Error: ' + response.data.message);
alert('Error: ' + response.data.message);
}
},
error: function(jqXHR, textStatus, errorThrown) {
console.error("AJAX error: " + textStatus + ' : ' + errorThrown);
}
});
}
});
</script>
<?php
}
add_action('admin_footer-edit-comments.php', 'aicc_add_js_to_comment_page');
function aicc_generate_reply() {
$comment_text = sanitize_text_field($_POST['comment_text']);
$openai_api_key = get_option('openai_api_key');
$model = get_option('model');
$temperature = floatval(get_option('temperature'));
$max_tokens = intval(get_option('max_tokens'));
$top_p = floatval(get_option('top_p'));
$frequency_penalty = floatval(get_option('frequency_penalty'));
$presence_penalty = floatval(get_option('presence_penalty'));
// Step 1: Moderation des Kommentars vor der Weiterleitung an die API
$moderation_response = wp_remote_post('https://api.openai.com/v1/moderations', [
'headers' => [
'Authorization' => 'Bearer ' . $openai_api_key,
'Content-Type' => 'application/json',
],
'body' => json_encode([
'input' => $comment_text,
]),
'timeout' => 30,
]);
if (is_wp_error($moderation_response)) {
error_log('Error in Moderation API request: ' . $moderation_response->get_error_message());
wp_send_json_error(['message' => 'Failed to moderate the comment.']);
}
$moderation_body = wp_remote_retrieve_body($moderation_response);
$moderation_result = json_decode($moderation_body, true);
// Check if the comment is flagged
if ($moderation_result && isset($moderation_result['results'][0]['flagged']) && $moderation_result['results'][0]['flagged']) {
error_log('Comment was flagged by moderation.');
wp_send_json_error(['message' => 'The comment was flagged as inappropriate and cannot be processed.']);
}
// Step 2: Wenn der Kommentar genehmigt ist, generiere eine Antwort
$response = wp_remote_post('https://api.openai.com/v1/chat/completions', [
'headers' => [
'Authorization' => 'Bearer ' . $openai_api_key,
'Content-Type' => 'application/json',
],
'body' => json_encode([
'model' => $model,
'messages' => [
['role' => 'system', 'content' => 'You are a helpful assistant.'],
['role' => 'user', 'content' => $comment_text]
],
'max_tokens' => $max_tokens,
'temperature' => $temperature,
'top_p' => $top_p,
'frequency_penalty' => $frequency_penalty,
'presence_penalty' => $presence_penalty,
]),
'timeout' => 30,
]);
if (is_wp_error($response)) {
error_log('Error in OpenAI request: ' . $response->get_error_message());
wp_send_json_error(['message' => $response->get_error_message()]);
}
$body = wp_remote_retrieve_body($response);
$result = json_decode($body, true);
if (isset($result['choices'][0]['message']['content'])) {
wp_send_json_success(['reply' => $result['choices'][0]['message']['content']]);
} else {
error_log('Failed to generate a reply.');
wp_send_json_error(['message' => 'Failed to generate a reply.']);
}
}
add_action('wp_ajax_aicc_generate_reply', 'aicc_generate_reply');