PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.10
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.10
4.4.10 4.4.9.1 4.4.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 All 142 releases
← All changes | inc/jwt/rest-api/version1/class-lp-rest-courses-v1-controller.php +76 -13 4.4.8 → 4.4.10 View file →
@@ -458,9 +458,15 @@
458 458 $params = $request->get_params();
459 459 $params = $this->convert_params_query_courses( $params );
460 460
461 461 Courses::handle_params_for_query_courses( $filter, $params );
462 - $filter->post_status = [ PostModel::STATUS_PUBLISH ];
462 + $filter->post_status = [ PostModel::STATUS_PUBLISH ];
463 +
464 + // Filter by slug (post_name).
465 + if ( ! empty( $params['slug'] ) ) {
466 + $filter->post_name = is_array( $params['slug'] ) ? reset( $params['slug'] ) : $params['slug'];
467 + }
468 +
463 469 $key_cache = 'api/' . md5( json_encode( $params ) );
464 470 $key_cache_total = $key_cache . '_total';
465 471 $key_cache_total_pages = $key_cache . '_total_pages';
466 472 $lp_courses_cache = new LP_Courses_Cache( true );
@@ -494,9 +500,9 @@
494 500 } else {
495 501 // Check cache with case not learned
496 502 $courses_cache = $lp_courses_cache->get_cache( $key_cache );
497 503 if ( $courses_cache !== false ) {
498 - $courses = json_decode( $courses_cache, true );
504 + $courses = $courses_cache;
499 505 $total = (int) $lp_courses_cache->get_cache( $key_cache_total );
500 506 $total_pages = (int) $lp_courses_cache->get_cache( $key_cache_total_pages );
501 507
502 508 $response = rest_ensure_response( $courses );
@@ -543,12 +549,16 @@
543 549 if ( empty( $course ) ) {
544 550 continue;
545 551 }
546 552
547 - $courseObjPrepare = new stdClass();
548 - $courseObjPrepare->id = (int) $courseObj->ID ?? 0;
549 - $courseObjPrepare->name = html_entity_decode( $course->get_title() );
550 - $courseObjPrepare->image = $course->get_image_url();
553 + $courseObjPrepare = new stdClass();
554 + $courseObjPrepare->id = (int) $courseObj->ID ?? 0;
555 + $courseObjPrepare->name = html_entity_decode( $course->get_title() );
556 + $courseObjPrepare->slug = $courseObj->post_name ?? get_post_field( 'post_name', $courseObj->ID );
557 + $courseObjPrepare->permalink = get_permalink( $courseObj->ID );
558 + $courseObjPrepare->excerpt = $courseObj->post_excerpt ?? '';
559 + $courseObjPrepare->count_students = method_exists( $course, 'count_students' ) ? (int) $course->count_students() : 0;
560 + $courseObjPrepare->image = $course->get_image_url();
551 561 $author = $course->get_author_model();
552 562 $courseObjPrepare->instructor = ! empty( $author ) ? $this->get_author_info( $author ) : [];
553 563 $duration = $course->get_meta_value_by_key( CoursePostModel::META_KEY_DURATION, '' );
554 564 $duration_arr = explode( ' ', $duration );
@@ -578,11 +588,11 @@
578 588 $courseObjPrepare->sale_price_rendered = html_entity_decode(
579 589 learn_press_format_price( $course->get_sale_price(), true )
580 590 );
581 591 // When release Addon Course Review v4.1.3 a long time, we will remove this code.
582 - $courseObjPrepare->rating = $this->get_course_rating( $courseObj->ID );
583 - $courseObjPrepare->meta_data = new stdClass();
584 - $courseObjPrepare->meta_data->_lp_passing_condition = $course->get_meta_value_by_key( CoursePostModel::META_KEY_PASSING_CONDITION );
592 + $courseObjPrepare->rating = $this->get_course_rating( $courseObj->ID );
593 + // All settings of the course metabox, same as the single course response.
594 + $courseObjPrepare->meta_data = (object) $this->get_course_meta( $courseObj->ID );
585 595
586 596 // Add more fields
587 597 if ( ! empty( $params['learned'] ) ) {
588 598 $courseObjPrepare->course_data = new stdClass();
@@ -731,8 +741,13 @@
731 741 public function prepare_object_for_response( $object, $request ) {
732 742 $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
733 743 $data = $this->get_course_data( $object, $context, $request );
734 744
745 + // Ensure sections are always included for single-item response, regardless of schema/context filtering.
746 + if ( empty( $request['optimize'] ) && ! isset( $data['sections'] ) ) {
747 + $data['sections'] = $this->get_all_items( $object );
748 + }
749 +
735 750 $response = rest_ensure_response( $data );
736 751
737 752 return apply_filters( "lp_jwt_rest_prepare_{$this->post_type}_object", $response, $object, $request );
738 753 }
@@ -995,13 +1010,44 @@
995 1010 $output['id'] = $author->get_id();
996 1011 $output['name'] = $author->get_display_name();
997 1012 $output['description'] = $author->get_description();
998 1013 $output['social'] = $extra_info;
1014 + $output['statistic'] = $this->get_instructor_statistic( $author );
999 1015
1000 1016 return $output;
1001 1017 }
1002 1018
1003 1019 /**
1020 + * Count the courses and the students of an instructor.
1021 + *
1022 + * Only used for the single course response, it costs several queries and
1023 + * "LP_Cache::cache_load_first" only keeps the result within the current request.
1024 + *
1025 + * @param UserModel $author
1026 + *
1027 + * @return array
1028 + */
1029 + protected function get_instructor_statistic( UserModel $author ): array {
1030 + $statistic = $author->get_instructor_statistic();
1031 +
1032 + // Keep the extra keys added by add-ons, only cast the ones of the core.
1033 + $numeric_keys = array(
1034 + 'total_course',
1035 + 'published_course',
1036 + 'pending_course',
1037 + 'total_student',
1038 + 'student_completed',
1039 + 'student_in_progress',
1040 + );
1041 +
1042 + foreach ( $numeric_keys as $numeric_key ) {
1043 + $statistic[ $numeric_key ] = absint( $statistic[ $numeric_key ] ?? 0 );
1044 + }
1045 +
1046 + return $statistic;
1047 + }
1048 +
1049 + /**
1004 1050 * Get instructor info
1005 1051 *
1006 1052 * @param UserModel $author
1007 1053 *
@@ -1139,8 +1185,9 @@
1139 1185
1140 1186 $data_item[] = array(
1141 1187 'id' => $item->get_id(),
1142 1188 'type' => $item->get_item_type(),
1189 + 'slug' => $post->post_name,
1143 1190 'title' => $post->post_title,
1144 1191 'preview' => $item->is_preview(),
1145 1192 'duration' => $item->get_duration()->to_timer( $format, true ),
1146 1193 'graduation' => $user_item ? $user_item->get_graduation() : '',
@@ -1158,12 +1205,17 @@
1158 1205
1159 1206 return $output;
1160 1207 }
1161 1208
1209 + /**
1210 + * Get all settings of the course metabox.
1211 + *
1212 + * @param int $id Course ID.
1213 + *
1214 + * @return array
1215 + */
1162 1216 public function get_course_meta( $id ) {
1163 - $user_id = get_current_user_id();
1164 -
1165 - if ( ! $user_id ) {
1217 + if ( empty( absint( $id ) ) ) {
1166 1218 return array();
1167 1219 }
1168 1220
1169 1221 if ( ! class_exists( 'LP_Meta_Box' ) ) {
@@ -1173,9 +1225,9 @@
1173 1225 if ( ! class_exists( 'LP_Meta_Box_Course' ) ) {
1174 1226 include_once LP_PLUGIN_PATH . 'inc/admin/views/meta-boxes/course/settings.php';
1175 1227 }
1176 1228
1177 - $metabox = new LP_Meta_Box_Course();
1229 + $metabox = LP_Meta_Box_Course::instance();
1178 1230
1179 1231 $output = array();
1180 1232
1181 1233 foreach ( $metabox->metabox( $id ) as $key => $tab ) {
@@ -1528,8 +1580,14 @@
1528 1580 'type' => 'array',
1529 1581 'context' => array( 'view' ),
1530 1582 'readonly' => true,
1531 1583 ),
1584 + 'statistic' => array(
1585 + 'description' => __( 'Total courses and students of the instructor.', 'learnpress' ),
1586 + 'type' => 'object',
1587 + 'context' => array( 'view' ),
1588 + 'readonly' => true,
1589 + ),
1532 1590 ),
1533 1591 ),
1534 1592 ),
1535 1593 'sections' => array(
@@ -1570,8 +1628,13 @@
1570 1628 'context' => array( 'view', 'edit' ),
1571 1629 ),
1572 1630 'type' => array(
1573 1631 'description' => __( 'Item Type.', 'learnpress' ),
1632 + 'type' => 'string',
1633 + 'context' => array( 'view', 'edit' ),
1634 + ),
1635 + 'slug' => array(
1636 + 'description' => __( 'Item slug.', 'learnpress' ),
1574 1637 'type' => 'string',
1575 1638 'context' => array( 'view', 'edit' ),
1576 1639 ),
1577 1640 'title' => array(