*/
use Recombee\RecommApi\Client;
use Recombee\RecommApi\Requests as Reqs;
use Recombee\RecommApi\Exceptions as Ex;
class AIPERS_AIPersonalize360 {
/**
* The loader that's responsible for maintaining and registering all hooks that power
* the plugin.
*
* @since 1.0.0
* @access protected
* @var AIPersonalize360_Loader $loader Maintains and registers all hooks for the plugin.
*/
protected $loader;
/**
* The unique identifier of this plugin.
*
* @since 1.0.0
* @access protected
* @var string $plugin_name The string used to uniquely identify this plugin.
*/
protected $plugin_name;
/**
* The current version of the plugin.
*
* @since 1.0.0
* @access protected
* @var string $version The current version of the plugin.
*/
protected $version;
/**
* Define the core functionality of the plugin.
*
* Set the plugin name and the plugin version that can be used throughout the plugin.
* Load the dependencies, define the locale, and set the hooks for the admin area and
* the public-facing side of the site.
*
* @since 1.0.0
*/
public function __construct() {
if ( defined( 'AIPERS_VERSION' ) ) {
$this->version = AIPERS_VERSION;
} else {
$this->version = '1.0.0';
}
$this->plugin_name = 'aipersonalize360';
$this->aipers_load_dependencies();
$this->aipers_define_admin_hooks();
$this->aipers_define_public_hooks();
add_action( 'aipers_import_items', array($this, 'aipers_import_items_data') );
add_action( 'aipers_import_users', array($this, 'aipers_import_users_data') );
add_action( 'wp_footer', array($this, 'aipers_log_view_events') );
add_action(
'woocommerce_add_to_cart',
array($this, 'aipers_log_add_to_cart_events'),
10,
3
);
add_action(
'woocommerce_new_order',
array($this, 'aipers_log_product_buy_events'),
10,
1
);
add_shortcode( 'aipers_product_recommendations', array($this, 'aipers_get_recommend_items_to_user') );
add_shortcode( 'aipers_related_product_recommendations', array($this, 'aipers_get_recommend_ItemsToItem') );
add_action(
'user_register',
array($this, 'aipers_registration_meta_save'),
10,
1
);
add_action(
'transition_post_status',
array($this, 'aipers_create_recom_post'),
10,
3
);
$aipers_ai_engine = get_option( 'aipers_ai_engine' );
if ( empty( $aipers_ai_engine ) ) {
return false;
}
if ( $aipers_ai_engine == 'recombee' ) {
$aipers_recombee_db_name = get_option( 'aipers_recombee_db_name' );
$aipers_recombee_private_token = get_option( 'aipers_recombee_private_token' );
$aipers_recombee_region = get_option( 'aipers_recombee_region' );
if ( empty( $aipers_recombee_db_name ) || empty( $aipers_recombee_private_token ) || empty( $aipers_recombee_region ) ) {
return false;
}
$client = new Client($aipers_recombee_db_name, $aipers_recombee_private_token, [
'region' => $aipers_recombee_region,
]);
$result = $client->send( new Reqs\ListItemProperties() );
if ( empty( $result ) ) {
$client->send( new Reqs\AddItemProperty('title', 'string') );
$client->send( new Reqs\AddItemProperty('description', 'string') );
$client->send( new Reqs\AddItemProperty('image', 'image') );
$client->send( new Reqs\AddItemProperty('available', 'boolean') );
$client->send( new Reqs\AddItemProperty('categories', 'set') );
$client->send( new Reqs\AddItemProperty('tags', 'set') );
$client->send( new Reqs\AddItemProperty('price', 'double') );
$client->send( new Reqs\AddItemProperty('url', 'string') );
$client->send( new Reqs\AddItemProperty('brand', 'string') );
$client->send( new Reqs\AddItemProperty('on sale', 'boolean') );
$client->send( new Reqs\AddItemProperty('deleted', 'boolean') );
}
}
}
/**
* Retrieves the user's IP address from the server variables.
*
* This method checks various server variables to determine the user's IP address:
* - 'HTTP_CLIENT_IP': IP from shared internet.
* - 'HTTP_X_FORWARDED_FOR': IP passed from a proxy.
* - 'REMOTE_ADDR': Default remote address.
*
* @return string The detected IP address of the user.
*/
public function aipers_getUserIpAddr() {
if ( !empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
//ip from share internet
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif ( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
//ip pass from proxy
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
/**
* Handles the creation of a recommendation post when a product is published.
*
* This method is triggered when a post status changes to 'publish' and the post type is 'product'.
* It integrates with the Recombee AI engine to add the product as an item in the Recombee database,
* and sets various product attributes such as title, description, image, availability, categories, tags, price, and URL.
*
* Steps performed:
* - Checks for autosave and valid post status transition.
* - Verifies supported post types and required Recombee configuration options.
* - Initializes Recombee client and checks if the product already exists in the Recombee database.
* - If not, adds the product and sets its attributes based on WooCommerce product data.
* - Handles both simple and variable/grouped products, extracting relevant details.
*
* @param string $new The new post status.
* @param string $old The old post status.
* @param WP_Post $post The post object being saved.
*
* @return void|false Returns false if required conditions are not met, otherwise void.
*/
public function aipers_create_recom_post( $new, $old, $post ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( $new == 'publish' && $old != 'publish' ) {
$supported_post_types = array('product');
if ( !in_array( $post->post_type, $supported_post_types ) ) {
return;
}
$aipers_ai_engine = get_option( 'aipers_ai_engine' );
if ( empty( $aipers_ai_engine ) ) {
return false;
}
if ( $aipers_ai_engine == 'recombee' ) {
$aipers_recombee_db_name = get_option( 'aipers_recombee_db_name' );
$aipers_recombee_private_token = get_option( 'aipers_recombee_private_token' );
$aipers_recombee_region = get_option( 'aipers_recombee_region' );
if ( empty( $aipers_recombee_db_name ) || empty( $aipers_recombee_private_token ) || empty( $aipers_recombee_region ) ) {
return false;
}
$client = new Client($aipers_recombee_db_name, $aipers_recombee_private_token, [
'region' => $aipers_recombee_region,
]);
$item_id = $post->ID;
$item_type = get_post_type( $item_id );
if ( $item_type && $item_type == 'product' ) {
$item_list = $client->send( new Reqs\ListItems() );
$item_list = ( $item_list ? $item_list : array() );
if ( !in_array( $item_id, $item_list ) ) {
$r = new Reqs\AddItem($item_id);
$r->setTimeout( 5000 );
// 5 seconds
$client->send( $r );
$product = wc_get_product( $item_id );
$available = false;
if ( $product->is_type( 'simple' ) ) {
if ( $product->is_in_stock() ) {
$available = true;
}
$tags_array = [];
$terms_array = [];
$url = get_permalink( $product->get_id() );
$on_sale = false;
if ( $product->is_on_sale() ) {
$price = $_POST['_sale_price'];
$on_sale = true;
} else {
$price = $_POST['_regular_price'];
}
$terms = get_the_terms( $item_id, 'product_cat' );
if ( !empty( $terms ) ) {
foreach ( $terms as $term ) {
$terms_array[] = $term->name;
}
}
$tags = get_the_terms( $item_id, 'product_tag' );
if ( !empty( $tags ) ) {
foreach ( $tags as $tag ) {
$tags_array[] = $tag->name;
}
}
$_description = $product->get_description();
$image_url = wp_get_attachment_url( $product->get_image_id() );
$image_url = ( $image_url ? $image_url : wc_placeholder_img_src() );
$title = $product->get_name();
} else {
$available = true;
$_description = $product->get_description();
$title = $product->get_name();
$url = get_permalink( $product->get_id() );
$tags_array = [];
$terms_array = [];
$terms = get_the_terms( $item_id, 'product_cat' );
if ( !empty( $terms ) ) {
foreach ( $terms as $term ) {
$terms_array[] = $term->name;
}
}
$tags = get_the_terms( $item_id, 'product_tag' );
if ( !empty( $tags ) ) {
foreach ( $tags as $tag ) {
$tags_array[] = $tag->name;
}
}
if ( $product->get_type() == 'grouped' ) {
$children = $product->get_children();
$price = 0;
foreach ( $children as $key => $value ) {
$_product = wc_get_product( $value );
$price = $_product->get_price();
$image_url = wp_get_attachment_url( $product->get_image_id() );
}
} else {
$product = new WC_Product_Variable($item_id);
$variations = $product->get_available_variations();
foreach ( $variations as $variation ) {
if ( $variation['image']['thumb_src'] ) {
$image_url = $variation['image']['thumb_src'];
}
if ( $variation['display_regular_price'] ) {
$price = $variation['display_regular_price'];
}
}
}
}
$values = [
'title' => $title,
'description' => $_description,
'image' => $image_url,
'available' => $available,
'categories' => $terms_array,
'tags' => $tags_array,
'price' => $price,
'url' => $url,
];
$request = new Reqs\SetItemValues($item_id, $values, [
'cascadeCreate' => true,
]);
$request->setTimeout( 5000 );
// 5 seconds
$client->send( $request );
}
}
}
}
}
/**
* Saves registration meta data for a user based on the selected AI engine.
*
* This method checks the configured AI engine and, if set to 'recombee', attempts to add the user
* to the Recombee database if they do not already exist. It retrieves necessary Recombee configuration
* options and ensures they are set before proceeding. If any required option is missing, or if the AI engine
* is not set, the function returns false.
*
* @param int $user_id The ID of the user whose registration meta is being saved.
* @return bool Returns false if the AI engine or required Recombee options are not set; otherwise, void.
*/
public function aipers_registration_meta_save( $user_id ) {
$aipers_ai_engine = get_option( 'aipers_ai_engine' );
if ( empty( $aipers_ai_engine ) ) {
return false;
}
if ( $aipers_ai_engine == 'recombee' ) {
$aipers_recombee_db_name = get_option( 'aipers_recombee_db_name' );
$aipers_recombee_private_token = get_option( 'aipers_recombee_private_token' );
$aipers_recombee_region = get_option( 'aipers_recombee_region' );
if ( empty( $aipers_recombee_db_name ) || empty( $aipers_recombee_private_token ) || empty( $aipers_recombee_region ) ) {
return false;
}
$client = new Client($aipers_recombee_db_name, $aipers_recombee_private_token, [
'region' => $aipers_recombee_region,
]);
$all_users = $client->send( new Reqs\ListUsers() );
$all_users = ( $all_users ? $all_users : array() );
if ( !in_array( $user_id, $all_users ) ) {
$request = new Reqs\AddUser($user_id);
$request->setTimeout( 5000 );
// 5 seconds
$client->send( $request );
}
}
}
/**
* Generates and returns a list of recommended products related to the current product.
*
* This function is intended to be used on single product pages (not in admin).
* It uses the configured AI engine (currently supports 'recombee') to fetch recommendations
* based on the current product and user (logged-in or guest). If recommendations are available,
* it renders them in a WooCommerce-compatible product list. If not, or if the AI engine is not configured,
* it falls back to default recommendations.
*
* @return string HTML markup for the recommended products section.
*/
public function aipers_get_recommend_ItemsToItem( $atts ) {
$aipers_recommendations_title = apply_filters( 'aipers_recommendations_title', __( 'Recommendation related products', 'aipersonalize360' ) );
$aipers_recommendations_description = apply_filters( 'aipers_recommendations_description', __( 'Recommends a set of items that are somehow related to one given item, X.', 'aipersonalize360' ) );
$shortcode_args = shortcode_atts( array(
'title' => ( $aipers_recommendations_title ? $aipers_recommendations_title : '' ),
'description' => ( $aipers_recommendations_description ? $aipers_recommendations_description : '' ),
), $atts );
if ( !is_admin() && is_product() ) {
global $product;
$item_id = $product->get_id();
$target_user_id = ( is_user_logged_in() ? get_current_user_id() : 0 );
if ( $target_user_id == 0 ) {
global $wp_session;
if ( !empty( $wp_session['unregister_user_id'] ) ) {
$target_user_id = $wp_session['unregister_user_id'];
}
}
if ( $target_user_id > 0 ) {
$aipers_ai_engine = get_option( 'aipers_ai_engine' );
//if(empty($aipers_ai_engine)){
//return false;
//}
if ( $aipers_ai_engine == 'recombee' ) {
$aipers_show_num_of_items = get_option( 'aipers_show_num_of_items' );
$count = ( $aipers_show_num_of_items ? (int) $aipers_show_num_of_items : 5 );
$aipers_recombee_db_name = get_option( 'aipers_recombee_db_name' );
$aipers_recombee_private_token = get_option( 'aipers_recombee_private_token' );
$aipers_recombee_region = get_option( 'aipers_recombee_region' );
if ( empty( $aipers_recombee_db_name ) || empty( $aipers_recombee_private_token ) || empty( $aipers_recombee_region ) ) {
return false;
}
$client = new Client($aipers_recombee_db_name, $aipers_recombee_private_token, [
'region' => $aipers_recombee_region,
]);
$result = $client->send( new Reqs\RecommendItemsToItem($item_id, $target_user_id, $count) );
if ( !empty( $result['recomms'] ) ) {
ob_start();
?>
aipers_load_default_recommendations( $shortcode_args );
return ob_get_clean();
}
} else {
ob_start();
echo $this->aipers_load_default_recommendations( $shortcode_args );
return ob_get_clean();
}
} else {
ob_start();
echo $this->aipers_load_default_recommendations( $shortcode_args );
return ob_get_clean();
}
}
}
/**
* Retrieves recommended items for the current user using the selected AI engine.
*
* This function checks if the user is logged in or has a session-based user ID.
* If a valid user ID is found, it attempts to fetch personalized product recommendations
* using the configured AI engine (currently supports 'recombee'). If recommendations are
* available, it renders them as a WooCommerce product grid. If not, or if configuration
* is missing, it falls back to default recommendations.
*
* @return string HTML content for the recommended items section.
*/
public function aipers_get_recommend_items_to_user( $atts ) {
$aipers_recommendations_title = get_option( 'aipers_recommendations_title' );
$aipers_recommendations_description = get_option( 'aipers_recommendations_description' );
$shortcode_args = shortcode_atts( array(
'title' => ( $aipers_recommendations_title ? $aipers_recommendations_title : '' ),
'description' => ( $aipers_recommendations_description ? $aipers_recommendations_description : '' ),
), $atts );
$user_id = ( is_user_logged_in() ? get_current_user_id() : 0 );
if ( $user_id == 0 ) {
global $wp_session;
if ( !empty( $wp_session['unregister_user_id'] ) ) {
$user_id = $wp_session['unregister_user_id'];
}
}
if ( $user_id > 0 ) {
$aipers_ai_engine = get_option( 'aipers_ai_engine' );
//if(empty($aipers_ai_engine)){
//return false;
//}
if ( $aipers_ai_engine == 'recombee' ) {
$aipers_recombee_db_name = get_option( 'aipers_recombee_db_name' );
$aipers_recombee_private_token = get_option( 'aipers_recombee_private_token' );
$aipers_show_num_of_items = get_option( 'aipers_show_num_of_items' );
$aipers_show_num_of_items = ( $aipers_show_num_of_items ? (int) $aipers_show_num_of_items : 5 );
$aipers_recombee_region = get_option( 'aipers_recombee_region' );
if ( empty( $aipers_recombee_db_name ) || empty( $aipers_recombee_private_token ) || empty( $aipers_recombee_region ) ) {
return false;
}
$client = new Client($aipers_recombee_db_name, $aipers_recombee_private_token, [
'region' => $aipers_recombee_region,
]);
$result = $client->send( new Reqs\RecommendItemsToUser($user_id, $aipers_show_num_of_items) );
if ( !empty( $result['recomms'] ) ) {
ob_start();
?>
aipers_load_default_recommendations( $shortcode_args );
return ob_get_clean();
}
} else {
ob_start();
echo $this->aipers_load_default_recommendations( $shortcode_args );
return ob_get_clean();
}
} else {
ob_start();
echo $this->aipers_load_default_recommendations( $shortcode_args );
return ob_get_clean();
}
}
/**
* Loads and displays default product recommendations based on various strategies.
*
* This method retrieves recommendation settings from WordPress options, determines the current product context,
* and selects a recommendation strategy at random from average rating, total sales, or on-sale items.
* It then queries WooCommerce products according to the chosen strategy, excluding the current product if applicable.
* The recommended products are rendered in a section with a title, description, and a grid layout.
* Each product displays its image, name, price, sale status, and appropriate action buttons (add to cart or select options).
*
* @return string HTML markup for the recommendations section, or an empty string if no products are found.
*/
public function aipers_load_default_recommendations( $shortcode_args ) {
$aipers_show_num_of_items = get_option( 'aipers_show_num_of_items' );
$aipers_show_num_of_items = ( $aipers_show_num_of_items ? (int) $aipers_show_num_of_items : 5 );
if ( !is_admin() && is_product() ) {
global $product, $woocommerce;
$current_product_id = $product->get_id();
} else {
$current_product_id = 0;
}
$recommendation_strategies = array('_wc_average_rating', 'total_sales', 'on_sale_items');
shuffle( $recommendation_strategies );
if ( $recommendation_strategies[0] == 'on_sale_items' && count( wc_get_product_ids_on_sale() ) > 0 ) {
$product_ids_on_sale = wc_get_product_ids_on_sale();
if ( !empty( $product_ids_on_sale ) ) {
shuffle( $product_ids_on_sale );
$product_ids_on_sale_number = count( $product_ids_on_sale );
if ( $product_ids_on_sale_number > $aipers_show_num_of_items ) {
$product_ids_on_sale = array_slice( $product_ids_on_sale, 0, $aipers_show_num_of_items );
}
$recommended_products = new WP_Query(array(
'post_type' => 'product',
'post__in' => $product_ids_on_sale,
'post_status' => 'publish',
));
}
} else {
$recommended_products = new WP_Query(array(
'post_type' => 'product',
'post__not_in' => array($current_product_id),
'meta_key' => $recommendation_strategies[0],
'post_status' => 'publish',
'posts_per_page' => $aipers_show_num_of_items,
'orderby' => 'meta_value_num',
'meta_type' => 'NUMERIC',
'order' => 'DESC',
));
}
if ( !empty( $recommended_products->posts ) ) {
ob_start();
?>
posts as $recommendationItem ) {
$recommendationItem_id = $recommendationItem->ID;
$recommendationItem_type = get_post_type( $recommendationItem_id );
if ( $recommendationItem_type && $recommendationItem_type == 'product' ) {
$product = wc_get_product( $recommendationItem_id );
?>
- >
get_image() );
?>
get_name() );
?>
is_on_sale() ) {
?>
get_price_html() );
?>
is_type( 'simple' ) ) {
?>
$aipers_recombee_region,
]);
if ( $user_id > 0 && $order_id ) {
$order = wc_get_order( $order_id );
$time = time();
$requests = array();
foreach ( $order->get_items() as $items => $order_item ) {
$item_id = $order_item->get_product_id();
$quantity = $order_item->get_quantity();
$price = get_post_meta( $item_id, '_price', true );
$price = $price * $quantity;
$r = new Reqs\AddPurchase($user_id, $item_id, [
'timestamp' => $time,
'cascadeCreate' => true,
'amount' => $quantity,
'price' => $price,
]);
array_push( $requests, $r );
}
$result = $client->send( new Reqs\Batch($requests) );
} else {
}
}
}
/**
* Log add to cart action
*
* @since 1.0.0
*/
public function aipers_log_add_to_cart_events( $cart_id, $product_id, $quantity ) {
$aipers_ai_engine = get_option( 'aipers_ai_engine' );
if ( empty( $aipers_ai_engine ) ) {
return false;
}
if ( $aipers_ai_engine == 'recombee' ) {
$aipers_recombee_db_name = get_option( 'aipers_recombee_db_name' );
$aipers_recombee_private_token = get_option( 'aipers_recombee_private_token' );
$aipers_show_num_of_items = get_option( 'aipers_show_num_of_items' );
$aipers_show_num_of_items = ( $aipers_show_num_of_items ? (int) $aipers_show_num_of_items : 5 );
$aipers_recombee_region = get_option( 'aipers_recombee_region' );
if ( empty( $aipers_recombee_db_name ) || empty( $aipers_recombee_private_token ) || empty( $aipers_recombee_region ) ) {
return false;
}
$client = new Client($aipers_recombee_db_name, $aipers_recombee_private_token, [
'region' => $aipers_recombee_region,
]);
$user_id = ( is_user_logged_in() ? get_current_user_id() : 0 );
if ( $user_id > 0 && $product_id ) {
$item_id = $product_id;
$time = time();
$price = get_post_meta( $item_id, '_price', true );
$price = $price * $quantity;
$request = new Reqs\AddCartAddition($user_id, $item_id, [
'timestamp' => $time,
'cascadeCreate' => true,
'amount' => $quantity,
'price' => $price,
]);
$request->setTimeout( 5000 );
// 5 seconds
$client->send( $request );
} else {
}
}
}
/**
* Log view events
*
* @since 1.0.0
*/
public function aipers_log_view_events() {
if ( !is_404() ) {
global $wp, $wp_query;
$url = add_query_arg( array(), $wp->request );
if ( !$this->aipers_bot_detected() ) {
$this->pdwchs_add_view_events( ( is_user_logged_in() ? get_current_user_id() : 0 ), $url, $wp_query->queried_object );
}
}
}
/**
* Detect bot.
*
* @return boolean
*/
public function aipers_bot_detected() {
if ( isset( $_SERVER['HTTP_USER_AGENT'] ) && preg_match( '/bot|crawl|slurp|spider/i', $_SERVER['HTTP_USER_AGENT'] ) ) {
return true;
} else {
return false;
}
}
/**
* Insert User Events
*
* @param $user_id
* @param $url
*/
public function pdwchs_add_view_events( $user_id, $url, $queried_object ) {
$aipers_ai_engine = get_option( 'aipers_ai_engine' );
if ( empty( $aipers_ai_engine ) ) {
return false;
}
if ( $aipers_ai_engine == 'recombee' ) {
$aipers_recombee_db_name = get_option( 'aipers_recombee_db_name' );
$aipers_recombee_private_token = get_option( 'aipers_recombee_private_token' );
$aipers_show_num_of_items = get_option( 'aipers_show_num_of_items' );
$aipers_show_num_of_items = ( $aipers_show_num_of_items ? (int) $aipers_show_num_of_items : 5 );
$aipers_recombee_region = get_option( 'aipers_recombee_region' );
if ( empty( $aipers_recombee_db_name ) || empty( $aipers_recombee_private_token ) || empty( $aipers_recombee_region ) ) {
return false;
}
$client = new Client($aipers_recombee_db_name, $aipers_recombee_private_token, [
'region' => $aipers_recombee_region,
]);
if ( $user_id > 0 && $url != '' ) {
if ( !empty( $queried_object->ID ) ) {
$item_type = $queried_object->post_type;
if ( $item_type !== 'product' ) {
return;
}
$item_id = $queried_object->ID;
$time = time();
$request = new Reqs\AddDetailView($user_id, $item_id, [
'timestamp' => $time,
'cascadeCreate' => true,
]);
$request->setTimeout( 5000 );
// 5 seconds
$client->send( $request );
}
} else {
}
}
}
/**
* Imports WordPress users into the Recombee AI engine.
*
* This method retrieves all site users and synchronizes them with the Recombee database.
* It checks for necessary Recombee configuration options and initializes the Recombee client.
* The method compares the list of WordPress user IDs with those in Recombee, and adds any missing users.
*
* @return bool|void Returns false if required options are missing or if no users are found; otherwise, performs the import.
*/
public function aipers_import_users_data() {
$siteusers = get_users();
if ( !empty( $siteusers ) ) {
$aipers_ai_engine = get_option( 'aipers_ai_engine' );
if ( empty( $aipers_ai_engine ) ) {
return false;
}
if ( $aipers_ai_engine == 'recombee' ) {
$aipers_recombee_db_name = get_option( 'aipers_recombee_db_name' );
$aipers_recombee_private_token = get_option( 'aipers_recombee_private_token' );
$aipers_show_num_of_items = get_option( 'aipers_show_num_of_items' );
$aipers_show_num_of_items = ( $aipers_show_num_of_items ? (int) $aipers_show_num_of_items : 5 );
$aipers_recombee_region = get_option( 'aipers_recombee_region' );
if ( empty( $aipers_recombee_db_name ) || empty( $aipers_recombee_private_token ) || empty( $aipers_recombee_region ) ) {
return false;
}
$client = new Client($aipers_recombee_db_name, $aipers_recombee_private_token, [
'region' => $aipers_recombee_region,
]);
$ListUsers = $client->send( new Reqs\ListUsers() );
$user_ids = wp_list_pluck( $siteusers, 'ID' );
$output = array_merge( array_diff( $user_ids, $ListUsers ), array_diff( $ListUsers, $user_ids ) );
$requests = array();
foreach ( $output as $user_id ) {
$r = new Reqs\AddUser($user_id, [
'cascadeCreate' => true,
]);
array_push( $requests, $r );
}
$result = $client->send( new Reqs\Batch($requests) );
}
}
}
/**
* Imports WooCommerce product data into Recombee if the selected AI engine is 'recombee'.
*
* This method retrieves all published WooCommerce products and checks if each product already exists in Recombee.
* For products not yet imported, it gathers relevant product information such as title, description, image, availability,
* categories, tags, price, URL, and brand, then sends a batch request to Recombee to create or update these items.
*
* Steps:
* 1. Checks if the AI engine is set to 'recombee' and required Recombee credentials are available.
* 2. Initializes the Recombee client.
* 3. Retrieves the list of items already present in Recombee.
* 4. Queries all published WooCommerce products.
* 5. For each product not in Recombee, collects product data and prepares a Recombee SetItemValues request.
* 6. Sends all prepared requests in a batch to Recombee.
*
* @return bool Returns false if required options are missing or if the AI engine is not 'recombee'.
*/
public function aipers_import_items_data() {
$aipers_ai_engine = get_option( 'aipers_ai_engine' );
if ( empty( $aipers_ai_engine ) ) {
return false;
}
if ( $aipers_ai_engine == 'recombee' ) {
$aipers_recombee_db_name = get_option( 'aipers_recombee_db_name' );
$aipers_recombee_private_token = get_option( 'aipers_recombee_private_token' );
$aipers_show_num_of_items = get_option( 'aipers_show_num_of_items' );
$aipers_show_num_of_items = ( $aipers_show_num_of_items ? (int) $aipers_show_num_of_items : 5 );
$aipers_recombee_region = get_option( 'aipers_recombee_region' );
if ( empty( $aipers_recombee_db_name ) || empty( $aipers_recombee_private_token ) || empty( $aipers_recombee_region ) ) {
return false;
}
$client = new Client($aipers_recombee_db_name, $aipers_recombee_private_token, [
'region' => $aipers_recombee_region,
]);
$listItems = $client->send( new Reqs\ListItems() );
$listItems = ( $listItems ? $listItems : array() );
$requests = array();
$all_products = new WP_Query(array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
));
if ( !empty( $all_products->posts ) ) {
foreach ( $all_products->posts as $item ) {
$item_id = $item->ID;
if ( in_array( $item_id, $listItems ) ) {
continue;
}
$item_type = get_post_type( $item_id );
if ( $item_type && $item_type == 'product' ) {
$product = wc_get_product( $item_id );
$available = false;
if ( $product->is_type( 'simple' ) ) {
if ( $product->is_in_stock() ) {
$available = true;
}
$tags_array = [];
$terms_array = [];
$url = get_permalink( $product->get_id() );
$on_sale = false;
if ( $product->is_on_sale() ) {
$price = $product->get_sale_price();
$on_sale = true;
} else {
$price = $product->get_regular_price();
}
$terms = get_the_terms( $item_id, 'product_cat' );
if ( !empty( $terms ) ) {
foreach ( $terms as $term ) {
$terms_array[] = $term->name;
}
}
$tags = get_the_terms( $item_id, 'product_tag' );
if ( !empty( $tags ) ) {
foreach ( $tags as $tag ) {
$tags_array[] = $tag->name;
}
}
$_description = $product->get_description();
$image_url = wp_get_attachment_url( $product->get_image_id() );
$image_url = ( $image_url ? $image_url : wc_placeholder_img_src() );
$title = $product->get_name();
} else {
$available = true;
$price = '';
$_description = $product->get_description();
$title = $product->get_name();
$url = get_permalink( $product->get_id() );
$tags_array = [];
$terms_array = [];
$terms = get_the_terms( $item_id, 'product_cat' );
if ( !empty( $terms ) ) {
foreach ( $terms as $term ) {
$terms_array[] = $term->name;
}
}
$tags = get_the_terms( $item_id, 'product_tag' );
if ( !empty( $tags ) ) {
foreach ( $tags as $tag ) {
$tags_array[] = $tag->name;
}
}
if ( $product->get_type() == 'grouped' ) {
$children = $product->get_children();
$price = 0;
foreach ( $children as $key => $value ) {
$_product = wc_get_product( $value );
$price = $_product->get_price();
$image_url = wp_get_attachment_url( $product->get_image_id() );
}
} else {
$product = new WC_Product_Variable($item_id);
$variations = $product->get_available_variations();
foreach ( $variations as $variation ) {
if ( $variation['image']['thumb_src'] ) {
$image_url = $variation['image']['thumb_src'];
}
if ( $variation['display_regular_price'] ) {
$price = $variation['display_regular_price'];
}
}
}
}
$item = new Reqs\SetItemValues($item_id, [
'title' => $title,
'description' => $_description,
'image' => $image_url,
'available' => $available,
'categories' => $terms_array,
'tags' => $tags_array,
'price' => $price,
'url' => $url,
'brand' => '',
], [
'cascadeCreate' => true,
]);
array_push( $requests, $item );
}
}
$result = $client->send( new Reqs\Batch($requests) );
}
}
}
/**
* Load the required dependencies for this plugin.
*
* Include the following files that make up the plugin:
*
* - AIPersonalize360_Loader. Orchestrates the hooks of the plugin.
* - AIPersonalize360_i18n. Defines internationalization functionality.
* - AIPersonalize360_Admin. Defines all hooks for the admin area.
* - AIPersonalize360_Public. Defines all hooks for the public side of the site.
*
* Create an instance of the loader which will be used to register the hooks
* with WordPress.
*
* @since 1.0.0
* @access private
*/
private function aipers_load_dependencies() {
/**
* The class responsible for orchestrating the actions and filters of the
* core plugin.
*/
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-puredevs-aipersonalize360-loader.php';
/**
* The class responsible for defining all actions that occur in the admin area.
*/
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-puredevs-aipersonalize360-admin.php';
/**
* The class responsible for defining all actions that occur in the public-facing
* side of the site.
*/
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-puredevs-aipersonalize360-public.php';
$this->loader = new AIPERS_AIPersonalize360_Loader();
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/services/recombee/vendor/autoload.php';
}
/**
* Register all of the hooks related to the admin area functionality
* of the plugin.
*
* @since 1.0.0
* @access private
*/
private function aipers_define_admin_hooks() {
$plugin_admin = new AIPERS_AIPersonalize360_Admin($this->aipers_get_plugin_name(), $this->aipers_get_version());
$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'aipers_enqueue_styles' );
$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'aipers_enqueue_scripts' );
$this->loader->add_action( 'admin_menu', $plugin_admin, 'aipers_settings_page' );
$this->loader->add_action( 'admin_init', $plugin_admin, 'aipers_settings_fields' );
$this->loader->add_action( 'current_screen', $plugin_admin, 'aipers_kses_allowed_html_tags' );
}
/**
* Register all of the hooks related to the public-facing functionality
* of the plugin.
*
* @since 1.0.0
* @access private
*/
private function aipers_define_public_hooks() {
$plugin_public = new AIPERS_AIPersonalize360_Public($this->aipers_get_plugin_name(), $this->aipers_get_version());
$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'aipers_enqueue_styles' );
$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'aipers_enqueue_scripts' );
}
/**
* Run the loader to execute all of the hooks with WordPress.
*
* @since 1.0.0
*/
public function aipers_run() {
$this->loader->aipers_loader_run();
}
/**
* The name of the plugin used to uniquely identify it within the context of
* WordPress and to define internationalization functionality.
*
* @since 1.0.0
* @return string The name of the plugin.
*/
public function aipers_get_plugin_name() {
return $this->plugin_name;
}
/**
* The reference to the class that orchestrates the hooks with the plugin.
*
* @since 1.0.0
* @return AIPersonalize360_Loader Orchestrates the hooks of the plugin.
*/
public function get_loader() {
return $this->loader;
}
/**
* Retrieve the version number of the plugin.
*
* @since 1.0.0
* @return string The version number of the plugin.
*/
public function aipers_get_version() {
return $this->version;
}
}