toolkit_settings_module_switch_cap
Filters the capability required to switch toolkit settings modules on the admin screen.
add_filter( 'toolkit_settings_module_switch_cap', $callback, 10, 1 );
Description
Allows developers to modify the capability required to activate or deactivate Uncanny Toolkit modules via AJAX. By default, only administrators ('manage_options') can perform this action. Developers can change this capability to restrict or broaden access to module management.
Usage
add_filter( 'toolkit_settings_module_switch_cap', 'your_function_name', 10, 1 );
Return Value
The filtered value.
Examples
/**
* Filters the capability required to switch modules in the Uncanny Toolkit settings.
*
* By default, only users with the 'manage_options' capability (Administrators)
* can switch modules. This filter allows developers to change this requirement
* to a more restrictive or permissive capability if needed.
*
* @param string $capability The current capability required to switch modules.
* @return string The modified capability required to switch modules.
*/
function uncanny_toolkit_custom_module_switch_cap( $capability ) {
// Example: Allow users with the 'edit_posts' capability (Contributors, Authors, Editors, Administrators)
// to switch modules. In a real-world scenario, you might want to be more specific
// or check for custom roles.
if ( current_user_can( 'edit_posts' ) ) {
return 'edit_posts';
}
// If the user doesn't have 'edit_posts', fall back to the default 'manage_options'.
return $capability;
}
add_filter( 'toolkit_settings_module_switch_cap', 'uncanny_toolkit_custom_module_switch_cap', 10, 1 );
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/config.php:591
public static function ajax_activate_deactivate_module() {
// Nonce verification
if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'uncanny_toolkit' ) ) {
echo 'nonce failed';
wp_die();
}
$capability = apply_filters( 'toolkit_settings_module_switch_cap', 'manage_options' );
if ( ! current_user_can( $capability ) ) {
echo __( 'You must be an admin to save settings', 'uncanny-learndash-toolkit' );
wp_die();
}
if ( ! isset( $_POST['value'] ) ) {
echo 'Value field missing.';
wp_die();
}
$value = stripslashes( $_POST['value'] );
$active_classes = get_option( 'uncanny_toolkit_active_classes', 0 );
if ( 0 !== $active_classes ) {
if ( ! is_array( $active_classes ) ) {
$active_classes = array();
}
if ( 'active' === $_POST['active'] ) {
$new_classes = array_merge( array( $value => $value ), $active_classes );
} elseif ( 'inactive' === $_POST['active'] ) {
unset( $active_classes[ $value ] );
$new_classes = $active_classes;
}
update_option( 'uncanny_toolkit_active_classes', $new_classes );
$response = 'success';
} else {
$save_settings = add_option( 'uncanny_toolkit_active_classes', array( $value => $value ) );
$response = ( $save_settings ) ? 'success' : 'notsaved';
}
// If the uo dashboard module is being turned on then set the default template as 3_0
if ( 'uncanny_pro_toolkit\learnDashMyCourses' === $value ) {
if ( 'active' === $_POST['active'] ) {
update_option(
'uncanny_pro_toolkitlearnDashMyCourses',
array(
array(
'name' => 'uo_dashboard_template',
'value' => '3_0',
),
),
'no'
);
}
}
// If the frontend login module is being turned on then check if settings are available or not.
if ( 'uncanny_learndash_toolkitFrontendLoginPlus' === $value ) {
if ( 'active' === $_POST['active'] ) {
$existing_settings = get_option( 'FrontendLoginPlus', '' );
if ( empty( $existing_settings ) ) {
$default_settings = array(
array(
'name' => 'uo_frontendloginplus_enable_ajax_support',
'value' => 'on',
),
);
update_option( 'FrontendLoginPlus', $default_settings );
}
}
}
echo $response; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
wp_die();
}