'; 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($opts['title']) . '
' : '') . '' . $html . '
' . (isset($opts['detail']) ? $opts['detail'] : '') . (isset($opts['actions']) ? '' . $opts['actions'] . '
' : '') . '' . 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__('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') . '
'primary', 'attrs' => array('id' => 'aieo-upsell-import-button'))); ?>