| 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 |
$title = $post->post_title; |
| 43 |
$content = $post->post_content; |
| 44 |
$service = DataEmbedding::instance(); |
| 45 |
$embedded = $service->generate_and_store( $post_id, $title, $content ); |
| 46 |
if ( $embedded ) { |
| 47 |
update_post_meta( $post_id, '_has_embedding', 1 ); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Starts the AI embedding cron process. |
| 53 |
* |
| 54 |
* @return void |
| 55 |
*/ |
| 56 |
public function start_cron() { |
| 57 |
if ( ! isset( $_GET['action'] ) || sanitize_text_field( wp_unslash( $_GET['action'] ) ) !== 'rtsb_start_embedding_process' ) { |
| 58 |
return; |
| 59 |
} |
| 60 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 61 |
wp_die( esc_html__( 'Access denied.', 'shopbuilder' ) ); |
| 62 |
} |
| 63 |
if ( ! wp_verify_nonce( Fns::get_nonce(), rtsb()->nonceText ) ) { |
| 64 |
wp_die( esc_html__( 'Invalid request.', 'shopbuilder' ) ); |
| 65 |
} |
| 66 |
if ( ! wp_next_scheduled( 'rtsb_embedding_cron_run' ) ) { |
| 67 |
wp_schedule_single_event( time() + 2, 'rtsb_embedding_cron_run' ); |
| 68 |
} |
| 69 |
update_option( 'rtsb_embedding_in_progress', true ); |
| 70 |
update_option( |
| 71 |
'rtsb_embedding_progress', |
| 72 |
[ |
| 73 |
'processed' => 0, |
| 74 |
'total' => AIFns::need_product_embedding(), |
| 75 |
] |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
/** |
| 81 |
* @return void |
| 82 |
*/ |
| 83 |
public function process_batch() { |
| 84 |
$products = get_posts( |
| 85 |
[ |
| 86 |
'post_type' => 'product', |
| 87 |
'post_status' => 'publish', |
| 88 |
'posts_per_page' => 25, |
| 89 |
'fields' => 'ids', |
| 90 |
'meta_query' => [ // phpcs:ignore WordPress.DB.SlowDBQuery |
| 91 |
[ |
| 92 |
'key' => '_has_embedding', |
| 93 |
'compare' => 'NOT EXISTS', |
| 94 |
], |
| 95 |
], |
| 96 |
] |
| 97 |
); |
| 98 |
if ( empty( $products ) ) { |
| 99 |
delete_option( 'rtsb_embedding_in_progress' ); |
| 100 |
delete_option( 'rtsb_embedding_progress' ); |
| 101 |
update_option( 'rtsb_embedding_process_completed', time() ); |
| 102 |
return; // all done. |
| 103 |
} |
| 104 |
$service = DataEmbedding::instance(); |
| 105 |
foreach ( $products as $id ) { |
| 106 |
$title = get_the_title( $id ); |
| 107 |
$content = get_post_field( 'post_content', $id ); |
| 108 |
$embedded = $service->generate_and_store( $id, $title, $content ); |
| 109 |
if ( $embedded ) { |
| 110 |
update_post_meta( $id, '_has_embedding', 1 ); |
| 111 |
} |
| 112 |
} |
| 113 |
// Update progress. |
| 114 |
$progress = get_option( |
| 115 |
'rtsb_embedding_progress', |
| 116 |
[ |
| 117 |
'processed' => 0, |
| 118 |
'total' => 0, |
| 119 |
] |
| 120 |
); |
| 121 |
$progress['processed'] += count( $products ); |
| 122 |
update_option( 'rtsb_embedding_progress', $progress ); |
| 123 |
// Schedule the next batch immediately. |
| 124 |
wp_schedule_single_event( time() + 2, 'rtsb_embedding_cron_run' ); |
| 125 |
} |
| 126 |
} |
| 127 |
|