= 90 ) {
$color = '#0c6';
} elseif ( $score >= 50 && $score < 90 ) {
$color = '#fa3';
} elseif ( $score >= 0 && $score < 50 ) {
$color = '#f33';
}
}
return $color;
}
/**
* Render the PageSpeed desktop/mobile score guage.
*
* @param array $data PageSpeed desktop/mobile data containing score key.
* @param string $icon Desktop/Mobile icon value.
*
* @return void
*/
public static function print_gauge( $data, $icon ) {
if ( ! isset( $data ) || empty( $data['score'] ) || empty( $icon ) ) {
return;
}
$color = self::get_gauge_color( $data['score'] );
$angle = self::get_gauge_angle( $data['score'] );
?>
= 90 ) {
$bar = '' . esc_html( $metric['displayValue'] ) . '
';
} elseif ( $metric['score'] >= 50 && $metric['score'] < 90 ) {
$bar = '' . esc_html( $metric['displayValue'] ) . '
';
} elseif ( $metric['score'] < 50 ) {
$bar = '' . esc_html( $metric['displayValue'] ) . '
';
}
echo wp_kses(
'' . $bar . '
',
array(
'div' => array(
'style' => array(),
'class' => array(),
),
'span' => array(
'class' => array(),
),
)
);
}
/**
* Render core metric for desktop/mobile. Used by PageSpeed page.
*
* @param array $data PageSpeed data.
* @param string $metric Metric key.
* @param string $name Metric name.
* @param bool $widget Widget flag to add line break between desktop/mobile metric.
*
* @return void
*/
public static function print_bar_combined_with_icon( $data, $metric, $name, $widget = false ) {
if ( ! isset( $data ) || empty( $metric ) || empty( $name ) ) {
return;
}
$widget_break = $widget ? '
' : '';
?>
= 90 ) {
$notice = 'notice notice-success inline';
} elseif ( $score >= 50 && $score < 90 ) {
$noitce = 'notice notice-warning inline';
} elseif ( $score >= 0 && $score < 50 ) {
$notice = 'notice notice-error inline';
}
}
return $notice;
}
/**
* Get PageSpeed metric grade.
*
* @param int $score PageSpeed desktop/mobile score.
* @param string $display_mode PageSpeed desktop/mobile score display mode.
*
* @return string
*/
public static function get_breakdown_grade( $score, $display_mode ) {
$grade = 'w3tcps_blank';
if ( isset( $display_mode ) && in_array( $display_mode, array( 'metric', 'metricSavings' ), true ) && isset( $score ) && is_numeric( $score ) ) {
if ( $score >= 90 ) {
$grade = 'w3tcps_pass';
} elseif ( $score >= 50 && $score < 90 ) {
$grade = 'w3tcps_average';
} elseif ( $score >= 0 && $score < 50 ) {
$grade = 'w3tcps_fail';
}
}
return $grade;
}
/**
* Render the final generated screenshot.
*
* @param array $data PageSpeed data.
*
* @return void
*/
public static function print_final_screenshot( $data ) {
if ( isset( $data ) && isset( $data['screenshots']['final']['screenshot'] ) ) {
echo '';
}
}
/**
* Render all "building" screenshots.
*
* @param mixed $data PageSpeed desktop/mobile score.
*
* @return void
*/
public static function print_screenshots( $data ) {
if ( isset( $data ) && isset( $data['screenshots']['other']['screenshots'] ) ) {
foreach ( $data['screenshots']['other']['screenshots'] as $screenshot ) {
echo '';
}
}
}
/**
* Render all metric data into listable items.
*
* @param array $data PageSpeed desktop/mobile score.
*
* @return void
*/
public static function print_breakdown( $data ) {
if ( ! isset( $data ) || ( empty( $data['opportunities'] ) && empty( $data['diagnostics'] ) ) ) {
return;
}
$opportunities = '';
$diagnostics = '';
$passed_audits = '';
foreach ( $data['opportunities'] as $opportunity ) {
$opportunity['score'] *= 100;
$notice = 'notice notice-info inline';
$grade = 'w3tcps_blank';
if ( isset( $opportunity['score'] ) ) {
$notice = self::get_breakdown_bg( $opportunity['score'], $opportunity['scoreDisplayMode'] );
$grade = self::get_breakdown_grade( $opportunity['score'], $opportunity['scoreDisplayMode'] );
}
$audit_classes = '';
if ( isset( $opportunity['type'] ) ) {
foreach ( $opportunity['type'] as $type ) {
$audit_classes .= ' ' . $type;
}
}
$opportunity['description'] = preg_replace( '/(.*?)(\[.*?\])\((.*?)\)(.*?[,.?!]*)/', '$1$2$4', esc_html( $opportunity['description'] ) );
$headers = '';
$items = '';
$opportunity['details'] = $opportunity['details'] ?? array();
foreach ( $opportunity['details'] as $item ) {
$headers = '';
$items .= '';
if ( isset( $item['url'] ) ) {
$headers .= '' . esc_html__( 'URL', 'w3-total-cache' ) . ' | ';
if ( filter_var( $item['url'], FILTER_VALIDATE_URL ) !== false ) {
// The value is confirmed as a valid URL. We create a HTML link with the full URL value but display it with a trucated value.
$items .= ' ' . esc_url( $item['url'] ) . ' | ';
} else {
// For certain metrics Google uses the 'url' field for non-URL values. These are often HTML/CSS that shouldn't be escaped and will be displayed as plain text.
$items .= '' . esc_html( $item['url'] ) . ' | ';
}
}
if ( isset( $item['source'] ) ) {
$headers .= '' . esc_html__( 'URL', 'w3-total-cache' ) . ' | ';
if ( filter_var( $item['source']['url'], FILTER_VALIDATE_URL ) !== false ) {
// The value is confirmed as a valid URL. We create a HTML link with the full URL value but display it with a trucated value.
$items .= ' ' . esc_url( $item['url'] ) . ' | ';
} else {
// For certain metrics Google uses the 'url' field for non-URL values. These are often HTML/CSS that shouldn't be escaped and will be displayed as plain text.
$items .= '' . esc_html( $item['source']['url'] ) . ' | ';
}
}
if ( isset( $item['totalBytes'] ) ) {
$headers .= '' . esc_html__( 'Total Bytes', 'w3-total-cache' ) . ' | ';
$items .= '' . esc_html( $item['totalBytes'] ) . ' | ';
}
if ( isset( $item['wastedBytes'] ) ) {
$headers .= '' . esc_html__( 'Wasted Bytes', 'w3-total-cache' ) . ' | ';
$items .= '' . esc_html( $item['wastedBytes'] ) . ' | ';
}
if ( isset( $item['wastedPercent'] ) ) {
$headers .= '' . esc_html__( 'Wasted Percentage', 'w3-total-cache' ) . ' | ';
$items .= '' . round( $item['wastedPercent'], 2 ) . '% | ';
}
if ( isset( $item['wastedMs'] ) ) {
$headers .= '' . esc_html__( 'Wasted Miliseconds', 'w3-total-cache' ) . ' | ';
$items .= '' . round( $item['wastedMs'], 2 ) . ' | ';
}
if ( isset( $item['label'] ) ) {
$headers .= '' . esc_html__( 'Type', 'w3-total-cache' ) . ' | ';
$items .= '' . esc_html( $item['label'] ) . ' | ';
}
if ( isset( $item['groupLabel'] ) ) {
$headers .= '' . esc_html__( 'Group', 'w3-total-cache' ) . ' | ';
$items .= '' . esc_html( $item['groupLabel'] ) . ' | ';
}
if ( isset( $item['requestCount'] ) ) {
$headers .= '' . esc_html__( 'Requests', 'w3-total-cache' ) . ' | ';
$items .= '' . esc_html( $item['requestCount'] ) . ' | ';
}
if ( isset( $item['transferSize'] ) ) {
$headers .= '' . esc_html__( 'Transfer Size', 'w3-total-cache' ) . ' | ';
$items .= '' . esc_html( $item['transferSize'] ) . ' | ';
}
if ( isset( $item['startTime'] ) ) {
$headers .= '' . esc_html__( 'Start Time', 'w3-total-cache' ) . ' | ';
$items .= '' . esc_html( $item['startTime'] ) . ' | ';
}
if ( isset( $item['duration'] ) ) {
$headers .= '' . esc_html__( 'Duration', 'w3-total-cache' ) . ' | ';
$items .= '' . esc_html( $item['duration'] ) . ' | ';
}
if ( isset( $item['scriptParseCompile'] ) ) {
$headers .= '' . esc_html__( 'Parse/Compile Time', 'w3-total-cache' ) . ' | ';
$items .= '' . esc_html( $item['scriptParseCompile'] ) . ' | ';
}
if ( isset( $item['scripting'] ) ) {
$headers .= '' . esc_html__( 'Execution Time', 'w3-total-cache' ) . ' | ';
$items .= '' . esc_html( $item['scripting'] ) . ' | ';
}
if ( isset( $item['total'] ) ) {
$headers .= '' . esc_html__( 'Total', 'w3-total-cache' ) . ' | ';
$items .= '' . esc_html( $item['total'] ) . ' | ';
}
if ( isset( $item['cacheLifetimeMs'] ) ) {
$headers .= '' . esc_html__( 'Cache Lifetime Miliseconds', 'w3-total-cache' ) . ' | ';
$items .= '' . esc_html( $item['cacheLifetimeMs'] ) . ' | ';
}
if ( isset( $item['cacheHitProbability'] ) ) {
$headers .= '' . esc_html__( 'Cache Hit Probability', 'w3-total-cache' ) . ' | ';
$items .= '' . esc_html( $item['cacheHitProbability'] ) . ' | ';
}
if ( isset( $item['value'] ) && isset( $item['statistic'] ) ) {
$headers .= '' . esc_html__( 'Statistic', 'w3-total-cache' ) . ' | ';
$items .= '' . esc_html( $item['statistic'] ) . ' | ';
$headers .= '' . esc_html__( 'Element', 'w3-total-cache' ) . ' | ';
$items .= '';
if ( isset( $item['node'] ) ) {
$items .= ' ' . __( 'Snippet', 'w3-total-cache' ) . ': ' . esc_html( $item['node']['snippet'] ) . ' ';
$items .= '' . __( 'Selector', 'w3-total-cache' ) . ': ' . esc_html( $item['node']['selector'] ) . ' ';
}
$items .= ' | ';
$headers .= '' . esc_html__( 'Value', 'w3-total-cache' ) . ' | ';
$items .= is_array( $item['value'] ) ? '' . esc_html( $item['value']['value'] ) . ' | ' : '' . esc_html( $item['value'] ) . ' | ';
} elseif ( isset( $item['node'] ) ) {
$headers .= '' . esc_html__( 'Element', 'w3-total-cache' ) . ' | ';
$items .= '';
$items .= ' ' . __( 'Snippet', 'w3-total-cache' ) . ': ' . esc_html( $item['node']['snippet'] ) . ' ';
$items .= '' . __( 'Selector', 'w3-total-cache' ) . ': ' . esc_html( $item['node']['selector'] ) . ' ';
$items .= ' | ';
}
if ( isset( $item['headings'] ) && isset( $item['items'] ) ) {
$items .= '';
foreach ( $item['items'] as $sub_item ) {
$items .= '';
if ( isset( $sub_item['node'] ) ) {
$items .= '';
$items .= ' ' . __( 'Snippet', 'w3-total-cache' ) . ': ' . esc_html( $sub_item['node']['snippet'] ) . ' ';
$items .= '' . __( 'Selector', 'w3-total-cache' ) . ': ' . esc_html( $sub_item['node']['selector'] ) . ' ';
$items .= ' | ';
}
if ( isset( $sub_item['phase'] ) && isset( $sub_item['timing'] ) && isset( $sub_item['percent'] ) ) {
$items .= '' . esc_html( $sub_item['phase'] ) . ' | ';
$items .= '' . esc_html( $sub_item['percent'] ) . ' | ';
$items .= '' . esc_html( $sub_item['timing'] ) . ' ms | ';
}
$items .= ' ';
}
$items .= ' | ';
}
if ( isset( $item['responseTime'] ) ) {
$headers .= '' . esc_html__( 'Response Time', 'w3-total-cache' ) . ' | ';
$items .= '' . esc_html( $item['responseTime'] ) . ' | ';
}
$items .= '
';
}
$items = ( isset( $items ) ? $items : '' . esc_html__( 'No identified items were provided by Google PageSpeed Insights API for this metric', 'w3-total-cache' ) . '
' );
if ( $opportunity['score'] >= 90 || in_array( $opportunity['scoreDisplayMode'], array( 'notApplicable' ), true ) ) {
$passed_audits .= '
' . esc_html( $opportunity['title'] ) . ( isset( $opportunity['displayValue'] ) ? ' - ' . esc_html( $opportunity['displayValue'] ) : '' ) . '
' . $opportunity['description'] . '
' . $opportunity['instructions'] . '
';
} else {
$opportunities .= '
' . esc_html( $opportunity['title'] ) . ( isset( $opportunity['displayValue'] ) ? ' - ' . esc_html( $opportunity['displayValue'] ) : '' ) . '
' . $opportunity['description'] . '
' . $opportunity['instructions'] . '
';
}
}
foreach ( $data['diagnostics'] as $diagnostic ) {
$diagnostic['score'] *= 100;
$notice = 'notice notice-info inline';
$grade = 'w3tcps_blank';
if ( isset( $diagnostic['score'] ) ) {
$notice = self::get_breakdown_bg( $diagnostic['score'], $diagnostic['scoreDisplayMode'] );
$grade = self::get_breakdown_grade( $diagnostic['score'], $diagnostic['scoreDisplayMode'] );
}
$audit_classes = '';
foreach ( $opportunity['type'] as $type ) {
$audit_classes .= ' ' . $type;
}
$diagnostic['description'] = preg_replace( '/(.*?)(\[.*?\])\((.*?)\)(.*?[,.?!]*)/', '$1$2$4', esc_html( $diagnostic['description'] ) );
$headers = '';
$items = '';
$diagnostic['details'] = $diagnostic['details'] ?? array();
foreach ( $diagnostic['details'] as $item ) {
$headers = '';
$items .= '';
if ( isset( $item['url'] ) ) {
$headers .= '' . esc_html__( 'URL', 'w3-total-cache' ) . ' | ';
if ( filter_var( $item['url'], FILTER_VALIDATE_URL ) !== false ) {
// The value is confirmed as a valid URL. We create a HTML link with the full URL value but display it with a trucated value.
$items .= '' . esc_url( $item['url'] ) . ' | ';
} else {
// For certain metrics Google uses the 'url' field for non-URL values. These are often HTML/CSS that shouldn't be escaped and will be displayed as plain text.
$items .= '' . esc_html( $item['url'] ) . ' | ';
}
}
if ( isset( $item['source'] ) ) {
$headers .= '' . esc_html__( 'URL', 'w3-total-cache' ) . ' | ';
if ( filter_var( $item['source']['url'], FILTER_VALIDATE_URL ) !== false ) {
// The value is confirmed as a valid URL. We create a HTML link with the full URL value but display it with a trucated value.
$items .= '' . esc_url( $item['url'] ) . ' | ';
} else {
// For certain metrics Google uses the 'url' field for non-URL values. These are often HTML/CSS that shouldn't be escaped and will be displayed as plain text.
$items .= '' . esc_html( $item['source']['url'] ) . ' | ';
}
}
if ( isset( $item['totalBytes'] ) ) {
$headers .= '' . esc_html__( 'Total Bytes', 'w3-total-cache' ) . ' | ';
$items .= '' . esc_html( $item['totalBytes'] ) . ' | ';
}
if ( isset( $item['wastedBytes'] ) ) {
$headers .= '' . esc_html__( 'Wasted Bytes', 'w3-total-cache' ) . ' | ';
$items .= '' . esc_html( $item['wastedBytes'] ) . ' | ';
}
if ( isset( $item['wastedPercent'] ) ) {
$headers .= '' . esc_html__( 'Wasted Percentage', 'w3-total-cache' ) . ' | ';
$items .= '' . round( $item['wastedPercent'], 2 ) . '% | ';
}
if ( isset( $item['wastedMs'] ) ) {
$headers .= '' . esc_html__( 'Wasted Miliseconds', 'w3-total-cache' ) . ' | ';
$items .= '' . round( $item['wastedMs'], 2 ) . ' | ';
}
if ( isset( $item['label'] ) ) {
$headers .= '' . esc_html__( 'Type', 'w3-total-cache' ) . ' | ';
$items .= '' . esc_html( $item['label'] ) . ' | ';
}
if ( isset( $item['groupLabel'] ) ) {
$headers .= '' . esc_html__( 'Group', 'w3-total-cache' ) . ' | ';
$items .= '' . esc_html( $item['groupLabel'] ) . ' | ';
}
if ( isset( $item['requestCount'] ) ) {
$headers .= '' . esc_html__( 'Requests', 'w3-total-cache' ) . ' | ';
$items .= '' . esc_html( $item['requestCount'] ) . ' | ';
}
if ( isset( $item['transferSize'] ) ) {
$headers .= '' . esc_html__( 'Transfer Size', 'w3-total-cache' ) . ' | ';
$items .= '' . esc_html( $item['transferSize'] ) . ' | ';
}
if ( isset( $item['startTime'] ) ) {
$headers .= '' . esc_html__( 'Start Time', 'w3-total-cache' ) . ' | ';
$items .= '' . esc_html( $item['startTime'] ) . ' | ';
}
if ( isset( $item['duration'] ) ) {
$headers .= '' . esc_html__( 'Duration', 'w3-total-cache' ) . ' | ';
$items .= '' . esc_html( $item['duration'] ) . ' | ';
}
if ( isset( $item['scriptParseCompile'] ) ) {
$headers .= '' . esc_html__( 'Parse/Compile Time', 'w3-total-cache' ) . ' | ';
$items .= '' . esc_html( $item['scriptParseCompile'] ) . ' | ';
}
if ( isset( $item['scripting'] ) ) {
$headers .= '' . esc_html__( 'Execution Time', 'w3-total-cache' ) . ' | ';
$items .= '' . esc_html( $item['scripting'] ) . ' | ';
}
if ( isset( $item['total'] ) ) {
$headers .= '' . esc_html__( 'Total', 'w3-total-cache' ) . ' | ';
$items .= '' . esc_html( $item['total'] ) . ' | ';
}
if ( isset( $item['cacheLifetimeMs'] ) ) {
$headers .= '' . esc_html__( 'Cache Lifetime Miliseconds', 'w3-total-cache' ) . ' | ';
$items .= '' . esc_html( $item['cacheLifetimeMs'] ) . ' | ';
}
if ( isset( $item['cacheHitProbability'] ) ) {
$headers .= '' . esc_html__( 'Cache Hit Probability', 'w3-total-cache' ) . ' | ';
$items .= '' . ( esc_html( $item['cacheHitProbability'] ) * 100 ) . '% | ';
}
if ( isset( $item['value'] ) && isset( $item['statistic'] ) ) {
$headers .= '' . esc_html__( 'Statistic', 'w3-total-cache' ) . ' | ';
$items .= '' . esc_html( $item['statistic'] ) . ' | ';
$headers .= '' . esc_html__( 'Element', 'w3-total-cache' ) . ' | ';
$items .= '';
if ( isset( $item['node'] ) ) {
$items .= ' ' . __( 'Snippet', 'w3-total-cache' ) . ': ' . esc_html( $item['node']['snippet'] ) . ' ';
$items .= '' . __( 'Selector', 'w3-total-cache' ) . ': ' .esc_html( $item['node']['selector'] ) . ' ';
}
$items .= ' | ';
$headers .= '' . esc_html__( 'Value', 'w3-total-cache' ) . ' | ';
$items .= is_array( $item['value'] ) ? '' . esc_html( $item['value']['value'] ) . ' | ' : '' . esc_html( $item['value'] ) . ' | ';
} elseif ( isset( $item['node'] ) ) {
$headers .= '' . esc_html__( 'Element', 'w3-total-cache' ) . ' | ';
$items .= '';
$items .= ' ' . __( 'Snippet', 'w3-total-cache' ) . ': ' . esc_html( $item['node']['snippet'] ) . ' ';
$items .= '' . __( 'Selector', 'w3-total-cache' ) . ': ' . esc_html( $item['node']['selector'] ) . ' ';
$items .= ' | ';
}
if ( isset( $item['headings'] ) && isset( $item['items'] ) ) {
$items .= '';
foreach ( $item['items'] as $sub_item ) {
$items .= '';
if ( isset( $sub_item['node'] ) ) {
$items .= '';
$items .= ' ' . __( 'Snippet', 'w3-total-cache' ) . ': ' . esc_html( $sub_item['node']['snippet'] ) . ' ';
$items .= '' . __( 'Selector', 'w3-total-cache' ) . ': ' . esc_html( $sub_item['node']['selector'] ) . ' ';
$items .= ' | ';
}
if ( isset( $sub_item['phase'] ) && isset( $sub_item['timing'] ) && isset( $sub_item['percent'] ) ) {
$items .= '' . esc_html( $sub_item['phase'] ) . ' | ';
$items .= '' . esc_html( $sub_item['percent'] ) . ' | ';
$items .= '' . esc_html( $sub_item['timing'] ) . ' ms | ';
}
$items .= ' ';
}
$items .= ' | ';
}
if ( isset( $item['responseTime'] ) ) {
$headers .= '' . esc_html__( 'Response Time', 'w3-total-cache' ) . ' | ';
$items .= '' . esc_html( $item['responseTime'] ) . ' | ';
}
$items .= '
';
}
$items = ( isset( $items ) ? $items : '' . esc_html__( 'No identified items were provided by Google PageSpeed Insights API for this metric', 'w3-total-cache' ) . '
' );
if ( $diagnostic['score'] >= 90 || in_array( $diagnostic['scoreDisplayMode'], array( 'notApplicable' ), true ) ) {
$passed_audits .= '
' . esc_html( $diagnostic['title'] ) . ( isset( $diagnostic['displayValue'] ) ? ' - ' . esc_html( $diagnostic['displayValue'] ) : '' ) . '
' . $diagnostic['description'] . '
' . $diagnostic['instructions'] . '
';
} else {
$diagnostics .= '
' . esc_html( $diagnostic['title'] ) . ( isset( $diagnostic['displayValue'] ) ? ' - ' . esc_html( $diagnostic['displayValue'] ) : '' ) . '
' . $diagnostic['description'] . '
' . $diagnostic['instructions'] . '
';
}
}
$allowed_tags = self::get_allowed_tags();
echo wp_kses(
'
' . esc_html__( 'Opportunities', 'w3-total-cache' ) . '
' . $opportunities . '
' . esc_html__( 'Diagnostics', 'w3-total-cache' ) . '
' . $diagnostics . '
' . esc_html__( 'Passed Audits', 'w3-total-cache' ) . '
' . $passed_audits . '
',
$allowed_tags
);
}
/**
* Recursively get value based on series of key decendents.
*
* @param array $data PageSpeed data.
* @param array $elements Array of key decendents.
*
* @return object | null
*/
public static function get_value_recursive( $data, $elements ) {
if ( empty( $elements ) ) {
return $data;
}
$key = array_shift( $elements );
if ( ! isset( $data[ $key ] ) ) {
return null;
}
return self::get_value_recursive( $data[ $key ], $elements );
}
/**
* Return wp_kses allowed HTML tags/attributes.
*
* @return array
*/
public static function get_allowed_tags() {
return array(
'div' => array(
'id' => array(),
'class' => array(),
),
'span' => array(
'id' => array(),
'class' => array(),
'title' => array(),
'gatitle' => array(),
'copyurl' => array(),
),
'p' => array(
'id' => array(),
'class' => array(),
),
'table' => array(
'id' => array(),
'class' => array(),
),
'tr' => array(
'id' => array(),
'class' => array(),
),
'td' => array(
'id' => array(),
'class' => array(),
),
'th' => array(
'id' => array(),
'class' => array(),
),
'b' => array(
'id' => array(),
'class' => array(),
),
'br' => array(),
'a' => array(
'id' => array(),
'class' => array(),
'href' => array(),
'target' => array(),
'rel' => array(),
'title' => array(),
),
'link' => array(
'id' => array(),
'class' => array(),
'href' => array(),
'rel' => array(),
'as' => array(),
'type' => array(),
),
'code' => array(
'id' => array(),
'class' => array(),
),
'img' => array(
'id' => array(),
'class' => array(),
'srcset' => array(),
'src' => array(),
'alt' => array(),
),
'ul' => array(
'id' => array(),
'class' => array(),
),
'ol' => array(
'id' => array(),
'class' => array(),
),
'li' => array(
'id' => array(),
'class' => array(),
),
'h3' => array(
'id' => array(),
'class' => array(),
),
);
}
/**
* Get cache life time.
*
* @return int
*/
public static function get_cache_life() {
return 3600;
}
/**
* Conver seconds into string breaking down days/hours/minutes/seconds.
*
* @param int $seconds Seconds.
*
* @return string
*/
public static function seconds_to_str( $seconds ) {
$buffer = '';
if ( $seconds >= 86400 ) {
$days = floor( $seconds / 86400 );
$seconds = $seconds % 86400;
$buffer .= $days . ' day' . ( $days > 1 ? 's' : '' ) . ( $seconds > 0 ? ', ' : '' );
}
if ( $seconds >= 3600 ) {
$hours = floor( $seconds / 3600 );
$seconds = $seconds % 3600;
$buffer .= $hours . ' hour' . ( $hours > 1 ? 's' : '' ) . ( $seconds > 0 ? ', ' : '' );
}
if ( $seconds >= 60 ) {
$minutes = floor( $seconds / 60 );
$seconds = $seconds % 60;
$buffer .= $minutes . ' minute' . ( $minutes > 1 ? 's' : '' ) . ( $seconds > 0 ? ', ' : '' );
}
if ( $seconds > 0 ) {
$buffer .= $seconds . ' second' . ( $seconds > 1 ? 's' : '' );
}
return $buffer;
}
}