Exemplo n.º 1
0
function generate_sitemap($str)
{
    $default_priority = '0.5';
    header('X-Robots-Tag: noindex');
    echo '<?xml version="1.0" encoding="UTF-8"?>';
    if ($str == 'index') {
        echo '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
        if (config('sitemap.priority.base') !== 'false') {
            echo '<sitemap><loc>' . site_url() . 'sitemap.base.xml</loc></sitemap>';
        }
        if (config('sitemap.priority.post') !== 'false') {
            echo '<sitemap><loc>' . site_url() . 'sitemap.post.xml</loc></sitemap>';
        }
        if (config('sitemap.priority.static') !== 'false') {
            echo '<sitemap><loc>' . site_url() . 'sitemap.static.xml</loc></sitemap>';
        }
        if (config('sitemap.priority.category') !== 'false') {
            echo '<sitemap><loc>' . site_url() . 'sitemap.category.xml</loc></sitemap>';
        }
        if (config('sitemap.priority.tag') !== 'false') {
            echo '<sitemap><loc>' . site_url() . 'sitemap.tag.xml</loc></sitemap>';
        }
        if (config('sitemap.priority.archiveDay') !== 'false' || config('sitemap.priority.archiveMonth') !== 'false' || config('sitemap.priority.archiveYear') !== 'false') {
            echo '<sitemap><loc>' . site_url() . 'sitemap.archive.xml</loc></sitemap>';
        }
        if (config('sitemap.priority.author') !== 'false') {
            echo '<sitemap><loc>' . site_url() . 'sitemap.author.xml</loc></sitemap>';
        }
        echo '</sitemapindex>';
    } elseif ($str == 'base') {
        $priority = config('sitemap.priority.base') ? config('sitemap.priority.base') : '1.0';
        echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
        if ($priority !== 'false') {
            echo '<url><loc>' . site_url() . '</loc><priority>' . $priority . '</priority></url>';
        }
        echo '</urlset>';
    } elseif ($str == 'post') {
        $priority = config('sitemap.priority.post') ? config('sitemap.priority.post') : $default_priority;
        $posts = array();
        if ($priority !== 'false') {
            $posts = sitemap_post_path();
        }
        echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
        foreach ($posts as $p) {
            echo '<url><loc>' . $p->url . '</loc><priority>' . $priority . '</priority><lastmod>' . date('Y-m-d', $p->date) . '</lastmod></url>';
        }
        echo '</urlset>';
    } elseif ($str == 'static') {
        $priority = config('sitemap.priority.static') ? config('sitemap.priority.static') : $default_priority;
        $posts = array();
        if ($priority !== 'false') {
            $posts = sitemap_page_path();
        }
        echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
        foreach ($posts as $p) {
            echo '<url><loc>' . $p->url . '</loc><priority>' . $priority . '</priority></url>';
        }
        echo '</urlset>';
    } elseif ($str == 'tag') {
        $priority = config('sitemap.priority.tag') ? config('sitemap.priority.tag') : $default_priority;
        $posts = array();
        if ($priority !== 'false') {
            $posts = get_post_unsorted();
        }
        $tags = array();
        echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
        if ($posts) {
            foreach ($posts as $index => $v) {
                $arr = explode('_', $v);
                $data = $arr[1];
                $mtag = explode(',', $data);
                foreach ($mtag as $etag) {
                    $tags[] = strtolower($etag);
                }
            }
            foreach ($tags as $t) {
                $tag[] = site_url() . 'tag/' . strtolower($t);
            }
            if (isset($tag)) {
                $tag = array_unique($tag, SORT_REGULAR);
                foreach ($tag as $t) {
                    echo '<url><loc>' . $t . '</loc><priority>' . $priority . '</priority></url>';
                }
            }
        }
        echo '</urlset>';
    } elseif ($str == 'archive') {
        $priorityDay = config('sitemap.priority.archiveDay') ? config('sitemap.priority.archiveDay') : $default_priority;
        $priorityMonth = config('sitemap.priority.archiveMonth') ? config('sitemap.priority.archiveMonth') : $default_priority;
        $priorityYear = config('sitemap.priority.archiveYear') ? config('sitemap.priority.archiveYear') : $default_priority;
        $posts = sitemap_post_path();
        $day = array();
        $month = array();
        $year = array();
        foreach ($posts as $p) {
            $day[] = $p->archiveday;
            $month[] = $p->archivemonth;
            $year[] = $p->archiveyear;
        }
        $day = array_unique($day, SORT_REGULAR);
        $month = array_unique($month, SORT_REGULAR);
        $year = array_unique($year, SORT_REGULAR);
        echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
        if ($priorityDay !== 'false') {
            foreach ($day as $d) {
                echo '<url><loc>' . $d . '</loc><priority>' . $priorityDay . '</priority></url>';
            }
        }
        if ($priorityMonth !== 'false') {
            foreach ($month as $m) {
                echo '<url><loc>' . $m . '</loc><priority>' . $priorityMonth . '</priority></url>';
            }
        }
        if ($priorityYear !== 'false') {
            foreach ($year as $y) {
                echo '<url><loc>' . $y . '</loc><priority>' . $priorityYear . '</priority></url>';
            }
        }
        echo '</urlset>';
    } elseif ($str == 'author') {
        $priority = config('sitemap.priority.author') ? config('sitemap.priority.author') : $default_priority;
        $author = array();
        if ($priority !== 'false') {
            $posts = sitemap_post_path();
            foreach ($posts as $p) {
                $author[] = $p->authorUrl;
            }
            $author = array_unique($author, SORT_REGULAR);
        }
        echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
        if ($priority !== 'false') {
            foreach ($author as $a) {
                echo '<url><loc>' . $a . '</loc><priority>' . $priority . '</priority></url>';
            }
        }
        echo '</urlset>';
    } elseif ($str == 'category') {
        $priority = config('sitemap.priority.category') ? config('sitemap.priority.category') : $default_priority;
        $posts = array();
        if ($priority !== 'false') {
            $posts = get_post_unsorted();
        }
        $cats = array();
        echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
        if ($posts) {
            foreach ($posts as $index => $v) {
                $arr = explode('_', $v);
                $replaced = substr($arr[0], 0, strrpos($arr[0], '/')) . '/';
                $str = explode('/', $replaced);
                $cats[] = $str[count($str) - 3];
            }
            foreach ($cats as $c) {
                $cat[] = site_url() . 'category/' . strtolower($c);
            }
            if (isset($cat)) {
                $cat = array_unique($cat, SORT_REGULAR);
                foreach ($cat as $c) {
                    echo '<url><loc>' . $c . '</loc><priority>' . $priority . '</priority></url>';
                }
            }
        }
        echo '</urlset>';
    }
}
Exemplo n.º 2
0
function generate_sitemap($str)
{
    header('X-Robots-Tag: noindex');
    echo '<?xml version="1.0" encoding="UTF-8"?>';
    if ($str == 'index') {
        echo '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
        echo '<sitemap><loc>' . site_url() . 'sitemap.base.xml</loc></sitemap>';
        echo '<sitemap><loc>' . site_url() . 'sitemap.post.xml</loc></sitemap>';
        echo '<sitemap><loc>' . site_url() . 'sitemap.static.xml</loc></sitemap>';
        echo '<sitemap><loc>' . site_url() . 'sitemap.tag.xml</loc></sitemap>';
        echo '<sitemap><loc>' . site_url() . 'sitemap.archive.xml</loc></sitemap>';
        echo '<sitemap><loc>' . site_url() . 'sitemap.author.xml</loc></sitemap>';
        echo '</sitemapindex>';
    } elseif ($str == 'base') {
        echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
        echo '<url><loc>' . site_url() . '</loc><priority>1.0</priority></url>';
        echo '</urlset>';
    } elseif ($str == 'post') {
        $posts = sitemap_post_path();
        echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
        foreach ($posts as $p) {
            echo '<url><loc>' . $p->url . '</loc><priority>0.5</priority></url>';
        }
        echo '</urlset>';
    } elseif ($str == 'static') {
        $posts = sitemap_page_path();
        echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
        if (!empty($posts)) {
            foreach ($posts as $p) {
                echo '<url><loc>' . $p->url . '</loc><priority>0.5</priority></url>';
            }
        }
        echo '</urlset>';
    } elseif ($str == 'tag') {
        $posts = get_post_unsorted();
        $tags = array();
        if (!empty($posts)) {
            foreach ($posts as $index => $v) {
                $arr = explode('_', $v);
                $data = $arr[1];
                $mtag = explode(',', $data);
                foreach ($mtag as $etag) {
                    $tags[] = $etag;
                }
            }
            foreach ($tags as $t) {
                $tag[] = site_url() . 'tag/' . $t;
            }
            echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
            if (isset($tag)) {
                $tag = array_unique($tag, SORT_REGULAR);
                foreach ($tag as $t) {
                    echo '<url><loc>' . $t . '</loc><priority>0.5</priority></url>';
                }
            }
            echo '</urlset>';
        }
    } elseif ($str == 'archive') {
        $posts = sitemap_post_path();
        $day = array();
        $month = array();
        $year = array();
        foreach ($posts as $p) {
            $day[] = $p->archiveday;
            $month[] = $p->archivemonth;
            $year[] = $p->archiveyear;
        }
        $day = array_unique($day, SORT_REGULAR);
        $month = array_unique($month, SORT_REGULAR);
        $year = array_unique($year, SORT_REGULAR);
        echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
        foreach ($day as $d) {
            echo '<url><loc>' . $d . '</loc><priority>0.5</priority></url>';
        }
        foreach ($month as $m) {
            echo '<url><loc>' . $m . '</loc><priority>0.5</priority></url>';
        }
        foreach ($year as $y) {
            echo '<url><loc>' . $y . '</loc><priority>0.5</priority></url>';
        }
        echo '</urlset>';
    } elseif ($str == 'author') {
        $posts = sitemap_post_path();
        $author = array();
        foreach ($posts as $p) {
            $author[] = $p->authorUrl;
        }
        $author = array_unique($author, SORT_REGULAR);
        echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
        foreach ($author as $a) {
            echo '<url><loc>' . $a . '</loc><priority>0.5</priority></url>';
        }
        echo '</urlset>';
    }
}