PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 2.1.10
ShopBuilder – WooCommerce Builder For Elementor v2.1.10
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 / WishList / WishlistRouteV1.php

WishlistRouteV1.php in ShopBuilder – WooCommerce Builder For Elementor 2.1.10, at app/Modules/WishList/WishlistRouteV1.php

122 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\WishList;
4
5 use RadiusTheme\SB\Abstracts\Api;
6 use RadiusTheme\SB\Helpers\Fns;
7 use WP_Error;
8 use WP_HTTP_Response;
9 use WP_REST_Request;
10 use WP_REST_Response;
11 use WP_REST_Server;
12
13 // Do not allow directly accessing this file.
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit( 'This script cannot be accessed directly.' );
16 }
17
18 class WishlistRouteV1 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 '/wishlist/add_to_list',
32 [
33 'methods' => WP_REST_Server::CREATABLE,
34 'callback' => [ $this, 'add_to_list_callback' ],
35 'permission_callback' => [ $this, 'wishlist_permission_callback' ] , // '__return_true'
36 ]
37 );
38 register_rest_route(
39 $this->getNamespace(),
40 '/wishlist/remove_from_list',
41 [
42 'methods' => WP_REST_Server::CREATABLE,
43 'callback' => [ $this, 'remove_from_list_callback' ],
44 'permission_callback' => [ $this, 'wishlist_permission_callback' ],
45 ]
46 );
47 }
48 );
49 }
50 public function wishlist_permission_callback( $request ) {
51 $has_permition = ! get_current_user_id() && Fns::get_option( 'modules', 'wishlist', 'enable_login_limit', false, 'checkbox' );
52
53 if ( $has_permition ) {
54 return new WP_Error(
55 'authentication_error',
56 __( 'Sorry, you are not allowed to do that!. Only Logged-in user can use this feature', 'shopbuilder' ),
57 [ 'status' => rest_authorization_required_code() ]
58 );
59 }
60 return true;
61 }
62 /**
63 * @param WP_REST_Request $request
64 *
65 * @return WP_Error|WP_HTTP_Response|WP_REST_Response
66 */
67 public function add_to_list_callback( $request ) {
68 $product_id = absint( $request->get_param( 'product_id' ) );
69
70 if ( ! $product_id || 'publish' !== get_post_status( $product_id ) ) {
71 return $this->sendError( esc_html__( 'Product id not found.', 'shopbuilder' ) );
72 }
73
74 $inserted = WishlistFns::instance()->add_product( $product_id );
75 if ( $inserted === 0 ) {
76 return $this->sendError(
77 esc_html__( 'Product already in wishlist!', 'shopbuilder' ),
78 [
79 'action' => 'exist',
80 ]
81 );
82 }
83
84 return $this->sendResponse(
85 [
86 'item_count' => count( WishlistFns::instance()->get_wishlist_ids() ),
87 'product_id' => $product_id,
88 ]
89 );
90 }
91
92
93 /**
94 * @param WP_REST_Request $request
95 *
96 * @return WP_Error|WP_HTTP_Response|WP_REST_Response
97 */
98 public function remove_from_list_callback( $request ) {
99 $product_id = absint( $request->get_param( 'product_id' ) );
100
101 if ( ! $product_id ) {
102 return $this->sendError( esc_html__( 'Product id not found.', 'shopbuilder' ) );
103 }
104
105 $deleted = WishlistFns::instance()->remove_product( $product_id );
106 if ( ! $deleted ) {
107 if ( $deleted === 0 ) {
108 return $this->sendError( esc_html__( 'The product is not exist at the list!', 'shopbuilder' ) );
109 }
110
111 return $this->sendError( esc_html__( 'The product does not deleted!', 'shopbuilder' ) );
112 }
113
114 return $this->sendResponse(
115 [
116 'product_id' => $product_id,
117 'item_count' => count( WishlistFns::instance()->get_wishlist_ids() ),
118 ]
119 );
120 }
121 }
122