php 一个自定义函数,返回一个数组,其中包含CoursePress的每个用户ID的讲师和学生角色

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 一个自定义函数,返回一个数组,其中包含CoursePress的每个用户ID的讲师和学生角色相关的知识,希望对你有一定的参考价值。

<?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;

}

以上是关于php 一个自定义函数,返回一个数组,其中包含CoursePress的每个用户ID的讲师和学生角色的主要内容,如果未能解决你的问题,请参考以下文章

PHP常用的自定义函数

一个自定义函数带来的思考

php自定义函数及内部函数----数组处理函数

Scala UDF 函数对数组列进行操作并返回自定义值

带有连接的休眠自定义 SQL 查询 - 避免返回数组列表

php将一维数组转换为字符串并自定义间隔符号