/*
Plugin Name: Dagon Design Sitemap Generator
Plugin URI: http://www.dagondesign.com/articles/sitemap-generator-plugin-for-wordpress/
Description: Generates a fully customizable sitemap
Author: Dagon Design
Version: 3.17
Author URI: http://www.dagondesign.com
*/
$ddsg_ver = ’3.17′;
/*
* Set up options if they do not exist
*/
add_option(‘ddsg_language’, ‘English’);
add_option(‘ddsg_items_per_page’, 50);
add_option(‘ddsg_sm_name’, ”);
add_option(‘ddsg_what_to_show’, ‘both’);
add_option(‘ddsg_which_first’, ‘posts’);
add_option(‘ddsg_post_sort_order’, ‘title’);
add_option(‘ddsg_page_sort_order’, ‘title’);
add_option(‘ddsg_comments_on_posts’, FALSE);
add_option(‘ddsg_comments_on_pages’, FALSE);
add_option(‘ddsg_show_zero_comments’, FALSE);
add_option(‘ddsg_hide_future’, FALSE);
add_option(‘ddsg_new_window’, FALSE);
add_option(‘ddsg_show_post_date’, FALSE);
add_option(‘ddsg_show_page_date’, FALSE);
add_option(‘ddsg_date_format’, ‘F jS, Y’);
add_option(‘ddsg_hide_protected’, TRUE);
add_option(‘ddsg_excluded_cats’, ”);
add_option(‘ddsg_excluded_pages’, ”);
add_option(‘ddsg_page_nav’, ’1′);
add_option(‘ddsg_page_nav_where’, ‘top’);
add_option(‘ddsg_xml_path’, ”);
add_option(‘ddsg_xml_where’, ‘last’);
/*
* Load language file
*/
$ddsg_lang_file = trim(get_option(ddsg_language));
if ($ddsg_lang_file == ”) {
$ddsg_lang_file = ‘english’;
}
include ABSPATH . ‘wp-content/plugins/sitemap-generator/lang/’ . $ddsg_lang_file . ‘.php’;
/*
* Add options page
*/
function ddsg_add_option_pages() {
if (function_exists(‘add_options_page’)) {
add_options_page(‘Dagon Design Sitemap Generator’, ‘DDSitemapGen’, 8, __FILE__, ‘ddsg_options_page’);
}
}
/*
* Generate options page
*/
function ddsg_options_page() {
global $ddsg_ver;
if (isset($_POST['set_defaults'])) {
echo ‘
‘;
update_option(‘ddsg_language’, ‘English’);
update_option(‘ddsg_items_per_page’, 50);
update_option(‘ddsg_sm_name’, ”);
update_option(‘ddsg_what_to_show’, ‘both’);
update_option(‘ddsg_which_first’, ‘posts’);
update_option(‘ddsg_post_sort_order’, ‘title’);
update_option(‘ddsg_page_sort_order’, ‘title’);
update_option(‘ddsg_comments_on_posts’, FALSE);
update_option(‘ddsg_comments_on_pages’, FALSE);
update_option(‘ddsg_show_zero_comments’, FALSE);
update_option(‘ddsg_hide_future’, FALSE);
update_option(‘ddsg_new_window’, FALSE);
update_option(‘ddsg_show_post_date’, FALSE);
update_option(‘ddsg_show_page_date’, FALSE);
update_option(‘ddsg_date_format’, ‘F jS, Y’);
update_option(‘ddsg_hide_protected’, TRUE);
update_option(‘ddsg_excluded_cats’, ”);
update_option(‘ddsg_excluded_pages’, ”);
update_option(‘ddsg_page_nav’, ’1′);
update_option(‘ddsg_page_nav_where’, ‘top’);
update_option(‘ddsg_xml_path’, ”);
update_option(‘ddsg_xml_where’, ‘last’);
echo DDSG_DEFAULTS_LOADED;
echo ‘
‘;
} else if (isset($_POST['info_update'])) {
echo ‘
‘;
update_option(‘ddsg_language’, (string) $_POST["ddsg_language"]);
update_option(‘ddsg_items_per_page’, (int) $_POST["ddsg_items_per_page"]);
update_option(‘ddsg_sm_name’, (string) $_POST["ddsg_sm_name"]);
update_option(‘ddsg_what_to_show’, (string) $_POST["ddsg_what_to_show"]);
update_option(‘ddsg_which_first’, (string) $_POST["ddsg_which_first"]);
update_option(‘ddsg_post_sort_order’, (string) $_POST["ddsg_post_sort_order"]);
update_option(‘ddsg_page_sort_order’, (string) $_POST["ddsg_page_sort_order"]);
update_option(‘ddsg_comments_on_posts’, (bool) $_POST["ddsg_comments_on_posts"]);
update_option(‘ddsg_comments_on_pages’, (bool) $_POST["ddsg_comments_on_pages"]);
update_option(‘ddsg_show_zero_comments’, (bool) $_POST["ddsg_show_zero_comments"]);
update_option(‘ddsg_hide_future’, (bool) $_POST["ddsg_hide_future"]);
update_option(‘ddsg_new_window’, (bool) $_POST["ddsg_new_window"]);
update_option(‘ddsg_show_post_date’, (bool) $_POST["ddsg_show_post_date"]);
update_option(‘ddsg_show_page_date’, (bool) $_POST["ddsg_show_page_date"]);
update_option(‘ddsg_date_format’, (string) $_POST["ddsg_date_format"]);
update_option(‘ddsg_hide_protected’, (bool) $_POST["ddsg_hide_protected"]);
update_option(‘ddsg_excluded_cats’, (string) $_POST["ddsg_excluded_cats"]);
update_option(‘ddsg_excluded_pages’, (string) $_POST["ddsg_excluded_pages"]);
update_option(‘ddsg_page_nav’, (string) $_POST["ddsg_page_nav"]);
update_option(‘ddsg_page_nav_where’, (string) $_POST["ddsg_page_nav_where"]);
update_option(‘ddsg_xml_path’, (string) $_POST["ddsg_xml_path"]);
update_option(‘ddsg_xml_where’, (string) $_POST["ddsg_xml_where"]);
echo DDSG_CONFIG_UPDATED;
echo ‘
‘;
} ?>
Dagon Design Sitemap Generator v
}
/*
* Build list of categories
*/
function ddsg_get_cats($cat_data, $cats, $num_cats, $cats_with_children, $excluded_cats, $parent = 0, $level = 0) {
// echo nl2br(htmlspecialchars(print_r($cats, TRUE)));
// exit();
$k = 0;
while (isset($cats[$k]) && ($cats[$k]->category_parent != $parent) && ($k < $num_cats)) {
$k++;
}
while (isset($cats[$k]) && ($cats[$k]->category_parent == $parent) && ($k < $num_cats)) {
if (in_array($cats[$k]->category_ID, $excluded_cats, FALSE) === FALSE) {
$cat_data[] = array(
‘cat_id’ => $cats[$k]->category_ID,
‘cat_name’ => $cats[$k]->cat_name,
‘level’ => $level
);
}
if (count($cats_with_children) > 0) {
if (in_array($cats[$k]->category_ID, $cats_with_children, FALSE)) {
if (in_array($cats[$k]->category_ID, $excluded_cats, FALSE) === FALSE) {
$cat_data = ddsg_get_cats($cat_data, $cats, $num_cats, $cats_with_children, $excluded_cats, $cats[$k]->category_ID, $level + 1);
}
}
}
$k++;
}
return $cat_data;
}
/*
* Build list of pages
*/
function ddsg_get_pages($page_data, $pages, $num_pages, $pages_with_children, $excluded_pages, $comments_on_pages, $show_page_date, $parent = 0, $level = 0) {
$k = 0;
while (isset($pages[$k]) && ($pages[$k]->post_parent != $parent) && ($k < $num_pages)) {
$k++;
}
while (isset($pages[$k]) && ($pages[$k]->post_parent == $parent) && ($k < $num_pages)) {
if (in_array($pages[$k]->ID, $excluded_pages, FALSE) === FALSE) {
$tmp_array = array();
$tmp_array['id'] = $pages[$k]->ID;
$tmp_array['title'] = $pages[$k]->post_title;
$tmp_array['level'] = $level;
if ($comments_on_pages) $tmp_array['comments'] = $pages[$k]->comment_count;
if ($show_page_date) $tmp_array['date'] = $pages[$k]->post_date;
$page_data[] = $tmp_array;
}
if (in_array($pages[$k]->ID, $pages_with_children, FALSE)) {
if (in_array($pages[$k]->ID, $excluded_pages, FALSE) === FALSE) {
$page_data = ddsg_get_pages($page_data, $pages, $num_pages, $pages_with_children, $excluded_pages, $comments_on_pages, $show_page_date, $pages[$k]->ID, $level + 1);
}
}
$k++;
}
return $page_data;
}
/*
* Find parent of page
*/
function ddsg_find_parent_page($page_data, $p, $k) {
$level = $page_data[$p]['level'];
while (($page_data[$p]['level'] > $k) and ($p >= 0)) {
$p–;
}
return ” . $page_data[$p]['title'] . ” . “n”;
}
/*
* Generate page output
*/
function ddsg_display_pages($page_data, $num_pages, $page_start, $page_end, $comments_on_pages, $new_window, $show_page_date, $date_format) {
$show_zero_comments = get_option(‘ddsg_show_zero_comments’);
if ($num_pages == 0) return “”;
if (($page_start == 0) && ($page_end == 0)) return “”;
$t_out = “”;
$t_out .= DDSG_PAGE_HEADER;
$llevel = -1;
for ($p = $page_start; $p <= $page_end; $p++) {
$level = $page_data[$p]['level'];
if ($level > $llevel) {
$t_out .= ‘
- ‘;
- ‘ . ddsg_find_parent_page($page_data, $p, $k) . ‘ ‘ . DDSG_CONTINUED . ‘
- ‘;
}
}if ($level == $llevel) {
$t_out .= ‘‘;
}if ($level < $llevel) {
$t_out .= ‘‘;
for ($k = $llevel; $k > $level; $k–) {
$t_out .= ‘ - $t_out .= ‘ title=”‘ . strip_tags($the_title) . ‘”‘;
if ($new_window) {
$t_out .= ‘ target=”_blank”‘;
}
$t_out .= ‘>’ . $the_title . ”;if ($show_page_date) {
// $t_out .= ‘ ‘ . date($date_format, strtotime($page_data[$p]['date']));
$t_out .= ‘ ‘ . date_i18n($date_format, strtotime($page_data[$p]['date']));
}if ($comments_on_pages) {
if ($show_zero_comments || ($page_data[$p]['comments'] > 0)) {
$t_out .= ‘ (‘ . $page_data[$p]['comments'] . ‘)’;
}
}$llevel = $level;
}$t_out .= ‘
for ($k = $llevel + 1; $k < $level; $k++) {
$t_out .= ‘
‘;
}
}
$the_title = htmlspecialchars(trim($page_data[$p]['title']));
if ($the_title == ”) {
$the_title = DDSG_NO_TITLE;
}
$t_out .= ‘
‘ . “n”;
for ($k = $llevel; $k > 0; $k–) {
$t_out .= ‘
‘;
}
$t_out .= ‘
‘;
return $t_out;
}
/*
* Build post parent
*/
function ddsg_find_parent_post($post_data, $p, $k) {
$level = $post_data[$p]['level'];
while (($post_data[$p]['level'] > $k) and ($p >= 0)) {
$p–;
}
$the_cat_link = ”;
if (!is_array(get_category_link($post_data[$p]['id'])))
$the_cat_link = get_category_link($post_data[$p]['id']);
$the_title = ”;
if (!is_array($post_data[$p]['title']))
$the_title = $post_data[$p]['title'];
return DDSG_CAT_HEADER . ‘ ‘ . $the_title . ”;
}
/*
* Generate post output
*/
function ddsg_display_posts($post_data, $num_posts, $post_start, $post_end, $comments_on_posts, $new_window, $show_post_date, $date_format) {
$show_zero_comments = get_option(‘ddsg_show_zero_comments’);
if ($num_posts == 0) return “”;
if (($post_start == 0) && ($post_end == 0)) return “”;
$t_out = “”;
$t_out .= DDSG_POST_HEADER;
$llevel = -1;
for ($p = $post_start; $p <= $post_end; $p++) {
$level = $post_data[$p]['level'];
if ($level > $llevel) {
$t_out .= ‘
- ‘;
- ‘ . ddsg_find_parent_post($post_data, $p, $k) . ‘ ‘ . DDSG_CONTINUED . ‘
- ‘;
}
}
if ($level == $llevel) {
$t_out .= ”;
}if ($level < $llevel) {
$t_out .= ‘‘;
for ($k = $llevel; $k > $level; $k–) {
$t_out .= ‘ - ‘ . DDSG_CAT_HEADER . ‘ ‘;
$t_out .= ‘ $t_out .= ‘ title=”‘ . strip_tags($post_data[$p]['title']) . ‘”‘;
if ($new_window) {
$t_out .= ‘ target=”_blank”‘;
}
$t_out .= ‘>’ . $post_data[$p]['title'] . ”;} else {
$the_title = htmlspecialchars(trim($post_data[$p]['title']));
if ($the_title == ”) {
$the_title = DDSG_NO_TITLE;
}$t_out .= ‘
- $t_out .= ‘ title=”‘ . $the_title . ‘”‘;
if ($new_window) {
$t_out .= ‘ target=”_blank”‘;
}
$t_out .= ‘>’ . $the_title . ”;if ($show_post_date) {
// $t_out .= ‘ ‘ . date($date_format, strtotime($post_data[$p]['date']));
$t_out .= ‘ ‘ . date_i18n($date_format, strtotime($post_data[$p]['date']));}
if ($comments_on_posts) {
if ($show_zero_comments || ($post_data[$p]['comments'] > 0)) {
$t_out .= ‘ (‘ . $post_data[$p]['comments'] . ‘)’;
}
}}
$llevel = $level;
}$t_out .= ‘
for ($k = $llevel + 1; $k < $level; $k++) {
$t_out .= ‘
‘;
}
}
if ($post_data[$p]['type'] == ‘c’) {
$t_out .= ‘
‘;
for ($k = $llevel; $k > 0; $k–) {
$t_out .= ‘
‘;
}
$t_out .= ‘
‘;
return $t_out;
}
/*
* Merge categories with posts
*/
function ddsg_merge_cats_posts($post_data, $posts, $cat_data, $num_posts, $num_cats, $comments_on_posts, $show_post_date) {
for ($c = 0; $c < $num_cats; $c++) {
$level = $cat_data[$c]['level'];
$tmp_array = array();
$tmp_array['type'] = ‘c’;
$tmp_array['id'] = $cat_data[$c]['cat_id'];
$tmp_array['title'] = $cat_data[$c]['cat_name'];
$tmp_array['level'] = $level;
$post_data[] = $tmp_array;
$k = 0;
while (isset($posts[$k]) && ($posts[$k]->category_id != $cat_data[$c]['cat_id']) && ($k < $num_posts)) {
$k++;
}
$any_posts = 0;
while (isset($posts[$k]) && ($posts[$k]->category_id == $cat_data[$c]['cat_id']) && ($k < $num_posts)) {
$tmp_array = array();
$tmp_array['type'] = ‘p’;
$tmp_array['id'] = $posts[$k]->ID;
$tmp_array['title'] = $posts[$k]->post_title;
$tmp_array['level'] = $level + 1;
if ($comments_on_posts) $tmp_array['comments'] = $posts[$k]->comment_count;
if ($show_post_date) $tmp_array['date'] = $posts[$k]->post_date;
$post_data[] = $tmp_array;
$k++;
}
}
return $post_data;
}
/*
* Remove categories with no posts
*/
function ddsg_remove_empty_cats($post_data) {
$llp = -1;
$last_type = ‘x’;
$last_level = ‘x’;
$last_del = FALSE;
$pdc = count($post_data);
for ($i = $pdc – 1; $i >= 0; $i–) {
$type = $post_data[$i]['type'];
$title = $post_data[$i]['title'];
$level = $post_data[$i]['level'];
if (($type == ‘c’) && ($last_type == ‘c’) && (($last_level <= $level) || ($last_del == TRUE))) {
$post_data[$i]['type'] = ‘r’;
$last_del = TRUE;
} else {
$last_del = FALSE;
}
$last_type = $type;
$last_level = $level;
if ($post_data[$i]['type'] == ‘p’) {
$llp = $post_data[$i]['level'];
}
}
$new_post_data = array();
foreach ($post_data as $pd) {
if ($pd['type'] != ‘r’) {
$new_post_data[] = $pd;
}
}
return $new_post_data;
}
/*
* Generate sitemap navigation
*/
function ddsg_generate_nav($total_pages, $current_page, $page_nav, $sm_name) {
global $wp_query;
$output1 = NULL;
if (strlen($sm_name) > 0) { // permalinks enabled
$the_url = get_bloginfo(‘url’);
if ($the_url{strlen($the_url)-1} != “/”) {
$the_url = $the_url . “/” . $sm_name . “/”;
}
if ($total_pages > 1) {
if ($page_nav == 1) {
$output1 .= ”
” . DDSG_NAV1_PAGE . ” ” . $current_page . ” ” . DDSG_NAV1_OF . ” ” . $total_pages;
if ($current_page > 1) {
$output1 .= ‘ : ‘ . DDSG_NAV1_PREV . ”;
}
if ($current_page < $total_pages) {
$output1 .= ‘ : ‘ . DDSG_NAV1_NEXT . ”;
}
$output1 .= ”
“;
} else if ($page_nav == 2) {
$output1 .= ‘
‘ . DDSG_NAV2_PAGE . ‘ ‘;
for ($i = 1; $i <= $total_pages; $i++) {
if ($i == $current_page) {
$output1 .= $i . ” “;
} else {
$output1 .= ” . $i . ‘ ‘;
}
}
$output1 .= ”
“;
}
}
} else { // permalinks not enabled
$the_url = get_bloginfo(‘url’);
if ($the_url{strlen($the_url)-1} != “/”) {
$the_url = $the_url . “/index.php”;
}
if ($total_pages > 1) {
if ($page_nav == 1) {
$output1 .= ”
” . DDSG_NAV1_PAGE . ” ” . $current_page . ” ” . DDSG_NAV1_OF . ” ” . $total_pages;
if ($current_page > 1) {
$output1 .= ‘ : ‘ . DDSG_NAV1_PREV . ”;
}
if ($current_page < $total_pages) {
$output1 .= ‘ : ‘ . DDSG_NAV1_NEXT . ”;
}
$output1 .= ”
“;
} else if ($page_nav == 2) {
$output1 .= ‘
‘ . DDSG_NAV2_PAGE . ‘ ‘;
for ($i = 1; $i <= $total_pages; $i++) {
if ($i == $current_page) {
$output1 .= $i . ” “;
} else {
$output1 .= ‘‘ . $i . ‘ ‘;
}
}
$output1 .= ”
“;
}
}
}
if (strlen($sm_name) > 0) {
$output1 = str_replace($the_url . ’1/’, $the_url, $output1);
} else {
$output1 = str_replace(‘&pg=1″‘, ‘”‘, $output1);
}
return $output1;
}
/*
* Create the sitemap
*/
function ddsg_create_sitemap() {
global $wpdb;
$tp = $wpdb->prefix;
// Currently using a work-around for the version system
// determines if pre or post 2.3 from wp_term_taxonomy
$ver = 2.2;
$wpv = $wpdb->get_results(“show tables like ‘{$tp}term_taxonomy’”);
if (count($wpv) > 0) {
$ver = 2.3;
}
$items_per_page = (int)get_option(‘ddsg_items_per_page’);
$sm_name = (string)get_option(‘ddsg_sm_name’);
$what_to_show = trim(get_option(‘ddsg_what_to_show’));
$which_first = trim(get_option(‘ddsg_which_first’));
$post_sort_order = trim(get_option(‘ddsg_post_sort_order’));
$page_sort_order = trim(get_option(‘ddsg_page_sort_order’));
$comments_on_posts = get_option(‘ddsg_comments_on_posts’);
$comments_on_pages = get_option(‘ddsg_comments_on_pages’);
$hide_future = get_option(‘ddsg_hide_future’);
$new_window = get_option(‘ddsg_new_window’);
$show_post_date = get_option(‘ddsg_show_post_date’);
$show_page_date = get_option(‘ddsg_show_page_date’);
$date_format = get_option(‘ddsg_date_format’);
$hide_protected = get_option(‘ddsg_hide_protected’);
$excluded_cats = trim(get_option(‘ddsg_excluded_cats’));
$excluded_pages = trim(get_option(‘ddsg_excluded_pages’));
$page_nav = trim(get_option(‘ddsg_page_nav’));
$page_nav_where = trim(get_option(‘ddsg_page_nav_where’));
$xml_path = trim(get_option(‘ddsg_xml_path’));
$xml_where = get_option(‘ddsg_xml_where’);
// prepare exclusion lists
$excluded_cats = str_replace(‘ ‘, ”, $excluded_cats);
$excluded_cats = (array)explode(‘,’, $excluded_cats);
$excluded_pages = str_replace(‘ ‘, ”, $excluded_pages);
$excluded_pages = (array)explode(‘,’, $excluded_pages);
if ($what_to_show != ‘pages’) {
// gets cats
if ($ver < 2.3) {
$cats = (array)$wpdb->get_results(”
SELECT cat_ID as category_ID, cat_name, category_parent
FROM {$tp}categories
GROUP BY cat_ID
ORDER BY category_parent, cat_name
“);
$cats_with_children = (array)$wpdb->get_col(”
SELECT category_parent
FROM {$tp}categories
WHERE category_parent != ’0′
GROUP BY category_parent
ORDER BY category_parent
“, 0);
} else { // >= 2.3
$cats = (array)$wpdb->get_results(”
SELECT {$tp}terms.term_id as category_ID,
{$tp}terms.name as cat_name,
{$tp}term_taxonomy.parent as category_parent
FROM {$tp}terms, {$tp}term_taxonomy
WHERE {$tp}term_taxonomy.taxonomy = ‘category’
AND {$tp}terms.term_id = {$tp}term_taxonomy.term_id
GROUP BY category_ID
ORDER BY category_parent, cat_name
“);
$cats_with_children = (array)$wpdb->get_col(”
SELECT parent as category_parent
FROM {$tp}term_taxonomy
WHERE parent != ’0′
AND {$tp}term_taxonomy.taxonomy = ‘category’
GROUP BY category_parent
ORDER BY category_parent
“, 0);
}
$sort_string = ”;
switch ($post_sort_order) {
case ‘datea’:
$sort_string = ‘post_date ASC’;
break;
case ‘dated’:
$sort_string = ‘post_date DESC’;
break;
default: // title
$sort_string = ‘post_title’;
break;
}
$extra_data = ”;
if ($comments_on_posts) {
$extra_data .= ‘, comment_count ‘;
}
if ($show_post_date) {
$extra_data .= ‘, post_date ‘;
}
$dup_check = ”;
$pass_check = ”;
if ($hide_protected) {
$pass_check = ” AND post_password = ” “;
}
$future_check = ”;
if ($hide_future) {
$future_check = ” AND post_status != ‘future’ “;
}
if ($ver < 2.3) {
$posts = (array)$wpdb->get_results(”
SELECT ID, category_id, post_title $extra_data
FROM {$tp}posts, {$tp}post2cat
WHERE {$tp}posts.ID = {$tp}post2cat.post_id
AND post_status = ‘publish’
AND post_type = ‘post’
$dup_check
$pass_check
$future_check
ORDER BY category_id, $sort_string
“);
} else { // >= 2.3
$posts = (array)$wpdb->get_results(”
SELECT ID, {$tp}term_taxonomy.term_id as category_id, post_title $extra_data
FROM {$tp}posts, {$tp}term_relationships, {$tp}term_taxonomy
WHERE {$tp}posts.ID = {$tp}term_relationships.object_id
AND {$tp}term_relationships.term_taxonomy_id = {$tp}term_taxonomy.term_taxonomy_id
AND {$tp}term_taxonomy.taxonomy = ‘category’
AND post_status = ‘publish’
AND post_type = ‘post’
$dup_check
$pass_check
$future_check
ORDER BY category_id, $sort_string
“);
}
$num_cats = count($cats);
$num_posts = count($posts);
$cat_data = array();
$post_data = array();
$cat_data = ddsg_get_cats($cat_data, $cats, $num_cats, $cats_with_children, $excluded_cats);
$num_cats = count($cat_data);
$post_data = ddsg_merge_cats_posts($post_data, $posts, $cat_data, $num_posts, $num_cats, $comments_on_posts, $show_post_date);
$post_data = ddsg_remove_empty_cats($post_data);
$num_posts = count($post_data);
}
if ($what_to_show != ‘posts’) {
$sort_string = ”;
switch ($page_sort_order) {
case ‘datea’:
$sort_string = ‘post_date ASC’;
break;
case ‘dated’:
$sort_string = ‘post_date DESC’;
break;
case ‘menua’:
$sort_string = ‘menu_order ASC’;
break;
case ‘menud’:
$sort_string = ‘menu_order DESC’;
break;
default: // title
$sort_string = ‘post_title’;
break;
}
$extra_data = ”;
if ($comments_on_pages) {
$extra_data .= ‘, comment_count ‘;
}
if ($show_page_date) {
$extra_data .= ‘, post_date ‘;
}
$pass_check = ”;
if ($hide_protected) {
$pass_check = ” AND post_password = ” “;
}
$pages = (array)$wpdb->get_results(”
SELECT post_title, ID, post_parent $extra_data
FROM {$tp}posts
WHERE post_type = ‘page’
AND post_status = ‘publish’
$pass_check
ORDER BY post_parent, $sort_string
“);
$pages_with_children = (array)$wpdb->get_col(”
SELECT post_parent
FROM {$tp}posts
WHERE post_type = ‘page’
AND post_status = ‘publish’
AND post_parent != ’0′
GROUP BY post_parent
ORDER BY post_parent
“, 0);
$num_pages = count($pages);
$page_data = array();
$page_data = ddsg_get_pages($page_data, $pages, $num_pages, $pages_with_children, $excluded_pages, $comments_on_pages, $show_page_date);
$num_pages = count($page_data);
}
$wpdb->flush();
switch ($what_to_show) {
case ‘posts’:
$total_items = $num_posts;
break;
case ‘pages’:
$total_items = $num_pages;
break;
default:
$total_items = $num_posts + $num_pages;
break;
}
$current_page = 1;
if (get_query_var(“pg”)) {
$current_page = get_query_var(“pg”);
}
if ($items_per_page <= 0) {
$total_pages = 1;
$items_per_page = $total_items;
} else {
$total_pages = (int)ceil($total_items / $items_per_page);
}
$t_start = ($current_page – 1) * $items_per_page;
$t_end = $t_start + $items_per_page – 1;
if ($t_end > $total_items) {
$t_end = $total_items – 1;
}
if ($current_page < 1) {
$current_page = 1;
} else if ($current_page > $total_pages) {
$current_page = $total_pages;
$t_start = ($current_page – 1) * $items_per_page;
$t_end = $t_start + $items_per_page – 1;
if ($t_end > $total_items) {
$t_end = $total_items – 1;
}
}
$t_out = ”;
$t_out .= “nnnn”;
$t_out .= ‘
if ($page_nav_where != ‘bottom’) {
$t_out .= ddsg_generate_nav($total_pages, $current_page, $page_nav, $sm_name);
}
if ($what_to_show == ‘posts’) { // show just posts
$post_start = $t_start;
$post_end = $t_end;
if ($t_end >= $num_posts) { // *** added =
$post_end = $num_posts – 1;
}
$t_out .= ddsg_display_posts($post_data, $num_posts, $post_start, $post_end, $comments_on_posts, $new_window, $show_post_date, $date_format);
} else if ($what_to_show == ‘pages’) { // show just pages
$page_start = $t_start;
$page_end = $t_end;
if ($t_end > $num_pages – 1) {
$page_end = $num_pages – 1;
}
$t_out .= ddsg_display_pages($page_data, $num_pages, $page_start, $page_end, $comments_on_pages, $new_window, $show_page_date, $date_format);
} else { // show both
if ($which_first == ‘pages’) {
$num1 = $num_pages;
$num2 = $num_posts;
} else {
$num1 = $num_posts;
$num2 = $num_pages;
}
if ($t_start > ($num1 – 1)) {
$start1 = $end1 = 0;
$start2 = $t_start – $num1;
$end2 = $t_end – $num1;
if ($end2 >= $num2) {
$end2 = $num2 – 1;
}
} else if ($t_end < $num1) {
$start2 = $end2 = 0;
$start1 = $t_start;
$end1 = $t_end;
if ($end1 >= $num1) {
$end1 = $num1 – 1;
}
} else {
$start1 = $t_start;
$end1 = $num1 – 1;
$start2 = 0;
$end2 = $items_per_page – ($end1 – $start1) – 2;
}
if ($end2 >= $num2) {
$end2 = $num2 – 1;
}
if ($end1 >= $num1) {
$end1 = $num1 – 1;
}
if ($which_first == ‘pages’) {
$t_out .= ddsg_display_pages($page_data, $num1, $start1, $end1, $comments_on_pages, $new_window, $show_page_date, $date_format);
$t_out .= ddsg_display_posts($post_data, $num2, $start2, $end2, $comments_on_posts, $new_window, $show_post_date, $date_format);
} else {
$t_out .= ddsg_display_posts($post_data, $num1, $start1, $end1, $comments_on_posts, $new_window, $show_post_date, $date_format);
$t_out .= ddsg_display_pages($page_data, $num2, $start2, $end2, $comments_on_pages, $new_window, $show_page_date, $date_format);
}
}
if ($page_nav_where != ‘top’) {
$t_out .= ddsg_generate_nav($total_pages, $current_page, $page_nav, $sm_name);
}
if ($xml_path != ”) {
if (($xml_where == ‘every’) OR ($current_page == $total_pages)) {
$t_out .= ‘
‘ . DDSG_VIEW_XML . ‘
‘;
}
}
$t_out .= ‘
‘ . DDSG_CREDITS . ‘ dagondesign.com
‘;
$t_out .= ‘
‘;
$t_out .= “nnnn”;
// Ampersand fix
$t_out = str_replace(“&”, “&”, $t_out);
return $t_out;
}
/*
* Create rewrite rules for sitemap pages
*/
function ddsg_permalinks($rules) {
global $wp_rewrite;
$ddsg_sm_name = trim(get_option(‘ddsg_sm_name’));
if ($wp_rewrite->use_verbose_rules || !isset($wp_rewrite->use_verbose_rules)) {
$match_form = ‘$1′;
} else {
$match_form = ‘$matches[1]‘;
}
if ($ddsg_sm_name != ”) {
$newrules[$ddsg_sm_name . '/([0-9]{1,})/?$’] = ‘index.php?&pagename=’ . $ddsg_sm_name . ‘&pg=’ . $match_form;
$newrules = array_merge($newrules,$rules);
return $newrules;
} else {
return $rules;
}
}
/*
* Initialize query var for sitemap permalinks
*/
function ddsg_query_vars ( $vars ) {
$vars[] = “pg”;
return $vars;
}
/*
* Display sitemap if trigger is found
*/
function ddsg_generate_sitemap($content) {
if (strpos($content, ““) !== FALSE) {
$content = preg_replace(‘/
s*s*</p>/i’, ““, $content);
$content = str_replace(‘‘, ddsg_create_sitemap(), $content);
}
return $content;
}
add_filter(‘query_vars’, ‘ddsg_query_vars’);
add_filter(‘rewrite_rules_array’, ‘ddsg_permalinks’);
add_filter(‘the_content’, ‘ddsg_generate_sitemap’);
add_action(‘admin_menu’, ‘ddsg_add_option_pages’);
?>
