Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save dexit/a5cd9568eb9bb30b663285ca96902339 to your computer and use it in GitHub Desktop.

Select an option

Save dexit/a5cd9568eb9bb30b663285ca96902339 to your computer and use it in GitHub Desktop.
A method to provide full management of form submissions to less-privileged WP roles.
<?php
/*
Plugin Name: Elementor form submission access
Description: Promotes the Elementor Pro form submissions page to top level, and bypasses the excessive and inflexible restrictions on access.
Version: 2.0.0
Author: Some netizen
Update URI: do-not-auto-update
*/
namespace Elementor\Core\Admin\Menu;
defined('ABSPATH') or die;
/**
* Shim the WordPress submenu function by creating a namespaced version that only exists when Elementor creates its
* menu items. That means it can intercept the form submissions link to promote it to the top level but other plugins
* and WordPress code do not see this shimmed version.
*/
function add_submenu_page($parent_slug, $page_title, $menu_title, $capability, $menu_slug, $callback = '', $position = null) {
if ($menu_slug === 'e-form-submissions') {
return \add_menu_page($page_title, $menu_title, 'etor_submissions_read', $menu_slug, $callback, 'dashicons-forms', 3.5);
}
return \add_submenu_page($parent_slug, $page_title, $menu_title, $capability, $menu_slug, $callback, $position);
}
/**
* Wait till elementor has registered its REST endpoints, and replace the permissions check with capabilities you can
* assign to whoever you need, instead of Elementor's use of the generic 'manage_options' capability.
*/
add_filter(
'rest_endpoints',
function($endpoints) {
$permission_callback = function($request) {
if ($request->get_method() === 'GET') {
return current_user_can('etor_submissions_read');
}
return current_user_can('etor_submissions_full_management');
};
foreach ($endpoints as $route => $handlers) {
if (preg_match('#^/elementor/v1/form(?:-submission)?s(?:$|/)#', $route)) {
if (!isset($handlers['callback'])) {
foreach ($handlers as $i => $handler) {
if (isset($handler['permission_callback'])) {
$endpoints[$route][$i]['permission_callback'] = $permission_callback;
}
}
}
elseif (isset($handlers['permission_callback'])) {
$endpoints[$route]['permission_callback'] = $permission_callback;
}
}
}
return $endpoints;
},
444444444
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment