PluginProbe
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO / 2.1.0
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO v2.1.0
2.7.0 2.6.0 2.5.0 2.4.0 2.3.0 2.2.0 2.1.1 2.1.0 2.0.2 2.0.1 2.0.0 1.32.0 1.31.0 1.30.0 1.29.0 1.28.0 1.27.0 1.26.0 1.25.0 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.10.0 All 48 releases
thinkrank / includes / admin / class-metabox-manager.php

class-metabox-manager.php in ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO 2.1.0, at includes/admin/class-metabox-manager.php

1,306 lines 51.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Meta Box Manager
5 *
6 * Handles ThinkRank meta boxes in post/page edit screens
7 *
8 * @package ThinkRank\Admin
9 * @since 1.0.0
10 */
11
12 declare(strict_types=1);
13
14 namespace ThinkRank\Admin;
15
16 use ThinkRank\AI\Metadata_Generator;
17 use ThinkRank\AI\SEOScoreCalculator;
18 use ThinkRank\Core\Settings;
19 use ThinkRank\Core\Database;
20 use ThinkRank\Core\Plan_Config;
21 use ThinkRank\SEO\Focus_Keywords;
22 use ThinkRank\SEO\Pattern_Resolver;
23
24 // Prevent direct access
25 if (!defined('ABSPATH')) {
26 exit;
27 }
28
29 /**
30 * Meta Box Manager Class
31 *
32 * Single Responsibility: Manage post/page meta boxes
33 *
34 * @since 1.0.0
35 */
36 class Metabox_Manager {
37
38 /**
39 * Settings instance
40 *
41 * @var Settings
42 */
43 private Settings $settings;
44
45 /**
46 * Metadata generator instance
47 *
48 * @var Metadata_Generator
49 */
50 private Metadata_Generator $metadata_generator;
51
52 /**
53 * SEO Score Calculator instance
54 *
55 * @var SEOScoreCalculator
56 */
57 private SEOScoreCalculator $seo_calculator;
58
59 /**
60 * Constructor
61 *
62 * @param Settings|null $settings Settings instance
63 * @param Metadata_Generator|null $metadata_generator Metadata generator instance
64 * @param SEOScoreCalculator|null $seo_calculator SEO Score Calculator instance
65 */
66 public function __construct(?Settings $settings = null, ?Metadata_Generator $metadata_generator = null, ?SEOScoreCalculator $seo_calculator = null) {
67 $this->settings = $settings ?? Settings::instance();
68 $this->metadata_generator = $metadata_generator ?? new Metadata_Generator();
69 $this->seo_calculator = $seo_calculator ?? new SEOScoreCalculator(new Database());
70 }
71
72 /**
73 * Initialize meta box manager
74 *
75 * @return void
76 */
77 public function init(): void {
78 add_action('add_meta_boxes', [$this, 'add_meta_boxes']);
79 add_action('save_post', [$this, 'save_meta_boxes'], 10, 2);
80 add_action('admin_enqueue_scripts', [$this, 'enqueue_metabox_scripts']);
81 add_action('init', [$this, 'register_meta_fields']);
82
83 // AJAX handlers for meta box functionality
84 add_action('wp_ajax_thinkrank_generate_post_metadata', [$this, 'ajax_generate_post_metadata']);
85
86 // Full metabox save used by editors that don't submit the #post form
87 // (e.g. the Elementor editor). Persists every metabox field at once.
88 add_action('wp_ajax_thinkrank_save_metabox', [$this, 'ajax_save_metabox']);
89
90 // Removed debug hooks
91 }
92
93 /**
94 * Register meta fields for REST API access
95 *
96 * @return void
97 */
98 public function register_meta_fields(): void {
99 // Register schema form data meta fields
100 register_post_meta('', '_thinkrank_schema_form_data', [
101 'show_in_rest' => true,
102 'single' => true,
103 'type' => 'string',
104 'sanitize_callback' => [$this, 'sanitize_json_meta_field'],
105 'auth_callback' => function () {
106 return current_user_can('edit_posts') || current_user_can('edit_pages');
107 }
108 ]);
109
110 register_post_meta('', '_thinkrank_selected_schema_type', [
111 'show_in_rest' => true,
112 'single' => true,
113 'type' => 'string',
114 'auth_callback' => function () {
115 return current_user_can('edit_posts') || current_user_can('edit_pages');
116 }
117 ]);
118
119 register_post_meta('', '_thinkrank_additional_schemas', [
120 'show_in_rest' => true,
121 'single' => true,
122 'type' => 'string',
123 'sanitize_callback' => [$this, 'sanitize_json_ld_field'],
124 'auth_callback' => function() {
125 return current_user_can('edit_posts') || current_user_can('edit_pages');
126 }
127 ]);
128
129 // SEO meta fields for import support
130 $string_meta_fields = [
131 '_thinkrank_canonical_url',
132 '_thinkrank_og_title',
133 '_thinkrank_og_description',
134 '_thinkrank_og_image',
135 '_thinkrank_twitter_title',
136 '_thinkrank_twitter_description',
137 '_thinkrank_twitter_image',
138 '_thinkrank_imported_from',
139 ];
140
141 foreach ($string_meta_fields as $meta_key) {
142 register_post_meta('', $meta_key, [
143 'show_in_rest' => true,
144 'single' => true,
145 'type' => 'string',
146 'auth_callback' => function () {
147 return current_user_can('edit_posts') || current_user_can('edit_pages');
148 }
149 ]);
150 }
151
152 // Multiple focus keywords (array). The legacy single-value
153 // `_thinkrank_focus_keyword` is kept in sync by Focus_Keywords for
154 // backward compatibility and registered for REST as a string elsewhere.
155 register_post_meta('', Focus_Keywords::META_KEY, [
156 'show_in_rest' => [
157 'schema' => [
158 'type' => 'array',
159 'items' => ['type' => 'string'],
160 ],
161 ],
162 'single' => true,
163 'type' => 'array',
164 'sanitize_callback' => function ($value) {
165 return Focus_Keywords::normalize($value);
166 },
167 'auth_callback' => function () {
168 return current_user_can('edit_posts') || current_user_can('edit_pages');
169 }
170 ]);
171
172 register_post_meta('', Focus_Keywords::LEGACY_META_KEY, [
173 'show_in_rest' => true,
174 'single' => true,
175 'type' => 'string',
176 'auth_callback' => function () {
177 return current_user_can('edit_posts') || current_user_can('edit_pages');
178 }
179 ]);
180
181 register_post_meta('', '_thinkrank_robots_meta_enabled', [
182 'show_in_rest' => true,
183 'single' => true,
184 'type' => 'integer',
185 'auth_callback' => function () {
186 return current_user_can('edit_posts') || current_user_can('edit_pages');
187 }
188 ]);
189
190 register_post_meta('', '_thinkrank_robots_meta', [
191 'show_in_rest' => true,
192 'single' => true,
193 'type' => 'string',
194 'sanitize_callback' => [$this, 'sanitize_json_meta_field'],
195 'auth_callback' => function () {
196 return current_user_can('edit_posts') || current_user_can('edit_pages');
197 }
198 ]);
199
200 register_post_meta('', '_thinkrank_advanced_robots_meta', [
201 'show_in_rest' => true,
202 'single' => true,
203 'type' => 'string',
204 'sanitize_callback' => [$this, 'sanitize_json_meta_field'],
205 'auth_callback' => function () {
206 return current_user_can('edit_posts') || current_user_can('edit_pages');
207 }
208 ]);
209
210 register_post_meta('', '_thinkrank_primary_category', [
211 'show_in_rest' => true,
212 'single' => true,
213 'type' => 'integer',
214 'auth_callback' => function () {
215 return current_user_can('edit_posts') || current_user_can('edit_pages');
216 }
217 ]);
218 }
219
220 /**
221 * Sanitize JSON meta field data
222 *
223 * Validates JSON structure and recursively sanitizes all string values
224 * to prevent XSS and injection attacks.
225 *
226 * @param string $value Raw JSON string value
227 * @return string Sanitized JSON string or empty string if invalid
228 */
229 public function sanitize_json_meta_field(string $value): string {
230 // Return empty string for non-string values
231 if (!is_string($value) || empty($value)) {
232 return '';
233 }
234
235 // Validate JSON structure
236 $decoded = json_decode($value, true);
237 if (json_last_error() !== JSON_ERROR_NONE) {
238 // Invalid JSON - return empty string
239 return '';
240 }
241
242 // Check for reasonable data size (prevent JSON bombs)
243 if (strlen($value) > 50000) { // 50KB limit
244 return '';
245 }
246
247 // Recursively sanitize all values
248 $sanitized = $this->sanitize_json_recursively($decoded);
249
250 // Re-encode as JSON
251 $result = wp_json_encode($sanitized);
252 return $result !== false ? $result : '';
253 }
254
255 /**
256 * Recursively sanitize JSON data
257 *
258 * @param mixed $data Data to sanitize
259 * @param int $depth Current recursion depth
260 * @return mixed Sanitized data
261 */
262 private function sanitize_json_recursively($data, int $depth = 0) {
263 // Prevent deep recursion attacks
264 if ($depth > 10) {
265 return null;
266 }
267
268 if (is_array($data)) {
269 $sanitized = [];
270 foreach ($data as $key => $value) {
271 $clean_key = sanitize_key($key);
272 $sanitized[$clean_key] = $this->sanitize_json_recursively($value, $depth + 1);
273 }
274 return $sanitized;
275 }
276
277 if (is_string($data)) {
278 // Sanitize string data to prevent XSS
279 return sanitize_textarea_field($data);
280 }
281
282 if (is_numeric($data)) {
283 return $data;
284 }
285
286 if (is_bool($data)) {
287 return $data;
288 }
289
290 // For any other data type, return null
291 return null;
292 }
293
294 /**
295 * Sanitize JSON-LD meta field data
296 *
297 * Validates JSON structure, recursively sanitizes all string values,
298 * and preserves @ characters in keys (crucial for JSON-LD).
299 *
300 * @param string $value Raw JSON string value
301 *
302 * @return string Sanitized JSON string or empty string if invalid
303 */
304 public function sanitize_json_ld_field( string $value ): string {
305 // Return empty string for non-string values
306 if ( ! is_string( $value ) || empty( $value ) ) {
307 return '';
308 }
309
310 // Validate JSON structure
311 $decoded = json_decode( $value, true );
312 if ( json_last_error() !== JSON_ERROR_NONE ) {
313 // Invalid JSON - return empty string
314 return '';
315 }
316
317 // Check for reasonable data size (prevent JSON bombs)
318 if ( strlen( $value ) > 200000 ) { // Limit to 200KB for larger schemas
319 return '';
320 }
321
322 // Recursively sanitize all values
323 $sanitized = $this->sanitize_json_ld_recursively( $decoded );
324
325 // Re-encode as JSON
326 $result = wp_json_encode( $sanitized );
327
328 return $result !== false ? $result : '';
329 }
330
331 /**
332 * Recursively sanitize JSON-LD data
333 *
334 * Similar to sanitize_json_recursively but preserves @ symbol and case in keys.
335 *
336 * @param mixed $data Data to sanitize
337 * @param int $depth Current recursion depth
338 *
339 * @return mixed Sanitized data
340 */
341 private function sanitize_json_ld_recursively( $data, int $depth = 0 ) {
342 // Prevent deep recursion attacks
343 if ( $depth > 10 ) {
344 return null;
345 }
346
347 if ( is_array( $data ) ) {
348 $sanitized = [];
349 foreach ( $data as $key => $value ) {
350 // Allow alphanumeric, underscore, dash, and @ (crucial for JSON-LD)
351 // Also preserve case as JSON-LD keys are case-sensitive
352 $clean_key = preg_replace( '/[^a-zA-Z0-9_\-@]/', '', (string) $key );
353 $sanitized[ $clean_key ] = $this->sanitize_json_ld_recursively( $value, $depth + 1 );
354 }
355
356 return $sanitized;
357 }
358
359 if ( is_string( $data ) ) {
360 // Sanitize string data to prevent XSS
361 return sanitize_textarea_field( $data );
362 }
363
364 if ( is_numeric( $data ) ) {
365 return $data;
366 }
367
368 if ( is_bool( $data ) ) {
369 return $data;
370 }
371
372 // For any other data type, return null
373 return null;
374 }
375
376 // Removed debug methods
377
378 /**
379 * Add ThinkRank meta boxes
380 *
381 * @return void
382 */
383 public function add_meta_boxes(): void {
384 $post_types = $this->get_supported_post_types();
385
386 $is_block_editor = $this->is_block_editor_screen();
387
388 foreach ($post_types as $post_type) {
389 add_meta_box(
390 'thinkrank-seo-metabox',
391 __('ThinkRank SEO', 'thinkrank'),
392 [$this, 'render_seo_metabox'],
393 $post_type,
394 'normal',
395 'high'
396 );
397
398 // Classic Editor sidebar quick-access widget (mirrors SureRank's
399 // "Manage your SEO" sidebar box). Skipped in the Block Editor, which
400 // surfaces the full panel below the content instead.
401 if (!$is_block_editor) {
402 add_meta_box(
403 'thinkrank-seo-sidebar',
404 __('ThinkRank', 'thinkrank'),
405 [$this, 'render_sidebar_meta_box'],
406 $post_type,
407 'side',
408 'high'
409 );
410 }
411 }
412 }
413
414 /**
415 * Whether the current edit screen is using the Block Editor.
416 *
417 * @return bool True when the Block Editor is active, false for Classic Editor.
418 */
419 private function is_block_editor_screen(): bool {
420 if (!function_exists('get_current_screen')) {
421 return false;
422 }
423
424 $screen = get_current_screen();
425
426 return $screen instanceof \WP_Screen
427 && method_exists($screen, 'is_block_editor')
428 && $screen->is_block_editor();
429 }
430
431 /**
432 * Render the Classic Editor sidebar quick-access widget.
433 *
434 * Shows a short label plus a button that scrolls to (and expands) the full
435 * "ThinkRank SEO" panel in the main column.
436 *
437 * @param \WP_Post $post Post object.
438 * @return void
439 */
440 public function render_sidebar_meta_box(\WP_Post $post): void {
441 $box_title = apply_filters('thinkrank_seo_sidebar_box_title', __('Optimize this content for search & AI with ThinkRank.', 'thinkrank'), $post);
442 $cta_label = apply_filters('thinkrank_seo_sidebar_cta_label', __('Open ThinkRank SEO', 'thinkrank'), $post);
443 ?>
444 <div class="thinkrank-classic-sidebar-box">
445 <p class="thinkrank-classic-sidebar-box-title"><?php echo esc_html($box_title); ?></p>
446 <button
447 type="button"
448 class="button button-primary thinkrank-classic-sidebar-box-cta"
449 >
450 <?php echo esc_html($cta_label); ?>
451 </button>
452 </div>
453 <?php
454 }
455
456 /**
457 * Render SEO meta box
458 *
459 * @param \WP_Post $post Post object
460 * @return void
461 */
462 public function render_seo_metabox(\WP_Post $post): void {
463 // Add nonce for security
464 wp_nonce_field('thinkrank_metabox_nonce', 'thinkrank_metabox_nonce');
465
466 // Get existing metadata
467 $existing_metadata = $this->get_post_metadata($post->ID);
468
469 // Get post content for AI analysis
470 $content_preview = $this->get_content_preview($post);
471
472 // Render React metabox container with hidden form fields for data
473 ?>
474 <div id="thinkrank-metabox-container" class="thinkrank-metabox">
475
476 <!-- Hidden form fields for React to read initial data -->
477 <input type="hidden" id="thinkrank_seo_title" name="thinkrank_seo_title" value="<?php echo esc_attr($existing_metadata['title'] ?? ''); ?>" />
478 <input type="hidden" id="thinkrank_meta_description" name="thinkrank_meta_description" value="<?php echo esc_attr($existing_metadata['description'] ?? ''); ?>" />
479 <?php // Render-time mirrors of the two fields a background writer (Auto AI, bulk, import) can fill after load. The React app never touches these, so on save they still hold the value shown when the form loaded — letting persist_metadata() tell a stale blank apart from a deliberate clear. ?>
480 <input type="hidden" id="thinkrank_seo_title__orig" name="thinkrank_seo_title__orig" value="<?php echo esc_attr($existing_metadata['title'] ?? ''); ?>" />
481 <input type="hidden" id="thinkrank_meta_description__orig" name="thinkrank_meta_description__orig" value="<?php echo esc_attr($existing_metadata['description'] ?? ''); ?>" />
482 <input type="hidden" id="thinkrank_focus_keyword" name="thinkrank_focus_keyword" value="<?php echo esc_attr($existing_metadata['focus_keyword'] ?? ''); ?>" />
483 <input type="hidden" id="thinkrank_focus_keywords" name="thinkrank_focus_keywords" value="<?php echo esc_attr(wp_json_encode($existing_metadata['focus_keywords'] ?? [])); ?>" />
484 <input type="hidden" id="thinkrank_seo_score" name="thinkrank_seo_score" value="<?php echo esc_attr($existing_metadata['seo_score'] ?? '0'); ?>" />
485 <input type="hidden" id="thinkrank_generated_at" name="thinkrank_generated_at" value="<?php echo esc_attr($existing_metadata['generated_at'] ?? ''); ?>" />
486 <input type="hidden" id="thinkrank_pillar_content" name="thinkrank_pillar_content" value="<?php echo esc_attr($existing_metadata['pillar_content'] ?? ''); ?>" />
487 <input type="hidden" id="thinkrank_canonical_url" name="thinkrank_canonical_url" value="<?php echo esc_url($existing_metadata['canonical_url'] ?? ''); ?>" />
488 <input type="hidden" id="thinkrank_robots_meta_enabled" name="thinkrank_robots_meta_enabled" value="<?php echo esc_attr((string) ($existing_metadata['robots_meta_enabled'] ?? '0')); ?>" />
489 <input type="hidden" id="thinkrank_robots_meta" name="thinkrank_robots_meta" value="<?php echo esc_attr((string) ($existing_metadata['robots_meta'] ?? '')); ?>" />
490 <input type="hidden" id="thinkrank_advanced_robots_meta" name="thinkrank_advanced_robots_meta" value="<?php echo esc_attr((string) ($existing_metadata['advanced_robots_meta'] ?? '')); ?>" />
491 <input type="hidden" id="thinkrank_og_title" name="thinkrank_og_title" value="<?php echo esc_attr((string) ($existing_metadata['og_title'] ?? '')); ?>" />
492 <input type="hidden" id="thinkrank_og_description" name="thinkrank_og_description" value="<?php echo esc_attr((string) ($existing_metadata['og_description'] ?? '')); ?>" />
493 <input type="hidden" id="thinkrank_og_image" name="thinkrank_og_image" value="<?php echo esc_url((string) ($existing_metadata['og_image'] ?? '')); ?>" />
494 <input type="hidden" id="thinkrank_twitter_title" name="thinkrank_twitter_title" value="<?php echo esc_attr((string) ($existing_metadata['twitter_title'] ?? '')); ?>" />
495 <input type="hidden" id="thinkrank_twitter_description" name="thinkrank_twitter_description" value="<?php echo esc_attr((string) ($existing_metadata['twitter_description'] ?? '')); ?>" />
496 <input type="hidden" id="thinkrank_twitter_image" name="thinkrank_twitter_image" value="<?php echo esc_url((string) ($existing_metadata['twitter_image'] ?? '')); ?>" />
497 <textarea id="thinkrank_content_preview" style="display: none;"><?php echo esc_textarea($content_preview); ?></textarea>
498 </div>
499 <?php
500
501 }
502
503 /**
504 * Save meta box data
505 *
506 * @param int $post_id Post ID
507 * @param \WP_Post $post Post object
508 * @return void
509 */
510 public function save_meta_boxes(int $post_id, \WP_Post $post): void {
511 // Verify nonce
512 if (!isset($_POST['thinkrank_metabox_nonce'])) {
513 return;
514 }
515
516 $nonce = sanitize_text_field(wp_unslash($_POST['thinkrank_metabox_nonce']));
517 if (!wp_verify_nonce($nonce, 'thinkrank_metabox_nonce')) {
518 return;
519 }
520
521 // Check permissions
522 if (!current_user_can('edit_post', $post_id)) {
523 return;
524 }
525
526 // Skip autosave
527 if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
528 return;
529 }
530
531 // The classic/block editor submits the metabox fields as part of the
532 // #post form, so they arrive (slashed) in $_POST. Hand them straight to
533 // the shared persistence routine.
534 // phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce verified above
535 $this->persist_metadata($post_id, wp_unslash($_POST));
536 }
537
538 /**
539 * Persist metabox fields for a post from a form-field-name => value map,
540 * reusing the shared metabox persistence (sanitization, JSON encoding,
541 * focus-keyword normalization, empty-value deletion).
542 *
543 * Intended for non-form callers such as the MCP abilities layer. The
544 * caller is responsible for authorization; keys use the same
545 * `thinkrank_*` field names accepted by the metabox form (e.g.
546 * `thinkrank_seo_title`, `thinkrank_meta_description`, `thinkrank_robots_meta`).
547 *
548 * @param int $post_id Post to update.
549 * @param array $fields Field name => value map.
550 * @return void
551 */
552 public function save_seo_fields(int $post_id, array $fields): void {
553 $this->persist_metadata($post_id, $fields);
554 }
555
556 /**
557 * Persist all metabox fields for a post from a $_POST-shaped (already
558 * unslashed) source array.
559 *
560 * Shared by `save_meta_boxes()` (classic/block editor form submit) and
561 * `ajax_save_metabox()` (editors like Elementor that don't submit the #post
562 * form). Nonce/permission checks are the caller's responsibility. Each field
563 * is independently sanitized; missing keys are left untouched.
564 *
565 * @param int $post_id Post to update.
566 * @param array $src Field name => raw value map (unslashed).
567 * @return void
568 */
569 private function persist_metadata(int $post_id, array $src): void {
570 // Title & meta description are handled separately below: they can be
571 // written out-of-band (Auto AI on publish, imports)
572 // after an editor was opened, so a plain save from that now-stale editor
573 // would clobber the generated value with a blank. Focus keywords are
574 // likewise handled separately (array meta) via Focus_Keywords below.
575 $this->persist_seo_text_field($post_id, $src, 'thinkrank_seo_title', '_thinkrank_seo_title', 'sanitize_text_field');
576 $this->persist_seo_text_field($post_id, $src, 'thinkrank_meta_description', '_thinkrank_meta_description', 'sanitize_textarea_field');
577
578 $fields = [
579 'thinkrank_seo_score' => 'absint',
580 'thinkrank_generated_at' => 'sanitize_text_field',
581 'thinkrank_pillar_content' => 'sanitize_text_field',
582 ];
583
584 // Focus keywords: prefer the JSON array field; fall back to the legacy
585 // single string. Focus_Keywords::save() normalizes (dedupe, drop empty,
586 // cap at MAX) and keeps the legacy single-value meta in sync.
587 if (isset($src['thinkrank_focus_keywords'])) {
588 $raw_keywords = $src['thinkrank_focus_keywords'];
589 $decoded = is_string($raw_keywords) ? json_decode($raw_keywords, true) : $raw_keywords;
590 Focus_Keywords::save($post_id, is_array($decoded) ? $decoded : []);
591 } elseif (isset($src['thinkrank_focus_keyword'])) {
592 Focus_Keywords::save($post_id, $src['thinkrank_focus_keyword']);
593 }
594
595 // Update the post slug (post_name) when the metabox permalink field
596 // was edited. This touches the WP post itself, not post meta.
597 //
598 // The baseline is what the field was RENDERED with. Without it the
599 // guard here was a bare isset(), and the hidden input is always
600 // posted — so a user who edited WordPress's own permalink field in
601 // the Classic Editor had their new slug written by core and then
602 // overwritten by this page-load snapshot (#441).
603 if (isset($src['thinkrank_post_slug'])) {
604 $this->maybe_update_slug(
605 $post_id,
606 (string) $src['thinkrank_post_slug'],
607 isset($src['thinkrank_post_slug_baseline'])
608 ? (string) $src['thinkrank_post_slug_baseline']
609 : null
610 );
611 }
612
613 // Save canonical URL separately with URL sanitization
614 if (isset($src['thinkrank_canonical_url'])) {
615 $canonical_url = esc_url_raw((string) $src['thinkrank_canonical_url']);
616 if (empty($canonical_url)) {
617 delete_post_meta($post_id, '_thinkrank_canonical_url');
618 } else {
619 update_post_meta($post_id, '_thinkrank_canonical_url', $canonical_url);
620 }
621 }
622
623 foreach ($fields as $field => $sanitize_callback) {
624 if (isset($src[$field])) {
625 $value = call_user_func($sanitize_callback, $src[$field]);
626 update_post_meta($post_id, "_{$field}", $value);
627 }
628 }
629
630 $this->save_robots_meta($post_id, $src);
631 $this->save_social_meta($post_id, $src);
632
633 // Update last modified timestamp
634 update_post_meta($post_id, '_thinkrank_last_updated', current_time('mysql'));
635 }
636
637 /**
638 * Persist one SEO text field with an out-of-band-write guard.
639 *
640 * Auto AI (on publish) and imports write the SEO title / meta description
641 * directly to post meta. When that happens after an editor
642 * was opened, the editor's hidden input is a stale blank; a normal save
643 * would overwrite the freshly generated value with that blank. This guard
644 * skips the write only when the submitted value is empty AND it was also
645 * empty when the form was rendered (the `<field>__orig` mirror), yet the
646 * stored value is now non-empty — i.e. a background writer won the race.
647 *
648 * A deliberate clear still applies: if the field held a value at render and
649 * is submitted empty, `$orig` is non-empty so the guard does not trigger.
650 * Callers that don't send the `__orig` mirror (e.g. the MCP/Elementor
651 * paths) keep the plain write behavior.
652 *
653 * @param int $post_id Post being saved.
654 * @param array $src Unslashed field map.
655 * @param string $field POST field name (e.g. thinkrank_seo_title).
656 * @param string $meta_key Target post meta key.
657 * @param callable $sanitize Sanitizer applied to the submitted value.
658 * @return void
659 */
660 private function persist_seo_text_field(int $post_id, array $src, string $field, string $meta_key, callable $sanitize): void {
661 if (!isset($src[$field])) {
662 return; // Field absent from this submit → leave the stored value untouched.
663 }
664
665 $submitted = (string) call_user_func($sanitize, (string) $src[$field]);
666
667 if ('' === $submitted && isset($src[$field . '__orig'])) {
668 $orig = (string) $src[$field . '__orig'];
669 $stored = (string) get_post_meta($post_id, $meta_key, true);
670 // Blank now, blank at render, but populated in storage → a background
671 // write landed after this editor loaded; don't clobber it.
672 if ('' === $orig && '' !== $stored) {
673 return;
674 }
675 }
676
677 update_post_meta($post_id, $meta_key, $submitted);
678 }
679
680 /**
681 * Update the post slug (post_name) from the metabox permalink field.
682 *
683 * Runs inside the save_post cycle, so wp_update_post() would recurse — a
684 * static guard prevents re-entry. WordPress applies wp_unique_post_slug(),
685 * so a colliding slug is de-duplicated automatically. Empty input is left
686 * alone (WP keeps/auto-generates the slug); auto-drafts and revisions are
687 * skipped so we don't fight the editor's own slug generation.
688 *
689 * @param int $post_id Post to update.
690 * @param string $raw_slug Desired slug from the metabox.
691 * @return void
692 */
693 private function maybe_update_slug(int $post_id, string $raw_slug, ?string $baseline = null): void {
694 static $updating = false;
695 if ($updating) {
696 return;
697 }
698
699 $post = get_post($post_id);
700 if (!$post || wp_is_post_revision($post_id)) {
701 return;
702 }
703
704 if (in_array($post->post_status, ['auto-draft', 'trash'], true)) {
705 return;
706 }
707
708 $desired = sanitize_title($raw_slug);
709 if ($desired === '') {
710 return;
711 }
712
713 // Unchanged from what the form was rendered with, so the user did not
714 // choose this value — they left it alone. Writing it back would undo
715 // whatever core already saved from WordPress's own permalink field a
716 // moment ago, on the same save_post priority (#441).
717 //
718 // Compared against the BASELINE rather than the current post_name on
719 // purpose: by the time this runs core has already updated post_name,
720 // so that comparison cannot tell a deliberate edit from a stale one.
721 if ($baseline !== null && $desired === sanitize_title($baseline)) {
722 return;
723 }
724
725 if ($desired === $post->post_name) {
726 return;
727 }
728
729 $updating = true;
730 wp_update_post([
731 'ID' => $post_id,
732 'post_name' => $desired,
733 ]);
734 $updating = false;
735 }
736
737 /**
738 * Persist per-post Open Graph and Twitter Card overrides.
739 *
740 * Empty values delete the meta entry so the frontend falls back to the
741 * SEO title / meta description / featured image chain.
742 */
743 private function save_social_meta(int $post_id, array $src): void {
744 $text_fields = [
745 'thinkrank_og_title' => '_thinkrank_og_title',
746 'thinkrank_og_description' => '_thinkrank_og_description',
747 'thinkrank_twitter_title' => '_thinkrank_twitter_title',
748 'thinkrank_twitter_description' => '_thinkrank_twitter_description',
749 ];
750 foreach ($text_fields as $field => $meta_key) {
751 if (!isset($src[$field])) {
752 continue;
753 }
754 $value = sanitize_textarea_field((string) $src[$field]);
755 if ($value === '') {
756 delete_post_meta($post_id, $meta_key);
757 } else {
758 update_post_meta($post_id, $meta_key, $value);
759 }
760 }
761
762 $url_fields = [
763 'thinkrank_og_image' => '_thinkrank_og_image',
764 'thinkrank_twitter_image' => '_thinkrank_twitter_image',
765 ];
766 foreach ($url_fields as $field => $meta_key) {
767 if (!isset($src[$field])) {
768 continue;
769 }
770 $value = esc_url_raw((string) $src[$field]);
771 if ($value === '') {
772 delete_post_meta($post_id, $meta_key);
773 } else {
774 update_post_meta($post_id, $meta_key, $value);
775 }
776 }
777 }
778
779 /**
780 * Save per-post robots meta and advanced robots meta from the metabox.
781 *
782 * Stores the robots payloads as JSON-encoded strings, sanitized via
783 * sanitize_json_meta_field. The toggle plus the two JSON blobs are the
784 * single source of truth for per-post robots overrides.
785 */
786 private function save_robots_meta(int $post_id, array $src): void {
787 if (!isset($src['thinkrank_robots_meta_enabled'])) {
788 return;
789 }
790
791 $enabled = (int) (bool) $src['thinkrank_robots_meta_enabled'];
792 update_post_meta($post_id, '_thinkrank_robots_meta_enabled', $enabled);
793
794 if (isset($src['thinkrank_robots_meta'])) {
795 update_post_meta($post_id, '_thinkrank_robots_meta', $this->sanitize_json_meta_field((string) $src['thinkrank_robots_meta']));
796 }
797
798 if (isset($src['thinkrank_advanced_robots_meta'])) {
799 update_post_meta($post_id, '_thinkrank_advanced_robots_meta', $this->sanitize_json_meta_field((string) $src['thinkrank_advanced_robots_meta']));
800 }
801 }
802
803
804 /**
805 * Enqueue meta box scripts
806 *
807 * @param string $hook Current admin page hook
808 * @return void
809 */
810 public function enqueue_metabox_scripts(string $hook): void {
811 // Only load on post edit screens (including block editor)
812 if (!in_array($hook, ['post.php', 'post-new.php'], true)) {
813 return;
814 }
815
816 // Get current post type - handle both classic and block editor contexts
817 $current_post_type = $this->get_current_post_type();
818 if (!$current_post_type || !in_array($current_post_type, $this->get_supported_post_types(), true)) {
819 return;
820 }
821
822 // Get post object for additional data
823 global $post;
824
825 // Ensure wp.media is available for the social-image media picker.
826 wp_enqueue_media();
827
828 // No chunk dependencies needed - all bundled into main metabox.js
829 // Enqueue React metabox script with direct dependencies
830 $asset_file = THINKRANK_PLUGIN_DIR . 'assets/metabox.asset.php';
831 $asset = file_exists($asset_file) ? include $asset_file : [
832 'dependencies' => ['react', 'wp-element', 'wp-i18n', 'wp-api-fetch', 'wp-components'],
833 'version' => THINKRANK_VERSION
834 ];
835
836 // Use dependencies directly from the asset file. The pinned "Configure
837 // SEO" launcher needs wp-plugins (already listed by the build) and
838 // resolves PinnedItems from wp.editor / wp.interface at runtime, so we do
839 // NOT add wp-interface here: it is not a registered script handle on
840 // WP 7.x, and an unmet dependency would drop the whole metabox script.
841 $dependencies = $asset['dependencies'];
842
843 wp_enqueue_script(
844 'thinkrank-metabox',
845 THINKRANK_PLUGIN_URL . 'assets/metabox.js',
846 $dependencies,
847 $asset['version'],
848 true
849 );
850
851 // Localize script data (shared builder; reused by the Elementor editor
852 // integration, which has no #post form / hidden inputs of its own).
853 wp_localize_script('thinkrank-metabox', 'thinkrankMetabox', $this->get_localized_data($post->ID));
854
855 // Add defer attribute for non-blocking script loading
856 wp_script_add_data('thinkrank-metabox', 'defer', true);
857
858 // Enqueue metabox styles
859 wp_enqueue_style(
860 'thinkrank-metabox',
861 THINKRANK_PLUGIN_URL . 'assets/metabox.css',
862 ['wp-components'],
863 THINKRANK_VERSION
864 );
865
866 // Classic Editor sidebar widget: lightweight styles + a vanilla-JS
867 // handler so the "Optimize Here" button works without depending on the
868 // React bundle. The box itself is only registered in the Classic Editor.
869 $sidebar_css = <<<'CSS'
870 .thinkrank-classic-sidebar-box-title{margin:0 0 12px;font-size:13px;line-height:1.5;color:#1e1e1e;}
871 .thinkrank-classic-sidebar-box-cta{width:100%;text-align:center;justify-content:center;}
872 CSS;
873 wp_add_inline_style('thinkrank-metabox', $sidebar_css);
874
875 $sidebar_js = <<<'JS'
876 (function(){
877 document.addEventListener('click', function(e){
878 var btn = e.target.closest && e.target.closest('.thinkrank-classic-sidebar-box-cta');
879 if(!btn){return;}
880 e.preventDefault();
881 // Open the ThinkRank SEO drawer mounted by the React metabox app.
882 window.dispatchEvent(new CustomEvent('thinkrank:toggle-seo-drawer'));
883 });
884 })();
885 JS;
886 wp_add_inline_script('thinkrank-metabox', $sidebar_js);
887 }
888
889 /**
890 * Build the data object localized into the metabox script (`thinkrankMetabox`).
891 *
892 * Extracted so the Elementor editor integration can reuse the exact same
893 * configuration. Callers that run outside the #post form (Elementor) also
894 * read `existingMetadata`/`contentPreview`, which they add on top of this.
895 *
896 * @param int $post_id Post being edited.
897 * @return array Localized config consumed by the React metabox.
898 */
899 public function get_localized_data(int $post_id): array {
900 $post = get_post($post_id);
901 $post_type = $post ? $post->post_type : 'post';
902
903 // Resolve the correct REST API base for the current post type.
904 // Falls back to the post type slug for any type without a rest_base.
905 $post_type_obj = get_post_type_object($post_type);
906 $post_rest_base = ($post_type_obj && !empty($post_type_obj->rest_base))
907 ? $post_type_obj->rest_base
908 : $post_type;
909
910 return [
911 'ajaxUrl' => admin_url('admin-ajax.php'),
912 'nonce' => wp_create_nonce('thinkrank_metabox_ajax'),
913 'postId' => $post_id,
914 'postType' => $post_type,
915 'postRestBase' => $post_rest_base,
916 'postPermalink' => get_permalink($post_id),
917 'postSlug' => $post ? $post->post_name : '',
918 'restUrl' => rest_url('thinkrank/v1/'),
919 'restNonce' => wp_create_nonce('wp_rest'),
920 'homeUrl' => home_url(),
921 'siteName' => get_bloginfo('name'),
922 'faviconUrl' => $this->get_site_favicon_url(),
923 'featuredImageUrl' => $this->get_post_featured_image_url($post_id),
924 'strings' => [
925 'generating' => __('Generating...', 'thinkrank'),
926 'analyzing' => __('Analyzing...', 'thinkrank'),
927 'error' => __('Error occurred', 'thinkrank'),
928 'success' => __('Success!', 'thinkrank'),
929 'generated' => __('Metadata generated successfully', 'thinkrank'),
930 'contentTooShort' => __('Please add some content before generating SEO metadata.', 'thinkrank'),
931 'apiError' => __('Failed to connect to AI service. Please check your API settings.', 'thinkrank'),
932 ],
933 'seoScore' => $this->get_persisted_seo_score($post_id),
934 'postModified' => $post ? get_the_modified_date('c', $post) : '',
935 'linkSuggestionsEnabled' => $this->is_link_suggestions_enabled($post_type),
936 'postStatus' => get_post_status($post_id),
937 'isPro' => Plan_Config::is_pro(),
938 /**
939 * Filter the editor SEO panel's post-load refresh behaviour.
940 *
941 * The panel re-checks `/metadata/{id}` after load so values written
942 * by a background writer (Auto AI on publish, imports) appear
943 * without a reload. It only polls while the server
944 * reports a write in flight, but the poll lives in JavaScript, so
945 * the switch has to be localized into the bundle rather than being
946 * a PHP-side filter alone (#329).
947 *
948 * Set `enabled` to false to switch the refresh off entirely.
949 *
950 * @since 1.30.0
951 *
952 * @param array $config enabled (bool), intervalMs (int), maxTicks (int).
953 * @param int $post_id Post being edited.
954 */
955 'seoRefresh' => apply_filters(
956 'thinkrank_metabox_seo_refresh',
957 [
958 'enabled' => true,
959 'intervalMs' => 4000,
960 'maxTicks' => 10,
961 ],
962 $post_id
963 ),
964 // Whether any AI provider API key is configured — gates the
965 // "Generate with AI" button in the metabox
966 'aiConfigured' => !empty($this->settings->get('openai_api_key', ''))
967 || !empty($this->settings->get('claude_api_key', ''))
968 || !empty($this->settings->get('gemini_api_key', ''))
969 || !empty($this->settings->get('openrouter_api_key', '')),
970 // Focus keywords plan limits (max_keywords; 0 = unlimited).
971 'focusKeywords' => Plan_Config::focus_keywords(),
972 // Resolved Global/Bulk SEO variable-tag patterns for this post, shown
973 // as placeholder previews when a field is empty (the frontend applies
974 // these same patterns on output). Typing a value overrides them.
975 'patternPreviews' => Pattern_Resolver::previews($post_id),
976 // Token => value map (keys without %), for live client-side preview of
977 // a custom pattern typed into a metabox field.
978 'patternVariables' => Pattern_Resolver::variables($post_id),
979 ];
980 }
981
982 /**
983 * Latest persisted SEO score for a post.
984 *
985 * Read from the scores table — the same source the posts list column and the
986 * Analysis panel use — so the editor badge agrees with them on load rather
987 * than showing 0 until the Analysis panel mounts and fetches the score.
988 *
989 * The `thinkrank_seo_score` form field is unusable for this: it is only
990 * written by the AI "Analyze Content" flow and is reset to 0 on every save.
991 *
992 * @param int $post_id Post ID
993 * @return int|null Score, or null when the post has never been analyzed.
994 */
995 private function get_persisted_seo_score(int $post_id): ?int {
996 $existing = $this->seo_calculator->get_existing_score_data($post_id);
997 $score = $existing['overall_score'] ?? null;
998
999 return $score !== null ? (int) $score : null;
1000 }
1001
1002 /**
1003 * Check if link suggestions are enabled for a post type
1004 *
1005 * @param string $post_type Post type to check
1006 * @return bool True if enabled, false otherwise
1007 */
1008 private function is_link_suggestions_enabled(string $post_type): bool {
1009 $settings = get_option('thinkrank_global_seo_settings', []);
1010
1011 if (isset($settings[$post_type]['link_suggestions'])) {
1012 return (bool) $settings[$post_type]['link_suggestions'];
1013 }
1014
1015 return true;
1016 }
1017
1018 /**
1019 * Get current post type in admin context
1020 *
1021 * Handles both classic editor and block editor contexts
1022 *
1023 * @return string|null Current post type or null if not found
1024 */
1025 private function get_current_post_type(): ?string {
1026 global $post, $typenow, $current_screen;
1027
1028 // Try to get post type from various sources
1029 if ($post && !empty($post->post_type)) {
1030 return $post->post_type;
1031 }
1032
1033 if (!empty($typenow)) {
1034 return $typenow;
1035 }
1036
1037 if ($current_screen && !empty($current_screen->post_type)) {
1038 return $current_screen->post_type;
1039 }
1040
1041 // Fallback: check URL parameters for block editor
1042 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading URL parameters for context determination, not processing form data
1043 if (isset($_GET['post_type'])) {
1044 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading URL parameters for context determination, not processing form data
1045 return sanitize_text_field(wp_unslash($_GET['post_type']));
1046 }
1047
1048 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading URL parameters for context determination, not processing form data
1049 if (isset($_GET['post'])) {
1050 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading URL parameters for context determination, not processing form data
1051 $post_id = absint($_GET['post']);
1052 $post_type = get_post_type($post_id);
1053 if ($post_type) {
1054 return $post_type;
1055 }
1056 }
1057
1058 return null;
1059 }
1060
1061 /**
1062 * Get supported post types
1063 *
1064 * @return array Supported post types
1065 */
1066 public function get_supported_post_types(): array {
1067 $default_types = ['post', 'page'];
1068
1069 // Add WooCommerce product if available
1070 if (class_exists('WooCommerce')) {
1071 $default_types[] = 'product';
1072 }
1073
1074 // Add other common e-commerce post types
1075 $ecommerce_types = ['product', 'shop_order', 'shop_coupon'];
1076 foreach ($ecommerce_types as $type) {
1077 if (post_type_exists($type) && !in_array($type, $default_types, true)) {
1078 $default_types[] = $type;
1079 }
1080 }
1081
1082 // Add custom post types that are public and have UI
1083 $custom_post_types = get_post_types([
1084 'public' => true,
1085 'show_ui' => true,
1086 '_builtin' => false,
1087 ]);
1088
1089 foreach ($custom_post_types as $post_type) {
1090 // Skip certain post types that shouldn't have SEO metabox
1091 $excluded_types = [
1092 'attachment',
1093 'revision',
1094 'nav_menu_item',
1095 'custom_css',
1096 'customize_changeset',
1097 'oembed_cache',
1098 'user_request',
1099 'wp_block',
1100 'wp_template',
1101 'wp_template_part',
1102 'wp_global_styles',
1103 'wp_navigation',
1104 'acf-field',
1105 'acf-field-group',
1106 ];
1107
1108 if (!in_array($post_type, $excluded_types, true) && !in_array($post_type, $default_types, true)) {
1109 $default_types[] = $post_type;
1110 }
1111 }
1112
1113 return apply_filters('thinkrank_supported_post_types', $default_types);
1114 }
1115
1116 /**
1117 * Get existing post metadata
1118 *
1119 * @param int $post_id Post ID
1120 * @return array Existing metadata
1121 */
1122 public function get_post_metadata(int $post_id): array {
1123 return [
1124 'title' => get_post_meta($post_id, '_thinkrank_seo_title', true),
1125 'description' => get_post_meta($post_id, '_thinkrank_meta_description', true),
1126 'focus_keyword' => Focus_Keywords::get_primary($post_id),
1127 'focus_keywords' => Focus_Keywords::get($post_id),
1128 'seo_score' => get_post_meta($post_id, '_thinkrank_seo_score', true),
1129 'generated_at' => get_post_meta($post_id, '_thinkrank_generated_at', true),
1130 'pillar_content' => get_post_meta($post_id, '_thinkrank_pillar_content', true),
1131 'canonical_url' => get_post_meta($post_id, '_thinkrank_canonical_url', true),
1132 'robots_meta_enabled' => get_post_meta($post_id, '_thinkrank_robots_meta_enabled', true),
1133 'robots_meta' => get_post_meta($post_id, '_thinkrank_robots_meta', true),
1134 'advanced_robots_meta' => get_post_meta($post_id, '_thinkrank_advanced_robots_meta', true),
1135 'og_title' => get_post_meta($post_id, '_thinkrank_og_title', true),
1136 'og_description' => get_post_meta($post_id, '_thinkrank_og_description', true),
1137 'og_image' => get_post_meta($post_id, '_thinkrank_og_image', true),
1138 'twitter_title' => get_post_meta($post_id, '_thinkrank_twitter_title', true),
1139 'twitter_description' => get_post_meta($post_id, '_thinkrank_twitter_description', true),
1140 'twitter_image' => get_post_meta($post_id, '_thinkrank_twitter_image', true),
1141 ];
1142 }
1143
1144 /**
1145 * Get site favicon URL
1146 *
1147 * Prefers WordPress' native Site Icon (zero HTTP). Only when no Site Icon is
1148 * configured does it probe common favicon locations — and that probe is
1149 * cached (including a "no favicon" sentinel) so the editor never fires
1150 * blocking loopback HEAD requests on every load.
1151 *
1152 * @since 1.16.2 Prefer get_site_icon_url() and cache the fallback probe.
1153 * @return string Favicon URL, or '' when none can be resolved.
1154 */
1155 private function get_site_favicon_url(): string {
1156 // Try the native Site Icon first (WordPress 4.3+) — no HTTP required.
1157 $site_icon_url = get_site_icon_url();
1158 if ($site_icon_url) {
1159 return $site_icon_url;
1160 }
1161
1162 // No Site Icon configured: fall back to probing common favicon paths.
1163 // Cache the resolved value (empty string included) so the blocking
1164 // loopback probe runs at most once per ~12h per site host instead of on
1165 // every editor load.
1166 $cache_key = 'thinkrank_favicon_url_' . md5((string) wp_parse_url(home_url(), PHP_URL_HOST));
1167 $cached = get_transient($cache_key);
1168 if (false !== $cached) {
1169 return (string) $cached;
1170 }
1171
1172 $favicon_url = '';
1173
1174 $favicon_paths = [
1175 '/favicon.ico',
1176 '/favicon.png',
1177 '/apple-touch-icon.png',
1178 ];
1179
1180 foreach ($favicon_paths as $path) {
1181 $candidate = home_url($path);
1182 $response = wp_remote_head($candidate, ['timeout' => 3]);
1183 if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) === 200) {
1184 $favicon_url = $candidate;
1185 break;
1186 }
1187 }
1188
1189 set_transient($cache_key, $favicon_url, 12 * HOUR_IN_SECONDS);
1190
1191 return $favicon_url;
1192 }
1193
1194 /**
1195 * Get post featured image URL
1196 *
1197 * @param int $post_id Post ID
1198 * @return string|null Featured image URL or null if not available
1199 */
1200 private function get_post_featured_image_url(int $post_id): ?string {
1201 $thumbnail_id = get_post_thumbnail_id($post_id);
1202 if ($thumbnail_id) {
1203 // Use the full-size image — social share images need a large source
1204 // (the preview scales it down), not a small thumbnail.
1205 $image_url = wp_get_attachment_image_url($thumbnail_id, 'full');
1206 return $image_url ?: null;
1207 }
1208 return null;
1209 }
1210
1211 /**
1212 * Get content preview for AI analysis
1213 *
1214 * @param \WP_Post $post Post object
1215 * @return string Content preview
1216 */
1217 public function get_content_preview(\WP_Post $post): string {
1218 $content = $post->post_title . "\n\n";
1219
1220 if (!empty($post->post_excerpt)) {
1221 $content .= $post->post_excerpt . "\n\n";
1222 }
1223
1224 // Resolve through Builder_Content: a page builder keeps its words
1225 // outside post_content (Oxygen empties it entirely), and the editor
1226 // cannot reach postmeta, so without this the preview handed to the
1227 // browser is just the title and the live panel reports "No content".
1228 if (!class_exists('\\ThinkRank\\SEO\\Builder_Content')) {
1229 require_once THINKRANK_PLUGIN_DIR . 'includes/seo/class-builder-content.php';
1230 }
1231 $content .= \ThinkRank\SEO\Builder_Content::resolve($post);
1232
1233 // Clean and limit content
1234 $content = wp_strip_all_tags($content);
1235 $content = preg_replace('/\s+/', ' ', $content);
1236
1237 return trim(substr($content, 0, 4000));
1238 }
1239
1240 /**
1241 * AJAX handler for generating post metadata
1242 *
1243 * @return void
1244 */
1245 public function ajax_generate_post_metadata(): void {
1246 // Verify nonce
1247 $nonce = sanitize_text_field(wp_unslash($_POST['nonce'] ?? ''));
1248 if (!wp_verify_nonce($nonce, 'thinkrank_metabox_ajax')) {
1249 wp_die('Security check failed');
1250 }
1251
1252 // Check permissions
1253 $post_id = absint($_POST['post_id'] ?? 0);
1254 if (!current_user_can('edit_post', $post_id)) {
1255 wp_die('Insufficient permissions');
1256 }
1257
1258 try {
1259 $options = [
1260 'target_keyword' => sanitize_text_field(wp_unslash($_POST['target_keyword'] ?? '')),
1261 'content_type' => sanitize_text_field(wp_unslash($_POST['content_type'] ?? 'blog_post')),
1262 'tone' => sanitize_text_field(wp_unslash($_POST['tone'] ?? 'professional')),
1263 ];
1264
1265 $metadata = $this->metadata_generator->generate_for_post($post_id, $options);
1266
1267 wp_send_json_success([
1268 'metadata' => $metadata,
1269 'message' => __('SEO metadata generated successfully!', 'thinkrank'),
1270 ]);
1271 } catch (\Exception $e) {
1272 wp_send_json_error([
1273 'message' => $e->getMessage(),
1274 ]);
1275 }
1276 }
1277
1278 /**
1279 * AJAX: persist the full set of metabox fields for a post.
1280 *
1281 * Used by editors that don't submit the #post form (Elementor). Expects the
1282 * same field names the classic/block metabox submits, sent as POST params.
1283 * $_POST is slashed by WordPress, matching what `persist_metadata()` expects.
1284 *
1285 * @return void
1286 */
1287 public function ajax_save_metabox(): void {
1288 $nonce = sanitize_text_field(wp_unslash($_POST['nonce'] ?? ''));
1289 if (!wp_verify_nonce($nonce, 'thinkrank_metabox_ajax')) {
1290 wp_send_json_error(['message' => __('Security check failed', 'thinkrank')], 403);
1291 }
1292
1293 $post_id = absint($_POST['post_id'] ?? 0);
1294 if (!$post_id || !current_user_can('edit_post', $post_id)) {
1295 wp_send_json_error(['message' => __('Insufficient permissions', 'thinkrank')], 403);
1296 }
1297
1298 // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- nonce verified above; each field sanitized inside persist_metadata()
1299 $this->persist_metadata($post_id, wp_unslash($_POST));
1300
1301 wp_send_json_success([
1302 'message' => __('SEO settings saved successfully!', 'thinkrank'),
1303 ]);
1304 }
1305 }
1306