PluginProbe
Ever Compare – Products Compare Plugin for WooCommerce / 1.1.9
Ever Compare – Products Compare Plugin for WooCommerce v1.1.9
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.1.9, at includes/classes/Frontend/Manage_Compare.php

669 lines 23.7 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 * [$reached_max_limit]
16 * @var boolean
17 */
18 public $reached_max_limit = false;
19
20 /**
21 * [instance] Initializes a singleton instance
22 * @return [Compare_Button]
23 */
24 public static function instance() {
25 if ( is_null( self::$_instance ) ) {
26 self::$_instance = new self();
27 }
28 return self::$_instance;
29 }
30
31 /**
32 * Initialize the class
33 */
34 private function __construct() {
35 add_action( 'init', [ $this, 'button_manager' ] );
36 add_action( 'ever_compare_before_table', [ $this, 'reached_max_limit_message' ], 1 );
37 add_action( 'ever_compare_after_table', [ $this, 'shareable_link' ], 1 );
38 }
39
40 /**
41 * [compare_button]
42 * @return [void]
43 */
44 public function button_print(){
45 echo do_shortcode( '[evercompare_button]' );
46 }
47
48 /**
49 * [button_manager] Button Manager
50 * @return [void]
51 */
52 public function button_manager(){
53
54 $enable_btn = ever_compare_get_option( 'btn_show_shoppage', 'ever_compare_settings_tabs', 'off' );
55 $product_enable_btn = ever_compare_get_option( 'btn_show_productpage', 'ever_compare_settings_tabs', 'on' );
56
57 $shop_page_btn_position = ever_compare_get_option( 'shop_btn_position', 'ever_compare_settings_tabs', 'after_cart_btn' );
58 $product_page_btn_position = ever_compare_get_option( 'product_btn_position', 'ever_compare_settings_tabs', 'after_cart_btn' );
59
60
61 // Shop Button Position
62 if( $shop_page_btn_position != 'use_shortcode' && $enable_btn == 'on' ){
63 switch ( $shop_page_btn_position ) {
64
65 case 'before_cart_btn':
66 add_action( 'woocommerce_after_shop_loop_item', [ $this, 'button_print' ], 7 );
67 break;
68
69 case 'top_thumbnail':
70 add_action( 'woocommerce_before_shop_loop_item', [ $this, 'button_print' ], 5 );
71 break;
72
73 case 'custom_position':
74 $hook_name = ever_compare_get_option( 'shop_custom_hook_name', 'ever_compare_settings_tabs', '' );
75 $priority = ever_compare_get_option( 'shop_custom_hook_priority', 'ever_compare_settings_tabs', 10 );
76 if( !empty( $hook_name ) ){
77 add_action( $hook_name, [ $this, 'button_print' ], $priority );
78 }
79 break;
80
81 default:
82 add_action( 'woocommerce_after_shop_loop_item', [ $this, 'button_print' ], 20 );
83 break;
84
85 }
86 }
87
88 // Product Page Button Position
89 if( $product_page_btn_position != 'use_shortcode' && $product_enable_btn == 'on' ){
90 switch ( $product_page_btn_position ) {
91
92 case 'before_cart_btn':
93 add_action( 'woocommerce_before_add_to_cart_button', [ $this, 'button_print' ], 20 );
94 break;
95
96 case 'after_thumbnail':
97 add_action( 'woocommerce_product_thumbnails', [ $this, 'button_print' ], 21 );
98 break;
99
100 case 'after_summary':
101 add_action( 'woocommerce_after_single_product_summary', [ $this, 'button_print' ], 11 );
102 break;
103
104 case 'custom_position':
105 $hook_name = ever_compare_get_option( 'product_custom_hook_name', 'ever_compare_settings_tabs', '' );
106 $priority = ever_compare_get_option( 'product_custom_hook_priority', 'ever_compare_settings_tabs', 10 );
107 if( !empty( $hook_name ) ){
108 add_action( $hook_name, [ $this, 'button_print' ], $priority );
109 }
110 break;
111
112 default:
113 add_action( 'woocommerce_single_product_summary', [ $this, 'button_print' ], 31 );
114 break;
115 }
116 }
117
118 /**
119 * Popup HTML Render
120 */
121 if( ever_compare_get_option( 'open_popup', 'ever_compare_settings_tabs', 'on' ) === 'on' ){
122 add_action( 'wp_footer', [ $this, 'pop_up_html' ] );
123 }
124
125 /**
126 * Manage maximum product added message
127 */
128 if( isset( $_COOKIE[ 'ever_compare_max_limit' ] ) && $_COOKIE[ 'ever_compare_max_limit' ] == 'yes' ) {
129 setcookie( 'ever_compare_max_limit', 'no', 0, COOKIEPATH, COOKIE_DOMAIN, false, false );
130 $this->reached_max_limit = true;
131 }
132
133
134 }
135
136 /**
137 * [button_html] Button HTML
138 * @param [type] $atts
139 * @return [HTML]
140 */
141 public function button_html( $atts ) {
142 $button_attr = apply_filters( 'evercompare_button_arg', $atts );
143 return ever_compare_get_template( 'evercompare-button-'.$atts['template_name'].'.php', $button_attr, false );
144 }
145
146 /**
147 * [table_html] Wishlist table HTML
148 * @return [HTML]
149 */
150 public function table_html( $atts ) {
151 $table_attr = apply_filters( 'evercompare_table_arg', $atts );
152 return ever_compare_get_template( 'evercompare-table.php', $table_attr, false );
153 }
154
155 /**
156 * [pop_up_html]
157 * @return [void]
158 */
159 public function pop_up_html(){
160 echo '<div class="htcompare-popup"><div class="htcompare-popup-content-area"><span class="htcompare-popup-close">&nbsp;</span>'.do_shortcode( '[evercompare_table]' ).'</div></div>';
161 }
162
163 /**
164 * [reached_max_limit_message]
165 * @return [void]
166 */
167 public function reached_max_limit_message(){
168 if( ! $this->reached_max_limit ) {
169 return;
170 }
171
172 $message = !empty( ever_compare_get_option('reached_max_limit_message','ever_compare_table_settings_tabs') ) ? ever_compare_get_option('reached_max_limit_message','ever_compare_table_settings_tabs') : esc_html__( 'You are already added the maximum number of products.', 'ever-compare' );
173 echo '<div class="ever-compare-message-error"><p>'. wp_kses_post( $message ).'</p></div>';
174
175 }
176
177 /**
178 * [shareable_link]
179 * @return [void]
180 */
181 public function shareable_link(){
182
183 $shareable_link = ever_compare_get_option( 'enable_shareable_link','ever_compare_table_settings_tabs','off' );
184 if ( $shareable_link !== 'on' ) {
185 return;
186 }
187
188 $ids = $this->get_compared_products();
189
190 if ( isset( $_GET['evcompare'] ) ) {
191 $query_perametter_ids = sanitize_text_field( $_GET['evcompare'] );
192 if( !empty( $query_perametter_ids ) ){
193 $ids = explode( ',', $query_perametter_ids );
194 }
195 }
196
197 $buttonText = ever_compare_get_option( 'shareable_link_button_text','ever_compare_table_settings_tabs','Copy shareable link' ) ? ever_compare_get_option( 'shareable_link_button_text','ever_compare_table_settings_tabs','Copy shareable link' ) : esc_html__( 'Copy shareable link', 'ever-compare' );
198 $aftercopy_buttonText = ever_compare_get_option( 'shareable_link_after_button_text','ever_compare_table_settings_tabs','Copied' ) ? ever_compare_get_option( 'shareable_link_after_button_text','ever_compare_table_settings_tabs','Copied' ) : esc_html__( 'Copied', 'ever-compare' );
199 $button_pos = ever_compare_get_option( 'linkshare_btn_pos','ever_compare_table_settings_tabs','right' );
200
201 $idsString = is_array( $ids ) ? implode( ',', $ids ) : '';
202 $shareablelink = $this->get_compare_page_url() . '?evcompare='.$idsString;
203 ?>
204 <div class="ever-compare-shareable-link <?php echo esc_attr( $button_pos );?>">
205 <button class="evercompare-copy-link" data-copytext="<?php echo esc_attr( $aftercopy_buttonText ); ?>" data-btntext="<?php echo esc_attr( $buttonText ); ?>"><?php echo $buttonText; ?></button>
206 <p style="display: none;" class="evercompare-share-link"><?php echo $shareablelink; ?></p>
207 </div>
208 <?php
209
210 }
211
212 /**
213 * [add_to_compare] Ajax callable function
214 */
215 public function add_to_compare( $id ) {
216
217 $cookie_name = $this->get_cookie_name();
218
219 if ( $this->is_product_in_compare( $id ) ) {
220 $this->compare_json_response();
221 }
222
223 $products = $this->get_compared_products();
224
225 // Reached Maximum Limit
226 if( $this->reached_max_limit() ) {
227 setcookie( 'ever_compare_max_limit', 'yes', 0, COOKIEPATH, COOKIE_DOMAIN, false, false );
228 $this->compare_json_response();
229 }
230
231 $products[] = $id;
232
233 setcookie( $cookie_name, json_encode( $products ), 0, COOKIEPATH, COOKIE_DOMAIN, false, false );
234
235 $_COOKIE[$cookie_name] = json_encode( $products );
236
237 $this->compare_json_response();
238
239 }
240
241 /**
242 * [reached_max_limit]
243 * @return [bool]
244 */
245 public function reached_max_limit(){
246
247 $products = $this->get_compared_products();
248 $max_limit = intval( ever_compare_get_option( 'limit','ever_compare_table_settings_tabs', 10 ) );
249
250 if( $max_limit && count( $products ) >= $max_limit ) {
251 $this->reached_max_limit = true;
252 } else {
253 $this->reached_max_limit = false;
254 }
255
256 return $this->reached_max_limit;
257 }
258
259 /**
260 * [remove_from_compare] Ajax callable function
261 * @return [json]
262 */
263 public function remove_from_compare( $id ) {
264
265 $cookie_name = $this->get_cookie_name();
266
267 if ( ! $this->is_product_in_compare( $id ) ) {
268 $this->compare_json_response();
269 }
270
271 $products = $this->get_compared_products();
272
273 foreach ( $products as $prod_key => $product_id ) {
274 if ( intval( $id ) == $product_id ) {
275 unset( $products[ $prod_key ] );
276 $this->reached_max_limit = false;
277 }
278 }
279
280 if ( empty( $products ) ) {
281 setcookie( $cookie_name, false, 0, COOKIEPATH, COOKIE_DOMAIN, false, false );
282 $_COOKIE[$cookie_name] = false;
283 } else {
284 setcookie( $cookie_name, json_encode( $products ), 0, COOKIEPATH, COOKIE_DOMAIN, false, false );
285 $_COOKIE[$cookie_name] = json_encode( $products );
286 }
287
288 $this->compare_json_response();
289 }
290
291 /**
292 * [get_response_html ] Compare product table HTML
293 * @return [html]
294 */
295 public function get_response_html(){
296
297 $products = $this->get_compared_products_data();
298 $fields = $this->get_compare_fields();
299
300 $empty_compare_text = ever_compare_get_option('empty_table_text','ever_compare_table_settings_tabs');
301 $return_shop_button = ever_compare_get_option('shop_button_text','ever_compare_table_settings_tabs','Return to shop');
302
303 $custom_heading = !empty( ever_compare_get_option( 'table_heading', 'ever_compare_table_settings_tabs' ) ) ? ever_compare_get_option( 'table_heading', 'ever_compare_table_settings_tabs' ) : array();
304
305 $default_atts = array(
306 'evercompare' => self::instance(),
307 'products' => $products,
308 'fields' => $fields,
309 'heading_txt' => $custom_heading,
310 'return_shop_button' => $return_shop_button,
311 'empty_compare_text' => $empty_compare_text,
312 );
313 $table_attr = apply_filters( 'evercompare_response_html_args', $default_atts );
314
315 ever_compare_get_template( 'evercompare-table.php', $table_attr );
316
317 }
318
319 /**
320 * [get_compared_products_data] generate compared products data
321 * @return [array] product list
322 */
323 public function get_compared_products_data() {
324 $ids = $this->get_compared_products();
325
326 // For shareable link
327 $shareable_link = ever_compare_get_option('enable_shareable_link','ever_compare_table_settings_tabs','off');
328 if ( ( $shareable_link === 'on' ) && isset( $_GET['evcompare'] ) ) {
329 $query_perametter_ids = sanitize_text_field( $_GET['evcompare'] );
330 if( !empty( $query_perametter_ids ) ){
331 $ids = explode( ',', $query_perametter_ids );
332 }
333 }
334
335 if ( empty( $ids ) ) {
336 return array();
337 }
338
339 $args = array(
340 'include' => $ids,
341 );
342
343 $products = wc_get_products( $args );
344
345 $products_data = array();
346
347 $fields = $this->get_compare_fields();
348
349 $fields = array_filter( $fields, function( $field ) {
350 return 'pa_' === substr( $field, 0, 3 );
351 }, ARRAY_FILTER_USE_KEY );
352
353 $data_none = '-';
354
355 foreach ( $products as $product ) {
356
357 $rating_count = $product->get_rating_count();
358 $average = $product->get_average_rating();
359
360 $products_data[ $product->get_id() ] = array(
361 'primary' => array(
362 'image' => $product->get_image() ? $product->get_image('ever-compare-image') : $data_none,
363 ),
364 'id' => $product->get_id(),
365 'title' => $product->get_title() ? $product->get_title() : $data_none,
366 'image_id' => $product->get_image_id(),
367 'permalink' => $product->get_permalink(),
368 'price' => $product->get_price_html() ? $product->get_price_html() : $data_none,
369 'rating' => wc_get_rating_html( $average, $rating_count ),
370 'add_to_cart' => $this->add_to_cart_html( $product ) ? $this->add_to_cart_html( $product ) :$data_none,
371 'dimensions' => wc_format_dimensions( $product->get_dimensions( false ) ),
372 'description' => $product->get_short_description() ? $product->get_short_description() : $data_none,
373 'weight' => $product->get_weight() ? $product->get_weight() : $data_none,
374 'sku' => $product->get_sku() ? $product->get_sku() : $data_none,
375 'availability' => $this->availability_html( $product ),
376 );
377
378 foreach ( $fields as $field_id => $field_name ) {
379 if ( taxonomy_exists( $field_id ) ) {
380 $products_data[ $product->get_id() ][ $field_id ] = array();
381 $terms = get_the_terms( $product->get_id(), $field_id );
382 if ( ! empty( $terms ) ) {
383 foreach ( $terms as $term ) {
384 $term = sanitize_term( $term, $field_id );
385 $products_data[ $product->get_id() ][ $field_id ][] = $term->name;
386 }
387 } else {
388 $products_data[ $product->get_id() ][ $field_id ][] = '-';
389 }
390 $products_data[ $product->get_id() ][ $field_id ] = implode( ', ', $products_data[ $product->get_id() ][ $field_id ] );
391 }
392 }
393 }
394
395 return $products_data;
396 }
397
398 /**
399 * [get_compare_fields] Table field list
400 * @return [array] Table Field list
401 */
402 public function get_compare_fields() {
403 $fields = array(
404 'primary' => ''
405 );
406
407 $default_show = array(
408 'title' => esc_html__( 'title', 'ever-compare' ),
409 'ratting' => esc_html__( 'ratting', 'ever-compare' ),
410 'price' => esc_html__( 'price', 'ever-compare' ),
411 'add_to_cart' => esc_html__( 'add_to_cart', 'ever-compare' ),
412 'description' => esc_html__( 'description', 'ever-compare' ),
413 'availability' => esc_html__( 'availability', 'ever-compare' ),
414 'sku' => esc_html__( 'sku', 'ever-compare' ),
415 'weight' => esc_html__( 'weight', 'ever-compare' ),
416 'dimensions' => esc_html__( 'dimensions', 'ever-compare' ),
417 );
418
419 $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();
420
421 if ( isset( $fields_settings ) && count( $fields_settings ) > 1 ) {
422 $fields = $fields + $fields_settings;
423 }else{
424 $fields = $fields + $default_show;
425 }
426
427 return $fields;
428 }
429
430 /**
431 * [is_products_have_field]
432 * @param [string] $field_id
433 * @param [object] $products
434 * @return boolean
435 */
436 public function is_products_have_field( $field_id, $products ) {
437 foreach ( $products as $product_id => $product ) {
438 if ( isset( $product[ $field_id ] ) && ( ! empty( $product[ $field_id ] ) && '-' !== $product[ $field_id ] && 'N/A' !== $product[ $field_id ] ) ) {
439 return true;
440 }
441 }
442 return false;
443 }
444
445 /**
446 * [compare_display_field]
447 * @param [string] $field_id
448 * @param [array] $product
449 * @return [html]
450 */
451 public function compare_display_field( $field_id, $product ) {
452
453 $type = $field_id;
454
455 if ( 'pa_' === substr( $field_id, 0, 3 ) ) {
456 $type = 'attribute';
457 }
458
459 switch ( $type ) {
460 case 'primary':
461 ?>
462 <div class="htcompare-primary-content-area">
463 <a href="#" class="htcompare-remove" data-product_id="<?php echo esc_attr( $product['id'] ); ?>">&nbsp;</a>
464 <a href="<?php echo get_permalink( $product['id'] ); ?>" class="htcompare-product-image">
465 <?php echo $product['primary']['image']; ?>
466 </a>
467 </div>
468 <?php
469 break;
470
471 case 'title':
472 echo '<a href="'.get_permalink( $product['id'] ).'" class="htcompare-product-title">'.$product[ $field_id ].'</a>';
473 break;
474
475 case 'ratting':
476 echo '<span class="htcompare-product-ratting">'.wp_kses_post( $product[ $field_id ] ).'</span>';
477 break;
478
479 case 'price':
480 echo '<div class="htcompare-product-price">'.wp_kses_post( $product[ $field_id ] ).'</div>';
481 break;
482
483 case 'add_to_cart':
484 echo apply_filters( 'htcompare_add_to_cart_btn', $product[ $field_id ] );
485 break;
486
487 case 'attribute':
488 echo wp_kses_post( $product[ $field_id ] );
489 break;
490
491 case 'weight':
492 if ( $product[ $field_id ] ) {
493 $unit = $product[ $field_id ] !== '-' ? get_option( 'woocommerce_weight_unit' ) : '';
494 echo wc_format_localized_decimal( $product[ $field_id ] ) . ' ' . esc_attr( $unit );
495 }
496 break;
497
498 case 'description':
499 echo apply_filters( 'woocommerce_short_description', $product[ $field_id ] );
500 break;
501
502 default:
503 echo wp_kses_post( $product[ $field_id ] );
504 break;
505 }
506 }
507
508 /**
509 * [add_to_cart_html] Generate Cart button
510 * @param [object] $product
511 */
512 public function add_to_cart_html( $product ) {
513 if ( ! $product ) return;
514
515 $defaults = array(
516 'quantity' => 1,
517 'class' => implode( ' ', array_filter( array(
518 'htcompare-cart-button button',
519 'product_type_' . $product->get_type(),
520 $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
521 $product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ? 'ajax_add_to_cart' : '',
522 ) ) ),
523 'attributes' => array(
524 'data-product_id' => $product->get_id(),
525 'data-product_sku' => $product->get_sku(),
526 'aria-label' => $product->add_to_cart_description(),
527 'rel' => 'nofollow',
528 ),
529 );
530
531 $args = apply_filters( 'woocommerce_loop_add_to_cart_args', $defaults, $product );
532
533 if ( isset( $args['attributes']['aria-label'] ) ) {
534 $args['attributes']['aria-label'] = strip_tags( $args['attributes']['aria-label'] );
535 }
536
537 return apply_filters( 'woocommerce_loop_add_to_cart_link',
538 sprintf( '<a href="%s" data-quantity="%s" class="%s add-to-cart-loop" %s><span>%s</span></a>',
539 esc_url( $product->add_to_cart_url() ),
540 esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
541 esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
542 isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
543 esc_html( $product->add_to_cart_text() )
544 ),
545 $product, $args );
546 }
547
548 /**
549 * [availability_html]
550 * @param [object] $product
551 * @return [html]
552 */
553 public function availability_html( $product ) {
554 $html = '';
555 $availability = $product->get_availability();
556
557 if( empty( $availability['availability'] ) ) {
558 $availability['availability'] = __( 'In stock', 'woocommerce' );
559 }
560
561 if ( ! empty( $availability['availability'] ) ) {
562 ob_start();
563
564 wc_get_template( 'single-product/stock.php', array(
565 'product' => $product,
566 'class' => $availability['class'],
567 'availability' => $availability['availability'],
568 ) );
569
570 $html = ob_get_clean();
571 }
572
573 return apply_filters( 'woocommerce_get_stock_html', $html, $product );
574 }
575
576 /**
577 * [compare_cookie_name] Get Compare cookie name
578 * @return [string]
579 */
580 public function get_cookie_name() {
581 $name = 'ever_compare_compare_list';
582 if ( is_multisite() ){
583 $name .= '_' . get_current_blog_id();
584 }
585 return $name;
586 }
587
588 /**
589 * [is_product_in_compare]
590 * @param [int] $id product id
591 * @return [array] product id list
592 */
593 public function is_product_in_compare( $id ) {
594 $list = $this->get_compared_products();
595 return in_array( $id, $list, true );
596 }
597
598 /**
599 * [get_compared_products]
600 * @return [json]
601 */
602 public function get_compared_products() {
603 $cookie_name = $this->get_cookie_name();
604 return isset( $_COOKIE[ $cookie_name ] ) ? json_decode( wp_unslash( $_COOKIE[ $cookie_name ] ), true ) : array();
605 }
606
607 /**
608 * [compare_json_response]
609 * @return [json] product json
610 */
611 public function compare_json_response() {
612 $count = 0;
613 $products = $this->get_compared_products();
614
615 ob_start();
616
617 $this->get_response_html();
618
619 $table_html = ob_get_clean();
620
621 if ( is_array( $products ) ) {
622 $count = count( $products );
623 }
624
625 wp_send_json( array(
626 'count' => $count,
627 'table' => $table_html,
628 ) );
629
630 }
631
632 /**
633 * [get_compare_page_url] Compare Page Link
634 * @return [URL]
635 */
636 public function get_compare_page_url() {
637 $page_id = ever_compare_get_option( 'compare_page', 'ever_compare_table_settings_tabs' );
638 return get_permalink( $page_id );
639 }
640
641 /**
642 * [field_name]
643 * @param [string] $field
644 * @return [string]
645 */
646 public function field_name( $field = '', $custom = false ){
647
648 if( empty( $field ) ){
649 return;
650 }
651
652 if( $custom === true ){
653 return $field;
654 }
655
656 $default = ever_compare_get_default_fields();
657
658 $str = substr( $field, 0, 3 );
659 if( 'pa_' === $str ){
660 $field_name = wc_attribute_label( $field );
661 }else{
662 $field_name = $default[$field];
663 }
664 return $field_name;
665
666 }
667
668
669 }