Filter
uncanny-automator
uap_option_all_wpcw_modules
Filters the array of all WP Courseware modules before they are processed by the plugin.
add_filter( 'uap_option_all_wpcw_modules', $callback, 10, 1 );
Description
This filter allows developers to modify the default settings for WP Courseware module options. It fires when WP Courseware module options are being processed. Developers can use this hook to dynamically change the `required`, `current_value`, `validation_type`, or `options` for any WP Courseware module.
Usage
add_filter( 'uap_option_all_wpcw_modules', 'your_function_name', 10, 1 );
Parameters
-
$option(mixed) - This parameter holds the option data that will be filtered.
Return Value
The filtered value.
Examples
/**
* Example function to hook into 'uap_option_all_wpcw_modules' to conditionally disable a WP Courseware module.
*
* This function checks if a specific WP Courseware module (e.g., 'assignments') is enabled.
* If it is, and another condition is met (e.g., a specific user role is active),
* it sets the 'current_value' for that module to false, effectively disabling it via the filter.
*
* @param array $option An array containing the configuration for the WP Courseware module option.
* Expected keys include 'required', 'current_value', 'validation_type', and 'options'.
* @return array The modified or unmodified $option array.
*/
add_filter( 'uap_option_all_wpcw_modules', function( $option ) {
// Ensure we are dealing with the correct option and it's an array
if ( ! is_array( $option ) || ! isset( $option['options'] ) || ! is_array( $option['options'] ) ) {
return $option;
}
// Check if the 'assignments' module is currently enabled in the $option
// The actual structure of $option['options'] might vary, this is an assumption.
// You would need to inspect the actual array structure from the source context.
if ( isset( $option['options']['assignments'] ) && $option['options']['assignments']['current_value'] === true ) {
// Example condition: Only disable if the current user has the 'administrator' role.
// In a real-world scenario, you might check for other user meta, plugin settings, etc.
if ( current_user_can( 'manage_options' ) ) {
// Disable the 'assignments' module by setting its current_value to false.
// Again, adjust the array path based on the actual structure of $option.
if ( isset( $option['options']['assignments'] ) ) {
$option['options']['assignments']['current_value'] = false;
// You might also want to add a notice or log this action if needed for debugging.
// error_log( 'WP Courseware Assignments module has been programmatically disabled for administrators.' );
}
}
}
// Always return the $option array, whether modified or not.
return $option;
}, 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/integrations/wp-courseware/helpers/wp-courseware-helpers.php:131
public function all_wpcw_modules( $label = null, $option_code = 'WPCW_MODULE', $any_option = true ) {
if ( ! $label ) {
$label = esc_attr__( 'Module', 'uncanny-automator' );
}
$modules = array();
$options = array();
if ( Automator()->helpers->recipe->load_helpers ) {
if ( function_exists( 'wpcw_get_modules' ) ) {
$modules = wpcw_get_modules( array( 'number' => 99999 ) );
}
if ( $any_option ) {
$options['-1'] = esc_attr__( 'Any module', 'uncanny-automator' );
}
if ( ! empty( $modules ) ) {
foreach ( $modules as $module ) {
$options[ $module->module_id ] = $module->module_title;
}
}
}
$option = array(
'option_code' => $option_code,
'label' => $label,
'input_type' => 'select',
'required' => true,
// to setup example, lets define the value the child will be based on
'current_value' => false,
'validation_type' => 'text',
'options' => $options,
);
return apply_filters( 'uap_option_all_wpcw_modules', $option );
}