PluginProbe ʕ •ᴥ•ʔ
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution / 4.8.7
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution v4.8.7
4.9.1 4.9.0 2.0.0 2.1.0 2.2.0 2.2.1 2.2.2 2.3.0 2.4.0 2.5.0 2.5.1 3.0.0 3.1.0 3.1.1 4.0.0 4.0.1 4.1.0 4.1.1 4.2.0 4.2.1 4.3.0 4.3.1 4.4.0 4.5.0 4.5.1 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.6.5 4.6.6 4.6.7 4.6.8 4.6.9 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.7.5 4.7.6 4.7.7 4.7.8 4.7.9 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5 4.8.6 4.8.7 4.8.8 4.8.9 trunk 0.1.2-beta 0.1.3-beta 0.1.4-beta 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.5.0 1.5.1 1.6.0 1.6.1 1.7.0 1.8.0 1.8.1 1.9.0
shopengine / modules / wishlist / wishlist.php
shopengine / modules / wishlist Last commit date
assets 1 year ago screens 3 years ago route.php 7 months ago wishlist.php 3 years ago
wishlist.php
214 lines
1 <?php
2
3 namespace ShopEngine\Modules\Wishlist;
4
5 use ShopEngine\Core\Register\Module_List;
6 use ShopEngine\Traits\Singleton;
7 use ShopEngine\Utils\Helper;
8
9 class Wishlist {
10
11 const COOKIE_KEY = 'shopengine_wishlist_offline';
12 const UMK_WISHLIST = 'shopengine_wishlist';
13 const ONCLICK_SELECTOR_CLS = 'shopengine_add_to_list_action';
14
15 /**
16 * @var array
17 */
18 public $settings = [];
19
20 use Singleton;
21
22 public function init() {
23
24 new Route();
25
26 $this->settings = Module_List::instance()->get_settings('wishlist');
27 $is_show_in_single = isset($this->settings['show_on_single_page']['value']) ? ($this->settings['show_on_single_page']['value'] === 'yes') : true;
28
29 if ($is_show_in_single === true) {
30
31 if ($this->get_where_to($this->settings) === 'after') {
32
33 add_action('woocommerce_after_add_to_cart_button', [$this, 'print_wish_button'], 10, 0);
34
35 } else {
36
37 add_action('woocommerce_before_add_to_cart_button', [$this, 'print_wish_button'], 10, 0);
38 }
39 }
40
41 $is_show = isset($this->settings['show_on_archive_page']['value']) ? ($this->settings['show_on_archive_page']['value'] === 'yes') : true;
42
43 $show_in_archive = apply_filters('shopengine/module/wishlist/show_icon_in_shop_page', $is_show);
44
45 if ($show_in_archive === true) {
46
47 add_filter('woocommerce_loop_add_to_cart_link', [$this, 'print_button_in_shop'], 10, 3);
48 }
49
50 add_action('wp_enqueue_scripts', [$this, 'enqueue']);
51
52 if (is_user_logged_in()) {
53
54 if (!empty($_COOKIE[Wishlist::COOKIE_KEY])) {
55
56 $uid = get_current_user_id();
57
58 $content = get_user_meta($uid, self::UMK_WISHLIST, true);
59 $content = empty($content) ? [] : $content;
60
61 $cck = explode(',', sanitize_text_field( wp_unslash( $_COOKIE[Wishlist::COOKIE_KEY] ) ));
62
63 foreach ($cck as $pid) {
64
65 $content[$pid] = $pid;
66 }
67
68 update_user_meta($uid, self::UMK_WISHLIST, $content);
69
70 setcookie(Wishlist::COOKIE_KEY, '', time() - 3600, '/' );
71 }
72 }
73
74 add_action('init', function () {
75 add_rewrite_endpoint('wishlist', EP_ROOT | EP_PAGES);
76 });
77
78 add_filter('woocommerce_account_menu_items', [$this, 'add_to_menu'], 40);
79 add_action('woocommerce_account_wishlist_endpoint', [$this, 'wishlist_content']);
80 }
81
82 public function enqueue() {
83
84 wp_enqueue_style('shopengine-wishlist', plugin_dir_url(__FILE__) . 'assets/css/wishlist.css', [], \ShopEngine::version());
85
86 wp_enqueue_script('shopengine-wishlist', plugin_dir_url(__FILE__) . 'assets/js/wishlist.js', ['jquery']);
87
88 wp_localize_script('shopengine-wishlist', 'shopEngineWishlist', [
89 'product_id' => get_the_ID(),
90 'resturl' => get_rest_url(),
91 'isLoggedIn' => is_user_logged_in(),
92 'rest_nonce' => wp_create_nonce('wp_rest'),
93 'wishlist_position' => isset($this->settings['position']['value']) ? $this->settings['position']['value'] : 'bottom-right',
94 'wishlist_added_notice' => esc_html__('Your product is added to wishlist', 'shopengine'),
95 'wishlist_removed_notice' => esc_html__('Your product is removed from wishlist', 'shopengine')
96 ]);
97 }
98
99 /**
100 *
101 * @return mixed - only other exact value we are expecting is after!
102 */
103 public function get_where_to() {
104
105 $position = !empty($this->settings['show_icon_where_to']['value']) ? $this->settings['show_icon_where_to']['value'] : 'before';
106
107 return apply_filters('shopengine/module/wishlist/put_icon_in_side', $position);
108 }
109
110 /**
111 * @param $idd
112 */
113 private function is_exists_in_wishlist($idd) {
114
115 if (is_user_logged_in()) {
116
117 $content = get_user_meta(get_current_user_id(), self::UMK_WISHLIST, true);
118
119 return isset($content[$idd]);
120 }
121
122 if (empty($_COOKIE[Wishlist::COOKIE_KEY])) {
123 return false;
124 }
125
126 $content = explode(',', sanitize_text_field(wp_unslash($_COOKIE[Wishlist::COOKIE_KEY])));
127
128 return in_array($idd, $content);
129 }
130
131 public function print_wish_button() {
132
133 $pid = get_the_ID();
134 $exist = $this->is_exists_in_wishlist($pid);
135 $cls = $exist ? 'active' : 'inactive';
136
137 $left_text = apply_filters('shopengine/module/wishlist/optional_text_left', '');
138 $right_text = apply_filters('shopengine/module/wishlist/optional_text_right', '');
139
140 ?>
141
142 <button title="<?php esc_html_e('Add to Wishlist','shopengine')?>" class="<?php echo esc_attr(self::ONCLICK_SELECTOR_CLS) ?> shopengine-wishlist badge <?php echo esc_attr($cls) ?>" data-pid="<?php echo intval($pid) ?>">
143 <?php echo wp_kses($left_text, Helper::get_kses_array()) ?>
144 <i class="shopengine-icon-add_to_favourite_1"></i>
145 <?php echo wp_kses($right_text, Helper::get_kses_array()) ?>
146 </button>
147
148 <?php
149 }
150
151 /**
152 * @param $add_to_cart_html
153 * @param $product
154 * @param array $args
155 * @return mixed
156 */
157 function print_button_in_shop($add_to_cart_html, $product, $args = []) {
158
159 $this->settings = Module_List::instance()->get_settings('wishlist');
160
161 $exist = $this->is_exists_in_wishlist($product->get_id());
162 $cls = $exist ? 'active' : 'inactive';
163
164 $left_text = apply_filters('shopengine/module/wishlist/optional_text_left', '');
165 $right_text = apply_filters('shopengine/module/wishlist/optional_text_right', '');
166
167 $btn = '<a data-pid="' . $product->get_id() . '" class="' . self::ONCLICK_SELECTOR_CLS . ' shopengine-wishlist badge se-btn ' . esc_attr($cls) . '" href="#" >' .$left_text .
168 '<i class="shopengine-icon-add_to_favourite_1"></i>' .
169 $right_text . '</a>';
170
171 $button_content = apply_filters('shopengine_wishlist_button_content', $btn);
172
173 if ($this->get_where_to($this->settings) == 'after') {
174
175 $before = '';
176 $after = $button_content;
177
178 } else {
179
180 $before = $button_content;
181 $after = '';
182 }
183
184 return $before . $add_to_cart_html . $after;
185 }
186
187 /**
188 * @param $menu_links
189 * @return mixed
190 */
191 public function add_to_menu($menu_links) {
192
193 $menu_links['wishlist'] = esc_html__('Wishlist', 'shopengine');
194
195 if (isset($menu_links['customer-logout'])) {
196
197 $logout = $menu_links['customer-logout'];
198 unset($menu_links['customer-logout']);
199 $menu_links['customer-logout'] = $logout;
200 } else {
201 $menu_links['customer-logout'] = esc_html__('Logout', 'shopengine');
202 }
203
204 return $menu_links;
205 }
206
207 public function wishlist_content() {
208
209 $list = get_user_meta(get_current_user_id(), Wishlist::UMK_WISHLIST, true);
210
211 include __DIR__ . '/screens/default.php';
212 }
213 }
214