14 defined(
'ABSPATH' ) || exit;
36 if( ! self::$instance ) {
37 self::$instance =
new self;
40 return self::$instance;
56 add_filter(
'members_get_capabilities', array(
'GravityView_Roles_Capabilities',
'merge_with_all_caps' ) );
57 add_action(
'members_register_cap_groups', array( $this,
'members_register_cap_group' ), 20 );
58 add_filter(
'user_has_cap', array( $this,
'filter_user_has_cap' ), 10, 4 );
59 add_action(
'admin_init', array( $this,
'add_caps') );
80 if( ! $usercaps || ! $caps ) {
87 if( ! empty( $usercaps[
'gravityview_full_access'] ) ) {
89 $all_gravityview_caps = self::all_caps();
91 foreach( $all_gravityview_caps as $gv_cap ) {
92 $usercaps[ $gv_cap ] =
true;
95 unset( $all_gravityview_caps );
119 $gf_to_gv_caps = array(
120 'gravityforms_edit_entries' =>
'gravityview_edit_others_entries',
121 'gravityforms_delete_entries' =>
'gravityview_delete_others_entries',
122 'gravityforms_view_entry_notes' =>
'gravityview_view_entry_notes',
123 'gravityforms_edit_entry_notes' =>
'gravityview_delete_entry_notes',
126 foreach ( $gf_to_gv_caps as $gf_cap => $gv_cap ) {
127 if ( isset( $usercaps[ $gf_cap ] ) && ! isset( $usercaps[ $gv_cap ] ) ) {
128 $usercaps[ $gv_cap ] = $usercaps[ $gf_cap ];
142 if ( function_exists(
'members_register_cap_group' ) ) {
145 'label' => __(
'GravityView',
'gk-gravityview' ),
146 'icon' =>
'gv-icon-astronaut-head',
147 'caps' => self::all_caps(),
148 'merge_added' =>
true,
149 'diff_added' =>
false,
165 $return_caps = array_merge( $caps, self::all_caps() );
167 return array_unique( $return_caps );
182 if ( ! isset( $wp_roles ) ) {
183 $wp_roles =
new WP_Roles();
200 if ( is_object( $wp_roles ) ) {
202 $_use_db_backup = $wp_roles->use_db;
208 $wp_roles->use_db =
false;
210 $capabilities = self::all_caps(
false,
false );
212 foreach ( $capabilities as $role_slug => $role_caps ) {
213 foreach ( $role_caps as $cap ) {
214 $wp_roles->add_cap( $role_slug, $cap );
223 update_option( $wp_roles->role_key, $wp_roles->roles );
228 $wp_roles->use_db = $_use_db_backup;
244 public static function all_caps( $single_role =
false, $flat_array =
true ) {
247 $administrator_caps = array(
248 'gravityview_full_access',
249 'gravityview_view_settings',
250 'gravityview_edit_settings',
251 'gravityview_uninstall',
252 'gravityview_contact_support',
254 'edit_others_gravityviews',
255 'edit_private_gravityviews',
256 'edit_published_gravityviews',
260 $editor_caps = array(
261 'read_private_gravityviews',
262 'delete_private_gravityviews',
263 'delete_others_gravityviews',
264 'publish_gravityviews',
265 'delete_published_gravityviews',
269 'gravityview_edit_others_entries',
270 'gravityview_moderate_entries',
271 'gravityview_delete_others_entries',
272 'gravityview_add_entry_notes',
273 'gravityview_view_entry_notes',
274 'gravityview_delete_entry_notes',
275 'gravityview_email_entry_notes',
279 $author_caps = array(
281 'gravityview_edit_entries',
282 'gravityview_edit_entry',
283 'gravityview_edit_form_entries',
284 'gravityview_delete_entries',
285 'gravityview_delete_entry',
289 $contributor_caps = array(
291 'delete_gravityviews',
292 'gravityview_getting_started',
293 'gravityview_support_port',
297 $subscriber_caps = array(
298 'gravityview_view_entries',
299 'gravityview_view_others_entries',
302 $subscriber = $subscriber_caps;
303 $contributor = array_merge( $contributor_caps, $subscriber_caps );
304 $author = array_merge( $author_caps, $contributor_caps, $subscriber_caps );
305 $editor = array_merge( $editor_caps, $author_caps, $contributor_caps, $subscriber_caps );
306 $administrator = array_merge( $administrator_caps, $editor_caps, $author_caps, $contributor_caps, $subscriber_caps );
307 $all = $administrator;
311 $caps = isset( ${$single_role} ) ? ${$single_role} :
false;
312 return $flat_array ? $caps : array( $single_role => $caps );
316 return $flat_array ? $all : compact(
'administrator',
'editor',
'author',
'contributor',
'subscriber' );
336 public static function has_cap( $caps_to_check =
'', $object_id = null, $user_id = null ) {
355 $allow_logged_out = apply_filters(
'gravityview/capabilities/allow_logged_out',
false, $caps_to_check, $object_id, $user_id );
357 if (
true === $allow_logged_out ) {
359 $all_caps = self::all_caps(
'editor');
361 if( array_intersect( $all_caps, (array) $caps_to_check ) ) {
372 if ( ( ! is_user_logged_in() && ! $allow_logged_out ) || empty( $caps_to_check ) ) {
379 $caps_to_check = self::maybe_add_full_access_caps( $caps_to_check );
381 foreach ( $caps_to_check as $cap ) {
382 if( ! is_null( $object_id ) ) {
383 $has_cap = $user_id ? user_can( $user_id, $cap, $object_id ) : current_user_can( $cap, $object_id );
385 $has_cap = $user_id ? user_can( $user_id, $cap ) : current_user_can( $cap );
407 $caps_to_check = (array)$caps_to_check;
409 $all_gravityview_caps = self::all_caps();
412 if( $has_gravityview_caps = array_intersect( $caps_to_check, $all_gravityview_caps ) ) {
413 $caps_to_check[] =
'gravityview_full_access';
416 $all_gravity_forms_caps = class_exists(
'GFCommon' ) ? GFCommon::all_caps() : array();
419 if( $all_gravity_forms_caps = array_intersect( $caps_to_check, $all_gravity_forms_caps ) ) {
420 $caps_to_check[] =
'gform_full_access';
423 return array_unique( $caps_to_check );
436 if ( is_object( $wp_roles ) ) {
439 $capabilities = self::all_caps();
442 foreach( $wp_roles->get_names() as $role_slug => $role_name ) {
443 foreach ( $capabilities as $cap ) {
444 $wp_roles->remove_cap( $role_slug, $cap );
451 add_action(
'init', array(
'GravityView_Roles_Capabilities',
'get_instance' ), 1 );
remove_caps()
Remove all GravityView caps_to_check from all roles.
static all_caps( $single_role=false, $flat_array=true)
Get an array of GravityView capabilities.
static has_cap( $caps_to_check='', $object_id=null, $user_id=null)
Check whether the current user has a capability.
wp_roles()
Retrieves the global WP_Roles instance and instantiates it if necessary.
static merge_with_all_caps( $caps)
Merge capabilities array with GravityView capabilities.
static maybe_add_full_access_caps( $caps_to_check=array())
Add Gravity Forms and GravityView's "full access" caps when any other caps are checked against...
add_gravity_forms_usercaps_to_gravityview_caps( $usercaps)
If a user has been assigned custom capabilities for Gravity Forms, but they haven't been assigned sim...
add_caps()
Add capabilities to their respective roles if they don't already exist This could be simpler...
filter_user_has_cap( $usercaps=array(), $caps=array(), $args=array(), $user=NULL)
Add support for gravityview_full_access capability, and.
__construct()
Get things going.
members_register_cap_group()
Add GravityView group to Members 1.x plugin management screen.