-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeta-gpt.php
355 lines (307 loc) · 12.3 KB
/
meta-gpt.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
/**
* Plugin Name: Meta GPT Generator
* Plugin URI: https://github.com/beuzathor/meta-gpt
* Description: Génère et met à jour les meta titles et descriptions avec ChatGPT
* Version: 1.1
* Requires at least: 1.1
* Tested up to: 6
* Requires PHP: 7.2
* Author: beuzathor
* Author URI: https://github.com/beuzathor/meta-gpt
* License: GPL v2 or later
* License URI: https://github.com/beuzathor/meta-gpt
* Text Domain: domain
* Domain Path: /languages.
*/
// Sécurité
defined('ABSPATH') or die('Access denied');
// Ajouter le menu admin
add_action('admin_menu', 'meta_gpt_menu');
function meta_gpt_menu() {
add_menu_page(
'Meta GPT Generator',
'Meta GPT',
'manage_options',
'meta-gpt',
'meta_gpt_page',
'dashicons-admin-generic'
);
}
// Ajouter les options du plugin
add_action('admin_init', 'meta_gpt_settings');
function meta_gpt_settings() {
register_setting('meta-gpt-settings', 'meta_gpt_api_key');
register_setting('meta-gpt-settings', 'meta_gpt_auto_generate');
}
// Fonction simplifiée pour sauvegarder les meta
function update_seo_meta($post_id, $meta_title, $meta_desc) {
update_post_meta($post_id, '_meta_gpt_title', $meta_title);
update_post_meta($post_id, '_meta_gpt_description', $meta_desc);
}
// Ajout des meta tags dans le head
add_action('wp_head', 'add_meta_tags_to_head', 1);
function add_meta_tags_to_head() {
if (is_singular()) { // Pour les articles et pages
global $post;
$meta_title = get_post_meta($post->ID, '_meta_gpt_title', true);
$meta_desc = get_post_meta($post->ID, '_meta_gpt_description', true);
if (!empty($meta_title)) {
echo '<title>' . esc_html($meta_title) . '</title>' . "\n";
echo '<meta property="og:title" content="' . esc_attr($meta_title) . '" />' . "\n";
echo '<meta name="twitter:title" content="' . esc_attr($meta_title) . '" />' . "\n";
}
if (!empty($meta_desc)) {
echo '<meta name="description" content="' . esc_attr($meta_desc) . '" />' . "\n";
echo '<meta property="og:description" content="' . esc_attr($meta_desc) . '" />' . "\n";
echo '<meta name="twitter:description" content="' . esc_attr($meta_desc) . '" />' . "\n";
}
}
}
// Retirer le title par défaut de WordPress
remove_action('wp_head', '_wp_render_title_tag', 1);
// Page d'administration
function meta_gpt_page() {
?>
<div class="wrap">
<h1>Meta GPT Generator</h1>
<!-- Formulaire de configuration -->
<form method="post" action="options.php">
<?php settings_fields('meta-gpt-settings'); ?>
<table class="form-table">
<tr>
<th>Clé API ChatGPT</th>
<td>
<input type="text" name="meta_gpt_api_key"
value="<?php echo esc_attr(get_option('meta_gpt_api_key')); ?>"
class="regular-text">
</td>
</tr>
<tr>
<th>Génération automatique</th>
<td>
<label>
<input type="checkbox"
name="meta_gpt_auto_generate"
value="1"
<?php checked(get_option('meta_gpt_auto_generate', '1'), '1'); ?>>
Générer automatiquement les meta tags à la publication
</label>
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
<!-- Liste des articles -->
<h2>Articles</h2>
<table class="wp-list-table widefat fixed striped">
<thead>
<tr>
<th>Titre</th>
<th>Meta Title</th>
<th>Meta Description</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
$posts = get_posts(['posts_per_page' => -1]);
foreach ($posts as $post) {
$meta_title = get_post_meta($post->ID, '_meta_gpt_title', true);
$meta_desc = get_post_meta($post->ID, '_meta_gpt_description', true);
?>
<tr>
<td><?php echo esc_html($post->post_title); ?></td>
<td><?php echo esc_html($meta_title); ?></td>
<td><?php echo esc_html($meta_desc); ?></td>
<td>
<button class="button generate-meta"
data-post-id="<?php echo $post->ID; ?>">
Générer Meta
</button>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<script>
jQuery(document).ready(function($) {
$('.generate-meta').click(function(e) {
e.preventDefault();
const button = $(this);
const row = button.closest('tr');
const postId = button.data('post-id');
button.prop('disabled', true);
button.text('Génération...');
$.ajax({
url: ajaxurl,
type: 'POST',
data: {
action: 'generate_meta',
post_id: postId,
nonce: '<?php echo wp_create_nonce("meta_gpt_nonce"); ?>'
},
success: function(response) {
if (response.success) {
row.find('td:nth-child(2)').text(response.data.title);
row.find('td:nth-child(3)').text(response.data.description);
} else {
alert('Erreur: ' + response.data);
}
},
error: function() {
alert('Erreur de communication avec le serveur');
},
complete: function() {
button.prop('disabled', false);
button.text('Générer Meta');
}
});
});
});
</script>
<?php
}
// Traitement AJAX pour la génération manuelle
add_action('wp_ajax_generate_meta', 'generate_meta_ajax');
function generate_meta_ajax() {
check_ajax_referer('meta_gpt_nonce', 'nonce');
$post_id = intval($_POST['post_id']);
$post = get_post($post_id);
if (!$post) {
wp_send_json_error('Article non trouvé');
}
$api_key = get_option('meta_gpt_api_key');
if (!$api_key) {
wp_send_json_error('Clé API non configurée');
}
$content = wp_strip_all_tags($post->post_title . "\n\n" . $post->post_content);
$content = substr($content, 0, 1500);
$response = wp_remote_post('https://api.openai.com/v1/chat/completions', [
'headers' => [
'Authorization' => 'Bearer ' . $api_key,
'Content-Type' => 'application/json',
],
'body' => json_encode([
'model' => 'gpt-3.5-turbo',
'messages' => [
[
'role' => 'system',
'content' => "Tu es un expert SEO. Génère un meta title et une meta description optimisés pour le référencement.
Pour le title : entre 50 et 60 caractères, inclure les mots-clés importants.
Pour la description : entre 145 et 160 caractères, donner envie de cliquer.
Réponds UNIQUEMENT avec ce format exact :
TITLE: [meta title]
DESC: [meta description]"
],
[
'role' => 'user',
'content' => $content
]
],
'temperature' => 0.7
])
]);
if (is_wp_error($response)) {
wp_send_json_error('Erreur API: ' . $response->get_error_message());
}
$body = json_decode(wp_remote_retrieve_body($response), true);
if (empty($body['choices'][0]['message']['content'])) {
wp_send_json_error('Réponse API invalide');
}
$content = $body['choices'][0]['message']['content'];
if (preg_match('/TITLE:\s*(.*?)\s*(?:\n|$)/i', $content, $title_matches) &&
preg_match('/DESC(?:RIPTION)?:\s*(.*?)(?:\n|$)/i', $content, $desc_matches)) {
$meta_title = trim($title_matches[1]);
$meta_desc = trim($desc_matches[1]);
update_seo_meta($post_id, $meta_title, $meta_desc);
wp_send_json_success([
'title' => $meta_title,
'description' => $meta_desc
]);
} else {
wp_send_json_error('Format de réponse non reconnu');
}
}
// Hook pour la génération automatique à la publication
add_action('transition_post_status', 'auto_generate_meta_on_publish', 10, 3);
function auto_generate_meta_on_publish($new_status, $old_status, $post) {
if (get_option('meta_gpt_auto_generate', '1') !== '1') {
return;
}
if ($new_status === 'publish' && $old_status !== 'publish' &&
in_array($post->post_type, ['post', 'page'])) {
$api_key = get_option('meta_gpt_api_key');
if (empty($api_key)) {
return;
}
$content = wp_strip_all_tags($post->post_title . "\n\n" . $post->post_content);
$content = substr($content, 0, 1500);
$response = wp_remote_post('https://api.openai.com/v1/chat/completions', [
'headers' => [
'Authorization' => 'Bearer ' . $api_key,
'Content-Type' => 'application/json',
],
'body' => json_encode([
'model' => 'gpt-3.5-turbo',
'messages' => [
[
'role' => 'system',
'content' => "Tu es un expert SEO. Je vais te donner le contenu d'un article.
Tu dois générer :
1. Un meta title qui inclut les mots-clés principaux
2. Une meta description qui donne envie de cliquer
IMPORTANT : Le but est de faire un titre et une meta description qui fasse cliquer au maximum.
Réponds UNIQUEMENT avec ce format exact :
TITLE: [meta title]
DESC: [meta description]"
],
[
'role' => 'user',
'content' => $content
]
],
'temperature' => 0.7
])
]);
if (!is_wp_error($response)) {
$body = json_decode(wp_remote_retrieve_body($response), true);
if (!empty($body['choices'][0]['message']['content'])) {
$content = $body['choices'][0]['message']['content'];
if (preg_match('/TITLE:\s*(.*?)\s*(?:\n|$)/i', $content, $title_matches) &&
preg_match('/DESC(?:RIPTION)?:\s*(.*?)(?:\n|$)/i', $content, $desc_matches)) {
update_seo_meta($post->ID, trim($title_matches[1]), trim($desc_matches[1]));
}
}
}
}
}
// Enregistrer les champs personnalisés dans l'API REST
add_action('rest_api_init', 'register_meta_gpt_rest_fields');
function register_meta_gpt_rest_fields() {
register_rest_field('post', 'meta_gpt', array(
'get_callback' => function($post) {
return array(
'title' => get_post_meta($post['id'], '_meta_gpt_title', true),
'description' => get_post_meta($post['id'], '_meta_gpt_description', true)
);
},
'schema' => array(
'description' => 'Meta GPT data',
'type' => 'object',
'properties' => array(
'title' => array(
'type' => 'string',
'description' => 'Meta title generated by GPT'
),
'description' => array(
'type' => 'string',
'description' => 'Meta description generated by GPT'
)
)
)
));
}