| 1 |
<?php |
| 2 |
/** |
| 3 |
* Template hooks Single Course Offline. |
| 4 |
* |
| 5 |
* @since 4.2.7 |
| 6 |
* @version 1.0.1 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace LearnPress\TemplateHooks\Course; |
| 10 |
|
| 11 |
use LearnPress\Helpers\Config; |
| 12 |
use LearnPress\Helpers\Singleton; |
| 13 |
use LearnPress\Helpers\Template; |
| 14 |
use LearnPress\Models\CourseModel; |
| 15 |
use LearnPress\Models\CoursePostModel; |
| 16 |
use LearnPress\Models\UserModel; |
| 17 |
use LearnPress\TemplateHooks\Instructor\SingleInstructorTemplate; |
| 18 |
use LP_Course; |
| 19 |
use LP_Datetime; |
| 20 |
use Throwable; |
| 21 |
|
| 22 |
class SingleCourseOfflineTemplate { |
| 23 |
use Singleton; |
| 24 |
|
| 25 |
/** |
| 26 |
* @var SingleCourseTemplate |
| 27 |
*/ |
| 28 |
public $singleCourseTemplate; |
| 29 |
|
| 30 |
public function init() { |
| 31 |
$this->singleCourseTemplate = SingleCourseTemplate::instance(); |
| 32 |
|
| 33 |
add_action( |
| 34 |
'learn-press/single-course/offline/layout', |
| 35 |
[ $this, 'course_offline_layout' ] |
| 36 |
); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Offline course layout |
| 41 |
* |
| 42 |
* @param $course |
| 43 |
* |
| 44 |
* @return void |
| 45 |
*/ |
| 46 |
public function course_offline_layout( $course ) { |
| 47 |
if ( ! $course instanceof CourseModel ) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
if ( ! $course->is_offline() ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
$user = UserModel::find( get_current_user_id(), true ); |
| 56 |
|
| 57 |
// Author |
| 58 |
$singleInstructorTemplate = SingleInstructorTemplate::instance(); |
| 59 |
$author = $course->get_author_model(); |
| 60 |
$html_author = ''; |
| 61 |
if ( $author ) { |
| 62 |
$html_author = sprintf( |
| 63 |
'%s %s', |
| 64 |
__( 'By', 'learnpress' ), |
| 65 |
sprintf( '<a href="%s">%s</a>', $author->get_url_instructor(), $singleInstructorTemplate->html_display_name( $author ) ) |
| 66 |
); |
| 67 |
} |
| 68 |
// End author |
| 69 |
|
| 70 |
// Instructor |
| 71 |
$html_instructor = ''; |
| 72 |
if ( $author ) { |
| 73 |
$html_instructor_image = sprintf( |
| 74 |
'<a href="%s" title="%s">%s</a>', |
| 75 |
$author->get_url_instructor(), |
| 76 |
$author->get_display_name(), |
| 77 |
$singleInstructorTemplate->html_avatar( $author ) |
| 78 |
); |
| 79 |
$section_instructor_meta = [ |
| 80 |
'wrapper' => '<div class="lp-instructor-meta">', |
| 81 |
'count_students' => sprintf( |
| 82 |
'<div class="instructor-item-meta">%s</div>', |
| 83 |
$singleInstructorTemplate->html_count_students( $author ) |
| 84 |
), |
| 85 |
'count_courses' => sprintf( |
| 86 |
'<div class="instructor-item-meta">%s</div>', |
| 87 |
$singleInstructorTemplate->html_count_courses( $author ) |
| 88 |
), |
| 89 |
'wrapper_end' => '</div>', |
| 90 |
]; |
| 91 |
$html_instructor_meta = Template::combine_components( $section_instructor_meta ); |
| 92 |
|
| 93 |
$section_instructor_right = apply_filters( |
| 94 |
'learn-press/single-course/offline/section-instructor/right', |
| 95 |
[ |
| 96 |
'wrapper' => '<div class="lp-section-instructor">', |
| 97 |
'name' => sprintf( |
| 98 |
'<a href="%s">%s</a>', |
| 99 |
$author->get_url_instructor(), |
| 100 |
$singleInstructorTemplate->html_display_name( $author ) |
| 101 |
), |
| 102 |
'meta' => $html_instructor_meta, |
| 103 |
'description' => $singleInstructorTemplate->html_description( $author ), |
| 104 |
'social' => $singleInstructorTemplate->html_social( $author ), |
| 105 |
'wrapper_end' => '</div>', |
| 106 |
], |
| 107 |
$course, |
| 108 |
$user |
| 109 |
); |
| 110 |
$html_instructor_right = Template::combine_components( $section_instructor_right ); |
| 111 |
$section_instructor = apply_filters( |
| 112 |
'learn-press/single-course/offline/section-instructor', |
| 113 |
[ |
| 114 |
'wrapper' => '<div class="lp-section-instructor">', |
| 115 |
'header' => sprintf( '<h3 class="section-title">%s</h3>', __( 'Instructor', 'learnpress' ) ), |
| 116 |
'wrapper_info' => '<div class="lp-instructor-info">', |
| 117 |
'image' => $html_instructor_image, |
| 118 |
'instructor_right' => $html_instructor_right, |
| 119 |
'wrapper_info_end' => '</div>', |
| 120 |
'wrapper_end' => '</div>', |
| 121 |
], |
| 122 |
$course, |
| 123 |
$user |
| 124 |
); |
| 125 |
$html_instructor = Template::combine_components( $section_instructor ); |
| 126 |
} |
| 127 |
// End instructor |
| 128 |
|
| 129 |
// Info one |
| 130 |
$html_address = $this->html_address( $course ); |
| 131 |
$section_info_one = apply_filters( |
| 132 |
'learn-press/single-course/offline/info-bar', |
| 133 |
[ |
| 134 |
'wrapper' => '<div class="lp-single-course-offline-info-one">', |
| 135 |
'author' => sprintf( '<div class="item-meta">%s</div>', $html_author ), |
| 136 |
'address' => ! empty( $html_address ) ? sprintf( '<div class="item-meta">%s</div>', $html_address ) : '', |
| 137 |
'wrapper_end' => '</div>', |
| 138 |
], |
| 139 |
$course, |
| 140 |
$user |
| 141 |
); |
| 142 |
|
| 143 |
// Section left |
| 144 |
$section_left = apply_filters( |
| 145 |
'learn-press/single-course/offline/section-left', |
| 146 |
[ |
| 147 |
'wrapper' => '<div class="lp-single-offline-course__left">', |
| 148 |
'breadcrumb' => Template::html_breadcrumb(), |
| 149 |
'title' => $this->singleCourseTemplate->html_title( $course, 'h1' ), |
| 150 |
'info_one' => Template::combine_components( $section_info_one ), |
| 151 |
'image' => $this->singleCourseTemplate->html_image( $course, [ 'size' => 'full' ] ), |
| 152 |
'info_main_mobile' => $this->render_html_info_main( $course, $user, [ 'lp_display_on' => 'lp-is-mobile' ] ), |
| 153 |
'description' => $this->singleCourseTemplate->html_description( $course ), |
| 154 |
'features' => $this->singleCourseTemplate->html_features( $course ), |
| 155 |
'target' => $this->singleCourseTemplate->html_target( $course ), |
| 156 |
'requirements' => $this->singleCourseTemplate->html_requirements( $course ), |
| 157 |
'material' => $this->singleCourseTemplate->html_material( $course ), |
| 158 |
'faqs' => $this->singleCourseTemplate->html_faqs( $course ), |
| 159 |
'instructor' => $html_instructor, |
| 160 |
'featured_review_mobile' => $this->singleCourseTemplate->html_feature_review( $course, $user, [ 'lp_display_on' => 'lp-is-mobile' ] ), |
| 161 |
'wrapper_end' => '</div>', |
| 162 |
], |
| 163 |
$course, |
| 164 |
$user |
| 165 |
); |
| 166 |
|
| 167 |
// Section right |
| 168 |
$section_right = apply_filters( |
| 169 |
'learn-press/single-course/offline/section-right', |
| 170 |
[ |
| 171 |
'wrapper' => '<div class="lp-single-offline-course__right">', |
| 172 |
'wrapper_inner' => '<div class="lp-single-offline-course__right__sticky">', |
| 173 |
'info_main' => $this->render_html_info_main( $course, $user, [ 'lp_display_on' => 'lp-is-pc' ] ), |
| 174 |
'featured_review' => $this->singleCourseTemplate->html_feature_review( $course, $user, [ 'lp_display_on' => 'lp-is-pc' ] ), |
| 175 |
'sidebar' => $this->singleCourseTemplate->html_sidebar( $course ), |
| 176 |
'wrapper_inner_end' => '</div>', |
| 177 |
'wrapper_end' => '</div>', |
| 178 |
], |
| 179 |
$course, |
| 180 |
$user |
| 181 |
); |
| 182 |
// End section right |
| 183 |
|
| 184 |
// Related courses |
| 185 |
ob_start(); |
| 186 |
do_action( 'learn-press/single-course/courses-related/layout', $course, 4 ); |
| 187 |
$html_courses_related = ob_get_clean(); |
| 188 |
// End related courses |
| 189 |
|
| 190 |
$sections = apply_filters( |
| 191 |
'learn-press/single-course/offline/sections', |
| 192 |
[ |
| 193 |
'wrapper_container' => '<div class="lp-content-area">', |
| 194 |
'wrapper' => '<div class="lp-single-course lp-single-offline-course">', |
| 195 |
'wrapper_main' => '<div class="lp-single-offline-course-main">', |
| 196 |
'section_left' => Template::combine_components( $section_left ), |
| 197 |
'section_right' => Template::combine_components( $section_right ), |
| 198 |
'wrapper_main_end' => '</div>', |
| 199 |
'related_courses' => $html_courses_related, |
| 200 |
'wrapper_end' => '</div>', |
| 201 |
'wrapper_container_end' => '</div>', |
| 202 |
] |
| 203 |
); |
| 204 |
|
| 205 |
echo Template::combine_components( $sections ); |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Render html info main |
| 210 |
* Price, deliver type, capacity, level, duration, lessons |
| 211 |
* Buttons: Contact, Buy, Enroll |
| 212 |
* |
| 213 |
* @param CourseModel $courseModel |
| 214 |
* @param UserModel|false $userModel |
| 215 |
* @param array $data |
| 216 |
* |
| 217 |
* @return string |
| 218 |
* @since 4.2.7.6 |
| 219 |
* @version 1.0.1 |
| 220 |
*/ |
| 221 |
public function render_html_info_main( CourseModel $courseModel, $userModel, array $data = [] ): string { |
| 222 |
// Info two |
| 223 |
$data_info_meta = [ |
| 224 |
'price' => [ |
| 225 |
'label' => sprintf( '<span class="currency">%s</span> %s', learn_press_get_currency_symbol(), __( 'Price', 'learnpress' ) ), |
| 226 |
'value' => $this->singleCourseTemplate->html_price( $courseModel ), |
| 227 |
], |
| 228 |
'deliver_type' => [ |
| 229 |
'label' => sprintf( '<span class="lp-icon-bookmark-o"></span> %s', __( 'Delivery type', 'learnpress' ) ), |
| 230 |
'value' => $this->html_deliver_type( $courseModel ), |
| 231 |
], |
| 232 |
'capacity' => [ |
| 233 |
'label' => sprintf( '<span class="lp-icon-students"></span> %s', __( 'Capacity', 'learnpress' ) ), |
| 234 |
'value' => $this->singleCourseTemplate->html_capacity( $courseModel ), |
| 235 |
], |
| 236 |
'level' => [ |
| 237 |
'label' => sprintf( '<span class="lp-icon-signal"></span> %s', __( 'Level', 'learnpress' ) ), |
| 238 |
'value' => $this->singleCourseTemplate->html_level( $courseModel ), |
| 239 |
], |
| 240 |
'duration' => [ |
| 241 |
'label' => sprintf( '<span class="lp-icon-clock-o"></span> %s', __( 'Duration', 'learnpress' ) ), |
| 242 |
'value' => $this->singleCourseTemplate->html_duration( $courseModel ), |
| 243 |
], |
| 244 |
]; |
| 245 |
|
| 246 |
$html_lesson = $this->html_lesson_info( $courseModel ); |
| 247 |
if ( ! empty( $html_lesson ) ) { |
| 248 |
$data_info_meta['lessons'] = [ |
| 249 |
'label' => sprintf( '<span class="lp-icon-copy"></span> %s', __( 'Lessons', 'learnpress' ) ), |
| 250 |
'value' => $html_lesson, |
| 251 |
]; |
| 252 |
} |
| 253 |
|
| 254 |
$data_info_meta = apply_filters( 'learn-press/single-course/offline/info-meta', $data_info_meta, $courseModel, $userModel ); |
| 255 |
$html_info_meta = ''; |
| 256 |
if ( ! empty( $data_info_meta ) ) { |
| 257 |
foreach ( $data_info_meta as $info_meta ) { |
| 258 |
$label = $info_meta['label']; |
| 259 |
$value = $info_meta['value']; |
| 260 |
$html_info_two_item = sprintf( |
| 261 |
'<div class="info-meta-item"> |
| 262 |
<span class="info-meta-left">%s</span> |
| 263 |
<span class="info-meta-right">%s</span> |
| 264 |
</div>', |
| 265 |
$label, |
| 266 |
$value |
| 267 |
); |
| 268 |
$html_info_meta .= $html_info_two_item; |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
$section_buttons = apply_filters( |
| 273 |
'learn-press/single-course/offline/section-right/info-meta/buttons', |
| 274 |
[ |
| 275 |
'wrapper' => '<div class="course-buttons">', |
| 276 |
'btn_contact' => $this->singleCourseTemplate->html_btn_external( $courseModel, $userModel ), |
| 277 |
'btn_buy' => $this->singleCourseTemplate->html_btn_purchase_course( $courseModel, $userModel ), |
| 278 |
'btn_enroll' => $this->singleCourseTemplate->html_btn_enroll_course( $courseModel, $userModel ), |
| 279 |
'wrapper_end' => '</div>', |
| 280 |
], |
| 281 |
$courseModel, |
| 282 |
$userModel |
| 283 |
); |
| 284 |
$html_buttons = Template::combine_components( $section_buttons ); |
| 285 |
|
| 286 |
$section = apply_filters( |
| 287 |
'learn-press/single-course/offline/section-right/info-meta', |
| 288 |
[ |
| 289 |
'wrapper' => sprintf( |
| 290 |
'<div class="info-metas %s">', |
| 291 |
$data['lp_display_on'] ?? '' |
| 292 |
), |
| 293 |
'meta' => $html_info_meta, |
| 294 |
'buttons' => $html_buttons, |
| 295 |
'wrapper_end' => '</div>', |
| 296 |
], |
| 297 |
$courseModel, |
| 298 |
$userModel |
| 299 |
); |
| 300 |
|
| 301 |
return Template::combine_components( $section ); |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* Html lesson info |
| 306 |
* |
| 307 |
* @param CourseModel $course |
| 308 |
* @param bool $show_label |
| 309 |
* |
| 310 |
* @return string |
| 311 |
*/ |
| 312 |
public function html_lesson_info( CourseModel $course, bool $show_label = false ): string { |
| 313 |
if ( ! $course->is_offline() ) { |
| 314 |
return ''; |
| 315 |
} |
| 316 |
|
| 317 |
$lesson_count = $course->get_meta_value_by_key( CoursePostModel::META_KEY_OFFLINE_LESSON_COUNT, 10 ); |
| 318 |
|
| 319 |
if ( ! $lesson_count ) { |
| 320 |
return ''; |
| 321 |
} |
| 322 |
|
| 323 |
$html = sprintf( |
| 324 |
'<span class="course-count-lesson">%s %s</span>', |
| 325 |
$lesson_count, |
| 326 |
$show_label ? __( 'lessons', 'learnpress' ) : '' |
| 327 |
); |
| 328 |
|
| 329 |
return $html; |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* Get html address of course offline |
| 334 |
* |
| 335 |
* @param CourseModel $course |
| 336 |
* |
| 337 |
* @return string |
| 338 |
* @since 4.2.7.3 |
| 339 |
* @version 1.0.0 |
| 340 |
*/ |
| 341 |
public function html_address( CourseModel $course ): string { |
| 342 |
$content = ''; |
| 343 |
|
| 344 |
try { |
| 345 |
if ( ! $course->is_offline() ) { |
| 346 |
return $content; |
| 347 |
} |
| 348 |
|
| 349 |
$address = $course->get_meta_value_by_key( CoursePostModel::META_KEY_ADDRESS, '' ); |
| 350 |
if ( empty( $address ) ) { |
| 351 |
return $content; |
| 352 |
} |
| 353 |
|
| 354 |
$html_wrapper = [ |
| 355 |
'<span class="course-address">' => '</span>', |
| 356 |
]; |
| 357 |
$content = Template::instance()->nest_elements( $html_wrapper, $address ); |
| 358 |
apply_filters( 'learn-press/single-course/html-address', $content, $course ); |
| 359 |
} catch ( Throwable $e ) { |
| 360 |
error_log( __METHOD__ . ': ' . $e->getMessage() ); |
| 361 |
} |
| 362 |
|
| 363 |
return $content; |
| 364 |
} |
| 365 |
|
| 366 |
/** |
| 367 |
* Get deliver type |
| 368 |
* |
| 369 |
* @param CourseModel $course |
| 370 |
* |
| 371 |
* @return string |
| 372 |
* @since 4.2.7.3 |
| 373 |
* @version 1.0.0 |
| 374 |
*/ |
| 375 |
public function html_deliver_type( CourseModel $course ): string { |
| 376 |
$content = ''; |
| 377 |
|
| 378 |
if ( ! $course->is_offline() ) { |
| 379 |
return $content; |
| 380 |
} |
| 381 |
|
| 382 |
$html_wrapper = [ |
| 383 |
'<span class="course-deliver-type">' => '</span>', |
| 384 |
]; |
| 385 |
|
| 386 |
$deliver_type_options = Config::instance()->get( 'course-deliver-type' ); |
| 387 |
$key = $course->get_meta_value_by_key( CoursePostModel::META_KEY_DELIVER, 'private_1_1' ); |
| 388 |
$content = $deliver_type_options[ $key ] ?? ''; |
| 389 |
|
| 390 |
return Template::instance()->nest_elements( $html_wrapper, $content ); |
| 391 |
} |
| 392 |
} |
| 393 |
|