Initial
This commit is contained in:
132
api.php
Normal file
132
api.php
Normal file
@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
|
||||
function get_all_user_roles() {
|
||||
global $wp_roles;
|
||||
|
||||
if (!isset($wp_roles)) {
|
||||
$wp_roles = new WP_Roles();
|
||||
}
|
||||
|
||||
$roles = $wp_roles->roles;
|
||||
$role_names = [];
|
||||
|
||||
foreach ($roles as $role_key => $role_data) {
|
||||
$role_names[] = [
|
||||
'key' => $role_key,
|
||||
'name' => $role_data['name'],
|
||||
];
|
||||
}
|
||||
|
||||
return rest_ensure_response($role_names);
|
||||
}
|
||||
|
||||
function is_admin_user() {
|
||||
return true;
|
||||
/* return current_user_can('manage_options'); */
|
||||
}
|
||||
|
||||
function register_custom_user_roles_endpoint() {
|
||||
register_rest_route('lcp-paywall/v1', '/user-roles', [
|
||||
'methods' => 'GET',
|
||||
'callback' => 'get_all_user_roles',
|
||||
'permission_callback' => 'is_admin_user',
|
||||
]);
|
||||
}
|
||||
|
||||
add_action('rest_api_init', 'register_custom_user_roles_endpoint');
|
||||
|
||||
|
||||
/* GET TAXONOMIES */
|
||||
function get_taxonomies_by_post_type( $request ) {
|
||||
$post_type = $request->get_param('post_type');
|
||||
|
||||
if (empty($post_type)) {
|
||||
return new WP_Error('missing_post_type', 'Post type parameter is required', ['status' => 400]);
|
||||
}
|
||||
|
||||
// Get taxonomies associated with the post type
|
||||
$taxonomies = get_object_taxonomies($post_type, 'names'); // Use 'names' instead of 'objects' for simple list
|
||||
|
||||
// If no taxonomies found, return an error message
|
||||
if (empty($taxonomies)) {
|
||||
return new WP_Error('no_taxonomies', 'No taxonomies found for the given post type', ['status' => 404]);
|
||||
}
|
||||
|
||||
// Fetch taxonomy objects for the specific taxonomies
|
||||
$taxonomy_objects = [];
|
||||
foreach ($taxonomies as $taxonomy) {
|
||||
$taxonomy_objects[] = get_taxonomy($taxonomy); // Retrieve taxonomy object for each taxonomy
|
||||
}
|
||||
|
||||
return rest_ensure_response($taxonomy_objects);
|
||||
}
|
||||
|
||||
|
||||
function register_taxonomies_endpoint() {
|
||||
register_rest_route('lcp-paywall/v1', '/taxonomies', [
|
||||
'methods' => 'GET',
|
||||
'callback' => 'get_taxonomies_by_post_type',
|
||||
'args' => [
|
||||
'post_type' => [
|
||||
'required' => true,
|
||||
'type' => 'string',
|
||||
],
|
||||
],
|
||||
'permission_callback' => '__return_true',
|
||||
]);
|
||||
}
|
||||
|
||||
add_action('rest_api_init', 'register_taxonomies_endpoint');
|
||||
|
||||
function get_terms_by_taxonomy_slug( $request ) {
|
||||
// Get the taxonomy slug from the request parameters
|
||||
$taxonomy_slug = $request->get_param( 'taxonomy' );
|
||||
|
||||
// Check if the taxonomy slug is provided
|
||||
if ( empty( $taxonomy_slug ) ) {
|
||||
return new WP_Error( 'missing_taxonomy', 'Taxonomy slug parameter is required', array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
// Check if the taxonomy exists
|
||||
if ( ! taxonomy_exists( $taxonomy_slug ) ) {
|
||||
return new WP_Error( 'invalid_taxonomy', 'The provided taxonomy does not exist', array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
// Get the terms for the taxonomy
|
||||
$terms = get_terms( array(
|
||||
'taxonomy' => $taxonomy_slug,
|
||||
'hide_empty' => false, // Set to true to only get terms that have posts assigned
|
||||
) );
|
||||
|
||||
// Check if terms were found
|
||||
if ( is_wp_error( $terms ) ) {
|
||||
return $terms;
|
||||
}
|
||||
|
||||
if ( empty( $terms ) ) {
|
||||
return new WP_Error( 'no_terms', 'No terms found for the given taxonomy', array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
// Return the terms as a response
|
||||
return rest_ensure_response( $terms );
|
||||
}
|
||||
|
||||
function register_custom_taxonomy_terms_endpoint() {
|
||||
// Register the custom REST route
|
||||
register_rest_route( 'lcp-paywall/v1','/terms', array(
|
||||
'methods' => 'GET',
|
||||
'callback' => 'get_terms_by_taxonomy_slug',
|
||||
'args' => array(
|
||||
'taxonomy' => array(
|
||||
'required' => true,
|
||||
'validate_callback' => function( $param, $request, $key ) {
|
||||
// Validate that the taxonomy exists
|
||||
return taxonomy_exists( $param );
|
||||
},
|
||||
),
|
||||
),
|
||||
) );
|
||||
}
|
||||
|
||||
add_action( 'rest_api_init', 'register_custom_taxonomy_terms_endpoint' );
|
||||
Reference in New Issue
Block a user