Skip to content

Instantly share code, notes, and snippets.

@GaryJones
Created December 8, 2011 11:40
Show Gist options
  • Save GaryJones/1446779 to your computer and use it in GitHub Desktop.
Save GaryJones/1446779 to your computer and use it in GitHub Desktop.
<?php
/**
* Allows XHTML sitemap to be added
*
* @package GT_Sitemap
* @author Gary Jones
* @version 2010-05-19
* @since 2010-03-20
*/
class GT_Sitemap {
/**
* @var string
*/
protected $_pagesText;
/**
* @var string
*/
protected $_postsText;
/**
* @var string
*/
protected $_archivesText;
/**
* @var int
*/
protected $_headingLevel;
/**
* @var array
*/
protected $_order = array( );
/**
* @var bool
*/
protected $_showPageDate;
/**
* @var string
*/
protected $_showPostDate;
/**
* @var bool
*/
protected $_showPostCount;
/**
* @var string
*/
protected $_archivesType;
/**
* @var string
*/
protected $_pagesDateFormat;
/**
* @var string
*/
protected $_postsDateFormat;
/**
* @var string
*/
protected $_customPagesQuery = '';
/**
* PHP5 constructor, setting defaults
*/
public function __construct() {
$this->_pagesText = 'Pages';
$this->_postsText = 'Posts';
$this->_archivesText = 'Monthly Archives';
$this->_headingLevel = 3;
$this->_order = array( 'pages', 'posts', 'archives' );
$this->_showPageDate = true;
$this->_showPostDate = 'published';
$this->_showPostCount = true;
$this->_archivesType = 'monthly';
$this->_pagesDateFormat = get_option( 'date_format' );
$this->_postsDateFormat = get_option( 'date_format' );
$this->_customPagesQuery = '';
}
/**
* @param string $id
*/
public function setPagesText( $id ) {
$this->_pagesText = $id;
return $this;
}
/**
* @param string $id
*/
public function setPostsText( $id ) {
$this->_postsText = $id;
return $this;
}
/**
* @param string $id
*/
public function setArchivesText( $id ) {
$this->_archivesText = $id;
return $this;
}
/**
* @param array $id
*/
public function setOrder( $arg1, $arg2 = null, $arg3 = null ) {
$this->_order = func_get_args();
return $this;
}
/**
* @param int $id
*/
public function setHeadingLevel( $id ) {
$this->_headingLevel = $id;
return $this;
}
/**
* @param string $id
*/
public function setPageDateFormat( $id ) {
if ( 0 === func_num_args() ) {
$this->_showPageDate = '';
} else {
$this->_pagesDateFormat = $id;
}
return $this;
}
/**
* @param string $id
*/
public function setPostDateFormat( $id ) {
if ( 0 === func_num_args() ) {
$this->_showPostDate = '';
} else {
$this->_postsDateFormat = $id;
}
return $this;
}
/**
* @param string $id
*/
public function setDateFormat( $id ) {
$this->setPageDateFormat( $id );
$this->setPostDateFormat( $id );
return $this;
}
/**
*
*/
public function hidePostCount() {
$this->_showPostCount = false;
return $this;
}
/**
* @param string $id
*/
public function setArchivesType( $id ) {
$archiveTypes = array( 'yearly', 'monthly', 'daily', 'weekly', 'postbypost', 'alpha' );
if ( in_array( $id, $archiveTypes ) ) {
$this->_archivesType = $id;
$this->setArchivesText( substr_replace( $id, strtoupper( substr( $id, 0, 1 ) ), 0, 1 ) . ' Archives' );
}
return $this;
}
/**
* @param string $id
*/
function setCustomPagesQuery( $id ) {
$this->_customPagesQuery = $id;
return $this;
}
/**
* @param string $shortcode The shortcode keyword that will be used to output the sitemap
*/
public function shortcode( $shortcode ) {
add_shortcode( $shortcode, array( &$this, 'build' ) );
}
/**
* Does the main work of creating the output
*/
public function build() {
foreach ( $this->_order as $section ) {
if ( 'pages' === $section ) {
$output .= '<' . $this->_headingLevel . '>' . $this->_pagesText . '<' . $this->_headingLevel . '>' . "\n"
. '<ul>' . "\n" . wp_list_pages( 'echo=0&show_date=' . $this->_showPageDate . '&date_format=' . $this->_pagesDateFormat . '&title_li=&' . $this->_customPagesQuery ) . '</ul>' . "\n";
}
if ( 'posts' === $section ) {
$output .= '<' . $this->_headingLevel . '>' . $this->_postsText . '<' . $this->_headingLevel . '>' . "\n"
. '<ul>' . "\n" . $this->_posts_by_category() . '</ul>' . "\n";
}
if ( 'archives' === $section ) {
$output .= '<' . $this->_headingLevel . '>' . $this->_archivesText . '<' . $this->_headingLevel . '>' . "\n"
. '<ul>' . "\n" . wp_get_archives( 'type=' . $this->_archivesType . '&echo=0&show_post_count=' . $this->_showPostCount ) . '</ul>' . "\n";
}
}
return $output;
}
protected function _posts_by_category() {
global $wpdb, $post;
$tp = $wpdb->prefix;
$sort_code = 'ORDER BY name ASC, post_date DESC';
$the_output = NULL;
$last_posts = (array) $wpdb->get_results( "SELECT {$tp}terms.name, {$tp}terms.term_id, {$tp}term_taxonomy.term_taxonomy_id FROM {$tp}terms, {$tp}term_taxonomy WHERE {$tp}terms.term_id = {$tp}term_taxonomy.term_id AND {$tp}term_taxonomy.taxonomy = 'category'" );
if ( ! $last_posts )
return;
$the_output .= '';
$used_cats = array( );
$i = 0;
foreach ( $last_posts as $posts ) {
if ( in_array( $posts->name, $used_cats ) )
unset( $last_posts[$i] );
else
$used_cats[] = $posts->name;
$i++;
}
$last_posts = array_values( $last_posts );
foreach ( $last_posts as $posts ) {
$the_output .= '<li><a href="' . get_page_link( $posts->term_id ) . '"><strong>' . apply_filters( 'list_cats', $posts->name, $posts ) . '</strong></a><ul>';
$arcresults = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' AND ID IN (SELECT object_id FROM {$tp}term_relationships, {$tp}terms WHERE {$tp}term_relationships.term_taxonomy_id =" . $posts->term_taxonomy_id . ") ORDER BY post_date DESC" );
foreach ( $arcresults as $arcresult ) {
$the_output .= '<li><a href="' . get_page_link($arcresult->ID) . '">' . apply_filters('the_title', $arcresult->post_title) . '</a>';
if ($this->_showPostDate)
$the_output .= date($this->_postsDateFormat,strtotime($arcresult->post_date));
$the_output .= '</li>';
}
$the_output .= '</ul></li>';
}
return $the_output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment