wpmudev-sls
9/21/2017 - 5:45 PM

A custom function that returns an array with instructor and student role per user id for CoursePress

A custom function that returns an array with instructor and student role per user id for CoursePress

<?php
/*
Plugin Name: [CoursePress 2] - User role in CoursePress
Plugin URI: https://premium.wpmudev.org/
Description: A custom function that returns an array with instructor and student role per user id for CoursePress
Author: Panos Lyrakis @ WPMUDEV
Author URI: https://premium.wpmudev.org/
License: GPLv2 or later
*/

function cp_user_roles( $user_id = null ){

	if( is_null( $user_id ) ){
		return array( 'is_student' => false, 'is_instructor' => false );
	}

	$user_cp_role = array( 'is_student' => false, 'is_instructor' => CoursePress_Data_Capabilities::is_instructor( $user_id ) );

	$post_args = array(
		'post_type' => CoursePress_Data_Course::get_post_type_name(),
		'posts_per_page' => -1
	);
	$courses = new WP_Query( $post_args );

	foreach( $courses->posts as $course ){
		if( ! $user_cp_role['is_student'] && CoursePress_Data_Course::student_enrolled( $user_id, $course->ID ) ){
			$user_cp_role['is_student'] = true;
		}

	}

	return $user_cp_role;

}