| @@ -280,9 +280,9 @@ | ||
| 280 | 280 | |
| 281 | 281 | $upload = wp_handle_upload($file_array, ['test_form' => false]); |
| 282 | 282 | |
| 283 | 283 | if (isset($upload['error'])) { |
| 284 | - echo "<p>Error uploading: " . esc_html($filename) . "</p>"; | |
| 284 | + echo "<p>Error uploading: {$filename}</p>"; | |
| 285 | 285 | continue; |
| 286 | 286 | } |
| 287 | 287 | |
| 288 | 288 | $file_url = $upload['url']; |
| @@ -287,9 +287,9 @@ | ||
| 287 | 287 | |
| 288 | 288 | $file_url = $upload['url']; |
| 289 | 289 | $file_path = $upload['file']; |
| 290 | 290 | |
| 291 | - echo "<p>Uploaded: " . esc_html($filename) . "</p>"; | |
| 291 | + echo "<p>Uploaded: $filename</p>"; | |
| 292 | 292 | |
| 293 | 293 | $handle = fopen($file_path, 'r'); // phpcs:ignore WordPress.WP.AlternativeFunctions |
| 294 | 294 | if ($handle === false) { |
| 295 | 295 | echo "<p style='color:red;'>Failed to open CSV file</p>"; |
| @@ -302,9 +302,9 @@ | ||
| 302 | 302 | fclose($handle); // phpcs:ignore WordPress.WP.AlternativeFunctions |
| 303 | 303 | continue; |
| 304 | 304 | } |
| 305 | 305 | |
| 306 | - echo "<p>Columns: " . esc_html( implode(', ', $header) ) . "</p>"; | |
| 306 | + echo "<p>Columns: " . implode(', ', $header) . "</p>"; | |
| 307 | 307 | |
| 308 | 308 | global $wpdb; |
| 309 | 309 | $table = $wpdb->prefix . "rag_documents"; |
| 310 | 310 | $row_count = 0; |
| @@ -324,16 +324,15 @@ | ||
| 324 | 324 | continue; |
| 325 | 325 | } |
| 326 | 326 | $embedding = $this->generate_embedding($content); |
| 327 | 327 | if (empty($embedding)) { |
| 328 | - echo "<p style='color:red;'>Failed embedding for row " . esc_html($row_count) . "</p>"; | |
| 328 | + echo "<p style='color:red;'>Failed embedding for row $row_count</p>"; | |
| 329 | 329 | continue; |
| 330 | 330 | } |
| 331 | 331 | |
| 332 | 332 | $title = !empty($row[0]) ? substr($row[0], 0, 100) : "CSV Row $row_count"; |
| 333 | 333 | |
| 334 | - $result = $wpdb->insert( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery | |
| 335 | - $table, [ | |
| 334 | + $result = $wpdb->insert($table, [ | |
| 336 | 335 | 'title' => sanitize_text_field($title), |
| 337 | 336 | 'content' => $content, |
| 338 | 337 | 'embedding' => wp_json_encode($embedding), |
| 339 | 338 | 'source_type' => 'csv', |
| @@ -346,14 +345,14 @@ | ||
| 346 | 345 | |
| 347 | 346 | if ($result !== false) { |
| 348 | 347 | $success_count++; |
| 349 | 348 | } else { |
| 350 | - echo "<p style='color:red;'>DB error row " . esc_html($row_count) . ": " . esc_html($wpdb->last_error) . "</p>"; | |
| 349 | + echo "<p style='color:red;'>DB error row $row_count: " . $wpdb->last_error . "</p>"; | |
| 351 | 350 | } |
| 352 | 351 | } |
| 353 | 352 | |
| 354 | 353 | fclose($handle); // phpcs:ignore WordPress.WP.AlternativeFunctions |
| 355 | - echo "<p style='color:green;'>✓ Processed " . esc_html($success_count) . " of " . esc_html($row_count) . " rows</p>"; | |
| 354 | + echo "<p style='color:green;'>✓ Processed $success_count of $row_count rows</p>"; | |
| 356 | 355 | } |
| 357 | 356 | |
| 358 | 357 | echo "<h3>CSV Processing Complete!</h3>"; |
| 359 | 358 | } |
| @@ -379,9 +378,9 @@ | ||
| 379 | 378 | |
| 380 | 379 | $upload = wp_handle_upload($file_array, ['test_form' => false]); |
| 381 | 380 | |
| 382 | 381 | if (isset($upload['error'])) { |
| 383 | - echo "<p>Error uploading: " . esc_html($filename) . "</p>"; | |
| 382 | + echo "<p>Error uploading: {$filename}</p>"; | |
| 384 | 383 | continue; |
| 385 | 384 | } |
| 386 | 385 | |
| 387 | 386 | $file_url = $upload['url']; |
| @@ -386,9 +385,9 @@ | ||
| 386 | 385 | |
| 387 | 386 | $file_url = $upload['url']; |
| 388 | 387 | $file_path = $upload['file']; |
| 389 | 388 | |
| 390 | - echo "<p>Uploaded: " . esc_html($filename) . "</p>"; | |
| 389 | + echo "<p>Uploaded: $filename</p>"; | |
| 391 | 390 | |
| 392 | 391 | // Extract PDF text (uses Smalot/PdfParser) |
| 393 | 392 | if (!class_exists('\Smalot\PdfParser\Parser')) { |
| 394 | 393 | echo "<p>PDF parser missing! Install `smalot/pdfparser`.</p>"; |
| @@ -398,15 +397,15 @@ | ||
| 398 | 397 | $parser = new \Smalot\PdfParser\Parser(); |
| 399 | 398 | $pdf = $parser->parseFile($file_path); |
| 400 | 399 | $text = $pdf->getText(); |
| 401 | 400 | |
| 402 | - echo "<p>Extracted text length: " . esc_html(strlen($text)) . "</p>"; | |
| 401 | + echo "<p>Extracted text length: ".strlen($text)."</p>"; | |
| 403 | 402 | |
| 404 | 403 | // Generate Embedding |
| 405 | 404 | $embedding = $this->generate_embedding($text); |
| 406 | 405 | |
| 407 | 406 | if (empty($embedding)) { |
| 408 | - echo "<p style='color:red;'>Failed to generate embedding for: " . esc_html($filename) . "</p>"; | |
| 407 | + echo "<p style='color:red;'>Failed to generate embedding for: $filename</p>"; | |
| 409 | 408 | continue; |
| 410 | 409 | } |
| 411 | 410 | |
| 412 | 411 | // Save to DB |
| @@ -412,10 +411,9 @@ | ||
| 412 | 411 | // Save to DB |
| 413 | 412 | global $wpdb; |
| 414 | 413 | $table = $wpdb->prefix . "rag_documents"; |
| 415 | 414 | |
| 416 | - $result = $wpdb->insert( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery | |
| 417 | - $table, [ | |
| 415 | + $result = $wpdb->insert($table, [ | |
| 418 | 416 | 'title' => sanitize_text_field($filename), |
| 419 | 417 | 'content' => $text, |
| 420 | 418 | 'embedding' => wp_json_encode($embedding), |
| 421 | 419 | 'source_type' => 'pdf', |
| @@ -426,11 +424,11 @@ | ||
| 426 | 424 | 'created_at' => current_time('mysql') |
| 427 | 425 | ]); |
| 428 | 426 | |
| 429 | 427 | if ($result === false) { |
| 430 | - echo "<p style='color:red;'>Database error: " . esc_html($wpdb->last_error) . "</p>"; | |
| 428 | + echo "<p style='color:red;'>Database error: " . $wpdb->last_error . "</p>"; | |
| 431 | 429 | } else { |
| 432 | - echo "<p style='color:green;'>✓ Saved PDF embedding for: " . esc_html($filename) . " (ID: " . esc_html($wpdb->insert_id) . ")</p>"; | |
| 430 | + echo "<p style='color:green;'>✓ Saved PDF embedding for: $filename (ID: " . $wpdb->insert_id . ")</p>"; | |
| 433 | 431 | } |
| 434 | 432 | } |
| 435 | 433 | |
| 436 | 434 | echo "<h3>PDF Processing Complete!</h3>"; |
| @@ -461,9 +459,9 @@ | ||
| 461 | 459 | 'test_type' => false, // Bypass mime type check |
| 462 | 460 | ]); |
| 463 | 461 | |
| 464 | 462 | if (isset($upload['error'])) { |
| 465 | - echo "<p>Error uploading " . esc_html($filename) . ": " . esc_html($upload['error']) . "</p>"; | |
| 463 | + echo "<p>Error uploading {$filename}: " . $upload['error'] . "</p>"; | |
| 466 | 464 | continue; |
| 467 | 465 | } |
| 468 | 466 | |
| 469 | 467 | $file_url = $upload['url']; |
| @@ -468,9 +466,9 @@ | ||
| 468 | 466 | |
| 469 | 467 | $file_url = $upload['url']; |
| 470 | 468 | $file_path = $upload['file']; |
| 471 | 469 | |
| 472 | - echo "<p>Uploaded: " . esc_html($filename) . "</p>"; | |
| 470 | + echo "<p>Uploaded: $filename</p>"; | |
| 473 | 471 | |
| 474 | 472 | // Read XAML/XML content |
| 475 | 473 | global $wp_filesystem; |
| 476 | 474 | if ( empty( $wp_filesystem ) ) { |
| @@ -479,9 +477,9 @@ | ||
| 479 | 477 | } |
| 480 | 478 | $xml_content = $wp_filesystem->get_contents( $file_path ); |
| 481 | 479 | |
| 482 | 480 | if (empty($xml_content)) { |
| 483 | - echo "<p style='color:red;'>Failed to read file or file is empty: " . esc_html($filename) . "</p>"; | |
| 481 | + echo "<p style='color:red;'>Failed to read file or file is empty: $filename</p>"; | |
| 484 | 482 | continue; |
| 485 | 483 | } |
| 486 | 484 | |
| 487 | 485 | // Attempt to parse as XML |
| @@ -530,9 +528,9 @@ | ||
| 530 | 528 | // Generate Embedding |
| 531 | 529 | $embedding = $this->generate_embedding($clean_content); |
| 532 | 530 | |
| 533 | 531 | if (empty($embedding)) { |
| 534 | - echo "<p style='color:red;'>Failed to generate embedding for item: " . esc_html($item_data['title']) . "</p>"; | |
| 532 | + echo "<p style='color:red;'>Failed to generate embedding for item: {$item_data['title']}</p>"; | |
| 535 | 533 | continue; |
| 536 | 534 | } |
| 537 | 535 | |
| 538 | 536 | // Save to DB |
| @@ -538,10 +536,9 @@ | ||
| 538 | 536 | // Save to DB |
| 539 | 537 | global $wpdb; |
| 540 | 538 | $table = $wpdb->prefix . "rag_documents"; |
| 541 | 539 | |
| 542 | - $result = $wpdb->insert( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery | |
| 543 | - $table, [ | |
| 540 | + $result = $wpdb->insert($table, [ | |
| 544 | 541 | 'title' => sanitize_text_field($item_data['title']), |
| 545 | 542 | 'content' => $clean_content, |
| 546 | 543 | 'embedding' => wp_json_encode($embedding), |
| 547 | 544 | 'source_type' => 'xaml', |
| @@ -552,11 +549,11 @@ | ||
| 552 | 549 | 'created_at' => current_time('mysql') |
| 553 | 550 | ]); |
| 554 | 551 | |
| 555 | 552 | if ($result === false) { |
| 556 | - echo "<p style='color:red;'>Database error for item " . esc_html($item_data['title']) . ": " . esc_html($wpdb->last_error) . "</p>"; | |
| 553 | + echo "<p style='color:red;'>Database error for item {$item_data['title']}: " . $wpdb->last_error . "</p>"; | |
| 557 | 554 | } else { |
| 558 | - echo "<p style='color:green;'>✓ Saved embedding for: " . esc_html($item_data['title']) . " (ID: " . esc_html($wpdb->insert_id) . ")</p>"; | |
| 555 | + echo "<p style='color:green;'>✓ Saved embedding for: {$item_data['title']} (ID: " . $wpdb->insert_id . ")</p>"; | |
| 559 | 556 | } |
| 560 | 557 | } |
| 561 | 558 | } |
| 562 | 559 | |
| @@ -580,9 +577,9 @@ | ||
| 580 | 577 | if (strlen($content) < 20) continue; |
| 581 | 578 | |
| 582 | 579 | $embedding = $this->wp_rag_create_embedding($content, $apiKey); |
| 583 | 580 | |
| 584 | - $wpdb->insert( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery | |
| 581 | + $wpdb->insert( | |
| 585 | 582 | $wpdb->prefix . "rag_documents", |
| 586 | 583 | [ |
| 587 | 584 | "title" => $p->post_title, |
| 588 | 585 | "content" => $content, |
| @@ -668,10 +665,9 @@ | ||
| 668 | 665 | |
| 669 | 666 | $table = $wpdb->prefix . "rag_documents"; |
| 670 | 667 | |
| 671 | 668 | // Check if this post already exists in the database |
| 672 | - $existing = $wpdb->get_row( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 673 | - $wpdb->prepare( | |
| 669 | + $existing = $wpdb->get_row($wpdb->prepare( | |
| 674 | 670 | "SELECT id FROM $table WHERE metadata LIKE %s AND source_type = %s", |
| 675 | 671 | '%"post_id":' . $p->ID . '%', |
| 676 | 672 | $p->post_type |
| 677 | 673 | )); |
| @@ -689,9 +685,9 @@ | ||
| 689 | 685 | ]; |
| 690 | 686 | |
| 691 | 687 | if ($existing) { |
| 692 | 688 | // Update existing record |
| 693 | - $wpdb->update( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 689 | + $wpdb->update( | |
| 694 | 690 | $table, |
| 695 | 691 | $data, |
| 696 | 692 | ['id' => $existing->id] |
| 697 | 693 | ); |
| @@ -698,9 +694,9 @@ | ||
| 698 | 694 | echo "<li style='color:blue;'>✓ Updated: " . esc_html($p->post_title) . " (" . esc_html($p->post_type) . ")</li>"; |
| 699 | 695 | $updated_count++; |
| 700 | 696 | } else { |
| 701 | 697 | // Insert new record |
| 702 | - $wpdb->insert($table, $data); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery | |
| 698 | + $wpdb->insert($table, $data); | |
| 703 | 699 | echo "<li style='color:green;'>✓ Embedded: " . esc_html($p->post_title) . " (" . esc_html($p->post_type) . ")</li>"; |
| 704 | 700 | $inserted_count++; |
| 705 | 701 | } |
| 706 | 702 | |
| @@ -715,9 +711,9 @@ | ||
| 715 | 711 | |
| 716 | 712 | // Simple Text Responses Embedding |
| 717 | 713 | if (get_option('rag_embed_str') == '1') { |
| 718 | 714 | $str_table = $wpdb->prefix . 'wpbot_response'; |
| 719 | - $str_results = $wpdb->get_results("SELECT * FROM $str_table"); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 715 | + $str_results = $wpdb->get_results("SELECT * FROM $str_table"); | |
| 720 | 716 | |
| 721 | 717 | if (!empty($str_results)) { |
| 722 | 718 | foreach ($str_results as $str) { |
| 723 | 719 | $content = "Query: " . $str->query . "\n"; |
| @@ -737,10 +733,9 @@ | ||
| 737 | 733 | continue; |
| 738 | 734 | } |
| 739 | 735 | |
| 740 | 736 | // Check if this STR already exists in the RAG database |
| 741 | - $existing = $wpdb->get_row( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 742 | - $wpdb->prepare( | |
| 737 | + $existing = $wpdb->get_row($wpdb->prepare( | |
| 743 | 738 | "SELECT id FROM $table WHERE metadata LIKE %s AND source_type = %s", |
| 744 | 739 | '%"str_id":' . $str->id . '%', |
| 745 | 740 | 'str' |
| 746 | 741 | )); |
| @@ -757,13 +752,13 @@ | ||
| 757 | 752 | "created_at" => current_time('mysql') |
| 758 | 753 | ]; |
| 759 | 754 | |
| 760 | 755 | if ($existing) { |
| 761 | - $wpdb->update($table, $data, ['id' => $existing->id]); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 756 | + $wpdb->update($table, $data, ['id' => $existing->id]); | |
| 762 | 757 | echo "<li style='color:blue;'>✓ Updated STR: " . esc_html($str->query) . "</li>"; |
| 763 | 758 | $updated_count++; |
| 764 | 759 | } else { |
| 765 | - $wpdb->insert($table, $data); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery | |
| 760 | + $wpdb->insert($table, $data); | |
| 766 | 761 | echo "<li style='color:green;'>✓ Embedded STR: " . esc_html($str->query) . "</li>"; |
| 767 | 762 | $inserted_count++; |
| 768 | 763 | } |
| 769 | 764 | |
| @@ -776,11 +771,11 @@ | ||
| 776 | 771 | echo "</ul>"; |
| 777 | 772 | echo "<h3>All Selected Sources Processed!</h3>"; |
| 778 | 773 | echo "<p><strong>Summary:</strong></p>"; |
| 779 | 774 | echo "<ul>"; |
| 780 | - echo "<li>New entries created: <strong>" . esc_html($inserted_count) . "</strong></li>"; | |
| 781 | - echo "<li>Existing entries updated: <strong style='color:blue;'>" . esc_html($updated_count) . "</strong></li>"; | |
| 782 | - echo "<li>Skipped (too short): <strong>" . esc_html($skipped_count) . "</strong></li>"; | |
| 775 | + echo "<li>New entries created: <strong>$inserted_count</strong></li>"; | |
| 776 | + echo "<li>Existing entries updated: <strong style='color:blue;'>$updated_count</strong></li>"; | |
| 777 | + echo "<li>Skipped (too short): <strong>$skipped_count</strong></li>"; | |
| 783 | 778 | echo "</ul>"; |
| 784 | 779 | } |
| 785 | 780 | public function wp_rag_create_embedding($text, $apiKey) |
| 786 | 781 | { |
| @@ -788,13 +783,9 @@ | ||
| 788 | 783 | $response = $this->generate_embedding($text); |
| 789 | 784 | return $response; |
| 790 | 785 | } |
| 791 | 786 | public function ajax_rag_manual_sync() { |
| 792 | - | |
| 793 | 787 | check_ajax_referer('wp_chatbot', 'nonce'); |
| 794 | - if ( ! current_user_can( 'manage_options' ) ) { | |
| 795 | - wp_die( 'Unauthorized access' ); | |
| 796 | - } | |
| 797 | 788 | |
| 798 | 789 | $doc_id = isset($_POST['id']) ? intval($_POST['id']) : 0; |
| 799 | 790 | if (!$doc_id) { |
| 800 | 791 | wp_send_json_error(['message' => 'Invalid document ID']); |
| @@ -801,9 +792,9 @@ | ||
| 801 | 792 | } |
| 802 | 793 | |
| 803 | 794 | global $wpdb; |
| 804 | 795 | $table = $wpdb->prefix . 'rag_documents'; |
| 805 | - $doc = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table WHERE id = %d", $doc_id)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 796 | + $doc = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table WHERE id = %d", $doc_id)); | |
| 806 | 797 | |
| 807 | 798 | if (!$doc) { |
| 808 | 799 | wp_send_json_error(['message' => 'Document not found']); |
| 809 | 800 | } |
| @@ -842,9 +833,9 @@ | ||
| 842 | 833 | global $wpdb; |
| 843 | 834 | $id = intval($_POST['id']); |
| 844 | 835 | $table_name = $wpdb->prefix . 'rag_documents'; |
| 845 | 836 | |
| 846 | - $deleted = $wpdb->delete($table_name, array('id' => $id)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 837 | + $deleted = $wpdb->delete($table_name, array('id' => $id)); | |
| 847 | 838 | |
| 848 | 839 | if ($deleted) { |
| 849 | 840 | wp_send_json_success('Document deleted successfully.'); |
| 850 | 841 | } else { |
| @@ -865,9 +856,9 @@ | ||
| 865 | 856 | $ids = array_map('intval', $_POST['ids']); |
| 866 | 857 | $table_name = $wpdb->prefix . 'rag_documents'; |
| 867 | 858 | |
| 868 | 859 | $ids_string = implode(',', $ids); |
| 869 | - $deleted = $wpdb->query("DELETE FROM $table_name WHERE id IN ($ids_string)"); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 860 | + $deleted = $wpdb->query("DELETE FROM $table_name WHERE id IN ($ids_string)"); | |
| 870 | 861 | |
| 871 | 862 | if ($deleted !== false) { |
| 872 | 863 | wp_send_json_success('Selected documents deleted successfully.'); |
| 873 | 864 | } else { |
| @@ -882,14 +873,14 @@ | ||
| 882 | 873 | |
| 883 | 874 | global $wpdb; |
| 884 | 875 | $table_name = $wpdb->prefix . 'rag_documents'; |
| 885 | 876 | |
| 886 | - $deleted = $wpdb->query("TRUNCATE TABLE $table_name"); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 877 | + $deleted = $wpdb->query("TRUNCATE TABLE $table_name"); | |
| 887 | 878 | |
| 888 | 879 | // Some DBs might not support TRUNCATE on tables with foreign keys or other constraints, |
| 889 | 880 | // though rag_documents is likely simple. Fallback to DELETE. |
| 890 | 881 | if ($deleted === false) { |
| 891 | - $deleted = $wpdb->query("DELETE FROM $table_name"); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 882 | + $deleted = $wpdb->query("DELETE FROM $table_name"); | |
| 892 | 883 | } |
| 893 | 884 | |
| 894 | 885 | if ($deleted !== false) { |
| 895 | 886 | wp_send_json_success('All documents deleted successfully.'); |
| @@ -906,9 +897,9 @@ | ||
| 906 | 897 | global $wpdb; |
| 907 | 898 | $id = intval($_POST['id']); |
| 908 | 899 | $table_name = $wpdb->prefix . 'rag_documents'; |
| 909 | 900 | |
| 910 | - $document = $wpdb->get_row($wpdb->prepare("SELECT id, title, content FROM $table_name WHERE id = %d", $id)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 901 | + $document = $wpdb->get_row($wpdb->prepare("SELECT id, title, content FROM $table_name WHERE id = %d", $id)); | |
| 911 | 902 | |
| 912 | 903 | if ($document) { |
| 913 | 904 | wp_send_json_success($document); |
| 914 | 905 | } else { |
| @@ -927,9 +918,9 @@ | ||
| 927 | 918 | $content = sanitize_textarea_field(wp_unslash($_POST['content'])); |
| 928 | 919 | $table_name = $wpdb->prefix . 'rag_documents'; |
| 929 | 920 | |
| 930 | 921 | // Re-generate embedding if content changed |
| 931 | - $old_content = $wpdb->get_var($wpdb->prepare("SELECT content FROM $table_name WHERE id = %d", $id)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 922 | + $old_content = $wpdb->get_var($wpdb->prepare("SELECT content FROM $table_name WHERE id = %d", $id)); | |
| 932 | 923 | |
| 933 | 924 | $update_data = array( |
| 934 | 925 | 'title' => $title, |
| 935 | 926 | 'content' => $content, |
| @@ -944,9 +935,9 @@ | ||
| 944 | 935 | $update_data['status'] = 'error'; |
| 945 | 936 | } |
| 946 | 937 | } |
| 947 | 938 | |
| 948 | - $updated = $wpdb->update($table_name, $update_data, array('id' => $id)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 939 | + $updated = $wpdb->update($table_name, $update_data, array('id' => $id)); | |
| 949 | 940 | |
| 950 | 941 | if ($updated !== false) { |
| 951 | 942 | wp_send_json_success('Document updated successfully.'); |
| 952 | 943 | } else { |
| @@ -994,13 +985,12 @@ | ||
| 994 | 985 | global $wpdb; |
| 995 | 986 | $table = $wpdb->prefix . "rag_documents"; |
| 996 | 987 | |
| 997 | 988 | // Check if it already exists (by source_url or custom metadata if we had it) |
| 998 | - $existing = $wpdb->get_row($wpdb->prepare("SELECT id FROM $table WHERE source_url = %s", $url)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 989 | + $existing = $wpdb->get_row($wpdb->prepare("SELECT id FROM $table WHERE source_url = %s", $url)); | |
| 999 | 990 | |
| 1000 | 991 | if ($existing) { |
| 1001 | - $result = $wpdb->update( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 1002 | - $table, [ | |
| 992 | + $result = $wpdb->update($table, [ | |
| 1003 | 993 | 'title' => sanitize_text_field($title), |
| 1004 | 994 | 'content' => $content, |
| 1005 | 995 | 'embedding' => wp_json_encode($embedding), |
| 1006 | 996 | 'status' => 'complete', |
| @@ -1006,10 +996,9 @@ | ||
| 1006 | 996 | 'status' => 'complete', |
| 1007 | 997 | 'created_at' => current_time('mysql') |
| 1008 | 998 | ], ['id' => $existing->id]); |
| 1009 | 999 | } else { |
| 1010 | - $result = $wpdb->insert( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery | |
| 1011 | - $table, [ | |
| 1000 | + $result = $wpdb->insert($table, [ | |
| 1012 | 1001 | 'title' => sanitize_text_field($title), |
| 1013 | 1002 | 'content' => $content, |
| 1014 | 1003 | 'embedding' => wp_json_encode($embedding), |
| 1015 | 1004 | 'source_type' => ($post->post_type === 'page' || $post->post_type === 'post') ? $post->post_type : 'xaml', |
| @@ -1079,9 +1068,9 @@ | ||
| 1079 | 1068 | global $wpdb; |
| 1080 | 1069 | $table = $wpdb->prefix . "rag_documents"; |
| 1081 | 1070 | |
| 1082 | 1071 | // Get all embeddings and texts |
| 1083 | - $rows = $wpdb->get_results("SELECT content, embedding FROM $table WHERE status = 'complete'", ARRAY_A); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 1072 | + $rows = $wpdb->get_results("SELECT content, embedding FROM $table WHERE status = 'complete'", ARRAY_A); | |
| 1084 | 1073 | |
| 1085 | 1074 | if (empty($rows)) { |
| 1086 | 1075 | return "No knowledge base found."; |
| 1087 | 1076 | } |
| @@ -1178,9 +1167,9 @@ | ||
| 1178 | 1167 | } |
| 1179 | 1168 | |
| 1180 | 1169 | // Simple Text Responses |
| 1181 | 1170 | if (get_option('rag_embed_str') == '1') { |
| 1182 | - $str_ids = $wpdb->get_col("SELECT id FROM {$wpdb->prefix}wpbot_response"); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 1171 | + $str_ids = $wpdb->get_col("SELECT id FROM {$wpdb->prefix}wpbot_response"); | |
| 1183 | 1172 | foreach ($str_ids as $str_id) { |
| 1184 | 1173 | $queue[] = ['id' => $str_id, 'type' => 'str']; |
| 1185 | 1174 | } |
| 1186 | 1175 | } |
| @@ -1242,10 +1231,9 @@ | ||
| 1242 | 1231 | $error_msg = is_wp_error($embedding) ? $embedding->get_error_message() : 'Failed to generate embedding'; |
| 1243 | 1232 | wp_send_json_error($error_msg); |
| 1244 | 1233 | } |
| 1245 | 1234 | |
| 1246 | - $existing = $wpdb->get_row( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 1247 | - $wpdb->prepare( | |
| 1235 | + $existing = $wpdb->get_row($wpdb->prepare( | |
| 1248 | 1236 | "SELECT id FROM $table WHERE metadata LIKE %s AND source_type = %s", |
| 1249 | 1237 | '%"post_id":' . $p->ID . '%', |
| 1250 | 1238 | $p->post_type |
| 1251 | 1239 | )); |
| @@ -1262,18 +1250,18 @@ | ||
| 1262 | 1250 | "created_at" => current_time('mysql') |
| 1263 | 1251 | ]; |
| 1264 | 1252 | |
| 1265 | 1253 | if ($existing) { |
| 1266 | - $wpdb->update($table, $data, ['id' => $existing->id]); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 1254 | + $wpdb->update($table, $data, ['id' => $existing->id]); | |
| 1267 | 1255 | wp_send_json_success(['status' => 'updated', 'title' => $title]); |
| 1268 | 1256 | } else { |
| 1269 | - $wpdb->insert($table, $data); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery | |
| 1257 | + $wpdb->insert($table, $data); | |
| 1270 | 1258 | wp_send_json_success(['status' => 'inserted', 'title' => $title]); |
| 1271 | 1259 | } |
| 1272 | 1260 | |
| 1273 | 1261 | } elseif ($type === 'str') { |
| 1274 | 1262 | global $wpdb; |
| 1275 | - $str = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}wpbot_response WHERE id = %d", $id)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 1263 | + $str = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}wpbot_response WHERE id = %d", $id)); | |
| 1276 | 1264 | if (!$str) { |
| 1277 | 1265 | wp_send_json_error('STR not found'); |
| 1278 | 1266 | } |
| 1279 | 1267 | |
| @@ -1297,10 +1285,9 @@ | ||
| 1297 | 1285 | } |
| 1298 | 1286 | |
| 1299 | 1287 | $table = $wpdb->prefix . "rag_documents"; |
| 1300 | 1288 | |
| 1301 | - $existing = $wpdb->get_row( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 1302 | - $wpdb->prepare( | |
| 1289 | + $existing = $wpdb->get_row($wpdb->prepare( | |
| 1303 | 1290 | "SELECT id FROM $table WHERE metadata LIKE %s AND source_type = %s", |
| 1304 | 1291 | '%"str_id":' . $str->id . '%', |
| 1305 | 1292 | 'str' |
| 1306 | 1293 | )); |
| @@ -1317,11 +1304,11 @@ | ||
| 1317 | 1304 | "created_at" => current_time('mysql') |
| 1318 | 1305 | ]; |
| 1319 | 1306 | |
| 1320 | 1307 | if ($existing) { |
| 1321 | - $wpdb->update($table, $data, ['id' => $existing->id]); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 1308 | + $wpdb->update($table, $data, ['id' => $existing->id]); | |
| 1322 | 1309 | } else { |
| 1323 | - $wpdb->insert($table, $data); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery | |
| 1310 | + $wpdb->insert($table, $data); | |
| 1324 | 1311 | } |
| 1325 | 1312 | wp_send_json_success(['status' => 'processed', 'title' => 'Simple Text Response ID ' . $id]); |
| 1326 | 1313 | } |
| 1327 | 1314 | |