Skip to content

Commit

Permalink
Check if class IntlDateFormatter exists (#1251)
Browse files Browse the repository at this point in the history
The class is not found if the PHP extension `Intl` is missing.
Give a more comprehensible error message to the admin.
  • Loading branch information
herrvigg committed Nov 28, 2022
1 parent 19e4b08 commit a4437ca
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
21 changes: 14 additions & 7 deletions admin/qtx_admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,20 @@ function qtranxf_admin_init() {
qtranxf_edit_config();
}

// Check for deprecated options.
if ( isset( $q_config['use_strftime'] ) && ( $q_config['use_strftime'] ) == QTX_STRFTIME_OVERRIDE || $q_config['use_strftime'] == QTX_DATE_OVERRIDE ) {
$options_link = admin_url( 'options-general.php?page=qtranslate-xt#advanced' );
$options_desc = __( 'advanced settings', 'qtranslate' );
$warning = sprintf( __( 'The value set for option "%s" is deprecated, it will not be supported in the future. Go to the <a href="%s">%s</a> to update it.', 'qtranslate' ),
__( 'Date / Time Conversion', 'qtranslate' ), $options_link, $options_desc );
qtranxf_add_warning( $warning );
// Check for deprecated and invalid options.
if ( isset( $q_config['use_strftime'] ) ) {
if ( $q_config['use_strftime'] == QTX_STRFTIME_OVERRIDE || $q_config['use_strftime'] == QTX_DATE_OVERRIDE ) {
$warning = sprintf( __( 'The value set for option "%s" is deprecated, it will not be supported in the future. Go to the <a href="%s">%s</a> to update it.', 'qtranslate' ),
__( 'Date / Time Conversion', 'qtranslate' ), admin_url( 'options-general.php?page=qtranslate-xt#advanced' ), __( 'advanced settings', 'qtranslate' ) );
qtranxf_add_warning( $warning );
}
if ( $q_config['use_strftime'] != QTX_DATE_WP && ! class_exists( 'IntlDateFormatter' ) ) {
$warning = sprintf( __( 'The value set for option "%s" cannot be used.', 'qtranslate' ), __( 'Date / Time Conversion', 'qtranslate' ) ) . ' ';
$warning .= sprintf( __( 'Class not found: <a href="%s">%s</a> likely due to missing PHP extension: <a href="%s">%s</a>.', 'qtranslate' ),
'https://www.php.net/manual/en/class.intldateformatter.php', '`IntlDateFormatter`',
'https://www.php.net/manual/en/intl.setup.php', '`Intl`' );
qtranxf_add_error( $warning );
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion qtranslate_date_time.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,14 +465,15 @@ function qtranxf_strftime( $format, $date, $default = '', $before = '', $after =

/**
* [Legacy] Generalized formatting of a date, applying qTranslate 'use_strftime' config.
* Requires `IntlDateFormatter` in most cases.
*
* @param string $format
* @param string $mysql_time date string in MySQL format
* @param string $default_value default date value.
* @param string $before Deprecated. Not used, will be removed in a future version.
* @param string $after Deprecated. Not used, will be removed in a future version.
*
* @return string
* @return string date/time if the format is valid and `IntlDateFormatter` is found, default value otherwise.
*/
function qtranxf_format_date( $format, $mysql_time, $default_value, $before = '', $after = '' ) {
if ( ! empty( $before ) || ! empty( $after ) ) {
Expand All @@ -482,6 +483,9 @@ function qtranxf_format_date( $format, $mysql_time, $default_value, $before = ''
if ( $format == 'U' ) {
return $timestamp;
}
if ( ! class_exists( 'IntlDateFormatter' ) ) {
return $default_value;
}
$language_format = qtranxf_get_language_date_or_time_format( 'date_format' );
// TODO: abandon strftime format in qTranslate.
$date_format = qtranxf_convert_to_strftime_format_using_config( $format, $language_format );
Expand Down

0 comments on commit a4437ca

Please sign in to comment.