true]; public function __construct() { $this->handle_form_submissions(); } private function handle_form_submissions(): void { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce is verified below via check_admin_referer(). if (isset($_POST['contai_generate_now'])) { check_admin_referer('contai_comments_nonce', 'contai_comments_nonce'); if (!current_user_can('manage_options')) { add_settings_error( 'contai_comments', 'no_permission', __('You do not have permission to perform this action.', '1platform-content-ai'), 'error' ); return; } $this->handle_comment_generation(); } } private function handle_comment_generation(): void { // Validate credits before generating comments require_once __DIR__ . '/../../../services/billing/CreditGuard.php'; $creditGuard = new ContaiCreditGuard(); $creditCheck = $creditGuard->validateCredits(); if (!$creditCheck['has_credits']) { add_settings_error( 'contai_comments', 'insufficient_credits', $creditCheck['message'], 'error' ); return; } // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verified in handle_form_submissions() via check_admin_referer(). $num_posts = isset($_POST['contai_num_posts']) ? absint($_POST['contai_num_posts']) : 10; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verified in handle_form_submissions() via check_admin_referer(). $comments_per_post = isset($_POST['contai_comments_per_post']) ? absint($_POST['contai_comments_per_post']) : 1; $num_posts = max(1, min(100, $num_posts)); $comments_per_post = max(1, min(10, $comments_per_post)); $posts = get_posts([ 'numberposts' => $num_posts, 'orderby' => 'rand', 'post_type' => 'post', 'post_status' => 'publish', ]); if (empty($posts)) { add_settings_error( 'contai_comments', 'no_posts', __('No published posts found to generate comments for.', '1platform-content-ai'), 'error' ); return; } $this->generated_comments = $this->generate_comments_for_posts($posts, $comments_per_post); $this->comments_generated = true; } private function generate_comments_for_posts(array $posts, int $comments_per_post): array { $service = ContaiCommentsService::create(); $website_topic = get_option('contai_site_theme', ''); $comments = []; foreach ($posts as $post) { $context = ContaiCommentsService::buildContext($website_topic, $post->post_title); $lang = ContaiCommentsService::languageForPost($post); $result = $service->generateComments($comments_per_post, $lang, $context); if (!$result['success']) { contai_log(sprintf( '[WPContentAI] Comments generation failed for post #%d (%s): %s', $post->ID, $post->post_title, $result['error'] )); $this->posts_failed++; continue; } $this->posts_processed++; foreach ($result['comments'] as $api_comment) { $full_name = sanitize_text_field($api_comment['full_name'] ?? ''); $content = sanitize_textarea_field($api_comment['content'] ?? ''); if (empty($full_name) || empty($content)) { continue; } [$comment_date, $comment_date_gmt] = ContaiCommentsService::commentDatesForPost($post); $email = $this->generate_email_from_name($full_name); wp_insert_comment([ 'comment_post_ID' => $post->ID, 'comment_author' => $full_name, 'comment_author_email' => $email, 'comment_author_url' => '', 'comment_content' => $content, 'comment_approved' => 1, 'comment_date' => $comment_date, 'comment_date_gmt' => $comment_date_gmt, 'user_id' => 0, ]); $comments[] = [ 'url' => get_permalink($post->ID), 'title' => $post->post_title, 'name' => $full_name, 'email' => $email, 'comment' => $content, ]; } } return $comments; } private function generate_email_from_name(string $name): string { $normalized = strtolower(str_replace(' ', '.', $name)); $normalized = preg_replace('/[^a-z0-9.]/', '', $normalized); return $normalized . '@example.com'; } public function render(): void { $this->render_notices(); settings_errors('contai_comments'); $creditGuard = new ContaiCreditGuard(); $this->creditCheck = $creditGuard->validateCredits(); if ( ! $this->creditCheck['has_credits'] ) : ?>

— creditCheck['balance'], 2 ) ), esc_html( $this->creditCheck['currency'] ) ); ?>

render_generation_form(); if ($this->comments_generated && !empty($this->generated_comments)) { $this->render_results_table(); } } private function render_notices(): void { if (!$this->comments_generated) { return; } $total = count($this->generated_comments); if ($total > 0): ?>

posts_processed) ); ?>

posts_failed > 0): ?>

posts_failed) ); ?>

generated_comments) ); ?>

generated_comments as $row): ?>