0) { return $id; } $page = get_page_by_path('epaggelmaties-spoudastes'); return $page ? (int) $page->ID : 0; } public static function membership_url() { $id = self::membership_page_id(); if ($id > 0) { $url = get_permalink($id); if ($url) { return (string) $url; } } return home_url(); } public function add_endpoint() { add_rewrite_endpoint(self::ENDPOINT, EP_ROOT | EP_PAGES); } public function query_vars($vars) { $vars[] = self::ENDPOINT; return $vars; } public function menu_items($items) { $logout = isset($items['customer-logout']) ? $items['customer-logout'] : null; if ($logout !== null) { unset($items['customer-logout']); } $items[self::ENDPOINT] = __('Professional account', 'ai-eshop-optimizer'); if ($logout !== null) { $items['customer-logout'] = $logout; } return $items; } public function wc_query_vars($vars) { $vars[self::ENDPOINT] = self::ENDPOINT; return $vars; } public function endpoint_title() { return __('Professional account', 'ai-eshop-optimizer'); } public function endpoint_content() { echo do_shortcode('[' . self::SHORTCODE . ']'); } public function maybe_flush_rules() { if (get_option(self::FLUSH_OPTION) === 'done') { return; } flush_rewrite_rules(false); update_option(self::FLUSH_OPTION, 'done', false); } public function enqueue_assets() { global $wp_query; if (is_admin() || empty($wp_query) || !isset($wp_query->query_vars[self::ENDPOINT])) { return; } $rel = 'assets/css/aieo-push.css'; $dir = defined('AIEO_RUNTIME_DIR') ? AIEO_RUNTIME_DIR : WP_PLUGIN_DIR . '/ai-eshop-optimizer-runtime/'; $uri = defined('AIEO_RUNTIME_URI') ? AIEO_RUNTIME_URI : plugins_url('ai-eshop-optimizer-runtime/'); $path = $dir . $rel; if (file_exists($path) && !wp_style_is('aieo-push-optin', 'enqueued')) { wp_enqueue_style('aieo-push-optin', $uri . $rel, array(), (string) filemtime($path)); } } public function shortcode() { if (!is_user_logged_in()) { $login_url = function_exists('wc_get_page_permalink') ? (string) wc_get_page_permalink('myaccount') : wp_login_url(); return '

' . sprintf( wp_kses( __('Sign in to see your professional account.', 'ai-eshop-optimizer'), array('a' => array('href' => array())) ), esc_url($login_url) ) . '

'; } $user = wp_get_current_user(); $pro_slugs = self::pro_role_slugs(); $active = !empty($pro_slugs) ? array_values(array_intersect((array) $user->roles, $pro_slugs)) : array(); return !empty($active) ? $this->render_pro($user, $active) : $this->render_non_pro(); } private function render_pro($user, $active) { $names = $this->role_display_names($active); $out = '
'; $out .= '
'; $out .= '

' . esc_html(self::heading()) . '

'; $out .= '

' . esc_html__('You are registered as:', 'ai-eshop-optimizer') . ' ' . esc_html(implode(', ', $names)) . '

'; $out .= '
'; $out .= $this->render_savings_card((int) $user->ID); $out .= '
'; $out .= '

' . esc_html__('Manage / change plan', 'ai-eshop-optimizer') . '

'; $out .= '
'; $out .= '
'; return $out; } private static function heading() { return (string) apply_filters('aieo_pro_account_heading', __('Professional account', 'ai-eshop-optimizer')); } private function render_non_pro() { $out = '
'; $out .= '
'; $out .= '

' . esc_html(self::heading()) . '

'; $out .= '

' . esc_html__('Are you a professional? Join our professional programme for special prices, access to exclusive products and invitations to events.', 'ai-eshop-optimizer') . '

'; $out .= '
'; $form = shortcode_exists('membership_gf') ? trim((string) do_shortcode('[membership_gf]')) : ''; if ('' === trim(wp_strip_all_tags($form)) && !preg_match('/<(form|input|iframe)\b/i', $form)) { $form = '

' . esc_html__('Join the professional programme', 'ai-eshop-optimizer') . '

'; } $out .= '
' . $form . '
'; $out .= '
'; return $out; } private function role_display_names($slugs) { $names = wp_roles()->get_names(); $out = array(); foreach ($slugs as $slug) { if (isset($names[$slug])) { $out[] = function_exists('translate_user_role') ? translate_user_role($names[$slug]) : (string) $names[$slug]; } else { $out[] = (string) $slug; } } return $out; } private function render_savings_card($user_id) { $reads_ledger = class_exists('AIEO_DMM_Price_Filter') && AIEO_DMM_Price_Filter::resolves(); list($savings, $pro_lines, $orders) = $reads_ledger ? self::ledger_savings((int) $user_id) : self::analytics_savings((int) $user_id); $out = '
'; $out .= '

' . esc_html__('Your savings', 'ai-eshop-optimizer') . '

'; if ($savings !== null && $savings > 0) { $out .= '

' . sprintf( esc_html__('You have saved %s thanks to your professional status.', 'ai-eshop-optimizer'), wp_kses_post(wc_price($savings)) ) . '

'; $out .= '

' . sprintf( esc_html(_n( '%1$s product at a professional price, across %2$s orders.', '%1$s products at a professional price, across %2$s orders.', $pro_lines, 'ai-eshop-optimizer' )), esc_html(number_format_i18n($pro_lines)), esc_html(number_format_i18n($orders)) ) . '

'; } else { $out .= '

' . esc_html__('You have not saved anything yet — your purchases at professional prices will appear here.', 'ai-eshop-optimizer') . '

'; } $out .= '
'; return $out; } private static function ledger_savings($user_id) { global $wpdb; $table = class_exists('AIEO_DMM_Redemptions') ? AIEO_DMM_Redemptions::table() : ''; if ($table === '' || $user_id <= 0) { return array(null, 0, 0); } $decimals = function_exists('wc_get_price_decimals') ? max(0, (int) wc_get_price_decimals()) : 2; $show_tax = function_exists('wc_tax_enabled') && wc_tax_enabled() && 'incl' === get_option('woocommerce_tax_display_cart'); $meta = $wpdb->prefix . 'woocommerce_order_itemmeta'; $joins = ''; $saved = 'r.reduction'; if ($show_tax) { $joins = " LEFT JOIN `{$meta}` sub ON sub.order_item_id = r.order_item_id AND sub.meta_key = '_line_subtotal'" . " LEFT JOIN `{$meta}` tax ON tax.order_item_id = r.order_item_id AND tax.meta_key = '_line_subtotal_tax'"; $saved = 'r.reduction * (1 + COALESCE(CAST(tax.meta_value AS DECIMAL(20,6)) / NULLIF(CAST(sub.meta_value AS DECIMAL(20,6)), 0), 0))'; } $row = $wpdb->get_row($wpdb->prepare( "SELECT ROUND(SUM({$saved}), %d) AS savings, COUNT(DISTINCT r.order_id, r.order_item_id) AS pro_lines, COUNT(DISTINCT r.order_id) AS orders FROM `{$table}` r{$joins} WHERE r.user_id = %d AND r.item_type = 'line' AND r.status IN ('paid', 'partly_refunded') AND r.reduction > 0", $decimals, (int) $user_id )); if ((class_exists('AIEO_DMM_Schema_Gate', false) && AIEO_DMM_Schema_Gate::failed('pro_account_savings')) || !$row) { return array(null, 0, 0); } return array(($row->savings === null) ? null : (float) $row->savings, (int) $row->pro_lines, (int) $row->orders); } private static function analytics_savings($user_id) { global $wpdb; $table = $wpdb->prefix . 'aieo_order_products_analytics'; $savings = null; $pro_lines = 0; $orders = 0; $paid = function_exists('wc_get_is_paid_statuses') ? (array) wc_get_is_paid_statuses() : array('processing', 'completed'); $paid = array_values(array_filter(array_map(static function ($s) { $s = (string) $s; return ('' === $s || 0 === strpos($s, 'wc-')) ? $s : 'wc-' . $s; }, $paid))); if ($paid && $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $table))) { $orders_table = $wpdb->prefix . 'wc_orders'; $in = implode(',', array_fill(0, count($paid), '%s')); $items_table = $wpdb->prefix . 'woocommerce_order_items'; $meta_table = $wpdb->prefix . 'woocommerce_order_itemmeta'; $lookup_table = $wpdb->prefix . 'wc_product_meta_lookup'; $net = '1'; if (function_exists('wc_prices_include_tax') && wc_prices_include_tax() && class_exists('WC_Tax')) { $share = static function ($tax_class) { $tax = array_sum(WC_Tax::calc_tax(10000, WC_Tax::get_base_tax_rates($tax_class), true)); return sprintf('%.8F', 1 - $tax / 10000); }; $net = "CASE (CASE WHEN a.variation_id > 0 AND lv.tax_class IS NOT NULL AND lv.tax_class <> 'parent' THEN lv.tax_class ELSE COALESCE(lp.tax_class, '') END)" . " WHEN '' THEN " . $share(''); foreach (WC_Tax::get_tax_class_slugs() as $slug) { $net .= $wpdb->prepare(' WHEN %s THEN ', (string) $slug) . $share((string) $slug); } $net = "IF(COALESCE(lp.tax_status, 'taxable') = 'taxable', {$net} ELSE " . $share('') . ' END, 1)'; } $show_tax = function_exists('wc_tax_enabled') && wc_tax_enabled() && 'incl' === get_option('woocommerce_tax_display_cart') ? 1 : 0; $rate = '(1 + COALESCE(CAST(tx.meta_value AS DECIMAL(20,6)) / NULLIF(a.price,0), 0))'; $kept = 'GREATEST(COALESCE(a.quantity,1) - COALESCE(rf.qty,0), 0)'; $row = $wpdb->get_row($wpdb->prepare( "SELECT ROUND(SUM(CASE WHEN a.prof_discount = 'NAI' THEN GREATEST(COALESCE(a.list_price,0) * {$net} - COALESCE(a.unit_price,0), 0) * {$kept} * IF(%d = 1, {$rate}, 1) ELSE 0 END),2) AS savings, SUM(a.prof_discount = 'NAI' AND {$kept} > 0) AS pro_lines, COUNT(DISTINCT a.order_id) AS orders FROM `{$table}` a JOIN `{$orders_table}` o ON o.id = a.order_id AND o.type = 'shop_order' AND o.status IN ({$in}) LEFT JOIN `{$meta_table}` tx ON tx.order_item_id = a.order_item_id AND tx.meta_key = '_line_subtotal_tax' LEFT JOIN `{$lookup_table}` lp ON lp.product_id = a.product_id LEFT JOIN `{$lookup_table}` lv ON lv.product_id = a.variation_id LEFT JOIN ( SELECT CAST(rid.meta_value AS UNSIGNED) AS item_id, SUM(ABS(CAST(rq.meta_value AS DECIMAL(10,3)))) AS qty FROM `{$orders_table}` p JOIN `{$orders_table}` r ON r.parent_order_id = p.id AND r.type = 'shop_order_refund' JOIN `{$items_table}` ri ON ri.order_id = r.id AND ri.order_item_type = 'line_item' JOIN `{$meta_table}` rid ON rid.order_item_id = ri.order_item_id AND rid.meta_key = '_refunded_item_id' JOIN `{$meta_table}` rq ON rq.order_item_id = ri.order_item_id AND rq.meta_key = '_qty' WHERE p.customer_id = %d GROUP BY CAST(rid.meta_value AS UNSIGNED) ) rf ON rf.item_id = a.order_item_id WHERE a.customer_id = %d", array_merge(array($show_tax), $paid, array((int) $user_id, (int) $user_id)) )); if ($row) { $savings = ($row->savings === null) ? null : (float) $row->savings; $pro_lines = (int) $row->pro_lines; $orders = (int) $row->orders; } } return array($savings, $pro_lines, $orders); } } }