PluginProbe
Ever Compare – Products Compare Plugin for WooCommerce / trunk
Ever Compare – Products Compare Plugin for WooCommerce vtrunk
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 trunk, at includes/classes/Ajax.php

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