Instantly share code, notes, and snippets.

Embed
What would you like to do?
GravityView - Examples for modifying editing capabilities by View ID and Entry ID
<?php
// Forked from gist linked at https://docs.gravityview.co/article/311-gravityview-capabilities
/**
* Modify whether an user has access to edit a specific View.
*
* @see https://codex.wordpress.org/Plugin_API/Filter_Reference/user_has_cap
*
* @param array $allcaps All the capabilities of the user
* @param array $cap [0] Required capability
* @param array $args [0] Requested capability
* [1] User ID
* [2] Associated object ID
*/
function modify_edit_gravityview_filter( $allcaps, $cap, $args ) {
// Bail out if we're not asking about a post:
if ( 'edit_gravityview' != $args[0] )
return $allcaps;
list( $requested_cap, $user_id, $view_id ) = $args;
// Load the View data
$post = get_post( $view_id );
// Do some checks here to discover if you want this person to be able to edit this View
// If so, then set this to true:
$allcaps[ $requested_cap ] = true;
return $allcaps;
}
add_filter( 'user_has_cap', 'modify_edit_gravityview_filter', 10, 3 );
/**
* Modify whether an user has access to edit a specific Gravity Forms entry.
*
* @see https://codex.wordpress.org/Plugin_API/Filter_Reference/user_has_cap
*
* @param array $allcaps All the capabilities of the user
* @param array $cap [0] Required capability
* @param array $args [0] Requested capability
* [1] User ID
* [2] Associated object ID
*/
function modify_gravityview_edit_entries_filter( $allcaps, $cap, $args ) {
// Bail out if we're not asking about a post:
if ( 'gravityview_edit_entries' != $args[0] )
return $allcaps;
list( $requested_cap, $user_id, $entry_id ) = $args;
// Load the Entry data
$entry = GFAPI::get_entry( $entry_id );
$user_info = get_userdata( $user_id );
// You can do some fancy stuff here, like allow editing an entry if the form email matches their profile email address
// Here, we pretend like field #3 is an email address to check against.
if( $user_info && $user_info->user_email === $entry['3'] ) {
// Emails match! Let's let this person edit Entry ID $entry_id
$allcaps[ $requested_cap ] = true;
} else {
// You can prevent editing by setting to false, if you want:
$allcaps[ $requested_cap ] = false;
}
return $allcaps;
}
add_filter( 'user_has_cap', 'modify_gravityview_edit_entries_filter', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment