PluginProbe
Ever Compare – Products Compare Plugin for WooCommerce / 1.3.5
Ever Compare – Products Compare Plugin for WooCommerce v1.3.5
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 / Ajax.php

Ajax.php in Ever Compare – Products Compare Plugin for WooCommerce 1.3.5, at includes/classes/Ajax.php

114 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace EverCompare;
3 /**
4 * Ajax handlers class
5 */
6 class Ajax {
7
8 /**
9 * [$_instance]
10 * @var null
11 */
12 private static $_instance = null;
13
14 /**
15 * [instance] Initializes a singleton instance
16 * @return [Ajax]
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 // Add Ajax Callback
31 add_action( 'wp_ajax_ever_compare_add_to_compare', [ $this, 'add_to_compare' ] );
32 add_action( 'wp_ajax_nopriv_ever_compare_add_to_compare', [ $this, 'add_to_compare' ] );
33
34 // Remove Ajax Callback
35 add_action( 'wp_ajax_ever_compare_remove_from_compare', [ $this, 'remove_from_compare' ] );
36 add_action( 'wp_ajax_nopriv_ever_compare_remove_from_compare', [ $this,'remove_from_compare' ] );
37
38 // Get Nonce endpoint (no nonce verification needed)
39 add_action( 'wp_ajax_ever_compare_get_nonce', [ $this, 'get_nonce' ] );
40 add_action( 'wp_ajax_nopriv_ever_compare_get_nonce', [ $this, 'get_nonce' ] );
41
42 // Get Table endpoint
43 add_action( 'wp_ajax_ever_compare_get_table', [ $this, 'get_table' ] );
44 add_action( 'wp_ajax_nopriv_ever_compare_get_table', [ $this, 'get_table' ] );
45
46 }
47
48 /**
49 * [add_to_compare] Product add ajax callback
50 */
51 public function add_to_compare(){
52 check_ajax_referer('ever_compare_nonce', 'nonce');
53 if(!empty($_GET['id'])) {
54 $id = absint( $_GET['id'] );
55 \EverCompare\Frontend\Manage_Compare::instance()->add_to_compare( $id );
56 }
57 }
58
59 /**
60 * [remove_from_compare] Product delete ajax callback
61 * @return [void]
62 */
63 public function remove_from_compare(){
64 check_ajax_referer('ever_compare_nonce', 'nonce');
65 if(!empty($_GET['id'])) {
66 $id = absint( $_GET['id'] );
67 \EverCompare\Frontend\Manage_Compare::instance()->remove_from_compare( $id );
68 }
69 }
70
71 /**
72 * [get_nonce] Return a fresh nonce for AJAX operations
73 * @return [void]
74 */
75 public function get_nonce(){
76 wp_send_json( array(
77 'nonce' => wp_create_nonce( 'ever_compare_nonce' ),
78 ) );
79 }
80
81 /**
82 * [get_table] Return compare table HTML for given product IDs
83 * @return [void]
84 */
85 public function get_table(){
86 $ids = array();
87 if ( ! empty( $_GET['ids'] ) ) {
88 $raw_ids = sanitize_text_field( wp_unslash( $_GET['ids'] ) );
89 $ids = array_filter( array_map( 'absint', explode( ',', $raw_ids ) ) );
90 $ids = array_slice( $ids, 0, \EverCompare\Frontend\Manage_Compare::instance()->max_limit );
91 }
92
93 if ( empty( $ids ) ) {
94 wp_send_json( array(
95 'table' => '',
96 'count' => 0,
97 ) );
98 }
99
100 $cookie_name = \EverCompare\Frontend\Manage_Compare::instance()->get_cookie_name();
101 $_COOKIE[ $cookie_name ] = wp_json_encode( $ids );
102
103 ob_start();
104 \EverCompare\Frontend\Manage_Compare::instance()->get_response_html();
105 $table_html = ob_get_clean();
106
107 wp_send_json( array(
108 'table' => $table_html,
109 'count' => count( $ids ),
110 'products' => $ids,
111 ) );
112 }
113
114 }