SHARE
TWEET

Gravity Forms granular form capabilities management

dwenaus Mar 21st, 2013 221 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Gravity Forms granular form capabilities management. see usage below
  3.  *
  4.  * Usage:
  5.  * install Members plugin http://wordpress.org/extend/plugins/members/
  6.  * edit the capabilities for a role (ie. editor)
  7.  * add a new capabilty for each form users with that role can access
  8.  * ie. add a capability called gravityforms_manage_form_3 to enable the user to view only form with id 3
  9.  *
  10.  * Caveat: the entries of the first form sorted alphabetically by title will always be visible, so add a placeholder titles something like "0 form"
  11.  */
  12. function smc_granular_form_management( $all_caps, $cap, $args ) {
  13.         $cap = reset( $cap );
  14.  
  15.         if ( strpos( $cap, 'gravityforms_' ) !== 0 )
  16.                 return $all_caps;
  17.  
  18.         // save initial values
  19.         $view_entries = isset( $all_caps['gravityforms_view_entries'] ) ? $all_caps['gravityforms_view_entries'] : 0;
  20.         $export_entries = isset( $all_caps['gravityforms_export_entries'] ) ? $all_caps['gravityforms_export_entries'] : 0;
  21.         $edit_forms = isset( $all_caps['gravityforms_edit_forms'] ) ? $all_caps['gravityforms_edit_forms'] : 0;
  22.  
  23.         // add these so that the menus show up
  24.         $all_caps['gravityforms_view_entries'] = 1;
  25.         $all_caps['gravityforms_export_entries'] = 1;
  26.         $all_caps['gravityforms_edit_forms'] = 1;
  27.  
  28.         // viewing entries, editing form
  29.         $form_id = isset( $_GET['id'] ) ? $_GET['id'] : null;
  30.  
  31.         // exporting entries
  32.         if ( ! $form_id )
  33.                 $form_id = isset( $_POST['export_form'] ) ? $_POST['export_form'] : null;
  34.  
  35.         // see if the capability exists for this form for this user
  36.         $per_form_cap = 'gravityforms_manage_form_' . $form_id;
  37.  
  38.         if ( $form_id && isset( $all_caps[ $per_form_cap ] ) && 1 == $all_caps[ $per_form_cap ] ) {
  39.                 // enable all caps when viewing authorized form
  40.                 $all_caps['gravityforms_edit_entries'] = 1;
  41.                 $all_caps['gravityforms_edit_entry_notes'] = 1;
  42.                 $all_caps['gravityforms_edit_forms'] = 1;
  43.                 $all_caps['gravityforms_export_entries'] = 1;
  44.                 $all_caps['gravityforms_delete_entries'] = 1;
  45.                 $all_caps['gravityforms_preview_forms'] = 1;
  46.                 $all_caps['gravityforms_view_entries'] = 1;
  47.                 $all_caps['gravityforms_view_entry_notes'] = 1;
  48.         } elseif ( $form_id ) {
  49.                 // return caps to defaults when on potentially unauthorized form
  50.                 $all_caps['gravityforms_view_entries'] = $view_entries;
  51.                 $all_caps['gravityforms_export_entries'] = $export_entries;
  52.                 $all_caps['gravityforms_edit_forms'] = $edit_forms;
  53.         }
  54.  
  55.         return $all_caps;
  56. }
  57. add_filter( 'user_has_cap', 'smc_granular_form_management', 10, 3 );
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top