503)); return SEO_AI_Connector::get_instance()->rest_signed_write_permission($request); } private static function verified($request) { if (!is_object($request) || !method_exists($request, 'get_attributes')) return false; $attributes = $request->get_attributes(); return is_array($attributes) && ($attributes['ai4ranks_signed_write_verified'] ?? null) === true; } private static function error($message, $status = 409) { return new WP_Error('ai4ranks_agent_draft', $message, array('status' => $status)); } public static function request($request) { if (!self::verified($request)) return self::error('Unauthorized', 401); if (strlen($request->get_body()) > 100000) return self::error('Request too large', 413); $args = $request->get_json_params(); if (!is_array($args)) return self::error('A JSON request body is required', 400); $action = $args['action'] ?? null; if ($action === 'capabilities') return array('schema_version' => 1, 'draft_only' => true); if ($action !== 'create') return self::error('Only draft creation is supported', 422); if (!is_array($args) || array_diff(array_keys($args), array('operation_id','action','draft_id','title','content_html','excerpt'))) return self::error('Unexpected request fields', 422); $id = $args['draft_id'] ?? null; $title = $args['title'] ?? null; $html = $args['content_html'] ?? null; $excerpt = $args['excerpt'] ?? null; if (!is_string($id) || !preg_match('/^art_[a-f0-9]{32}$/D', $id) || !is_string($title) || trim($title) === '' || strlen($title) > 800 || !is_string($html) || trim($html) === '' || strlen($html) > 80000 || !is_string($excerpt) || strlen($excerpt) > 2000) return self::error('Invalid draft', 422); if (wp_strip_all_tags($title) !== $title || wp_kses_post($html) !== $html || wp_strip_all_tags($excerpt) !== $excerpt) return self::error('Review sanitized content before saving', 422); $hash = hash('sha256', $title . " " . $html . " " . $excerpt); global $wpdb; $lock = 'ai4ranks_draft_' . substr(hash('sha256', $id), 0, 40); if ((int)$wpdb->get_var($wpdb->prepare('SELECT GET_LOCK(%s, 0)', $lock)) !== 1) return self::error('Draft creation is already in progress'); try { $key = 'ai4ranks_agent_' . $id; $stored = get_option($key, null); if ($stored !== null) { if (!is_array($stored) || ($stored['hash'] ?? '') !== $hash || empty($stored['post_id'])) return self::error('Existing operation needs review; do not repeat creation'); $post = get_post((int)$stored['post_id']); } else { // Durable fence remains after an interrupted insert. Never create twice. if (!add_option($key, array('hash' => $hash, 'post_id' => null), '', false)) return self::error('Operation already claimed'); $post_id = wp_insert_post(wp_slash(array('post_type' => 'post', 'post_status' => 'draft', 'post_title' => $title, 'post_content' => $html, 'post_excerpt' => $excerpt)), true); if (is_wp_error($post_id)) return self::error('Draft creation failed; inspect the operation'); update_option($key, array('hash' => $hash, 'post_id' => (int)$post_id), false); $post = get_post($post_id); } if (!$post || $post->post_status !== 'draft' || $post->post_title !== $title || $post->post_content !== $html || $post->post_excerpt !== $excerpt) return self::error('Stored draft differs from the approved content'); return array('schema_version' => 1, 'draft_id' => $id, 'post_id' => (int)$post->ID, 'post_status' => 'draft', 'content_hash' => $hash); } finally { $wpdb->get_var($wpdb->prepare('SELECT RELEASE_LOCK(%s)', $lock)); } } } add_action('rest_api_init', function () { register_rest_route('seo-ai/v1', '/agent-drafts', array('methods' => 'POST', 'callback' => array('AI4Ranks_Agent_Drafts', 'request'), 'permission_callback' => array('AI4Ranks_Agent_Drafts', 'permission'))); });