__construct();
}
// PHP 5
function __construct() {
// Pre-load Options
$this->load_options();
if (!$this->opts) { $this->load_default_options(); }
// Launch
if (!is_admin()) {
add_action('init', array(&$this,'public_init'));
} else {
add_action('init', array(&$this,'admin_init'));
}
}
/**
* Option helper functions
*/
function load_default_options() {
$this->opts = array(
'sitemap_page' => false,
'sel_sitemap_position' => 'after',
'sel_use_title' => 'original',
'exclude_pages' => array(),
'exclude_cats' => array(),
);
}
function load_options() {
$this->opts = get_option($this->option_key);
}
function save_options($options = false) {
if (!$options) { $options = $this->opts; }
update_option($this->option_key, $options);
}
/**
* Check if 'All In One SEO' Plugin is active
*/
function check_allineoneseo() {
// Flag so that we only run once...
if (isset($this->flag_allineoneseo)) return $this->flag_allineoneseo;
// Check active plugins
$plugins = get_option('active_plugins');
$this->flag_allineoneseo = (in_array('all-in-one-seo-pack/all_in_one_seo_pack.php', $plugins)) ? true : false;
return $this->flag_allineoneseo;
}
/**
* Public functions
*/
function public_init() {
add_action('template_redirect', array(&$this,'template_redirect'));
}
function template_redirect() {
// Register Shortcode
add_shortcode($this->shortcode, array(&$this,'handle_shortcode'));
// Only filter content if a page was selected
if (empty($this->opts['sitemap_page']) || !is_page($this->opts['sitemap_page'])) return;
add_filter('the_content', array(&$this,'do_sitemap'));
}
// Shortcode Handler: view=pages|posts
function handle_shortcode($atts=false) {
extract(shortcode_atts(array(
'view' => 'all'
), $atts));
ob_start();
switch ($view) {
case 'pages': $this->sitemap_show_pages(); break;
case 'posts': $this->sitemap_show_posts(); break;
default: echo $this->do_sitemap(); break;
}
return ob_get_clean();
}
// Returns "Regular Page Content"+sitemap
function do_sitemap($content='') {
ob_start();
// List Pages
echo '
Pages '."\n";
$this->sitemap_show_pages();
// List Posts
echo 'Posts '."\n";
$this->sitemap_show_posts();
$output = ob_get_clean();
// Decide where the $content goes. Defaults to 'after' if option not set.
if ($this->opts['sel_sitemap_position'] == 'before') {
$content = $output.$content;
} else {
$content = $content.$output;
}
// Return Output
return $content;
}
// Output pages as UL/LI
function sitemap_show_pages($parent=0, $type='page', $class='sitemap_pages') {
if (!is_array($this->opts['exclude_pages'])) $this->opts['exclude_pages'] = array();
ob_start();
$posts = get_posts(array(
'post_type' => $type,
'orderby' => 'menu_order',
'numberposts' => -1,
'post_parent' => $parent,
'post__not_in' => $this->opts['exclude_pages'],
));
foreach ($posts as $post) {
// $label = apply_filters('the_title',$post->post_title);
$label = $this->sitemap_postitem_title($post);
echo ''.$label.' ';
$this->sitemap_show_pages($post->ID, 'page', 'sitemap_pages');
echo ' '."\n";
}
$items = ob_get_clean(); // Doing buffering now to prevent outputting empty elements.
if (!empty($items)) echo '\n";
}
// Output psots as UL/LI
function sitemap_show_posts() {
echo ''."\n";
$posts = get_posts(array(
'category__not_in' => $this->opts['exclude_cats'],
));
foreach ($posts as $post) {
//$label = apply_filters('the_title',$post->post_title);
$label = $this->sitemap_postitem_title($post);
echo ''.date('d-m-Y',strtotime($post->post_date)).' - '.$label.' '."\n";
}
echo ' '."\n";
}
// Return the appropriate $title for a Post object, depending on settings
function sitemap_postitem_title($post=false) {
// Note: Not filtering Thesis & AIO:SEO titles deliberately.
switch ($this->opts['sel_use_title']) {
case 'thesis':
$label = get_post_meta($post->ID, 'thesis_title', true);
break;
case 'allinoneseo':
$label = get_post_meta($post->ID, '_aioseop_title', true);
break;
default:
$label = apply_filters('the_title',$post->post_title);
break;
}
// If still empty, default fallback:
if (empty($label)) {
$label = apply_filters('the_title',$post->post_title);
}
return $label;
}
/**
* Admin functions
*/
function admin_init() {
add_action('admin_menu', array(&$this,'admin_menu'));
}
function admin_menu() {
$hooks[] = add_submenu_page('options-general.php', 'Sitemap', 'Sitemap', 'administrator', 'sitemap', array(&$this,'admin_settings_page'));
foreach ($hooks as $hook) { add_action("load-$hook", array(&$this,'admin_enqueue')); }
}
function admin_enqueue() {
wp_enqueue_style('sitemap', SIMPLESITEMAP_PLUGIN_URL.'/admin.css');
}
function admin_settings_page() {
echo ''."\n";
echo '
Sitemap Settings '."\n";
if (isset($_POST['SaveSitemapSettings'])) {
if (!wp_verify_nonce($_POST['_wpnonce'], $this->nonce)) { echo '
Invalid Security
'."\n"; return; }
$this->opts = array_merge($this->opts, array(
'sitemap_page' => $_POST['sel_sitemap_page'],
'sel_sitemap_position' => $_POST['sel_sitemap_position'],
'sel_use_title' => $_POST['sel_use_title'],
'exclude_pages' => (array)$_POST['exclude_pages'],
'exclude_cats' => (array)$_POST['exclude_categories'],
));
$this->save_options();
echo 'Settings have been saved.
';
}
// Let's check for sitemap.xml generator. His is better then my attempt anyway.
if (!class_exists('GoogleSitemapGeneratorLoader')) {
$all_plugins = apply_filters( 'all_plugins', get_plugins() );
if (!array_key_exists('google-sitemap-generator/sitemap.php',$all_plugins)) {
// Not installed
$install_link = admin_url('plugin-install.php?tab=plugin-information&plugin=google-sitemap-generator&TB_iframe=true&width=640&height=880');
echo '';
} else {
// Installed, not active
echo '';
}
}
// echo ' '; print_r($this->opts); echo ' ';
?>
'."\n";
}
function admin_show_page_items($parentID=0, $level=0) {
$pages = get_pages(array(
'parent' => $parentID,
'child_of' => $parentID,
'hierarchical' => false,
));
foreach ($pages as $page) {
$s = (in_array($page->ID, $this->opts['exclude_pages'])) ? ' checked="checked"' : '';
echo ' '.$page->post_title.' ';
$this->admin_show_page_items($page->ID, $level+1);
}
}
function admin_show_category_items($parentID=0, $level=0) {
$cats = get_categories(array(
'parent' => $parentID,
'child_of' => $parentID,
'hierarchical' => false,
));
foreach ($cats as $cat) {
$s = (in_array($cat->term_id, $this->opts['exclude_cats'])) ? ' checked="checked"' : '';
echo ' '.$cat->name.' ';
$this->admin_show_category_items($cat->term_id, $level+1);
}
}
}
?>