| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class LP_Lesson_Post_Type |
| 4 |
* |
| 5 |
* @author ThimPress |
| 6 |
* @package LearnPress/Classes |
| 7 |
* @version 3.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
/** |
| 11 |
* Prevent loading this file directly |
| 12 |
*/ |
| 13 |
defined( 'ABSPATH' ) || exit(); |
| 14 |
|
| 15 |
if ( ! class_exists( 'LP_Lesson_Post_Type' ) ) { |
| 16 |
|
| 17 |
/** |
| 18 |
* Class LP_Lesson_Post_Type |
| 19 |
*/ |
| 20 |
final class LP_Lesson_Post_Type extends LP_Abstract_Post_Type { |
| 21 |
/** |
| 22 |
* @var null |
| 23 |
*/ |
| 24 |
protected static $_instance = null; |
| 25 |
|
| 26 |
/** |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
protected $_post_type = LP_LESSON_CPT; |
| 30 |
|
| 31 |
/** |
| 32 |
* LP_Lesson_Post_Type constructor. |
| 33 |
* |
| 34 |
* @param $post_type |
| 35 |
*/ |
| 36 |
public function __construct() { |
| 37 |
|
| 38 |
// $this->add_map_method( 'before_delete', 'before_delete_lesson' ); |
| 39 |
// hide View Lesson link if not assigned to course |
| 40 |
|
| 41 |
add_filter( 'views_edit-' . LP_LESSON_CPT, array( $this, 'views_pages' ), 10 ); |
| 42 |
|
| 43 |
parent::__construct(); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Filter items unassigned. |
| 48 |
* |
| 49 |
* @param string $where |
| 50 |
* |
| 51 |
* @return string |
| 52 |
*/ |
| 53 |
public function posts_where_paged( $where ): string { |
| 54 |
|
| 55 |
if ( ! $this->is_page_list_posts_on_backend() ) { |
| 56 |
return $where; |
| 57 |
} |
| 58 |
|
| 59 |
global $wpdb; |
| 60 |
|
| 61 |
if ( 'yes' === LP_Request::get( 'unassigned' ) ) { |
| 62 |
$where .= $wpdb->prepare( |
| 63 |
" |
| 64 |
AND {$wpdb->posts}.ID NOT IN( |
| 65 |
SELECT si.item_id |
| 66 |
FROM {$wpdb->learnpress_section_items} si |
| 67 |
INNER JOIN {$wpdb->posts} p ON p.ID = si.item_id |
| 68 |
WHERE p.post_type = %s |
| 69 |
) |
| 70 |
", |
| 71 |
LP_LESSON_CPT |
| 72 |
); |
| 73 |
} |
| 74 |
|
| 75 |
$preview = LP_Request::get( 'preview' ); |
| 76 |
|
| 77 |
if ( $preview ) { |
| 78 |
$clause = $wpdb->prepare( |
| 79 |
" |
| 80 |
SELECT ID |
| 81 |
FROM {$wpdb->posts} p |
| 82 |
INNER JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id AND pm.meta_key = %s |
| 83 |
WHERE pm.meta_value = %s |
| 84 |
AND p.post_type = %s", |
| 85 |
'_lp_preview', |
| 86 |
'yes', |
| 87 |
LP_LESSON_CPT |
| 88 |
); |
| 89 |
|
| 90 |
$in = ''; |
| 91 |
if ( 'no' === $preview ) { |
| 92 |
$in = 'NOT'; |
| 93 |
} |
| 94 |
|
| 95 |
$where .= " AND {$wpdb->posts}.ID {$in} IN({$clause})"; |
| 96 |
} |
| 97 |
|
| 98 |
return $where; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Add filters to lesson view. |
| 103 |
* |
| 104 |
* @param array $views |
| 105 |
* |
| 106 |
* @return array |
| 107 |
* @since 3.0.0 |
| 108 |
* @editor tungnx |
| 109 |
* @modify 4.1.4.1 |
| 110 |
*/ |
| 111 |
public function views_pages( array $views ): array { |
| 112 |
$count_unassigned_lesson = LP_Course_DB::getInstance()->get_total_item_unassigned( LP_LESSON_CPT ); |
| 113 |
|
| 114 |
if ( $count_unassigned_lesson > 0 ) { |
| 115 |
$views['unassigned'] = sprintf( |
| 116 |
'<a href="%s" class="%s">%s <span class="count">(%d)</span></a>', |
| 117 |
admin_url( 'edit.php?post_type=' . LP_LESSON_CPT . '&unassigned=yes' ), |
| 118 |
isset( $_GET['unassigned'] ) ? 'current' : '', |
| 119 |
__( 'Unassigned', 'learnpress' ), |
| 120 |
$count_unassigned_lesson |
| 121 |
); |
| 122 |
} |
| 123 |
|
| 124 |
$total_preview_items = LP_Lesson_DB::getInstance()->get_total_preview_items(); |
| 125 |
if ( $total_preview_items > 0 ) { |
| 126 |
$views['lesson-preview'] = sprintf( |
| 127 |
'<a href="%s" class="%s">%s <span class="count">(%d)</span></a>', |
| 128 |
admin_url( 'edit.php?post_type=' . LP_LESSON_CPT . '&preview=yes' ), |
| 129 |
isset( $_GET['preview'] ) && $_GET['preview'] === 'yes' ? 'current' : '', |
| 130 |
__( 'Preview', 'learnpress' ), |
| 131 |
$total_preview_items |
| 132 |
); |
| 133 |
} |
| 134 |
|
| 135 |
$total_no_preview_items = LP_Lesson_DB::getInstance()->get_total_no_preview_items( $total_preview_items ); |
| 136 |
if ( $total_no_preview_items > 0 ) { |
| 137 |
$views['lesson-no-preview'] = sprintf( |
| 138 |
'<a href="%s" class="%s">%s <span class="count">(%d)</span></a>', |
| 139 |
admin_url( 'edit.php?post_type=' . LP_LESSON_CPT . '&preview=no' ), |
| 140 |
isset( $_GET['preview'] ) && $_GET['preview'] === 'no' ? 'current' : '', |
| 141 |
__( 'No Preview', 'learnpress' ), |
| 142 |
$total_no_preview_items |
| 143 |
); |
| 144 |
} |
| 145 |
|
| 146 |
return $views; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Register lesson post type. |
| 151 |
*/ |
| 152 |
public function args_register_post_type(): array { |
| 153 |
|
| 154 |
return array( |
| 155 |
'labels' => array( |
| 156 |
'name' => esc_html__( 'Lessons', 'learnpress' ), |
| 157 |
'menu_name' => esc_html__( 'Lessons', 'learnpress' ), |
| 158 |
'singular_name' => esc_html__( 'Lesson', 'learnpress' ), |
| 159 |
'add_new_item' => esc_html__( 'Add A New Lesson', 'learnpress' ), |
| 160 |
'all_items' => esc_html__( 'Lessons', 'learnpress' ), |
| 161 |
'view_item' => esc_html__( 'View Lesson', 'learnpress' ), |
| 162 |
'add_new' => esc_html__( 'Add New', 'learnpress' ), |
| 163 |
'edit_item' => esc_html__( 'Edit Lesson', 'learnpress' ), |
| 164 |
'update_item' => esc_html__( 'Update Lesson', 'learnpress' ), |
| 165 |
'search_items' => esc_html__( 'Search Lessons', 'learnpress' ), |
| 166 |
'not_found' => esc_html__( 'No lesson found', 'learnpress' ), |
| 167 |
'not_found_in_trash' => esc_html__( 'There was no lesson found in the trash', 'learnpress' ), |
| 168 |
), |
| 169 |
'public' => true, |
| 170 |
'query_var' => true, |
| 171 |
'taxonomies' => array( 'lesson_tag' ), |
| 172 |
'publicly_queryable' => true, |
| 173 |
'show_ui' => true, |
| 174 |
'has_archive' => false, |
| 175 |
'capability_type' => LP_LESSON_CPT, |
| 176 |
'map_meta_cap' => true, |
| 177 |
'show_in_menu' => 'learn_press', |
| 178 |
'show_in_admin_bar' => true, |
| 179 |
'show_in_nav_menus' => true, |
| 180 |
'show_in_rest' => learn_press_user_maybe_is_a_teacher(), |
| 181 |
'supports' => array( |
| 182 |
'title', |
| 183 |
'editor', |
| 184 |
'post-formats', |
| 185 |
'revisions', |
| 186 |
'comments', |
| 187 |
), |
| 188 |
'hierarchical' => true, |
| 189 |
'rewrite' => array( |
| 190 |
'slug' => 'lessons', |
| 191 |
'hierarchical' => true, |
| 192 |
'with_front' => false, |
| 193 |
), |
| 194 |
); |
| 195 |
|
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Remove lesson form course items. |
| 200 |
* |
| 201 |
* @param int $post_id |
| 202 |
* |
| 203 |
* @since 3.0.0 |
| 204 |
*/ |
| 205 |
/*public function before_delete( int $post_id = 0 ) { |
| 206 |
$curd = new LP_Lesson_CURD(); |
| 207 |
$curd->delete( $post_id ); |
| 208 |
}*/ |
| 209 |
|
| 210 |
/** |
| 211 |
* Add columns to admin manage lesson page |
| 212 |
* |
| 213 |
* @param array $columns |
| 214 |
* |
| 215 |
* @return array |
| 216 |
*/ |
| 217 |
public function columns_head( $columns ) { |
| 218 |
// append new column after title column |
| 219 |
$pos = array_search( 'title', array_keys( $columns ) ); |
| 220 |
$new_columns = array( |
| 221 |
'instructor' => esc_html__( 'Author', 'learnpress' ), |
| 222 |
LP_COURSE_CPT => $this->_get_course_column_title(), |
| 223 |
); |
| 224 |
|
| 225 |
if ( current_theme_supports( 'post-formats' ) ) { |
| 226 |
$new_columns['format'] = esc_html__( 'Format', 'learnpress' ); |
| 227 |
$new_columns['duration'] = esc_html__( 'Duration', 'learnpress' ); |
| 228 |
} |
| 229 |
|
| 230 |
$new_columns['preview'] = esc_html__( 'Preview', 'learnpress' ); |
| 231 |
|
| 232 |
if ( false !== $pos && ! array_key_exists( LP_COURSE_CPT, $columns ) ) { |
| 233 |
$columns = array_merge( |
| 234 |
array_slice( $columns, 0, $pos + 1 ), |
| 235 |
$new_columns, |
| 236 |
array_slice( $columns, $pos + 1 ) |
| 237 |
); |
| 238 |
|
| 239 |
} |
| 240 |
|
| 241 |
unset( $columns['taxonomy-lesson-tag'] ); |
| 242 |
$user = wp_get_current_user(); |
| 243 |
|
| 244 |
if ( in_array( LP_TEACHER_ROLE, $user->roles ) ) { |
| 245 |
unset( $columns['instructor'] ); |
| 246 |
} |
| 247 |
|
| 248 |
if ( ! empty( $columns['author'] ) ) { |
| 249 |
unset( $columns['author'] ); |
| 250 |
} |
| 251 |
|
| 252 |
return $columns; |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Display content for custom column |
| 257 |
* |
| 258 |
* @param string $name |
| 259 |
* @param int $post_id |
| 260 |
*/ |
| 261 |
public function columns_content( $name, $post_id = 0 ) { |
| 262 |
switch ( $name ) { |
| 263 |
case 'instructor': |
| 264 |
$this->column_instructor( $post_id ); |
| 265 |
break; |
| 266 |
case LP_COURSE_CPT: |
| 267 |
$this->_get_item_course( $post_id ); |
| 268 |
break; |
| 269 |
case 'preview': |
| 270 |
$lesson_is_preview = 'yes' === get_post_meta( $post_id, '_lp_preview', true ); |
| 271 |
echo $lesson_is_preview ? '<span class="dashicons dashicons-saved" style="color: #00c700"></span>' : ''; |
| 272 |
break; |
| 273 |
case 'format': |
| 274 |
learn_press_item_meta_format( $post_id, __( 'Standard', 'learnpress' ) ); |
| 275 |
break; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* @param $columns |
| 281 |
* |
| 282 |
* @return mixed |
| 283 |
*/ |
| 284 |
public function sortable_columns( $columns ) { |
| 285 |
$columns[ LP_COURSE_CPT ] = 'course-name'; |
| 286 |
$columns['author'] = 'author'; |
| 287 |
|
| 288 |
return $columns; |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Add admin params. |
| 293 |
* |
| 294 |
* @return array |
| 295 |
*/ |
| 296 |
public function admin_params() { |
| 297 |
return array( 'notice_empty_lesson' => '' ); |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* Lesson assigned view. |
| 302 |
* |
| 303 |
* @since 3.0.0 |
| 304 |
*/ |
| 305 |
public function lesson_assigned() { |
| 306 |
learn_press_admin_view( 'meta-boxes/course/assigned.php' ); |
| 307 |
} |
| 308 |
|
| 309 |
public function meta_boxes() { |
| 310 |
return array( |
| 311 |
'lesson_assigned' => array( |
| 312 |
'title' => esc_html__( 'Assigned', 'learnpress' ), |
| 313 |
'callback' => array( $this, 'lesson_assigned' ), |
| 314 |
'context' => 'side', |
| 315 |
'priority' => 'high', |
| 316 |
), |
| 317 |
); |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* @return LP_Lesson_Post_Type|null |
| 322 |
*/ |
| 323 |
public static function instance() { |
| 324 |
if ( ! self::$_instance ) { |
| 325 |
self::$_instance = new self(); |
| 326 |
} |
| 327 |
|
| 328 |
return self::$_instance; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
$lesson_post_type = LP_Lesson_Post_Type::instance(); |
| 333 |
} |
| 334 |
|