Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AhmedTheGeek/146fc913693bf92934b48567c43275c5 to your computer and use it in GitHub Desktop.
Save AhmedTheGeek/146fc913693bf92934b48567c43275c5 to your computer and use it in GitHub Desktop.
skyverge-php-engineer-code-review-1.php
<?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.
*
* == What changes would you suggest to reduce or remove that delay? ==
*/
public function add_my_courses_section() {
global $current_user;
//There was a missing $ before current_user
$api_user_id = get_user_meta( $current_user->ID, '_external_api_user_id', true );
if ( !$api_user_id ) {
return;
}
//These API calls are the biggest culprit to the performance issue
//Adding a limit to the call might help. Also pagination support will reduce the time needed to print the items in the loop below.
//The best solution is caching the data locally
$courses = $this->get_api()->get_courses_assigned_to_user( $api_user_id );
$sso_link = $this->get_api()->get_sso_link( $api_user_id );
$active_course = isset($_GET['active_course']) ? $_GET['active_course'] : "";
?>
<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
foreach( $courses as $course ) :
?><tr>
<td><?php _e( $course['Code'] ); ?></td>
<td><?php _e( $course['Name'] ); ?></td>
<td><?php _e( $course['PercentageComplete'] ); ?> &#37;</td>
<td><?php _e( $course['DateCompleted'] ); ?></td>
<?php endforeach;
?>
</tbody>
</table>
<p><a href="<?php echo $sso_link ?>" target="_blank" class="button <?php echo $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