WPSEO_Sitemap_Timezone

Class WPSEO_Sitemap_Timezone.

Defined (1)

The class is defined in the following location(s).

/inc/sitemaps/class-sitemap-timezone.php  
  1. class WPSEO_Sitemap_Timezone { 
  2.  
  3. /** 
  4. * Holds the timezone string value to reuse for performance 
  5. * @var string $timezone_string 
  6. */ 
  7. private $timezone_string = ''; 
  8.  
  9. /** 
  10. * Format arbitrary UTC datetime string to desired form in site's time zone. 
  11. * @param string $datetime_string The input datetime string in UTC time zone. 
  12. * @param string $format Date format to use. 
  13. * @return string 
  14. */ 
  15. public function format_date( $datetime_string, $format = 'c' ) { 
  16.  
  17. $date_time = $this->get_datetime_with_timezone( $datetime_string ); 
  18.  
  19. if ( is_null( $date_time ) ) { 
  20. return ''; 
  21.  
  22. return $date_time->format( $format ); 
  23. /** 
  24. * Get the datetime object, in site's time zone, if the datetime string was valid 
  25. * @param string $datetime_string The datetime string in UTC time zone, that needs to be converted to a DateTime object. 
  26. * @return DateTime|null in site's time zone 
  27. */ 
  28. public function get_datetime_with_timezone( $datetime_string ) { 
  29.  
  30. static $utc_timezone, $local_timezone; 
  31.  
  32. if ( ! isset( $utc_timezone ) ) { 
  33. $utc_timezone = new DateTimeZone( 'UTC' ); 
  34. $local_timezone = new DateTimeZone( $this->get_timezone_string() ); 
  35.  
  36. if ( ! empty( $datetime_string ) && WPSEO_Utils::is_valid_datetime( $datetime_string ) ) { 
  37. $datetime = new DateTime( $datetime_string, $utc_timezone ); 
  38. $datetime->setTimezone( $local_timezone ); 
  39.  
  40. return $datetime; 
  41.  
  42. return null; 
  43.  
  44. /** 
  45. * Returns the timezone string for a site, even if it's set to a UTC offset 
  46. * Adapted from http://www.php.net/manual/en/function.timezone-name-from-abbr.php#89155 
  47. * @return string valid PHP timezone string 
  48. */ 
  49. private function determine_timezone_string() { 
  50.  
  51. // If site timezone string exists, return it. 
  52. if ( $timezone = get_option( 'timezone_string' ) ) { 
  53. return $timezone; 
  54.  
  55. // Get UTC offset, if it isn't set then return UTC. 
  56. if ( 0 === ( $utc_offset = (int) get_option( 'gmt_offset', 0 ) ) ) { 
  57. return 'UTC'; 
  58.  
  59. // Adjust UTC offset from hours to seconds. 
  60. $utc_offset *= HOUR_IN_SECONDS; 
  61.  
  62. // Attempt to guess the timezone string from the UTC offset. 
  63. $timezone = timezone_name_from_abbr( '', $utc_offset ); 
  64.  
  65. if ( false !== $timezone ) { 
  66. return $timezone; 
  67.  
  68. // Last try, guess timezone string manually. 
  69. foreach ( timezone_abbreviations_list() as $abbr ) { 
  70. foreach ( $abbr as $city ) { 
  71. if ( $city['offset'] == $utc_offset ) { 
  72. return $city['timezone_id']; 
  73.  
  74. // Fallback to UTC. 
  75. return 'UTC'; 
  76.  
  77. /** 
  78. * Returns the correct timezone string 
  79. * @return string 
  80. */ 
  81. private function get_timezone_string() { 
  82. if ( '' == $this->timezone_string ) { 
  83. $this->timezone_string = $this->determine_timezone_string(); 
  84.  
  85. return $this->timezone_string;