Modify code to change timestamp timezone in sitemap

Posted by Aahan Krish on Stack Overflow See other posts from Stack Overflow or by Aahan Krish
Published on 2012-07-02T14:53:36Z Indexed on 2012/07/02 15:15 UTC
Read the original article Hit count: 519

Below is the code from a plugin I use for sitemaps.

  1. I would like to know if there's a way to "enforce" a different timezone on all date variable and functions in it.

  2. If not, how do I modify the code to change the timestamp to reflect a different timezone? Say for example, to America/New_York timezone.

Please look for date to quickly get to the appropriate code blocks. Code on Pastebin.

<?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();

© Stack Overflow or respective owner

Related posts about php

Related posts about timezone