Skip to content

filter query api permission_callback #203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions includes/class-fontawesome-api-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,35 @@ public function register_routes() {
array(
'methods' => 'POST',
'callback' => array( $this, 'query' ),
'permission_callback' => function() {
'permission_callback' => function( $request ) {
/**
* It's possible that a non-admin user may need to be able
* to issue requests through this API Controller, such as
* when searching through the Font Awesome API search via
* an icon chooser. That's why 'edit_posts' is allowed here.
* an icon chooser. That's why 'edit_posts' is allowed here,
* by default.
*
* However, it seems there are cases where a user may be
* able to manage_options but not edit_posts, so we'll include
* that permission separately.
* that permission separately, by default.
*
* Finally, we'll filter it so developers can further customize.
*/

/**
* Filters the `permission_callback` for the plugin's
* REST endpoint that queries the Font Awesome search API.
*
* See also: WordPress REST API [`permission_callback`](https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/#permissions-callback) documentation.
*
* @param WP_REST_Request
*/
return current_user_can( 'manage_options' ) || current_user_can( 'edit_posts' ); },
return apply_filters(
'font_awesome_query_api_permission_callback',
current_user_can( 'manage_options' ) || current_user_can( 'edit_posts' ),
$request,
);
},
'args' => array(),
),
)
Expand Down
8 changes: 8 additions & 0 deletions integrations/themes/theme-alpha/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,11 @@ function theme_alpha_fa_classes(){

return implode(' ', $class_list);
}

add_filter('font_awesome_query_api_permission_callback', function($val, $request) {
// This is how we might filter the permisssions for the plugin's WP REST API
// endpoint, to determine whether we'll issue a search request on behalf
// of a user.
error_log('DEBUG: filtering font_awesome_query_api_permission_callback');
return $val;
}, 10, 2);