Skip to content

Instantly share code, notes, and snippets.

@mehulkaklotar
Created April 19, 2019 06:54
Show Gist options
  • Save mehulkaklotar/f6fefd78d32e900671146cd21405e096 to your computer and use it in GitHub Desktop.
Save mehulkaklotar/f6fefd78d32e900671146cd21405e096 to your computer and use it in GitHub Desktop.
Course data from an external API and displays it in the user's account area
/**
* 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() {
// get external api user id from user's meta for current user
$api_user_id = get_user_meta( get_current_user_id(), '_external_api_user_id', true );
// return if api user id is not set or nothing found
if ( ! $api_user_id ) {
return;
}
// get courses for the user
$courses = $this->get_api()->get_courses_assigned_to_user( $api_user_id );
// get sso link to the user
$sso_link = $this->get_api()->get_sso_link( $api_user_id );
?>
<h2 style="margin-top: 40px;"><?php _e( 'My Courses', 'text-domain' ); ?></h2>
<table>
<thead>
<tr>
<th><?php _e( 'Course Code', 'text-domain' ); ?></th>
<th><?php _e( 'Course Title', 'text-domain' ); ?></th>
<th><?php _e( 'Completion', 'text-domain' ); ?></th>
<th><?php _e( 'Date Completed', 'text-domain' ); ?></th>
</tr>
</thead>
<tbody>
<?php
if ( ! empty( $courses ) ) :
foreach ( $courses as $course ) :
$code = ! empty( $course['Code'] ) ? $course['Code'] : '';
$name = ! empty( $course['Name'] ) ? $course['Name'] : '';
$percentageComplete = ! empty( $course['PercentageComplete'] ) ? $course['PercentageComplete'] : '';
$dateCompleted = ! empty( $course['DateCompleted'] ) ? $course['DateCompleted'] : '';
?>
<tr>
<td><?php echo $code; ?></td>
<td><?php echo $name; ?></td>
<td><?php echo $percentageComplete; ?> &#37;</td>
<td><?php echo $dateCompleted; ?></td>
</tr>
<?php endforeach;
else :
?>
<tr>
<td colspan="4"><?php _e( 'No courses found.', 'text-domain' ); ?></td>
</tr>
<?php
endif;
?>
</tbody>
</table>
<p>
<a href="<?php echo $sso_link; ?>" target="_blank" class="button <?php echo isset( $_GET['active_course'] ) ? $_GET['active_course'] : ''; ?>">
<?php _e( 'Course Login', 'text-domain' ); ?>
</a>
</p>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment