Skip to content

Add crontrol_manage_cap filter for the required capability#275

Closed
Gilmoursa wants to merge 1 commit into
johnbillion:developfrom
Gilmoursa:feature/filterable-manage-cap
Closed

Add crontrol_manage_cap filter for the required capability#275
Gilmoursa wants to merge 1 commit into
johnbillion:developfrom
Gilmoursa:feature/filterable-manage-cap

Conversation

@Gilmoursa

Copy link
Copy Markdown

Summary

Closes #157.

Adds a crontrol_manage_cap filter so site owners can override the capability required to access WP Crontrol, without needing to hack around the menu or hook into access checks manually.

The most common use case is multisite installs where manage_options is too permissive — standard site admins have it, but the site owner only wants network admins (who have manage_network_options) to see and use the plugin.

Usage

add_filter( 'crontrol_manage_cap', function( string $cap ): string {
    return 'manage_network_options';
} );

Implementation

  • Added get_manage_cap() helper function in bootstrap.php that applies the filter and defaults to 'manage_options'
  • Replaced all 13 hardcoded 'manage_options' capability strings in bootstrap.php (action handlers + menu registration) with get_manage_cap()
  • Updated WordPressUserContext to call \Crontrol\get_manage_cap() for the same consistency

The edit_files capability used for PHP cron events is unchanged — that one is tied to WordPress core's own "can this user edit PHP files" concept and is separate from the general management cap.

Test plan

  • Default behaviour unchanged — manage_options users can access everything as before
  • With the filter returning manage_network_options, standard site admins can no longer see the Cron Events or Cron Schedules menu items
  • With the filter returning manage_network_options, standard site admins get a wp_die() if they attempt to POST to any crontrol action directly

Introduces a get_manage_cap() helper that wraps apply_filters() so site
owners can override the default manage_options capability check — for
example, multisite installs that want to restrict access to network
admins only via manage_network_options.

All capability checks in bootstrap.php and WordPressUserContext are
updated to use get_manage_cap() instead of the hardcoded string.

Closes johnbillion#157
@johnbillion

Copy link
Copy Markdown
Owner

Thanks for the PR @Gilmoursa, I appreciate it, but I'll close this in favour of the existing PR at #190 which implements fine-grained capabilities for each of the actions in WP Crontrol. That approach means capabilities can be granted to and denied from users and roles directly without having to use filters.

Cheers.

@Gilmoursa

Copy link
Copy Markdown
Author

Thanks for the context, that makes sense! The fine-grained capabilities approach in #190 is a much better solution for users. Looking forward to seeing it land. Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Request: Filter capability required to use WP Crontrol

2 participants