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 |