Guest User

Untitled

a guest
Jun 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. <?php
  2. /**
  3. * This code retrieves course data from an external API and displays it in the user's
  4. * My Account area. A merchant has noticed that there's a delay when loading the page.
  5. *
  6. * 1) What changes would you suggest to reduce or remove that delay?
  7. * 2) Is there any other code changes that you would make?
  8. */
  9. public function add_my_courses_section() {
  10.  
  11. $api_user_id = get_user_meta( get_current_user_id(), '_external_api_user_id', true );
  12.  
  13. if ( ! $api_user_id ) {
  14. return;
  15. }
  16.  
  17. // set $this->get_api() to its own variable so we only connect once to reduce the delay waiting for another connection
  18. $api_data = $this->get_api();
  19.  
  20. $courses = $api_data->get_courses_assigned_to_user( $api_user_id );
  21. $sso_link = $api_data->get_sso_link( $api_user_id );
  22.  
  23. ?>
  24.  
  25. <?php // Removed the style margin-top, not really for a performance thing. more organized for me. Ideally goes in a CSS file. ?>
  26. <h2><?php echo __( 'My Courses', 'text-domain' ); ?></h2>
  27.  
  28. <?php // check that we have courses so we don't get a foreach loop error with an empty array ?>
  29. <?php if ( count($courses) > 0 ) : ?>
  30. <table>
  31. <thead>
  32. <tr>
  33. <th><?php echo __( 'Course Code', 'text-domain' ); ?></th>
  34. <th><?php echo __( 'Course Title', 'text-domain' ); ?></th>
  35. <th><?php echo __( 'Completion', 'text-domain' ); ?></th>
  36. <th><?php echo __( 'Date Completed', 'text-domain' ); ?></th>
  37. </tr>
  38. </thead>
  39. <tbody>
  40. <?php foreach( $courses as $course ) : ?>
  41. <tr>
  42. <td><?php echo __( $course['Code'] ); ?></td>
  43. <td><?php echo __( $course['Name'] ); ?></td>
  44. <td><?php echo __( $course['PercentageComplete'] ); ?> &#37;</td>
  45. <td><?php echo __( $course['DateCompleted'] ); ?></td>
  46. </tr>
  47. <?php endforeach; ?>
  48. </tbody>
  49. </table>
  50. <?php else: ?>
  51. <?php echo __( 'You have not registered or purchased any courses.', 'text-domain' ); ?>
  52. <?php endif; ?>
  53.  
  54. <?php // Added rel noopener and noreferrer for security ?>
  55. <p><a href="<?php echo $sso_link ?>" target="_blank" rel="noopener noreferrer" class="button <?php echo $_GET['active_course']; ?>">
  56. <?php echo __( 'Course Login', 'text-domain' ); ?>
  57. </a></p>
  58.  
  59. <?php
  60. }
Add Comment
Please, Sign In to add comment