Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * This code retrieves course data from an external API and displays it in the user's
- * My Account area. A merchant has noticed that there's a delay when loading the page.
- *
- * 1) What changes would you suggest to reduce or remove that delay?
- * 2) Is there any other code changes that you would make?
- */
- public function add_my_courses_section() {
- $api_user_id = get_user_meta( get_current_user_id(), '_external_api_user_id', true );
- if ( ! $api_user_id ) {
- return;
- }
- // set $this->get_api() to its own variable so we only connect once to reduce the delay waiting for another connection
- $api_data = $this->get_api();
- $courses = $api_data->get_courses_assigned_to_user( $api_user_id );
- $sso_link = $api_data->get_sso_link( $api_user_id );
- ?>
- <?php // Removed the style margin-top, not really for a performance thing. more organized for me. Ideally goes in a CSS file. ?>
- <h2><?php echo __( 'My Courses', 'text-domain' ); ?></h2>
- <?php // check that we have courses so we don't get a foreach loop error with an empty array ?>
- <?php if ( count($courses) > 0 ) : ?>
- <table>
- <thead>
- <tr>
- <th><?php echo __( 'Course Code', 'text-domain' ); ?></th>
- <th><?php echo __( 'Course Title', 'text-domain' ); ?></th>
- <th><?php echo __( 'Completion', 'text-domain' ); ?></th>
- <th><?php echo __( 'Date Completed', 'text-domain' ); ?></th>
- </tr>
- </thead>
- <tbody>
- <?php foreach( $courses as $course ) : ?>
- <tr>
- <td><?php echo __( $course['Code'] ); ?></td>
- <td><?php echo __( $course['Name'] ); ?></td>
- <td><?php echo __( $course['PercentageComplete'] ); ?> %</td>
- <td><?php echo __( $course['DateCompleted'] ); ?></td>
- </tr>
- <?php endforeach; ?>
- </tbody>
- </table>
- <?php else: ?>
- <?php echo __( 'You have not registered or purchased any courses.', 'text-domain' ); ?>
- <?php endif; ?>
- <?php // Added rel noopener and noreferrer for security ?>
- <p><a href="<?php echo $sso_link ?>" target="_blank" rel="noopener noreferrer" class="button <?php echo $_GET['active_course']; ?>">
- <?php echo __( 'Course Login', 'text-domain' ); ?>
- </a></p>
- <?php
- }
Add Comment
Please, Sign In to add comment