Esempio n. 1
0
/** build the XML file, sitemap_baidu.xml **/
function build_baidu_sitemap()
{
    global $wpdb, $posts, $wp_version;
    $get_baidu_sitemap_options = get_option(BAIDU_SITEMAP_OPTION);
    if (!empty($get_baidu_sitemap_options)) {
        list($lc_blog_url, $lc_admin_email, $lc_updatePeri, $lc_limits, $lc_sitemap_auto, $lc_order_1, $lc_order_2, $lc_order_3, $lc_comments, $lc_post_length, $lc_post_cat, $lc_post_views, $lc_pickcats, $lc_comments_count, $lc_views_count, $lc_sitemap_html, $lc_sitemap_publish_post, $lc_support, $lc_baidu_sitemap_by_post) = explode("|", $get_baidu_sitemap_options);
    }
    $lc_pickcats_array = explode(";", $lc_pickcats);
    $blog_home = get_bloginfo('url');
    /** Get the current time **/
    $blogtime = current_time('mysql');
    list($today_year, $today_month, $today_day, $hour, $minute, $second) = split('([^0-9])', $blogtime);
    /** XML_begin **/
    $xml_begin = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";
    $xml_begin .= '<document xmlns:bbs="http://www.baidu.com/search/bbs_sitemap.xsd">' . "\n";
    $xml_begin .= xml_annotate();
    $xml_begin .= '<webSite>' . "{$lc_blog_url}" . '</webSite>' . "\n";
    $xml_begin .= '<webMaster>' . "{$lc_admin_email}" . '</webMaster>' . "\n";
    $xml_begin .= '<updatePeri>' . "{$lc_updatePeri}" . '</updatePeri>' . "\n";
    $xml_begin .= '<updatetime>' . "{$today_year}-{$today_month}-{$today_day} {$hour}:{$minute}:{$second}" . '</updatetime>' . "\n";
    $xml_begin .= '<version>' . "WordPress" . '</version>' . "\n";
    //echo $xml_begin;
    /** get the post title,ID,post_date from database **/
    $sql = "SELECT DISTINCT ID\r\n\t\tFROM {$wpdb->posts}\r\n\t\tLEFT JOIN {$wpdb->comments} ON ( {$wpdb->posts}.ID = {$wpdb->comments}.comment_post_ID ) \r\n\t\tWHERE post_password = ''\r\n\t\tAND post_type = 'post'\r\n\t\tAND post_status = 'publish'\r\n\t\tORDER BY {$lc_order_1} DESC \r\n\t\tLIMIT 0,{$lc_limits}";
    $recentposts = $wpdb->get_results($sql);
    if ($recentposts) {
        foreach ($recentposts as $post) {
            /** Post URL **/
            $permalink = EscapeXML(stripslashes_deep(get_permalink($post->ID)));
            if ($lc_blog_url) {
                $permalink = str_replace("{$blog_home}", "{$lc_blog_url}", $permalink);
            }
            //2009-11-25
            /** Post **/
            $my_post = get_post($post->ID, ARRAY_A);
            $post_title = EscapeXML(stripslashes_deep($my_post['post_title']));
            $post_date = $my_post['post_date'];
            /** show the comments info **/
            if ($lc_comments == '1') {
                $comment_count = $my_post['comment_count'];
                $comment_array = get_approved_comments($post->ID);
                if ($comment_array) {
                    $last_comment = array_pop($comment_array);
                    $my_comment = get_comment($last_comment->comment_ID, ARRAY_A);
                    $comment_date = $my_comment['comment_date'];
                } else {
                    $comment_date = $post_date;
                }
            }
            /** show the post_length **/
            if ($lc_post_length == '1') {
                $post_content_str = strlen($my_post['post_content']);
            }
            /** show the cat name **/
            if ($lc_post_cat == '1') {
                $category = get_the_category($post->ID);
                if (count($category) == '1') {
                    $my_cat = EscapeXML(stripslashes_deep($category[0]->cat_name));
                }
                if (count($category) == '2') {
                    $my_cat = EscapeXML(stripslashes_deep($category[0]->cat_name . "," . $category[1]->cat_name));
                }
                if (count($category) == '3') {
                    $my_cat = EscapeXML(stripslashes_deep($category[0]->cat_name . "," . $category[1]->cat_name . "," . $category[2]->cat_name));
                }
                if (count($category) == '4') {
                    $my_cat = EscapeXML(stripslashes_deep($category[0]->cat_name . "," . $category[1]->cat_name . "," . $category[2]->cat_name . "," . $category[3]->cat_name));
                }
                if (count($category) == '5') {
                    $my_cat = EscapeXML(stripslashes_deep($category[0]->cat_name . "," . $category[1]->cat_name . "," . $category[2]->cat_name . "," . $category[3]->cat_name . "," . $category[4]->cat_name));
                }
            }
            /** show the post_views **/
            if (function_exists('the_views')) {
                //echo "YES";
                //echo $post->ID."#";
                $sql_views = "SELECT DISTINCT meta_value as views\r\n\t\t\t\tFROM {$wpdb->postmeta}\r\n\t\t\t\tWHERE post_id = '{$post->ID}'\r\n\t\t\t\tAND meta_key = 'views'\r\n\t\t\t\tLIMIT 0,1";
                $post_views_array = $wpdb->get_results($sql_views);
                if ($post_views_array || !empty($post_views_array)) {
                    foreach ($post_views_array as $post_views) {
                        if ($post_views) {
                            $post_views = number_format_i18n(intval($post_views->views));
                        } else {
                            $post_views = '1';
                        }
                    }
                } else {
                    $post_views = '1';
                }
            }
            //echo $post_views.";";
            /** the pick post **/
            if (!empty($lc_pickcats_array) && $lc_pickcats_array[0] != '0' || !empty($lc_comments_count) && $lc_comments_count != '0' || !empty($lc_views_count) && ${$lc_views_count} != '0') {
                $pick = 0;
                if ($pick == '0') {
                    if (!empty($lc_pickcats_array) && $lc_pickcats_array[0] != '0') {
                        $cat_ID = array();
                        foreach (get_the_category($post->ID) as $category) {
                            array_push($cat_ID, $category->cat_ID);
                            if (array_intersect($cat_ID, $lc_pickcats_array)) {
                                $pick = '1';
                            }
                        }
                    }
                }
                if ($pick == '0') {
                    if (!empty($lc_comments_count) && $lc_comments_count != '0') {
                        if (empty($comment_count)) {
                            $comment_count = $my_post['comment_count'];
                        }
                        //get post_comment_count
                        if ($comment_count - $lc_comments_count >= '1') {
                            $pick = '1';
                        }
                    }
                }
                if ($pick == '0') {
                    if (!empty($lc_views_count) && function_exists('the_views') && $lc_views_count != '0') {
                        if ($post_views - $lc_views_count >= '1') {
                            $pick = '1';
                        }
                    }
                }
            }
            //echo $pick.";  ";
            $xml_middle = '<item>' . "\n";
            //$xml_middle .= '<link>'."$lc_blog_url".'/?p='."$post_ID".'</link>'."\n";
            $xml_middle .= '<link>' . $permalink . '</link>' . "\n";
            $xml_middle .= '<title>' . "{$post_title}" . '</title>' . "\n";
            $xml_middle .= '<pubDate>' . "{$post_date}" . '</pubDate>' . "\n";
            if ($lc_comments == '1') {
                $xml_middle .= '<bbs:lastDate>' . $comment_date . '</bbs:lastDate>' . "\n";
                $xml_middle .= '<bbs:reply>' . $comment_count . '</bbs:reply>' . "\n";
            }
            if (function_exists('the_views') && $lc_post_views == '1') {
                $xml_middle .= '<bbs:hit>' . $post_views . '</bbs:hit>' . "\n";
            }
            if ($lc_post_length == '1') {
                $xml_middle .= '<bbs:mainLen>' . $post_content_str . '</bbs:mainLen>' . "\n";
            }
            if ($lc_post_cat == '1') {
                $xml_middle .= '<bbs:boardid>' . $my_cat . '</bbs:boardid>' . "\n";
            }
            if (!empty($lc_pickcats_array) && $lc_pickcats_array[0] != '0' || !empty($lc_comments_count) && $lc_comments_count != '0' || !empty($lc_views_count) && ${$lc_views_count} != '0') {
                $xml_middle .= '<bbs:pick>' . $pick . '</bbs:pick>' . "\n";
            }
            $xml_middle .= '</item>' . "\n";
            $xml_middle_done .= $xml_middle;
            /** html_contents **/
            $comment_count = $my_post['comment_count'];
            if ($comment_count || $comment_count == 0) {
                $html_comment = str_replace("%html_comment%", $comment_count, __('Comments %html_comment%', 'baidu_sitemap'));
            }
            if ($post_views) {
                $html_views = str_replace("%html_views%", $post_views, __('&nbsp;&nbsp;&nbsp;Views %html_views%', 'baidu_sitemap'));
            } else {
                $html_views = '';
            }
            $html_content = '<li><a href="' . $permalink . '">' . $post_title . '</a>&nbsp;&nbsp;(' . $html_comment . '' . $html_views . ')</li>';
            $html_contents .= $html_content . "\n";
        }
    }
    /** XML_end **/
    $xml_end = '</document>';
    /** XML_ALL **/
    $baidu_xml = $xml_begin . $xml_middle_done . $xml_end;
    /** save XML file as sitemap_baidu.xml **/
    $GetHomePath = GetHomePath();
    $filename = $GetHomePath . 'sitemap_baidu.xml';
    if (IsFileWritable($GetHomePath) || IsFileWritable($filename)) {
        file_put_contents("{$filename}", "{$baidu_xml}");
        /** Messages  **/
        baidu_sitemap_topbarmessage(__('Congratulate, Build the XML file success', 'baidu_sitemap'));
    } else {
        /** Messages  **/
        baidu_sitemap_topbarmessage(__('Directory is not writable. please chmod your directory to 777.', 'baidu_sitemap'));
    }
    /** html sitemap Page **/
    if ($lc_sitemap_html = '1') {
        $blog_title = __('SiteMap', 'baidu_sitemap');
        $blog_name = get_bloginfo('name');
        $blog_keywords = $blog_title . ',' . $blog_name;
        $lc_generator = 'Baidu SiteMap Generator';
        $lc_author = 'Liucheng.Name';
        $lc_copyright = 'Liucheng.Name';
        $blog_home = get_bloginfo('url');
        $sitemap_url = get_bloginfo('url') . '/sitemap.html';
        $recentpost = __('RecentPost', 'baidu_sitemap');
        $footnote = __('HomePage', 'baidu_sitemap');
        $updated_time = "{$today_year}-{$today_month}-{$today_day} {$hour}:{$minute}:{$second}";
        if ($html_contents) {
            $path_html = GetPluginPath() . 'sitemap.html';
            $html = file_get_contents("{$path_html}");
            $html = str_replace("%blog_title%", $blog_title, $html);
            $html = str_replace("%blog_name%", $blog_name, $html);
            $html = str_replace("%blog_home%", $blog_home, $html);
            $html = str_replace("%blog_keywords%", $blog_keywords, $html);
            $html = str_replace("%lc_generator%", $lc_generator, $html);
            $html = str_replace("%lc_author%", $lc_author, $html);
            $html = str_replace("%lc_copyright%", $lc_copyright, $html);
            $html = str_replace("%sitemap_url%", $sitemap_url, $html);
            $html = str_replace("%footnote%", $footnote, $html);
            $html = str_replace("%RecentPost%", $recentpost, $html);
            $html = str_replace("%updated_time%", $updated_time, $html);
            $html = str_replace("%contents%", $html_contents, $html);
            $filename_html = $GetHomePath . 'sitemap.html';
            if (IsFileWritable($GetHomePath) || IsFileWritable($filename_html)) {
                file_put_contents("{$filename_html}", "{$html}");
                /** Messages  **/
                //baidu_sitemap_topbarmessage(__('Congratulate, Build the XML file success','baidu_sitemap'));
            }
        }
        //echo $html;
    }
    if (function_exists('wp_clear_scheduled_hook')) {
        wp_clear_scheduled_hook('do_this_auto');
    }
    if (function_exists('wp_clear_scheduled_hook')) {
        wp_clear_scheduled_hook('do_baidu_sitemap_2');
    }
    baidu_sitemap_is_auto();
}
function build_baidu_sitemap_html()
{
    global $wpdb;
    /** Get the current time **/
    $blogtime = current_time('mysql');
    list($today_year, $today_month, $today_day, $hour, $minute, $second) = split('([^0-9])', $blogtime);
    ##文章
    $html_contents = '';
    $post = query_posts('ignore_sticky_posts=1&posts_per_page=1000');
    while (have_posts()) {
        the_post();
        $html_contents .= '<li><a href="' . get_permalink() . '" title="' . get_the_title() . '" target="_blank">' . get_the_title() . '</a></li>';
    }
    $html_category_contents = wp_list_categories('echo=0');
    $html_page_contents = wp_list_pages('echo=0');
    $blog_title = __('SiteMap', 'baidu_sitemap');
    $blog_name = get_bloginfo('name');
    $blog_keywords = $blog_title . ',' . $blog_name;
    $lc_generator = 'Baidu SiteMap Generator';
    $lc_author = 'Liucheng.Name';
    $lc_copyright = 'Liucheng.Name';
    $blog_home = get_bloginfo('url');
    $sitemap_url = get_bloginfo('url') . '/sitemap.html';
    $recentpost = __('RecentPost', 'baidu_sitemap');
    $footnote = __('HomePage', 'baidu_sitemap');
    $updated_time = "{$today_year}-{$today_month}-{$today_day} {$hour}:{$minute}:{$second}";
    if ($html_contents) {
        $path_html = GetPluginPath() . 'sitemap.html';
        $html = file_get_contents("{$path_html}");
        $html = str_replace("%blog_title%", $blog_title, $html);
        $html = str_replace("%blog_name%", $blog_name, $html);
        $html = str_replace("%blog_home%", $blog_home, $html);
        $html = str_replace("%blog_keywords%", $blog_keywords, $html);
        $html = str_replace("%lc_generator%", $lc_generator, $html);
        $html = str_replace("%lc_author%", $lc_author, $html);
        $html = str_replace("%lc_copyright%", $lc_copyright, $html);
        $html = str_replace("%sitemap_url%", $sitemap_url, $html);
        $html = str_replace("%footnote%", $footnote, $html);
        $html = str_replace("%RecentPost%", $recentpost, $html);
        $html = str_replace("%updated_time%", $updated_time, $html);
        $html = str_replace("%contents%", $html_contents, $html);
        $html = str_replace("%Lc_category_contents%", $html_category_contents, $html);
        $html = str_replace("%Lc_page_contents%", $html_page_contents, $html);
        $GetHomePath = GetHomePath();
        $filename_html = $GetHomePath . 'sitemap.html';
        if (IsFileWritable($GetHomePath) || IsFileWritable($filename_html)) {
            file_put_contents("{$filename_html}", "{$html}");
            @chmod($filename_html, 0777);
            /** Messages  **/
            /*baidu_sitemap_topbarmessage(__('Congratulate, Build the Html file success','baidu_sitemap'));*/
        }
    }
}