ulgm_is_hierarchy_setting_enabled
Filters whether the hierarchy setting is enabled for a specific group, allowing modification before the setting is used.
add_filter( 'ulgm_is_hierarchy_setting_enabled', $callback, 10, 4 );
Description
Filters whether the group hierarchy setting is enabled. Developers can use this to conditionally enable or disable group hierarchy features based on custom logic or specific group IDs, influencing how group enrollments and reports are displayed.
Usage
add_filter( 'ulgm_is_hierarchy_setting_enabled', 'your_function_name', 10, 4 );
Parameters
-
$is_hierarchy_setting_enabled(mixed) - This parameter indicates whether the hierarchy setting is currently enabled for the given group.
-
$group_id(mixed) - This parameter indicates whether the hierarchy setting is currently enabled.
-
$bypass_transient(mixed) - This parameter represents the ID of the group for which the hierarchy setting is being checked.
-
$disable_hierarchy(mixed) - This parameter is used to determine whether to bypass any cached (transient) data and fetch the hierarchy setting directly.
Return Value
The filtered value.
Examples
/**
* Example of how to filter the 'ulgm_is_hierarchy_setting_enabled' hook.
*
* This example will forcefully disable the hierarchy setting for a specific group ID.
* In a real-world scenario, you might use this to conditionally enable or disable
* hierarchy based on user roles, specific group configurations, or other logic.
*/
add_filter(
'ulgm_is_hierarchy_setting_enabled',
function ( $is_hierarchy_setting_enabled, $group_id, $bypass_transient, $disable_hierarchy ) {
// Define a specific group ID for which we want to disable hierarchy.
$specific_group_to_disable_hierarchy = 123;
// Check if the current group ID matches our target group ID.
if ( absint( $group_id ) === $specific_group_to_disable_hierarchy ) {
// If it matches, force the hierarchy setting to be disabled.
return false;
}
// Otherwise, return the original value of the setting.
return $is_hierarchy_setting_enabled;
},
10, // Priority: 10 is the default priority.
4 // Accepted Args: The filter hook passes 4 arguments.
);
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/classes/learndash/learndash-function-overrides.php:331
src/classes/learndash/learndash-function-overrides.php:669
src/classes/reports/group-reports-interface.php:451
src/classes/helpers/rest-api-end-points.php:1881
src/classes/helpers/shared-functions.php:1315
public static function learndash_group_enrolled_courses( $group_id = 0, $bypass_transient = false, $disable_hierarchy = false ) {
// For group hierarchy support
$is_hierarchy_setting_enabled = false;
if ( function_exists( 'learndash_is_groups_hierarchical_enabled' ) && learndash_is_groups_hierarchical_enabled() && 'yes' === get_option( 'ld_hierarchy_settings_child_groups', 'no' ) ) {
$is_hierarchy_setting_enabled = true;
}
if ( $disable_hierarchy ) {
$is_hierarchy_setting_enabled = false;
}
$is_hierarchy_setting_enabled = apply_filters(
'ulgm_is_hierarchy_setting_enabled',
$is_hierarchy_setting_enabled,
$group_id,
$bypass_transient,
$disable_hierarchy
);
$group_id = absint( $group_id );
// Bail early if group id is not set
if ( 0 === $group_id || empty( $group_id ) || is_null( $group_id ) ) {
return array();
}
// check if there's transient data available
if ( false === $bypass_transient ) {
$transient_key = 'learndash_group_enrolled_courses_' . $group_id;
if ( $is_hierarchy_setting_enabled ) {
$transient_key .= '_hierarchy';
}
$group_users_objects = self::get_ld_transient( $transient_key );
if ( ! empty( $group_users_objects ) ) {
return $group_users_objects;
}
}
$search_condition = " meta_key = 'learndash_group_enrolled_{$group_id}' ";
if ( $is_hierarchy_setting_enabled ) {
$group_children = learndash_get_group_children( $group_id );
if ( ! empty( $group_children ) ) {
foreach ( $group_children as $child_group_id ) {
$child_group_id = absint( $child_group_id );
$search_condition .= " OR meta_key = 'learndash_group_enrolled_{$child_group_id}' ";
}
}
}
global $wpdb;
$qry = "SELECT pm.post_id FROM $wpdb->postmeta pm LEFT JOIN $wpdb->posts p ON p.ID = pm.post_id WHERE 1=1 AND p.post_status = 'publish' AND ( $search_condition ) ";
$results = $wpdb->get_col( $qry );
if ( empty( $results ) ) {
$results = array();
}
if ( ! empty( $results ) ) {
$results = array_values( array_unique( $results ) );
}
/*
* Filter for customizing group courses
* ulgm_learndash_group_enrolled_courses
*/
$results = apply_filters( 'ulgm_learndash_group_enrolled_courses', $results, $group_id );
if ( false === $bypass_transient ) {
self::set_ld_transient( $transient_key, $results );
}
return $results;
}