| 1 |
<?php |
| 2 |
|
| 3 |
namespace LearnPress\Models\WPTables; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use LearnPress\Helpers\LPDateTime; |
| 7 |
use LearnPress\Helpers\Response; |
| 8 |
use LearnPress\Helpers\Template; |
| 9 |
use LearnPress\Models\CourseSectionItemModel; |
| 10 |
use LearnPress\Models\LessonPostModel; |
| 11 |
use LP_Abstract_Post_Type; |
| 12 |
use Throwable; |
| 13 |
use WP_Post; |
| 14 |
use WP_Posts_List_Table; |
| 15 |
|
| 16 |
defined( 'ABSPATH' ) || exit; |
| 17 |
|
| 18 |
/** |
| 19 |
* Lessons Table class. |
| 20 |
* Display columns for lessons on WP Backend |
| 21 |
* |
| 22 |
* @since 4.4.8 |
| 23 |
* @version 1.0.0 |
| 24 |
*/ |
| 25 |
class LessonsTable extends WP_Posts_List_Table { |
| 26 |
/** |
| 27 |
* Get the table columns. |
| 28 |
* |
| 29 |
* @return array |
| 30 |
* @throws Exception |
| 31 |
*/ |
| 32 |
public function get_columns() { |
| 33 |
$columns = parent::get_columns(); |
| 34 |
|
| 35 |
$pos = array_search( 'title', array_keys( $columns ), true ); |
| 36 |
if ( false !== $pos ) { |
| 37 |
$new_columns = array( |
| 38 |
'instructor' => esc_html__( 'Author', 'learnpress' ), |
| 39 |
LP_COURSE_CPT => $this->get_course_column_title(), |
| 40 |
); |
| 41 |
|
| 42 |
$new_columns['duration'] = esc_html__( 'Duration', 'learnpress' ); |
| 43 |
// Set key lp_preview instead of preview to not apply style float right CSS of system |
| 44 |
$new_columns['lp_preview'] = esc_html__( 'Preview', 'learnpress' ); |
| 45 |
|
| 46 |
$columns = array_merge( |
| 47 |
array_slice( $columns, 0, $pos + 1 ), |
| 48 |
$new_columns, |
| 49 |
array_slice( $columns, $pos + 1 ) |
| 50 |
); |
| 51 |
} |
| 52 |
|
| 53 |
unset( $columns['taxonomy-lesson-tag'] ); |
| 54 |
unset( $columns['comments'] ); |
| 55 |
|
| 56 |
if ( ! empty( $columns['author'] ) ) { |
| 57 |
unset( $columns['author'] ); |
| 58 |
} |
| 59 |
|
| 60 |
$user = wp_get_current_user(); |
| 61 |
if ( in_array( LP_TEACHER_ROLE, $user->roles, true ) ) { |
| 62 |
unset( $columns['instructor'] ); |
| 63 |
} |
| 64 |
|
| 65 |
return $columns; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Set columns can be sortable. |
| 70 |
* |
| 71 |
* @return array |
| 72 |
*/ |
| 73 |
public function get_sortable_columns() { |
| 74 |
$sortable_columns = parent::get_sortable_columns(); |
| 75 |
$sortable_columns['author'] = array( 'author', 'asc' ); |
| 76 |
$sortable_columns[ LP_COURSE_CPT ] = array( 'course-name', 'asc' ); |
| 77 |
|
| 78 |
return $sortable_columns; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Column instructor. |
| 83 |
* |
| 84 |
* @param WP_Post $post |
| 85 |
* |
| 86 |
* @return void |
| 87 |
*/ |
| 88 |
public function column_instructor( $post ) { |
| 89 |
try { |
| 90 |
LP_Abstract_Post_Type::column_author( $post ); |
| 91 |
} catch ( Throwable $e ) { |
| 92 |
Template::print_message( $e->getMessage(), Response::STATUS_ERROR ); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Column course. |
| 98 |
* |
| 99 |
* @param WP_Post $post |
| 100 |
* |
| 101 |
* @return void |
| 102 |
*/ |
| 103 |
public function column_lp_course( $post ) { |
| 104 |
try { |
| 105 |
$courses = CourseSectionItemModel::get_courses_from_item_id( $post->ID, LP_LESSON_CPT ); |
| 106 |
|
| 107 |
if ( $courses ) { |
| 108 |
foreach ( $courses as $course ) { |
| 109 |
$course_id = $course->section_course_id; |
| 110 |
echo '<div><a href="' . esc_url_raw( remove_query_arg( 'orderby', add_query_arg( array( 'course' => $course_id ) ) ) ) . '">' . get_the_title( $course_id ) . '</a>'; |
| 111 |
echo '<div class="row-actions">'; |
| 112 |
printf( '<a href="%s">%s</a>', admin_url( sprintf( 'post.php?post=%d&action=edit', $course_id ) ), __( 'Edit', 'learnpress' ) ); |
| 113 |
echo ' | '; |
| 114 |
printf( '<a href="%s">%s</a>', get_the_permalink( $course_id ), __( 'View', 'learnpress' ) ); |
| 115 |
|
| 116 |
if ( $this->get_filter_course_id() ) { |
| 117 |
echo ' | '; |
| 118 |
printf( |
| 119 |
'<a href="%s">%s</a>', |
| 120 |
esc_url_raw( remove_query_arg( array( 'course', 'orderby' ) ) ), |
| 121 |
__( 'Remove Filter', 'learnpress' ) |
| 122 |
); |
| 123 |
} |
| 124 |
echo '</div></div>'; |
| 125 |
} |
| 126 |
} else { |
| 127 |
_e( 'Not assigned yet', 'learnpress' ); |
| 128 |
} |
| 129 |
} catch ( Throwable $e ) { |
| 130 |
|
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Column preview. |
| 136 |
* |
| 137 |
* @param WP_Post $post |
| 138 |
* |
| 139 |
* @return void |
| 140 |
*/ |
| 141 |
public function column_lp_preview( $post ) { |
| 142 |
try { |
| 143 |
$lessonPostModel = LessonPostModel::find( $post->ID, true ); |
| 144 |
if ( ! $lessonPostModel ) { |
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
$lesson_is_preview = 'yes' === get_post_meta( $post->ID, '_lp_preview', true ); |
| 149 |
|
| 150 |
echo $lesson_is_preview |
| 151 |
? '<span class="lp-icon-eye"></span>' |
| 152 |
: '<span class="lp-icon-eye-slash"></span>'; |
| 153 |
} catch ( Throwable $e ) { |
| 154 |
Template::print_message( $e->getMessage(), Response::STATUS_ERROR ); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Column duration. |
| 160 |
* |
| 161 |
* @param WP_Post $post |
| 162 |
* |
| 163 |
* @return void |
| 164 |
*/ |
| 165 |
public function column_duration( $post ) { |
| 166 |
try { |
| 167 |
$lessonPostModel = LessonPostModel::find( $post->ID, true ); |
| 168 |
if ( ! $lessonPostModel ) { |
| 169 |
return; |
| 170 |
} |
| 171 |
|
| 172 |
$duration_raw = $lessonPostModel->get_duration(); |
| 173 |
$duration_number = absint( $duration_raw ); |
| 174 |
if ( $duration_number === 0 ) { |
| 175 |
_e( 'Unlimited', 'learnpress' ); |
| 176 |
} else { |
| 177 |
$duration_arr = explode( ' ', $duration_raw ); |
| 178 |
$duration_number = floatval( $duration_arr[0] ?? 0 ); |
| 179 |
$duration_type = $duration_arr[1] ?? ''; |
| 180 |
if ( empty( $duration_number ) ) { |
| 181 |
$duration_str = __( 'Lifetime', 'learnpress' ); |
| 182 |
} else { |
| 183 |
$duration_str = LPDateTime::get_string_plural_duration( $duration_number, $duration_type ); |
| 184 |
} |
| 185 |
|
| 186 |
echo $duration_str; |
| 187 |
} |
| 188 |
} catch ( Throwable $e ) { |
| 189 |
Template::print_message( $e->getMessage(), Response::STATUS_ERROR ); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Get the course column title. |
| 195 |
* |
| 196 |
* @return string |
| 197 |
* @throws \Exception |
| 198 |
*/ |
| 199 |
protected function get_course_column_title(): string { |
| 200 |
$title = esc_html__( 'Course', 'learnpress' ); |
| 201 |
$course_id = $this->get_filter_course_id(); |
| 202 |
|
| 203 |
if ( $course_id ) { |
| 204 |
$course = learn_press_get_course( $course_id ); |
| 205 |
if ( $course ) { |
| 206 |
$count = $course->count_items( LP_LESSON_CPT ); |
| 207 |
$post_object = get_post_type_object( LP_LESSON_CPT ); |
| 208 |
$title = sprintf( |
| 209 |
/* translators: 1: count 2: item label */ |
| 210 |
_n( 'Course (%1$d %2$s)', 'Course (%1$d %2$s)', $count, 'learnpress' ), |
| 211 |
$count, |
| 212 |
$count > 1 ? $post_object->label : $post_object->labels->singular_name |
| 213 |
); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
return $title; |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Get the course id currently filtering by. |
| 222 |
* |
| 223 |
* @return int|false |
| 224 |
*/ |
| 225 |
protected function get_filter_course_id() { |
| 226 |
$course_id = ! empty( $_REQUEST['course'] ) ? absint( $_REQUEST['course'] ) : false; |
| 227 |
|
| 228 |
if ( ! $course_id && learn_press_is_support_course_item_type( LP_LESSON_CPT ) ) { |
| 229 |
$course_id = false; |
| 230 |
} |
| 231 |
|
| 232 |
return $course_id; |
| 233 |
} |
| 234 |
} |
| 235 |
|