IngmarBoddington
3/21/2013 - 8:42 PM

WordPress template for simply listing a user's Gists on GitHub

WordPress template for simply listing a user's Gists on GitHub

<?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(); ?>