Skip to content

Instantly share code, notes, and snippets.

@New0
Last active November 2, 2021 12:07
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 New0/06d5b7664a8cfbb256a13bc68de0aa98 to your computer and use it in GitHub Desktop.
Save New0/06d5b7664a8cfbb256a13bc68de0aa98 to your computer and use it in GitHub Desktop.
Allow editors to manage Ninja Forms > 3.6 Submissions
<?php
/**
* Filter hook used in the API route permission callback to retrieve submissions
*
* return bool as for authorized or not.
*/
add_filter( 'ninja_forms_api_allow_get_submissions', 'nf_define_permission_level', 10, 2 );
add_filter( 'ninja_forms_api_allow_delete_submissions', 'nf_define_permission_level', 10, 2 );
add_filter( 'ninja_forms_api_allow_update_submission', 'nf_define_permission_level', 10, 2 );
add_filter( 'ninja_forms_api_allow_handle_extra_submission', 'nf_define_permission_level', 10, 2 );
add_filter( 'ninja_forms_api_allow_email_action', 'nf_define_permission_level', 10, 2 );
function nf_define_permission_level() {
$allowed = \current_user_can("delete_others_posts");
return $allowed;
}
@ouija
Copy link

ouija commented Oct 19, 2021

Awesome, thanks. Note that you have an unnecessary closing bracket at the end of your function.

I ran into issues with this after the plugin got updated last week, specifically with users assigned to the shop_manager role, and (after looking at the source code for the related /wp-content/plugins/ninja-forms/includes/Routes/Submissions.php page, which defines access for the ninjaforms submissions api endpoint now being utilized in the submissions admin page), I realized that this latest update is now checking if the user role has the manage_options permission (which the modified shop_manager role in my client's configuration does not) and instead I've opted to instead modify this code to check against the role itself, rather than a certain permission.

For example:

add_filter( 'ninja_forms_api_allow_get_submissions', 'nf_define_permission_level', 10, 2 );
add_filter( 'ninja_forms_api_allow_delete_submissions', 'nf_define_permission_level', 10, 2 );
add_filter( 'ninja_forms_api_allow_update_submission', 'nf_define_permission_level', 10, 2 );
add_filter( 'ninja_forms_api_allow_handle_extra_submission', 'nf_define_permission_level', 10, 2 );
add_filter( 'ninja_forms_api_allow_email_action', 'nf_define_permission_level', 10, 2 );

function nf_define_permission_level() {
    $allowedRoles = array('administrator', 'shop_manager');
    $user = wp_get_current_user();

    if (array_intersect($allowedRoles, $user->roles)) {
        $allowed = true;
    } else $allowed = false; 
  
    return $allowed;
}

Alternativly, you could also install the User Role Editor plugin and modify whatever role you need to have this manage_options permission:

Hope this helps anyone else looking to achive somethig similar.

@New0
Copy link
Author

New0 commented Oct 19, 2021

Thank you @ouija , originally this function was written inside a filter hook and I forgot to remove the closing tags.

Thanks also for sharing your example of use, we're returning a boolean so the conditions that lead to it can be designed as needed. That will certainly help others to determine what they need.

@ouija
Copy link

ouija commented Oct 19, 2021

Thank you @ouija , originally this function was written inside a filter hook and I forgot to remove the closing tags.

No problem! I was actually going to create my own gist but came across yours and figured I'd add to it instead, and have also shared this with some threads in the Wordpress Support Forums.

@Banilancer
Copy link

@New0 May I know how to implement your code? Is it just put your code into a specific folder or add the code into a specific php file? Many thanks for your help.

@New0
Copy link
Author

New0 commented Oct 29, 2021

Hello @Banilancer you can use this code in the functions.php file of your (child-) theme or in a custom plugin if you add the plugin header information at the top of this file and upload it in the plugins folder of your WP installation.

@lujo76
Copy link

lujo76 commented Nov 2, 2021

Thanks New0 and ouija for this help - this update has really caused some issues for my clients situation - following on from Banilancer, can/do I add the...

// Must use all three filters for this to work properly.
add_filter( 'ninja_forms_admin_parent_menu_capabilities', 'nf_subs_capabilities' ); // Parent Menu
add_filter( 'ninja_forms_admin_all_forms_capabilities', 'nf_subs_capabilities' ); // Forms Submenu
add_filter( 'ninja_forms_admin_submissions_capabilities', 'nf_subs_capabilities' ); // Submissions Submenu

function nf_subs_capabilities( $cap ) {
return 'edit_posts'; // EDIT: User Capability
}

...to the functions.php file too. I'm not having much luck as yet. Also are the User_capability and current_user_can related?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment