Forked from maxrice/jilt-marketing-engineer-code-review-1.php
Last active
March 26, 2020 04:43
-
-
Save lideo/49a6b0a0500d4a59cf671f0487180f6e to your computer and use it in GitHub Desktop.
WP course list
This file contains hidden or 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 | |
class Some_Class { | |
/** | |
* 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() { | |
// Use the WordPress function get_current_user_id | |
// to get the customer id instead of using a global | |
$api_user_id = get_user_meta( get_current_user_id(), '_external_api_user_id', true ); | |
if ( !$api_user_id ) { | |
return; | |
} | |
$courses = $this->fetch_api_data( $api_user_id, 'user_courses_cache', 'get_courses_assigned_to_user' ); | |
$sso_link = $this->fetch_api_data( $api_user_id, 'sso_link_cache', 'get_courses_assigned_to_user' ); | |
$active_course = $_GET['active_course']; | |
// I moved the HTML code to a separate template file. | |
// I prefer not to have HTML inside PHP. | |
// Set variables needed in the template | |
set_query_var( 'courses', $courses ); | |
set_query_var( 'sso_link', $sso_link ); | |
set_query_var( 'active_course', $active_course ); | |
// Start the buffer | |
ob_start(); | |
// Get the content from the template | |
get_template_part( 'courses' ); | |
// Store the content of the buffer and delete it | |
$output = ob_get_clean(); | |
echo $output; | |
} | |
private function fetch_api_data( $api_user_id, $cache_key, $method ) { | |
// Cache the result of the API calls using transients | |
// to reduce the delay. | |
// I set the expiration time quite arbitrarily. | |
$result = get_transient( $cache_key ); | |
if ( false === $result ) { | |
$result = $this->get_api()->{$method}( $api_user_id ); | |
set_transient( $cache_key, $result, 1 * HOUR_IN_SECONDS ); | |
} | |
return $result; | |
} | |
} |
This file contains hidden or 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 | |
$courses = get_query_var( 'courses' ); | |
$sso_link = get_query_var( 'sso_link' ); | |
$active_course = get_query_var( 'active_course' ); | |
?> | |
<h2 style="margin-top: 40px;"><?php print __( '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'] ); ?> %</td> | |
<td><?php echo __( $course['DateCompleted'] ); ?></td> | |
<?php endforeach; | |
?> | |
</tbody> | |
</table> | |
<p><a href="<?php echo $sso_link; ?>" target="_blank" class="button <?php echo $active_course; ?>"><?php echo __( 'Course Login', 'text-domain' ); ?></a></p> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment