PluginProbe
Ever Compare – Products Compare Plugin for WooCommerce / 1.0.2
Ever Compare – Products Compare Plugin for WooCommerce v1.0.2
1.3.7 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 trunk 1.0.0 1.0.1 1.0.2 All 31 releases
ever-compare / includes / classes / Frontend / Manage_Compare.php

Manage_Compare.php in Ever Compare – Products Compare Plugin for WooCommerce 1.0.2, at includes/classes/Frontend/Manage_Compare.php

494 lines 16.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace EverCompare\Frontend;
3 /**
4 * Button handlers class
5 */
6 class Manage_Compare {
7
8 /**
9 * [$_instance]
10 * @var null
11 */
12 private static $_instance = null;
13
14 /**
15 * [instance] Initializes a singleton instance
16 * @return [Compare_Button]
17 */
18 public static function instance() {
19 if ( is_null( self::$_instance ) ) {
20 self::$_instance = new self();
21 }
22 return self::$_instance;
23 }
24
25 /**
26 * Initialize the class
27 */
28 private function __construct() {
29 add_action( 'init', [ $this, 'button_manager' ] );
30 }
31
32 /**
33 * [compare_button]
34 * @return [void]
35 */
36 public function button_print(){
37 echo do_shortcode( '[evercompare_button]' );
38 }
39
40 /**
41 * [button_manager] Button Manager
42 * @return [void]
43 */
44 public function button_manager(){
45 if( ever_compare_get_option( 'btn_show_shoppage', 'ever_compare_settings_tabs' ) === 'on' ){
46 add_action( 'woocommerce_after_shop_loop_item', [ $this, 'button_print' ], 20 );
47 }
48
49 if( ever_compare_get_option( 'btn_show_productpage', 'ever_compare_settings_tabs', 'on' ) === 'on' ){
50 add_action( 'woocommerce_single_product_summary', [ $this, 'button_print' ], 35 );
51 }
52
53 if( ever_compare_get_option( 'open_popup', 'ever_compare_settings_tabs', 'on' ) === 'on' ){
54 add_action( 'wp_footer', [ $this, 'pop_up_html' ] );
55 }
56 }
57
58 /**
59 * [button_html] Button HTML
60 * @param [type] $atts
61 * @return [HTML]
62 */
63 public function button_html( $atts ) {
64 $button_attr = apply_filters( 'evercompare_button_arg', $atts );
65 return ever_compare_get_template( 'evercompare-button-'.$atts['template_name'].'.php', $button_attr, false );
66 }
67
68 /**
69 * [table_html] Wishlist table HTML
70 * @return [HTML]
71 */
72 public function table_html( $atts ) {
73 $table_attr = apply_filters( 'evercompare_table_arg', $atts );
74 return ever_compare_get_template( 'evercompare-table.php', $table_attr, false );
75 }
76
77 /**
78 * [pop_up_html]
79 * @return [void]
80 */
81 public function pop_up_html(){
82 echo '<div class="htcompare-popup"><div class="htcompare-popup-content-area"><span class="htcompare-popup-close">&nbsp;</span>'.do_shortcode( '[evercompare_table]' ).'</div></div>';
83 }
84
85 /**
86 * [add_to_compare] Ajax callable function
87 */
88 public function add_to_compare( $id ) {
89
90 $cookie_name = $this->get_cookie_name();
91
92 if ( $this->is_product_in_compare( $id ) ) {
93 $this->compare_json_response();
94 }
95
96 $products = $this->get_compared_products();
97
98 $products[] = $id;
99
100 setcookie( $cookie_name, json_encode( $products ), 0, COOKIEPATH, COOKIE_DOMAIN, false, false );
101
102 $_COOKIE[$cookie_name] = json_encode( $products );
103
104 $this->compare_json_response();
105
106 }
107
108 /**
109 * [remove_from_compare] Ajax callable function
110 * @return [json]
111 */
112 public function remove_from_compare( $id ) {
113
114 $cookie_name = $this->get_cookie_name();
115
116 if ( ! $this->is_product_in_compare( $id ) ) {
117 $this->compare_json_response();
118 }
119
120 $products = $this->get_compared_products();
121
122 foreach ( $products as $prod_key => $product_id ) {
123 if ( intval( $id ) == $product_id ) {
124 unset( $products[ $prod_key ] );
125 }
126 }
127
128 if ( empty( $products ) ) {
129 setcookie( $cookie_name, false, 0, COOKIEPATH, COOKIE_DOMAIN, false, false );
130 $_COOKIE[$cookie_name] = false;
131 } else {
132 setcookie( $cookie_name, json_encode( $products ), 0, COOKIEPATH, COOKIE_DOMAIN, false, false );
133 $_COOKIE[$cookie_name] = json_encode( $products );
134 }
135
136 $this->compare_json_response();
137 }
138
139 /**
140 * [get_response_html ] Compare product table HTML
141 * @return [html]
142 */
143 public function get_response_html(){
144
145 $products = $this->get_compared_products_data();
146 $fields = $this->get_compare_fields();
147 $empty_compare_text = ever_compare_get_option('empty_table_text','ever_compare_table_settings_tabs');
148 $return_shop_button = ever_compare_get_option('shop_button_text','ever_compare_table_settings_tabs','Return to shop');
149
150 $default_atts = array(
151 'evercompare' => self::instance(),
152 'products' => $products,
153 'fields' => $fields,
154 'return_shop_button' => $return_shop_button,
155 'empty_compare_text' => $empty_compare_text,
156 );
157 $table_attr = apply_filters( 'evercompare_response_html_args', $default_atts );
158
159 ever_compare_get_template( 'evercompare-table.php', $table_attr );
160
161 }
162
163 /**
164 * [get_compared_products_data] generate compared products data
165 * @return [array] product list
166 */
167 public function get_compared_products_data() {
168 $ids = $this->get_compared_products();
169
170 if ( empty( $ids ) ) {
171 return array();
172 }
173
174 $args = array(
175 'include' => $ids,
176 );
177
178 $products = wc_get_products( $args );
179
180 $products_data = array();
181
182 $fields = $this->get_compare_fields();
183
184 $fields = array_filter( $fields, function( $field ) {
185 return 'pa_' === substr( $field, 0, 3 );
186 }, ARRAY_FILTER_USE_KEY );
187
188 $data_none = '-';
189
190 foreach ( $products as $product ) {
191
192 $rating_count = $product->get_rating_count();
193 $average = $product->get_average_rating();
194
195 $products_data[ $product->get_id() ] = array(
196 'primary' => array(
197 'image' => $product->get_image() ? $product->get_image('ever-compare-image') : $data_none,
198 ),
199 'id' => $product->get_id(),
200 'title' => $product->get_title() ? $product->get_title() : $data_none,
201 'image_id' => $product->get_image_id(),
202 'permalink' => $product->get_permalink(),
203 'price' => $product->get_price_html() ? $product->get_price_html() : $data_none,
204 'rating' => wc_get_rating_html( $average, $rating_count ),
205 'add_to_cart' => $this->add_to_cart_html( $product ) ? $this->add_to_cart_html( $product ) :$data_none,
206 'dimensions' => wc_format_dimensions( $product->get_dimensions( false ) ),
207 'description' => $product->get_short_description() ? $product->get_short_description() : $data_none,
208 'weight' => $product->get_weight() ? $product->get_weight() : $data_none,
209 'sku' => $product->get_sku() ? $product->get_sku() : $data_none,
210 'availability' => $this->availability_html( $product ),
211 );
212
213 foreach ( $fields as $field_id => $field_name ) {
214 if ( taxonomy_exists( $field_id ) ) {
215 $products_data[ $product->get_id() ][ $field_id ] = array();
216 $terms = get_the_terms( $product->get_id(), $field_id );
217 if ( ! empty( $terms ) ) {
218 foreach ( $terms as $term ) {
219 $term = sanitize_term( $term, $field_id );
220 $products_data[ $product->get_id() ][ $field_id ][] = $term->name;
221 }
222 } else {
223 $products_data[ $product->get_id() ][ $field_id ][] = '-';
224 }
225 $products_data[ $product->get_id() ][ $field_id ] = implode( ', ', $products_data[ $product->get_id() ][ $field_id ] );
226 }
227 }
228 }
229
230 return $products_data;
231 }
232
233 /**
234 * [get_compare_fields] Table field list
235 * @return [array] Table Field list
236 */
237 public function get_compare_fields() {
238 $fields = array(
239 'primary' => ''
240 );
241
242 $default_show = array(
243 'title' => esc_html__( 'title', 'ever-compare' ),
244 'ratting' => esc_html__( 'ratting', 'ever-compare' ),
245 'price' => esc_html__( 'price', 'ever-compare' ),
246 'add_to_cart' => esc_html__( 'add_to_cart', 'ever-compare' ),
247 'description' => esc_html__( 'description', 'ever-compare' ),
248 'availability' => esc_html__( 'availability', 'ever-compare' ),
249 'sku' => esc_html__( 'sku', 'ever-compare' ),
250 'weight' => esc_html__( 'weight', 'ever-compare' ),
251 'dimensions' => esc_html__( 'dimensions', 'ever-compare' ),
252 );
253
254 $fields_settings = !empty( ever_compare_get_option( 'show_fields', 'ever_compare_table_settings_tabs' ) ) ? ever_compare_get_option( 'show_fields', 'ever_compare_table_settings_tabs' ) : array();
255
256 if ( isset( $fields_settings ) && count( $fields_settings ) > 1 ) {
257 $fields = $fields + $fields_settings;
258 }else{
259 $fields = $fields + $default_show;
260 }
261
262 return $fields;
263 }
264
265 /**
266 * [is_products_have_field]
267 * @param [string] $field_id
268 * @param [object] $products
269 * @return boolean
270 */
271 public function is_products_have_field( $field_id, $products ) {
272 foreach ( $products as $product_id => $product ) {
273 if ( isset( $product[ $field_id ] ) && ( ! empty( $product[ $field_id ] ) && '-' !== $product[ $field_id ] && 'N/A' !== $product[ $field_id ] ) ) {
274 return true;
275 }
276 }
277 return false;
278 }
279
280 /**
281 * [compare_display_field]
282 * @param [string] $field_id
283 * @param [array] $product
284 * @return [html]
285 */
286 public function compare_display_field( $field_id, $product ) {
287
288 $type = $field_id;
289
290 if ( 'pa_' === substr( $field_id, 0, 3 ) ) {
291 $type = 'attribute';
292 }
293
294 switch ( $type ) {
295 case 'primary':
296 ?>
297 <div class="htcompare-primary-content-area">
298 <a href="#" class="htcompare-remove" data-product_id="<?php echo esc_attr( $product['id'] ); ?>">&nbsp;</a>
299 <a href="<?php echo get_permalink( $product['id'] ); ?>" class="htcompare-product-image">
300 <?php echo $product['primary']['image']; ?>
301 </a>
302 </div>
303 <?php
304 break;
305
306 case 'title':
307 echo '<a href="'.get_permalink( $product['id'] ).'" class="htcompare-product-title">'.$product[ $field_id ].'</a>';
308 break;
309
310 case 'ratting':
311 echo '<span class="htcompare-product-ratting">'.wp_kses_post( $product[ $field_id ] ).'</span>';
312 break;
313
314 case 'price':
315 echo '<div class="htcompare-product-price">'.wp_kses_post( $product[ $field_id ] ).'</div>';
316 break;
317
318 case 'add_to_cart':
319 echo apply_filters( 'htcompare_add_to_cart_btn', $product[ $field_id ] );
320 break;
321
322 case 'attribute':
323 echo wp_kses_post( $product[ $field_id ] );
324 break;
325
326 case 'weight':
327 if ( $product[ $field_id ] ) {
328 $unit = $product[ $field_id ] !== '-' ? get_option( 'woocommerce_weight_unit' ) : '';
329 echo wc_format_localized_decimal( $product[ $field_id ] ) . ' ' . esc_attr( $unit );
330 }
331 break;
332
333 case 'description':
334 echo apply_filters( 'woocommerce_short_description', $product[ $field_id ] );
335 break;
336
337 default:
338 echo wp_kses_post( $product[ $field_id ] );
339 break;
340 }
341 }
342
343 /**
344 * [add_to_cart_html] Generate Cart button
345 * @param [object] $product
346 */
347 public function add_to_cart_html( $product ) {
348 if ( ! $product ) return;
349
350 $defaults = array(
351 'quantity' => 1,
352 'class' => implode( ' ', array_filter( array(
353 'htcompare-cart-button button',
354 'product_type_' . $product->get_type(),
355 $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
356 $product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ? 'ajax_add_to_cart' : '',
357 ) ) ),
358 'attributes' => array(
359 'data-product_id' => $product->get_id(),
360 'data-product_sku' => $product->get_sku(),
361 'aria-label' => $product->add_to_cart_description(),
362 'rel' => 'nofollow',
363 ),
364 );
365
366 $args = apply_filters( 'woocommerce_loop_add_to_cart_args', $defaults, $product );
367
368 if ( isset( $args['attributes']['aria-label'] ) ) {
369 $args['attributes']['aria-label'] = strip_tags( $args['attributes']['aria-label'] );
370 }
371
372 return apply_filters( 'woocommerce_loop_add_to_cart_link',
373 sprintf( '<a href="%s" data-quantity="%s" class="%s add-to-cart-loop" %s><span>%s</span></a>',
374 esc_url( $product->add_to_cart_url() ),
375 esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
376 esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
377 isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
378 esc_html( $product->add_to_cart_text() )
379 ),
380 $product, $args );
381 }
382
383 /**
384 * [availability_html]
385 * @param [object] $product
386 * @return [html]
387 */
388 public function availability_html( $product ) {
389 $html = '';
390 $availability = $product->get_availability();
391
392 if( empty( $availability['availability'] ) ) {
393 $availability['availability'] = __( 'In stock', 'woocommerce' );
394 }
395
396 if ( ! empty( $availability['availability'] ) ) {
397 ob_start();
398
399 wc_get_template( 'single-product/stock.php', array(
400 'product' => $product,
401 'class' => $availability['class'],
402 'availability' => $availability['availability'],
403 ) );
404
405 $html = ob_get_clean();
406 }
407
408 return apply_filters( 'woocommerce_get_stock_html', $html, $product );
409 }
410
411 /**
412 * [compare_cookie_name] Get Compare cookie name
413 * @return [string]
414 */
415 public function get_cookie_name() {
416 $name = 'ever_compare_compare_list';
417 if ( is_multisite() ){
418 $name .= '_' . get_current_blog_id();
419 }
420 return $name;
421 }
422
423 /**
424 * [is_product_in_compare]
425 * @param [int] $id product id
426 * @return [array] product id list
427 */
428 public function is_product_in_compare( $id ) {
429 $list = $this->get_compared_products();
430 return in_array( $id, $list, true );
431 }
432
433 /**
434 * [get_compared_products]
435 * @return [json]
436 */
437 public function get_compared_products() {
438 $cookie_name = $this->get_cookie_name();
439 return isset( $_COOKIE[ $cookie_name ] ) ? json_decode( wp_unslash( $_COOKIE[ $cookie_name ] ), true ) : array();
440 }
441
442 /**
443 * [compare_json_response]
444 * @return [json] product json
445 */
446 public function compare_json_response() {
447 $count = 0;
448 $products = $this->get_compared_products();
449
450 ob_start();
451
452 $this->get_response_html();
453
454 $table_html = ob_get_clean();
455
456 if ( is_array( $products ) ) {
457 $count = count( $products );
458 }
459
460 wp_send_json( array(
461 'count' => $count,
462 'table' => $table_html,
463 ) );
464
465 }
466
467 /**
468 * [get_compare_page_url] Compare Page Link
469 * @return [URL]
470 */
471 public function get_compare_page_url() {
472 $page_id = ever_compare_get_option( 'compare_page', 'ever_compare_table_settings_tabs' );
473 return get_permalink( $page_id );
474 }
475
476 /**
477 * [field_name]
478 * @param [string] $field
479 * @return [string]
480 */
481 public function field_name( $field ){
482 $str = substr( $field, 0, 3 );
483 if( 'pa_' === $str ){
484 $field_name = wc_attribute_taxonomy_slug( $field );
485 }else{
486 $field_name = $field;
487 }
488 $field_name = explode('_', $field_name );
489 $field_name = ucfirst( implode(' ', $field_name ) );
490 return $field_name;
491 }
492
493
494 }