Home / Admin / LifterLMS_DIR_AC_RAPI – Expose LifterLMS ACF Fields to REST API
Duplicate Snippet

Embed Snippet on Your Site

LifterLMS_DIR_AC_RAPI – Expose LifterLMS ACF Fields to REST API

WPCode Snippet: Expose LifterLMS ACF Fields to REST API
* Description: Makes LifterLMS module data accessible via Application Password authentication
* Location: Run Everywhere
* Priority: 15

ismail daugherty PRO
<10
Code Preview
php
<?php
/**
 * WPCode Snippet: Expose LifterLMS ACF Fields to REST API
 * Description: Makes LifterLMS module data accessible via Application Password authentication
 * Location: Run Everywhere
 * Priority: 15
 */
defined( 'ABSPATH' ) || exit;
/**
 * Register LifterLMS ACF meta fields in REST API
 */
add_action( 'rest_api_init', function() {
    
    $llms_fields = array(
        // Module ID
        'item_id'                   => 'integer',
        
        // Edit Links
        'llms_course_edit_link'     => 'string',
        'llms_course_builder_link'  => 'string',
        'llms_course_reporting_link' => 'string',
        
        // Status
        'status'                    => 'string',
        
        // Module Settings
        'course_duration'           => 'string',
        'course_capacity'           => 'integer',
        'course_instructors'        => 'string',
        'is_free'                   => 'boolean',
        
        // Drip Settings
        'drip_method'               => 'string',
        'drip_value'                => 'string',
        
        // Module Content
        'excerpt'                   => 'string',
        'course_content'            => 'string',
        
        // Combined Content Fields
        'all_lesson_content'        => 'string',
        'all_section_content'       => 'string',
        
        // Counts
        'section_count'             => 'integer',
        'total_lessons'             => 'integer',
        'total_quizzes'             => 'integer',
        'total_assignments'         => 'integer',
        'engagements_count'         => 'integer',
        
        // Prerequisites
        'has_prerequisite'          => 'boolean',
        'prerequisite_id'           => 'integer',
        
        // Certificate
        'has_certificate'           => 'boolean',
        
        // Comprehensive Detail TextAreas
        'steps_details_list'        => 'string',
        'quizzes_details_list'      => 'string',
        'assignments_details_list'  => 'string',
        'sections_details_list'     => 'string',
        'certificates_details_list' => 'string',
        'engagements_details_list'  => 'string',
        
        // Sync Status
        'last_sync'                 => 'string',
        'sync_status'               => 'string',
    );
    
    // Register LifterLMS fields
    foreach ( $llms_fields as $field_name => $field_type ) {
        register_rest_field( 'llms_content_sync', $field_name, array(
            'get_callback' => function( $object ) use ( $field_name, $field_type ) {
                $value = get_post_meta( $object['id'], $field_name, true );
                
                if ( $value === '' || $value === false ) {
                    return null;
                }
                
                switch ( $field_type ) {
                    case 'integer':
                        return intval( $value );
                    case 'boolean':
                        return (bool) $value;
                    case 'array':
                        return is_array( $value ) ? $value : array();
                    case 'object':
                        return is_array( $value ) || is_object( $value ) ? $value : new stdClass();
                    default:
                        return $value;
                }
            },
            'update_callback' => function( $value, $object ) use ( $field_name ) {
                return update_post_meta( $object->ID, $field_name, $value );
            },
            'schema' => array(
                'description' => ucwords( str_replace( '_', ' ', $field_name ) ),
                'type'        => $field_type,
                'context'     => array( 'view', 'edit' ),
            ),
        ) );
    }
    
} );
/**
 * Enable REST API for LifterLMS CPT
 */
add_filter( 'register_post_type_args', function( $args, $post_type ) {
    
    if ( $post_type === 'llms_content_sync' ) {
        $args['show_in_rest'] = true;
        $args['rest_base'] = 'llms-content-sync';
        $args['rest_controller_class'] = 'WP_REST_Posts_Controller';
    }
    
    return $args;
}, 10, 2 );
/**
 * Security: Block access unless using Application Password
 */
add_filter( 'rest_pre_dispatch', function( $result, $server, $request ) {
    
    $route = $request->get_route();
    if ( strpos( $route, '/wp/v2/llms-content-sync' ) === false &&
         strpos( $route, '/llms-content-sync/v1' ) === false ) {
        return $result;
    }
    
    $user = wp_get_current_user();
    
    if ( ! $user || ! $user->exists() ) {
        return new WP_Error(
            'rest_forbidden',
            __( 'Authentication required. Please use Application Password credentials.' ),
            array( 'status' => 401 )
        );
    }
    
    $auth_header = $request->get_header( 'authorization' );
    
    if ( empty( $auth_header ) ) {
        return new WP_Error(
            'rest_forbidden',
            __( 'Application Password authentication required. Browser login not allowed.' ),
            array( 'status' => 403 )
        );
    }
    
    return $result;
    
}, 10, 3 );
/**
 * Debug endpoint
 */
add_action( 'rest_api_init', function() {
    register_rest_route( 'llms-content-sync/v1', '/test', array(
        'methods'  => 'GET',
        'callback' => function() {
            
            $posts = get_posts( array(
                'post_type' => 'llms_content_sync',
                'numberposts' => 1,
            ) );
            
            if ( empty( $posts ) ) {
                return new WP_REST_Response( array(
                    'status' => 'error',
                    'message' => 'No LMS posts found. Run sync first.',
                ), 404 );
            }
            
            $post = $posts[0];
            
            return new WP_REST_Response( array(
                'status' => 'success',
                'message' => 'LifterLMS REST API is working!',
                'post_info' => array(
                    'id' => $post->ID,
                    'title' => $post->post_title,
                ),
                'total_fields_available' => 34,
            ), 200 );
        },
        'permission_callback' => '__return_true',
    ) );
} );

Comments

Add a Comment