Controllers
1 week ago
DigitalProducts
5 months ago
Emails
3 months ago
Models
1 month ago
PaymentMethods
1 month ago
Repositories
2 months ago
Services
4 months ago
CheckoutFields.php
1 month ago
CurrencyFormatter.php
9 months ago
Init.php
2 years ago
StatSync.php
1 year ago
index.php
3 years ago
StatSync.php
75 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Membership; |
| 4 | |
| 5 | use ProfilePress\Core\Membership\Models\Customer\CustomerFactory; |
| 6 | use ProfilePress\Core\Membership\Models\Order\OrderEntity; |
| 7 | use ProfilePress\Core\Membership\Models\Order\OrderFactory; |
| 8 | |
| 9 | class StatSync |
| 10 | { |
| 11 | public function __construct() |
| 12 | { |
| 13 | add_action('ppress_customer_updated', function ($customer_id) { |
| 14 | $this->core_actions(false, $customer_id); |
| 15 | }); |
| 16 | |
| 17 | add_action('ppress_order_completed', function (OrderEntity $order) { |
| 18 | $this->core_actions(false, $order->customer_id); |
| 19 | }); |
| 20 | |
| 21 | add_action('ppress_order_added', function ($result, OrderEntity $order) { |
| 22 | $this->core_actions(false, $order->customer_id); |
| 23 | }, 10, 2); |
| 24 | |
| 25 | add_action('ppress_order_updated', function ($result, OrderEntity $order) { |
| 26 | $this->core_actions(false, $order->customer_id); |
| 27 | }, 10, 2); |
| 28 | |
| 29 | add_action('ppress_order_deleted', function ($order_id) { |
| 30 | $this->core_actions( |
| 31 | false, |
| 32 | OrderFactory::fromId($order_id)->get_customer_id() |
| 33 | ); |
| 34 | }); |
| 35 | |
| 36 | add_action('wp_login', function ($user_login, $user) { |
| 37 | |
| 38 | if ($user instanceof \WP_User) { |
| 39 | $user_id = $user->ID; |
| 40 | } else { |
| 41 | $user = get_user_by('login', $user_login); |
| 42 | $user_id = $user->exists() ? $user->ID : get_current_user_id(); |
| 43 | } |
| 44 | |
| 45 | self::core_actions($user_id); |
| 46 | |
| 47 | }, 10, 2); |
| 48 | } |
| 49 | |
| 50 | public function core_actions($user_id = false, $customer_id = false) |
| 51 | { |
| 52 | $user_id = false !== $user_id ? absint($user_id) : get_current_user_id(); |
| 53 | |
| 54 | if ($customer_id) { |
| 55 | CustomerFactory::fromId($customer_id)->recalculate_stats(); |
| 56 | } elseif ($user_id) { |
| 57 | CustomerFactory::fromUserId($user_id)->recalculate_stats(); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @return self |
| 63 | */ |
| 64 | public static function init() |
| 65 | { |
| 66 | static $instance = null; |
| 67 | |
| 68 | if (is_null($instance)) { |
| 69 | $instance = new self(); |
| 70 | } |
| 71 | |
| 72 | return $instance; |
| 73 | } |
| 74 | } |
| 75 |