LearnPress / learnpress

LearnPress WordPress LMS Plugin by ThimPress
https://thimpress.com/learnpress
240 stars 140 forks source link

API users courses bug #568

Closed Ananasick closed 1 month ago

Ananasick commented 3 months ago

I apologize in advance for any possible errors in formatting, this is my first issue :) To be clear: i have already check your developer branch, and it contain same error.

I have found a bug, when i try get courses per user with REST API, there is no "content" attribute. I have found 2 errors in your code (it's just a hot fix, i hope you can find a better way to fix it ) :

  1. False name tabs and content array in file /inc/jwt/rest-api/version1/class-lp-rest-users-v1-controller.php Function _learn_press_get_user_profiletabs return follow tabs: ['my-courses', 'quizzes', 'orders', 'order-details', 'settings', 'logout', ] . But there is array $content in function _get_lp_datatabs that is then distributed to the tabs. Also missing attribute in function _get_course_tabcontents instead $request to get profile should be $user (or $user->ID) So fixed function _get_lp_datatabs should be like this:

    1137 |  public function get_lp_data_tabs( $user, $request ): array {
    1138 |      $output = array();
    1139 |
    1140 |      if ( get_current_user_id() === $user->ID || current_user_can( 'list_users' ) ) {
    1141 |          if ( function_exists( 'learn_press_get_user_profile_tabs' ) ) {
    1142 |              $tabs = learn_press_get_user_profile_tabs();
    1143 |              $content = array(
    1144 |                  'overview' => $this->get_overview_tab_contents( $user ),
    1145  - |                               'courses'  => $this->get_course_tab_contents( $request ),
    1145  + |               'my-courses'  => $this->get_course_tab_contents( $request, $user ),
    1146 |                  'quizzes'  => $this->get_quiz_tab_contents( $request ),
    1147 |                  'orders'   => $this->get_order_content_tab( $request ),
    1148 |              );
    ...
  2. Also change _get_course_tabcontents function in follow way:

    653 - |        public function get_course_tab_contents( $request ) {
    653 + |        public function get_course_tab_contents( $request, $user ) {
    ...
    659 - |     $profile          = learn_press_get_profile( $request['id'] );
    659 + |     $profile          = learn_press_get_profile( $user->ID );
    660 - |     $user             = learn_press_get_user( $request['id'] );
    660 + |     $user             = learn_press_get_user( $user->ID );
    ...

    I hope it will be helpful!

P.S.: I changed $request['id'] to $user->ID because $user->ID work with every request (all users or specific user), instead of $request['id'] with work only with specific users. P.S.S: Also quizzes i can not became, when i try to get all users, but when i try to get specific user i see all quizzes

tungnxt89 commented 2 months ago

Hi Ananasick,

Thanks for your feedback, we'll check and fix it.

Thank so much. Best Regard!