Created
September 18, 2017 02:04
-
-
Save moksamedia/81fa84eb9f300e4baa8aedd47ac3f072 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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() { | |
$current_user_id = get_current_user_id(); | |
if ( ! $current_user_id ) { | |
return; | |
} | |
$api_user_id = get_user_meta($current_user_id, '_external_api_user_id', true ); | |
if ( ! $api_user_id ) { | |
return; | |
} | |
$sso_link = $this->get_api()->get_sso_link( $api_user_id ); | |
?> | |
<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> | |
</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> | |
<script> | |
// This would probably be stored in a config var or config constant | |
var apiUrl = "https://someurltoapi/user/courses"; | |
// Perform async request to API to get course data so page can load without waiting | |
// on external API | |
$.get( apiUrl, { api_user_id: "<?php echo $api_user_id?>" }, function(data) { | |
// Make sure DOM is loaded before we start inserting data into table | |
$(function() { | |
// Use jQuery to insert course data objects into table when loaded | |
}); | |
}) | |
.fail(function() { | |
// log error and flash message to user | |
}); | |
</script> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment