<?php if ( ! defined( 'ABSPATH' ) ) exit; class GT_BOGO_Savings_History { private $id = 'savings_history'; public function __construct() { if ( is_admin() && ! defined('DOING_AJAX') ) return; if ( ! $this->enabled() ) return; add_action( 'woocommerce_account_dashboard', [ $this, 'render_account' ] ); add_action( 'wp_enqueue_scripts', [ $this, 'enqueue' ] ); add_action( 'template_redirect', [ $this, 'register_hooks' ], 5 ); } public function register_hooks(): void { if ( class_exists( 'GT_BOGO_Blocks_Compat' ) && GT_BOGO_Blocks_Compat::is_blocks_page() ) { add_action( 'wp_footer', [ $this, 'register_blocks_injection' ], 10 ); } else { add_action( 'woocommerce_cart_totals_before_order_total', [ $this, 'render_cart' ] ); } } public function register_blocks_injection(): void { if ( ! class_exists( 'GT_BOGO_Frontend_Injector' ) ) return; ob_start(); $this->render_cart(); $html = ob_get_clean(); if ( empty( trim( $html ) ) ) return; $selector = GT_BOGO_Blocks_Compat::is_blocks_checkout() ? '.wc-block-components-totals-footer-item' : '.wc-block-cart__totals-title'; GT_BOGO_Frontend_Injector::register( 'gtbogo-savings-history', $selector, $html, 'before', 10 ); } private function enabled() { return is_user_logged_in() && (bool) get_option( 'gtbogo_sp_' . $this->id . '_enabled', 0 ) && get_option( 'gtbogo_sp_' . $this->id . '_intensity', 'subtle' ) !== 'off'; } private function intensity() { return get_option( 'gtbogo_sp_' . $this->id . '_intensity', 'subtle' ); } private function styles() { $map = [ 'subtle' => [ 'font_size' => '0.8em', 'padding' => '10px 14px', 'opacity' => '0.7' ], 'moderate' => [ 'font_size' => '0.88em', 'padding' => '12px 18px', 'opacity' => '0.85' ], 'bold' => [ 'font_size' => '0.95em', 'padding' => '14px 22px', 'opacity' => '1.0' ], ]; return $map[ $this->intensity() ] ?? $map['subtle']; } private function get_total_savings( $user_id ) { $cached = get_user_meta( $user_id, '_gtbogo_total_savings', true ); if ( $cached !== '' ) return (float) $cached; global $wpdb; $total = (float) $wpdb->get_var( $wpdb->prepare( "SELECT SUM(meta_value) FROM {$wpdb->postmeta} pm INNER JOIN {$wpdb->posts} p ON pm.post_id = p.ID WHERE pm.meta_key = '_gtbogo_savings' AND p.post_type = 'shop_order' AND p.post_author = %d", $user_id ) ); update_user_meta( $user_id, '_gtbogo_total_savings', $total ); return $total; } public function enqueue() { wp_add_inline_style( 'woocommerce-general', ' .gtbogo-savings-history{font-family:inherit;color:inherit;border-left:3px solid currentColor;display:flex;align-items:center;gap:14px;margin:10px 0} .gtbogo-savings-amount{font-size:1.4em;font-weight:700;line-height:1} .gtbogo-savings-label{opacity:0.65;font-size:0.9em} ' ); } private function render( $context = '' ) { $user_id = get_current_user_id(); if ( ! $user_id ) return; $total = $this->get_total_savings( $user_id ); if ( $total < 0.01 ) return; $s = $this->styles(); ?> <div class="gtbogo-savings-history" style="padding:<?php echo esc_attr($s['padding']); ?>;font-size:<?php echo esc_attr($s['font_size']); ?>;opacity:<?php echo esc_attr($s['opacity']); ?>"> <div> <div class="gtbogo-savings-amount"><?php echo wp_kses_post( wc_price( $total ) ); ?></div> <div class="gtbogo-savings-label"><?php esc_html_e( 'saved with deals at this store', 'gt-bogo-engine' ); ?></div> </div> </div> <?php } public function render_cart() { $this->render( 'cart' ); } public function render_account() { $this->render( 'account' ); } }