Refactor -- use rest api helper

This commit is contained in:
Moon 2021-03-03 18:36:13 -08:00
parent 9c928fc86e
commit 4f8c54eb04
4 changed files with 74 additions and 101 deletions

View File

@ -1,22 +1,27 @@
<?php
function register_woocommerce_admin_test_helper_rest_route( $route, $callback ) {
add_action( 'rest_api_init', function() use ( $route, $callback ) {
function register_woocommerce_admin_test_helper_rest_route( $route, $callback, $additional_options = array() ) {
add_action( 'rest_api_init', function() use ( $route, $callback, $additional_options ) {
$default_options = array(
'methods' => 'POST',
'callback' => $callback,
'permission_callback' => function( $request ) {
if ( ! wc_rest_check_manager_permissions( 'settings', 'edit' ) ) {
return new \WP_Error(
'woocommerce_rest_cannot_edit',
__( 'Sorry, you cannot perform this action', 'woocommerce-admin-test-helper' )
);
}
return true;
},
);
$default_options = array_merge( $default_options, $additional_options );
register_rest_route(
'wc-admin-test-helper',
$route,
array(
'methods' => 'POST',
'callback' => $callback,
'permission_callback' => function( $request ) {
if ( ! wc_rest_check_manager_permissions( 'settings', 'edit' ) ) {
return new \WP_Error(
'woocommerce_rest_cannot_edit',
__( 'Sorry, you cannot perform this action', 'woocommerce-admin-test-helper' )
);
}
return true;
},
)
$default_options
);
} );
}

View File

@ -1,82 +1,11 @@
<?php
class Options_Rest_Api extends WP_REST_Controller {
protected $namespace = 'wc-admin-test-helper/v1';
public function register_routes() {
register_rest_route(
$this->namespace,
'/options',
array(
'methods' => 'GET',
'callback' => array( $this, 'get_items' ),
'args' => array(
'per_page' => $this->get_collection_params()['per_page'],
),
'permission_callback' => function( $request ) {
return true;
},
)
);
register_rest_route(
$this->namespace,
'/options/(?P<option_id>\d+)',
array(
'methods' => 'DELETE',
'callback' => array( $this, 'delete_item' ),
'args' => array(
'option_id' => array(
'type' => 'integer',
'sanitize_callback' => 'absint',
),
),
'permission_callback' => function( $request ) {
return true;
},
)
);
}
public function delete_item( $request ) {
global $wpdb;
$option_id = $request->get_param( 'option_id' );
$query = $wpdb->prepare( "delete from {$wpdb->prefix}options where option_id = %d", $option_id );
$wpdb->query( $query );
return new WP_REST_RESPONSE( null, 204 );
}
public function get_items( $request ) {
global $wpdb;
$per_page = $request->get_param( 'per_page' );
$page = $request->get_param( 'page' );
$search = $request->get_param( 'search' );
$query = "
select option_id, option_name, autoload
from {$wpdb->prefix}options
";
if ( $search ) {
$query .= "where option_name like '%{$search}%'";
}
$query .= ' order by option_id desc limit 30';
$options = $wpdb->get_results( $query );
return new WP_REST_Response( $options, 200 );
}
/**
* Get the query params for collections
*
* @return array
*/
public function get_collection_params() {
return array(
register_woocommerce_admin_test_helper_rest_route(
'/options',
'wca_test_helper_get_options',
array(
'methods' => 'GET',
'args' => array(
'page' => array(
'description' => 'Current page of the collection.',
'type' => 'integer',
@ -94,13 +23,53 @@ class Options_Rest_Api extends WP_REST_Controller {
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
),
);
}
),
)
);
register_woocommerce_admin_test_helper_rest_route(
'/options/(?P<option_id>\d+)',
'wca_test_helper_delete_option',
array(
'methods' => 'DELETE',
'args' => array(
'option_id' => array(
'type' => 'integer',
'sanitize_callback' => 'absint',
),
),
)
);
function wca_test_helper_delete_option( $request ) {
global $wpdb;
$option_id = $request->get_param( 'option_id' );
$query = $wpdb->prepare( "delete from {$wpdb->prefix}options where option_id = %d", $option_id );
$wpdb->query( $query );
return new WP_REST_RESPONSE( null, 204 );
}
add_action(
'rest_api_init',
function() {
( new Options_Rest_Api() )->register_routes();
function wca_test_helper_get_options( $request ) {
global $wpdb;
$per_page = $request->get_param( 'per_page' );
$page = $request->get_param( 'page' );
$search = $request->get_param( 'search' );
$query = "
select option_id, option_name, autoload
from {$wpdb->prefix}options
";
if ( $search ) {
$query .= "where option_name like '%{$search}%'";
}
);
$query .= ' order by option_id desc limit 30';
$options = $wpdb->get_results( $query );
return new WP_REST_Response( $options, 200 );
}

View File

@ -1,2 +1,2 @@
export const STORE_KEY = 'wc-admin-helper/options';
export const API_NAMESPACE = '/wc-admin-test-helper/v1';
export const API_NAMESPACE = '/wc-admin-test-helper';

View File

@ -14,7 +14,6 @@ export function* getOptions( search ) {
if ( search ) {
path += `search=${ search }`;
}
console.log( path );
try {
const response = yield apiFetch( {