Skip to content

Instantly share code, notes, and snippets.

@veekthoven
Last active March 17, 2021 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veekthoven/55fa90596a7a8915695b39b582f4822f to your computer and use it in GitHub Desktop.
Save veekthoven/55fa90596a7a8915695b39b582f4822f to your computer and use it in GitHub Desktop.
Demonstrating Transient
<?php
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_transient_key = 'user_courses_for_' . $api_user_id;
$sso_transient_key = 'sso_link_for_' . $api_user_id;
$courses = get_transient($courses_transient_key);
if (! $courses) {
$courses = $this->get_api()->get_courses_assigned_to_user($api_user_id);
$sso_link = $this->get_api()->get_sso_link($api_user_id);
//Cache it for a day
set_transient($courses_transient_key, $courses, DAY_IN_SECONDS);
set_transient($sso_transient_key, $sso_link, DAY_IN_SECONDS);
} else {
$sso_link = get_transient($sso_transient_key);
}
?>
<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