Skip to content

Instantly share code, notes, and snippets.

@adeel-raza
Last active July 31, 2024 14:06
Show Gist options
  • Save adeel-raza/2a935c235bb633b6c26af0e988cb2085 to your computer and use it in GitHub Desktop.
Save adeel-raza/2a935c235bb633b6c26af0e988cb2085 to your computer and use it in GitHub Desktop.
Custom Endpoints to Fetch Learndash Courses and Users Information for a LD Group
<?php
/**
* Plugin Name: Custom LD Rest API
* Plugin URI: https://elearningevolve.com/
* Description: Provides custom REST API endpoints utilizing LearnDash
* Version: 1.0.0
* Author: eLearning evolve
* Author URI: https://elearningevolve.com/
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: custom-ld-rest-api
* Domain Path: /languages
*
* @package Custom_LD_Rest_API
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Define plugin constants.
define( 'CSLDAPI_VERSION', '1.0.0' );
define( 'CSLDAPI_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'CSLDAPI_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'CSLDAPI_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
if ( ! class_exists( 'Custom_LD_Rest_API' ) ) :
/**
* Main plugin class.
*/
class Custom_LD_Rest_API {
/**
* Instance of this class.
*
* @var Custom_LD_Rest_API
*/
protected static $instance = null;
/**
* Initialize the plugin.
*/
public function __construct() {
add_action( 'plugins_loaded', array( $this, 'init' ) );
}
/**
* Get an instance of this class.
*
* @return Custom_LD_Rest_API A single instance of this class.
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Initialize the plugin.
*/
public function init() {
if ( ! $this->is_learndash_active() ) {
add_action( 'admin_notices', array( $this, 'learndash_missing_notice' ) );
return;
}
add_action( 'rest_api_init', array( $this, 'register_endpoints' ) );
}
/**
* Check if LearnDash is active.
*
* @return bool True if LearnDash is active, false otherwise.
*/
private function is_learndash_active() {
return class_exists( 'SFWD_LMS' );
}
/**
* Display admin notice if LearnDash is not active.
*/
public function learndash_missing_notice() {
?>
<div class="notice notice-error">
<p><?php esc_html_e( 'Custom LD Rest API requires LearnDash to be installed and activated.', 'custom-ld-rest-api' ); ?></p>
</div>
<?php
}
/**
* Register endpoints for API.
*/
public function register_endpoints() {
register_rest_route(
'csldapi/v1',
'/groups/(?P<group_id>\d+)/courses',
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'display_courses' ),
'permission_callback' => array( $this, 'permissions_check' ),
'args' => array(
'group_id' => array(
'validate_callback' => array( $this, 'validate_group_id' ),
),
),
)
);
register_rest_route(
'csldapi/v1',
'/groups/(?P<group_id>\d+)/users',
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'display_users' ),
'permission_callback' => array( $this, 'permissions_check' ),
'args' => array(
'group_id' => array(
'validate_callback' => array( $this, 'validate_group_id' ),
),
),
)
);
}
/**
* Check permissions for API.
*
* @param WP_REST_Request $request The request object.
* @return true|WP_Error True if the request has access, WP_Error otherwise.
*/
public function permissions_check( $request ) {
if ( ! $this->validate_group_id( $request['group_id'] ) ) {
return new WP_Error(
'rest_invalid_group_id',
esc_html__( 'Invalid group ID. Please provide a valid positive integer.', 'custom-ld-rest-api' ),
array( 'status' => 400 )
);
}
$group_id = (int) $request['group_id'];
$current_user_id = get_current_user_id();
$group_leaders = learndash_get_groups_administrator_ids( $group_id );
if ( in_array( $current_user_id, $group_leaders, true ) || current_user_can( 'manage_options' ) ) {
return true;
}
return new WP_Error(
'rest_forbidden',
esc_html__( 'You do not have permission to access this group.', 'custom-ld-rest-api' ),
array( 'status' => 403 )
);
}
/**
* Gets the list of associated courses.
*
* @param WP_REST_Request $request The request object.
* @return WP_REST_Response|WP_Error The list of courses or WP_Error on failure.
*/
public function display_courses( $request ) {
$group_id = (int) $request['group_id'];
$course_ids = learndash_group_enrolled_courses( $group_id );
$courses = array();
foreach ( $course_ids as $course_id ) {
$courses[] = array(
'course_id' => $course_id,
'course_title' => get_the_title( $course_id ),
);
}
return rest_ensure_response( $courses );
}
/**
* Get the list of users.
*
* @param WP_REST_Request $request The request object.
* @return WP_REST_Response|WP_Error The list of users or WP_Error on failure.
*/
public function display_users( $request ) {
$group_id = (int) $request['group_id'];
$user_ids = learndash_get_groups_user_ids( $group_id );
$user_info = array();
foreach ( $user_ids as $user_id ) {
$user_data = get_userdata( $user_id );
$user_info[] = array(
'user_id' => $user_id,
'user_name' => $user_data->user_login,
);
}
return rest_ensure_response( $user_info );
}
/**
* Validate group ID.
*
* @param mixed $param The value to validate.
* @return bool|WP_Error True if the parameter is a valid group ID, WP_Error otherwise.
*/
public function validate_group_id( $param ) {
$group_id = (int) $param;
if ( ! $group_id || ! get_post( $group_id ) ) {
return new WP_Error(
'rest_invalid_param',
esc_html__( 'Invalid group ID.', 'custom-ld-rest-api' ),
array( 'status' => 404 )
);
}
return true;
}
}
endif;
/**
* Returns the main instance of Custom_LD_Rest_API.
*
* @return Custom_LD_Rest_API
*/
function custom_ld_rest_api() {
return Custom_LD_Rest_API::get_instance();
}
// Initialize the plugin.
custom_ld_rest_api();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment