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
← All changes | includes/classes/Ajax.php +55 -2 1.3.4trunk View file →
@@ -1,6 +1,8 @@
1 1 <?php
2 2 namespace EverCompare;
3 +
4 +defined( 'ABSPATH' ) || exit;
3 5 /**
4 6 * Ajax handlers class
5 7 */
6 8 class Ajax {
@@ -34,8 +36,16 @@
34 36 // Remove Ajax Callback
35 37 add_action( 'wp_ajax_ever_compare_remove_from_compare', [ $this, 'remove_from_compare' ] );
36 38 add_action( 'wp_ajax_nopriv_ever_compare_remove_from_compare', [ $this,'remove_from_compare' ] );
37 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 +
38 48 }
39 49
40 50 /**
41 51 * [add_to_compare] Product add ajax callback
@@ -42,9 +52,9 @@
42 52 */
43 53 public function add_to_compare(){
44 54 check_ajax_referer('ever_compare_nonce', 'nonce');
45 55 if(!empty($_GET['id'])) {
46 - $id = sanitize_text_field( wp_unslash($_GET['id']) );
56 + $id = absint( $_GET['id'] );
47 57 \EverCompare\Frontend\Manage_Compare::instance()->add_to_compare( $id );
48 58 }
49 59 }
50 60
@@ -54,10 +64,53 @@
54 64 */
55 65 public function remove_from_compare(){
56 66 check_ajax_referer('ever_compare_nonce', 'nonce');
57 67 if(!empty($_GET['id'])) {
58 - $id = sanitize_text_field( wp_unslash($_GET['id']) );
68 + $id = absint( $_GET['id'] );
59 69 \EverCompare\Frontend\Manage_Compare::instance()->remove_from_compare( $id );
60 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 + ) );
61 114 }
62 115
63 116 }