Skip to content

Instantly share code, notes, and snippets.

@danishiqbal4
Last active March 3, 2020 07:01
Show Gist options
  • Save danishiqbal4/f9a538530636f82dab6b1b965a8eb8b5 to your computer and use it in GitHub Desktop.
Save danishiqbal4/f9a538530636f82dab6b1b965a8eb8b5 to your computer and use it in GitHub Desktop.
A Gist for Sky Verge PHP Engineer
<?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;
// $ missing with 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 );
?>
<!-- echo is marginally faster than print
But the WordPress docs recommend to use _e instead of echo for translations -->
<!-- Inline CSS on H2 is not good -->
<h2><?php _e( 'My Courses', 'text-domain' ); ?></h2>
<table>
<thead><tr>
<!-- _e instead of echo for translations -->
<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>
<!-- We need to escape the output for HTML safety
We can add text domain to it too if necessary -->
<td><?php esc_html_e( $course['Code'] ); ?></td>
<td><?php esc_html_e( $course['Name'] ); ?></td>
<td><?php esc_html_e( $course['PercentageComplete'] ); ?> &#37;</td>
<td><?php esc_html_e( $course['DateCompleted'] ); ?></td>
<?php endforeach;
?>
</tbody>
</table>
<!-- Escape the URL
Escape the class name
Replace echo with _e -->
<p><a href="<?php echo esc_url($sso_link) ?>" target="_blank" class="button <?php echo esc_html($_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