PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 3.2.3
ShopBuilder – WooCommerce Builder For Elementor v3.2.3
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 2.3.0 All 62 releases
shopbuilder / app / AI / AiController.php

AiController.php in ShopBuilder – WooCommerce Builder For Elementor 3.2.3, at app/AI/AiController.php

134 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * AI Initialization Class.
4 *
5 * Handles AI-related module setup, including meta boxes, assets, and AJAX actions.
6 *
7 * @package RadiusTheme\SB
8 */
9
10 namespace RadiusTheme\SB\AI;
11
12 use RadiusTheme\SB\AI\Embedding\DataEmbedding;
13 use RadiusTheme\SB\Helpers\Fns;
14 use RadiusTheme\SB\Traits\SingletonTrait;
15
16 defined( 'ABSPATH' ) || exit();
17
18 /**
19 * Initializes AI module components.
20 */
21 class AiController {
22 /**
23 * Use Singleton trait.
24 */
25 use SingletonTrait;
26
27 /**
28 * Handle product save event and generate AI embeddings for product data.
29 *
30 * Triggers when a product is saved and published, then sends the title
31 * and content to the DataEmbedding service for embedding generation and storage.
32 *
33 * @param int $post_id The ID of the saved product.
34 * @param \WP_Post $post The post object of the saved product.
35 *
36 * @return void
37 */
38 public function handle_product_save( $post_id, $post ) {
39 if ( 'publish' !== $post->post_status ) {
40 return;
41 }
42 $ai_data = AIFns::activated_ai_data();
43
44 if ( empty( $ai_data['api_key'] ) || empty( $ai_data['client'] ) ) {
45 return;
46 }
47 $title = $post->post_title;
48 $content = $post->post_content;
49 $service = DataEmbedding::instance();
50 $embedded = $service->generate_and_store( $post_id, $title, $content );
51 if ( $embedded ) {
52 update_post_meta( $post_id, '_has_embedding', 1 );
53 }
54 }
55
56 /**
57 * Starts the AI embedding cron process.
58 *
59 * @return void
60 */
61 public function start_cron() {
62 if ( ! isset( $_GET['action'] ) || sanitize_text_field( wp_unslash( $_GET['action'] ) ) !== 'rtsb_start_embedding_process' ) {
63 return;
64 }
65 if ( ! current_user_can( 'manage_options' ) ) {
66 wp_die( esc_html__( 'Access denied.', 'shopbuilder' ) );
67 }
68 if ( ! wp_verify_nonce( Fns::get_nonce(), rtsb()->nonceText ) ) {
69 wp_die( esc_html__( 'Invalid request.', 'shopbuilder' ) );
70 }
71 if ( ! wp_next_scheduled( 'rtsb_embedding_cron_run' ) ) {
72 wp_schedule_single_event( time() + 2, 'rtsb_embedding_cron_run' );
73 }
74 update_option( 'rtsb_embedding_in_progress', true );
75 update_option(
76 'rtsb_embedding_progress',
77 [
78 'processed' => 0,
79 ]
80 );
81 }
82
83
84 /**
85 * @return void
86 */
87 public function process_batch() {
88 $ai_data = AIFns::activated_ai_data();
89 if ( empty( $ai_data['api_key'] ) ) {
90 return;
91 }
92 $products = get_posts(
93 [
94 'post_type' => 'product',
95 'post_status' => 'publish',
96 'posts_per_page' => 25,
97 'fields' => 'ids',
98 'meta_query' => [ // phpcs:ignore WordPress.DB.SlowDBQuery
99 [
100 'key' => '_has_embedding',
101 'compare' => 'NOT EXISTS',
102 ],
103 ],
104 ]
105 );
106 if ( empty( $products ) ) {
107 delete_option( 'rtsb_embedding_in_progress' );
108 delete_option( 'rtsb_embedding_progress' );
109 update_option( 'rtsb_embedding_process_completed', time() );
110 return; // all done.
111 }
112 $service = DataEmbedding::instance();
113 foreach ( $products as $id ) {
114 $title = get_the_title( $id );
115 $content = get_post_field( 'post_content', $id );
116 $embedded = $service->generate_and_store( $id, $title, $content );
117 if ( $embedded ) {
118 update_post_meta( $id, '_has_embedding', 1 );
119 }
120 }
121 // Update progress.
122 $progress = get_option(
123 'rtsb_embedding_progress',
124 [
125 'processed' => 0,
126 ]
127 );
128 $progress['processed'] += count( $products );
129 update_option( 'rtsb_embedding_progress', $progress );
130 // Schedule the next batch immediately.
131 wp_schedule_single_event( time() + 2, 'rtsb_embedding_cron_run' );
132 }
133 }
134