PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 3.2.0
ShopBuilder – WooCommerce Builder For Elementor v3.2.0
3.4.2 3.4.1 3.4.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 All 63 releases
shopbuilder / app / Modules / Compare / CompareRouteV1.php

CompareRouteV1.php in ShopBuilder – WooCommerce Builder For Elementor 3.2.0, at app/Modules/Compare/CompareRouteV1.php

128 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace RadiusTheme\SB\Modules\Compare;
4
5 use RadiusTheme\SB\Abstracts\Api;
6 use WP_Error;
7 use WP_HTTP_Response;
8 use WP_REST_Request;
9 use WP_REST_Response;
10 use WP_REST_Server;
11
12 // Do not allow directly accessing this file.
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit( 'This script cannot be accessed directly.' );
15 }
16
17
18 class CompareRouteV1 extends Api {
19
20
21 public function config() {
22 }
23
24
25 public function init() {
26 add_action(
27 'rest_api_init',
28 function () {
29 register_rest_route(
30 $this->getNamespace(),
31 '/compare/add_to_list',
32 [
33 'methods' => WP_REST_Server::CREATABLE,
34 'callback' => [ $this, 'add_to_list_callback' ],
35 'permission_callback' => [ $this, 'check_permission' ],
36 ]
37 );
38 register_rest_route(
39 $this->getNamespace(),
40 '/compare/remove_from_list',
41 [
42 'methods' => WP_REST_Server::CREATABLE,
43 'callback' => [ $this, 'remove_from_list_callback' ],
44 'permission_callback' => [ $this, 'check_permission' ],
45 ]
46 );
47 }
48 );
49 }
50
51 /**
52 * Check permission for the given WP_REST_Request.
53 *
54 * @param WP_REST_Request $request The REST request object.
55 *
56 * @return boolean
57 */
58 public function check_permission( WP_REST_Request $request ) {
59 $product_id = $request->get_param( 'product_id' );
60 $nonce = $request->get_header( 'X-WP-Nonce' );
61
62 if ( ! $product_id || 'publish' !== get_post_status( $product_id ) ) {
63 return false;
64 }
65
66 if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
67 return false;
68 }
69
70 return true;
71 }
72
73
74 public function add_to_list_callback( $request ) {
75 $product_id = $request->get_param( 'product_id' );
76
77 if ( ! $product_id || 'publish' !== get_post_status( $product_id ) ) {
78 return $this->sendError( esc_html__( 'Product id not found.', 'shopbuilder' ) );
79 }
80
81 $inserted = CompareFns::instance()->add_product( $product_id );
82 if ( is_wp_error( $inserted ) ) {
83 return $this->sendError( $inserted->get_error_message() );
84 }
85
86 return $this->sendResponse(
87 [
88 'item_count' => count( CompareFns::instance()->get_compared_product_ids() ),
89 'product_id' => $product_id,
90 'list_html' => CompareFrontEnd::instance()->list_shortcode( [] ),
91 ]
92 );
93 }
94
95
96 /**
97 * Callback function for removing a product to the comparison list.
98 *
99 * @param WP_REST_Request $request The REST request object.
100 *
101 * @return WP_Error|WP_HTTP_Response|WP_REST_Response
102 */
103 public function remove_from_list_callback( $request ) {
104 $product_id = $request->get_param( 'product_id' );
105
106 if ( ! $product_id ) {
107 return $this->sendError( esc_html__( 'Product id not found.', 'shopbuilder' ) );
108 }
109
110 $deleted = CompareFns::instance()->remove_product( $product_id );
111 if ( is_wp_error( $deleted ) ) {
112 return $this->sendError( $deleted->get_error_message() );
113 }
114
115 if ( ! $deleted ) {
116 return $this->sendError( esc_html__( 'The product does not delete!', 'shopbuilder' ) );
117 }
118
119 return $this->sendResponse(
120 [
121 'product_id' => $product_id,
122 'item_count' => count( CompareFns::instance()->get_compared_product_ids() ),
123 'list_html' => CompareFrontEnd::instance()->list_shortcode( [] ),
124 ]
125 );
126 }
127 }
128