PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 1.0.0
ShopBuilder – WooCommerce Builder For Elementor v1.0.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 / WishList / WishlistRouteV1.php

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

103 lines 3.2 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 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 class WishlistRouteV1 extends Api {
18
19
20 public function config() {
21 }
22
23
24 public function init() {
25 add_action( 'rest_api_init', function () {
26 register_rest_route( $this->getNamespace(), '/wishlist/add_to_list', [
27 'methods' => WP_REST_Server::CREATABLE,
28 'callback' => [ $this, 'add_to_list_callback' ],
29 'permission_callback' => [ $this, 'wishlist_permission_callback' ] , //'__return_true'
30 ] );
31 register_rest_route( $this->getNamespace(), '/wishlist/remove_from_list', [
32 'methods' => WP_REST_Server::CREATABLE,
33 'callback' => [ $this, 'remove_from_list_callback' ],
34 'permission_callback' => [ $this, 'wishlist_permission_callback' ]
35 ] );
36 } );
37 }
38 public function wishlist_permission_callback( $request ) {
39 $has_permition = ! get_current_user_id() && Wishlist::instance()->get_option( 'enable_login_limit', false, 'checkbox' );
40 if ( $has_permition ) {
41 return new WP_Error(
42 'authentication_error',
43 __( 'Sorry, you are not allowed to do that!. Only Logged-in user can use this feature' ),
44 array( 'status' => rest_authorization_required_code() )
45 );
46 }
47 return true;
48 }
49 /**
50 * @param WP_REST_Request $request
51 *
52 * @return WP_Error|WP_HTTP_Response|WP_REST_Response
53 */
54 public function add_to_list_callback( $request ) {
55 $product_id = absint( $request->get_param( 'product_id' ) );
56
57 if ( ! $product_id ) {
58 return $this->sendError( esc_html__( 'Product id not found.', 'shopbuilder' ) );
59 }
60
61 $inserted = WishlistFns::instance()->add_product( $product_id );
62 if ( $inserted === 0 ) {
63 return $this->sendError( esc_html__( 'Product already in wishlist!', 'shopbuilder' ), [
64 'action' => 'exist'
65 ] );
66 }
67
68 return $this->sendResponse( [
69 'item_count' => count( WishlistFns::instance()->get_wishlist_ids() ),
70 'product_id' => $product_id,
71 ] );
72 }
73
74
75 /**
76 * @param WP_REST_Request $request
77 *
78 * @return WP_Error|WP_HTTP_Response|WP_REST_Response
79 */
80 public function remove_from_list_callback( $request ) {
81 $product_id = absint( $request->get_param( 'product_id' ) );
82
83 if ( ! $product_id ) {
84 return $this->sendError( esc_html__( 'Product id not found.', 'shopbuilder' ) );
85 }
86
87 $deleted = WishlistFns::instance()->remove_product( $product_id );
88 if ( ! $deleted ) {
89 if ( $deleted === 0 ) {
90 return $this->sendError( esc_html__( 'The product is not exist at the list!', 'shopbuilder' ) );
91 }
92
93 return $this->sendError( esc_html__( 'The product does not deleted!', 'shopbuilder' ) );
94 }
95
96 return $this->sendResponse( [
97 'product_id' => $product_id,
98 'item_count' => count( WishlistFns::instance()->get_wishlist_ids() )
99 ] );
100
101 }
102
103 }