Last active
March 4, 2023 22:35
-
-
Save Marinski/0225f6ff41b6d67588f35efe7b941b7b to your computer and use it in GitHub Desktop.
LearnPress REST API is a plugin which adds endpoints for LearnPress LMS to the WordPress REST API // EDIT: LearnPress released official version of a REST API for their core plugin. TheYou can read the docs in their website: https://docspress.thimpress.com/developer-learnpress/#sections-5 . LearnPress REST API has been introduced in version 4 of …
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
<?php | |
/* | |
* Plugin Name: LearnPress to WP Rest API | |
* Plugin URI: https://www.webapp.bg/wp-extensions/learnpress-rest-api | |
* Description: A plugin to add endpoints for LearnPress to WP REST API | |
* Version: 1.0 | |
* Author: Marin Stoyanov | |
* Author URI: https://www.webapp.bg/ | |
* License: GPL2 | |
*/ | |
if ( ! defined ( 'ABSPATH' ) ) { | |
exit; | |
} | |
// Add REST API support to an already registered post type. | |
add_filter( 'register_post_type_args', 'learnpress_post_type_args', 10, 2 ); | |
function learnpress_post_type_args( $args, $post_type ) { | |
if ( 'lp_course' === $post_type ) { | |
$args['show_in_rest'] = true; | |
// Optionally customize the rest_base or rest_controller_class | |
$args['rest_base'] = 'courses'; | |
$args['rest_controller_class'] = 'WP_REST_Posts_Controller'; | |
} | |
if ( 'lp_lesson' === $post_type ) { | |
$args['show_in_rest'] = true; | |
// Optionally customize the rest_base or rest_controller_class | |
$args['rest_base'] = 'lessons'; | |
$args['rest_controller_class'] = 'WP_REST_Posts_Controller'; | |
} | |
if ( 'lp_quiz' === $post_type ) { | |
$args['show_in_rest'] = true; | |
// Optionally customize the rest_base or rest_controller_class | |
$args['rest_base'] = 'quizzes'; | |
$args['rest_controller_class'] = 'WP_REST_Posts_Controller'; | |
} | |
if ( 'lp_question' === $post_type ) { | |
$args['show_in_rest'] = true; | |
// Optionally customize the rest_base or rest_controller_class | |
$args['rest_base'] = 'questions'; | |
$args['rest_controller_class'] = 'WP_REST_Posts_Controller'; | |
} | |
if ( 'lp_order' === $post_type ) { | |
$args['show_in_rest'] = true; | |
// Optionally customize the rest_base or rest_controller_class | |
$args['rest_base'] = 'courses-orders'; | |
$args['rest_controller_class'] = 'WP_REST_Posts_Controller'; | |
} | |
return $args; | |
} | |
// Add REST API support to an already registered taxonomy. | |
add_filter( 'register_taxonomy_args', 'learnpress_taxonomy_args', 10, 2 ); | |
function learnpress_taxonomy_args( $args, $taxonomy_name ) { | |
if ( 'course_category' === $taxonomy_name ) { | |
$args['show_in_rest'] = true; | |
// Optionally customize the rest_base or rest_controller_class | |
$args['rest_base'] = 'course-categories'; | |
$args['rest_controller_class'] = 'WP_REST_Terms_Controller'; | |
} | |
return $args; | |
} | |
// Add REST API support to custom fields for custom post types | |
add_action( 'rest_api_init', 'learnpress_register_custom_fields' ); | |
function learnpress_register_custom_fields() { | |
// Course custom fields | |
register_rest_field( 'lp_course', '_lp_duration', array( | |
'get_callback' => 'show_fields' | |
) ); | |
register_rest_field( 'lp_course', '_lp_max_students', array( | |
'get_callback' => 'show_fields' | |
) ); | |
register_rest_field( 'lp_course', '_lp_students', array( | |
'get_callback' => 'show_fields' | |
) ); | |
register_rest_field( 'lp_course', '_lp_retake_count', array( | |
'get_callback' => 'show_fields' | |
) ); | |
register_rest_field( 'lp_course', '_lp_featured', array( | |
'get_callback' => 'show_fields' | |
) ); | |
register_rest_field( 'lp_course', '_lp_submission', array( | |
'get_callback' => 'show_fields' | |
) ); | |
register_rest_field( 'lp_course', '_lp_course_result', array( | |
'get_callback' => 'show_fields' | |
) ); | |
register_rest_field( 'lp_course', '_lp_passing_condition', array( | |
'get_callback' => 'show_fields' | |
) ); | |
register_rest_field( 'lp_course', '_lp_price', array( | |
'get_callback' => 'show_fields' | |
) ); | |
register_rest_field( 'lp_course', '_lp_sale_price', array( | |
'get_callback' => 'show_fields' | |
) ); | |
register_rest_field( 'lp_course', '_lp_sale_start', array( | |
'get_callback' => 'show_fields' | |
) ); | |
register_rest_field( 'lp_course', '_lp_sale_end', array( | |
'get_callback' => 'show_fields' | |
) ); | |
register_rest_field( 'lp_course', '_lp_required_enroll', array( | |
'get_callback' => 'show_fields' | |
) ); | |
register_rest_field( 'lp_course', '_lp_curriculum', array( | |
'get_callback' => '_lp_curriculum', | |
'permission_callback' => '__return_true' | |
) ); | |
// Lesson custom fields | |
register_rest_field( 'lp_lesson', '_lp_duration', array( | |
'get_callback' => 'show_fields' | |
) ); | |
register_rest_field( 'lp_lesson', '_lp_preview', array( | |
'get_callback' => 'show_fields' | |
) ); | |
// register_rest_field( 'lp_lesson', '_lp_curriculum', array( | |
// 'get_callback' => 'show_fields' | |
// ) ); | |
} | |
function show_fields($object, $field_name, $request) { | |
return get_post_meta( $object['id'], $field_name, true ); | |
} | |
function _lp_curriculum( $object, $field_name, $request ) { | |
global $course; | |
$curriculum = $course->get_curriculum(); | |
$array = $array_item = []; | |
foreach ( $curriculum as $section_key => $section ) { | |
$section_title = $section->get_title(); | |
$items = $section->get_items(); | |
if ( ! empty( $items ) ) { | |
foreach ( $items as $items_key => $item ) { | |
$type = $item->get_item_type(); | |
$link = $item->get_permalink(); | |
$title = $item->get_title( 'display' ); | |
$array_item[] = array( | |
'id' => $items_key, | |
'type' => $type, | |
'link' => $link, | |
'title' => $title | |
); | |
} | |
} | |
$array[] = array( | |
'id' => $section_key, | |
'title' => $section_title, | |
'items' => $array_item | |
); | |
} | |
return $array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment