+ * @return array An array with messages such as failed writes etc.
+ */
+ function BuildSitemap() {
+ global $wpdb, $posts, $wp_version;
+ $this->Initate();
+
+ if($this->GetOption("b_memory")!='') {
+ @ini_set("memory_limit",$this->GetOption("b_memory"));
+ }
+
+ if($this->GetOption("b_time")!=-1) {
+ @set_time_limit($this->GetOption("b_time"));
+ }
+
+ //This object saves the status information of the script directly to the database
+ $status = new GoogleSitemapGeneratorStatus();
+
+ //Other plugins can detect if the building process is active
+ $this->_isActive = true;
+
+ //$this->AddElement(new GoogleSitemapGeneratorXmlEntry());
+
+ //Debug mode?
+ $debug=$this->GetOption("b_debug");
+
+ if($this->GetOption("b_xml")) {
+ $fileName = $this->GetXmlPath();
+ $status->StartXml($this->GetXmlPath(),$this->GetXmlUrl());
+
+ if($this->IsFileWritable($fileName)) {
+
+ $this->_fileHandle = fopen($fileName,"w");
+ if(!$this->_fileHandle) $status->EndXml(false,"Not openable");
+
+ } else $status->EndXml(false,"not writable");
+ }
+
+ //Write gzipped sitemap file
+ if($this->IsGzipEnabled()) {
+ $fileName = $this->GetZipPath();
+ $status->StartZip($this->GetZipPath(),$this->GetZipUrl());
+
+ if($this->IsFileWritable($fileName)) {
+
+ $this->_fileZipHandle = gzopen($fileName,"w1");
+ if(!$this->_fileZipHandle) $status->EndZip(false,"Not openable");
+
+ } else $status->EndZip(false,"not writable");
+ }
+
+ if(!$this->_fileHandle && !$this->_fileZipHandle) {
+ $status->End();
+ return;
+ }
+
+
+ //Content of the XML file
+ $this->AddElement(new GoogleSitemapGeneratorXmlEntry(''));
+
+ $styleSheet = ($this->GetDefaultStyle() && $this->GetOption('b_style_default')===true?$this->GetDefaultStyle():$this->GetOption('b_style'));
+
+ if(!empty($styleSheet)) {
+ $this->AddElement(new GoogleSitemapGeneratorXmlEntry('<' . '?xml-stylesheet type="text/xsl" href="' . $styleSheet . '"?' . '>'));
+ }
+
+ $this->AddElement(new GoogleSitemapGeneratorDebugEntry("generator=\"wordpress/" . get_bloginfo('version') . "\""));
+ $this->AddElement(new GoogleSitemapGeneratorDebugEntry("sitemap-generator-url=\"http://www.arnebrachhold.de\" sitemap-generator-version=\"" . $this->GetVersion() . "\""));
+ $this->AddElement(new GoogleSitemapGeneratorDebugEntry("generated-on=\"" . date(get_option("date_format") . " " . get_option("time_format")) . "\""));
+
+ //All comments as an asso. Array (postID=>commentCount)
+ $comments=($this->GetOption("b_prio_provider")!=""?$this->GetComments():array());
+
+ //Full number of comments
+ $commentCount=(count($comments)>0?$this->GetCommentCount($comments):0);
+
+ if($debug && $this->GetOption("b_prio_provider")!="") {
+ $this->AddElement(new GoogleSitemapGeneratorDebugEntry("Debug: Total comment count: " . $commentCount));
+ }
+
+ //Go XML!
+ $this->AddElement(new GoogleSitemapGeneratorXmlEntry(''));
+
+ $home = get_bloginfo('url');
+
+ $homePid = 0;
+
+ //Add the home page (WITH a slash!)
+ if($this->GetOption("in_home")) {
+ if('page' == get_option('show_on_front') && get_option('page_on_front')) {
+ $pageOnFront = get_option('page_on_front');
+ $p = get_page($pageOnFront);
+ if($p) {
+ $homePid = $p->ID;
+ $this->AddUrl(trailingslashit($home),$this->GetTimestampFromMySql(($p->post_modified_gmt && $p->post_modified_gmt!='0000-00-00 00:00:00'?$p->post_modified_gmt:$p->post_date_gmt)),$this->GetOption("cf_home"),$this->GetOption("pr_home"));
+ }
+ } else {
+ $this->AddUrl(trailingslashit($home),$this->GetTimestampFromMySql(get_lastpostmodified('GMT')),$this->GetOption("cf_home"),$this->GetOption("pr_home"));
+ }
+ }
+
+ //Add the posts
+ if($this->GetOption("in_posts") || $this->GetOption("in_pages")) {
+
+ if($debug) $this->AddElement(new GoogleSitemapGeneratorDebugEntry("Debug: Start Postings"));
+
+ //Pre 2.1 compatibility. 2.1 introduced 'future' as post_status so we don't need to check post_date
+ $wpCompat = (floatval($wp_version) < 2.1);
+
+ $useQTransLate = false; //function_exists('qtrans_convertURL') && function_exists('qtrans_getEnabledLanguages'); Not really working yet
+
+ $excludes = $this->GetOption('b_exclude'); //Excluded posts
+
+ $exclCats = $this->GetOption("b_exclude_cats"); // Excluded cats
+
+ if($exclCats && count($exclCats)>0 && $this->IsTaxonomySupported()) {
+
+ $exPosts = get_objects_in_term($exclCats,"category"); // Get all posts in excl. cats
+
+ if(is_array($exPosts) && count($exPosts) > 0) { //Merge it with the list of user excluded posts
+ $excludes = array_merge($excludes, $exPosts);
+ }
+ }
+
+
+ $contentStmt = '';
+ if($useQTransLate) {
+ $contentStmt.=', post_content ';
+ }
+
+ $postPageStmt = '';
+
+ $inSubPages = ($this->GetOption('in_posts_sub')===true);
+
+ if($inSubPages && $this->GetOption('in_posts')===true) {
+ $pageDivider='';
+ $postPageStmt = ", (character_length(`post_content`) - character_length(REPLACE(`post_content`, '$pageDivider', ''))) / " . strlen($pageDivider) . " as postPages";
+ }
+
+ $sql="SELECT `ID`, `post_author`, `post_date`, `post_date_gmt`, `post_status`, `post_name`, `post_modified`, `post_modified_gmt`, `post_parent`, `post_type` $postPageStmt $contentStmt FROM `" . $wpdb->posts . "` WHERE ";
+
+ $where = '(';
+
+ if($this->GetOption('in_posts')) {
+ //WP < 2.1: posts are post_status = publish
+ //WP >= 2.1: post_type must be 'post', no date check required because future posts are post_status='future'
+ if($wpCompat) $where.="(post_status = 'publish' AND post_date_gmt <= '" . gmdate('Y-m-d H:i:59') . "')";
+ else $where.=" (post_status = 'publish' AND (post_type = 'post' OR post_type = '')) ";
+ }
+
+ if($this->GetOption('in_pages')) {
+ if($this->GetOption('in_posts')) {
+ $where.=" OR ";
+ }
+ if($wpCompat) {
+ //WP < 2.1: posts have post_status = published, pages have post_status = static
+ $where.=" post_status='static' ";
+ } else {
+ //WP >= 2.1: posts have post_type = 'post' and pages have post_type = 'page'. Both must be published.
+ $where.=" (post_status = 'publish' AND post_type = 'page') ";
+ }
+ }
+
+ $where.=") ";
+
+
+ if(is_array($excludes) && count($excludes)>0) {
+ $where.=" AND ID NOT IN ('" . implode("','",$excludes) . "')";
+ }
+
+ $where.=" AND post_password='' ORDER BY post_modified DESC";
+
+ $sql .= $where;
+
+ if($this->GetOption("b_max_posts")>0) {
+ $sql.=" LIMIT 0," . $this->GetOption("b_max_posts");
+ }
+
+ $postCount = intval($wpdb->get_var("SELECT COUNT(*) AS cnt FROM `" . $wpdb->posts . "` WHERE ". $where,0,0));
+
+ //Create a new connection because we are using mysql_unbuffered_query and don't want to disturb the WP connection
+ //Safe Mode for other plugins which use mysql_query() without a connection handler and will destroy our resultset :(
+ $con = $postRes = null;
+
+ //In 2.2, a bug which prevented additional DB connections was fixed
+ if(floatval($wp_version) < 2.2) {
+ $this->SetOption("b_safemode",true);
+ }
+
+ if($this->GetOption("b_safemode")===true) {
+ $postRes = mysql_query($sql,$wpdb->dbh);
+ if(!$postRes) {
+ trigger_error("MySQL query failed: " . mysql_error(),E_USER_NOTICE); //E_USER_NOTICE will be displayed on our debug mode
+ return;
+ }
+ } else {
+ $con = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD,true);
+ if(!$con) {
+ trigger_error("MySQL Connection failed: " . mysql_error(),E_USER_NOTICE);
+ return;
+ }
+ if(!mysql_select_db(DB_NAME,$con)) {
+ trigger_error("MySQL DB Select failed: " . mysql_error(),E_USER_NOTICE);
+ return;
+ }
+ $postRes = mysql_unbuffered_query($sql,$con);
+
+ if(!$postRes) {
+ trigger_error("MySQL unbuffered query failed: " . mysql_error(),E_USER_NOTICE);
+ return;
+ }
+ }
+
+ if($postRes) {
+
+ //#type $prioProvider GoogleSitemapGeneratorPrioProviderBase
+ $prioProvider=NULL;
+
+ if($this->GetOption("b_prio_provider") != '') {
+ $providerClass=$this->GetOption('b_prio_provider');
+ $prioProvider = new $providerClass($commentCount,$postCount);
+ }
+
+ //$posts is used by Alex King's Popularity Contest plugin
+ //if($posts == null || !is_array($posts)) {
+ // $posts = &$postRes;
+ //}
+
+ $z = 1;
+ $zz = 1;
+
+ //Default priorities
+ $default_prio_posts = $this->GetOption('pr_posts');
+ $default_prio_pages = $this->GetOption('pr_pages');
+
+ //Change frequencies
+ $cf_pages = $this->GetOption('cf_pages');
+ $cf_posts = $this->GetOption('cf_posts');
+
+ $minPrio=$this->GetOption('pr_posts_min');
+
+
+ //Cycle through all posts and add them
+ while($post = mysql_fetch_object($postRes)) {
+
+ //Fill the cache with our DB result. Since it's incomplete (no text-content for example), we will clean it later.
+ $cache = array(&$post);
+ update_post_cache($cache);
+
+ //Set the current working post for other plugins which depend on "the loop"
+ $GLOBALS['post'] = &$post;
+
+ $permalink = get_permalink($post->ID);
+ if($permalink != $home && $post->ID != $homePid) {
+
+ $isPage = false;
+ if($wpCompat) {
+ $isPage = ($post->post_status == 'static');
+ } else {
+ $isPage = ($post->post_type == 'page');
+ }
+
+
+ //Default Priority if auto calc is disabled
+ $prio = 0;
+
+ if($isPage) {
+ //Priority for static pages
+ $prio = $default_prio_pages;
+ } else {
+ //Priority for normal posts
+ $prio = $default_prio_posts;
+ }
+
+ //If priority calc. is enabled, calculate (but only for posts, not pages)!
+ if($prioProvider !== null && !$isPage) {
+
+ //Comment count for this post
+ $cmtcnt = (isset($comments[$post->ID])?$comments[$post->ID]:0);
+ $prio = $prioProvider->GetPostPriority($post->ID, $cmtcnt, $post);
+
+ if($debug) $this->AddElement(new GoogleSitemapGeneratorDebugEntry('Debug: Priority report of postID ' . $post->ID . ': Comments: ' . $cmtcnt . ' of ' . $commentCount . ' = ' . $prio . ' points'));
+ }
+
+ if(!$isPage && $minPrio>0 && $prio<$minPrio) {
+ $prio = $minPrio;
+ }
+
+ //Add it
+ $this->AddUrl($permalink,$this->GetTimestampFromMySql(($post->post_modified_gmt && $post->post_modified_gmt!='0000-00-00 00:00:00'?$post->post_modified_gmt:$post->post_date_gmt)),($isPage?$cf_pages:$cf_posts),$prio);
+
+ if($inSubPages) {
+ $subPage = '';
+ for($p = 1; $p <= $post->postPages; $p++) {
+ if(get_option('permalink_structure') == '') {
+ $subPage = $permalink . '&page=' . ($p+1);
+ } else {
+ $subPage = trailingslashit($permalink) . user_trailingslashit($p+1, 'single_paged');
+ }
+
+ $this->AddUrl($subPage,$this->GetTimestampFromMySql(($post->post_modified_gmt && $post->post_modified_gmt!='0000-00-00 00:00:00'?$post->post_modified_gmt:$post->post_date_gmt)),($isPage?$cf_pages:$cf_posts),$prio);
+ }
+ }
+
+ // Multilingual Support with qTranslate, thanks to Qian Qin
+ if($useQTransLate) {
+ global $q_config;
+ foreach(qtrans_getEnabledLanguages($post->post_content) as $language) {
+ if($language!=$q_config['default_language']) {
+ $this->AddUrl(qtrans_convertURL($permalink,$language),$this->GetTimestampFromMySql(($post->post_modified_gmt && $post->post_modified_gmt!='0000-00-00 00:00:00'?$post->post_modified_gmt:$post->post_date_gmt)),($isPage?$cf_pages:$cf_posts),$prio);
+ }
+ }
+ }
+ }
+
+ //Update the status every 100 posts and at the end.
+ //If the script breaks because of memory or time limit,
+ //we have a "last reponded" value which can be compared to the server settings
+ if($zz==100 || $z == $postCount) {
+ $status->SaveStep($z);
+ $zz=0;
+ } else $zz++;
+
+ $z++;
+
+ //Clean cache because it's incomplete
+ if(version_compare($wp_version,"2.5",">=")) {
+ //WP 2.5 makes a mysql query for every clean_post_cache to clear the child cache
+ //so I've copied the function here until a patch arrives...
+ wp_cache_delete($post->ID, 'posts');
+ wp_cache_delete($post->ID, 'post_meta');
+ clean_object_term_cache($post->ID, 'post');
+ } else {
+ clean_post_cache($post->ID);
+ }
+ }
+ unset($postRes);
+ unset($prioProvider);
+
+ if($this->GetOption("b_safemode")!==true && $con) mysql_close($con);
+ }
+ if($debug) $this->AddElement(new GoogleSitemapGeneratorDebugEntry("Debug: End Postings"));
+ }
+
+ //Add the cats
+ if($this->GetOption("in_cats")) {
+ if($debug) $this->AddElement(new GoogleSitemapGeneratorDebugEntry("Debug: Start Cats"));
+
+ $exclCats = $this->GetOption("b_exclude_cats"); // Excluded cats
+ if($exclCats == null) $exclCats=array();
+
+ if(!$this->IsTaxonomySupported()) {
+
+ $catsRes=$wpdb->get_results("
+ SELECT
+ c.cat_ID AS ID,
+ MAX(p.post_modified_gmt) AS last_mod
+ FROM
+ `" . $wpdb->categories . "` c,
+ `" . $wpdb->post2cat . "` pc,
+ `" . $wpdb->posts . "` p
+ WHERE
+ pc.category_id = c.cat_ID
+ AND p.ID = pc.post_id
+ AND p.post_status = 'publish'
+ AND p.post_type='post'
+ GROUP
+ BY c.cat_id
+ ");
+ if($catsRes) {
+ foreach($catsRes as $cat) {
+ if($cat && $cat->ID && $cat->ID>0 && !in_array($cat->ID, $exclCats)) {
+ if($debug) if($debug) $this->AddElement(new GoogleSitemapGeneratorDebugEntry("Cat-ID:" . $cat->ID));
+ $this->AddUrl(get_category_link($cat->ID),$this->GetTimestampFromMySql($cat->last_mod),$this->GetOption("cf_cats"),$this->GetOption("pr_cats"));
+ }
+ }
+ }
+ } else {
+ $cats = get_terms("category",array("hide_empty"=>true,"hierarchical"=>false));
+ if($cats && is_array($cats) && count($cats)>0) {
+ foreach($cats AS $cat) {
+ if(!in_array($cat->term_id, $exclCats)) $this->AddUrl(get_category_link($cat->term_id),0,$this->GetOption("cf_cats"),$this->GetOption("pr_cats"));
+ }
+ }
+ }
+ if($debug) $this->AddElement(new GoogleSitemapGeneratorDebugEntry("Debug: End Cats"));
+ }
+
+ //Add the archives
+ if($this->GetOption("in_arch")) {
+ if($debug) $this->AddElement(new GoogleSitemapGeneratorDebugEntry("Debug: Start Archive"));
+ $now = current_time('mysql');
+
+ //WP2.1 introduced post_status='future', for earlier WP versions we need to check the post_date_gmt
+ $arcresults = $wpdb->get_results("
+ SELECT DISTINCT
+ YEAR(post_date_gmt) AS `year`,
+ MONTH(post_date_gmt) AS `month`,
+ MAX(post_date_gmt) as last_mod,
+ count(ID) as posts
+ FROM
+ $wpdb->posts
+ WHERE
+ post_date < '$now'
+ AND post_status = 'publish'
+ AND post_type = 'post'
+ " . (floatval($wp_version) < 2.1?"AND {$wpdb->posts}.post_date_gmt <= '" . gmdate('Y-m-d H:i:59') . "'":"") . "
+ GROUP BY
+ YEAR(post_date_gmt),
+ MONTH(post_date_gmt)
+ ORDER BY
+ post_date_gmt DESC");
+ if ($arcresults) {
+ foreach ($arcresults as $arcresult) {
+
+ $url = get_month_link($arcresult->year, $arcresult->month);
+ $changeFreq="";
+
+ //Archive is the current one
+ if($arcresult->month==date("n") && $arcresult->year==date("Y")) {
+ $changeFreq=$this->GetOption("cf_arch_curr");
+ } else { // Archive is older
+ $changeFreq=$this->GetOption("cf_arch_old");
+ }
+
+ $this->AddUrl($url,$this->GetTimestampFromMySql($arcresult->last_mod),$changeFreq,$this->GetOption("pr_arch"));
+ }
+ }
+ if($debug) $this->AddElement(new GoogleSitemapGeneratorDebugEntry("Debug: End Archive"));
+ }
+
+ //Add the author pages
+ if($this->GetOption("in_auth")) {
+ if($debug) $this->AddElement(new GoogleSitemapGeneratorDebugEntry("Debug: Start Author pages"));
+
+ $linkFunc = null;
+
+ //get_author_link is deprecated in WP 2.1, try to use get_author_posts_url first.
+ if(function_exists('get_author_posts_url')) {
+ $linkFunc = 'get_author_posts_url';
+ } else if(function_exists('get_author_link')) {
+ $linkFunc = 'get_author_link';
+ }
+
+ //Who knows what happens in later WP versions, so check again if it worked
+ if($linkFunc !== null) {
+ //Unfortunately there is no API function to get all authors, so we have to do it the dirty way...
+ //We retrieve only users with published and not password protected posts (and not pages)
+ //WP2.1 introduced post_status='future', for earlier WP versions we need to check the post_date_gmt
+ $sql = "SELECT DISTINCT
+ p.ID,
+ u.user_nicename,
+ MAX(p.post_modified_gmt) AS last_post
+ FROM
+ {$wpdb->users} u,
+ {$wpdb->posts} p
+ WHERE
+ p.post_author = u.ID
+ AND p.post_status = 'publish'
+ AND p.post_type = 'post'
+ AND p.post_password = ''
+ " . (floatval($wp_version) < 2.1?"AND p.post_date_gmt <= '" . gmdate('Y-m-d H:i:59') . "'":"") . "
+ GROUP BY
+ u.ID,
+ u.user_nicename";
+
+ $authors = $wpdb->get_results($sql);
+
+ if($authors && is_array($authors)) {
+ foreach($authors as $author) {
+ if($debug) if($debug) $this->AddElement(new GoogleSitemapGeneratorDebugEntry("Author-ID:" . $author->ID));
+ $url = ($linkFunc=='get_author_posts_url'?get_author_posts_url($author->ID,$author->user_nicename):get_author_link(false,$author->ID,$author->user_nicename));
+ $this->AddUrl($url,$this->GetTimestampFromMySql($author->last_post),$this->GetOption("cf_auth"),$this->GetOption("pr_auth"));
+ }
+ }
+ } else {
+ //Too bad, no author pages for you :(
+ if($debug) $this->AddElement(new GoogleSitemapGeneratorDebugEntry("Debug: No valid author link function found"));
+ }
+
+ if($debug) $this->AddElement(new GoogleSitemapGeneratorDebugEntry("Debug: End Author pages"));
+ }
+
+ //Add tag pages
+ if($this->GetOption("in_tags") && $this->IsTaxonomySupported()) {
+ if($debug) $this->AddElement(new GoogleSitemapGeneratorDebugEntry("Debug: Start Tags"));
+ $tags = get_terms("post_tag",array("hide_empty"=>true,"hierarchical"=>false));
+ if($tags && is_array($tags) && count($tags)>0) {
+ foreach($tags AS $tag) {
+ $this->AddUrl(get_tag_link($tag->term_id),0,$this->GetOption("cf_tags"),$this->GetOption("pr_tags"));
+ }
+ }
+ if($debug) $this->AddElement(new GoogleSitemapGeneratorDebugEntry("Debug: End Tags"));
+ }
+
+ //Add custom taxonomy pages
+ if($this->GetOption("in_tax") && $this->IsTaxonomySupported()) {
+
+ if($debug) $this->AddElement(new GoogleSitemapGeneratorDebugEntry("Debug: Start custom taxonomies"));
+
+ $enabledTaxonomies = $this->GetOption("in_tax");
+
+ $taxList = array();
+
+ foreach ($enabledTaxonomies as $taxName) {
+ $taxonomy = get_taxonomy($taxName);
+ if($taxonomy) $taxList[] = $wpdb->escape($taxonomy->name);
+ }
+
+ if(count($taxList)>0) {
+ //We're selecting all term information (t.*) plus some additional fields
+ //like the last mod date and the taxonomy name, so WP doesnt need to make
+ //additional queries to build the permalink structure.
+ //This does NOT work for categories and tags yet, because WP uses get_category_link
+ //and get_tag_link internally and that would cause one additional query per term!
+ $sql="
+ SELECT
+ t.*,
+ tt.taxonomy AS _taxonomy,
+ UNIX_TIMESTAMP(MAX(post_date_gmt)) as _mod_date
+ FROM
+ {$wpdb->posts} p ,
+ {$wpdb->term_relationships} r,
+ {$wpdb->terms} t,
+ {$wpdb->term_taxonomy} tt
+ WHERE
+ p.ID = r.object_id
+ AND p.post_status = 'publish'
+ AND p.post_type = 'post'
+ AND p.post_password = ''
+ AND r.term_taxonomy_id = t.term_id
+ AND t.term_id = tt.term_id
+ AND tt.count > 0
+ AND tt.taxonomy IN ('" . implode("','",$taxList) . "')
+ GROUP BY
+ t.term_id";
+
+ $termInfo = $wpdb->get_results($sql);
+
+ foreach($termInfo AS $term) {
+ $this->AddUrl(get_term_link($term,$term->_taxonomy),$term->_mod_date ,$this->GetOption("cf_tags"),$this->GetOption("pr_tags"));
+ }
+ }
+
+ if($debug) $this->AddElement(new GoogleSitemapGeneratorDebugEntry("Debug: End custom taxonomies"));
+ }
+
+ //Add the custom pages
+ if($debug) $this->AddElement(new GoogleSitemapGeneratorDebugEntry("Debug: Start Custom Pages"));
+ if($this->_pages && is_array($this->_pages) && count($this->_pages)>0) {
+ //#type $page GoogleSitemapGeneratorPage
+ foreach($this->_pages AS $page) {
+ $this->AddUrl($page->GetUrl(),$page->getLastMod(),$page->getChangeFreq(),$page->getPriority());
+ }
+ }
+
+ if($debug) $this->AddElement(new GoogleSitemapGeneratorDebugEntry("Debug: End Custom Pages"));
+
+ if($debug) $this->AddElement(new GoogleSitemapGeneratorDebugEntry("Debug: Start additional URLs"));
+
+ do_action('sm_buildmap');
+
+ if($debug) $this->AddElement(new GoogleSitemapGeneratorDebugEntry("Debug: End additional URLs"));
+
+ $this->AddElement(new GoogleSitemapGeneratorXmlEntry(" "));
+
+
+ $pingUrl='';
+
+ if($this->GetOption("b_xml")) {
+ if($this->_fileHandle && fclose($this->_fileHandle)) {
+ $this->_fileHandle = null;
+ $status->EndXml(true);
+ $pingUrl=$this->GetXmlUrl();
+ } else $status->EndXml(false,"Could not close the sitemap file.");
+ }
+
+ if($this->IsGzipEnabled()) {
+ if($this->_fileZipHandle && fclose($this->_fileZipHandle)) {
+ $this->_fileZipHandle = null;
+ $status->EndZip(true);
+ $pingUrl=$this->GetZipUrl();
+ } else $status->EndZip(false,"Could not close the zipped sitemap file");
+ }
+
+ //Ping Google
+ if($this->GetOption("b_ping") && !empty($pingUrl)) {
+ $sPingUrl="http://www.google.com/webmasters/sitemaps/ping?sitemap=" . urlencode($pingUrl);
+ $status->StartGooglePing($sPingUrl);
+ $pingres=$this->RemoteOpen($sPingUrl);
+
+ if($pingres==NULL || $pingres===false) {
+ $status->EndGooglePing(false,$this->_lastError);
+ trigger_error("Failed to ping Google: " . htmlspecialchars(strip_tags($pingres)),E_USER_NOTICE);
+ } else {
+ $status->EndGooglePing(true);
+ }
+ }
+
+ //Ping Ask.com
+ if($this->GetOption("b_pingask") && !empty($pingUrl)) {
+ $sPingUrl="http://submissions.ask.com/ping?sitemap=" . urlencode($pingUrl);
+ $status->StartAskPing($sPingUrl);
+ $pingres=$this->RemoteOpen($sPingUrl);
+
+ if($pingres==NULL || $pingres===false || strpos($pingres,"successfully received and added")===false) { //Ask.com returns 200 OK even if there was an error, so we need to check the content.
+ $status->EndAskPing(false,$this->_lastError);
+ trigger_error("Failed to ping Ask.com: " . htmlspecialchars(strip_tags($pingres)),E_USER_NOTICE);
+ } else {
+ $status->EndAskPing(true);
+ }
+ }
+
+ //Ping YAHOO
+ if($this->GetOption("b_pingyahoo")===true && $this->GetOption("b_yahookey")!="" && !empty($pingUrl)) {
+ $sPingUrl="http://search.yahooapis.com/SiteExplorerService/V1/updateNotification?appid=" . $this->GetOption("b_yahookey") . "&url=" . urlencode($pingUrl);
+ $status->StartYahooPing($sPingUrl);
+ $pingres=$this->RemoteOpen($sPingUrl);
+
+ if($pingres==NULL || $pingres===false || strpos(strtolower($pingres),"success")===false) {
+ trigger_error("Failed to ping YAHOO: " . htmlspecialchars(strip_tags($pingres)),E_USER_NOTICE);
+ $status->EndYahooPing(false,$this->_lastError);
+ } else {
+ $status->EndYahooPing(true);
+ }
+ }
+
+ //Ping Bing
+ if($this->GetOption("b_pingmsn") && !empty($pingUrl)) {
+ $sPingUrl="http://www.bing.com/webmaster/ping.aspx?siteMap=" . urlencode($pingUrl);
+ $status->StartMsnPing($sPingUrl);
+ $pingres=$this->RemoteOpen($sPingUrl);
+
+ if($pingres==NULL || $pingres===false || strpos($pingres,"Thanks for submitting your sitemap")===false) {
+ trigger_error("Failed to ping Bing: " . htmlspecialchars(strip_tags($pingres)),E_USER_NOTICE);
+ $status->EndMsnPing(false,$this->_lastError);
+ } else {
+ $status->EndMsnPing(true);
+ }
+ }
+
+ $status->End();
+
+
+ $this->_isActive = false;
+
+ //done...
+ return $status;
+ }
+
+ /**
+ * Tries to ping a specific service showing as much as debug output as possible
+ * @since 3.1.9
+ * @return null
+ */
+ function ShowPingResult() {
+
+ check_admin_referer('sitemap');
+
+ if(!current_user_can("administrator")) {
+ echo 'Please log in as admin
';
+ return;
+ }
+
+ $service = !empty($_GET["sm_ping_service"])?$_GET["sm_ping_service"]:null;
+
+ $status = GoogleSitemapGeneratorStatus::Load();
+
+ if(!$status) die("No build status yet. Build the sitemap first.");
+
+ $url = null;
+
+ switch($service) {
+ case "google":
+ $url = $status->_googleUrl;
+ break;
+ case "yahoo":
+ $url = $status->_yahooUrl;
+ break;
+ case "msn":
+ $url = $status->_msnUrl;
+ break;
+ case "ask":
+ $url = $status->_askUrl;
+ break;
+ }
+
+ if(empty($url)) die("Invalid ping url");
+
+ echo 'Ping Test ';
+ if(function_exists('wp_admin_css')) wp_admin_css('css/global',true);
+ echo 'Ping Test ';
+
+ echo 'Trying to ping: ' . $url . ' . The sections below should give you an idea whats going on.
';
+
+ //Try to get as much as debug / error output as possible
+ $errLevel = error_reporting(E_ALL);
+ $errDisplay = ini_set("display_errors",1);
+ if(!defined('WP_DEBUG')) define('WP_DEBUG',true);
+
+ echo 'Errors, Warnings, Notices: ';
+
+ if(WP_DEBUG == false) echo "WP_DEBUG was set to false somewhere before. You might not see all debug information until you remove this declaration! ";
+ if(ini_get("display_errors")!=1) echo "Your display_errors setting currently prevents the plugin from showing errors here. Please check your webserver logfile instead. ";
+
+ $res = $this->RemoteOpen($url);
+
+ echo 'Result (text only): ';
+
+ echo wp_kses($res,array('a' => array('href' => array()),'p' => array(), 'ul' => array(), 'ol' => array(), 'li' => array()));
+
+ echo 'Result (HTML): ';
+
+ echo htmlspecialchars($res);
+
+ //Revert back old values
+ error_reporting($errLevel);
+ ini_set("display_errors",$errDisplay);
+ echo '';
+ exit;
+ }
+
+ /**
+ * Opens a remote file using the WordPress API or Snoopy
+ * @since 3.0
+ * @param $url The URL to open
+ * @param $method get or post
+ * @param $postData An array with key=>value paris
+ * @param $timeout Timeout for the request, by default 10
+ * @return mixed False on error, the body of the response on success
+ */
+ function RemoteOpen($url,$method = 'get', $postData = null, $timeout = 10) {
+ global $wp_version;
+
+ //Before WP 2.7, wp_remote_fopen was quite crappy so Snoopy was favoured.
+ if(floatval($wp_version) < 2.7) {
+ if(!file_exists(ABSPATH . 'wp-includes/class-snoopy.php')) {
+ trigger_error('Snoopy Web Request failed: Snoopy not found.',E_USER_NOTICE);
+ return false; //Hoah?
+ }
+
+ require_once( ABSPATH . 'wp-includes/class-snoopy.php');
+
+ $s = new Snoopy();
+
+ $s->read_timeout = $timeout;
+
+ if($method == 'get') {
+ $s->fetch($url);
+ } else {
+ $s->submit($url,$postData);
+ }
+
+ if($s->status != "200") {
+ trigger_error('Snoopy Web Request failed: Status: ' . $s->status . "; Content: " . htmlspecialchars($s->results),E_USER_NOTICE);
+ }
+
+ return $s->results;
+
+ } else {
+
+ $options = array();
+ $options['timeout'] = $timeout;
+
+ if($method == 'get') {
+ $response = wp_remote_get( $url, $options );
+ } else {
+ $response = wp_remote_post($url, array_merge($options,array('body'=>$postData)));
+ }
+
+ if ( is_wp_error( $response ) ) {
+ $errs = $response->get_error_messages();
+ $errs = htmlspecialchars(implode('; ', $errs));
+ trigger_error('WP HTTP API Web Request failed: ' . $errs,E_USER_NOTICE);
+ return false;
+ }
+
+ return $response['body'];
+ }
+
+ return false;
+ }
+
+ /**
+ * Echos option fields for an select field containing the valid change frequencies
+ *
+ * @since 3.0
+ * @access private
+ * @author Arne Brachhold
+ * @param $currentVal The value which should be selected
+ * @return all valid change frequencies as html option fields
+ */
+ function HtmlGetFreqNames($currentVal) {
+
+ foreach($this->_freqNames AS $k=>$v) {
+ echo "HtmlGetSelected($k,$currentVal) .">" . $v . " ";
+ }
+ }
+
+ /**
+ * Echos option fields for an select field containing the valid priorities (0- 1.0)
+ *
+ * @since 3.0
+ * @access private
+ * @author Arne Brachhold
+ * @param $currentVal string The value which should be selected
+ * @return 0.0 - 1.0 as html option fields
+ */
+ function HtmlGetPriorityValues($currentVal) {
+ $currentVal=(float) $currentVal;
+ for($i=0.0; $i<=1.0; $i+=0.1) {
+ $v = number_format($i,1,".","");
+ //number_format_i18n is there since WP 2.3
+ $t = function_exists('number_format_i18n')?number_format_i18n($i,1):number_format($i,1);
+ echo "HtmlGetSelected("$i","$currentVal") .">";
+ echo $t;
+ echo " ";
+ }
+ }
+
+ /**
+ * Returns the checked attribute if the given values match
+ *
+ * @since 3.0
+ * @access private
+ * @author Arne Brachhold
+ * @param $val string The current value
+ * @param $equals string The value to match
+ * @return The checked attribute if the given values match, an empty string if not
+ */
+ function HtmlGetChecked($val,$equals) {
+ if($val==$equals) return $this->HtmlGetAttribute("checked");
+ else return "";
+ }
+
+ /**
+ * Returns the selected attribute if the given values match
+ *
+ * @since 3.0
+ * @access private
+ * @author Arne Brachhold
+ * @param $val string The current value
+ * @param $equals string The value to match
+ * @return The selected attribute if the given values match, an empty string if not
+ */
+ function HtmlGetSelected($val,$equals) {
+ if($val==$equals) return $this->HtmlGetAttribute("selected");
+ else return "";
+ }
+
+ /**
+ * Returns an formatted attribute. If the value is NULL, the name will be used.
+ *
+ * @since 3.0
+ * @access private
+ * @author Arne Brachhold
+ * @param $attr string The attribute name
+ * @param $value string The attribute value
+ * @return The formatted attribute
+ */
+ function HtmlGetAttribute($attr,$value=NULL) {
+ if($value==NULL) $value=$attr;
+ return " " . $attr . "=\"" . $value . "\" ";
+ }
+
+ /**
+ * Returns an array with GoogleSitemapGeneratorPage objects which is generated from POST values
+ *
+ * @since 3.0
+ * @see GoogleSitemapGeneratorPage
+ * @access private
+ * @author Arne Brachhold
+ * @return array An array with GoogleSitemapGeneratorPage objects
+ */
+ function HtmlApplyPages() {
+ // Array with all page URLs
+ $pages_ur=(!isset($_POST["sm_pages_ur"]) || !is_array($_POST["sm_pages_ur"])?array():$_POST["sm_pages_ur"]);
+
+ //Array with all priorities
+ $pages_pr=(!isset($_POST["sm_pages_pr"]) || !is_array($_POST["sm_pages_pr"])?array():$_POST["sm_pages_pr"]);
+
+ //Array with all change frequencies
+ $pages_cf=(!isset($_POST["sm_pages_cf"]) || !is_array($_POST["sm_pages_cf"])?array():$_POST["sm_pages_cf"]);
+
+ //Array with all lastmods
+ $pages_lm=(!isset($_POST["sm_pages_lm"]) || !is_array($_POST["sm_pages_lm"])?array():$_POST["sm_pages_lm"]);
+
+ //Array where the new pages are stored
+ $pages=array();
+ //Loop through all defined pages and set their properties into an object
+ if(isset($_POST["sm_pages_mark"]) && is_array($_POST["sm_pages_mark"])) {
+ for($i=0; $iSetUrl($pages_ur[$i]);
+ $p->SetProprity($pages_pr[$i]);
+ $p->SetChangeFreq($pages_cf[$i]);
+ //Try to parse last modified, if -1 (note ===) automatic will be used (0)
+ $lm=(!empty($pages_lm[$i])?strtotime($pages_lm[$i],time()):-1);
+ if($lm===-1) $p->setLastMod(0);
+ else $p->setLastMod($lm);
+ //Add it to the array
+ array_push($pages,$p);
+ }
+ }
+
+ return $pages;
+ }
+
+ /**
+ * Converts a mysql datetime value into a unix timestamp
+ *
+ * @param The value in the mysql datetime format
+ * @return int The time in seconds
+ */
+ function GetTimestampFromMySql($mysqlDateTime) {
+ list($date, $hours) = split(' ', $mysqlDateTime);
+ list($year,$month,$day) = split('-',$date);
+ list($hour,$min,$sec) = split(':',$hours);
+ return mktime(intval($hour), intval($min), intval($sec), intval($month), intval($day), intval($year));
+ }
+
+ /**
+ * Returns a link pointing to a spcific page of the authors website
+ *
+ * @since 3.0
+ * @param The page to link to
+ * @return string The full url
+ */
+ function GetRedirectLink($redir) {
+ return trailingslashit("http://www.arnebrachhold.de/redir/" . $redir);
+ }
+
+ /**
+ * Returns a link pointing back to the plugin page in WordPress
+ *
+ * @since 3.0
+ * @return string The full url
+ */
+ function GetBackLink() {
+ //admin_url was added in WP 2.6.0
+ if(function_exists("admin_url")) return admin_url("options-general.php?page=" . GoogleSitemapGeneratorLoader::GetBaseName());
+ else return $_SERVER['PHP_SELF'] . "?page=" . GoogleSitemapGeneratorLoader::GetBaseName();
+ }
+
+ /**
+ * Shows the option page of the plugin. Before 3.1.1, this function was basically the UI, afterwards the UI was outsourced to another class
+ *
+ * @see GoogleSitemapGeneratorUI
+ * @since 3.0
+ * @return bool
+ */
+ function HtmlShowOptionsPage() {
+
+ $ui = $this->GetUI();
+ if($ui) {
+ $ui->HtmlShowOptionsPage();
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Includes the user interface class and intializes it
+ *
+ * @since 3.1.1
+ * @see GoogleSitemapGeneratorUI
+ * @return GoogleSitemapGeneratorUI
+ */
+ function GetUI() {
+
+ global $wp_version;
+
+ if($this->_ui === null) {
+
+ $className='GoogleSitemapGeneratorUI';
+ $fileName='sitemap-ui.php';
+
+ if(!class_exists($className)) {
+
+ $path = trailingslashit(dirname(__FILE__));
+
+ if(!file_exists( $path . $fileName)) return false;
+ require_once($path. $fileName);
+ }
+
+ $this->_ui = new $className($this);
+
+ }
+
+ return $this->_ui;
+ }
+
+ function HtmlShowHelp() {
+
+
+ }
+}
\ No newline at end of file