Last active
December 15, 2015 06:29
-
-
Save IngmarBoddington/5216543 to your computer and use it in GitHub Desktop.
WordPress template for simply listing a user's Gists on GitHub
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 | |
/** | |
Template Name: Gists | |
*/ | |
get_header(); ?> | |
<div id="container"> | |
<div id="content" role="main"> | |
<h1 class="entry-title">Gists</h1> | |
<hr /><br /> | |
<?php | |
$strUsername = 'IngmarBoddington'; | |
$strDomain = 'glowingminds.co.uk'; | |
echo '<p>An easy access list of my gists on GitHub. Mixed lot of references and code in various states.</p>' . PHP_EOL; | |
//Grab data | |
$curl = curl_init('https://api.github.com/users/' . $strUsername . '/gists'); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($curl, CURLOPT_USERAGENT, $strUsername . " gist listing on " . $strDomain); | |
$curlResult = curl_exec($curl); | |
$arrGists = array(); | |
if (($curlResult) && (curl_getinfo($curl, CURLINFO_HTTP_CODE) == '200')) { | |
//We have a response and a 200 resonse, decode | |
$arrGists = json_decode($curlResult); | |
} else { | |
//No data or cache, fail gracefully | |
echo '<p>Unable to retrieve Gists at this time.</p>' . PHP_EOL; | |
} | |
if ($arrGists) { | |
//Start table | |
echo ' | |
<table>'; | |
//Parse obj structure to get the information we want and output into table | |
foreach ($arrGists as $objGist) { | |
//Grab the file info out (all my gists are single files anyway...) | |
foreach ($objGist->files as $strFilename => $objFile) { | |
$strFilename = $objFile->filename; | |
$strType = $objFile->type; | |
} | |
//Format dates | |
$strCreated = date('D, jS F Y', strtotime($objGist->created_at)); | |
$strUpdated = date('D, jS F Y', strtotime($objGist->updated_at)); | |
echo ' | |
<tr> | |
<td> | |
<a href="' . htmlentities($objGist->html_url, ENT_QUOTES) . '">' . htmlentities($strFilename, ENT_QUOTES) . '</a><br /> | |
<span class="smaller"> | |
Created: ' . htmlentities($strCreated, ENT_QUOTES) .'<br /> | |
Updated: ' . htmlentities($strUpdated, ENT_QUOTES) . ' | |
</span> | |
</td> | |
<td>' . htmlentities($objGist->description, ENT_QUOTES) . '</td> | |
</tr>' . PHP_EOL; | |
} | |
//Close table | |
echo '</table>'; | |
} | |
?> | |
</div> | |
</div> | |
<?php get_sidebar(); ?> | |
<?php get_footer(); ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment