Created
May 19, 2021 19:10
-
-
Save bennemann/b8863051541104e9e3d7b58be8be18e3 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
This file contains hidden or 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 | |
| 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 ) { | |
| $run_on_view_id = 9840; // Change the View to the ID of the View you want to match | |
| $id_field_to_check = 4; // Change this to the ID of the field you want to match against the user. | |
| 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->display_name; //User display name | |
| /* That is all you need to edit! */ | |
| // Make sure the user is logged-in. If not, return existing. | |
| if ( ! is_user_logged_in() ) { | |
| return $user_can_edit; | |
| } | |
| // Don't run on this View; default to existing | |
| if( $run_on_view_id !== (int) $view_id ) { | |
| return $user_can_edit; | |
| } | |
| // 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