'; echo ''; $checkbox_options = [ 'aieo_create_attributes', 'aieo_create_needs', 'aieo_create_catalogue', 'aieo_create_orders', 'aieo_erp_consolidation', 'aieo_erp_merge_orders', 'aieo_include_unsold', 'aieo_include_aggregate_stats', 'aieo_include_aggregate_cust_std_stats', 'aieo_include_aggregate_cust_adv_stats', 'aieo_create_uuids', 'aieo_create_export', 'aieo_include_text', 'aieo_include_attributes', 'aieo_include_full_text', 'aieo_include_price', 'aieo_variations_choice', 'aieo_include_eponymous', 'aieo_include_reciprocity', 'aieo_include_seasonality', 'aieo_include_restricted_seasonality', 'aieo_instore_anonymous', 'aieo_season_days_past', 'aieo_season_days_after' ]; foreach ($checkbox_options as $opt) { wp_cache_delete($opt, 'options'); } function aieo_getRandomNumberBetween1And3() { $randomNumber = wp_rand(1, 3); return $randomNumber; } $randomNumber = aieo_getRandomNumberBetween1And3(); function aieo_get_temp_orders_table_for_insights() { global $wpdb; $table = get_option('aieo_site_orders_table', $wpdb->prefix . 'aieo_temp_orders'); if (!$table) { return ''; } $table = sanitize_text_field($table); if (!preg_match('/^[a-zA-Z0-9_]+$/', $table)) { return ''; } $table_exists = $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $table)); if (!$table_exists) { return ''; } return $table; } function aieo_fetch_single_product_focus($table, $limit = 10) { global $wpdb; $cache_key = 'aieo_insight_single_' . md5($table . $limit); $cached = get_transient($cache_key); if ($cached !== false) { if (!empty($cached)) { return $cached; } delete_transient($cache_key); } $sql = $wpdb->prepare( "SELECT t.ParentProductId AS product_id, SUM(COALESCE(t.PrimaryQty, 1)) AS total_units, COUNT(*) AS order_lines FROM {$table} t INNER JOIN ( SELECT OrderId FROM {$table} GROUP BY OrderId HAVING COUNT(*) = 1 ) single ON t.OrderId = single.OrderId WHERE t.ParentProductId IS NOT NULL GROUP BY t.ParentProductId ORDER BY total_units DESC LIMIT %d", $limit ); $rows = $wpdb->get_results($sql); $results = []; if ($rows) { foreach ($rows as $row) { $product_id = (int) $row->product_id; if (!$product_id) { continue; } $title = get_the_title($product_id); if (!$title) { continue; } $permalink = get_permalink($product_id); $results[] = array( 'product_id' => $product_id, 'title' => $title, 'permalink' => $permalink, 'units' => (float) $row->total_units, 'orders' => (int) $row->order_lines, ); } } if (!empty($results)) { usort($results, function ($a, $b) { $brand_a = $a['brand'] ?? ''; $brand_b = $b['brand'] ?? ''; return strcasecmp($brand_a, $brand_b); }); set_transient($cache_key, $results, HOUR_IN_SECONDS); } else { delete_transient($cache_key); } return $results; } function aieo_fetch_favourite_products($table, $limit = 10) { global $wpdb; $cache_key = 'aieo_insight_favourites_' . md5($table . $limit); $cached = get_transient($cache_key); if ($cached !== false) { if (!empty($cached)) { return $cached; } delete_transient($cache_key); } $sql = $wpdb->prepare( "SELECT fav.* FROM ( SELECT ParentProductId AS product_id, MAX(COALESCE(FirstItemSales, 0)) AS first_units, MAX(COALESCE(LastItemSales, 0)) AS last_units FROM {$table} WHERE ParentProductId IS NOT NULL GROUP BY ParentProductId ) fav WHERE (fav.first_units + fav.last_units) > 0 ORDER BY (fav.first_units + fav.last_units) DESC LIMIT %d", $limit ); $rows = $wpdb->get_results($sql); $results = []; if ($rows) { foreach ($rows as $row) { $product_id = (int) $row->product_id; if (!$product_id) { continue; } $title = get_the_title($product_id); if (!$title) { continue; } $permalink = get_permalink($product_id); $first_units = (float) $row->first_units; $last_units = (float) $row->last_units; if (($first_units + $last_units) <= 0) { continue; } $results[] = array( 'product_id' => $product_id, 'title' => $title, 'permalink' => $permalink, 'first_units' => $first_units, 'last_units' => $last_units, 'score' => $first_units + $last_units, ); } } if (empty($results)) { $fallback_sql = $wpdb->prepare( "SELECT fav.* FROM ( SELECT ParentProductId AS product_id, SUM(CASE WHEN OrderSize > 1 AND ItemSequence = 1 THEN COALESCE(PrimaryQty,1) ELSE 0 END) AS first_units, SUM(CASE WHEN OrderSize > 1 AND ItemSequence = OrderSize THEN COALESCE(PrimaryQty,1) ELSE 0 END) AS last_units FROM {$table} WHERE ParentProductId IS NOT NULL GROUP BY ParentProductId ) fav WHERE (fav.first_units + fav.last_units) > 0 ORDER BY (fav.first_units + fav.last_units) DESC LIMIT %d", $limit ); $rows = $wpdb->get_results($fallback_sql); if ($rows) { foreach ($rows as $row) { $product_id = (int) $row->product_id; if (!$product_id) { continue; } $title = get_the_title($product_id); if (!$title) { continue; } $permalink = get_permalink($product_id); $first_units = (float) $row->first_units; $last_units = (float) $row->last_units; if (($first_units + $last_units) <= 0) { continue; } $results[] = array( 'product_id' => $product_id, 'title' => $title, 'permalink' => $permalink, 'first_units' => $first_units, 'last_units' => $last_units, 'score' => $first_units + $last_units, ); } } } if (!empty($results)) { set_transient($cache_key, $results, HOUR_IN_SECONDS); } else { delete_transient($cache_key); } return $results; } function aieo_fetch_brand_focus($table, $limit = 10) { global $wpdb; $cache_key = 'aieo_insight_brands_' . md5($table . $limit); $cached = get_transient($cache_key); if ($cached !== false) { if (!empty($cached)) { return $cached; } delete_transient($cache_key); } $sql = $wpdb->prepare( "SELECT brand_name, single_units, first_units, last_units, score FROM ( SELECT brand_name, SUM(single_unit) AS single_units, SUM(first_unit) AS first_units, SUM(last_unit) AS last_units, SUM(single_unit + first_unit + last_unit) AS score FROM ( SELECT COALESCE(NULLIF(TRIM(Brand), ''), 'Unbranded') AS brand_name, MAX(COALESCE(UniqueItemSales, 0)) AS single_unit, MAX(COALESCE(FirstItemSales, 0)) AS first_unit, MAX(COALESCE(LastItemSales, 0)) AS last_unit FROM {$table} WHERE ParentProductId IS NOT NULL GROUP BY ParentProductId, COALESCE(NULLIF(TRIM(Brand), ''), 'Unbranded') ) product_brand GROUP BY brand_name HAVING SUM(single_unit + first_unit + last_unit) > 0 ORDER BY score DESC LIMIT %d ) top_brands ORDER BY brand_name ASC", $limit ); $rows = $wpdb->get_results($sql); $results = []; if ($rows) { foreach ($rows as $row) { $brand = $row->brand_name; $single_units = (float) $row->single_units; $first_units = (float) $row->first_units; $last_units = (float) $row->last_units; $results[] = array( 'brand' => $brand, 'single_units' => $single_units, 'first_units' => $first_units, 'last_units' => $last_units, 'score' => $single_units + $first_units + $last_units, ); } } if (!empty($results)) { set_transient($cache_key, $results, HOUR_IN_SECONDS); } else { delete_transient($cache_key); } return $results; } $aieo_insight_data = array( 'available' => false, 'single_products' => array(), 'favourite_products' => array(), 'brands' => array(), ); $aieo_orders_table = aieo_get_temp_orders_table_for_insights(); if ($aieo_orders_table) { global $wpdb; $has_product_stats = (int) $wpdb->get_var("SELECT COUNT(*) FROM {$aieo_orders_table} WHERE TotalItemSales IS NOT NULL AND TotalItemSales > 0 LIMIT 1"); $has_adv_stats = (int) $wpdb->get_var("SELECT COUNT(*) FROM {$aieo_orders_table} WHERE AvgOrderSize IS NOT NULL AND AvgOrderSize > 0 LIMIT 1"); if ($has_product_stats && $has_adv_stats) { $aieo_insight_data['single_products'] = aieo_fetch_single_product_focus($aieo_orders_table); $aieo_insight_data['favourite_products'] = aieo_fetch_favourite_products($aieo_orders_table); $aieo_insight_data['brands'] = aieo_fetch_brand_focus($aieo_orders_table); if (!empty($aieo_insight_data['single_products']) || !empty($aieo_insight_data['favourite_products']) || !empty($aieo_insight_data['brands'])) { $aieo_insight_data['available'] = true; } } } ?>
'; } ?>
' . esc_html($title) . ''; } return rooster_admin_card_section_html($title, '' !== $id ? array('id' => $id) : array()); }; $aieo_as_notice = static function ($html, $tone, array $opts = array()) { if (class_exists('\Rooster\Admin\Ui')) { return \Rooster\Admin\Ui::notice($html, $tone, $opts + array('inline' => true)); } return '
' . (!empty($opts['title']) ? '

' . esc_html($opts['title']) . '

' : '') . '

' . $html . '

' . (isset($opts['detail']) ? $opts['detail'] : '') . (isset($opts['actions']) ? '

' . $opts['actions'] . '

' : '') . '
'; }; $aieo_as_why = static function ($text, $question = '') { if (class_exists('\Rooster\Admin\Guidance')) { return \Rooster\Admin\Guidance::text((string) $text, 'guidance', '' !== (string) $question ? array('question' => (string) $question) : array()); } return '

' . esc_html($text) . '

'; }; $aieo_chk = static function ($id, $name, $on, $label, array $opts = array()) { $has_lib = class_exists('\Rooster\Admin\Ui'); $extra = ''; $tie = ''; if (!empty($opts['soon'])) { $tie = ' disabled="disabled" aria-describedby="' . esc_attr($id) . '-reason"'; $extra = ' ' . $opts['soon'] . ''; } elseif (!empty($opts['warn'])) { $extra = '' . $opts['warn'] . ''; } return ''; }; $aieo_as_link = static function ($id, $label) { if (class_exists('\Rooster\Admin\Ui')) { return \Rooster\Admin\Ui::button($label, array('size' => 'sm', 'attrs' => array('id' => $id))); } return ''; }; list($aieo_as_open, $aieo_as_close) = AIEO_DMM_UI::card(array( 'anchor' => 'orders-export', 'icon' => 'send', 'title' => __('Superfast Export of your Orders', 'ai-eshop-optimizer'), 'description' => __('Choose which order data is exported and analysed to build your product recommendations.', 'ai-eshop-optimizer'), 'footer' => AIEO_Kit::button(__('Save Settings', 'ai-eshop-optimizer'), array('attrs' => array('id' => 'aieo-save-settings-button'))) . '' . AIEO_Kit::button(__('Save Settings & Analyze Data', 'ai-eshop-optimizer'), array('variant' => 'primary', 'attrs' => array('id' => 'aieo-export-start-button'))) . '', )); echo $aieo_as_open; echo $aieo_as_section(__('Privacy first', 'ai-eshop-optimizer'), 'aieo-card-privacy-first', true); ?>
esc_html__('Coming soon', 'ai-eshop-optimizer'))); ?>
array(__('Vendor attribute slug', 'ai-eshop-optimizer'), __('pa_vendor', 'ai-eshop-optimizer'), (string) get_option('aieo_core_attribute_vendor'), true), 'aieo_core_attribute_brands' => array(__('Brand attribute slug', 'ai-eshop-optimizer'), __('product_brand', 'ai-eshop-optimizer'), (string) get_option('aieo_core_attribute_brands'), true), 'aieo_core_attribute_size' => array(__('Size attribute slugs', 'ai-eshop-optimizer'), __('pa_size,pa_fit', 'ai-eshop-optimizer'), (string) get_option('aieo_core_attribute_size'), false), 'aieo_core_attribute_color' => array(__('Colour attribute slugs', 'ai-eshop-optimizer'), __('pa_color,pa_colour', 'ai-eshop-optimizer'), (string) get_option('aieo_core_attribute_color'), false), 'aieo_core_attribute_need' => array(__('Need attribute slug', 'ai-eshop-optimizer'), __('pa_need', 'ai-eshop-optimizer'), (string) get_option('aieo_core_attribute_need', 'pa_need'), false), ) as $aieo_slug_name => $aieo_slug) { echo AIEO_Kit::row($aieo_slug[0], AIEO_Kit::input($aieo_slug_name, $aieo_slug[2], array('id' => $aieo_slug_name, 'placeholder' => $aieo_slug[1], 'required' => $aieo_slug[3], 'medium' => true, 'code' => true)), '', array('id' => $aieo_slug_name, 'required' => $aieo_slug[3])); } ?>
before current date (in days)', 'ai-eshop-optimizer')), AIEO_Kit::input('aieo_season_days_past', (string) get_option('aieo_season_days_past'), array('type' => 'number', 'id' => 'aieo_season_days_past', 'placeholder' => '15', 'xs' => true)), '', array('id' => 'aieo_season_days_past', 'class' => 'aieo-seasonality-settings-row')); echo AIEO_Kit::row(wp_strip_all_tags(__('Consideration period after current date (in days)', 'ai-eshop-optimizer')), AIEO_Kit::input('aieo_season_days_after', (string) get_option('aieo_season_days_after'), array('type' => 'number', 'id' => 'aieo_season_days_after', 'placeholder' => '60', 'xs' => true)), '', array('id' => 'aieo_season_days_after', 'class' => 'aieo-seasonality-settings-row')); ?>
__('All time', 'ai-eshop-optimizer'), 'last_30_days' => __('Last 30 days', 'ai-eshop-optimizer'), 'last_3_months' => __('Last 3 months', 'ai-eshop-optimizer'), 'last_6_months' => __('Last 6 months', 'ai-eshop-optimizer'), 'last_12_months' => __('Last 12 months', 'ai-eshop-optimizer'), ); ?>
'aieo_category_sales_period', 'help' => true)), __('Which orders count towards a category or brand being popular. Applies to the aggregate above; it does not change anything a shopper sees.', 'ai-eshop-optimizer'), array('id' => 'aieo_category_sales_period')); ?>
esc_html__('Slow for large datasets with many customers', 'ai-eshop-optimizer'))); ?> esc_html__('Very slow for large datasets - may take several minutes', 'ai-eshop-optimizer'))); ?>
'number', 'id' => 'aieo_order_id', 'placeholder' => __('Set initial Order ID', 'ai-eshop-optimizer'), 'required' => true, 'short' => true)), '', array('id' => 'aieo_order_id', 'required' => true)); echo AIEO_Kit::row(__('Maximum Number of Records', 'ai-eshop-optimizer'), AIEO_Kit::input('aieo_max_records', (string) get_option('aieo_max_records'), array('type' => 'number', 'id' => 'aieo_max_records', 'placeholder' => __('Maximum Number of Records', 'ai-eshop-optimizer'), 'required' => true, 'xs' => true)), '', array('id' => 'aieo_max_records', 'required' => true)); ?>
array( 'href' => array(), 'title' => array(), 'class' => array(), 'style' => array(), ), 'br' => array(), ); $csv_info = get_option('aieo_last_export_csv', array()); $file_path = isset($csv_info['path']) ? $csv_info['path'] : ''; $download_url = isset($csv_info['url']) ? $csv_info['url'] : ''; $filename = isset($csv_info['filename']) ? $csv_info['filename'] : ''; $downloadfilemessage = ''; if ($download_url && $filename) { $downloadfilemessage = sprintf( __('The orders file has been created!!!
Download:
%2$s', 'ai-eshop-optimizer'), esc_url($download_url), esc_html($filename) ); } echo '
'; if ($file_path && file_exists($file_path)) { if ((int) get_option('aieo_activate_remote_connection') === 7) { $aieo_as_file = $aieo_as_notice( esc_html__('Your orders file has been successfully uploaded to our server', 'ai-eshop-optimizer'), 'success', array( 'id' => 'aieo-card-orders-file-uploaded', 'title' => __('Orders file uploaded', 'ai-eshop-optimizer'), 'detail' => '

' . esc_html__('You are going to be notified as soon as its processing completes in order to visit our portal and download your recommendations as well as see key vital statistics', 'ai-eshop-optimizer') . '

', ) ); } else { if ((int) get_option('aieo_activate_remote_connection') === 1) { $aieo_as_pro = '

' . esc_html__('As a pro user you can upload the file directly from this plugin via the upload data in your server tab', 'ai-eshop-optimizer') . '

'; } else { $aieo_as_pro = '

' . esc_html__('Why do we save your file on your server?', 'ai-eshop-optimizer') . '
' . esc_html__('Our pro users on annual contracts are able to transfer their data directly into our server for optimization calculations and fully automate the process', 'ai-eshop-optimizer') . '

'; } $delete_url = wp_nonce_url(admin_url('admin-post.php?action=delete_exported_file'), 'delete_exported_file'); $aieo_as_file = $aieo_as_notice( wp_kses($downloadfilemessage, $allowed_html_tags), 'success', array( 'id' => 'aieo-card-orders-file-download', 'title' => __('Download your orders file', 'ai-eshop-optimizer'), 'detail' => $aieo_as_pro, 'actions' => AIEO_Kit::button(__('Important! Click to remove the file from your server', 'ai-eshop-optimizer'), array('href' => $delete_url, 'variant' => 'destructive')), ) ); } echo $aieo_as_file; } echo '
'; echo $aieo_as_close; if (empty($aieo_insight_data['available'])) { echo ''; } if (!empty($aieo_insight_data['available'])): list($aieo_as_open, $aieo_as_close) = AIEO_DMM_UI::card(array( 'anchor' => 'indicative-statistics', 'icon' => 'chart-column', 'title' => __('Indicative Statistics', 'ai-eshop-optimizer'), )); echo $aieo_as_open; echo $aieo_as_section(__("Your customers' favourite products", 'ai-eshop-optimizer'), 'aieo-card-favourite-products', true); ?>
'eshop-update', 'icon' => 'refresh-cw', 'title' => __('Eshop Update', 'ai-eshop-optimizer'), 'description' => __('Writes the analysed up-sells and cross-sells back to your products, within the time limits you set here.', 'ai-eshop-optimizer'), )); echo $aieo_as_open; echo $aieo_as_section(__('Monitor Your Server and Define Time-Out Limits', 'ai-eshop-optimizer'), 'aieo-card-server-timeouts', true); ?>
'number', 'id' => 'aieo_batch_size', 'placeholder' => __('Set Batch Size', 'ai-eshop-optimizer'), 'required' => true, 'xs' => true)), '', array('id' => 'aieo_batch_size', 'required' => true)); echo AIEO_Kit::row(__('Sleep between batches (in seconds)', 'ai-eshop-optimizer'), AIEO_Kit::input('aieo_batch_sleep', (string) get_option('aieo_batch_sleep', 0), array('type' => 'number', 'id' => 'aieo_batch_sleep', 'placeholder' => __('Cooling time (s)', 'ai-eshop-optimizer'), 'required' => true, 'xs' => true)), '', array('id' => 'aieo_batch_sleep', 'required' => true)); ?>
'csv_file', 'form' => 'recommendationsUploadForm', 'accept' => '.csv,text/csv' ) ), '', array('id' => 'csv_file')); ?>

'primary', 'attrs' => array('id' => 'aieo-upsell-import-button'))); ?>

array('id' => 'aieo-upsell-export-button'))); ?>