| 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( 'rest_api_init', function () { |
| 27 |
register_rest_route( $this->getNamespace(), '/compare/add_to_list', [ |
| 28 |
'methods' => WP_REST_Server::CREATABLE, |
| 29 |
'callback' => [ $this, 'add_to_list_callback' ], |
| 30 |
'permission_callback' => '__return_true' |
| 31 |
] ); |
| 32 |
register_rest_route( $this->getNamespace(), '/compare/remove_from_list', [ |
| 33 |
'methods' => WP_REST_Server::CREATABLE, |
| 34 |
'callback' => [ $this, 'remove_from_list_callback' ], |
| 35 |
'permission_callback' => '__return_true' |
| 36 |
] ); |
| 37 |
} ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* @param WP_REST_Request $request |
| 42 |
* |
| 43 |
* @return WP_Error|WP_HTTP_Response|WP_REST_Response |
| 44 |
*/ |
| 45 |
public function add_to_list_callback( $request ) { |
| 46 |
$product_id = $request->get_param( 'product_id' ); |
| 47 |
|
| 48 |
if ( ! $product_id ) { |
| 49 |
return $this->sendError( esc_html__( 'Product id not found.', 'shopbuilder' ) ); |
| 50 |
} |
| 51 |
|
| 52 |
$inserted = CompareFns::instance()->add_product( $product_id ); |
| 53 |
if ( is_wp_error( $inserted ) ) { |
| 54 |
return $this->sendError( $inserted->get_error_message() ); |
| 55 |
} |
| 56 |
|
| 57 |
return $this->sendResponse( [ |
| 58 |
'item_count' => count( CompareFns::instance()->get_compared_product_ids() ), |
| 59 |
'product_id' => $product_id, |
| 60 |
'list_html' => CompareFrontEnd::instance()->list_shortcode([]) |
| 61 |
] ); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
/** |
| 66 |
* @param WP_REST_Request $request |
| 67 |
* |
| 68 |
* @return WP_Error|WP_HTTP_Response|WP_REST_Response |
| 69 |
*/ |
| 70 |
public function remove_from_list_callback( $request ) { |
| 71 |
$product_id = $request->get_param( 'product_id' ); |
| 72 |
|
| 73 |
if ( ! $product_id ) { |
| 74 |
return $this->sendError( esc_html__( 'Product id not found.', 'shopbuilder' ) ); |
| 75 |
} |
| 76 |
|
| 77 |
$deleted = CompareFns::instance()->remove_product( $product_id ); |
| 78 |
if ( is_wp_error( $deleted ) ) { |
| 79 |
return $this->sendError( $deleted->get_error_message() ); |
| 80 |
} |
| 81 |
|
| 82 |
if ( ! $deleted ) { |
| 83 |
return $this->sendError( esc_html__( 'The product does not delete!', 'shopbuilder' ) ); |
| 84 |
} |
| 85 |
|
| 86 |
return $this->sendResponse( [ |
| 87 |
'product_id' => $product_id, |
| 88 |
'item_count' => count( CompareFns::instance()->get_compared_product_ids() ), |
| 89 |
'list_html' => CompareFrontEnd::instance()->list_shortcode([]) |
| 90 |
] ); |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
} |