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

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