| 1 |
<?php |
| 2 |
/** |
| 3 |
* Template hooks Single Instructor. |
| 4 |
* |
| 5 |
* @since 4.2.3 |
| 6 |
* @version 1.0.1 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace LearnPress\TemplateHooks\Instructor; |
| 10 |
|
| 11 |
use LearnPress\Helpers\Template; |
| 12 |
use LearnPress\Models\CourseModel; |
| 13 |
use LearnPress\Models\Courses; |
| 14 |
use LearnPress\Models\UserModel; |
| 15 |
use LearnPress\Services\UserService; |
| 16 |
use LearnPress\TemplateHooks\Profile\ProfileTemplate; |
| 17 |
use LearnPress\TemplateHooks\Course\ListCoursesTemplate; |
| 18 |
use LearnPress\TemplateHooks\UserTemplate; |
| 19 |
use LP_Course_Filter; |
| 20 |
use LP_Settings; |
| 21 |
use LP_User; |
| 22 |
use Throwable; |
| 23 |
|
| 24 |
class SingleInstructorTemplate extends UserTemplate { |
| 25 |
public static function instance() { |
| 26 |
static $instance = null; |
| 27 |
if ( is_null( $instance ) ) { |
| 28 |
$instance = new self(); |
| 29 |
} |
| 30 |
|
| 31 |
return $instance; |
| 32 |
} |
| 33 |
|
| 34 |
public function __construct( $class_name = 'instructor' ) { |
| 35 |
parent::__construct( $class_name ); |
| 36 |
$this->init(); |
| 37 |
} |
| 38 |
|
| 39 |
public function init() { |
| 40 |
add_action( 'learn-press/single-instructor/layout', [ $this, 'sections' ] ); |
| 41 |
//add_action( 'wp_head', [ $this, 'add_internal_style_to_head' ] ); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Get html total courses of instructor. |
| 46 |
* |
| 47 |
* @param LP_User|UserModel $instructor |
| 48 |
* @param string $hidden |
| 49 |
* |
| 50 |
* @return string |
| 51 |
* @version 1.0.1 |
| 52 |
* @since 4.2.4 |
| 53 |
*/ |
| 54 |
public function html_count_courses( $instructor, $hidden = '' ): string { |
| 55 |
$content = ''; |
| 56 |
|
| 57 |
try { |
| 58 |
if ( $instructor instanceof LP_User ) { |
| 59 |
$instructor = UserModel::find( $instructor->get_id(), true ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* @var UserModel $instructor |
| 64 |
*/ |
| 65 |
$instructor_statistic = $instructor->get_instructor_statistic(); |
| 66 |
$html = sprintf( |
| 67 |
'%d %s', |
| 68 |
$instructor_statistic['published_course'], |
| 69 |
_n( 'Course', 'Courses', $instructor_statistic['published_course'], 'learnpress' ) |
| 70 |
); |
| 71 |
if ( $hidden === 'text' ) { |
| 72 |
$html = $instructor_statistic['published_course']; |
| 73 |
} |
| 74 |
|
| 75 |
$sections = [ |
| 76 |
'wrapper' => '<span class="instructor-total-courses">', |
| 77 |
'content' => $html, |
| 78 |
'wrapper_end' => '</span>', |
| 79 |
]; |
| 80 |
|
| 81 |
$content = Template::combine_components( $sections ); |
| 82 |
} catch ( Throwable $e ) { |
| 83 |
error_log( __METHOD__ . ': ' . $e->getMessage() ); |
| 84 |
} |
| 85 |
|
| 86 |
return $content; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Get html total students learn instructor. |
| 91 |
* |
| 92 |
* @param LP_User|UserModel $instructor |
| 93 |
* |
| 94 |
* @return string |
| 95 |
* @version 1.0.1 |
| 96 |
* @since 4.2.4 |
| 97 |
*/ |
| 98 |
public function html_count_students( $instructor, $hidden = '' ): string { |
| 99 |
$content = ''; |
| 100 |
|
| 101 |
try { |
| 102 |
if ( $instructor instanceof LP_User ) { |
| 103 |
$instructor = UserModel::find( $instructor->get_id(), true ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* @var UserModel $instructor |
| 108 |
*/ |
| 109 |
$instructor_statistic = $instructor->get_instructor_statistic(); |
| 110 |
$html = sprintf( |
| 111 |
'%d %s', |
| 112 |
$instructor_statistic['total_student'], |
| 113 |
_n( 'Student', 'Students', $instructor_statistic['total_student'], 'learnpress' ) |
| 114 |
); |
| 115 |
if ( $hidden === 'text' ) { |
| 116 |
$html = $instructor_statistic['total_student']; |
| 117 |
} |
| 118 |
|
| 119 |
$sections = [ |
| 120 |
'wrapper' => '<span class="instructor-total-students">', |
| 121 |
'content' => $html, |
| 122 |
'wrapper_end' => '</span>', |
| 123 |
]; |
| 124 |
|
| 125 |
$content = Template::combine_components( $sections ); |
| 126 |
} catch ( Throwable $e ) { |
| 127 |
error_log( __METHOD__ . ': ' . $e->getMessage() ); |
| 128 |
} |
| 129 |
|
| 130 |
return $content; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Get button view instructor. |
| 135 |
* |
| 136 |
* @param LP_User|UserModel|false $instructor |
| 137 |
* |
| 138 |
* @return string |
| 139 |
* @version 1.0.0 |
| 140 |
* @since 4.2.3 |
| 141 |
*/ |
| 142 |
public function html_button_view( $instructor ): string { |
| 143 |
$btn_view = ''; |
| 144 |
|
| 145 |
try { |
| 146 |
$btn_view = sprintf( |
| 147 |
'<a href="%s" class="instructor-btn-view">%s</a>', |
| 148 |
$instructor->get_url_instructor(), |
| 149 |
__( 'View Profile', 'learnpress' ) |
| 150 |
); |
| 151 |
} catch ( Throwable $e ) { |
| 152 |
error_log( __METHOD__ . ': ' . $e->getMessage() ); |
| 153 |
} |
| 154 |
|
| 155 |
return $btn_view; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Render string to data content |
| 160 |
* |
| 161 |
* @param UserModel|LP_User $instructor |
| 162 |
* @param string $data_content |
| 163 |
* |
| 164 |
* @return string |
| 165 |
*/ |
| 166 |
public function render_data( $instructor, string $data_content = '' ): string { |
| 167 |
if ( $instructor instanceof LP_User ) { |
| 168 |
$instructor = UserModel::find( $instructor->get_id(), true ); |
| 169 |
} |
| 170 |
|
| 171 |
$data_render = str_replace( |
| 172 |
[ |
| 173 |
'{{instructor_id}}', |
| 174 |
'{{instructor_avatar}}', |
| 175 |
'{{instructor_display_name}}', |
| 176 |
'{{instructor_description}}', |
| 177 |
'{{instructor_total_courses}}', |
| 178 |
'{{instructor_total_students}}', |
| 179 |
'{{instructor_social}}', |
| 180 |
'{{instructor_url}}', |
| 181 |
], |
| 182 |
[ |
| 183 |
$instructor->get_id(), |
| 184 |
$this->html_avatar( $instructor ), |
| 185 |
$this->html_display_name( $instructor ), |
| 186 |
$this->html_description( $instructor ), |
| 187 |
$this->html_count_courses( $instructor ), |
| 188 |
$this->html_count_students( $instructor ), |
| 189 |
$this->html_social( $instructor ), |
| 190 |
$instructor->get_url_instructor(), |
| 191 |
], |
| 192 |
$data_content |
| 193 |
); |
| 194 |
|
| 195 |
return apply_filters( 'learn-press/single-instructor/render-data', $data_render, $instructor, $data_content ); |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* List section of layout. |
| 200 |
* |
| 201 |
* @param array $data |
| 202 |
* |
| 203 |
* @return void |
| 204 |
*/ |
| 205 |
public function sections( array $data = [] ) { |
| 206 |
wp_enqueue_style( 'lp-instructor' ); |
| 207 |
|
| 208 |
try { |
| 209 |
if ( isset( $data['instructor_id'] ) ) { |
| 210 |
$instructor_id = (int) $data['instructor_id']; |
| 211 |
$instructor = UserModel::find( $instructor_id, true ); |
| 212 |
} else { |
| 213 |
$instructor = $this->detect_instructor_by_page(); |
| 214 |
} |
| 215 |
|
| 216 |
if ( ! $instructor || ! $instructor->is_instructor() ) { |
| 217 |
return; |
| 218 |
} |
| 219 |
|
| 220 |
$sections = apply_filters( |
| 221 |
'learn-press/single-instructor/sections', |
| 222 |
[ |
| 223 |
'wrapper' => '<div class="lp-content-area">', |
| 224 |
'wrapper_inner' => '<div class="lp-single-instructor">', |
| 225 |
'info' => $this->info( $instructor ), |
| 226 |
'courses' => $this->section_list_courses( $instructor ), |
| 227 |
'wrapper_inner_end' => '</div>', |
| 228 |
'wrapper_end' => '</div>', |
| 229 |
], |
| 230 |
$instructor |
| 231 |
); |
| 232 |
|
| 233 |
echo Template::combine_components( $sections ); |
| 234 |
} catch ( Throwable $e ) { |
| 235 |
error_log( __METHOD__ . ': ' . $e->getMessage() ); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Detected single instructor Page. |
| 241 |
* |
| 242 |
* @return false|UserModel |
| 243 |
* @since 4.2.3.4 |
| 244 |
* @version 1.0.4 |
| 245 |
*/ |
| 246 |
public function detect_instructor_by_page() { |
| 247 |
$instructor = false; |
| 248 |
|
| 249 |
try { |
| 250 |
$userModelCurrent = UserModel::find( get_current_user_id(), true ); |
| 251 |
if ( get_query_var( 'is_single_instructor' ) ) { |
| 252 |
$instructor_name = get_query_var( 'instructor_name' ); |
| 253 |
if ( $instructor_name && 'page' !== $instructor_name ) { |
| 254 |
$instructor = UserService::instance()->get_user_by_slug_link( $instructor_name ); |
| 255 |
} else { |
| 256 |
$instructor = $userModelCurrent; |
| 257 |
} |
| 258 |
} |
| 259 |
} catch ( Throwable $e ) { |
| 260 |
error_log( __METHOD__ . ': ' . $e->getMessage() ); |
| 261 |
} |
| 262 |
|
| 263 |
return $instructor; |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* @param UserModel $instructor |
| 268 |
* |
| 269 |
* @return false|string |
| 270 |
*/ |
| 271 |
public function info( UserModel $instructor ): string { |
| 272 |
$sections = apply_filters( |
| 273 |
'learn-press/single-instructor/info/sections', |
| 274 |
[ |
| 275 |
'wrapper' => '<div class="lp-single-instructor__info">', |
| 276 |
'cover_img' => ProfileTemplate::instance()->html_cover_image( $instructor ), |
| 277 |
'wrapper_content' => '<div class="lp-single-instructor__info__wrapper">', |
| 278 |
'avatar' => $this->html_avatar_edit( $instructor ), |
| 279 |
'info_right' => $this->info_right( $instructor ), |
| 280 |
'wrapper_content_end' => '</div>', |
| 281 |
'wrapper_end' => '</div>', |
| 282 |
], |
| 283 |
$instructor |
| 284 |
); |
| 285 |
|
| 286 |
return Template::combine_components( $sections ); |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* HTML info right of instructor. |
| 291 |
* |
| 292 |
* @param UserModel $instructor |
| 293 |
* |
| 294 |
* @return string |
| 295 |
* @since 4.2.3.4 |
| 296 |
* @version 1.0.1 |
| 297 |
*/ |
| 298 |
public function info_right( UserModel $instructor ): string { |
| 299 |
$section_instructor_meta = [ |
| 300 |
'wrapper' => '<div class="lp-instructor-meta">', |
| 301 |
'count_students' => sprintf( |
| 302 |
'<div class="instructor-item-meta"><i class="lp-icon-user-graduate"></i>%s</div>', |
| 303 |
$this->html_count_students( $instructor ) |
| 304 |
), |
| 305 |
'count_courses' => sprintf( |
| 306 |
'<div class="instructor-item-meta"><i class="lp-icon-file"></i>%s</div>', |
| 307 |
$this->html_count_courses( $instructor ) |
| 308 |
), |
| 309 |
'wrapper_end' => '</div>', |
| 310 |
]; |
| 311 |
|
| 312 |
$sections = apply_filters( |
| 313 |
'learn-press/single-instructor/info-right/sections', |
| 314 |
[ |
| 315 |
'wrapper' => '<div class="lp-single-instructor__info__right">', |
| 316 |
'wrapper_content' => '<div class="lp-single-instructor__info__right__content">', |
| 317 |
'title' => sprintf( '<h2>%s</h2>', $this->html_display_name( $instructor ) ), |
| 318 |
'social' => $this->html_social( $instructor ), |
| 319 |
'wrapper_content_end' => '</div>', |
| 320 |
'meta' => Template::combine_components( $section_instructor_meta ), |
| 321 |
'description' => $this->html_description( $instructor ), |
| 322 |
'wrapper_end' => '</div>', |
| 323 |
], |
| 324 |
$instructor |
| 325 |
); |
| 326 |
|
| 327 |
return Template::combine_components( $sections ); |
| 328 |
} |
| 329 |
|
| 330 |
public function section_list_courses( UserModel $instructor ): string { |
| 331 |
$content = ''; |
| 332 |
|
| 333 |
try { |
| 334 |
// Query courses of instructor |
| 335 |
$filter = new LP_Course_Filter(); |
| 336 |
Courses::handle_params_for_query_courses( $filter, [] ); |
| 337 |
$filter->post_author = $instructor->get_id(); |
| 338 |
$filter->limit = LP_Settings::get_option( 'archive_course_limit', 20 ); |
| 339 |
$filter->page = $GLOBALS['wp_query']->get( 'paged', 1 ) ? $GLOBALS['wp_query']->get( 'paged', 1 ) : 1; |
| 340 |
$filter = apply_filters( 'lp/single-instructor/courses/query/filter', $filter, [] ); |
| 341 |
|
| 342 |
$total_courses = 0; |
| 343 |
$courses = Courses::get_courses( $filter, $total_courses ); |
| 344 |
if ( ! empty( $courses ) ) { |
| 345 |
$html_courses = $this->list_courses( $instructor, $courses ); |
| 346 |
} else { |
| 347 |
$html_courses = Template::print_message( |
| 348 |
sprintf( |
| 349 |
__( '%s does not have any courses', 'learnpress' ), |
| 350 |
$instructor->get_display_name() |
| 351 |
), |
| 352 |
'info', |
| 353 |
false |
| 354 |
); |
| 355 |
} |
| 356 |
|
| 357 |
$sections = apply_filters( |
| 358 |
'learn-press/single-instructor/courses/sections', |
| 359 |
[ |
| 360 |
'wrapper' => '<div class="instructor-courses learn-press-courses">', |
| 361 |
'courses' => $html_courses, |
| 362 |
'pagination' => $this->courses_pagination( $filter->page, $filter->limit, $total_courses ), |
| 363 |
'wrapper_end' => '</div>', |
| 364 |
], |
| 365 |
$courses, |
| 366 |
$instructor |
| 367 |
); |
| 368 |
|
| 369 |
$content = Template::combine_components( $sections ); |
| 370 |
} catch ( Throwable $e ) { |
| 371 |
error_log( __METHOD__ . ': ' . $e->getMessage() ); |
| 372 |
} |
| 373 |
|
| 374 |
return $content; |
| 375 |
} |
| 376 |
|
| 377 |
/** |
| 378 |
* Display list courses. |
| 379 |
* |
| 380 |
* @param $instructor |
| 381 |
* @param $courses |
| 382 |
* |
| 383 |
* @return string |
| 384 |
*/ |
| 385 |
public function list_courses( $instructor, $courses ): string { |
| 386 |
$content = ''; |
| 387 |
|
| 388 |
try { |
| 389 |
// Li list courses |
| 390 |
$html_li_item_course = ''; |
| 391 |
foreach ( $courses as $course_obj ) { |
| 392 |
$course = CourseModel::find( $course_obj->ID, true ); |
| 393 |
$html_li_item_course .= ListCoursesTemplate::render_course( $course ); |
| 394 |
} |
| 395 |
|
| 396 |
$sections = [ |
| 397 |
'wrapper' => '<ul class="ul-instructor-courses">', |
| 398 |
'list_course' => $html_li_item_course, |
| 399 |
'wrapper_end' => '</ul>', |
| 400 |
]; |
| 401 |
|
| 402 |
$content = Template::combine_components( $sections ); |
| 403 |
} catch ( Throwable $e ) { |
| 404 |
error_log( __METHOD__ . ': ' . $e->getMessage() ); |
| 405 |
} |
| 406 |
|
| 407 |
return $content; |
| 408 |
} |
| 409 |
|
| 410 |
/** |
| 411 |
* Pagination courses. |
| 412 |
* |
| 413 |
* @param int $page |
| 414 |
* @param int $limit |
| 415 |
* @param int $total_courses |
| 416 |
* |
| 417 |
* @return string |
| 418 |
*/ |
| 419 |
public function courses_pagination( int $page, int $limit, int $total_courses ): string { |
| 420 |
$content = ''; |
| 421 |
|
| 422 |
try { |
| 423 |
$total_pages = \LP_Database::get_total_pages( $limit, $total_courses ); |
| 424 |
$data_pagination = array( |
| 425 |
'total' => $total_pages, |
| 426 |
'current' => max( 1, $page ), |
| 427 |
'base' => esc_url_raw( str_replace( 999999999, '%#%', get_pagenum_link( 999999999, false ) ) ), |
| 428 |
'format' => '', |
| 429 |
'per_page' => $limit, |
| 430 |
); |
| 431 |
|
| 432 |
ob_start(); |
| 433 |
Template::instance()->get_frontend_template( 'shared/pagination.php', $data_pagination ); |
| 434 |
$content = ob_get_clean(); |
| 435 |
} catch ( Throwable $e ) { |
| 436 |
ob_end_clean(); |
| 437 |
error_log( __METHOD__ . ': ' . $e->getMessage() ); |
| 438 |
} |
| 439 |
|
| 440 |
return $content; |
| 441 |
} |
| 442 |
} |
| 443 |
|