init();
}
public function init() {
add_action( 'learn-press/single-instructor/layout', [ $this, 'sections' ] );
//add_action( 'wp_head', [ $this, 'add_internal_style_to_head' ] );
}
/**
* Get html total courses of instructor.
*
* @param LP_User|UserModel $instructor
* @param string $hidden
*
* @return string
* @version 1.0.1
* @since 4.2.4
*/
public function html_count_courses( $instructor, $hidden = '' ): string {
$content = '';
try {
if ( $instructor instanceof LP_User ) {
$instructor = UserModel::find( $instructor->get_id(), true );
}
/**
* @var UserModel $instructor
*/
$instructor_statistic = $instructor->get_instructor_statistic();
$html = sprintf(
'%d %s',
$instructor_statistic['published_course'],
_n( 'Course', 'Courses', $instructor_statistic['published_course'], 'learnpress' )
);
if ( $hidden === 'text' ) {
$html = $instructor_statistic['published_course'];
}
$sections = [
'wrapper' => '',
'content' => $html,
'wrapper_end' => '',
];
$content = Template::combine_components( $sections );
} catch ( Throwable $e ) {
error_log( __METHOD__ . ': ' . $e->getMessage() );
}
return $content;
}
/**
* Get html total students learn instructor.
*
* @param LP_User|UserModel $instructor
*
* @return string
* @version 1.0.1
* @since 4.2.4
*/
public function html_count_students( $instructor, $hidden = '' ): string {
$content = '';
try {
if ( $instructor instanceof LP_User ) {
$instructor = UserModel::find( $instructor->get_id(), true );
}
/**
* @var UserModel $instructor
*/
$instructor_statistic = $instructor->get_instructor_statistic();
$html = sprintf(
'%d %s',
$instructor_statistic['total_student'],
_n( 'Student', 'Students', $instructor_statistic['total_student'], 'learnpress' )
);
if ( $hidden === 'text' ) {
$html = $instructor_statistic['total_student'];
}
$sections = [
'wrapper' => '',
'content' => $html,
'wrapper_end' => '',
];
$content = Template::combine_components( $sections );
} catch ( Throwable $e ) {
error_log( __METHOD__ . ': ' . $e->getMessage() );
}
return $content;
}
/**
* Get button view instructor.
*
* @param LP_User|UserModel|false $instructor
*
* @return string
* @version 1.0.0
* @since 4.2.3
*/
public function html_button_view( $instructor ): string {
$btn_view = '';
try {
$btn_view = sprintf(
'%s',
$instructor->get_url_instructor(),
__( 'View Profile', 'learnpress' )
);
} catch ( Throwable $e ) {
error_log( __METHOD__ . ': ' . $e->getMessage() );
}
return $btn_view;
}
/**
* Render string to data content
*
* @param UserModel|LP_User $instructor
* @param string $data_content
*
* @return string
*/
public function render_data( $instructor, string $data_content = '' ): string {
if ( $instructor instanceof LP_User ) {
$instructor = UserModel::find( $instructor->get_id(), true );
}
$data_render = str_replace(
[
'{{instructor_id}}',
'{{instructor_avatar}}',
'{{instructor_display_name}}',
'{{instructor_description}}',
'{{instructor_total_courses}}',
'{{instructor_total_students}}',
'{{instructor_social}}',
'{{instructor_url}}',
],
[
$instructor->get_id(),
$this->html_avatar( $instructor ),
$this->html_display_name( $instructor ),
$this->html_description( $instructor ),
$this->html_count_courses( $instructor ),
$this->html_count_students( $instructor ),
$this->html_social( $instructor ),
$instructor->get_url_instructor(),
],
$data_content
);
return apply_filters( 'learn-press/single-instructor/render-data', $data_render, $instructor, $data_content );
}
/**
* List section of layout.
*
* @param array $data
*
* @return void
*/
public function sections( array $data = [] ) {
wp_enqueue_style( 'lp-instructor' );
try {
if ( isset( $data['instructor_id'] ) ) {
$instructor_id = (int) $data['instructor_id'];
$instructor = UserModel::find( $instructor_id, true );
} else {
$instructor = $this->detect_instructor_by_page();
}
if ( ! $instructor || ! $instructor->is_instructor() ) {
return;
}
$sections = apply_filters(
'learn-press/single-instructor/sections',
[
'wrapper' => '
',
'wrapper_inner' => '
',
'info' => $this->info( $instructor ),
'courses' => $this->section_list_courses( $instructor ),
'wrapper_inner_end' => '
',
'wrapper_end' => '
',
],
$instructor
);
echo Template::combine_components( $sections );
} catch ( Throwable $e ) {
error_log( __METHOD__ . ': ' . $e->getMessage() );
}
}
/**
* Detected single instructor Page.
*
* @return false|UserModel
* @since 4.2.3.4
* @version 1.0.4
*/
public function detect_instructor_by_page() {
$instructor = false;
try {
$userModelCurrent = UserModel::find( get_current_user_id(), true );
if ( get_query_var( 'is_single_instructor' ) ) {
$instructor_name = get_query_var( 'instructor_name' );
if ( $instructor_name && 'page' !== $instructor_name ) {
$instructor = UserService::instance()->get_user_by_slug_link( $instructor_name );
} else {
$instructor = $userModelCurrent;
}
}
} catch ( Throwable $e ) {
error_log( __METHOD__ . ': ' . $e->getMessage() );
}
return $instructor;
}
/**
* @param UserModel $instructor
*
* @return false|string
*/
public function info( UserModel $instructor ): string {
$sections = apply_filters(
'learn-press/single-instructor/info/sections',
[
'wrapper' => '',
'cover_img' => ProfileTemplate::instance()->html_cover_image( $instructor ),
'wrapper_content' => '
',
'avatar' => $this->html_avatar_edit( $instructor ),
'info_right' => $this->info_right( $instructor ),
'wrapper_content_end' => '
',
'wrapper_end' => '
',
],
$instructor
);
return Template::combine_components( $sections );
}
/**
* HTML info right of instructor.
*
* @param UserModel $instructor
*
* @return string
* @since 4.2.3.4
* @version 1.0.1
*/
public function info_right( UserModel $instructor ): string {
$section_instructor_meta = [
'wrapper' => '',
];
$sections = apply_filters(
'learn-press/single-instructor/info-right/sections',
[
'wrapper' => '',
'wrapper_content' => '
',
'title' => sprintf( '
%s
', $this->html_display_name( $instructor ) ),
'social' => $this->html_social( $instructor ),
'wrapper_content_end' => '',
'meta' => Template::combine_components( $section_instructor_meta ),
'description' => $this->html_description( $instructor ),
'wrapper_end' => '
',
],
$instructor
);
return Template::combine_components( $sections );
}
public function section_list_courses( UserModel $instructor ): string {
$content = '';
try {
// Query courses of instructor
$filter = new LP_Course_Filter();
Courses::handle_params_for_query_courses( $filter, [] );
$filter->post_author = $instructor->get_id();
$filter->limit = LP_Settings::get_option( 'archive_course_limit', 20 );
$filter->page = $GLOBALS['wp_query']->get( 'paged', 1 ) ? $GLOBALS['wp_query']->get( 'paged', 1 ) : 1;
$filter = apply_filters( 'lp/single-instructor/courses/query/filter', $filter, [] );
$total_courses = 0;
$courses = Courses::get_courses( $filter, $total_courses );
if ( ! empty( $courses ) ) {
$html_courses = $this->list_courses( $instructor, $courses );
} else {
$html_courses = Template::print_message(
sprintf(
__( '%s does not have any courses', 'learnpress' ),
$instructor->get_display_name()
),
'info',
false
);
}
$sections = apply_filters(
'learn-press/single-instructor/courses/sections',
[
'wrapper' => '',
'courses' => $html_courses,
'pagination' => $this->courses_pagination( $filter->page, $filter->limit, $total_courses ),
'wrapper_end' => '
',
],
$courses,
$instructor
);
$content = Template::combine_components( $sections );
} catch ( Throwable $e ) {
error_log( __METHOD__ . ': ' . $e->getMessage() );
}
return $content;
}
/**
* Display list courses.
*
* @param $instructor
* @param $courses
*
* @return string
*/
public function list_courses( $instructor, $courses ): string {
$content = '';
try {
// Li list courses
$html_li_item_course = '';
foreach ( $courses as $course_obj ) {
$course = CourseModel::find( $course_obj->ID, true );
$html_li_item_course .= ListCoursesTemplate::render_course( $course );
}
$sections = [
'wrapper' => '',
'list_course' => $html_li_item_course,
'wrapper_end' => '
',
];
$content = Template::combine_components( $sections );
} catch ( Throwable $e ) {
error_log( __METHOD__ . ': ' . $e->getMessage() );
}
return $content;
}
/**
* Pagination courses.
*
* @param int $page
* @param int $limit
* @param int $total_courses
*
* @return string
*/
public function courses_pagination( int $page, int $limit, int $total_courses ): string {
$content = '';
try {
$total_pages = \LP_Database::get_total_pages( $limit, $total_courses );
$data_pagination = array(
'total' => $total_pages,
'current' => max( 1, $page ),
'base' => esc_url_raw( str_replace( 999999999, '%#%', get_pagenum_link( 999999999, false ) ) ),
'format' => '',
'per_page' => $limit,
);
ob_start();
Template::instance()->get_frontend_template( 'shared/pagination.php', $data_pagination );
$content = ob_get_clean();
} catch ( Throwable $e ) {
ob_end_clean();
error_log( __METHOD__ . ': ' . $e->getMessage() );
}
return $content;
}
}