Skip to content

Instantly share code, notes, and snippets.

@rafaehlers
Created January 16, 2024 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafaehlers/c8b19b19dab3146647b2b99953d35ada to your computer and use it in GitHub Desktop.
Save rafaehlers/c8b19b19dab3146647b2b99953d35ada to your computer and use it in GitHub Desktop.
Allow logged-out users to edit entrie
<?php //DO NOT COPY THIS LINE
// Removes Edit Entry "nonce" validation. Edit Entry checks whether an user has the ability to edit an entry, but it also checks a "nonce" to make sure that the Edit Entry link was recently generated by the current user visiting the page. This can sometimes cause problems; this code removes that "nonce" validation.
add_filter( 'gravityview/edit_entry/verify_nonce', '__return_true' );
// allow non logged-in users to edit entries
add_filter( 'gravityview/edit_entry/user_can_edit_entry', '__return_true' );
add_filter( 'gravityview/capabilities/allow_logged_out', '__return_true' );
add_filter( 'user_has_cap', function( $caps ) {
$caps['gravityview_edit_others_entries'] = true;
return $caps;
} );
/**
* Allow logged-out users to edit entries in Form #1
*
* @param boolean $allow_logged_out Allow the capability check or bail without even checking. Default: false. Do not allow. Do not pass Go. Do not collect $200.
* @param string|array $caps_to_check Single capability or array of capabilities to check against
* @param int|null $object_id (optional) Parameter can be used to check for capabilities against a specific object, such as a post or us.
* @param int|null $user_id (optional) Check the capabilities for a user who is not necessarily the currently logged-in user.
*/
add_filter( 'gravityview/capabilities/allow_logged_out', 'gravityview_allow_logged_out_users_to_edit_entries', 10, 4 );
function gravityview_allow_logged_out_users_to_edit_entries( $allow_logged_out = false, $caps_to_check = '', $object_id = null, $user_id = null ) {
$caps_to_check = (array) $caps_to_check;
$object_id = (int) $object_id;
/**
* Allow visitors to edit all entries from Form #37
* (This capability is also used by the Inline Entries by GravityView plugin)
*/
if ( in_array( 'gravityview_edit_form_entries', $caps_to_check ) && 1 === $object_id ) {
return true;
}
return $allow_logged_out;
}
/**
* Example usage of the `gravityview/capabilities/allow_logged_out` filter
*
* !!! IMPORTANT: Only return true for the specific capabilites you want to enable !!!
*
* @param boolean $allow_logged_out Allow the capability check or bail without even checking. Default: false. Do not allow. Do not pass Go. Do not collect $200.
* @param string|array $caps_to_check Single capability or array of capabilities to check against
* @param int|null $object_id (optional) Parameter can be used to check for capabilities against a specific object, such as a post or us.
* @param int|null $user_id (optional) Check the capabilities for a user who is not necessarily the currently logged-in user.
*/
add_filter( 'gravityview/capabilities/allow_logged_out', 'gravityview_allow_logged_out_users_to_edit_entries', 10, 4 );
function gravityview_allow_logged_out_users_to_edit_entries( $allow_logged_out = false, $caps_to_check = '', $object_id = null, $user_id = null ) {
$caps_to_check = (array) $caps_to_check;
$object_id = (int) $object_id;
/**
* Allow logged-out visitors to moderate entries (approve/disapprove/unapprove)
*/
if ( in_array( 'gravityview_moderate_entries', $caps_to_check ) ) {
return true;
}
/**
* Allow visitors to edit all entries
*/
if ( in_array( 'gravityforms_edit_entries', $caps_to_check ) ) {
// return true; /* <--- Don't use this unless you're absolutely certain! */
}
/**
* Allow visitors to edit a single entry, in this example, Entry #23413
*/
if ( in_array( 'gravityview_edit_others_entries', $caps_to_check ) && 23413 === $object_id ) {
return true;
}
/**
* Allow visitors to edit all entries from Form #37
* (This capability is also used by the Inline Entries by GravityView plugin)
*/
if ( in_array( 'gravityview_edit_form_entries', $caps_to_check ) && 37 === $object_id ) {
return true;
}
return $allow_logged_out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment