| 1 |
<?php |
| 2 |
|
| 3 |
namespace King_Addons\Wishlist; |
| 4 |
|
| 5 |
use WP_User; |
| 6 |
|
| 7 |
if (!defined('ABSPATH')) { |
| 8 |
exit; |
| 9 |
} |
| 10 |
|
| 11 |
/** |
| 12 |
* Boots wishlist functionality and exposes shared service instance. |
| 13 |
*/ |
| 14 |
class Wishlist_Module |
| 15 |
{ |
| 16 |
private ?Wishlist_Service $service = null; |
| 17 |
private ?Wishlist_Renderer $renderer = null; |
| 18 |
private ?Wishlist_WooCommerce $woocommerce = null; |
| 19 |
private ?Wishlist_Frontend $frontend = null; |
| 20 |
|
| 21 |
/** |
| 22 |
* Hook module initialization. |
| 23 |
*/ |
| 24 |
public function __construct() |
| 25 |
{ |
| 26 |
add_action('init', [$this, 'boot']); |
| 27 |
add_action('wp_login', [$this, 'handle_login'], 10, 2); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Initialize wishlist module on init. |
| 32 |
* |
| 33 |
* @return void |
| 34 |
*/ |
| 35 |
public function boot(): void |
| 36 |
{ |
| 37 |
Wishlist_DB::maybe_create_tables(); |
| 38 |
$this->service = new Wishlist_Service(); |
| 39 |
$this->renderer = new Wishlist_Renderer($this->service); |
| 40 |
$this->woocommerce = new Wishlist_WooCommerce($this->service, $this->renderer); |
| 41 |
$this->woocommerce->hooks(); |
| 42 |
$this->frontend = new Wishlist_Frontend($this->service, $this->renderer); |
| 43 |
$this->frontend->hooks(); |
| 44 |
|
| 45 |
if (isset($_GET['ka_wishlist'])) { |
| 46 |
$wishlist_id = sanitize_title(wp_unslash($_GET['ka_wishlist'])); |
| 47 |
if (!empty($wishlist_id)) { |
| 48 |
$this->service->set_active_wishlist_id($wishlist_id); |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Merge guest wishlist into user account after login. |
| 55 |
* |
| 56 |
* @param string $user_login Username. |
| 57 |
* @param WP_User $user User object. |
| 58 |
* @return void |
| 59 |
*/ |
| 60 |
public function handle_login(string $user_login, WP_User $user): void |
| 61 |
{ |
| 62 |
$session = new Wishlist_Session(); |
| 63 |
$session_key = $session->get_session_key(); |
| 64 |
|
| 65 |
$service = new Wishlist_Service($user->ID, $session_key); |
| 66 |
$service->merge_guest_items($user->ID, $session_key); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Get wishlist service instance. |
| 71 |
* |
| 72 |
* @return Wishlist_Service Wishlist service. |
| 73 |
*/ |
| 74 |
public function service(): Wishlist_Service |
| 75 |
{ |
| 76 |
if (!$this->service instanceof Wishlist_Service) { |
| 77 |
$this->service = new Wishlist_Service(); |
| 78 |
} |
| 79 |
|
| 80 |
return $this->service; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Get wishlist renderer instance. |
| 85 |
* |
| 86 |
* @return Wishlist_Renderer Renderer instance. |
| 87 |
*/ |
| 88 |
public function renderer(): Wishlist_Renderer |
| 89 |
{ |
| 90 |
if (!$this->renderer instanceof Wishlist_Renderer) { |
| 91 |
$this->renderer = new Wishlist_Renderer($this->service()); |
| 92 |
} |
| 93 |
|
| 94 |
return $this->renderer; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Get frontend handler instance. |
| 99 |
* |
| 100 |
* @return Wishlist_Frontend Frontend instance. |
| 101 |
*/ |
| 102 |
public function frontend(): Wishlist_Frontend |
| 103 |
{ |
| 104 |
if (!$this->frontend instanceof Wishlist_Frontend) { |
| 105 |
$this->frontend = new Wishlist_Frontend($this->service(), $this->renderer()); |
| 106 |
} |
| 107 |
|
| 108 |
return $this->frontend; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|