Created
March 20, 2020 22:16
-
-
Save bennemann/21772eeb4457e70dd38404032ca27a4e 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_email', 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_email( $user_can_edit, $entry, $view_id = 0 ) { | |
| $run_on_view_id = 60; // Change the View to the ID of the View you want to match | |
| $email_field_id = 5; // Change this to the ID of the email field you want to match. | |
| /* 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; | |
| } | |
| // Email field isn't set; default to existing | |
| if ( ! isset( $entry[ $email_field_id ] ) ) { | |
| return $user_can_edit; | |
| } | |
| $email_to_check = is_email( $entry[ $email_field_id ] ); | |
| // Email isn't valid; default to existing | |
| if ( ! $email_to_check ) { | |
| return $user_can_edit; | |
| } | |
| global $current_user; | |
| // This is where we override existing functionality: | |
| // If the email matches the email to check, let's do this! | |
| if( $current_user->user_email === $email_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