Skip to content

Commit

Permalink
Introduce constants to allow for easier expression of time periods in…
Browse files Browse the repository at this point in the history
… 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
nacin committed Sep 25, 2012
1 parent 0ada88e commit 557d931
Show file tree
Hide file tree
Showing 29 changed files with 97 additions and 90 deletions.
2 changes: 1 addition & 1 deletion wp-admin/credits.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion wp-admin/includes/class-wp-comments-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
2 changes: 1 addition & 1 deletion wp-admin/includes/class-wp-media-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions wp-admin/includes/class-wp-plugins-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand Down
2 changes: 1 addition & 1 deletion wp-admin/includes/class-wp-posts-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
6 changes: 3 additions & 3 deletions wp-admin/includes/dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion wp-admin/includes/import.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
2 changes: 1 addition & 1 deletion wp-admin/includes/nav-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
Expand Down
2 changes: 1 addition & 1 deletion wp-admin/includes/plugin-install.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions wp-admin/includes/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion wp-admin/load-scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion wp-admin/load-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion wp-includes/bookmark-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 .= ')';
}

Expand Down
2 changes: 1 addition & 1 deletion wp-includes/class-http.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
6 changes: 3 additions & 3 deletions wp-includes/comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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';
}
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions wp-includes/cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 );
}
Expand Down
8 changes: 8 additions & 0 deletions wp-includes/default-constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion wp-includes/deprecated.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 . '"';
Expand Down
2 changes: 1 addition & 1 deletion wp-includes/feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
38 changes: 19 additions & 19 deletions wp-includes/formatting.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}

Expand All @@ -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;
}
Expand All @@ -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]);
Expand Down Expand Up @@ -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;
}
Expand Down
14 changes: 7 additions & 7 deletions wp-includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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' );
}

Expand Down Expand Up @@ -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" );
Expand Down Expand Up @@ -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 );
}

/**
Expand Down Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion wp-includes/js/tinymce/wp-tinymce.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 557d931

Please sign in to comment.