Untitled

mail@pastecode.io avatar
unknown
plain_text
8 months ago
3.1 kB
14
Indexable
Never
<?php
/**
 * Plugin Name: LifterLMS Media Library and Content Restriction
 * Plugin URI: https://learnainow.ai
 * Description: Restricts access for all non-administrators so they can only see their own Media Library uploads and LifterLMS content (courses, lessons, and memberships). Integrates with LifterLMS for Instructor and Assistant Instructor roles.
 * Version: 1.0
 * Author: FastWebCreations LLC
 * Author URI: https://learnainow.ai
 */

// Filter Ajax view for Media Library
add_filter( 'ajax_query_attachments_args', 'fwc_show_current_user_attachments' );

function fwc_show_current_user_attachments( $query ) {
    $user_id = get_current_user_id();
    if ( $user_id && ! current_user_can( 'administrator' ) && ! current_user_can( 'editor' ) ) {
        if ( current_user_can( 'instructors_assistant' ) ) {
            $instructor_id = get_instructors_id_for_assistant( $user_id );
            $query['author'] = $instructor_id;
        } else {
            $query['author'] = $user_id;
        }
    }
    return $query;
}

// Filter list view for Media Library
add_filter( 'request', 'fwc_show_current_user_attachments_list' );

function fwc_show_current_user_attachments_list( $query ) {
    $screen = null;
    if ( function_exists( 'get_current_screen' ) ) {
        $screen = get_current_screen();
    }

    if ( ! is_null( $screen ) && in_array( $screen->id, array( 'upload' ), true ) ) {
        $user_id = get_current_user_id();
        if ( $user_id && ! current_user_can( 'administrator' ) && ! current_user_can( 'editor' ) ) {
            if ( current_user_can( 'instructors_assistant' ) ) {
                $instructor_id = get_instructors_id_for_assistant( $user_id );
                $query['author'] = $instructor_id;
            } else {
                $query['author'] = $user_id;
            }
        }
    }
    return $query;
}

// Restrict access to LifterLMS content
add_action( 'pre_get_posts', 'fwc_restrict_llms_content_view' );

function fwc_restrict_llms_content_view( $query ) {
    if ( ! is_admin() || ! $query->is_main_query() ) {
        return;
    }

    $user_id = get_current_user_id();
    // Include 'course' in the post types to restrict
    $post_types_to_restrict = array( 'course', 'lesson', 'llms_membership' );

    if ( $user_id && ! current_user_can( 'administrator' ) && in_array( $query->get( 'post_type' ), $post_types_to_restrict ) ) {
        if ( current_user_can( 'lifterlms_instructor' ) || current_user_can( 'instructors_assistant' ) ) {
            // For courses, lessons, and memberships, restrict to their own content
            $query->set( 'author', $user_id );
        } else if ( current_user_can( 'lifterlms_student' ) && 'llms_membership' === $query->get( 'post_type' ) ) {
            // Students can see only published memberships they own
            $query->set( 'post_status', 'publish' );
            $query->set( 'author', $user_id );
        }
        // Additional logic for other roles can be added here
    }
}
Leave a Comment