Skip to content

Instantly share code, notes, and snippets.

@deivamagalhaes
Last active April 17, 2019 00:38
Show Gist options
  • Save deivamagalhaes/24e4c9031fe55525eb9e0988fd250a87 to your computer and use it in GitHub Desktop.
Save deivamagalhaes/24e4c9031fe55525eb9e0988fd250a87 to your computer and use it in GitHub Desktop.
SkyVerge - PHP Engineer - How would you improve this code? Please do not fork this Gist.
<?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?
* I've refactored it to retrieve the data from the API using AJAX, so that this does not block the page.
*
* 2) Is there any other code changes that you would make?
* I'd also make the AJAX call secure (nonce) and move the JS and HTML to separate files
* (to separate presentation from logic and to make it easier to read).
*/
/**
* Creates a new instance of this class.
* If there is already a construct, add these lines instead of creating this function (obviously).
*/
public function __construct() {
// Add AJAX actions.
add_action( 'wp_ajax_get_courses_table', [ $this, 'get_courses_table' ] );
add_action( 'wp_ajax_get_course_sso_link', [ $this, 'get_course_sso_link' ] );
// Add custom JavaScript.
add_action( 'wp_footer', [ $this, 'enqueue_scripts' ] );
}
/**
* Get the API user ID from the current user metadata.
*/
protected funcion get_api_user_id() {
return get_user_meta( get_current_user_id(), '_external_api_user_id', true );
}
/**
* Retrieves courses from the external API and returns an HTML table.
*/
public function get_courses_table() {
// Get the API user ID from the user metadata.
$api_user_id = $this->get_api_user_id();
// Retrieve the courses from the external API.
$courses = $this->get_api()->get_courses_assigned_to_user( $api_user_id );
// Echo the HTML.
?>
<table id="courses-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>
<?php
die();
}
/**
* Retrieves a course's SSO link from the external API and returns an HTML link.
*/
public function get_course_sso_link() {
// Get the API user ID from the user metadata.
$api_user_id = $this->get_api_user_id();
// Get the active course from the sent data (used for style purposes).
$active_course = $_POST['active_course'];
// Retrieve the SSO link from the external API.
$sso_link = $this->get_api()->get_sso_link( $api_user_id );
// Echo the HTML.
?>
<p id="course-login"><a href="<?php echo $sso_link ?>" target="_blank" class="button <?php echo $active_course ?>"><?php echo __( 'Course Login', 'text-domain' ); ?></a></p>
<?php
die();
}
/**
* Add custom JavaScript to call AJAX functions.
*/
public function enqueue_scripts() {
?>
<script type="text/javascript">
// Get courses table and update the HTML.
jQuery.ajax({
url: '<?php echo admin_url( 'admin-ajax.php' ); ?>'
type: 'post',
data: {
action: 'get_courses_table'
},
success: function( response ) {
jQuery('#courses-table').replaceWith(response);
}
});
// Get active course link and update the HTML.
jQuery.ajax({
url: '<?php echo admin_url( 'admin-ajax.php' ); ?>'
type: 'post',
data: {
action: 'get_course_sso_link',
active_course: '<?php echo $_GET['active_course']; ?>'
},
success: function( response ) {
jQuery('#course-login').replaceWith(response);
}
});
</script>
<?php
}
public function add_my_courses_section() {
// Get the API user ID from the user metadata.
$api_user_id = $this->get_api_user_id();
if ( ! $api_user_id ) {
return;
}
// Print empty table.
?>
<h2 style="margin-top: 40px;"><?php echo __( 'My Courses', 'text-domain' ); ?></h2>
<table id="courses-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><tr>
<td colspan="4">Loading...</td>
</tr></tbody>
</table>
<p id="course-login">Loading...</p>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment