| 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('send_headers', [$this, 'maybe_expire_cookie']); |
| 27 |
add_action('init', [$this, 'boot']); |
| 28 |
add_action('wp_login', [$this, 'handle_login'], 10, 2); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Drop leftover guest cookies when wishlist is turned off in settings. |
| 33 |
* |
| 34 |
* @return void |
| 35 |
*/ |
| 36 |
public function maybe_expire_cookie(): void |
| 37 |
{ |
| 38 |
if (Wishlist_Settings::is_enabled() || is_admin()) { |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
Wishlist_Session::expire_if_present(); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Initialize wishlist module on init. |
| 47 |
* |
| 48 |
* @return void |
| 49 |
*/ |
| 50 |
public function boot(): void |
| 51 |
{ |
| 52 |
Wishlist_DB::maybe_create_tables(); |
| 53 |
Wishlist_Settings::maybe_disable_archive_buttons(); |
| 54 |
$this->service = new Wishlist_Service(); |
| 55 |
$this->renderer = new Wishlist_Renderer($this->service); |
| 56 |
$this->woocommerce = new Wishlist_WooCommerce($this->service, $this->renderer); |
| 57 |
$this->woocommerce->hooks(); |
| 58 |
$this->frontend = new Wishlist_Frontend($this->service, $this->renderer); |
| 59 |
$this->frontend->hooks(); |
| 60 |
|
| 61 |
if (isset($_GET['ka_wishlist'])) { |
| 62 |
$wishlist_id = sanitize_title(wp_unslash($_GET['ka_wishlist'])); |
| 63 |
if (!empty($wishlist_id)) { |
| 64 |
$this->service->set_active_wishlist_id($wishlist_id); |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Merge guest wishlist into user account after login. |
| 71 |
* |
| 72 |
* @param string $user_login Username. |
| 73 |
* @param WP_User $user User object. |
| 74 |
* @return void |
| 75 |
*/ |
| 76 |
public function handle_login(string $user_login, WP_User $user): void |
| 77 |
{ |
| 78 |
if (!Wishlist_Settings::is_enabled()) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
$session = new Wishlist_Session(); |
| 83 |
$session_key = $session->get_session_key(false); |
| 84 |
if ($session_key === '') { |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
$service = new Wishlist_Service($user->ID, $session_key); |
| 89 |
$service->merge_guest_items($user->ID, $session_key); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Get wishlist service instance. |
| 94 |
* |
| 95 |
* @return Wishlist_Service Wishlist service. |
| 96 |
*/ |
| 97 |
public function service(): Wishlist_Service |
| 98 |
{ |
| 99 |
if (!$this->service instanceof Wishlist_Service) { |
| 100 |
$this->service = new Wishlist_Service(); |
| 101 |
} |
| 102 |
|
| 103 |
return $this->service; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Get wishlist renderer instance. |
| 108 |
* |
| 109 |
* @return Wishlist_Renderer Renderer instance. |
| 110 |
*/ |
| 111 |
public function renderer(): Wishlist_Renderer |
| 112 |
{ |
| 113 |
if (!$this->renderer instanceof Wishlist_Renderer) { |
| 114 |
$this->renderer = new Wishlist_Renderer($this->service()); |
| 115 |
} |
| 116 |
|
| 117 |
return $this->renderer; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Get frontend handler instance. |
| 122 |
* |
| 123 |
* @return Wishlist_Frontend Frontend instance. |
| 124 |
*/ |
| 125 |
public function frontend(): Wishlist_Frontend |
| 126 |
{ |
| 127 |
if (!$this->frontend instanceof Wishlist_Frontend) { |
| 128 |
$this->frontend = new Wishlist_Frontend($this->service(), $this->renderer()); |
| 129 |
} |
| 130 |
|
| 131 |
return $this->frontend; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|