| 1 |
<?php |
| 2 |
/** |
| 3 |
* Template hooks List Instructors. |
| 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\UserModel; |
| 13 |
use LP_Assets; |
| 14 |
use LP_Helper; |
| 15 |
use LP_Page_Controller; |
| 16 |
use LP_User; |
| 17 |
use LP_WP_Filesystem; |
| 18 |
use Throwable; |
| 19 |
use WP_Query; |
| 20 |
|
| 21 |
class ListInstructorsTemplate { |
| 22 |
public static function instance() { |
| 23 |
static $instance = null; |
| 24 |
|
| 25 |
if ( is_null( $instance ) ) { |
| 26 |
$instance = new self(); |
| 27 |
} |
| 28 |
|
| 29 |
return $instance; |
| 30 |
} |
| 31 |
|
| 32 |
protected function __construct() { |
| 33 |
add_action( 'learn-press/list-instructors/layout', [ $this, 'sections' ] ); |
| 34 |
add_action( 'wp_head', [ $this, 'add_internal_scripts_to_head' ] ); |
| 35 |
} |
| 36 |
|
| 37 |
public function add_internal_scripts_to_head() { |
| 38 |
if ( ! class_exists( 'LP_Page_Controller' ) || ! LP_Page_Controller::is_page_instructors() ) { |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
LP_Helper::print_inline_script_tag( 'lpSkeletonParam', lp_archive_skeleton_get_args(), [ 'id' => 'lpSkeletonParam' ] ); |
| 43 |
?> |
| 44 |
<script id="lp-list-instructors-data"> |
| 45 |
const lpInstructorsUrl = '<?php echo learn_press_get_page_link( 'instructors' ); ?>'; |
| 46 |
const urlListInstructorsAPI = '<?php echo site_url( 'wp-json/lp/v1/instructors' ); ?>'; |
| 47 |
</script> |
| 48 |
<?php |
| 49 |
$is_rtl = is_rtl() ? '-rtl' : ''; |
| 50 |
$min = LP_Assets::$_min_assets; |
| 51 |
?> |
| 52 |
<style id="lp-list-instructors"> |
| 53 |
<?php echo wp_remote_fopen( LP_Assets::instance()->url( 'css/instructors' . $is_rtl . $min . '.css' ) ); ?> |
| 54 |
</style> |
| 55 |
<script id="lp-list-instructors" async data-wp-strategy="async" > |
| 56 |
<?php //echo wp_remote_fopen( LP_Assets::instance()->url( 'js/dist/frontend/instructors' . $min . '.js' ) ); ?> |
| 57 |
<?php echo LP_WP_Filesystem::instance()->file_get_contents( LP_PLUGIN_PATH . 'assets/js/dist/frontend/instructors' . $min . '.js' ); ?> |
| 58 |
</script> |
| 59 |
<?php |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* List section of layout. |
| 64 |
* |
| 65 |
* @param array $data |
| 66 |
* |
| 67 |
* @return void |
| 68 |
* @version 1.0.0 |
| 69 |
* @since 4.2.3 |
| 70 |
*/ |
| 71 |
public function sections( array $data = [] ) { |
| 72 |
if ( ! LP_Page_Controller::is_page_instructors() ) { |
| 73 |
wp_enqueue_style( 'lp-instructors' ); |
| 74 |
wp_enqueue_script( 'lp-instructors' ); |
| 75 |
} |
| 76 |
/** |
| 77 |
* @var WP_Query $wp_query |
| 78 |
*/ |
| 79 |
global $wp_query; |
| 80 |
|
| 81 |
try { |
| 82 |
ob_start(); |
| 83 |
?> |
| 84 |
<ul class="ul-list-instructors"> |
| 85 |
<?php lp_skeleton_animation_html( 10 ); ?> |
| 86 |
</ul> |
| 87 |
<?php |
| 88 |
$skeleton = ob_get_clean(); |
| 89 |
|
| 90 |
$sections = apply_filters( |
| 91 |
'learn-press/list-instructors/sections', |
| 92 |
[ |
| 93 |
'wrapper' => '<div class="lp-content-area">', |
| 94 |
'wrapper_inner' => '<div class="lp-list-instructors">', |
| 95 |
'skeleton' => $skeleton, |
| 96 |
'wrapper_inner_end' => '</div>', |
| 97 |
'wrapper_end' => '</div>', |
| 98 |
], |
| 99 |
$data |
| 100 |
); |
| 101 |
|
| 102 |
echo Template::combine_components( $sections ); |
| 103 |
|
| 104 |
} catch ( Throwable $e ) { |
| 105 |
error_log( __METHOD__ . ': ' . $e->getMessage() ); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Get instructor item. |
| 111 |
* |
| 112 |
* @param UserModel $instructor |
| 113 |
* |
| 114 |
* @return string |
| 115 |
*/ |
| 116 |
public function instructor_item( UserModel $instructor ): string { |
| 117 |
$content = ''; |
| 118 |
|
| 119 |
try { |
| 120 |
$singleInstructorTemplate = SingleInstructorTemplate::instance(); |
| 121 |
|
| 122 |
$items = apply_filters( |
| 123 |
'learn-press/list-instructors/instructor_item/sections', |
| 124 |
[ |
| 125 |
'img' => $singleInstructorTemplate->html_avatar( $instructor ), |
| 126 |
'name' => sprintf( |
| 127 |
'<a class="instructor-name" href="%s">%s</a>', |
| 128 |
$instructor->get_url_instructor(), |
| 129 |
$singleInstructorTemplate->html_display_name( $instructor ) |
| 130 |
), |
| 131 |
'info' => $this->instructor_item_info( $instructor ), |
| 132 |
'btn_view' => $singleInstructorTemplate->html_button_view( $instructor ), |
| 133 |
], |
| 134 |
$instructor, |
| 135 |
$singleInstructorTemplate |
| 136 |
); |
| 137 |
|
| 138 |
$sections = apply_filters( |
| 139 |
'learn-press/list-instructors/instructor_item/wrapper', |
| 140 |
[ |
| 141 |
'wrapper' => '<li class="item-instructor">', |
| 142 |
'content' => Template::combine_components( $items ), |
| 143 |
'wrapper_end' => '</li>', |
| 144 |
], |
| 145 |
$instructor |
| 146 |
); |
| 147 |
|
| 148 |
$content = Template::combine_components( $sections ); |
| 149 |
} catch ( Throwable $e ) { |
| 150 |
error_log( __METHOD__ . ': ' . $e->getMessage() ); |
| 151 |
} |
| 152 |
|
| 153 |
return $content; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Get instructor info: total courses, total students. |
| 158 |
* |
| 159 |
* @param UserModel $instructor |
| 160 |
* |
| 161 |
* @return string |
| 162 |
* @version 1.0.1 |
| 163 |
* @since 4.2.3 |
| 164 |
*/ |
| 165 |
public function instructor_item_info( UserModel $instructor ): string { |
| 166 |
$content = ''; |
| 167 |
|
| 168 |
try { |
| 169 |
$singleInstructorTemplate = SingleInstructorTemplate::instance(); |
| 170 |
|
| 171 |
$html_total_courses = sprintf( |
| 172 |
'<div class="instructor-count-courses"><span class="lp-ico lp-icon-book-open"></span> %s</div>', |
| 173 |
$singleInstructorTemplate->html_count_courses( $instructor ) |
| 174 |
); |
| 175 |
|
| 176 |
$html_total_students = sprintf( |
| 177 |
'<div class="instructor-count-students"><span class="lp-ico lp-icon-students"></span> %s</div>', |
| 178 |
$singleInstructorTemplate->html_count_students( $instructor ) |
| 179 |
); |
| 180 |
|
| 181 |
$items = apply_filters( |
| 182 |
'learn-press/list-instructors/instructor_item/info/sections', |
| 183 |
[ |
| 184 |
'total_courses' => $html_total_courses, |
| 185 |
'total_students' => $html_total_students, |
| 186 |
], |
| 187 |
$instructor, |
| 188 |
$singleInstructorTemplate |
| 189 |
); |
| 190 |
|
| 191 |
$sections = apply_filters( |
| 192 |
'learn-press/list-instructors/instructor_item/wrapper', |
| 193 |
[ |
| 194 |
'wrapper' => '<div class="instructor-info">', |
| 195 |
'content' => Template::combine_components( $items ), |
| 196 |
'wrapper_end' => '</div>', |
| 197 |
], |
| 198 |
$instructor |
| 199 |
); |
| 200 |
|
| 201 |
$content = Template::combine_components( $sections ); |
| 202 |
} catch ( Throwable $e ) { |
| 203 |
error_log( __METHOD__ . ': ' . $e->getMessage() ); |
| 204 |
} |
| 205 |
|
| 206 |
return $content; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Pagination of list instructor. |
| 211 |
* |
| 212 |
* @param int $page |
| 213 |
* @param int $limit |
| 214 |
* @param int $total_courses |
| 215 |
* |
| 216 |
* @return string |
| 217 |
*/ |
| 218 |
public function instructors_pagination( int $page, int $limit, int $total_courses ): string { |
| 219 |
$content = ''; |
| 220 |
|
| 221 |
$instructors_page_id = learn_press_get_page_id( 'instructors' ); |
| 222 |
$instructors_page_url = trailingslashit( get_permalink( $instructors_page_id ) ); |
| 223 |
|
| 224 |
try { |
| 225 |
$total_pages = \LP_Database::get_total_pages( $limit, $total_courses ); |
| 226 |
$data_pagination = array( |
| 227 |
'total' => $total_pages, |
| 228 |
'current' => max( 1, $page ), |
| 229 |
'base' => esc_url_raw( trailingslashit( $instructors_page_url . 'page/%#%' ) ), |
| 230 |
'format' => '', |
| 231 |
'per_page' => $limit, |
| 232 |
); |
| 233 |
|
| 234 |
ob_start(); |
| 235 |
Template::instance()->get_frontend_template( 'shared/pagination.php', $data_pagination ); |
| 236 |
$content = ob_get_clean(); |
| 237 |
} catch ( Throwable $e ) { |
| 238 |
ob_end_clean(); |
| 239 |
error_log( __METHOD__ . ': ' . $e->getMessage() ); |
| 240 |
} |
| 241 |
|
| 242 |
return $content; |
| 243 |
} |
| 244 |
} |
| 245 |
|