Php 修改代码以更改站点地图中的时间戳时区
下面是我用于站点地图的插件的代码Php 修改代码以更改站点地图中的时间戳时区,php,timezone,timestamp,sitemap,last-modified,Php,Timezone,Timestamp,Sitemap,Last Modified,下面是我用于站点地图的插件的代码 我想知道是否有一种方法可以“强制”所有日期变量和其中的函数使用不同的时区 如果没有,我如何修改代码来更改时间戳以反映不同的时区?例如,到美国/纽约时区 请查找date以快速找到相应的代码块 sitemap=''。“\n”; $base=$GLOBALS['wp\u rewrite']->使用_index\u permalinks()?'index.php/“:”; //参考特定于文章类型的站点地图 foreach(获取发布类型(数组('public'=>true
美国/纽约
时区date
以快速找到相应的代码块
sitemap=''。“\n”;
$base=$GLOBALS['wp\u rewrite']->使用_index\u permalinks()?'index.php/“:”;
//参考特定于文章类型的站点地图
foreach(获取发布类型(数组('public'=>true))作为$post\u类型){
如果($post_type=='attachment')
继续;
if(设置($options['post\u types-'.$post\u type.'-not\u in\u sitemap'])和&$options['post\u types-'.$post\u type.'-not\u in\u sitemap']))
继续;
$count=$wpdb->get_var($wpdb->prepare(“从$wpdb->post中选择count(ID),其中post_type=%s,post_status='publish'LIMIT 1',$post_type));
//不要包含没有帖子的帖子类型
如果(!$count)
继续;
$n=($count>1000)?(int)ceil($count/1000):1;
对于($i=0;$i<$n;$i++){
$count=($n>1)?$i+1:“”;
if(空($count)| |$count==n){
$date=$this->get\u last\u modified($post\u type);
}否则{
$date=$wpdb->get_var($wpdb->prepare(“从$wpdb->posts中选择post_modified_gmt,其中post_状态='publish',post_类型=%s按post_modified_gmt ASC LIMIT 1偏移量%d排序,$post_类型,$i*1000+999));
$date=日期('c',strottime($date));
}
$this->sitemap.=''。\n;
$this->sitemap.=''.home\u url($base.$post\u type.'-sitemap'.$count..xml')。“\n”;
$this->sitemap.=''.htmlspecialchars($date)。''.\n;
$this->sitemap.=''。\n;
}
}
//参考特定于分类法的站点地图
foreach(获取_分类法(数组('public'=>true))作为$tax){
如果(在数组中($tax,数组('link\u category','nav\u menu','post\u format'))
继续;
如果(isset($options['taxonomies-'.$tax.-not_-in_-sitemap'])和&$options['taxonomies-'.$tax.-not_-in_-sitemap']))
继续;
//不要包含没有术语的分类法
如果(!$wpdb->get_var($wpdb->prepare(“从$wpdb->term_taxonomy中选择term_id,其中taxonomy=%s和count!=0 LIMIT 1,$tax)))
继续;
//检索注册到此分类法的post_类型,然后检索所有这些组合的上次修改日期。
$taxobj=获取分类法($tax);
$date=$this->get\u last\u modified($taxobj->object\u type);
$this->sitemap.=''。\n;
$this->sitemap.='''.home\u url($base.$tax.-sitemap.xml').''.\n;
$this->sitemap.=''.htmlspecialchars($date)。''.\n;
$this->sitemap.=''。\n;
}
//允许其他插件将其站点地图添加到索引中
$this->sitemap.=apply_过滤器('wpseo_sitemap_索引','');
$this->sitemap.='';
}
/**
*为特定帖子类型构建子站点地图——example.com/post_type-sitemap.xml
*
*@param string$post_type已注册post type的slug
*/
函数构建\后类型\映射($post\类型){
$options=get_wpseo_options();
............
//我们还获取了post_日期、post_名称、post_作者和post_状态,这样我们就可以将这些对象放入get_permalink中,从而为每个permalink保存一个get_post调用。
而($total>$offset){
$join\u filter=apply\u filters('wpseo\u posts\u join','',$post\u type);
$where_filter=apply_filter('wpseo_posts_where','',$post_type);
//每个线程的优化查询:http://wordpress.org/support/topic/plugin-wordpress-seo-by-yoast-performance-suggestion
//也看到http://explainextended.com/2009/10/23/mysql-order-by-limit-performance-late-row-lookups/
$posts=$wpdb->get_results(“选择l.ID、post_内容、post_名称、post_作者、post_父项、post_修改的\u gmt、post_日期、post_日期\u gmt
由(
从$wpdb->posts{$join\u filter}中选择ID
其中post_状态=‘发布’
和post_密码=“”
和post_type='$post_type'
{$where_filter}
邮购经修改的ASC
限制$steps OFFSET$OFFSET)o
加入$wpdb->posts l
在l.ID=o.ID上
凭信用证订购);
/*$posts=$wpdb->get_results(“选择ID、post_内容、post_名称、post_作者、post_家长、post_修改后的时间、post_日期、post_日期
从$wpdb->posts{$join\u filter}
其中post_状态=‘发布’
和post_密码=“”
和post_type='$post_type'
{$where_filter}
邮购经修改的ASC
限额$steps OFFSET$OFFSET”)*/
$offset=$offset+$steps;
foreach(员额为$p){
$p->post\U type=$post\U type;
$p->post_status='publish';
$p->filter='sample';
如果(wpseo_get_value('meta robots noindex',$p->ID)&&wpseo_get_value('sitemap include',$p->ID)!='always')
继续;
if(wpseo_get_值('sitemap include',$p->ID)='never')
继续;
<?php
/**
* @package XML_Sitemaps
*/
class WPSEO_Sitemaps {
.....
/**
* Build the root sitemap -- example.com/sitemap_index.xml -- which lists sub-sitemaps
* for other content types.
*
* @todo lastmod for sitemaps?
*/
function build_root_map() {
global $wpdb;
$options = get_wpseo_options();
$this->sitemap = '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
$base = $GLOBALS['wp_rewrite']->using_index_permalinks() ? 'index.php/' : '';
// reference post type specific sitemaps
foreach ( get_post_types( array( 'public' => true ) ) as $post_type ) {
if ( $post_type == 'attachment' )
continue;
if ( isset( $options['post_types-' . $post_type . '-not_in_sitemap'] ) && $options['post_types-' . $post_type . '-not_in_sitemap'] )
continue;
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM $wpdb->posts WHERE post_type = %s AND post_status = 'publish' LIMIT 1", $post_type ) );
// don't include post types with no posts
if ( !$count )
continue;
$n = ( $count > 1000 ) ? (int) ceil( $count / 1000 ) : 1;
for ( $i = 0; $i < $n; $i++ ) {
$count = ( $n > 1 ) ? $i + 1 : '';
if ( empty( $count ) || $count == $n ) {
$date = $this->get_last_modified( $post_type );
} else {
$date = $wpdb->get_var( $wpdb->prepare( "SELECT post_modified_gmt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = %s ORDER BY post_modified_gmt ASC LIMIT 1 OFFSET %d", $post_type, $i * 1000 + 999 ) );
$date = date( 'c', strtotime( $date ) );
}
$this->sitemap .= '<sitemap>' . "\n";
$this->sitemap .= '<loc>' . home_url( $base . $post_type . '-sitemap' . $count . '.xml' ) . '</loc>' . "\n";
$this->sitemap .= '<lastmod>' . htmlspecialchars( $date ) . '</lastmod>' . "\n";
$this->sitemap .= '</sitemap>' . "\n";
}
}
// reference taxonomy specific sitemaps
foreach ( get_taxonomies( array( 'public' => true ) ) as $tax ) {
if ( in_array( $tax, array( 'link_category', 'nav_menu', 'post_format' ) ) )
continue;
if ( isset( $options['taxonomies-' . $tax . '-not_in_sitemap'] ) && $options['taxonomies-' . $tax . '-not_in_sitemap'] )
continue;
// don't include taxonomies with no terms
if ( !$wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->term_taxonomy WHERE taxonomy = %s AND count != 0 LIMIT 1", $tax ) ) )
continue;
// Retrieve the post_types that are registered to this taxonomy and then retrieve last modified date for all of those combined.
$taxobj = get_taxonomy( $tax );
$date = $this->get_last_modified( $taxobj->object_type );
$this->sitemap .= '<sitemap>' . "\n";
$this->sitemap .= '<loc>' . home_url( $base . $tax . '-sitemap.xml' ) . '</loc>' . "\n";
$this->sitemap .= '<lastmod>' . htmlspecialchars( $date ) . '</lastmod>' . "\n";
$this->sitemap .= '</sitemap>' . "\n";
}
// allow other plugins to add their sitemaps to the index
$this->sitemap .= apply_filters( 'wpseo_sitemap_index', '' );
$this->sitemap .= '</sitemapindex>';
}
/**
* Build a sub-sitemap for a specific post type -- example.com/post_type-sitemap.xml
*
* @param string $post_type Registered post type's slug
*/
function build_post_type_map( $post_type ) {
$options = get_wpseo_options();
............
// We grab post_date, post_name, post_author and post_status too so we can throw these objects into get_permalink, which saves a get_post call for each permalink.
while ( $total > $offset ) {
$join_filter = apply_filters( 'wpseo_posts_join', '', $post_type );
$where_filter = apply_filters( 'wpseo_posts_where', '', $post_type );
// Optimized query per this thread: http://wordpress.org/support/topic/plugin-wordpress-seo-by-yoast-performance-suggestion
// Also see http://explainextended.com/2009/10/23/mysql-order-by-limit-performance-late-row-lookups/
$posts = $wpdb->get_results( "SELECT l.ID, post_content, post_name, post_author, post_parent, post_modified_gmt, post_date, post_date_gmt
FROM (
SELECT ID FROM $wpdb->posts {$join_filter}
WHERE post_status = 'publish'
AND post_password = ''
AND post_type = '$post_type'
{$where_filter}
ORDER BY post_modified ASC
LIMIT $steps OFFSET $offset ) o
JOIN $wpdb->posts l
ON l.ID = o.ID
ORDER BY l.ID" );
/* $posts = $wpdb->get_results("SELECT ID, post_content, post_name, post_author, post_parent, post_modified_gmt, post_date, post_date_gmt
FROM $wpdb->posts {$join_filter}
WHERE post_status = 'publish'
AND post_password = ''
AND post_type = '$post_type'
{$where_filter}
ORDER BY post_modified ASC
LIMIT $steps OFFSET $offset"); */
$offset = $offset + $steps;
foreach ( $posts as $p ) {
$p->post_type = $post_type;
$p->post_status = 'publish';
$p->filter = 'sample';
if ( wpseo_get_value( 'meta-robots-noindex', $p->ID ) && wpseo_get_value( 'sitemap-include', $p->ID ) != 'always' )
continue;
if ( wpseo_get_value( 'sitemap-include', $p->ID ) == 'never' )
continue;
if ( wpseo_get_value( 'redirect', $p->ID ) && strlen( wpseo_get_value( 'redirect', $p->ID ) ) > 0 )
continue;
$url = array();
$url['mod'] = ( isset( $p->post_modified_gmt ) && $p->post_modified_gmt != '0000-00-00 00:00:00' ) ? $p->post_modified_gmt : $p->post_date_gmt;
$url['chf'] = 'weekly';
$url['loc'] = get_permalink( $p );
.............
}
/**
* Build a sub-sitemap for a specific taxonomy -- example.com/tax-sitemap.xml
*
* @param string $taxonomy Registered taxonomy's slug
*/
function build_tax_map( $taxonomy ) {
$options = get_wpseo_options();
..........
// Grab last modified date
$sql = "SELECT MAX(p.post_date) AS lastmod
FROM $wpdb->posts AS p
INNER JOIN $wpdb->term_relationships AS term_rel
ON term_rel.object_id = p.ID
INNER JOIN $wpdb->term_taxonomy AS term_tax
ON term_tax.term_taxonomy_id = term_rel.term_taxonomy_id
AND term_tax.taxonomy = '$c->taxonomy'
AND term_tax.term_id = $c->term_id
WHERE p.post_status = 'publish'
AND p.post_password = ''";
$url['mod'] = $wpdb->get_var( $sql );
$url['chf'] = 'weekly';
$output .= $this->sitemap_url( $url );
}
}
/**
* Build the <url> tag for a given URL.
*
* @param array $url Array of parts that make up this entry
* @return string
*/
function sitemap_url( $url ) {
if ( isset( $url['mod'] ) )
$date = mysql2date( "Y-m-d\TH:i:s+00:00", $url['mod'] );
else
$date = date( 'c' );
$output = "\t<url>\n";
$output .= "\t\t<loc>" . $url['loc'] . "</loc>\n";
$output .= "\t\t<lastmod>" . $date . "</lastmod>\n";
$output .= "\t\t<changefreq>" . $url['chf'] . "</changefreq>\n";
$output .= "\t\t<priority>" . str_replace( ',', '.', $url['pri'] ) . "</priority>\n";
if ( isset( $url['images'] ) && count( $url['images'] ) > 0 ) {
foreach ( $url['images'] as $img ) {
$output .= "\t\t<image:image>\n";
$output .= "\t\t\t<image:loc>" . esc_html( $img['src'] ) . "</image:loc>\n";
if ( isset( $img['title'] ) )
$output .= "\t\t\t<image:title>" . _wp_specialchars( html_entity_decode( $img['title'], ENT_QUOTES, get_bloginfo('charset') ) ) . "</image:title>\n";
if ( isset( $img['alt'] ) )
$output .= "\t\t\t<image:caption>" . _wp_specialchars( html_entity_decode( $img['alt'], ENT_QUOTES, get_bloginfo('charset') ) ) . "</image:caption>\n";
$output .= "\t\t</image:image>\n";
}
}
$output .= "\t</url>\n";
return $output;
}
/**
* Get the modification date for the last modified post in the post type:
*
* @param array $post_types Post types to get the last modification date for
* @return string
*/
function get_last_modified( $post_types ) {
global $wpdb;
if ( !is_array( $post_types ) )
$post_types = array( $post_types );
$result = 0;
foreach ( $post_types as $post_type ) {
$key = 'lastpostmodified:gmt:' . $post_type;
$date = wp_cache_get( $key, 'timeinfo' );
if ( !$date ) {
$date = $wpdb->get_var( $wpdb->prepare( "SELECT post_modified_gmt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = %s ORDER BY post_modified_gmt DESC LIMIT 1", $post_type ) );
if ( $date )
wp_cache_set( $key, $date, 'timeinfo' );
}
if ( strtotime( $date ) > $result )
$result = strtotime( $date );
}
// Transform to W3C Date format.
$result = date( 'c', $result );
return $result;
}
}
global $wpseo_sitemaps;
$wpseo_sitemaps = new WPSEO_Sitemaps();
date_default_timezone_set( 'America/New_York');
class WPSEO_Sitemaps {
private $old_timezone = '';
private $new_timezone = 'America/New_York';
function setTimezone() {
$this->old_timezone = date_default_timezone_get();
date_default_timezone_set( $this->new_timezone);
}
function revertTimezone() {
date_default_timezone_set( $this->old_timezone);
}
function foo() {
$this->setTimezone();
date();
$tihs->revertTimezone();
}
}