<?php if ( ! defined( 'ABSPATH' ) ) exit; class GT_BOGO_Social_Proof { private $id = 'social_proof'; public function __construct() { if ( is_admin() && ! defined('DOING_AJAX') ) return; if ( ! $this->enabled() ) return; add_action( 'wp_enqueue_scripts', [ $this, 'enqueue' ] ); } private function enabled() { return (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.78em', 'padding' => '10px 14px', 'opacity' => '0.65' ], 'moderate' => [ 'font_size' => '0.85em', 'padding' => '12px 16px', 'opacity' => '0.80' ], 'bold' => [ 'font_size' => '0.9em', 'padding' => '14px 18px', 'opacity' => '1.0' ], ]; return $map[ $this->intensity() ] ?? $map['subtle']; } private function get_recent_purchases( $product_id ) { global $wpdb; $count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT p.ID) FROM {$wpdb->posts} p INNER JOIN {$wpdb->prefix}woocommerce_order_items oi ON p.ID = oi.order_id INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta om ON oi.order_item_id = om.order_item_id WHERE p.post_type = 'shop_order' AND p.post_status IN ('wc-completed','wc-processing') AND om.meta_key = '_product_id' AND om.meta_value = %d AND p.post_date > DATE_SUB(NOW(), INTERVAL 7 DAY)", $product_id ) ); return $count; } public function enqueue() { wp_add_inline_style( 'woocommerce-general', ' .gtbogo-social-proof{display:flex;align-items:flex-start;gap:10px;font-family:inherit;color:inherit;border-left:2px solid currentColor;margin:12px 0} .gtbogo-sp-dot{width:7px;height:7px;border-radius:50%;background:currentColor;flex-shrink:0;margin-top:0.4em;animation:gtbogo-sp-blink 2s ease-in-out infinite} @keyframes gtbogo-sp-blink{0%,100%{opacity:0.9}50%{opacity:0.3}} .gtbogo-sp-lines{display:flex;flex-direction:column;gap:2px} .gtbogo-sp-main{font-weight:700} .gtbogo-sp-sub{opacity:0.55;font-size:0.9em} ' ); } public function render() { global $product; if ( ! $product ) return; $purchased = $this->get_recent_purchases( $product->get_id() ); $viewers = (int) get_post_meta( $product->get_id(), '_gtbogo_view_count', true ); update_post_meta( $product->get_id(), '_gtbogo_view_count', $viewers + 1 ); $viewers = max( $viewers, wp_rand( 8, 32 ) ); $s = $this->styles(); ?> <div class="gtbogo-social-proof" style="padding:<?php echo esc_attr($s['padding']); ?>;font-size:<?php echo esc_attr($s['font_size']); ?>;opacity:<?php echo esc_attr($s['opacity']); ?>"> <span class="gtbogo-sp-dot" aria-hidden="true"></span> <div class="gtbogo-sp-lines"> <?php if ( $purchased > 0 ) : ?> <span class="gtbogo-sp-main"> <?php printf( esc_html( _n( '%d person bought this in the last 7 days', '%d people bought this in the last 7 days', $purchased, 'gt-bogo-engine' ) ), (int) $purchased ); ?> </span> <?php endif; ?> <span class="gtbogo-sp-sub"> <?php printf( esc_html( _n( '%d person viewing this now', '%d people viewing this now', $viewers, 'gt-bogo-engine' ) ), (int) $viewers ); ?> </span> <span class="gtbogo-sp-sub"><?php esc_html_e( 'Deal available while stock lasts', 'gt-bogo-engine' ); ?></span> </div> </div> <?php } }