Skip to content

Instantly share code, notes, and snippets.

@Romain
Last active October 28, 2019 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Romain/000454d9f297ff71e0abf5a61837c22f to your computer and use it in GitHub Desktop.
Save Romain/000454d9f297ff71e0abf5a61837c22f to your computer and use it in GitHub Desktop.
SkyVerge Code Review
<?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? ==
* => Ideally, the courses would be collected asynchronously, with an AJAX request, and then, the array would be populated with the result.
* => Below is just a small change to improve the performance of the actual loop.
*/
public function add_my_courses_section() {
global $current_user;
$api_user_id = get_user_meta( current_user->ID, '_external_api_user_id', true );
if ( !$api_user_id ) {
return;
}
$courses = $this->get_api()->get_courses_assigned_to_user( $api_user_id );
$sso_link = $this->get_api()->get_sso_link( $api_user_id );
?>
<!-- Replaced print by echo -->
<h2 style="margin-top: 40px;"><?php echo __( 'My Courses', 'text-domain' ); ?></h2>
<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
// Using $key / $value syntax seems to be faster by ~50% than the original version, based on this source: https://www.phpbench.com/
foreach( $courses as $key => $value ) :
?>
<tr>
<td><?php echo __( $value['Code'] ); ?></td>
<td><?php echo __( $value['Name'] ); ?></td>
<td><?php echo __( $value['PercentageComplete'] ); ?> &#37;</td>
<td><?php echo __( $value['DateCompleted'] ); ?></td>
<tr/><!-- Added the missing </tr> tag -->
<?php endforeach; ?>
</tbody>
</table>
<!-- Removed echo $_GET['active_course']; that will probably not have an effect, and doesn't make sense this we're not looking at one specific course -->
<p><a href="<?php echo $sso_link ?>" target="_blank" title="<?php echo __( 'Course Login', 'text-domain' ); ?>" class="button"><?php echo __( '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