Skip to content

Instantly share code, notes, and snippets.

@jmramos02
Created July 11, 2019 13:33
Show Gist options
  • Select an option

  • Save jmramos02/ec01b024671cc9f2772bddf253390c4b to your computer and use it in GitHub Desktop.

Select an option

Save jmramos02/ec01b024671cc9f2772bddf253390c4b to your computer and use it in GitHub Desktop.
<?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;
}
$courses_request = [
'url' => 'https://some.url/to/get/courses',
'type' => 'GET',
];
$sso_link_request = [
'url' => 'https://some.url/to/get/sso_link',
'type' => 'GET',
];
$responses = Requests::request_multiple([ $sso_link_request, $courses_request ] );
$data = [];
foreach ( $responses as $response ){
if( is_a( $response, 'Requests_Response' ) ){
$data[] = json_decode( $response->body );
}
}
$sso_link = $data[2]
$courses = $data[1]
?>
<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
foreach( $courses as $course ) :
?><tr>
<td><?php echo __( $course['Code'] ); ?></td>
<td><?php echo __( $course['Name'] ); ?></td>
<td><?php echo __( $course['PercentageComplete'] ); ?> &#37;</td>
<td><?php echo __( $course['DateCompleted'] ); ?></td>
</tr>
<?php endforeach;
?>
</tbody>
</table>
<p><a href="<?php echo $sso_link ?>" target="_blank" class="button <?php echo $_GET['active_course']; ?>"><?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