PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7
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 4.2.1 All 138 releases
learnpress / inc / user / lp-user-functions.php

lp-user-functions.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.1.7, at inc/user/lp-user-functions.php

2,213 lines 56.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Common functions to process actions about user
4 *
5 * @author ThimPress
6 * @package LearnPress/Functions/User
7 * @version 1.0
8 */
9
10 function learn_press_get_user_profile_tabs() {
11 return LP_Profile::instance()->get_tabs();
12 }
13
14 /**
15 * Delete user data by user ID
16 *
17 * @param int $user_id
18 * @param int $course_id
19 */
20 function learn_press_delete_user_data( $user_id, $course_id = 0 ) {
21 global $wpdb;
22 // TODO: Should be deleted user's order and order data???
23
24 $query_args = array( $user_id );
25
26 if ( $course_id ) {
27 $query_args[] = $course_id;
28 }
29
30 $query = $wpdb->prepare(
31 "
32 SELECT user_item_id
33 FROM {$wpdb->prefix}learnpress_user_items
34 WHERE user_id = %d
35 " . ( $course_id ? ' AND item_id = %d' : '' ) . '
36 ',
37 $query_args
38 );
39
40 // delete all courses user has enrolled
41 $query = $wpdb->prepare(
42 "
43 DELETE FROM {$wpdb->prefix}learnpress_user_items
44 WHERE user_id = %d
45 " . ( $course_id ? ' AND item_id = %d' : '' ) . '
46 ',
47 $query_args
48 );
49
50 @$wpdb->query( $query );
51 }
52
53 /**
54 * Get user_item_id field in table learnpress_user_items
55 * with the user_id, item_id. If $course_id is not passed
56 * then item_id is ID of a course. Otherwise, item_id is
57 * ID of an item (like quiz/lesson).
58 *
59 * @param int $user_id
60 * @param int $item_id
61 * @param int $course_id
62 *
63 * @return bool
64 * @editor tungnx
65 * @reason this function only get cache, not handle get user_item_id
66 * @depecated 4.1.6.9
67 */
68 function learn_press_get_user_item_id( $user_id, $item_id, $course_id = 0 /* added 3.0.0 */ ) {
69 return false;
70 }
71
72 /**
73 * Get current user ID
74 *
75 * @return int
76 */
77 function learn_press_get_current_user_id() {
78 $user = learn_press_get_current_user();
79
80 return $user->get_id();
81 }
82
83 /**
84 * Get the user by $user_id passed. If $user_id is NULL, get current user.
85 * If current user is not logged in, return a GUEST user
86 *
87 * @param bool $create_temp - Optional. Create temp user if user is not logged in.
88 *
89 * @return bool|LP_User|LP_User_Guest
90 * @editor tungnx
91 * @modify 4.1.4
92 * @version 1.0.1
93 */
94 function learn_press_get_current_user( $create_temp = true ) {
95 $user_id = get_current_user_id();
96
97 if ( $user_id ) {
98 return learn_press_get_user( $user_id );
99 }
100
101 // Return LP_User_Guest
102 return learn_press_get_user( 0 );
103 }
104
105 if ( ! function_exists( 'learn_press_get_user' ) ) {
106 /**
107 * Get user by ID. Return false if the user does not exists.
108 *
109 * @param int $user_id
110 * @param bool $current
111 *
112 * @return LP_User|LP_User_Guest|mixed
113 * Todo: check this function - tungnx
114 */
115 function learn_press_get_user( $user_id, $current = false, $force_new = false ) {
116 $is_guest = false;
117 if ( ! is_null( LP()->session ) && $user_id != LP()->session->guest_user_id ) {
118 if ( $current && ! get_user_by( 'id', $user_id ) ) {
119 $user_id = get_current_user_id();
120 }
121 }
122
123 if ( ! $user_id && isset( LP()->session ) ) {
124 if ( ! LP()->session->guest_user_id ) {
125 LP()->session->set_customer_session_cookie( 1 );
126 LP()->session->guest_user_id = time();
127 }
128
129 $user_id = LP()->session->guest_user_id;
130 $is_guest = true;
131 }
132
133 if ( ! $user_id ) {
134 return false;
135 }
136
137 $user_id = '' . $user_id;
138
139 if ( $force_new || ! array_key_exists( $user_id, LP_Global::$users ) ) {
140 /**
141 * LP Hook.
142 *
143 * Filter the default class name to get LP user.
144 *
145 * @since 3.3.0
146 */
147 $userClass = apply_filters( 'learn-press/user-class', $is_guest ? 'LP_User_Guest' : 'LP_User', $is_guest );
148
149 LP_Global::$users[ $user_id ] = new $userClass( $user_id );
150
151 do_action( 'learn-press/get-user', LP_Global::$users[ $user_id ], $user_id );
152 }
153
154 return LP_Global::$users[ $user_id ];
155 }
156 }
157
158 /**
159 * Add more 2 user roles teacher and student
160 */
161 function learn_press_add_user_roles() {
162
163 $settings = LP_Settings::instance();
164
165 /* translators: user role */
166 _x( 'LP Instructor', 'User role' );
167
168 add_role(
169 LP_TEACHER_ROLE,
170 'LP Instructor',
171 array()
172 );
173
174 $course_cap = LP_COURSE_CPT . 's';
175 $lesson_cap = LP_LESSON_CPT . 's';
176 $order_cap = LP_ORDER_CPT . 's';
177
178 $teacher = get_role( LP_TEACHER_ROLE );
179 if ( $teacher ) {
180 $teacher->add_cap( 'read_private_' . $course_cap );
181 $teacher->add_cap( 'delete_published_' . $course_cap );
182 $teacher->add_cap( 'edit_published_' . $course_cap );
183 $teacher->add_cap( 'edit_' . $course_cap );
184 $teacher->add_cap( 'delete_' . $course_cap );
185 $teacher->add_cap( 'unfiltered_html' );
186
187 $settings->get( 'required_review' );
188
189 if ( $settings->get( 'required_review' ) == 'yes' ) {
190 $teacher->remove_cap( 'publish_' . $course_cap );
191 } else {
192 $teacher->add_cap( 'publish_' . $course_cap );
193 }
194
195 $teacher->add_cap( 'read_private_' . $lesson_cap );
196 $teacher->add_cap( 'delete_published_' . $lesson_cap );
197 $teacher->add_cap( 'edit_published_' . $lesson_cap );
198 $teacher->add_cap( 'edit_' . $lesson_cap );
199 $teacher->add_cap( 'delete_' . $lesson_cap );
200 $teacher->add_cap( 'publish_' . $lesson_cap );
201 $teacher->add_cap( 'upload_files' );
202 $teacher->add_cap( 'read' );
203 $teacher->add_cap( 'edit_posts' );
204 }
205
206 // administrator
207 $admin = get_role( 'administrator' );
208 if ( $admin ) {
209 $admin->add_cap( 'read_private_' . $course_cap );
210 $admin->add_cap( 'delete_' . $course_cap );
211 $admin->add_cap( 'delete_published_' . $course_cap );
212 $admin->add_cap( 'edit_' . $course_cap );
213 $admin->add_cap( 'edit_published_' . $course_cap );
214 $admin->add_cap( 'publish_' . $course_cap );
215 $admin->add_cap( 'delete_private_' . $course_cap );
216 $admin->add_cap( 'edit_private_' . $course_cap );
217 $admin->add_cap( 'delete_others_' . $course_cap );
218 $admin->add_cap( 'edit_others_' . $course_cap );
219
220 $admin->add_cap( 'read_private_' . $lesson_cap );
221 $admin->add_cap( 'delete_' . $lesson_cap );
222 $admin->add_cap( 'delete_published_' . $lesson_cap );
223 $admin->add_cap( 'edit_' . $lesson_cap );
224 $admin->add_cap( 'edit_published_' . $lesson_cap );
225 $admin->add_cap( 'publish_' . $lesson_cap );
226 $admin->add_cap( 'delete_private_' . $lesson_cap );
227 $admin->add_cap( 'edit_private_' . $lesson_cap );
228 $admin->add_cap( 'delete_others_' . $lesson_cap );
229 $admin->add_cap( 'edit_others_' . $lesson_cap );
230
231 $admin->add_cap( 'delete_' . $order_cap );
232 $admin->add_cap( 'delete_published_' . $order_cap );
233 $admin->add_cap( 'edit_' . $order_cap );
234 $admin->add_cap( 'edit_published_' . $order_cap );
235 $admin->add_cap( 'publish_' . $order_cap );
236 $admin->add_cap( 'delete_private_' . $order_cap );
237 $admin->add_cap( 'edit_private_' . $order_cap );
238 $admin->add_cap( 'delete_others_' . $order_cap );
239 $admin->add_cap( 'edit_others_' . $order_cap );
240 }
241 }
242
243 add_action( 'init', 'learn_press_add_user_roles' );
244
245 /**
246 * @param null $user_id
247 * @param array $args
248 *
249 * @return mixed
250 */
251 function learn_press_get_user_questions( $user_id = null, $args = array() ) {
252 if ( ! $user_id ) {
253 $user_id = get_current_user_id();
254 }
255
256 return learn_press_get_user( $user_id )->get_questions( $args );
257 }
258
259 /**
260 * Get the type of current user
261 *
262 * @param null $check_type
263 *
264 * @return bool|string
265 */
266 function learn_press_current_user_is( $check_type = null ) {
267 global $current_user;
268 $user_roles = $current_user->roles;
269 $user_type = '';
270
271 if ( in_array( 'lpr_teacher', $user_roles ) ) {
272 $user_type = 'instructor';
273 } elseif ( in_array( 'lp_teacher', $user_roles ) ) {
274 $user_type = 'instructor';
275 } elseif ( in_array( 'administrator', $user_roles ) ) {
276 $user_type = 'administrator';
277 }
278
279 return $check_type ? $check_type == $user_type : $user_type;
280 }
281
282 function learn_press_user_has_roles( $roles, $user_id = null ) {
283 $has_role = false;
284 if ( ! $user_id ) {
285 $user = wp_get_current_user();
286 } else {
287 $user = get_user_by( 'id', $user_id );
288 }
289 $available_roles = (array) $user->roles;
290 if ( is_array( $roles ) ) {
291 foreach ( $roles as $role ) {
292 if ( in_array( $role, $available_roles ) ) {
293 $has_role = true;
294 break; // only need one of roles is in available
295 }
296 }
297 } else {
298 if ( in_array( $roles, $available_roles ) ) {
299 $has_role = true;
300 }
301 }
302
303 return $has_role;
304 }
305
306 function learn_press_current_user_can_view_profile_section( $section, $user ) {
307 $current_user = wp_get_current_user();
308 $view = true;
309 if ( $user->get_data( 'user_login' ) != $current_user->user_login && $section == LP_Settings::instance()->get(
310 'profile_endpoints.profile-orders',
311 'profile-orders'
312 ) ) {
313 $view = false;
314 }
315
316 return apply_filters( 'learn_press_current_user_can_view_profile_section', $view, $section, $user );
317 }
318
319 /*function learn_press_profile_tab_courses_content( $current, $tab, $user ) {
320 learn_press_get_template(
321 'profile/tabs/courses.php',
322 array(
323 'user' => $user,
324 'current' => $current,
325 'tab' => $tab,
326 )
327 );
328 }*/
329
330 function learn_press_profile_tab_quizzes_content( $current, $tab, $user ) {
331 learn_press_get_template(
332 'profile/tabs/quizzes.php',
333 array(
334 'user' => $user,
335 'current' => $current,
336 'tab' => $tab,
337 )
338 );
339 }
340
341 function learn_press_profile_tab_orders_content( $current, $tab, $user ) {
342 learn_press_get_template(
343 'profile/tabs/orders.php',
344 array(
345 'user' => $user,
346 'current' => $current,
347 'tab' => $tab,
348 )
349 );
350 }
351
352 /**
353 * Get queried user in profile link
354 *
355 * @return false|WP_User
356 * @since 3.0.0
357 */
358 function learn_press_get_profile_user() {
359 return LP_Profile::get_queried_user();
360 }
361
362
363 /**
364 * Add instructor registration button to register page and admin bar
365 */
366 function learn_press_user_become_teacher_registration_form() {
367 if ( LP_Settings::instance()->get( 'instructor_registration' ) != 'yes' ) {
368 return;
369 }
370 ?>
371 <p>
372 <label for="become_teacher">
373 <input type="checkbox" name="become_teacher" id="become_teacher">
374 <?php esc_html_e( 'Want to become an instructor?', 'learnpress' ); ?>
375 </label>
376 </p>
377 <?php
378 }
379
380 add_action( 'register_form', 'learn_press_user_become_teacher_registration_form' );
381
382 /**
383 * Update data into table learnpress_user_items.
384 *
385 * @param array $fields - Fields and values to be updated.
386 * Format: array(
387 * field_name_1 => value 1,
388 * field_name_2 => value 2,
389 * ....
390 * field_name_n => value n
391 * )
392 * @param mixed $where - Optional. Fields with values for conditional update with the same format of $fields.
393 * @param bool $update_cache - Optional. Should be update to cache or not (since 3.0.0).
394 * @param bool $update_extra_fields_as_meta - Optional. Update extra fields as item meta (since 3.1.0).
395 *
396 * @return mixed
397 */
398 function learn_press_update_user_item_field( $fields, $where = false, $update_cache = true, $update_extra_fields_as_meta = false ) {
399 global $wpdb;
400
401 // Table fields
402 $table_fields = array(
403 'user_id' => '%d',
404 'item_id' => '%d',
405 'ref_id' => '%d',
406 'start_time' => '%s',
407 'end_time' => '%s',
408 'access_level' => '%d',
409 'graduation' => '%s',
410 'item_type' => '%s',
411 'status' => '%s',
412 'ref_type' => '%s',
413 'parent_id' => '%d',
414 );
415
416 /**
417 * Validate item status
418 */
419 if ( ! empty( $fields['item_id'] ) && ! empty( $fields['status'] ) ) {
420 $item_type = learn_press_get_post_type( $fields['item_id'] );
421
422 if ( LP_COURSE_CPT === $item_type ) {
423 if ( 'completed' === $fields['status'] ) {
424 $fields['status'] = 'finished';
425 }
426 } else {
427 if ( 'finished' === $fields['status'] ) {
428 $fields['status'] = 'completed';
429 }
430 }
431 }
432
433 $data = array();
434 $data_format = array();
435 $date_time_fields = array(
436 'start_time',
437 'end_time',
438 );
439
440 foreach ( $fields as $field => $value ) {
441 if ( ! empty( $table_fields[ $field ] ) ) {
442 $data[ $field ] = $value;
443
444 // Do not format the date-time field if it's value is NULL
445 if ( in_array( $field, $date_time_fields ) && ! $value ) {
446 $data[ $field ] = null;
447 $data_format[] = '';
448 } else {
449 $data_format[] = $table_fields[ $field ];
450 }
451 }
452 }
453
454 if ( ! empty( $fields['user_item_id'] ) ) {
455 $where = wp_parse_args(
456 $where,
457 array( 'user_item_id' => $fields['user_item_id'] )
458 );
459 }
460
461 if ( $where && empty( $where['user_id'] ) ) {
462 $where['user_id'] = ! empty( $fields['user_id'] ) ? $fields['user_id'] : learn_press_get_current_user_id();
463 }
464
465 $where_format = array();
466
467 // Build where and where format
468 if ( $where ) {
469 foreach ( $where as $field => $value ) {
470 if ( ! empty( $table_fields[ $field ] ) ) {
471 $where_format[] = $table_fields[ $field ];
472 }
473 }
474 }
475
476 if ( ! $data ) {
477 return false;
478 }
479
480 $inserted = false;
481 $updated = false;
482
483 // Ensure all fields are instance of LP_Datetime have to
484 // convert to string of datetime.
485 foreach ( $data as $k => $v ) {
486 if ( $v instanceof LP_Datetime ) {
487 $data[ $k ] = $v->toSql();
488 }
489 }
490
491 // If $where is not empty consider we are updating
492 if ( $where ) {
493 $updated = $wpdb->update(
494 $wpdb->learnpress_user_items,
495 $data,
496 $where,
497 $data_format,
498 $where_format
499 );
500 } else {
501
502 // Otherwise, insert a new one
503 if ( $wpdb->insert(
504 $wpdb->learnpress_user_items,
505 $data,
506 $data_format
507 )
508 ) {
509 $inserted = $wpdb->insert_id;
510 }
511 }
512
513 if ( $updated && ! empty( $where['user_item_id'] ) ) {
514 $inserted = $where['user_item_id'];
515 }
516
517 /**
518 * @var object|bool $updated_item
519 */
520 $updated_item = false;
521
522 // Get the item we just have updated or inserted.
523 if ( $inserted ) {
524 $updated_item = learn_press_get_user_item( $inserted );
525 } elseif ( $updated ) {
526 $updated_item = learn_press_get_user_item( $where );
527 }
528
529 /**
530 * If there is some fields does not contain in the main table
531 * then consider update them as meta data.
532 */
533 if ( $updated_item && $update_extra_fields_as_meta ) {
534 $extra_fields = array_diff_key( $fields, $table_fields );
535 if ( $extra_fields ) {
536 foreach ( $extra_fields as $meta_key => $meta_value ) {
537 if ( $meta_value == 'user_item_id' ) {
538 continue;
539 }
540
541 if ( $meta_value === false ) {
542 learn_press_delete_user_item_meta( $updated_item->user_item_id, $meta_key );
543 } else {
544
545 if ( empty( $meta_value ) ) {
546 $meta_value = '';
547 }
548 learn_press_update_user_item_meta( $updated_item->user_item_id, $meta_key, $meta_value );
549 }
550 }
551 }
552 }
553
554 do_action( 'learn-press/updated-user-item-meta', $updated_item );
555
556 return $updated_item;
557 }
558
559 /**
560 * Get user item row(s) from user items table by multiple WHERE conditional
561 *
562 * @param array|int $where
563 * @param bool $single
564 *
565 * @return array
566 */
567 function learn_press_get_user_item( $where, $single = true ) {
568 global $wpdb;
569
570 // Table fields
571 $table_fields = array(
572 'user_item_id' => '%d',
573 'user_id' => '%d',
574 'item_id' => '%d',
575 'ref_id' => '%d',
576 'start_time' => '%s',
577 'end_time' => '%s',
578 'item_type' => '%s',
579 'status' => '%s',
580 'ref_type' => '%s',
581 'parent_id' => '%d',
582 );
583
584 // If $where is a number consider we are searching the record with unique user_item_id
585 if ( is_numeric( $where ) ) {
586 $where = array( 'user_item_id' => $where );
587 }
588
589 $where_str = array();
590 foreach ( $where as $field => $value ) {
591 if ( ! empty( $table_fields[ $field ] ) ) {
592 $where_str[] = "{$field} = " . $table_fields[ $field ];
593 }
594 }
595 $item = false;
596
597 if ( $where_str ) {
598 $query = $wpdb->prepare(
599 "
600 SELECT *
601 FROM {$wpdb->prefix}learnpress_user_items
602 WHERE " . join( ' AND ', $where_str ) . '
603 ORDER BY user_item_id DESC
604 ',
605 $where
606 );
607 if ( $single || ! empty( $where['user_item_id'] ) ) {
608 $item = $wpdb->get_row( $query );
609 } else {
610 $item = $wpdb->get_results( $query );
611 }
612 }
613
614 return $item;
615 }
616
617 /**
618 * Get user item meta from user_itemmeta table
619 *
620 * @param int $user_item_id .
621 * @param string $meta_key .
622 * @param bool $single .
623 *
624 * @return mixed
625 */
626 function learn_press_get_user_item_meta( $user_item_id = 0, $meta_key = '', $single = true ) {
627 $meta = false;
628 if ( metadata_exists( 'learnpress_user_item', $user_item_id, $meta_key ) ) {
629 $meta = get_metadata( 'learnpress_user_item', $user_item_id, $meta_key, $single );
630 }
631
632 return $meta;
633 }
634
635 /**
636 * Add user item meta into table user_itemmeta
637 *
638 * @param int $user_item_id
639 * @param string $meta_key
640 * @param mixed $meta_value
641 * @param string $prev_value
642 *
643 * @return false|int
644 */
645 function learn_press_add_user_item_meta( $user_item_id, $meta_key, $meta_value, $prev_value = '' ) {
646 return add_metadata( 'learnpress_user_item', $user_item_id, $meta_key, $meta_value, $prev_value );
647 }
648
649 /**
650 * Update user item meta to table user_itemmeta
651 *
652 * @param int $user_item_id
653 * @param string $meta_key
654 * @param mixed $meta_value
655 * @param string $prev_value
656 *
657 * @return bool|int
658 */
659 function learn_press_update_user_item_meta( $user_item_id, $meta_key, $meta_value, $prev_value = '' ) {
660 return update_metadata( 'learnpress_user_item', $user_item_id, $meta_key, $meta_value, $prev_value );
661 }
662
663
664 /**
665 * Update user item meta to table user_itemmeta
666 *
667 * @param int $object_id
668 * @param string $meta_key
669 * @param mixed $meta_value
670 * @param bool $delete_all
671 *
672 * @return bool|int
673 */
674 function learn_press_delete_user_item_meta( $object_id, $meta_key, $meta_value = '', $delete_all = false ) {
675 return delete_metadata( 'learnpress_user_item', $object_id, $meta_key, $meta_value, $delete_all );
676 }
677
678 /**
679 * Get temp users.
680 *
681 * @return array
682 * @depecated 4.1.6.9
683 */
684 /*function learn_press_get_temp_users() {
685 return false;
686 if ( false === ( $temp_users = LP_Object_Cache::get( 'learn-press/temp-users' ) ) ) {
687 global $wpdb;
688 $query = $wpdb->prepare(
689 "
690 SELECT ID
691 FROM {$wpdb->users} u
692 INNER JOIN {$wpdb->usermeta} um ON u.ID = um.user_id AND um.meta_key = %s AND um.meta_value = %s
693 LEFT JOIN {$wpdb->usermeta} um2 ON u.ID = um2.user_id AND um2.meta_key = %s
694 ",
695 '_lp_temp_user',
696 'yes',
697 '_lp_expiration'
698 );
699
700 $temp_users = $wpdb->get_col( $query );
701
702 LP_Object_Cache::set( 'learn-press/temp-users', $temp_users );
703 }
704
705 return $temp_users;
706 }*/
707
708 /**
709 * Update field created_time after added user item meta
710 *
711 * @use updated_{meta_type}_meta hook
712 *
713 * @param $meta_id
714 * @param $object_id
715 * @param $meta_key
716 * @param $_meta_value
717 */
718 function _learn_press_update_created_time_user_item_meta( $meta_id, $object_id, $meta_key, $_meta_value ) {
719 global $wpdb;
720 $wpdb->update(
721 $wpdb->learnpress_user_itemmeta,
722 array( 'create_time' => current_time( 'mysql' ) ),
723 array( 'meta_id' => $meta_id ),
724 array( '%s' ),
725 array( '%d' )
726 );
727 }
728
729 // add_action( 'added_learnpress_user_item_meta', '_learn_press_update_created_time_user_item_meta', 10, 4 );
730
731 /**
732 * Update field updated_time after updated user item meta
733 *
734 * @use updated_{meta_type}_meta hook
735 *
736 * @param $meta_id
737 * @param $object_id
738 * @param $meta_key
739 * @param $_meta_value
740 */
741 function _learn_press_update_updated_time_user_item_meta( $meta_id, $object_id, $meta_key, $_meta_value ) {
742 global $wpdb;
743 $wpdb->update(
744 $wpdb->learnpress_user_itemmeta,
745 array( 'update_time' => current_time( 'mysql' ) ),
746 array( 'meta_id' => $meta_id ),
747 array( '%s' ),
748 array( '%d' )
749 );
750 }
751
752 // add_action( 'updated_learnpress_user_item_meta', '_learn_press_update_updated_time_user_item_meta', 10, 4 );
753
754 /**
755 * @param $status
756 * @param int $quiz_id
757 * @param int $user_id
758 * @param int $course_id
759 *
760 * @return bool|mixed
761 */
762 function learn_press_user_has_quiz_status( $status, $quiz_id = 0, $user_id = 0, $course_id = 0 ) {
763 $user = learn_press_get_user( $user_id );
764
765 return $user->has_quiz_status( $status, $quiz_id, $course_id );
766 }
767
768 if ( ! function_exists( 'learn_press_pre_get_avatar_callback' ) ) {
769 /**
770 * Filter the avatar
771 *
772 * @param string $avatar
773 * @param string $id_or_email
774 * @param array $size
775 *
776 * @return string
777 */
778 function learn_press_pre_get_avatar_callback( $avatar, $id_or_email = '', $size = array() ) {
779
780 $profile = LP_Profile::instance();
781
782 if ( ! $profile->is_enable_avatar() ) {
783 return $avatar;
784 }
785
786 if ( ( isset( $size['gravatar'] ) && $size['gravatar'] ) || ( $size['default'] && $size['force_default'] ) ) {
787 return $avatar;
788 }
789
790 $user_id = 0;
791
792 /**
793 * Get the ID of user from $id_or_email
794 */
795 if ( ! is_numeric( $id_or_email ) && is_string( $id_or_email ) ) {
796 $user = get_user_by( 'email', $id_or_email );
797 if ( $user ) {
798 $user_id = $user->ID;
799 }
800 } elseif ( is_numeric( $id_or_email ) ) {
801 $user_id = $id_or_email;
802 } elseif ( is_object( $id_or_email ) && isset( $id_or_email->user_id ) && $id_or_email->user_id ) {
803 $user_id = $id_or_email->user_id;
804 } elseif ( $id_or_email instanceof WP_Comment ) {
805 $user = get_user_by( 'email', $id_or_email->comment_author_email );
806 if ( $user ) {
807 $user_id = $user->ID;
808 }
809 }
810
811 if ( ! $user_id ) {
812 return $avatar;
813 }
814
815 $user = LP_User_Factory::get_user( $user_id );
816
817 $profile_picture_src = $user->get_upload_profile_src();
818 if ( $profile_picture_src ) {
819 $setting_size = learn_press_get_avatar_thumb_size();
820 $img_size = '';
821
822 // Get avatar size
823 if ( ! is_array( $size ) ) {
824 if ( $size === 'thumbnail' ) {
825 $img_size = '';
826 $height = $setting_size['height'];
827 $width = $setting_size['width'];
828 } else {
829 $height = 250;
830 $width = 250;
831 }
832 } else {
833 $img_size = $size['size'];
834 $height = $size['height'];
835 $width = $size['width'];
836 }
837
838 $avatar = '<img alt="' . esc_attr( $user->get_data( 'display_name' ) ) . '" src="' . esc_url_raw( $profile_picture_src ) . '" class="avatar avatar-' . $img_size . ' photo" height="' . $height . '" width="' . $width . '" />';
839 }
840
841 return $avatar;
842 }
843 }
844 add_filter( 'pre_get_avatar', 'learn_press_pre_get_avatar_callback', 1, 5 );
845
846
847 function learn_press_user_profile_picture_upload_dir( $width_user = true ) {
848 static $upload_dir;
849 if ( ! $upload_dir ) {
850 $upload_dir = wp_upload_dir();
851 $subdir = apply_filters( 'learn_press_user_profile_folder', 'learn-press-profile', $width_user );
852 if ( $width_user ) {
853 $subdir .= '/' . get_current_user_id();
854 }
855 $subdir = '/' . $subdir;
856
857 if ( ! empty( $upload_dir['subdir'] ) ) {
858 $u_subdir = str_replace( '\\', '/', $upload_dir['subdir'] );
859 $u_path = str_replace( '\\', '/', $upload_dir['path'] );
860
861 $upload_dir['path'] = str_replace( $u_subdir, $subdir, $u_path );
862 $upload_dir['url'] = str_replace( $u_subdir, $subdir, $upload_dir['url'] );
863 } else {
864 $upload_dir['path'] = $upload_dir['path'] . $subdir;
865 $upload_dir['url'] = $upload_dir['url'] . $subdir;
866 }
867
868 $upload_dir['subdir'] = $subdir;
869
870 // Point path/url to main site if we are in multisite
871 if ( is_multisite() && ! ( is_main_network() && is_main_site() && defined( 'MULTISITE' ) ) ) {
872 foreach ( array( 'path', 'url', 'basedir', 'baseurl' ) as $v ) {
873 $upload_dir[ $v ] = str_replace( '/sites/' . get_current_blog_id(), '', $upload_dir[ $v ] );
874 }
875 }
876 }
877
878 return $upload_dir;
879 }
880
881 add_action( 'learn_press_before_purchase_course_handler', '_learn_press_before_purchase_course_handler', 10, 2 );
882 function _learn_press_before_purchase_course_handler( $course_id, $cart ) {
883 // Redirect to login page if user is not logged in
884 if ( ! is_user_logged_in() ) {
885 $return_url = esc_url_raw( add_query_arg( $_POST, get_the_permalink( $course_id ) ) );
886 $return_url = apply_filters( 'learn_press_purchase_course_login_redirect_return_url', $return_url );
887 $redirect = apply_filters(
888 'learn_press_purchase_course_login_redirect',
889 learn_press_get_login_url( $return_url )
890 );
891 if ( $redirect !== false ) {
892 learn_press_add_message( __( 'Please login to enroll this course', 'learnpress' ) );
893
894 if ( learn_press_is_ajax() ) {
895 learn_press_send_json(
896 array(
897 'redirect' => $redirect,
898 'result' => 'success',
899 )
900 );
901 } else {
902 wp_redirect( $redirect );
903 exit();
904 }
905 }
906 } else {
907 $user = learn_press_get_current_user();
908 $redirect = false;
909 if ( $user->has_finished_course( $course_id ) ) {
910 learn_press_add_message( __( 'You have already finished course', 'learnpress' ) );
911 $redirect = true;
912 } elseif ( $user->has_purchased_course( $course_id ) ) {
913 learn_press_add_message( __( 'You have already enrolled in this course', 'learnpress' ) );
914 $redirect = true;
915 }
916 if ( $redirect ) {
917 wp_redirect( get_the_permalink( $course_id ) );
918 exit();
919 }
920 }
921 }
922
923 function learn_press_user_is( $role, $user_id = 0 ) {
924 if ( ! $user_id ) {
925 $user = learn_press_get_current_user();
926 } else {
927 $user = learn_press_get_user( $user_id );
928 }
929 if ( $role == 'admin' ) {
930 return $user->is_admin();
931 }
932 if ( $role == 'instructor' ) {
933 return $user->is_instructor();
934 }
935
936 return $role;
937 }
938
939 function learn_press_profile_tab_edit_content( $current, $tab, $user ) {
940 learn_press_get_template(
941 'profile/tabs/edit.php',
942 array(
943 'user' => $user,
944 'current' => $current,
945 'tab' => $tab,
946 )
947 );
948 }
949
950 function learn_press_get_profile_endpoints() {
951 $endpoints = (array) LP_Settings::instance()->get( 'profile_endpoints' );
952 $tabs = LP_Profile::instance()->get_tabs();
953 if ( $tabs ) {
954 foreach ( $tabs as $slug => $info ) {
955 if ( empty( $endpoints[ $slug ] ) ) {
956 $endpoints[ $slug ] = $slug;
957 }
958 }
959 }
960
961 return apply_filters( 'learn_press_profile_tab_endpoints', $endpoints );
962 }
963
964
965 function learn_press_update_user_option( $name, $value, $id = 0 ) {
966 if ( ! $id ) {
967 $id = get_current_user_id();
968 }
969 $key = 'learnpress_user_options';
970 $options = get_user_option( $key, $id );
971 $options[ $name ] = $value;
972 update_user_option( $id, $key, $options, true );
973 }
974
975 /**
976 * @param $name
977 * @param int $id
978 *
979 * @return bool
980 */
981 function learn_press_delete_user_option( $name, $id = 0 ) {
982 if ( ! $id ) {
983 $id = get_current_user_id();
984 }
985 $key = 'learnpress_user_options';
986 $options = get_user_option( $key, $id );
987 if ( is_array( $options ) && array_key_exists( $name, $options ) ) {
988 unset( $options[ $name ] );
989 update_user_option( $id, $key, $options, true );
990
991 return true;
992 }
993
994 return false;
995 }
996
997 /**
998 * @param $name
999 * @param int $id
1000 *
1001 * @return bool
1002 */
1003 function learn_press_get_user_option( $name, $id = 0 ) {
1004 if ( ! $id ) {
1005 $id = get_current_user_id();
1006 }
1007 $key = 'learnpress_user_options';
1008 $options = get_user_option( $key, $id );
1009 if ( is_array( $options ) && array_key_exists( $name, $options ) ) {
1010 return $options[ $name ];
1011 }
1012
1013 return false;
1014 }
1015
1016 /**
1017 * Check and update user information from request in user profile page
1018 */
1019 function learn_press_update_user_profile() {
1020
1021 if ( ! LP()->is_request( 'post' ) ) {
1022 return;
1023 }
1024 $nonce = learn_press_get_request( 'profile-nonce' );
1025
1026 if ( ! wp_verify_nonce( $nonce, 'learn-press-update-user-profile-' . get_current_user_id() ) ) {
1027 return;
1028 }
1029 $section = learn_press_get_request( 'lp-profile-section' );
1030
1031 do_action( 'learn_press_update_user_profile_' . $section );
1032 do_action( 'learn_press_update_user_profile', $section );
1033 }
1034
1035 // add_action( 'init', 'learn_press_update_user_profile' );
1036
1037 // /**
1038 // * Update user avatar
1039 // */
1040 // function learn_press_update_user_profile_avatar() {
1041 // $user_id = get_current_user_id();
1042 // $data = learn_press_get_request( 'lp-user-avatar-crop' );
1043
1044 // if ( ! $user_id ) {
1045 // return new WP_Error( 2, 'User is invalid!' );
1046 // }
1047
1048 // $upload_dir = learn_press_user_profile_picture_upload_dir();
1049
1050 // if ( learn_press_get_request( 'lp-user-avatar-custom' ) != 'yes' ) {
1051 // delete_user_meta( get_current_user_id(), '_lp_profile_picture' );
1052
1053 // return false;
1054 // }
1055
1056 // $path_img = get_user_meta( $user_id, '_lp_profile_picture', true );
1057
1058 // $path = $upload_dir['basedir'] . $path_img;
1059
1060 // if ( ! file_exists( $path ) ) {
1061 // return false;
1062 // }
1063
1064 // $filetype = wp_check_filetype( $path );
1065
1066 // if ( 'jpeg' == $filetype['ext'] ) {
1067 // $im = imagecreatefromjpeg( $path );
1068 // } elseif ( 'png' == $filetype['ext'] ) {
1069 // $im = imagecreatefrompng( $path );
1070 // }
1071
1072 // if ( ! isset( $im ) ) {
1073 // return false;
1074 // }
1075
1076 // $points = explode( ',', $data['points'] );
1077 // $im_crop = imagecreatetruecolor( $data['width'], $data['height'] );
1078
1079 // if ( ! $im ) {
1080 // return false;
1081 // }
1082
1083 // $dst_x = 0;
1084 // $dst_y = 0;
1085 // $dst_w = $data['width'];
1086 // $dst_h = $data['height'];
1087 // $src_x = $points[0];
1088 // $src_y = $points[1];
1089 // $src_w = $points[2] - $points[0];
1090 // $src_h = $points[3] - $points[1];
1091
1092 // imagecopyresampled( $im_crop, $im, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
1093
1094 // $newname = md5( $user_id . microtime( true ) );
1095 // $output = dirname( $path );
1096
1097 // if ( 'jpeg' == $filetype['ext'] ) {
1098 // $newname .= '.jpeg';
1099 // $output .= '/' . $newname;
1100 // imagejpeg( $im_crop, $output );
1101 // } elseif ( 'png' == $filetype['ext'] ) {
1102 // $newname .= '.png';
1103 // $output .= '/' . $newname;
1104 // imagepng( $im_crop, $output );
1105 // }
1106
1107 // $new_avatar = false;
1108
1109 // if ( file_exists( $output ) ) {
1110 // $new_avatar = preg_replace( '!^/!', '', $upload_dir['subdir'] ) . '/' . $newname;
1111 // update_user_meta( $user_id, '_lp_profile_picture', '/' . $new_avatar );
1112 // update_user_meta( $user_id, '_lp_profile_picture_changed', 'yes' );
1113
1114 // $new_avatar = $upload_dir['baseurl'] . '/' . $new_avatar;
1115 // }
1116
1117 // @unlink( $path );
1118
1119 // return $new_avatar;
1120 // }
1121
1122 // add_action( 'learn_press_update_user_profile_avatar', 'learn_press_update_user_profile_avatar' );
1123
1124 /**
1125 * Update user basic information.
1126 *
1127 * @param bool $wp_error - Optional. Return WP_Error object in case updating failed.
1128 *
1129 * @return bool|mixed|WP_Error
1130 */
1131 function learn_press_update_user_profile_basic_information( $wp_error = false ) {
1132 $user_id = get_current_user_id();
1133
1134 if ( ! $user_id ) {
1135 return new WP_Error( 2, 'User is invalid!' );
1136 }
1137
1138 $update_data = array(
1139 'ID' => $user_id,
1140 'first_name' => filter_input( INPUT_POST, 'first_name', FILTER_SANITIZE_STRING ),
1141 'last_name' => filter_input( INPUT_POST, 'last_name', FILTER_SANITIZE_STRING ),
1142 'description' => filter_input( INPUT_POST, 'description', FILTER_SANITIZE_STRING ),
1143 'display_name' => filter_input( INPUT_POST, 'account_display_name', FILTER_SANITIZE_STRING ),
1144 'user_email' => filter_input( INPUT_POST, 'account_email', FILTER_SANITIZE_EMAIL ),
1145 );
1146
1147 $update_data = apply_filters( 'learn-press/update-profile-basic-information-data', $update_data );
1148 $update_meta = LP_Helper::sanitize_params_submitted( $_POST['_lp_custom_register'] ?? '' );
1149
1150 $return = LP_Forms_Handler::update_user_data( $update_data, $update_meta );
1151
1152 // Update for social.
1153 $socials = LP_Request::get_array( 'user_profile_social' );
1154 $extra_data = get_user_meta( $user_id, '_lp_extra_info', true );
1155
1156 if ( ! empty( $extra_data ) ) {
1157 $socials = array_merge( $extra_data, $socials );
1158 }
1159
1160 update_user_meta( $user_id, '_lp_extra_info', $socials );
1161
1162 if ( is_wp_error( $return ) ) {
1163 return $wp_error ? $return : false;
1164 }
1165
1166 return $return;
1167 }
1168
1169 /**
1170 * Update new password.
1171 */
1172 function learn_press_update_user_profile_change_password( $wp_error = false ) {
1173 $user_id = get_current_user_id();
1174
1175 if ( ! $user_id ) {
1176 return new WP_Error( 2, 'User is invalid!' );
1177 }
1178
1179 $old_pass = filter_input( INPUT_POST, 'pass0' );
1180 $check_old_pass = false;
1181
1182 if ( $old_pass ) {
1183 $user = wp_get_current_user();
1184 require_once ABSPATH . 'wp-includes/class-phpass.php';
1185 $wp_hasher = new PasswordHash( 8, true );
1186
1187 if ( $wp_hasher->CheckPassword( $old_pass, $user->data->user_pass ) ) {
1188 $check_old_pass = true;
1189 }
1190 }
1191
1192 try {
1193 if ( ! $check_old_pass ) {
1194 throw new Exception( __( 'Old password incorrect!', 'learnpress' ) );
1195 } else {
1196 $new_pass = filter_input( INPUT_POST, 'pass1' );
1197 $new_pass2 = filter_input( INPUT_POST, 'pass2' );
1198
1199 if ( ! $new_pass || ! $new_pass2 || ( $new_pass != $new_pass2 ) ) {
1200 throw new Exception( __( 'Confirmation password incorrect!', 'learnpress' ) );
1201 } else {
1202 $update_data = array(
1203 'user_pass' => $new_pass,
1204 'ID' => get_current_user_id(),
1205 );
1206 $return = wp_update_user( $update_data );
1207
1208 if ( is_wp_error( $return ) ) {
1209 return $wp_error ? $return : false;
1210 }
1211
1212 return $return;
1213 }
1214 }
1215 } catch ( Exception $ex ) {
1216 return $wp_error ? new WP_Error( 'UPDATE_PROFILE_ERROR', $ex->getMessage() ) : false;
1217 }
1218 }
1219
1220 function learn_press_get_avatar_thumb_size() {
1221 $option = LP_Settings::get_option(
1222 'avatar_dimensions',
1223 array(
1224 'width' => 250,
1225 'height' => 250,
1226 )
1227 );
1228
1229 if ( ! isset( $option['width'] ) || ! isset( $option['height'] ) ) {
1230 $option = array(
1231 'width' => 250,
1232 'height' => 250,
1233 );
1234 }
1235
1236 return $option;
1237 }
1238
1239 function learn_press_get_course_thumbnail_dimensions() {
1240 $option = LP_Settings::get_option(
1241 'course_thumbnail_dimensions',
1242 array(
1243 'width' => 500,
1244 'height' => 300,
1245 )
1246 );
1247
1248 if ( ! isset( $option['width'] ) || ! isset( $option['height'] ) ) {
1249 $option = array(
1250 'width' => 500,
1251 'height' => 300,
1252 );
1253 }
1254
1255 return $option;
1256 }
1257
1258 /**
1259 * Set a fake cookie to
1260 */
1261 function learn_press_set_user_cookie_for_guest() {
1262 if ( ! is_admin() && ! headers_sent() ) {
1263 $guest_key = '_wordpress_lp_guest';
1264
1265 if ( is_user_logged_in() ) {
1266 if ( ! empty( $_COOKIE[ $guest_key ] ) ) {
1267 learn_press_remove_cookie( $guest_key );
1268 }
1269 } else {
1270 if ( empty( $_COOKIE[ $guest_key ] ) ) {
1271 learn_press_setcookie( $guest_key, md5( time() ), time() + 3600 );
1272 }
1273 }
1274 }
1275 }
1276
1277 add_action( 'wp', 'learn_press_set_user_cookie_for_guest' );
1278
1279 function learn_press_get_user_avatar( $user_id = 0, $size = '' ) {
1280 $user = learn_press_get_user( $user_id );
1281
1282 return $user->get_profile_picture( '', $size );
1283 }
1284
1285 /**
1286 * Get profile instance for an user to view.
1287 *
1288 * @param int $for_user
1289 *
1290 * @return LP_Profile|WP_Error
1291 */
1292 function learn_press_get_profile( $for_user = 0 ) {
1293 return LP_Profile::instance( $for_user );
1294 }
1295
1296 /**
1297 * Remove items from learnpress_user_items.
1298 *
1299 * @param int $user_id
1300 * @param int $item_id
1301 * @param int $course_id
1302 * @param bool $include_course - Optional. If TRUE then remove course and it's items
1303 */
1304 function learn_press_remove_user_items( $user_id, $item_id, $course_id, $include_course = false ) {
1305 global $wpdb;
1306
1307 settype( $item_id, 'array' );
1308
1309 $format = array_fill( 0, sizeof( $item_id ), '%d' );
1310 $where = '';
1311
1312 $args = array( $user_id );
1313 $args = array_merge( $args, $item_id );
1314
1315 if ( $course_id ) {
1316 $args[] = $course_id;
1317 $where = 'AND ref_id = %d';
1318 }
1319
1320 if ( $include_course ) {
1321 $where .= ' OR ( item_id = %d AND item_type = %s )';
1322 $args[] = $course_id;
1323 $args[] = LP_COURSE_CPT;
1324 }
1325
1326 $query = $wpdb->prepare(
1327 "
1328 DELETE
1329 FROM {$wpdb->learnpress_user_items}
1330 WHERE user_id = %d
1331 AND ( item_id IN(" . join( ',', $format ) . ")
1332 $where )
1333 ",
1334 $args
1335 );
1336 }
1337
1338 /**
1339 * Get user profile link
1340 *
1341 * @param int $user_id
1342 * @param null $tab
1343 *
1344 * @return mixed|string
1345 */
1346 function learn_press_user_profile_link( $user_id = 0, $tab = null ) {
1347 if ( ! $user_id ) {
1348 $user_id = get_current_user_id();
1349 }
1350 $user = false;
1351 $deleted = in_array( $user_id, LP_User_Factory::$_deleted_users );
1352 if ( ! $deleted ) {
1353 if ( is_numeric( $user_id ) ) {
1354 $user = get_user_by( 'id', $user_id );
1355 } else {
1356 $user = get_user_by( 'login', urldecode( $user_id ) );
1357 }
1358 } else {
1359 return '';
1360 }
1361 if ( ! $deleted && ! $user ) {
1362 LP_User_Factory::$_deleted_users[] = $user_id;
1363 }
1364
1365 $user = learn_press_get_user( $user_id );
1366
1367 if ( ! $user ) {
1368 return '';
1369 }
1370
1371 global $wp_query;
1372 $args = array(
1373 'user' => $user->get_username(),
1374 );
1375
1376 if ( isset( $args['user'] ) ) {
1377 if ( '' === $tab ) {
1378 $tab = learn_press_get_current_profile_tab();
1379 }
1380 if ( $tab ) {
1381 $args['tab'] = $tab;
1382 }
1383
1384 /**
1385 * If no tab is selected in profile and is current user
1386 * then no need the username in profile link.
1387 */
1388 if ( ( $user_id == get_current_user_id() ) && ! isset( $args['tab'] ) ) {
1389 unset( $args['user'] );
1390 }
1391 }
1392 $args = array_map( '_learn_press_urlencode', $args );
1393 $profile_link = trailingslashit( learn_press_get_page_link( 'profile' ) );
1394 if ( $profile_link ) {
1395 if ( get_option( 'permalink_structure' ) /*&& learn_press_get_page_id( 'profile' )*/ ) {
1396 $url = trailingslashit( $profile_link . join( '/', array_values( $args ) ) );
1397 } else {
1398 $url = esc_url_raw( add_query_arg( $args, $profile_link ) );
1399 }
1400 } else {
1401 $url = get_author_posts_url( $user_id );
1402 }
1403
1404 return apply_filters( 'learn_press_user_profile_link', $url, $user_id, $tab );
1405 }
1406
1407 /**********************************************/
1408 /* Functions are used for hooks */
1409 /**********************************************/
1410
1411 function learn_press_hk_before_start_quiz( $true, $quiz_id, $course_id, $user_id ) {
1412 if ( 'yes' !== get_post_meta( $quiz_id, '_lp_archive_history', true ) ) {
1413 learn_press_remove_user_items( $user_id, $quiz_id, $course_id );
1414 }
1415
1416 return $true;
1417 }
1418
1419 add_filter( 'learn-press/before-start-quiz', 'learn_press_hk_before_start_quiz', 10, 4 );
1420
1421 /*function learn_press_default_user_item_status( $item_id ) {
1422 $status = '';
1423 switch ( learn_press_get_post_type( $item_id ) ) {
1424 case LP_LESSON_CPT:
1425 $status = 'started';
1426 break;
1427 case LP_QUIZ_CPT:
1428 $status = 'viewed';
1429 break;
1430 case LP_COURSE_CPT:
1431 $status = 'enrolled';
1432 }
1433
1434 return apply_filters( 'learn-press/default-user-item-status', $status, $item_id );
1435 }*/
1436
1437 /**
1438 * Get current state of distraction mode
1439 *
1440 * @return mixed
1441 * @since 3.1.0
1442 */
1443 function learn_press_get_user_distraction() {
1444 if ( is_user_logged_in() ) {
1445 return get_user_option( 'distraction_mode', get_current_user_id() );
1446 } else {
1447 return LP()->session->distraction_mode;
1448 }
1449 }
1450
1451 function learn_press_get_user_role( $user_id ) {
1452 if ( $user = learn_press_get_user( $user_id ) ) {
1453 return $user->get_role();
1454 }
1455
1456 return false;
1457 }
1458
1459 /**
1460 * @param array $args
1461 * @param bool $wp_error
1462 *
1463 * @return bool|int|LP_User_Item|mixed|WP_Error
1464 */
1465 function learn_press_create_user_item( $args = array(), $wp_error = false ) {
1466 global $wpdb;
1467
1468 $defaults = array(
1469 'user_id' => get_current_user_id(),
1470 'item_id' => '',
1471 'start_time' => current_time( 'mysql', true ),
1472 'end_time' => '',
1473 'graduation' => '',
1474 'item_type' => '',
1475 'status' => '',
1476 'ref_id' => 0,
1477 'ref_type' => 0,
1478 'parent_id' => 0,
1479 'create_meta' => array(),
1480 );
1481
1482 $item_data = wp_parse_args( $args, $defaults );
1483
1484 // Validate item_id and post type
1485 if ( empty( $item_data['item_id'] ) ) {
1486 if ( $wp_error ) {
1487 return new WP_Error( 'invalid_item_id', __( 'Invalid item id.', 'learnpress' ) );
1488 }
1489
1490 return 0;
1491 }
1492
1493 if ( empty( $item_data['item_type'] ) && $post_type = learn_press_get_post_type( $item_data['item_id'] ) ) {
1494 $item_data['item_type'] = $post_type;
1495 }
1496
1497 // Get id and type of ref if they are null
1498 if ( ! empty( $item_data['parent_id'] ) && ( empty( $item_data['ref_id'] ) || ( empty( $item_data['ref_type'] ) ) ) ) {
1499 $parent = $wpdb->get_row(
1500 $wpdb->prepare(
1501 "SELECT * FROM {$wpdb->learnpress_user_items} WHERE %d",
1502 $item_data['parent_id']
1503 )
1504 );
1505
1506 if ( $parent ) {
1507 if ( empty( $item_data['ref_id'] ) ) {
1508 $item_data['ref_id'] = $parent->item_id;
1509 }
1510
1511 if ( empty( $item_data['ref_type'] ) ) {
1512 $item_data['ref_type'] = $parent->item_type;
1513 }
1514 }
1515 }
1516
1517 // Filter
1518 if ( ! $item_data = apply_filters( 'learn-press/create-user-item-data', $item_data ) ) {
1519 if ( $wp_error ) {
1520 return new WP_Error( 'invalid_item_data', __( 'Invalid item data.', 'learnpress' ) );
1521 }
1522
1523 return 0;
1524 }
1525
1526 do_action( 'learn-press/before-create-user-item', $item_data );
1527
1528 $create_meta = ! empty( $item_data['create_meta'] ) ? $item_data['create_meta'] : false;
1529
1530 if ( $create_meta ) {
1531 unset( $item_data['create_meta'] );
1532 }
1533
1534 $user_item = new LP_User_Item( $item_data );
1535
1536 $result = $user_item->update( true, false );
1537
1538 if ( ! $result || is_wp_error( $result ) ) {
1539
1540 if ( $wp_error && is_wp_error( $result ) ) {
1541 return $result;
1542 }
1543
1544 return 0;
1545 }
1546
1547 do_action( 'learn-press/created-user-item', $user_item, $item_data );
1548
1549 $create_meta = apply_filters( 'learn-press/create-user-item-meta', $create_meta, $item_data );
1550 if ( ! $create_meta ) {
1551 return $user_item;
1552 }
1553
1554 do_action( 'learn-press/before-create-user-item-meta', $create_meta );
1555
1556 foreach ( $create_meta as $key => $value ) {
1557 learn_press_update_user_item_meta( $user_item->get_user_item_id(), $key, $value );
1558 }
1559
1560 do_action( 'learn-press/created-user-item-meta', $user_item, $create_meta );
1561
1562 return $user_item;
1563 }
1564
1565 /**
1566 * @param array $args
1567 * @param bool $wp_error - Optional. TRUE will return WP_Error on fail.
1568 *
1569 * @return bool|array|LP_User_Item|WP_Error
1570 */
1571 function learn_press_create_user_item_for_quiz( $args = array(), $wp_error = false ) {
1572 global $wpdb;
1573
1574 $item_data = wp_parse_args(
1575 $args,
1576 array(
1577 'item_type' => LP_QUIZ_CPT,
1578 'status' => LP_ITEM_STARTED,
1579 'graduation' => LP_COURSE_GRADUATION_IN_PROGRESS,
1580 'user_id' => get_current_user_id(),
1581 )
1582 );
1583
1584 $user_item = learn_press_create_user_item( $item_data, $wp_error );
1585
1586 if ( $user_item && ! is_wp_error( $user_item ) ) {
1587 $user_item = new LP_User_Item_Quiz( $user_item->get_data() );
1588 $user_item->update( true );
1589 }
1590
1591 return $user_item;
1592 }
1593
1594 /**
1595 * Get list user_item_id for Quiz in table learnpress_user_items
1596 *
1597 * @param int $quiz_id
1598 * @param int $course_id
1599 * @return array || false
1600 */
1601 function learn_press_isset_user_item_for_quiz( $quiz_id, $course_id ) {
1602 global $wpdb;
1603
1604 $query = $wpdb->prepare( "SELECT user_item_id FROM $wpdb->learnpress_user_items WHERE ref_id=%d AND item_id=%d", $course_id, $quiz_id );
1605 $col = $wpdb->get_col( $query );
1606
1607 if ( ! empty( $col ) ) {
1608 return $col;
1609 } else {
1610 return false;
1611 }
1612 }
1613
1614 /**
1615 * Create new user item prepare for user starts a quiz
1616 * Update error retry course not work - Nhamdv.
1617 *
1618 * @param int $quiz_id
1619 * @param int $user_id
1620 * @param int $course_id
1621 * @param bool $wp_error
1622 *
1623 * @return array|bool|LP_User_Item|WP_Error
1624 * @since 4.0.0
1625 */
1626 function learn_press_user_start_quiz( $quiz_id, $user_id = 0, $course_id = 0, $wp_error = false ) {
1627 if ( ! $user_id ) {
1628 $user_id = get_current_user_id();
1629 }
1630
1631 global $wpdb;
1632
1633 $query = $wpdb->prepare(
1634 "
1635 SELECT user_item_id, item_id id, item_type type
1636 FROM {$wpdb->learnpress_user_items}
1637 WHERE user_item_id = (SELECT max(user_item_id)
1638 FROM {$wpdb->learnpress_user_items}
1639 WHERE user_id = %d AND item_id = %d AND status IN ('enrolled', 'in-progress'))
1640 ",
1641 $user_id,
1642 $course_id
1643 );
1644
1645 $parent = $wpdb->get_row( $query );
1646
1647 do_action( 'learn-press/before-user-start-quiz', $quiz_id, $user_id, $course_id );
1648
1649 $user = learn_press_get_user( $user_id );
1650 $course_data = $user->get_course_data( $course_id );
1651 $quiz_data = $course_data->get_item( $quiz_id );
1652
1653 $quiz = LP_Quiz::get_quiz( $quiz_id );
1654 $duration = $quiz->get_duration();
1655 $user_quiz = learn_press_create_user_item_for_quiz(
1656 array(
1657 'user_item_id' => $quiz_data ? $quiz_data->get_user_item_id() : 0,
1658 'item_id' => $quiz->get_id(),
1659 'duration' => $duration ? $duration->get() : 0,
1660 'user_id' => $user_id,
1661 'parent_id' => $parent ? absint( $parent->user_item_id ) : 0,
1662 'ref_type' => $parent ? $parent->type : '',
1663 'ref_id' => $parent ? $parent->id : '',
1664 ),
1665 $wp_error
1666 );
1667
1668 if ( $user_quiz && ! is_wp_error( $user_quiz ) ) {
1669 do_action( 'learn-press/user-started-quiz', $user_quiz, $quiz_id, $user_id, $course_id );
1670 }
1671
1672 // Reset first cache
1673 $user_quiz->get_status( 'status', true );
1674
1675 return $user_quiz;
1676 }
1677
1678 /**
1679 * Function retake quiz.
1680 *
1681 * @param [type] $quiz_id
1682 * @param integer $user_id
1683 * @param integer $course_id
1684 * @param boolean $wp_error
1685 *
1686 * @return void
1687 */
1688 function learn_press_user_retake_quiz( $quiz_id, $user_id = 0, $course_id = 0, $wp_error = false ) {
1689 if ( ! $user_id ) {
1690 $user_id = get_current_user_id();
1691 }
1692
1693 if ( ! $course_id ) {
1694 return new WP_Error( 'invalid_course_id', esc_html__( 'Invalid Course ID.', 'learnpress' ) );
1695 }
1696
1697 global $wpdb;
1698
1699 $query = $wpdb->prepare(
1700 "
1701 SELECT user_item_id, item_id id, item_type type
1702 FROM {$wpdb->learnpress_user_items}
1703 WHERE user_item_id = (SELECT max(user_item_id)
1704 FROM {$wpdb->learnpress_user_items}
1705 WHERE user_id = %d AND item_id = %d AND status IN ('enrolled', 'in-progress'))
1706 ",
1707 $user_id,
1708 $course_id
1709 );
1710
1711 $parent = $wpdb->get_row( $query );
1712
1713 if ( ! $parent ) {
1714 return new WP_Error( 'invalid_user_item', esc_html__( 'Invalid Quiz', 'learnpress' ) );
1715 }
1716
1717 $data = learn_press_get_user_item(
1718 array(
1719 'item_id' => $quiz_id,
1720 'user_id' => $user_id,
1721 'parent_id' => $parent ? absint( $parent->user_item_id ) : 0,
1722 'ref_type' => $parent ? $parent->type : LP_COURSE_CPT,
1723 'ref_id' => $parent ? $parent->id : '',
1724 )
1725 );
1726
1727 $user_item = new LP_User_Item_Quiz( $data );
1728
1729 $ratake_count = get_post_meta( $quiz_id, '_lp_retake_count', true );
1730
1731 if ( $ratake_count > 0 ) {
1732 $user_item->update_retake_count();
1733 }
1734
1735 // Create new result in table learnpress_user_item_results.
1736 LP_User_Items_Result_DB::instance()->insert( $data->user_item_id );
1737
1738 // Remove user_item_meta.
1739 learn_press_delete_user_item_meta( $data->user_item_id, '_lp_question_checked' );
1740
1741 $user_item->set_status( LP_ITEM_STARTED )
1742 ->set_start_time( current_time( 'mysql', 1 ) ) // Error Retake when change timezone - Nhamdv
1743 ->set_end_time( '' )
1744 ->set_graduation( LP_COURSE_GRADUATION_IN_PROGRESS )
1745 ->update();
1746
1747 // Reset first cache
1748 $user_item->get_status( 'status', true );
1749
1750 // Error Retake when change timezone - Nhamdv
1751 // learn_press_update_user_item_field(
1752 // array(
1753 // 'start_time' => current_time( 'mysql', true ),
1754 // ),
1755 // array(
1756 // 'user_item_id' => $data->user_item_id,
1757 // )
1758 // );
1759
1760 return $user_item;
1761 }
1762
1763
1764 /**
1765 * Prepares list of questions for rest api.
1766 *
1767 * @param int[] $question_ids
1768 * @param array $args
1769 *
1770 * @return array
1771 * @since 3.3.0
1772 */
1773 function learn_press_rest_prepare_user_questions( array $question_ids = array(), array $args = array() ) : array {
1774 if ( is_numeric( $args ) ) {
1775
1776 } else {
1777 $args = wp_parse_args(
1778 $args,
1779 array(
1780 'instant_hint' => true,
1781 'instant_check' => true,
1782 'quiz_status' => '',
1783 'checked_questions' => array(),
1784 'hinted_questions' => array(),
1785 'answered' => array(),
1786 'show_correct_review' => true,
1787 )
1788 );
1789 }
1790
1791 $checkedQuestions = $args['checked_questions'];
1792 $hintedQuestions = $args['hinted_questions'];
1793 $instantHint = $args['instant_hint'];
1794 $instantCheck = $args['instant_check'];
1795 $quizStatus = $args['quiz_status'];
1796 $answered = $args['answered'];
1797 $status = $args['status'] ?? '';
1798 $questions = array();
1799
1800 if ( $question_ids ) {
1801 foreach ( $question_ids as $id ) {
1802 $question = learn_press_get_question( $id );
1803 $hasHint = false;
1804 $hasExplanation = false;
1805 $canCheck = false;
1806 $hinted = false;
1807 $checked = false;
1808 $theHint = $question->get_hint();
1809 $theExplanation = '';
1810
1811 if ( $instantCheck || $status == 'completed' ) {
1812 $theExplanation = $question->get_explanation();
1813 $checked = in_array( $id, $checkedQuestions );
1814 $hasExplanation = ! ! $theExplanation;
1815 }
1816
1817 $mark = $question->get_mark() ? $question->get_mark() : 1;
1818
1819 $questionData = array(
1820 'object' => $question,
1821 'id' => absint( $id ),
1822 'title' => $question->get_title(),
1823 'type' => $question->get_type(),
1824 'point' => $mark,
1825 );
1826
1827 $content = $question->get_content();
1828 if ( $content ) {
1829 $questionData['content'] = $content;
1830 }
1831
1832 if ( $theHint ) {
1833 $questionData['hint'] = $theHint;
1834 }
1835
1836 if ( $status == 'completed' || ( $checked && $theExplanation ) ) {
1837 $questionData['explanation'] = $theExplanation;
1838 }
1839
1840 if ( $hasExplanation ) {
1841 $questionData['has_explanation'] = $hasExplanation;
1842
1843 if ( $checked ) {
1844 $questionData['explanation'] = $theExplanation;
1845 }
1846 }
1847
1848 $with_true_or_false = ( $checked || ( $quizStatus == 'completed' && $args['show_correct_review'] ) );
1849
1850 // if ( $question->is_support( 'answer-options' ) ) {
1851 $questionData['options'] = learn_press_get_question_options_for_js(
1852 $question,
1853 array(
1854 'include_is_true' => $with_true_or_false,
1855 'answer' => $answered[ $id ]['answered'] ?? '',
1856 )
1857 );
1858 // }
1859
1860 $questions[] = $questionData;
1861 }
1862
1863 /**
1864 * Remove answered
1865 */
1866 if ( $quizStatus !== 'completed' ) {
1867 if ( $checkedQuestions && $quizStatus ) {
1868
1869 $omitIds = array_diff( $question_ids, $checkedQuestions );
1870
1871 if ( $omitIds ) {
1872 foreach ( $omitIds as $omitId ) {
1873 if ( ! empty( $answered[ $omitId ] ) ) {
1874 unset( $answered[ $omitId ] );
1875 }
1876 }
1877 }
1878 }
1879 }
1880 }
1881
1882 return apply_filters( 'learn-press/list-questions-data', $questions );
1883 }
1884
1885 /**
1886 * Update extra profile data upon update user.
1887 *
1888 * @param int $user_id
1889 *
1890 * @since 4.0.0
1891 */
1892 function learn_press_update_extra_user_profile_fields( $user_id ) {
1893 if ( ! current_user_can( 'edit_user', $user_id ) ) {
1894 return;
1895 }
1896
1897 if ( isset( $_POST['_lp_extra_info'] ) ) {
1898 update_user_meta( $user_id, '_lp_extra_info', LP_Helper::sanitize_params_submitted( $_POST['_lp_extra_info'] ) );
1899 }
1900 }
1901 add_action( 'personal_options_update', 'learn_press_update_extra_user_profile_fields' );
1902 add_action( 'edit_user_profile_update', 'learn_press_update_extra_user_profile_fields' );
1903
1904 /**
1905 * Get extra profile info data
1906 *
1907 * @param int $user_id
1908 *
1909 * @return array
1910 * @since 4.0.0
1911 */
1912 function learn_press_get_user_extra_profile_info( $user_id = 0 ) {
1913 if ( ! $user_id ) {
1914 $user_id = get_current_user_id();
1915 }
1916
1917 $extra_profile_info = get_the_author_meta( '_lp_extra_info', $user_id );
1918 $extra_fields = learn_press_get_user_extra_profile_fields();
1919
1920 $extra_profile_info = wp_parse_args(
1921 $extra_profile_info,
1922 array_fill_keys( array_keys( $extra_fields ), '' )
1923 );
1924
1925 return apply_filters( 'learn-press/user-extra-profile-info', $extra_profile_info, $user_id );
1926 }
1927
1928 function learn_press_social_profiles() {
1929 return apply_filters(
1930 'learn-press/social-profiles',
1931 array(
1932 'facebook',
1933 'twitter',
1934 'youtube',
1935 'linkedin',
1936 )
1937 );
1938 }
1939
1940 function lp_add_default_fields( $fields ) {
1941 $first_name = LP_Settings::get_option( 'enable_register_first_name', 'no' );
1942
1943 if ( $first_name === 'yes' ) {
1944 ?>
1945 <li class="form-field">
1946 <label for="reg_first_name"><?php esc_html_e( 'First name', 'learnpress' ); ?></label>
1947 <input id="reg_first_name" name="reg_first_name" type="text"
1948 placeholder="<?php esc_attr_e( 'First name', 'learnpress' ); ?>"
1949 value="<?php echo esc_attr( wp_unslash( $_POST['reg_first_name'] ?? '' ) ); ?>">
1950 </li>
1951 <?php
1952 }
1953
1954 $last_name = LP_Settings::instance()->get( 'enable_register_last_name' );
1955
1956 if ( $last_name === 'yes' ) {
1957 ?>
1958 <li class="form-field">
1959 <label for="reg_last_name"><?php esc_html_e( 'Last name', 'learnpress' ); ?></label>
1960 <input id="reg_last_name" name="reg_last_name" type="text"
1961 placeholder="<?php esc_attr_e( 'Last name', 'learnpress' ); ?>"
1962 value="<?php echo esc_attr( wp_unslash( $_POST['reg_last_name'] ?? '' ) ); ?>">
1963 </li>
1964 <?php
1965 }
1966
1967 $display_name = LP_Settings::instance()->get( 'enable_register_display_name' );
1968
1969 if ( $display_name === 'yes' ) {
1970 ?>
1971 <li class="form-field">
1972 <label for="reg_display_name"><?php esc_html_e( 'Display name', 'learnpress' ); ?></label>
1973 <input id="reg_display_name" name="reg_display_name" type="text"
1974 placeholder="<?php esc_attr_e( 'Display name', 'learnpress' ); ?>"
1975 value="<?php echo esc_attr( wp_unslash( $_POST['reg_display_name'] ?? '' ) ); ?>">
1976 </li>
1977 <?php
1978 }
1979 }
1980
1981 add_filter( 'learn-press/after-form-register-fields', 'lp_add_default_fields' );
1982
1983 function lp_custom_register_fields_display() {
1984 ?>
1985 <?php $custom_fields = LP_Settings::instance()->get( 'register_profile_fields' ); ?>
1986
1987 <?php if ( $custom_fields ) : ?>
1988 <?php foreach ( $custom_fields as $custom_field ) : ?>
1989 <?php
1990 $cf_class = '';
1991 if ( $custom_field['required'] == 'yes' ) {
1992 $cf_class = ' required';
1993 ?>
1994 <style>
1995 .required label {
1996 font-weight: bold;
1997 }
1998 .required label:after {
1999 content: ' *';
2000 display:inline;
2001 }
2002 </style>
2003 <?php
2004 }
2005
2006 if ( isset( $custom_field['id'] ) ) {
2007 ?>
2008 <?php $value = $custom_field['id']; ?>
2009
2010 <li class="form-field<?php echo esc_attr( $cf_class ); ?>">
2011 <?php
2012 switch ( $custom_field['type'] ) {
2013 case 'text':
2014 case 'number':
2015 case 'email':
2016 case 'url':
2017 case 'tel':
2018 ?>
2019 <label for="description"><?php echo esc_html( $custom_field['name'] ); ?></label>
2020 <input name="_lp_custom_register_form[<?php echo esc_attr( $value ); ?>]"
2021 type="<?php echo esc_attr( $custom_field['type'] ); ?>" class="regular-text"
2022 value="" />
2023 <?php
2024 break;
2025 case 'textarea':
2026 ?>
2027 <label for="description"><?php echo esc_html( $custom_field['name'] ); ?></label>
2028 <textarea name="_lp_custom_register_form[<?php echo esc_attr( $value ); ?>]"></textarea>
2029 <?php
2030 break;
2031 case 'checkbox':
2032 ?>
2033 <label>
2034 <input name="_lp_custom_register_form[<?php echo esc_attr( $value ); ?>]"
2035 type="<?php echo esc_attr( $custom_field['type'] ); ?>" value="1">
2036 <?php echo esc_html( $custom_field['name'] ); ?>
2037 </label>
2038 <?php
2039 break;
2040 }
2041 ?>
2042 </li>
2043 <?php } ?>
2044 <?php endforeach; ?>
2045 <?php endif; ?>
2046 <?php
2047 }
2048
2049 add_action( 'learn-press/after-form-register-fields', 'lp_custom_register_fields_display' );
2050
2051 /**
2052 * Custom register fields
2053 *
2054 * @param [type] $user_id
2055 *
2056 * @return void
2057 */
2058 function lp_user_custom_register_fields( $user_id, $fields = array() ) {
2059 if ( ! empty( $fields ) ) {
2060 update_user_meta( $user_id, '_lp_custom_register', LP_Helper::sanitize_params_submitted( $fields ) );
2061 } elseif ( isset( $_POST['_lp_custom_register'] ) ) {
2062 update_user_meta( $user_id, '_lp_custom_register', LP_Helper::sanitize_params_submitted( $_POST['_lp_custom_register'] ) );
2063 }
2064 }
2065
2066 add_action( 'personal_options_update', 'lp_user_custom_register_fields' );
2067 add_action( 'edit_user_profile_update', 'lp_user_custom_register_fields' );
2068
2069 function lp_get_user_custom_register_fields( $user_id = 0 ) {
2070 if ( ! $user_id ) {
2071 $user_id = get_current_user_id();
2072 }
2073
2074 $register_fields = get_the_author_meta( '_lp_custom_register', $user_id );
2075 $defaults = lp_get_user_custom_fields();
2076
2077 $extra_profile_info = wp_parse_args( $register_fields, $defaults );
2078
2079 return apply_filters( 'lp/user-custom-register-fields', $register_fields, $user_id );
2080 }
2081
2082 function lp_get_user_custom_fields() {
2083 $custom_fields = LP_Settings::instance()->get( 'register_profile_fields' );
2084
2085 $output = array();
2086
2087 if ( $custom_fields ) {
2088 foreach ( $custom_fields as $field ) {
2089 $output[ $field['id'] ] = '';
2090 }
2091 }
2092
2093 return $output;
2094 }
2095
2096 /**
2097 * Check extra user data is a social profile.
2098 *
2099 * @param $key
2100 *
2101 * @return bool
2102 * @since 4.0.0
2103 */
2104 function learn_press_is_social_profile( $key ) {
2105 $is_socials = learn_press_social_profiles();
2106
2107 return in_array( $key, $is_socials );
2108 }
2109
2110 function learn_press_social_profile_name( $key ) {
2111 $name = '';
2112 switch ( $key ) {
2113 case 'facebook':
2114 $name = esc_html__( 'Facebook Profile', 'learnpress' );
2115 break;
2116 case 'twitter':
2117 $name = esc_html__( 'Twitter Profile', 'learnpress' );
2118 break;
2119 case 'googleplus':
2120 $name = esc_html__( 'Google Profile', 'learnpress' );
2121 break;
2122 case 'youtube':
2123 $name = esc_html__( 'Youtube Channel', 'learnpress' );
2124 break;
2125 case 'linkedin':
2126 $name = esc_html__( 'Linkedin Profile', 'learnpress' );
2127 break;
2128 default:
2129 $name = ucfirst( $key );
2130 }
2131
2132 return apply_filters( 'learn-press/social-profile-name', $name, $key );
2133 }
2134
2135 /**
2136 * Get extra profile fields will be registered in backend profile.
2137 *
2138 * @return array
2139 * @since 4.0.0
2140 */
2141 function learn_press_get_user_extra_profile_fields() {
2142 $socials = learn_press_social_profiles();
2143 $fields = array();
2144
2145 foreach ( $socials as $social ) {
2146 $fields[ $social ] = learn_press_social_profile_name( $social );
2147 }
2148
2149 return apply_filters( 'learn-press/user-extra-profile-fields', $fields );
2150 }
2151
2152 /**
2153 * Show courses user enrolled on backend
2154 *
2155 * @param $user
2156 *
2157 * @return void
2158 */
2159 function learn_press_user_profile_data( $user ) {
2160 if ( ! is_admin() ) {
2161 return;
2162 }
2163
2164 learn_press_admin_view( 'backend-user-profile', array( 'user' => $user ) );
2165 learn_press_admin_view( 'user/courses.php', array( 'user_id' => $user->ID ) );
2166 }
2167 add_action( 'edit_user_profile', 'learn_press_user_profile_data', 1000 );
2168
2169 function learnpress_get_count_by_user( $user_id = '', $post_type = 'lp_course' ) {
2170 if ( empty( $user_id ) ) {
2171 return false;
2172 }
2173
2174 $args = array(
2175 'author' => $user_id,
2176 'posts_per_page' => - 1,
2177 'post_type' => $post_type,
2178 'post_status' => 'any',
2179 );
2180
2181 $posts = get_posts( $args );
2182
2183 $output = array(
2184 'all' => count( $posts ),
2185 'publish' => array(),
2186 'pending' => array(),
2187 );
2188
2189 $pending = $public = array();
2190
2191 if ( ! empty( $posts ) ) {
2192 foreach ( $posts as $post ) {
2193 switch ( $post->post_status ) {
2194 case 'pending':
2195 $pending[] = $post;
2196 break;
2197 case 'publish':
2198 $public[] = $post;
2199 break;
2200 default:
2201 break;
2202 }
2203 }
2204 }
2205
2206 return array(
2207 'all' => count( $posts ),
2208 'publish' => count( $public ),
2209 'pending' => count( $pending ),
2210 );
2211
2212 }
2213