PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.3.9
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.3.9
4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
learnpress / inc / rest-api / v1 / frontend / class-lp-rest-lazy-load-controller.php

class-lp-rest-lazy-load-controller.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.3.9, at inc/rest-api/v1/frontend/class-lp-rest-lazy-load-controller.php

330 lines 10.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use LearnPress\Helpers\Template;
4
5 /**
6 * Class LP_REST_Lazy_Load_Controller
7 */
8 class LP_REST_Lazy_Load_Controller extends LP_Abstract_REST_Controller {
9 public function __construct() {
10 $this->namespace = 'lp/v1';
11 $this->rest_base = 'lazy-load';
12
13 parent::__construct();
14 }
15
16 public function register_routes() {
17 $this->routes = array(
18 'course-progress' => array(
19 array(
20 'methods' => WP_REST_Server::CREATABLE,
21 'callback' => array( $this, 'user_progress' ),
22 'permission_callback' => function () {
23 return is_user_logged_in();
24 },
25 ),
26 ),
27 'course-curriculum' => array(
28 array(
29 'methods' => WP_REST_Server::READABLE,
30 'callback' => array( $this, 'course_curriculum' ),
31 'permission_callback' => '__return_true',
32 ),
33 ),
34 'course-curriculum-items' => array(
35 array(
36 'methods' => WP_REST_Server::READABLE,
37 'callback' => array( $this, 'course_curriculum_items' ),
38 'permission_callback' => '__return_true',
39 ),
40 ),
41 'items-progress' => array(
42 array(
43 'methods' => WP_REST_Server::READABLE,
44 'callback' => array( $this, 'items_progress' ),
45 'permission_callback' => function () {
46 return is_user_logged_in();
47 },
48 ),
49 ),
50 );
51
52 parent::register_routes();
53 }
54
55 /**
56 * Load items progress in single curriculum items.
57 *
58 * @param [type] $request
59 *
60 * @return WP_REST_Response|WP_Error
61 *
62 * @author Nhamdv <daonham95>
63 */
64 public function items_progress( $request ) {
65 $params = $request->get_params();
66 $course_id = $params['courseId'] ?? 0;
67 $user_id = get_current_user_id();
68 $response = new LP_REST_Response();
69 $response->data = '';
70
71 try {
72 $course = learn_press_get_course( $course_id );
73 if ( ! $course ) {
74 throw new Exception( esc_html__( 'The course is invalid!', 'learnpress' ) );
75 }
76
77 if ( $course->is_no_required_enroll() ) {
78 throw new Exception( esc_html__( 'The course is not required to enroll!', 'learnpress' ) );
79 }
80
81 $user = learn_press_get_user( $user_id );
82
83 $check = $user->can_show_finish_course_btn( $course );
84
85 if ( $check['status'] !== 'success' ) {
86 throw new Exception( $check['message'] );
87 }
88
89 $response->status = 'success';
90 $response->data = learn_press_get_template_content(
91 'single-course/buttons/finish.php',
92 array(
93 'course' => $course,
94 'user' => $user,
95 )
96 );
97 } catch ( Exception $e ) {
98 $response->message = $e->getMessage();
99 }
100
101 return rest_ensure_response( $response );
102 }
103
104 /**
105 * Load user_progress in single course.
106 *
107 * @param WP_REST_Request $request
108 *
109 * @return LP_REST_Response
110 * @author Nhamdv <[email protected]>
111 * @editor tungnx
112 * @version 1.0.1
113 * @sicne 4.0.0
114 */
115 public function user_progress( WP_REST_Request $request ): LP_REST_Response {
116 $params = $request->get_params();
117 $course_id = intval( $params['courseId'] ?? 0 );
118 $user_id = intval( $params['userId'] ?? 0 );
119 $response = new LP_REST_Response();
120 $response->data = '';
121
122 try {
123 if ( ! $course_id || ! $user_id ) {
124 throw new Exception( __( 'Params is invalid!', 'learnpress' ) );
125 }
126
127 $course = learn_press_get_course( $course_id );
128 if ( ! $course ) {
129 throw new Exception( __( 'The course is invalid!', 'learnpress' ) );
130 }
131
132 $user = learn_press_get_user( $user_id );
133 if ( ! $user || $user->is_guest() ) {
134 throw new Exception( __( 'You are a Guest', 'learnpress' ) );
135 }
136
137 if ( ! $user->can_create_course() && get_current_user_id() !== $user_id ) {
138 throw new Exception( __( 'You are a not permission!', 'learnpress' ) );
139 }
140
141 $course_data = $user->get_course_data( $course->get_id() );
142 if ( ! $course_data ) {
143 throw new Exception( __( 'You are a not enroll course', 'learnpress' ) );
144 }
145
146 $course_results = $course_data->calculate_course_results();
147
148 $response->status = 'success';
149 $response->data = learn_press_get_template_content(
150 'single-course/sidebar/user-progress',
151 compact( 'user', 'course', 'course_data', 'course_results' )
152 );
153 } catch ( Throwable $e ) {
154 $response->message = $e->getMessage();
155 }
156
157 return $response;
158 }
159
160 /**
161 * Load sections of single course
162 *
163 * @param WP_REST_Request $request
164 *
165 * @return LP_REST_Response
166 * @author nhamdv
167 * @since 4.1.5
168 * @version 1.0.2
169 * @depreacted 4.2.8.7.1
170 * use new curriculum from LP v4.2.8, so now deprecated this function
171 */
172 public function course_curriculum( WP_REST_Request $request ): LP_REST_Response {
173 _deprecated_function( __METHOD__, '4.2.8.7.1' );
174 $response = new LP_REST_Response();
175 return $response;
176 $params = $request->get_params();
177 $total_rows = 0;
178 $course_id = absint( $params['courseId'] ?? 0 );
179 $per_page = LP_Settings::get_option( 'section_per_page', - 1 );
180 $page = absint( $params['page'] ?? 1 );
181 $section_id = wp_unslash( $params['sectionID'] ?? false );
182 $item_viewing = absint( $params['idItemViewing'] ?? 0 );
183
184 try {
185 ob_start();
186 if ( empty( $course_id ) ) {
187 throw new Exception( esc_html__( 'The course is invalid!', 'learnpress' ) );
188 }
189
190 $course = learn_press_get_course( $course_id );
191 if ( ! $course ) {
192 throw new Exception( esc_html__( 'The course is invalid!', 'learnpress' ) );
193 }
194
195 $filters = new LP_Section_Filter();
196 $filters->section_course_id = $course_id;
197 $filters->limit = $per_page;
198 $filters->page = $page;
199 $sections_result = LP_Section_DB::getInstance()->get_sections( $filters, $total_rows );
200 $sections_tmp = [];
201 foreach ( $sections_result as $section ) {
202 $sections_tmp[] = (array) $section;
203 }
204
205 $total_page = 1;
206 if ( $filters->limit > 0 ) {
207 $total_page = LP_Database::get_total_pages( $filters->limit, $total_rows );
208 }
209 $sections = array(
210 'results' => $sections_tmp,
211 'total' => $total_rows,
212 'pages' => $total_page,
213 );
214
215 if ( ! empty( $params['loadMore'] ) ) {
216 foreach ( $sections_tmp as $section ) {
217 Template::instance()->get_frontend_template(
218 'loop/single-course/loop-section.php',
219 compact( 'sections', 'section', 'course_id', 'filters' )
220 );
221 }
222 } else {
223 Template::instance()->get_frontend_template(
224 'single-course/tabs/curriculum-v2.php',
225 compact( 'sections', 'course_id', 'filters' )
226 );
227 }
228
229 if ( $section_id ) {
230 $response->data->section_ids = LP_Database::get_values_by_key( $sections_result, 'section_id' );
231 }
232
233 $response->status = 'success';
234 $response->data->pages = $total_page;
235 $response->data->page = $filters->page;
236 $response->data->content = ob_get_clean();
237 } catch ( Throwable $e ) {
238 ob_end_clean();
239 error_log( __METHOD__ . ': ' . $e->getMessage() );
240 $response->message = $e->getMessage();
241 }
242
243 return $response;
244 }
245
246 /**
247 * Load items' section on the single course
248 *
249 * @param WP_REST_Request $request
250 *
251 * @return WP_REST_Response|WP_Error
252 * @since 4.1.5
253 * @version 1.0.2
254 * @author nhamdv
255 */
256 public function course_curriculum_items( WP_REST_Request $request ) {
257 $params = $request->get_params();
258
259 $section_id = absint( $params['sectionId'] ?? 0 );
260 $course_id = absint( $params['courseId'] ?? 0 );
261 $per_page = LP_Settings::get_option( 'course_item_per_page', - 1 );
262 $page = absint( $params['page'] ?? 1 );
263
264 $response = new LP_REST_Response();
265 $response->data->content = '';
266
267 try {
268 ob_start();
269 if ( empty( $section_id ) ) {
270 throw new Exception( esc_html__( 'The section is invalid!', 'learnpress' ) );
271 }
272
273 $filters = new LP_Section_Items_Filter();
274 $filters->only_fields = [ 'ID', 'post_title' ];
275 $filters->section_id = $section_id;
276 $filters->limit = $per_page;
277 $filters->page = $page;
278 $total_rows = 0;
279 $section_items_result = LP_Section_DB::getInstance()->get_items( $filters, $total_rows );
280 $total_pages = 1;
281 if ( $filters->limit > 0 ) {
282 $total_pages = LP_Database::get_total_pages( $filters->limit, $total_rows );
283 }
284
285 foreach ( $section_items_result as $key => $section_item ) {
286 $section_item = (array) $section_item;
287 if ( ! $course_id ) {
288 $course_id = LP_Section_DB::getInstance()->get_course_id_by_section( $section_id );
289 }
290 if ( ! $course_id ) {
291 throw new Exception( esc_html__( 'Item not assign to course', 'learnpress' ) );
292 }
293
294 $course = learn_press_get_course( $course_id );
295 if ( ! $course ) {
296 throw new Exception( 'Course is not exists!' );
297 }
298
299 $course_item = $course->get_item( absint( $section_item['ID'] ?? 0 ) );
300 $can_view_item = new LP_Model_User_Can_View_Course_Item();
301 $user = learn_press_get_user( get_current_user_id() );
302 if ( $user ) {
303 $can_view_content_course = $user->can_view_content_course( absint( $course_id ) );
304 $can_view_item = $user->can_view_item( $section_item['ID'], $can_view_content_course );
305 }
306
307 // Ordinal numbers
308 $key = absint( ( ( $page - 1 ) * $per_page ) + $key + 1 );
309
310 Template::instance()->get_frontend_template(
311 'loop/single-course/loop-section-item.php',
312 compact( 'section_item', 'course_item', 'can_view_item', 'course_id', 'user', 'key' )
313 );
314 }
315
316 $response->data->pages = $total_pages;
317 $response->data->page = $filters->page;
318 $response->data->content = ob_get_clean();
319 $response->data->item_ids = LP_Database::get_values_by_key( $section_items_result );
320 $response->status = 'success';
321 } catch ( Throwable $e ) {
322 ob_end_clean();
323 error_log( __METHOD__ . ': ' . $e->getMessage() );
324 $response->message = $e->getMessage();
325 }
326
327 return rest_ensure_response( $response );
328 }
329 }
330