Created
November 29, 2022 21:13
-
-
Save rafaehlers/d093777f182395e80795bdba777a6e4f to your computer and use it in GitHub Desktop.
Modify whether the currently logged-in user can edit an entry that was not created by him - Several Views
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // DO NOT COPY THIS LINE | |
add_filter('gravityview/edit_entry/user_can_edit_entry', 'gv_claim_entry_by_user_data', 20, 3 ); | |
/** | |
* Modify whether the currently logged-in user can edit an entry that was not created by him | |
* | |
* @param boolean $user_can_edit Can the current user edit the current entry? (Default: false) | |
* @param array $entry Gravity Forms entry array | |
* @param int $view_id ID of the view you want to check visibility against {@since 1.15} | |
* | |
* @return bool True: User can edit the existing entry; False: they cannot. | |
*/ | |
function gv_claim_entry_by_user_data( $user_can_edit, $entry, $view_id = 0 ) { | |
global $current_user; | |
/* | |
You may change $user_field below to any of the following user fields | |
Username: $current_user->user_login | |
User email: $current_user->user_email | |
User display name: $current_user->display_name | |
User ID: $current_user->ID | |
*/ | |
$user_field = $current_user->ID; | |
// Make sure the user is logged-in. If not, return existing. | |
if ( ! is_user_logged_in() ) { | |
return $user_can_edit; | |
} | |
switch ( $view_id ): | |
case '123': | |
$id_field_to_check = 3; | |
break; | |
case '245': | |
$id_field_to_check = 4; | |
break; | |
default: | |
return $user_can_edit; //Return existing capabilities for every other View | |
break; | |
endswitch; | |
// field to check isn't set; default to existing | |
if ( ! isset( $entry[$id_field_to_check] ) ) { | |
return $user_can_edit; | |
} | |
$field_to_check = $entry[$id_field_to_check]; | |
if ( ! $field_to_check ) { | |
return $user_can_edit; | |
} | |
// This is where we override existing functionality: | |
// If the user data matches the field to check, let's do this! | |
if( $user_field == $field_to_check ) { | |
return true; | |
} | |
// Otherwise, return existing capabilities | |
return $user_can_edit; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment