Created
November 8, 2017 22:13
-
-
Save gera3d/ee1e2e4527de5620e76015e1298e4bc4 to your computer and use it in GitHub Desktop.
Get Groups
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Gera | |
* Get all groups | |
* | |
* @since 2.1.0 | |
* | |
* @param bool $id_only return id's only | |
* @return array groups | |
*/ | |
function learndash_get_groups( $id_only = false, $current_user_id = 0 ) { | |
if ( empty( $current_user_id ) ) { | |
if ( !is_user_logged_in() ) return $course_ids; | |
$current_user_id = get_current_user_id(); | |
} | |
if ( learndash_is_group_leader_user( $current_user_id ) ) { | |
return learndash_get_administrators_group_ids( $current_user_id ); | |
} else if ( learndash_is_admin_user( $current_user_id ) ) { | |
$groups_query_args = array( | |
'post_type' => 'groups', | |
'nopaging' => true | |
); | |
if ( $id_only ) { | |
$groups_query_args['fields'] = 'ids'; | |
} | |
$groups_query = new WP_Query( $groups_query_args ); | |
return $groups_query->posts; | |
} | |
} | |
/* | |
* Here is me adding the items----> | |
*/ | |
/* | |
* Add NMR filters if both plugins are active | |
*/ | |
function nmr_wcm_init(){ | |
add_filter( 'nav_menu_roles', 'learndash_get_groups_nuvo' ); | |
add_filter( 'nav_menu_roles_item_visibility', 'nmw_wcm_item_visibility', 10, 2 ); | |
} | |
add_action( 'plugins_loaded', 'nmr_wcm_init', 20 ); | |
/** | |
* Get The Items and load them into the menu | |
* | |
* @since 2.1.0 | |
* | |
* @param bool $id_only return id's only | |
* @return array groups | |
*/ | |
function learndash_get_groups_nuvo( $id_only = ture, $current_user_id = 0 ) { | |
if ( empty( $current_user_id ) ) { | |
if ( !is_user_logged_in() ) return $course_ids; | |
$current_user_id = get_current_user_id(); | |
} | |
$groups_query_args = array( | |
'post_type' => 'groups', | |
'nopaging' => true | |
); | |
if ( $id_only ) { | |
$groups_query_args['fields'] = 'ids'; | |
} | |
foreach( $groups_query_args as $groups_query_arg ){ | |
$groups_query_args['fields'] = 'ids'; | |
} | |
return $groups_query_args; | |
} | |
/* | |
* Change visibilty of each menu item | |
* param: $visible boolean | |
* param: $item object, the complete menu object. Nav Menu Roles adds its info to $item->roles | |
* $item->roles can be "in" (all logged in), "out" (all logged out) or an array of specific roles | |
* return boolean | |
*/ | |
function nmw_wcm_item_visibility( $visible, $item ){ | |
if( ! $visible && isset( $item->roles ) && is_array( $item->roles ) ){ | |
$plans = wc_memberships_get_membership_plans(); | |
$user_id = get_current_user_id(); | |
foreach( $plans as $plan ){ | |
if( in_array( 'wc_membership_' . $plan->id, $item->roles ) ){ | |
if ( wc_memberships_is_user_active_member( $user_id, $plan->id ) ){ | |
$visible = true; | |
break; | |
} else { | |
$visible = false; | |
} | |
} | |
} | |
} | |
return $visible; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment