Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
LearnDash LMS for Custom Bulk/Quick Edit plugin
<?php
/**
* LearnDash LMS for Custom Bulk/Quick Edit plugin
*
* Download and unpack this Gist into your theme folder. Then include this script itself in your theme's `functions.php` file via…
*
* require_once get_stylesheet_directory() . '/35b676bb68eee50737f3/cbqe-learndash-lms.php';
*
* In WordPress Admin > Settings > Custom Bulk/Quick, configure your fields as needed, per below. If configuration updates are needed, either manually edit them or remove the current field configuration and click Save Changes for automatic updates.
*
* Assigned Course - As LearnDash Course
* Assigned Lesson - As LearnDash Lesson
*
* @author Michael Cannon <mc@aihr.us>
*/
if ( ! function_exists( 'is_plugin_active' ) || ! is_plugin_active( 'sfwd-lms/sfwd_lms.php' ) ) {
return;
}
add_filter( 'cbqe_settings_as_types', 'ldlms_settings_as_types' );
function ldlms_settings_as_types( $as_types ) {
$as_types['ldlms-courses'] = esc_html__( 'As LearnDash Course' );
$as_types['ldlms-lessons'] = esc_html__( 'As LearnDash Lessons' );
return $as_types;
}
add_filter( 'cbqe_configuration_default', 'ldlms_configuration_default', 10, 3 );
/**
*
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
function ldlms_configuration_default( $default, $id, $type ) {
switch ( $type ) {
case 'ldlms-courses':
$options = array(
'post_type' => 'sfwd-courses',
'post_status' => 'any',
'numberposts' => -1,
'orderby' => 'title',
'order' => 'ASC',
);
$options = apply_filters( 'learndash_course_prerequisite_post_options', $options );
$courses = get_posts( $options );
$default = array();
// $default[] = '0|' . esc_html__( '-- Select a Course --', 'learndash' );
if ( ! empty( $courses ) ) {
foreach( $courses as $course ) {
$default[] = $course->ID . '|' . $course->post_title;
}
$default = implode( "\n", $default );
}
break;
case 'ldlms-lessons':
$options = array(
'post_type' => 'sfwd-lessons',
'post_status' => 'any',
'numberposts' => -1,
'orderby' => 'title',
'order' => 'ASC',
);
// $options = apply_filters( 'learndash_lesson_prerequisite_post_options', $options );
$lessons = get_posts( $options );
$default = array();
// $default[] = '0|' . esc_html__( '-- Select a Lesson --', 'learndash' );
if ( ! empty( $lessons ) ) {
foreach( $lessons as $lesson ) {
$default[] = $lesson->ID . '|' . $lesson->post_title;
}
$default = implode( "\n", $default );
}
break;
}
return $default;
}
add_filter( 'cbqe_quick_edit_custom_box_field', 'ldlms_quick_edit_custom_box_field', 10, 5 );
/**
*
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
function ldlms_quick_edit_custom_box_field( $input, $field_type, $field_name, $options, $bulk_mode ) {
$column_name = str_replace( Custom_Bulkquick_Edit::SLUG, '', $field_name );
$field_name_var = str_replace( '-', '_', $field_name );
$result = $input;
switch ( $field_type ) {
case 'ldlms-courses':
case 'ldlms-lessons':
$result = Custom_Bulkquick_Edit::custom_box_select( $column_name, $field_name, $field_name_var, $options, $bulk_mode );
break;
}
return $result;
}
add_action( 'cbqe_save_post', 'ldlms_save_post', 20 );
function ldlms_save_post( $post_id ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
$post = get_post( $post_id );
$post_type = $post->post_type;
$fields = array( 'course', 'lesson' );
foreach ( $fields as $field ) {
$field_type = Custom_Bulkquick_Edit::is_field_enabled( $post_type, $field );
if ( empty( $field_type ) ) {
continue;
}
if ( ! isset( $_POST[ Custom_Bulkquick_Edit::SLUG . $field ] ) ) {
continue;
}
$meta = get_post_meta( $post_id, '_' . $post_type, true );
if ( ! is_array( $meta ) ) {
$meta = array();
}
$value = $_POST[ Custom_Bulkquick_Edit::SLUG . $field ];
if ( in_array( Custom_Bulkquick_Edit_Settings::RESET, $value ) ) {
unset( $meta[ $post_type . '_' . $field ] );
switch( $field ) {
case 'course':
delete_post_meta( $post_id, 'course_id', $value );
break;
case 'lesson':
delete_post_meta( $post_id, 'lesson_id', $value );
break;
}
} else {
$meta[ $post_type . '_' . $field ] = $value;
switch( $field ) {
case 'course':
update_post_meta( $post_id, 'course_id', $value );
break;
case 'lesson':
update_post_meta( $post_id, 'lesson_id', $value );
break;
}
}
update_post_meta( $post_id, '_' . $post_type, $meta );
}
}
add_filter( 'cbqe_manage_posts_custom_column_field_type', 'ldlms_manage_posts_custom_column_field_type', 10, 4 );
/**
*
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
function ldlms_manage_posts_custom_column_field_type( $current, $field_type, $column, $post_id ) {
global $post;
$result = $current;
switch ( $field_type ) {
case 'ldlms-courses':
$field = 'course';
$current = get_post_meta( $post_id, $field );
$details = Custom_Bulkquick_Edit::get_field_config( $post->post_type, $column );
$options = explode( "\n", $details );
$result = Custom_Bulkquick_Edit::column_select( $column, $current, $options, $field_type );
break;
case 'ldlms-lessons':
$field = 'lesson';
$current = get_post_meta( $post_id, $field );
$details = Custom_Bulkquick_Edit::get_field_config( $post->post_type, $column );
$options = explode( "\n", $details );
$result = Custom_Bulkquick_Edit::column_select( $column, $current, $options, $field_type );
break;
}
return $result;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment