PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 3.2.6
ShopBuilder – WooCommerce Builder For Elementor v3.2.6
3.4.2 3.4.1 3.4.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 All 63 releases
shopbuilder / app / AI / DB / AIDB.php

AIDB.php in ShopBuilder – WooCommerce Builder For Elementor 3.2.6, at app/AI/DB/AIDB.php

114 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * AI Database Management Class.
4 *
5 * Handles database operations for AI-related embeddings,
6 * including fetching and upserting (insert or update) data in the embeddings table.
7 *
8 * @package RadiusTheme\SB
9 */
10
11 namespace RadiusTheme\SB\AI\DB;
12
13 use RadiusTheme\SB\AI\AIFns;
14 use RadiusTheme\SB\Helpers\Fns;
15 use RadiusTheme\SB\Traits\SingletonTrait;
16
17 defined( 'ABSPATH' ) || exit();
18
19 /**
20 * Class AIDB
21 *
22 * Provides methods to interact with the AI embeddings database table.
23 */
24 class AIDB {
25 /**
26 * Use Singleton trait to ensure a single instance of this class.
27 */
28 use SingletonTrait;
29
30 /**
31 * Retrieve all AI embeddings from the database.
32 *
33 * @since 1.0.0
34 *
35 * @return array List of all embedding records with properly unserialized data.
36 */
37 public static function get_all() {
38 $results = Fns::DB()::select( '*' )
39 ->from( AIFns::$ai_embeddings_table )
40 ->get();
41 if ( empty( $results ) ) {
42 return [];
43 }
44 // Process results to ensure proper data types.
45 $processed = [];
46 foreach ( $results as $row ) {
47 $row_array = (array) $row;
48 // Unserialize embedding and convert to floats.
49 if ( isset( $row_array['embedding'] ) ) {
50 $embedding = maybe_unserialize( $row_array['embedding'] );
51 if ( is_array( $embedding ) ) {
52 // Critical: Ensure all values are floats for cosine similarity.
53 $row_array['embedding'] = array_map( 'floatval', $embedding );
54 } else {
55 continue; // Skip invalid embeddings.
56 }
57 }
58 // Unserialize info data.
59 if ( isset( $row_array['info'] ) ) {
60 $row_array['info'] = maybe_unserialize( $row_array['info'] );
61 }
62 $processed[] = $row_array;
63 }
64 return $processed;
65 }
66
67 /**
68 * Insert or update embedding data for a specific product.
69 *
70 * Checks if a record already exists for the given product ID.
71 * If it exists, the record is updated; otherwise, a new record is inserted.
72 *
73 * @since 1.0.0
74 *
75 * @param int $product_id Product ID to associate with the embedding.
76 * @param string $title Product title or reference title for the embedding.
77 * @param array $embedding Embedding vector data (array of floats).
78 * @param array $info Additional metadata or information related to the embedding.
79 *
80 * @return bool True on successful insert or update, false on failure.
81 */
82 public static function upsert_embeding( $product_id, $title, $embedding, $info ) {
83 // Validate inputs.
84 if ( ! is_array( $embedding ) || empty( $embedding ) ) {
85 return false;
86 }
87 // Critical: Ensure all embedding values are floats before serialization.
88 $embedding = array_map( 'floatval', $embedding );
89 $exists = Fns::DB()::select( 'id' )
90 ->from( AIFns::$ai_embeddings_table )
91 ->where( 'product_id', '=', absint( $product_id ) )
92 ->get();
93 $data = [
94 'title' => sanitize_text_field( $title ),
95 'embedding' => maybe_serialize( $embedding ),
96 'info' => maybe_serialize( $info ),
97 ];
98 if ( ! empty( $exists[0] ) ) {
99 $data['updated_at'] = current_time( 'mysql' );
100 // Update existing record.
101 Fns::DB()::update( AIFns::$ai_embeddings_table, $data )
102 ->where( 'product_id', '=', absint( $product_id ) )
103 ->execute();
104 return true;
105 } else {
106 // Insert new record.
107 $data['product_id'] = absint( $product_id );
108 $data['created_at'] = current_time( 'mysql' );
109 Fns::DB()::insert( AIFns::$ai_embeddings_table, [ $data ] )->execute();
110 return true;
111 }
112 }
113 }
114