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

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