PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.2
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.2
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.4.2, at inc/rest-api/v1/frontend/class-lp-rest-lazy-load-controller.php

335 lines 9.9 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@email.com>
111 * @editor tungnx
112 * @version 1.0.1
113 * @sicne 4.0.0
114 * @deprecated 4.4.0
115 */
116 public function user_progress( WP_REST_Request $request ): LP_REST_Response {
117 _deprecated_function( __METHOD__, '4.4.0' );
118 $response = new LP_REST_Response();
119 return $response;
120 $params = $request->get_params();
121 $course_id = intval( $params['courseId'] ?? 0 );
122 $user_id = intval( $params['userId'] ?? 0 );
123 $response->data = '';
124
125 try {
126 if ( ! $course_id || ! $user_id ) {
127 throw new Exception( __( 'Params is invalid!', 'learnpress' ) );
128 }
129
130 $course = learn_press_get_course( $course_id );
131 if ( ! $course ) {
132 throw new Exception( __( 'The course is invalid!', 'learnpress' ) );
133 }
134
135 $user = learn_press_get_user( $user_id );
136 if ( ! $user || $user->is_guest() ) {
137 throw new Exception( __( 'You are a Guest', 'learnpress' ) );
138 }
139
140 if ( ! $user->can_create_course() && get_current_user_id() !== $user_id ) {
141 throw new Exception( __( 'You are a not permission!', 'learnpress' ) );
142 }
143
144 $course_data = $user->get_course_data( $course->get_id() );
145 if ( ! $course_data ) {
146 throw new Exception( __( 'You are a not enroll course', 'learnpress' ) );
147 }
148
149 $course_results = $course_data->calculate_course_results();
150
151 $response->status = 'success';
152 $response->data = learn_press_get_template_content(
153 'single-course/sidebar/user-progress',
154 compact( 'user', 'course', 'course_data', 'course_results' )
155 );
156 } catch ( Throwable $e ) {
157 $response->message = $e->getMessage();
158 }
159
160 return $response;
161 }
162
163 /**
164 * Load sections of single course
165 *
166 * @param WP_REST_Request $request
167 *
168 * @return LP_REST_Response
169 * @author nhamdv
170 * @since 4.1.5
171 * @version 1.0.2
172 * @depreacted 4.2.8.7.1
173 * use new curriculum from LP v4.2.8, so now deprecated this function
174 */
175 public function course_curriculum( WP_REST_Request $request ): LP_REST_Response {
176 _deprecated_function( __METHOD__, '4.2.8.7.1' );
177 $response = new LP_REST_Response();
178 return $response;
179 $params = $request->get_params();
180 $total_rows = 0;
181 $course_id = absint( $params['courseId'] ?? 0 );
182 $per_page = LP_Settings::get_option( 'section_per_page', - 1 );
183 $page = absint( $params['page'] ?? 1 );
184 $section_id = wp_unslash( $params['sectionID'] ?? false );
185 $item_viewing = absint( $params['idItemViewing'] ?? 0 );
186
187 try {
188 ob_start();
189 if ( empty( $course_id ) ) {
190 throw new Exception( esc_html__( 'The course is invalid!', 'learnpress' ) );
191 }
192
193 $course = learn_press_get_course( $course_id );
194 if ( ! $course ) {
195 throw new Exception( esc_html__( 'The course is invalid!', 'learnpress' ) );
196 }
197
198 $filters = new LP_Section_Filter();
199 $filters->section_course_id = $course_id;
200 $filters->limit = $per_page;
201 $filters->page = $page;
202 $sections_result = LP_Section_DB::getInstance()->get_sections( $filters, $total_rows );
203 $sections_tmp = [];
204 foreach ( $sections_result as $section ) {
205 $sections_tmp[] = (array) $section;
206 }
207
208 $total_page = 1;
209 if ( $filters->limit > 0 ) {
210 $total_page = LP_Database::get_total_pages( $filters->limit, $total_rows );
211 }
212 $sections = array(
213 'results' => $sections_tmp,
214 'total' => $total_rows,
215 'pages' => $total_page,
216 );
217
218 if ( ! empty( $params['loadMore'] ) ) {
219 foreach ( $sections_tmp as $section ) {
220 Template::instance()->get_frontend_template(
221 'loop/single-course/loop-section.php',
222 compact( 'sections', 'section', 'course_id', 'filters' )
223 );
224 }
225 } else {
226 Template::instance()->get_frontend_template(
227 'single-course/tabs/curriculum-v2.php',
228 compact( 'sections', 'course_id', 'filters' )
229 );
230 }
231
232 if ( $section_id ) {
233 $response->data->section_ids = LP_Database::get_values_by_key( $sections_result, 'section_id' );
234 }
235
236 $response->status = 'success';
237 $response->data->pages = $total_page;
238 $response->data->page = $filters->page;
239 $response->data->content = ob_get_clean();
240 } catch ( Throwable $e ) {
241 ob_end_clean();
242 error_log( __METHOD__ . ': ' . $e->getMessage() );
243 $response->message = $e->getMessage();
244 }
245
246 return $response;
247 }
248
249 /**
250 * Load items' section on the single course
251 *
252 * @param WP_REST_Request $request
253 *
254 * @return WP_REST_Response|WP_Error
255 * @since 4.1.5
256 * @version 1.0.2
257 * @author nhamdv
258 * @deprecated 4.4.0
259 */
260 public function course_curriculum_items( WP_REST_Request $request ) {
261 _deprecated_function( __METHOD__, '4.4.0' );
262 $response = new LP_REST_Response();
263 return $response;
264 $params = $request->get_params();
265
266 $section_id = absint( $params['sectionId'] ?? 0 );
267 $course_id = absint( $params['courseId'] ?? 0 );
268 $per_page = LP_Settings::get_option( 'course_item_per_page', - 1 );
269 $page = absint( $params['page'] ?? 1 );
270 $response->data->content = '';
271
272 try {
273 ob_start();
274 if ( empty( $section_id ) ) {
275 throw new Exception( esc_html__( 'The section is invalid!', 'learnpress' ) );
276 }
277
278 $filters = new LP_Section_Items_Filter();
279 $filters->only_fields = [ 'ID', 'post_title' ];
280 $filters->section_id = $section_id;
281 $filters->limit = $per_page;
282 $filters->page = $page;
283 $total_rows = 0;
284 $section_items_result = LP_Section_DB::getInstance()->get_items( $filters, $total_rows );
285 $total_pages = 1;
286 if ( $filters->limit > 0 ) {
287 $total_pages = LP_Database::get_total_pages( $filters->limit, $total_rows );
288 }
289
290 foreach ( $section_items_result as $key => $section_item ) {
291 $section_item = (array) $section_item;
292 if ( ! $course_id ) {
293 $course_id = LP_Section_DB::getInstance()->get_course_id_by_section( $section_id );
294 }
295 if ( ! $course_id ) {
296 throw new Exception( esc_html__( 'Item not assign to course', 'learnpress' ) );
297 }
298
299 $course = learn_press_get_course( $course_id );
300 if ( ! $course ) {
301 throw new Exception( 'Course is not exists!' );
302 }
303
304 $course_item = $course->get_item( absint( $section_item['ID'] ?? 0 ) );
305 $can_view_item = new LP_Model_User_Can_View_Course_Item();
306 $user = learn_press_get_user( get_current_user_id() );
307 if ( $user ) {
308 $can_view_content_course = $user->can_view_content_course( absint( $course_id ) );
309 $can_view_item = $user->can_view_item( $section_item['ID'], $can_view_content_course );
310 }
311
312 // Ordinal numbers
313 $key = absint( ( ( $page - 1 ) * $per_page ) + $key + 1 );
314
315 Template::instance()->get_frontend_template(
316 'loop/single-course/loop-section-item.php',
317 compact( 'section_item', 'course_item', 'can_view_item', 'course_id', 'user', 'key' )
318 );
319 }
320
321 $response->data->pages = $total_pages;
322 $response->data->page = $filters->page;
323 $response->data->content = ob_get_clean();
324 $response->data->item_ids = LP_Database::get_values_by_key( $section_items_result );
325 $response->status = 'success';
326 } catch ( Throwable $e ) {
327 ob_end_clean();
328 error_log( __METHOD__ . ': ' . $e->getMessage() );
329 $response->message = $e->getMessage();
330 }
331
332 return rest_ensure_response( $response );
333 }
334 }
335