WordPress Sitemap Class for PHP 4

Please see WordPress Sitemap Class for the main write-up and code examples.

The class file is slightly different to cope with PHP 4 limitations, but with the same class features. There’s one main difference in it’s usage, and that it that methods can’t chain together:

require_once 'classes/GT_Sitemap.php';
$sitemap = new GT_Sitemap;
$sitemap->setOrder('posts', 'pages');
$sitemap->setDateFormat('');
$sitemap->setCustomPagesQuery('exclude=812');
$sitemap->shortcode('sitemap');


The Class File for PHP4

<?php
/**
 * Allows XHTML sitemap to be added. Suitable for PHP 4.x
 *
 * @package GT_Sitemap
 * @author Gary Jones
 * @version 2010-04-26
 * @since 2010-03-20
 */
class GT_Sitemap
{

	/**
	 * @var string
	 */
	var $_pagesText;

	/**
	 * @var string
	 */
	var $_postsText;

	/**
	 * @var string
	 */
	var $_archivesText;

	/**
	 * @var int
	 */
	var $_headingLevel;

	/**
	 * @var array
	 */
	var $_order = array();

	/**
	 * @var bool
	 */
	var $_showPageDate;

	/**
	 * @var string
	 */
	var $_showPostDate;

	/**
	 * @var bool
	 */
	var $_showPostCount;

	/**
	 * @var string
	 */
	var $_archivesType;

	/**
	 * @var string
	 */
	var $_pagesDateFormat;

	/**
	 * @var string
	 */
	var $_postsDateFormat;

	/**
	 * @var string
	 */
	var $_customPagesQuery = '';

	/**
     * PHP4 compatible constructor
     */
  function GT_Sitemap() {
      if(version_compare(PHP_VERSION,"5.0.0","__construct();
      }
    }

	/**
	 * PHP5 constructor, setting defaults
	 */
	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');
	}

	/**
	 * @param string $id
	 */
	function setPagesText($id) {
	  $this->_pagesText = $id;
	  return $this;
	}

	/**
	 * @param string $id
	 */
	function setPostsText($id) {
	  $this->_postsText = $id;
	  return $this;
	}

	/**
	 * @param string $id
	 */
	function setArchivesText($id) {
	  $this->_archivesText = $id;
	  return $this;
	}

	/**
	 * @param array $id
	 */
	function setOrder($arg1, $arg2 = null, $arg3 = null) {
    $this->_order = func_get_args();
	  return $this;
	}

	/**
	 * @param int $id
	 */
	function setHeadingLevel($id) {
	  $this->_headingLevel = $id;
	  return $this;
	}

	/**
	 * @param string $id
	 */
	function setPageDateFormat($id) {
	  if ( 0 === func_num_args() ) {
	    $this->_showPageDate = '';
	  } else {
	    $this->_pagesDateFormat = $id;
	  }
	  return $this;
	}

	/**
	 * @param string $id
	 */
	function setPostDateFormat($id) {
	  if ( 0 === func_num_args() ) {
	    $this->_showPostDate = '';
	  } else {
	    $this->_postsDateFormat = $id;
	  }
	  return $this;
	}

	/**
	 * @param string $id
	 */
	function setDateFormat($id) {
	  $this->setPageDateFormat($id);
	  $this->setPostDateFormat($id);
	  return $this;
	}

	/**
	 *
	 */
	function hidePostCount() {
	  $this->_showPostCount = false;
	  return $this;
	}

	/**
	 * @param string $id
	 */
	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($pagesQuery) {
	    $this->_customPagesQuery = $pagesQuery;
	    return $this;
	}

	/**
	 * @param string $shortcode The shortcode keyword that will be used to output the sitemap
	 */
	function shortcode($shortcode) {
		add_shortcode( $shortcode, array(&$this, 'build') );
	}

	/**
	 * Does the main work of creating the output
	 */
	function build() {
	  foreach ($this->_order as $section) {

	    if ( 'pages' === $section ) {
	      $output .= '_headingLevel . '>' . $this->_pagesText . '_headingLevel . '>' . "\n"
	      . '<ul>' . "\n" . wp_list_pages('echo=0&amp;show_date=' . $this-&gt;_showPageDate . '&amp;date_format=' . $this-&gt;_pagesDateFormat . '&amp;title_li=&amp;' . $this-&gt;_customPagesQuery) . '</ul>' . "\n";
	    }

	    if ( 'posts' === $section ) {
	      $output .= '_headingLevel . '&gt;' . $this-&gt;_postsText . '_headingLevel . '&gt;'."\n"
        . '<ul>' . "\n" . $this-&gt;_posts_by_category() . '</ul>' . "\n";
      }

      if ( 'archives' === $section ) {
        $output .= '_headingLevel . '&gt;' . $this-&gt;_archivesText . '_headingLevel . '&gt;' . "\n"
        . '<ul>' . "\n" . wp_get_archives('type=' . $this-&gt;_archivesType . '&amp;echo=0&amp;show_post_count=' . $this-&gt;_showPostCount). '</ul>' . "\n";
      }
    }
    return $output;

	}

	function _posts_by_category() {
	  global $wpdb, $post;
	  $tp = $wpdb-&gt;prefix;
	  $sort_code = 'ORDER BY name ASC, post_date DESC';
	  $the_output = NULL;
	  $last_posts = (array)$wpdb-&gt;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 (empty($last_posts)) {
	    return NULL;
	  }
	  $the_output .= '';
	  $used_cats = array();
	  $i = 0;

	  foreach ($last_posts as $posts) {
  		if (in_array($posts-&gt;name, $used_cats)) {
			  unset($last_posts[$i]);
		  } else {
  			$used_cats[] = $posts-&gt;name;
		  }
		  $i++;
	  }
	  $last_posts = array_values($last_posts);

  	foreach ($last_posts as $posts) {
	    $the_output .= '<li><a>term_id) . '"&gt;<strong>' . apply_filters('list_cats', $posts-&gt;name, $posts) . '</strong></a><ul>';
	    $arcresults = $wpdb-&gt;get_results("SELECT * FROM $wpdb-&gt;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-&gt;term_taxonomy_id . ") ORDER BY post_date DESC");
	    foreach ( $arcresults as $arcresult ) {
  	    $the_output .= '<li><a>ID) . '"&gt;' . apply_filters('the_title', $arcresult-&gt;post_title) . '</a> ';
  	    if ($this-&gt;_showPostDate) {
  	      $the_output .= date($this-&gt;_postsDateFormat,strtotime($arcresult-&gt;post_date));
  	    }
  	    $the_output .= '</li>';
	    }
      $the_output .= '</ul></li>';
    }
    return $the_output;
  }
}

Trackbacks

  1. [...] only for PHP5 installations, and WILL cause errors if you have PHP4. Luckily, you can use the version for PHP4 instead [...]

Speak Your Mind

*