Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Further improvements to the display of times and timezones #154

Merged
merged 4 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ WP Crontrol is an essential tool for owners of WordPress websites and WooCommerc
* **Simplify Management**: Add, edit, delete, and pause cron events from a user-friendly interface, without needing to write any code.
* **Gain Insights**: Export cron event data for analysis or reporting.
* **Action Scheduler Compatibility**: Full support for the Action Scheduler system in WooCommerce, which is used to process recurring payments, subscriptions, and background orders.
* **Clarity of times and timezones**: All times are shown with a clear and accurate indication of which timezone applies. No more guesswork!

### For developers

Expand Down
72 changes: 65 additions & 7 deletions src/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
namespace Crontrol;

use Crontrol\Event\Table;
use DateTimeZone;
use stdClass;
use WP_Error;
use Exception;
use IntlTimeZone;

use function Crontrol\Event\check_integrity;

Expand Down Expand Up @@ -1403,6 +1405,65 @@ function get_timezone_name() {
/** @var string */
$timezone_string = get_option( 'timezone_string', '' );
$gmt_offset = get_option( 'gmt_offset', 0 );
$offset_string = get_utc_offset();

if ( 'UTC' === $timezone_string || ( empty( $gmt_offset ) && empty( $timezone_string ) ) ) {
return 'UTC';
}

if ( '' === $timezone_string ) {
return $offset_string;
}

$parts = explode( '/', $timezone_string );
array_shift( $parts );
$location = str_replace( '_', ' ', implode( ', ', array_reverse( $parts ) ) );
$time = time();

try {
$timezone = new DateTimeZone( $timezone_string );
$transitions = $timezone->getTransitions( $time, $time );

if ( empty( $transitions ) ) {
return sprintf(
'(%s) %s',
$offset_string,
$location,
);
}

$transition = reset( $transitions );
$name = $transition['abbr'];

if ( class_exists( 'IntlTimeZone' ) ) {
$intl_tz = IntlTimeZone::createTimeZone( $timezone_string );
$name = $intl_tz->getDisplayName( $transition['isdst'] );
}

return sprintf(
'(%1$s) %2$s - %3$s',
$offset_string,
$name,
$location,
);
} catch ( Exception $e ) {
return sprintf(
'(%s) %s',
$offset_string,
$location,
);
}
}

/**
* Returns the display name for the location of the site's timezone.
*
* @return string The name of the site's timezone location.
*/
function get_timezone_location(): string {
/** @var string */
$timezone_string = get_option( 'timezone_string', '' );
$gmt_offset = get_option( 'gmt_offset', 0 );

if ( 'UTC' === $timezone_string || ( empty( $gmt_offset ) && empty( $timezone_string ) ) ) {
return 'UTC';
Expand All @@ -1414,11 +1475,7 @@ function get_timezone_name() {

$parts = explode( '/', $timezone_string );

return sprintf(
'%s (%s)',
str_replace( '_', ' ', end( $parts ) ),
get_utc_offset()
);
return str_replace( '_', ' ', end( $parts ) );
}

/**
Expand Down Expand Up @@ -1528,8 +1585,9 @@ function show_cron_form( $editing ) {
);

$button = __( 'Add Event', 'wp-crontrol' );
$next_run_date_local = '';
$next_run_time_local = '';
$suggestion = strtotime( '+1 hour' );
$next_run_date_local = get_date_from_gmt( gmdate( 'Y-m-d H:i:s', $suggestion ), 'Y-m-d' );
$next_run_time_local = get_date_from_gmt( gmdate( 'Y-m-d H:\0\0:\0\0', $suggestion ), 'H:i:s' );
}

if ( $is_editing_php && isset( $existing['args']['code'] ) ) {
Expand Down
2 changes: 1 addition & 1 deletion src/event-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public function get_columns() {
sprintf(
/* translators: %s: UTC offset */
__( 'Next Run (%s)', 'wp-crontrol' ),
\Crontrol\get_utc_offset()
\Crontrol\get_timezone_location()
),
),
'crontrol_actions' => esc_html__( 'Action', 'wp-crontrol' ),
Expand Down