Permalink
Browse files

Introduce constants to allow for easier expression of time periods in…

… seconds. Adds MINUTE_IN_SECONDS, HOUR_IN_SECONDS, DAY_IN_SECONDS, WEEK_IN_SECONDS, YEAR_IN_SECONDS. props nbachiyski, SergeyBiryukov. fixes #20987.

git-svn-id: http://core.svn.wordpress.org/trunk@21996 1a063a9b-81f0-0310-95a4-ce76da25c4cd
  • Loading branch information...
1 parent 0ada88e commit 557d9313a7a3f350718cc6c1739af641e5ba3da2 @nacin nacin committed Sep 25, 2012
View
@@ -30,7 +30,7 @@ function wp_credits() {
if ( ! is_array( $results ) )
return false;
- set_site_transient( 'wordpress_credits_' . $locale, $results, 86400 ); // One day
+ set_site_transient( 'wordpress_credits_' . $locale, $results, DAY_IN_SECONDS );
}
return $results;
@@ -335,7 +335,7 @@ function column_comment( $comment ) {
$the_comment_status = wp_get_comment_status( $comment->comment_ID );
$ptime = date( 'G', strtotime( $comment->comment_date ) );
- if ( ( abs( time() - $ptime ) ) < 86400 )
+ if ( ( abs( time() - $ptime ) ) < DAY_IN_SECONDS )
$ptime = sprintf( __( '%s ago' ), human_time_diff( $ptime ) );
else
$ptime = mysql2date( __( 'Y/m/d \a\t g:i A' ), $comment->comment_date );
@@ -282,7 +282,7 @@ function display_rows() {
} else {
$m_time = $post->post_date;
$time = get_post_time( 'G', true, $post, false );
- if ( ( abs( $t_diff = time() - $time ) ) < 86400 ) {
+ if ( ( abs( $t_diff = time() - $time ) ) < DAY_IN_SECONDS ) {
if ( $t_diff < 0 )
$h_time = sprintf( __( '%s from now' ), human_time_diff( $time ) );
else
@@ -70,14 +70,13 @@ function prepare_items() {
}
}
- set_transient( 'plugin_slugs', array_keys( $plugins['all'] ), 86400 );
+ set_transient( 'plugin_slugs', array_keys( $plugins['all'] ), DAY_IN_SECONDS );
if ( ! $screen->is_network ) {
$recently_activated = get_option( 'recently_activated', array() );
- $one_week = 7*24*60*60;
foreach ( $recently_activated as $key => $time )
- if ( $time + $one_week < time() )
+ if ( $time + WEEK_IN_SECONDS < time() )
unset( $recently_activated[$key] );
update_option( 'recently_activated', $recently_activated );
}
@@ -580,7 +580,7 @@ function single_row( $post, $level = 0 ) {
$time_diff = time() - $time;
- if ( $time_diff > 0 && $time_diff < 24*60*60 )
+ if ( $time_diff > 0 && $time_diff < DAY_IN_SECONDS )
$h_time = sprintf( __( '%s ago' ), human_time_diff( $time ) );
else
$h_time = mysql2date( __( 'Y/m/d' ), $m_time );
@@ -916,7 +916,7 @@ function wp_dashboard_plugins_output() {
if ( false === $plugin_slugs = get_transient( 'plugin_slugs' ) ) {
$plugin_slugs = array_keys( get_plugins() );
- set_transient( 'plugin_slugs', $plugin_slugs, 86400 );
+ set_transient( 'plugin_slugs', $plugin_slugs, DAY_IN_SECONDS );
}
foreach ( array( 'popular' => __('Most Popular'), 'new' => __('Newest Plugins') ) as $feed => $label ) {
@@ -1024,7 +1024,7 @@ function wp_dashboard_cached_rss_widget( $widget_id, $callback, $check_urls = ar
array_unshift( $args, $widget_id );
ob_start();
call_user_func_array( $callback, $args );
- set_transient( $cache_key, ob_get_flush(), 43200); // Default lifetime in cache of 12 hours (same as the feeds)
+ set_transient( $cache_key, ob_get_flush(), 12 * HOUR_IN_SECONDS ); // Default lifetime in cache of 12 hours (same as the feeds)
}
return true;
@@ -1215,7 +1215,7 @@ function wp_check_browser_version() {
if ( ! is_array( $response ) )
return false;
- set_site_transient( 'browser_' . $key, $response, 604800 ); // cache for 1 week
+ set_site_transient( 'browser_' . $key, $response, WEEK_IN_SECONDS );
}
return $response;
@@ -89,7 +89,7 @@ function wp_import_handle_upload() {
$id = wp_insert_attachment( $object, $file );
// schedule a cleanup for one day from now in case of failed import or missing wp_import_cleanup() call
- wp_schedule_single_event( time() + 86400, 'importer_scheduled_cleanup', array( $id ) );
+ wp_schedule_single_event( time() + DAY_IN_SECONDS, 'importer_scheduled_cleanup', array( $id ) );
return array( 'file' => $file, 'id' => $id );
}
@@ -1150,7 +1150,7 @@ function wp_nav_menu_manage_columns() {
*/
function _wp_delete_orphaned_draft_menu_items() {
global $wpdb;
- $delete_timestamp = time() - (60*60*24*EMPTY_TRASH_DAYS);
+ $delete_timestamp = time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS );
// delete orphaned draft menu items
$menu_items_to_delete = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts AS p LEFT JOIN $wpdb->postmeta AS m ON p.ID = m.post_id WHERE post_type = 'nav_menu_item' AND post_status = 'draft' AND meta_key = '_menu_item_orphaned' AND meta_value < '%d'", $delete_timestamp ) );
@@ -74,7 +74,7 @@ function install_popular_tags( $args = array() ) {
if ( is_wp_error($tags) )
return $tags;
- set_site_transient('poptags_' . $key, $tags, 10800); // 3 * 60 * 60 = 10800
+ set_site_transient( 'poptags_' . $key, $tags, 3 * HOUR_IN_SECONDS );
return $tags;
}
@@ -511,11 +511,11 @@ function upgrade_110() {
$time_difference = $all_options->time_difference;
$server_time = time()+date('Z');
- $weblogger_time = $server_time + $time_difference*3600;
+ $weblogger_time = $server_time + $time_difference * HOUR_IN_SECONDS;
$gmt_time = time();
- $diff_gmt_server = ($gmt_time - $server_time) / 3600;
- $diff_weblogger_server = ($weblogger_time - $server_time) / 3600;
+ $diff_gmt_server = ($gmt_time - $server_time) / HOUR_IN_SECONDS;
+ $diff_weblogger_server = ($weblogger_time - $server_time) / HOUR_IN_SECONDS;
$diff_gmt_weblogger = $diff_gmt_server - $diff_weblogger_server;
$gmt_offset = -$diff_gmt_weblogger;
@@ -125,7 +125,7 @@ function get_file($path) {
$compress = ( isset($_GET['c']) && $_GET['c'] );
$force_gzip = ( $compress && 'gzip' == $_GET['c'] );
-$expires_offset = 31536000;
+$expires_offset = YEAR_IN_SECONDS;
$out = '';
$wp_scripts = new WP_Scripts();
View
@@ -104,7 +104,7 @@ function get_file($path) {
$compress = ( isset($_GET['c']) && $_GET['c'] );
$force_gzip = ( $compress && 'gzip' == $_GET['c'] );
$rtl = ( isset($_GET['dir']) && 'rtl' == $_GET['dir'] );
-$expires_offset = 31536000;
+$expires_offset = YEAR_IN_SECONDS;
$out = '';
$wp_styles = new WP_Styles();
@@ -78,7 +78,7 @@ function _walk_bookmarks($bookmarks, $args = '' ) {
if ( $show_updated )
if ( '00' != substr($bookmark->link_updated_f, 0, 2) ) {
$title .= ' (';
- $title .= sprintf(__('Last updated: %s'), date(get_option('links_updated_date_format'), $bookmark->link_updated_f + (get_option('gmt_offset') * 3600)));
+ $title .= sprintf(__('Last updated: %s'), date(get_option('links_updated_date_format'), $bookmark->link_updated_f + (get_option('gmt_offset') * HOUR_IN_SECONDS)));
$title .= ')';
}
@@ -808,7 +808,7 @@ public static function test( $args = array() ) {
if ( ! function_exists( 'fsockopen' ) )
return false;
- if ( false !== ($option = get_option( 'disable_fsockopen' )) && time()-$option < 43200 ) // 12 hours
+ if ( false !== ( $option = get_option( 'disable_fsockopen' ) ) && time() - $option < 12 * HOUR_IN_SECONDS )
return false;
$is_ssl = isset( $args['ssl'] ) && $args['ssl'];
View
@@ -699,7 +699,7 @@ function check_comment_flood_db( $ip, $email, $date ) {
global $wpdb;
if ( current_user_can( 'manage_options' ) )
return; // don't throttle admins
- $hour_ago = gmdate( 'Y-m-d H:i:s', time() - 3600 );
+ $hour_ago = gmdate( 'Y-m-d H:i:s', time() - HOUR_IN_SECONDS );
if ( $lasttime = $wpdb->get_var( $wpdb->prepare( "SELECT `comment_date_gmt` FROM `$wpdb->comments` WHERE `comment_date_gmt` >= %s AND ( `comment_author_IP` = %s OR `comment_author_email` = %s ) ORDER BY `comment_date_gmt` DESC LIMIT 1", $hour_ago, $ip, $email ) ) ) {
$time_lastcomment = mysql2date('U', $lasttime, false);
$time_newcomment = mysql2date('U', $date, false);
@@ -1981,7 +1981,7 @@ function _close_comments_for_old_posts( $posts, $query ) {
if ( ! $days_old )
return $posts;
- if ( time() - strtotime( $posts[0]->post_date_gmt ) > ( $days_old * 24 * 60 * 60 ) ) {
+ if ( time() - strtotime( $posts[0]->post_date_gmt ) > ( $days_old * DAY_IN_SECONDS ) ) {
$posts[0]->comment_status = 'closed';
$posts[0]->ping_status = 'closed';
}
@@ -2016,7 +2016,7 @@ function _close_comments_for_old_post( $open, $post_id ) {
if ( ! in_array( $post->post_type, $post_types ) )
return $open;
- if ( time() - strtotime( $post->post_date_gmt ) > ( $days_old * 24 * 60 * 60 ) )
+ if ( time() - strtotime( $post->post_date_gmt ) > ( $days_old * DAY_IN_SECONDS ) )
return false;
return $open;
View
@@ -22,7 +22,7 @@
function wp_schedule_single_event( $timestamp, $hook, $args = array()) {
// don't schedule a duplicate if there's already an identical event due in the next 10 minutes
$next = wp_next_scheduled($hook, $args);
- if ( $next && $next <= $timestamp + 600 )
+ if ( $next && $next <= $timestamp + 10 * MINUTE_IN_SECONDS )
return;
$crons = _get_cron_array();
@@ -206,7 +206,7 @@ function spawn_cron( $gmt_time = 0 ) {
*/
$lock = get_transient('doing_cron');
- if ( $lock > $gmt_time + 10*60 )
+ if ( $lock > $gmt_time + 10 * MINUTE_IN_SECONDS )
$lock = 0;
// don't run if another process is currently running it or more than once every 60 sec.
@@ -318,9 +318,9 @@ function wp_cron() {
*/
function wp_get_schedules() {
$schedules = array(
- 'hourly' => array( 'interval' => 3600, 'display' => __('Once Hourly') ),
- 'twicedaily' => array( 'interval' => 43200, 'display' => __('Twice Daily') ),
- 'daily' => array( 'interval' => 86400, 'display' => __('Once Daily') ),
+ 'hourly' => array( 'interval' => HOUR_IN_SECONDS, 'display' => __( 'Once Hourly' ) ),
+ 'twicedaily' => array( 'interval' => 12 * HOUR_IN_SECONDS, 'display' => __( 'Twice Daily' ) ),
+ 'daily' => array( 'interval' => DAY_IN_SECONDS, 'display' => __( 'Once Daily' ) ),
);
return array_merge( apply_filters( 'cron_schedules', array() ), $schedules );
}
@@ -72,6 +72,14 @@ function wp_initial_constants( ) {
if ( !defined('SHORTINIT') )
define('SHORTINIT', false);
+
+ // Constants for expressing human-interval intervals
+ // in their respective number of seconds.
+ define( 'MINUTE_IN_SECONDS', 60 );
+ define( 'HOUR_IN_SECONDS', 60 * MINUTE_IN_SECONDS );
+ define( 'DAY_IN_SECONDS', 24 * HOUR_IN_SECONDS );
+ define( 'WEEK_IN_SECONDS', 7 * DAY_IN_SECONDS );
+ define( 'YEAR_IN_SECONDS', 365 * DAY_IN_SECONDS );
}
/**
@@ -984,7 +984,7 @@ function get_links($category = -1, $before = '', $after = '<br />', $between = '
if ( $show_updated )
if (substr($row->link_updated_f, 0, 2) != '00')
- $title .= ' ('.__('Last updated') . ' ' . date(get_option('links_updated_date_format'), $row->link_updated_f + (get_option('gmt_offset') * 3600)) . ')';
+ $title .= ' ('.__('Last updated') . ' ' . date(get_option('links_updated_date_format'), $row->link_updated_f + (get_option('gmt_offset') * HOUR_IN_SECONDS)) . ')';
if ( '' != $title )
$title = ' title="' . $title . '"';
View
@@ -538,7 +538,7 @@ function fetch_feed($url) {
}
$feed->set_feed_url($url);
- $feed->set_cache_duration(apply_filters('wp_feed_cache_transient_lifetime', 43200, $url));
+ $feed->set_cache_duration( apply_filters( 'wp_feed_cache_transient_lifetime', 12 * HOUR_IN_SECONDS, $url ) );
do_action_ref_array( 'wp_feed_options', array( &$feed, $url ) );
$feed->init();
$feed->handle_content_type();
View
@@ -1898,13 +1898,13 @@ function get_gmt_from_date($string, $format = 'Y-m-d H:i:s') {
$datetime = new DateTime( $string );
$datetime->setTimezone( new DateTimeZone('UTC') );
$offset = $datetime->getOffset();
- $datetime->modify( '+' . $offset / 3600 . ' hours');
+ $datetime->modify( '+' . $offset / HOUR_IN_SECONDS . ' hours');
$string_gmt = gmdate($format, $datetime->format('U'));
date_default_timezone_set('UTC');
} else {
$string_time = gmmktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
- $string_gmt = gmdate($format, $string_time - get_option('gmt_offset') * 3600);
+ $string_gmt = gmdate($format, $string_time - get_option('gmt_offset') * HOUR_IN_SECONDS);
}
return $string_gmt;
}
@@ -1924,7 +1924,7 @@ function get_gmt_from_date($string, $format = 'Y-m-d H:i:s') {
function get_date_from_gmt($string, $format = 'Y-m-d H:i:s') {
preg_match('#([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})#', $string, $matches);
$string_time = gmmktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
- $string_localtime = gmdate($format, $string_time + get_option('gmt_offset')*3600);
+ $string_localtime = gmdate($format, $string_time + get_option('gmt_offset') * HOUR_IN_SECONDS);
return $string_localtime;
}
@@ -1944,7 +1944,7 @@ function iso8601_timezone_to_offset($timezone) {
$sign = (substr($timezone, 0, 1) == '+') ? 1 : -1;
$hours = intval(substr($timezone, 1, 2));
$minutes = intval(substr($timezone, 3, 4)) / 60;
- $offset = $sign * 3600 * ($hours + $minutes);
+ $offset = $sign * HOUR_IN_SECONDS * ($hours + $minutes);
}
return $offset;
}
@@ -1968,7 +1968,7 @@ function iso8601_to_datetime($date_string, $timezone = 'user') {
if (!empty($date_bits[7])) { // we have a timezone, so let's compute an offset
$offset = iso8601_timezone_to_offset($date_bits[7]);
} else { // we don't have a timezone, so we assume user local timezone (not server's!)
- $offset = 3600 * get_option('gmt_offset');
+ $offset = HOUR_IN_SECONDS * get_option('gmt_offset');
}
$timestamp = gmmktime($date_bits[4], $date_bits[5], $date_bits[6], $date_bits[2], $date_bits[3], $date_bits[1]);
@@ -2093,28 +2093,28 @@ function sanitize_email( $email ) {
* @return string Human readable time difference.
*/
function human_time_diff( $from, $to = '' ) {
- if ( empty($to) )
+ if ( empty( $to ) )
$to = time();
- $diff = (int) abs($to - $from);
- if ($diff <= 3600) {
- $mins = round($diff / 60);
- if ($mins <= 1) {
+ $diff = (int) abs( $to - $from );
+ if ( $diff <= HOUR_IN_SECONDS ) {
+ $mins = round( $diff / MINUTE_IN_SECONDS );
+ if ( $mins <= 1 ) {
$mins = 1;
}
/* translators: min=minute */
- $since = sprintf(_n('%s min', '%s mins', $mins), $mins);
- } else if (($diff <= 86400) && ($diff > 3600)) {
- $hours = round($diff / 3600);
- if ($hours <= 1) {
+ $since = sprintf( _n( '%s min', '%s mins', $mins ), $mins );
+ } elseif ( ( $diff <= DAY_IN_SECONDS ) && ( $diff > HOUR_IN_SECONDS ) ) {
+ $hours = round( $diff / HOUR_IN_SECONDS );
+ if ( $hours <= 1 ) {
$hours = 1;
}
- $since = sprintf(_n('%s hour', '%s hours', $hours), $hours);
- } elseif ($diff >= 86400) {
- $days = round($diff / 86400);
- if ($days <= 1) {
+ $since = sprintf( _n( '%s hour', '%s hours', $hours ), $hours );
+ } elseif ( $diff >= DAY_IN_SECONDS ) {
+ $days = round( $diff / DAY_IN_SECONDS );
+ if ( $days <= 1 ) {
$days = 1;
}
- $since = sprintf(_n('%s day', '%s days', $days), $days);
+ $since = sprintf( _n( '%s day', '%s days', $days ), $days );
}
return $since;
}
View
@@ -59,10 +59,10 @@ function mysql2date( $format, $date, $translate = true ) {
function current_time( $type, $gmt = 0 ) {
switch ( $type ) {
case 'mysql':
- return ( $gmt ) ? gmdate( 'Y-m-d H:i:s' ) : gmdate( 'Y-m-d H:i:s', ( time() + ( get_option( 'gmt_offset' ) * 3600 ) ) );
+ return ( $gmt ) ? gmdate( 'Y-m-d H:i:s' ) : gmdate( 'Y-m-d H:i:s', ( time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ) );
break;
case 'timestamp':
- return ( $gmt ) ? time() : time() + ( get_option( 'gmt_offset' ) * 3600 );
+ return ( $gmt ) ? time() : time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
break;
}
}
@@ -214,8 +214,8 @@ function get_weekstartend( $mysqlstring, $start_of_week = '' ) {
if ( $weekday < $start_of_week )
$weekday += 7;
- $start = $day - 86400 * ( $weekday - $start_of_week ); // The most recent week start day on or before $day
- $end = $start + 604799; // $start + 7 days - 1 second
+ $start = $day - DAY_IN_SECONDS * ( $weekday - $start_of_week ); // The most recent week start day on or before $day
+ $end = $start + 7 * DAY_IN_SECONDS - 1; // $start + 7 days - 1 second
return compact( 'start', 'end' );
}
@@ -934,7 +934,7 @@ function nocache_headers() {
* @since 2.1.0
*/
function cache_javascript_headers() {
- $expiresOffset = 864000; // 10 days
+ $expiresOffset = 10 * DAY_IN_SECONDS;
header( "Content-Type: text/javascript; charset=" . get_bloginfo( 'charset' ) );
header( "Vary: Accept-Encoding" ); // Handle proxies
header( "Expires: " . gmdate( "D, d M Y H:i:s", time() + $expiresOffset ) . " GMT" );
@@ -3117,7 +3117,7 @@ function wp_timezone_override_offset() {
if ( false === $timezone_object || false === $datetime_object ) {
return false;
}
- return round( timezone_offset_get( $timezone_object, $datetime_object ) / 3600, 2 );
+ return round( timezone_offset_get( $timezone_object, $datetime_object ) / HOUR_IN_SECONDS, 2 );
}
/**
@@ -3317,7 +3317,7 @@ function _cleanup_header_comment($str) {
function wp_scheduled_delete() {
global $wpdb;
- $delete_timestamp = time() - (60*60*24*EMPTY_TRASH_DAYS);
+ $delete_timestamp = time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS );
$posts_to_delete = $wpdb->get_results($wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_trash_meta_time' AND meta_value < '%d'", $delete_timestamp), ARRAY_A);
@@ -19,7 +19,7 @@ function get_file($path) {
return @file_get_contents($path);
}
-$expires_offset = 31536000;
+$expires_offset = YEAR_IN_SECONDS;
header('Content-Type: application/x-javascript; charset=UTF-8');
header('Vary: Accept-Encoding'); // Handle proxies
Oops, something went wrong.

0 comments on commit 557d931

Please sign in to comment.