Skip to content

Commit

Permalink
Country and Continent api added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aunshon committed Oct 23, 2024
1 parent 0b1ce02 commit e59521e
Show file tree
Hide file tree
Showing 3 changed files with 248 additions and 0 deletions.
123 changes: 123 additions & 0 deletions includes/REST/DokanDataContinentsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

namespace WeDevs\Dokan\REST;

use WC_REST_Data_Continents_Controller;
use WP_Error;
use WP_REST_Request;
use WP_REST_Response;

class DokanDataContinentsController extends WC_REST_Data_Continents_Controller {

/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'dokan/v1';

/**
* Route base.
*
* @var string
*/
protected $rest_base = 'data/continents';

/**
* Register routes.
*
* @since 3.5.0
*/
public function register_routes() {

Check warning on line 31 in includes/REST/DokanDataContinentsController.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Possible useless method overriding detected
parent::register_routes();
}

/**
* Return the list of states for all countries.
*
* @since 3.5.0
* @param WP_REST_Request $request Request data.
* @return WP_Error|WP_REST_Response
*/
public function get_items( $request ) {
$this->set_woocommerce_rest_check_permissions();

return parent::get_items( $request );
}

/**
* Return the list of states for a given country.
*
* @since 3.5.0
* @param WP_REST_Request $request Request data.
* @return WP_Error|WP_REST_Response
*/
public function get_item( $request ) {
$this->set_woocommerce_rest_check_permissions();
return parent::get_item( $request );
}

/**
* Check if a given request has access to read an item.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_item_permissions_check( $request ) {
$this->set_woocommerce_rest_check_permissions();
$item_permission = $this->get_items_permissions_check( $request );

if ( is_wp_error( $item_permission ) ) {
return $item_permission;
}

return parent::get_item_permissions_check( $request );
}

/**
* Check if a given request has access to read items.
*
* @since DOKAN_SINCE
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_items_permissions_check( $request ) {
// phpcs:ignore WordPress.WP.Capabilities.Unknown
if ( current_user_can( dokan_admin_menu_capability() ) || current_user_can( 'dokandar' ) ) {
return true;
}

return new WP_Error(
'dokan_pro_permission_failure',
__( 'You are not allowed to do this action.', 'dokan-lite' ),
[
'status' => rest_authorization_required_code(),
]
);
}

private function set_woocommerce_rest_check_permissions() {
add_filter( 'woocommerce_rest_check_permissions', [ $this, 'add_subscriptions_read_permission_to_vendors' ], 10, 4 );
}

/**
* Add permissions.
*
* @since DOKAN_PRO_SINCE
*
* @param $permission
* @param $context
* @param $object_id
* @param $obj
*
* @return true
*/
public function add_subscriptions_read_permission_to_vendors( $permission, $context, $object_id, $obj ) {
if ( 'read' === $context ) {
return true;
}

return $permission;
}
}
123 changes: 123 additions & 0 deletions includes/REST/DokanDataCountriesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

namespace WeDevs\Dokan\REST;

use WC_REST_Data_Countries_Controller;
use WP_Error;
use WP_REST_Request;
use WP_REST_Response;

class DokanDataCountriesController extends WC_REST_Data_Countries_Controller {

/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'dokan/v1';

/**
* Route base.
*
* @var string
*/
protected $rest_base = 'data/countries';

/**
* Register routes.
*
* @since 3.5.0
*/
public function register_routes() {

Check warning on line 31 in includes/REST/DokanDataCountriesController.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Possible useless method overriding detected
parent::register_routes();
}

/**
* Return the list of states for all countries.
*
* @since 3.5.0
* @param WP_REST_Request $request Request data.
* @return WP_Error|WP_REST_Response
*/
public function get_items( $request ) {
$this->set_woocommerce_rest_check_permissions();

return parent::get_items( $request );
}

/**
* Return the list of states for a given country.
*
* @since 3.5.0
* @param WP_REST_Request $request Request data.
* @return WP_Error|WP_REST_Response
*/
public function get_item( $request ) {
$this->set_woocommerce_rest_check_permissions();
return parent::get_item( $request );
}

/**
* Check if a given request has access to read an item.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_item_permissions_check( $request ) {
$this->set_woocommerce_rest_check_permissions();
$item_permission = $this->get_items_permissions_check( $request );

if ( is_wp_error( $item_permission ) ) {
return $item_permission;
}

return parent::get_item_permissions_check( $request );
}

/**
* Check if a given request has access to read items.
*
* @since DOKAN_SINCE
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_items_permissions_check( $request ) {
// phpcs:ignore WordPress.WP.Capabilities.Unknown
if ( current_user_can( dokan_admin_menu_capability() ) || current_user_can( 'dokandar' ) ) {
return true;
}

return new WP_Error(
'dokan_pro_permission_failure',
__( 'You are not allowed to do this action.', 'dokan-lite' ),
[
'status' => rest_authorization_required_code(),
]
);
}

private function set_woocommerce_rest_check_permissions() {
add_filter( 'woocommerce_rest_check_permissions', [ $this, 'add_subscriptions_read_permission_to_vendors' ], 10, 4 );
}

/**
* Add permissions.
*
* @since DOKAN_PRO_SINCE
*
* @param $permission
* @param $context
* @param $object_id
* @param $obj
*
* @return true
*/
public function add_subscriptions_read_permission_to_vendors( $permission, $context, $object_id, $obj ) {
if ( 'read' === $context ) {
return true;
}

return $permission;
}
}
2 changes: 2 additions & 0 deletions includes/REST/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ private function get_rest_api_class_map() {
DOKAN_DIR . '/includes/REST/StoreSettingControllerV2.php' => '\WeDevs\Dokan\REST\StoreSettingControllerV2',
DOKAN_DIR . '/includes/REST/VendorDashboardController.php' => '\WeDevs\Dokan\REST\VendorDashboardController',
DOKAN_DIR . '/includes/REST/ProductBlockController.php' => '\WeDevs\Dokan\REST\ProductBlockController',
DOKAN_DIR . '/includes/REST/DokanDataCountriesController.php' => '\WeDevs\Dokan\REST\DokanDataCountriesController',
DOKAN_DIR . '/includes/REST/DokanDataContinentsController.php' => '\WeDevs\Dokan\REST\DokanDataContinentsController',
)
);
}
Expand Down

0 comments on commit e59521e

Please sign in to comment.