Skip to content

Instantly share code, notes, and snippets.

@Gummibeer
Created October 7, 2014 15:33
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 Gummibeer/2735309d3012f911962e to your computer and use it in GitHub Desktop.
Save Gummibeer/2735309d3012f911962e to your computer and use it in GitHub Desktop.
Generate a sitemap.xml and HTML-Sitemap Twig-Var for Pico - https://github.com/Techn0tic/Pico_Sitemap
<?php
/**
* Generate an xml sitemap for Pico
*
* @author Dave Kinsella
* @link https://github.com/Techn0tic/Pico_Sitemap
* @license http://opensource.org/licenses/MIT
*
* @author Tom Witkowski
* @link https://github.com/Gummibeer
*/
class Pico_Sitemap {
private $is_sitemap;
private $is_html_sitemap;
private $exclude;
private $pages;
public function __construct() {
$this->is_sitemap = false;
$this->is_html_sitemap = false;
$this->exclude = array('home', 'slider', 'suche', 'sitemap');
$this->pages = array();
}
public function request_url(&$url) {
if($url == 'sitemap') $this->is_html_sitemap = true;
if($url == 'sitemap.xml') $this->is_sitemap = true;
}
public function get_pages($pages) {
if($this->is_sitemap || $this->is_html_sitemap) {
global $config;
foreach($pages as $page) {
preg_match('/\/('.implode('|', $this->exclude).')\//', $page['url'].'/', $matches);
if(empty($matches)) {
$index = '';
$date = false;
$path = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'content'.DIRECTORY_SEPARATOR.str_ireplace($config['base_url'], '', $page['url']);
if(is_dir($path)) { $index = 'index'; }
$file = realpath($path.$index.'.md');
if(file_exists($file)) { $date = date ("Y-m-d", filemtime($file)); }
array_push($this->pages, array( 'url' => $page['url'], 'title' => $page['title'], 'lastmod' => $date, 'index' => $index == '' ? false : true ));
}
}
sort($this->pages);
}
}
public function before_render(&$twig_vars, &$twig, &$template) {
if($this->is_sitemap) {
header($_SERVER['SERVER_PROTOCOL'].' 200 OK');
header('Content-Type: application/xml; charset=UTF-8');
$xml = '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
foreach($this->pages as $page) {
$xml .= '<url><loc>'.$page['url'].'</loc><lastmod>'.$page['lastmod'].'</lastmod></url>';
}
$xml .= '</urlset>';
header('Content-Type: text/xml');
die($xml);
} elseif($this->is_html_sitemap) {
$panel_raw = '<div class="col-md-4"><div class="panel panel-primary"><div class="panel-heading"><h3 class="panel-title"><a href="%url%">%title%</a></h3></div><div class="list-group">%childs%</div></div></div>';
$child_raw = '<a href="%url%" class="list-group-item"><i class="icon-link"></i> %title%</a>';
$panel = '';
$childs = array();
$out = '';
$out .= '<div class="row">';
foreach($this->pages as $page) {
if($page['index']) {
$panel = str_replace('%childs%', implode('', $childs), $panel);
$out .= $panel;
$childs = array();
$panel = $panel_raw;
$panel = str_replace('%url%', $page['url'], $panel);
$panel = str_replace('%title%', $page['title'], $panel);
} else {
$child = $child_raw;
$child = str_replace('%url%', $page['url'], $child);
$child = str_replace('%title%', $page['title'], $child);
array_push($childs, $child);
}
}
$panel = str_replace('%childs%', implode('', $childs), $panel);
$out .= $panel;
$out .= '</div>';
$twig_vars['sitemap'] = $out;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment