Created
March 10, 2023 00:45
-
-
Save codearachnid/9a083f2e6dd018f3643df7622437f2a6 to your computer and use it in GitHub Desktop.
Expose Ninja Table data as JSON via REST API.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Code snippet to expose ninja table data as JSON via REST API | |
* Example URL: https://yourdomain.com/wp-json/ninjatable/v1/table/# | |
* In this working example you should enable authentication to protect your data | |
* example is contained in `get_items_permissions_check` | |
* Route logic sourced from https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/ | |
*/ | |
add_action('rest_api_init', function () { | |
$version = 1; | |
$namespace = 'ninjatable/v' . $version; | |
$base = 'table'; | |
register_rest_route( $namespace, '/' . $base, [ | |
'methods' => WP_REST_Server::READABLE, | |
'callback' => 'get_ninjatable_tables' | |
]); | |
register_rest_route( $namespace, '/' . $base . '/(?P<id>[\d]+)', | |
[ | |
'methods' => WP_REST_Server::READABLE, | |
'callback' => 'get_ninjatable_data', | |
'permission_callback' => 'get_items_permissions_check', | |
]); | |
// add additional verb routes and capabilities via https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/ | |
}); | |
/* | |
* base route `ninjatable/v1/table` will load the available table list | |
* recommend to project your results instead of exposing in this manner | |
* | |
* @param WP_REST_Request $request The request object passed through the registered WordPress REST handler | |
* | |
*/ | |
function get_ninjatable_tables( WP_REST_Request $request ) { | |
global $wpdb; | |
$data = $wpdb->get_results( "SELECT ID, post_title AS name, post_status AS status FROM {$wpdb->posts} WHERE post_type = 'ninja-table'" ); | |
if ( ! empty( $data ) ) { | |
return new WP_REST_Response( $data, 200 ); | |
} else { | |
return new WP_Error( 'no_data', __( 'No tables found', 'text-domain' ), array( 'status' => 404 ) ); | |
} | |
} | |
/* | |
* get table data route `ninjatable/v1/table/#` will load the available data from specfic table | |
* recommend to project your results instead of exposing in this manner see use of `get_items_permissions_check` | |
* | |
* @param WP_REST_Request $request The request object passed through the registered WordPress REST handler | |
* | |
*/ | |
function get_ninjatable_data( WP_REST_Request $request ) { | |
$id = $request->get_param( 'id' ); | |
$data = ninjaTablesGetTablesDataByID($id); | |
if ( !empty( $data ) ) { | |
return new WP_REST_Response( $data, 200 ); | |
} else { | |
return new WP_Error( 'no_data', __( 'No data was found matching your criteria', 'text-domain' ), array( 'status' => 404 ) ); | |
} | |
} | |
/* | |
* `permission_callback` handler checks if the user can perform the action. This callback should | |
* return a boolean or a WP_Error instance. If this function returns true, the response will | |
* be processed. If it returns false, a default error message will be returned and the request | |
* will not proceed with processing. If it returns a WP_Error, that error will be returned | |
* to the client. | |
* | |
* @param WP_REST_Request $request The request object passed through the registered WordPress REST handler | |
* | |
*/ | |
function get_items_permissions_check( WP_REST_Request $request ) { | |
return true; // <--use to make readable by all | |
return current_user_can( 'edit_something' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment