assets
7 months ago
comparison-cookie.php
2 years ago
comparison-field-value.php
9 months ago
comparison-helper.php
3 years ago
comparison-share.php
3 years ago
comparison.php
3 years ago
group-meta.php
3 years ago
route.php
3 years ago
comparison.php
175 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ShopEngine\Modules\Comparison; |
| 4 | |
| 5 | use ShopEngine\Core\Register\Module_List; |
| 6 | use ShopEngine\Traits\Singleton; |
| 7 | use ShopEngine\Utils\Helper; |
| 8 | |
| 9 | class Comparison { |
| 10 | const COOKIE_KEY = 'shopengine_comparison_id_list'; |
| 11 | const ONCLICK_SELECTOR_CLS = 'shopengine_comparison_add_to_list_action'; |
| 12 | const COOKIE_TIME_IN_DAYS = 1; |
| 13 | |
| 14 | use Singleton; |
| 15 | |
| 16 | public function init() { |
| 17 | |
| 18 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Just checking current page |
| 19 | if(!empty($_REQUEST['shopengine_quickview'])) { |
| 20 | // In quickview modal we will not show anything |
| 21 | return; |
| 22 | } |
| 23 | |
| 24 | new Route(); |
| 25 | |
| 26 | $sett = Module_List::instance()->get_settings('comparison'); |
| 27 | $is_show_in_single = isset($sett['show_on_single_page']['value']) ? ($sett['show_on_single_page']['value'] === 'yes') : true; |
| 28 | |
| 29 | if($is_show_in_single === true) { |
| 30 | |
| 31 | if($this->get_where_to($sett) == 'after') { |
| 32 | |
| 33 | add_action('woocommerce_after_add_to_cart_button', [$this, 'print_the_button_in_single_page']); |
| 34 | |
| 35 | } else { |
| 36 | |
| 37 | add_action('woocommerce_before_add_to_cart_button', [$this, 'print_the_button_in_single_page']); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | $is_show = isset($sett['show_on_archive_page']['value']) ? ($sett['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 | |
| 51 | add_action('wp_enqueue_scripts', [$this, 'enqueue']); |
| 52 | |
| 53 | // Modal Wrapper |
| 54 | add_action( 'wp_footer', [$this, 'qc_modal_wrapper'] ); |
| 55 | |
| 56 | |
| 57 | add_action( 'woocommerce_admin_process_product_object', [new Group_Meta(), 'index'], 10, 1 ); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | public function qc_modal_wrapper() { |
| 62 | ?> |
| 63 | <div class="shopengine-comparison-modal se-modal-wrapper"> |
| 64 | <div class="se-modal-inner"></div> |
| 65 | </div> |
| 66 | <?php |
| 67 | } |
| 68 | |
| 69 | public function enqueue() { |
| 70 | // Comparison Styles |
| 71 | wp_enqueue_style('shopengine-comparison', \ShopEngine::module_url() . 'comparison/assets/css/comparison.css', ['shopengine-modal-styles']); |
| 72 | |
| 73 | // Comparison Scripts |
| 74 | wp_enqueue_script( |
| 75 | 'shopengine-comparison', |
| 76 | \ShopEngine::module_url() . 'comparison/assets/js/comparison.js', |
| 77 | ['jquery', 'shopengine-modal-script'], |
| 78 | \ShopEngine::version(), |
| 79 | true |
| 80 | ); |
| 81 | |
| 82 | |
| 83 | wp_localize_script('shopengine-comparison', 'shopEngineComparison', [ |
| 84 | 'product_id' => get_the_ID(), |
| 85 | 'resturl' => get_rest_url(), |
| 86 | 'rest_nonce' => wp_create_nonce('wp_rest'), |
| 87 | ]); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | public function get_where_to($settings = []) { |
| 92 | |
| 93 | $position = !empty($settings['show_icon_where_to']['value']) ? $settings['show_icon_where_to']['value'] : 'after'; |
| 94 | |
| 95 | return apply_filters('shopengine/module/comparison/put_icon_in_side', $position); |
| 96 | } |
| 97 | |
| 98 | |
| 99 | private function is_exists_in_list($idd) { |
| 100 | |
| 101 | if(empty($_COOKIE[Comparison::COOKIE_KEY])) { |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | $content = explode(',', sanitize_text_field( wp_unslash( $_COOKIE[Comparison::COOKIE_KEY] ) ) ); |
| 106 | |
| 107 | return in_array($idd, $content); |
| 108 | } |
| 109 | |
| 110 | |
| 111 | public function add_to_menu($menu_links) { |
| 112 | |
| 113 | $logout = $menu_links['customer-logout']; |
| 114 | |
| 115 | unset($menu_links['customer-logout']); |
| 116 | |
| 117 | $menu_links['wishlist'] = 'Wishlist'; |
| 118 | $menu_links['customer-logout'] = $logout; |
| 119 | |
| 120 | return $menu_links; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | public function print_the_button_in_single_page() { |
| 125 | |
| 126 | $left_text = apply_filters('shopengine/module/wishlist/optional_text_left', ''); |
| 127 | $right_text = apply_filters('shopengine/module/wishlist/optional_text_right', ''); |
| 128 | |
| 129 | $pid = get_the_ID(); |
| 130 | $exist = $this->is_exists_in_list($pid); |
| 131 | $cls = $exist ? 'active' : 'inactive'; |
| 132 | $compare_icon = '<i class="shopengine-icon-product_compare_1"></i>'; |
| 133 | |
| 134 | echo wp_kses($left_text, Helper::get_kses_array()); ?> |
| 135 | |
| 136 | <a |
| 137 | data-payload='{"pid":<?php echo intval($pid) ?>}' |
| 138 | class="<?php echo esc_attr(self::ONCLICK_SELECTOR_CLS) ?> shopengine-comparison badge <?php echo esc_attr($cls) ?>" |
| 139 | > <?php echo wp_kses($compare_icon, Helper::get_kses_array()) ?> </a><?php |
| 140 | |
| 141 | echo wp_kses($right_text, Helper::get_kses_array()); |
| 142 | } |
| 143 | |
| 144 | |
| 145 | function print_button_in_shop($add_to_cart_html, $product, $args = []) { |
| 146 | |
| 147 | $sett = Module_List::instance()->get_settings('comparison'); |
| 148 | |
| 149 | $left_text = apply_filters('shopengine/module/wishlist/optional_text_left', ''); |
| 150 | $right_text = apply_filters('shopengine/module/wishlist/optional_text_right', ''); |
| 151 | |
| 152 | $pid = $product->get_id(); |
| 153 | $exist = $this->is_exists_in_list($pid); |
| 154 | $cls = $exist ? 'active' : 'inactive'; |
| 155 | $compare_icon = '<i class="shopengine-icon-product_compare_1"></i>'; |
| 156 | |
| 157 | $btn = $left_text . |
| 158 | '<a data-payload=\'{"pid":' . $product->get_id() . '}\'' . |
| 159 | ' class="' . self::ONCLICK_SELECTOR_CLS . ' shopengine-comparison badge se-btn ' . esc_attr($cls) . '"> ' . $compare_icon . ' </a>' . |
| 160 | $right_text; |
| 161 | |
| 162 | |
| 163 | if($this->get_where_to() == 'after') { |
| 164 | $before = ''; |
| 165 | $after = $btn; |
| 166 | } else { |
| 167 | |
| 168 | $before = $btn; |
| 169 | $after = ''; |
| 170 | } |
| 171 | |
| 172 | return $before . $add_to_cart_html . $after; |
| 173 | } |
| 174 | } |
| 175 |