Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sandeepjain2015/90f830f51787a59e05fcc57832836aab to your computer and use it in GitHub Desktop.
Save sandeepjain2015/90f830f51787a59e05fcc57832836aab to your computer and use it in GitHub Desktop.
We can solve loading issue by cache
<?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.
*
* == By using cache ==
*/
public function add_my_courses_section() {
global $current_user;
$api_user_id = get_user_meta( current_user->ID, '_external_api_user_id', true );
if ( !$api_user_id ) {
return;
}
$cache_name = sanitize_key( md5( 'course_key' . $api_user_id ) );
$cache_group = 'course_group';
// get cache here.
$courses_data = wp_cache_get( $cache_name, $cache_group );
if ( empty( $courses_data ) ) {
$courses = $this->get_api()->get_courses_assigned_to_user( $api_user_id );
$sso_link = $this->get_api()->get_sso_link( $api_user_id );
ob_start();
?>
<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'] ); ?> &#37;</td>
<td><?php echo __( $course['DateCompleted'] ); ?></td>
<?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
$courses_data = ob_get_clean();
// set cache here.
wp_cache_set( $cache_name, $courses_data, $cache_group, 30 * MINUTE_IN_SECONDS );
}
echo wp_kses_post( $courses_data );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment