Skip to content
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

Add specific capability for granular assignment. #29

Merged
merged 9 commits into from
Nov 26, 2019

Conversation

ethanclevenger91
Copy link
Contributor

Also assign cap to administrators, who were already the only ones able to edit this via the manage_options capability.

@ethanclevenger91
Copy link
Contributor Author

Thanks @jeffpaul. The only thing I'll say about this PR is that it currently fetches an option on every load because I don't really trust putting capability assignments behind register_activation_hook. The option is autoloaded so it shouldn't add any queries, but if you guys have preferences either way, I can update accordingly.

@jeffpaul jeffpaul requested a review from helen August 8, 2019 17:58
@jeffpaul
Copy link
Member

jeffpaul commented Sep 5, 2019

Resolves #28

@adamsilverstein
Copy link

Hey @ethanclevenger91 - thanks for the PR!

This looks like it will work...

I don't really trust putting capability assignments behind register_activation_hook

Any reason in particular you don't trust this approach?

It would be nice of the plugin to also remove the custom capability when it is deactivated which makes me lean towards adding on activation. Thoughts?

@ethanclevenger91
Copy link
Contributor Author

ethanclevenger91 commented Sep 5, 2019

@adamsilverstein In a very edge case, it could be circumvented through directly editing the active plugins value in wp_options. The idea of setting permissions being tied to a dashboard action of any kind has always seemed flimsy, but I can certainly update to use register_activation_hook if preferred. In that case, yeah, it would make sense to add the permission removal as well.

@adamsilverstein
Copy link

I don't have a strong opinion either way and didn't find anything in our standards or prior art. I'm curious what @helen thinks.

@helen
Copy link
Contributor

helen commented Sep 18, 2019

I don't know that I have a very strong opinion but to me it makes sense to tie it to activation and clean up on deactivation.

it could be circumvented through directly editing the active plugins value in wp_options

I think at the point of this happening you probably have a lot of other problems as well :) which isn't to say it's a dismissable concern, just that I wouldn't take that as the primary decider for an approach.

@adamsilverstein
Copy link

@ethanclevenger91 Based on Helen's feedback, can you please update this PR to add/remove the custom capabilities during plugin activation/deactivation?

@ethanclevenger91
Copy link
Contributor Author

@adamsilverstein will do

@ethanclevenger91
Copy link
Contributor Author

@adamsilverstein updated, and also moved to main plugin file, since it didn't really feel like it fell under the same purview as the other contents of the admin.php file.

@adamsilverstein
Copy link

adamsilverstein commented Oct 7, 2019

@ethanclevenger91 thanks for updating this.

I am wondering about one potential issue with adding the capability in the plugin activation hook: users who already have the plugin activated will not have the capability added and therefore won't be able to access the Ads-txt admin page. (oops, apologies for not catching this earlier when I suggested moving to activation)

Two possible solutions for this issue would be:

  1. Move adding the capability back to admin_init as you had it previously (while maintaining the removal in plugin deactivation to clean up)
  2. Add an upgrade notice informing users they need to deactivate/reactivate the plugin after upgrading.

My preference at this point is 1 since that doesn't require user intervention. Perhaps we can have a plan to change to adding the capability during plugin activation in a future release, once we are sure existing users already have it.

@adamsilverstein
Copy link

I pushed some updates:

  • add hook on admin_init, also keeping hook on activation - otherwise capability wasn't added in time on activation for menu to appear - requiring a second page refresh.
  • switch check to avoid re-adding capability to use has_cap.
  • move the capability name to a plugin constant.

Copy link

@adamsilverstein adamsilverstein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice improvement @ethanclevenger91 - thanks!

@ethanclevenger91
Copy link
Contributor Author

@adamsilverstein thanks for those improvements! I have criminally overlooked has_cap, great idea. Sorry I wasn't able to get to this myself sooner.

@jeffpaul jeffpaul mentioned this pull request Nov 25, 2019
17 tasks
helen
helen previously approved these changes Nov 26, 2019
Copy link
Contributor

@helen helen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@helen helen dismissed stale reviews from adamsilverstein and themself via cc7897f November 26, 2019 18:21
Web UI isn't quite ready for prime-time usage, I suppose
@helen helen merged commit 3d6de15 into 10up:develop Nov 26, 2019
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.

4 participants