← All changes
|
inc/TemplateHooks/Course/CourseMaterialTemplate.php
+34
-13
4.4.7
→
4.4.9
View file →
| @@ -1,11 +1,5 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * Template hooks Archive Package. | |
| 4 | - * | |
| 5 | - * @since 1.0.0 | |
| 6 | - * @version 1.0.0 | |
| 7 | - */ | |
| 8 | 2 | namespace LearnPress\TemplateHooks\Course; |
| 9 | 3 | |
| 10 | 4 | use LearnPress\Helpers\Template; |
| 11 | 5 | use LearnPress\Models\CourseModel; |
| @@ -14,12 +8,20 @@ | ||
| 14 | 8 | use LearnPress\TemplateHooks\TemplateAJAX; |
| 15 | 9 | use LP_Material_Files_DB; |
| 16 | 10 | use stdClass; |
| 17 | 11 | use LP_Global; |
| 18 | -use LP_WP_Filesystem; | |
| 19 | 12 | use LP_Settings; |
| 20 | 13 | use Throwable; |
| 21 | 14 | use Exception; |
| 15 | + | |
| 16 | +defined( 'ABSPATH' ) || exit; | |
| 17 | + | |
| 18 | +/** | |
| 19 | + * Template Material course, lesson. | |
| 20 | + * | |
| 21 | + * @since 1.0.0 | |
| 22 | + * @version 1.0.1 | |
| 23 | + */ | |
| 22 | 24 | class CourseMaterialTemplate { |
| 23 | 25 | public static function instance() { |
| 24 | 26 | static $instance = null; |
| 25 | 27 | |
| @@ -71,8 +73,9 @@ | ||
| 71 | 73 | $args = array_merge( |
| 72 | 74 | [ |
| 73 | 75 | 'id_url' => 'course-material', |
| 74 | 76 | 'item_id' => $item_id, |
| 77 | + 'item_type' => $item ? $item->get_item_type() : LP_COURSE_CPT, | |
| 75 | 78 | 'paged' => 1, |
| 76 | 79 | 'per_page' => $per_page, |
| 77 | 80 | 'total_pages' => $total_pages, |
| 78 | 81 | ], |
| @@ -101,9 +104,13 @@ | ||
| 101 | 104 | try { |
| 102 | 105 | $userModel = UserModel::find( get_current_user_id(), true ); |
| 103 | 106 | $course_id = $args['course_id'] ?? 0; |
| 104 | 107 | $item_id = $args['item_id'] ?? 0; |
| 105 | - if ( get_post_type( $item_id ) === LP_COURSE_CPT ) { | |
| 108 | + $item_type = $args['item_type'] ?? ''; | |
| 109 | + if ( empty( $item_type ) ) { | |
| 110 | + $item_type = get_post_type( $item_id ); | |
| 111 | + } | |
| 112 | + if ( $item_type === LP_COURSE_CPT ) { | |
| 106 | 113 | $courseModel = CourseModel::find( $item_id, true ); |
| 107 | 114 | } elseif ( $course_id ) { |
| 108 | 115 | $courseModel = CourseModel::find( $course_id, true ); |
| 109 | 116 | } else { |
| @@ -109,8 +116,19 @@ | ||
| 109 | 116 | } else { |
| 110 | 117 | throw new Exception( esc_html__( 'Course not found!', 'learnpress' ) ); |
| 111 | 118 | } |
| 112 | 119 | |
| 120 | + if ( ! $courseModel instanceof CourseModel ) { | |
| 121 | + throw new Exception( esc_html__( 'Course not found!', 'learnpress' ) ); | |
| 122 | + } | |
| 123 | + | |
| 124 | + if ( $item_type !== LP_COURSE_CPT ) { | |
| 125 | + $itemModel = $courseModel->get_item_model( $item_id, $item_type ); | |
| 126 | + if ( false === $itemModel ) { | |
| 127 | + throw new Exception( esc_html__( 'Lesson not found!', 'learnpress' ) ); | |
| 128 | + } | |
| 129 | + } | |
| 130 | + | |
| 113 | 131 | $can_show = false; |
| 114 | 132 | if ( $userModel instanceof UserModel ) { |
| 115 | 133 | if ( $courseModel->check_user_is_author( $userModel ) |
| 116 | 134 | || user_can( $userModel->get_id(), ADMIN_ROLE ) ) { |
| @@ -145,9 +163,9 @@ | ||
| 145 | 163 | $content->content = ''; |
| 146 | 164 | } else { |
| 147 | 165 | $material_html = ''; |
| 148 | 166 | foreach ( $material_files as $m ) { |
| 149 | - $material_html .= self::material_item( $m, $args['item_id'] ); | |
| 167 | + $material_html .= self::material_item( $m, $args['item_id'], $item_type ); | |
| 150 | 168 | } |
| 151 | 169 | if ( $args['paged'] === 1 ) { |
| 152 | 170 | $sections = array( |
| 153 | 171 | 'wrap' => '<div class="lp-list-material">', |
| @@ -188,12 +206,12 @@ | ||
| 188 | 206 | ); |
| 189 | 207 | return Template::combine_components( $sections ); |
| 190 | 208 | } |
| 191 | 209 | |
| 192 | - public static function material_item( $material, $current_item_id ) { | |
| 210 | + public static function material_item( $material, $current_item_id, $item_type = '' ) { | |
| 193 | 211 | $sections = array( |
| 194 | 212 | 'wrap' => '<tr class="lp-material-item">', |
| 195 | - 'file-name' => self::html_file_name( $material, $current_item_id ), | |
| 213 | + 'file-name' => self::html_file_name( $material, $current_item_id, $item_type ), | |
| 196 | 214 | 'file-type' => self::html_file_type( $material ), |
| 197 | 215 | 'file-size' => self::html_file_size( $material ), |
| 198 | 216 | 'file-link' => self::html_file_link( $material ), |
| 199 | 217 | 'wrap_end' => '</tr>', |
| @@ -208,10 +226,13 @@ | ||
| 208 | 226 | * @param int $current_item_id Current item ID. |
| 209 | 227 | * |
| 210 | 228 | * @return string |
| 211 | 229 | */ |
| 212 | - public static function html_file_name( $material, $current_item_id ): string { | |
| 213 | - if ( get_post_type( $current_item_id ) == LP_COURSE_CPT && $material->item_type == LP_LESSON_CPT ) { | |
| 230 | + public static function html_file_name( $material, $current_item_id, $item_type = '' ): string { | |
| 231 | + if ( empty( $item_type ) ) { | |
| 232 | + $item_type = get_post_type( $current_item_id ); | |
| 233 | + } | |
| 234 | + if ( $item_type == LP_COURSE_CPT && $material->item_type == LP_LESSON_CPT ) { | |
| 214 | 235 | $html_file_name = sprintf( esc_html( '%1$s ( %2$s )' ), $material->file_name, get_the_title( $material->item_id ) ); |
| 215 | 236 | } else { |
| 216 | 237 | $html_file_name = sprintf( esc_html( '%s' ), $material->file_name ); |
| 217 | 238 | } |