= 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 ? '
' : ''; ?>

array() ) ); ?>

= 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 '' . ( isset( $data['screenshots']['final']['title'] ) ? esc_attr( $data['screenshots']['final']['title'] ) : esc_attr__( 'Final Screenshot', 'w3-total-cache' ) ) . ''; } } /** * 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 '' . ( isset( $data['screenshots']['other']['title'] ) ? esc_attr( $data['screenshots']['other']['title'] ) : esc_attr__( 'Other Screenshot', 'w3-total-cache' ) ) . ''; } } } /** * 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['insights'] ) && empty( $data['diagnostics'] ) ) ) { return; } $insights = ''; $diagnostics = ''; $passed_audits = ''; foreach ( $data['insights'] as $insight ) { $insight['score'] *= 100; $insight_id = $insight['id'] ?? ''; $notice = 'notice notice-info inline'; $grade = 'w3tcps_blank'; if ( isset( $insight['score'] ) ) { $notice = self::get_breakdown_bg( $insight['score'], $insight['scoreDisplayMode'] ); $grade = self::get_breakdown_grade( $insight['score'], $insight['scoreDisplayMode'] ); } $audit_classes = ''; if ( isset( $insight['type'] ) && \is_array( $insight['type'] ) ) { foreach ( $insight['type'] as $type ) { $audit_classes .= ' ' . $type; } } $insight['description'] = preg_replace( '/(.*?)(\[.*?\])\((.*?)\)(.*?[,.?!]*)/', '$1$2$4', esc_html( $insight['description'] ) ); if ( isset( $insight['networkDependency'] ) && ! empty( $insight['networkDependency'] ) ) { $insights .= self::render_network_dependency_audit( $insight, $notice, $grade, $audit_classes ); continue; } $headers = ''; $items = ''; $insight['details'] = $insight['details'] ?? array(); foreach ( $insight['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'] ) ) { $icon = ''; if ( isset( $item['value'] ) ) { if ( true === $item['value'] || 1 === $item['value'] ) { $icon = ''; } elseif ( false === $item['value'] || 0 === $item['value'] ) { $icon = ''; } } $headers .= '' . esc_html__( 'Type', 'w3-total-cache' ) . ''; $items .= '' . $icon . 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 .= self::render_subitems_table_cell( $item['headings'], $item['items'] ); } if ( isset( $item['responseTime'] ) ) { $headers .= '' . esc_html__( 'Response Time', 'w3-total-cache' ) . ''; $items .= '' . esc_html( $item['responseTime'] ) . ''; } self::append_document_latency_status( $insight_id, $item, $headers, $items ); $items .= ''; } $items = ! empty( $items ) ? $items : '

' . esc_html__( 'No identified items were provided by Google PageSpeed Insights API for this metric', 'w3-total-cache' ) . '

'; if ( $insight['score'] >= 90 || in_array( $insight['scoreDisplayMode'], array( 'notApplicable' ), true ) ) { $passed_audits .= '
' . esc_html( $insight['title'] ) . ( isset( $insight['displayValue'] ) ? ' - ' . esc_html( $insight['displayValue'] ) : '' ) . '

' . $insight['description'] . '

' . $headers . '' . $items . '
Total Cache : ' . esc_html__( 'Tips', 'w3-total-cache' ) . '
' . $insight['instructions'] . '
'; } else { $insights .= '
' . esc_html( $insight['title'] ) . ( isset( $insight['displayValue'] ) ? ' - ' . esc_html( $insight['displayValue'] ) : '' ) . '

' . $insight['description'] . '

' . $headers . '' . $items . '
Total Cache : ' . esc_html__( 'Tips', 'w3-total-cache' ) . '
' . $insight['instructions'] . '
'; } } foreach ( $data['diagnostics'] as $diagnostic ) { $diagnostic['score'] *= 100; $diagnostic_id = $diagnostic['id'] ?? ''; $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 = ''; if ( isset( $diagnostic['type'] ) && \is_array( $diagnostic['type'] ) ) { foreach ( $diagnostic['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 .= self::render_subitems_table_cell( $item['headings'], $item['items'] ); } if ( isset( $item['responseTime'] ) ) { $headers .= '' . esc_html__( 'Response Time', 'w3-total-cache' ) . ''; $items .= '' . esc_html( $item['responseTime'] ) . ''; } self::append_document_latency_status( $diagnostic_id, $item, $headers, $items ); $items .= ''; } $items = ! empty( $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'] . '

' . $headers . '' . $items . '
Total Cache : ' . esc_html__( 'Tips', 'w3-total-cache' ) . '
' . $diagnostic['instructions'] . '
'; } else { $diagnostics .= '
' . esc_html( $diagnostic['title'] ) . ( isset( $diagnostic['displayValue'] ) ? ' - ' . esc_html( $diagnostic['displayValue'] ) : '' ) . '

' . $diagnostic['description'] . '

' . $headers . '' . $items . '
Total Cache : ' . esc_html__( 'Tips', 'w3-total-cache' ) . '
' . $diagnostic['instructions'] . '
'; } } $allowed_tags = self::get_allowed_tags(); echo wp_kses( '

' . esc_html__( 'Insights', 'w3-total-cache' ) . '

' . $insights . '

' . esc_html__( 'Diagnostics', 'w3-total-cache' ) . '

' . $diagnostics . '

' . esc_html__( 'Passed Audits', 'w3-total-cache' ) . '

' . $passed_audits . '
', $allowed_tags ); } /** * Render the specialized Network Dependency Tree insight. * * @since 2.9.1 * * @param array $insight Insight payload. * @param string $notice_class Notice classes. * @param string $grade_class Grade classes. * @param string $audit_classes Filter classes. * * @return string */ private static function render_network_dependency_audit( $insight, $notice_class, $grade_class, $audit_classes ) { $dependency = $insight['networkDependency']; $title_markup = esc_html( $insight['title'] ) . ( isset( $insight['displayValue'] ) ? ' - ' . esc_html( $insight['displayValue'] ) : '' ); $meta_text = ''; if ( ! empty( $dependency['longestChainDuration'] ) ) { $meta_text = '

' . sprintf( // Translators: 1 Longest chain duration. esc_html__( 'Longest chain duration: %1$d ms', 'w3-total-cache' ), (int) $dependency['longestChainDuration'] ) . '

'; } $chains_html = self::render_network_chain_list( $dependency['chains'] ?? array() ); $preconnected_html = self::render_preconnect_section_markup( $dependency['preconnected'] ?? array(), esc_html__( 'Preconnected origins', 'w3-total-cache' ) ); $candidates_html = self::render_preconnect_section_markup( $dependency['candidates'] ?? array(), esc_html__( 'Preconnect candidates', 'w3-total-cache' ) ); $sections = '
'; $sections .= '

' . esc_html__( 'Critical request chains', 'w3-total-cache' ) . '

'; $sections .= $meta_text; $sections .= $chains_html; if ( ! empty( $preconnected_html ) || ! empty( $candidates_html ) ) { $sections .= '
'; $sections .= $preconnected_html; $sections .= $candidates_html; $sections .= '
'; } $sections .= '
'; return '
' . $title_markup . '

' . $insight['description'] . '

' . $sections . '
Total Cache : ' . esc_html__( 'Tips', 'w3-total-cache' ) . '
' . $insight['instructions'] . '
'; } /** * Render the network chain list recursively. * * @since 2.9.1 * * @param array $chains Chain list. * * @return string */ private static function render_network_chain_list( $chains ) { if ( empty( $chains ) ) { return '

' . esc_html__( 'No critical request chains were provided for this audit.', 'w3-total-cache' ) . '

'; } $html = ''; return $html; } /** * Render a single network chain node. * * @since 2.9.1 * * @param array $node Node payload. * * @return string */ private static function render_network_chain_node( $node ) { $url = $node['url'] ?? ''; $children = $node['children'] ?? array(); $meta_parts = array(); if ( isset( $node['transferSize'] ) && \is_numeric( $node['transferSize'] ) ) { $meta_parts[] = sprintf( /* translators: %s – transfer size. */ esc_html__( '%s transferred', 'w3-total-cache' ), self::format_transfer_size( $node['transferSize'] ) ); } if ( isset( $node['duration'] ) && \is_numeric( $node['duration'] ) ) { $meta_parts[] = sprintf( /* translators: %d – duration in milliseconds. */ esc_html__( '%d ms', 'w3-total-cache' ), (int) $node['duration'] ); } $meta = ! empty( $meta_parts ) ? '' . esc_html( implode( ' • ', $meta_parts ) ) . '' : ''; $class = 'w3tcps_network_node'; if ( ! empty( $node['isLongest'] ) ) { $class .= ' w3tcps_network_node_longest'; } $label = esc_html( $url ); if ( filter_var( $url, FILTER_VALIDATE_URL ) !== false ) { $label = '' . esc_html( $url ) . ''; } elseif ( empty( $url ) ) { $label = esc_html__( '(unknown request)', 'w3-total-cache' ); } $html = '
  • '; $html .= '
    ' . $label . $meta . '
    '; if ( ! empty( $children ) ) { $html .= ''; } $html .= '
  • '; return $html; } /** * Render preconnect sections. * * @since 2.9.1 * * @param array $section Section payload. * @param string $default_name Fallback title. * * @return string */ private static function render_preconnect_section_markup( $section, $default_name ) { if ( empty( $section ) || empty( $section['entries'] ) ) { return ''; } $title = ! empty( $section['title'] ) ? $section['title'] : $default_name; $html = '
    '; $html .= '

    ' . esc_html( $title ) . '

    '; if ( ! empty( $section['description'] ) ) { $html .= '

    ' . esc_html( $section['description'] ) . '

    '; } if ( \is_string( $section['entries'] ) ) { $html .= '

    ' . esc_html( $section['entries'] ) . '

    '; } elseif ( \is_array( $section['entries'] ) ) { $html .= ''; } $html .= '
    '; return $html; } /** * Render a nested table cell from headings + sub-items. * * @since 2.9.1 * * @param array $headings Table headings. * @param array $rows Table rows. * * @return string */ private static function render_subitems_table_cell( $headings, $rows ) { if ( empty( $headings ) ) { return ''; } $headings = array_values( $headings ); $colspan = count( $headings ); $html = ''; foreach ( $headings as $heading ) { $html .= ''; } $html .= ''; if ( empty( $rows ) ) { $html .= ''; $html .= '
    ' . esc_html( $heading['label'] ?? '' ) . '
    ' . esc_html__( 'No additional data provided by PageSpeed.', 'w3-total-cache' ) . '
    '; return $html; } foreach ( $rows as $row ) { $html .= ''; foreach ( $headings as $heading ) { $html .= '' . self::format_subitem_value( $heading, $row ) . ''; } $html .= ''; } $html .= ''; return $html; } /** * Format a subitem value according to the heading definition. * * @since 2.9.1 * * @param array $heading Heading definition. * @param array $row Row data. * * @return string */ private static function format_subitem_value( $heading, $row ) { $key = $heading['key'] ?? ''; $value_type = $heading['valueType'] ?? ''; $value = ( '' !== $key && isset( $row[ $key ] ) ) ? $row[ $key ] : null; if ( null === $value && isset( $row['value'] ) && '' === $key ) { $value = $row['value']; } if ( 'source-location' === $value_type && \is_array( $value ) ) { return self::format_source_location_value( $value ); } if ( 'ms' === $value_type && \is_numeric( $value ) ) { $precision = isset( $heading['granularity'] ) ? (int) $heading['granularity'] : 0; $ms_value = $precision > 0 ? round( $value, $precision ) : round( $value ); return esc_html( $ms_value . ' ms' ); } if ( 'link' === $value_type && \is_string( $value ) && filter_var( $value, FILTER_VALIDATE_URL ) ) { return '' . esc_html( $value ) . ''; } if ( 'node' === $key && isset( $row['node'] ) ) { $snippet = isset( $row['node']['snippet'] ) ? '

    ' . esc_html__( 'Snippet', 'w3-total-cache' ) . ': ' . esc_html( $row['node']['snippet'] ) . '

    ' : ''; $selector = isset( $row['node']['selector'] ) ? '

    ' . esc_html__( 'Selector', 'w3-total-cache' ) . ': ' . esc_html( $row['node']['selector'] ) . '

    ' : ''; return $snippet . $selector; } if ( \is_array( $value ) ) { return esc_html( wp_json_encode( $value ) ); } if ( null !== $value ) { return esc_html( $value ); } return '—'; } /** * Format a source-location value into HTML. * * @since 2.9.1 * * @param array $source Source location payload. * * @return string */ private static function format_source_location_value( $source ) { $url = $source['url'] ?? ''; $file = $source['file'] ?? $url; $line = $source['line'] ?? $source['lineNumber'] ?? null; $col = $source['column'] ?? $source['columnNumber'] ?? null; $label = $file; if ( null !== $line ) { $label .= ':' . (int) $line; if ( null !== $col ) { $label .= ':' . (int) $col; } } if ( ! empty( $url ) ) { return '' . esc_html( $label ) . ''; } return esc_html( $label ); } /** * Append the document latency status icon for qualifying rows. * * @since 2.9.1 * * @param string $audit_id Audit identifier. * @param array $item Detail item. * @param string $headers Headers markup (passed by reference). * @param string $items Items markup (passed by reference). * * @return void */ private static function append_document_latency_status( $audit_id, $item, &$headers, &$items ) { if ( 'document-latency-insight' !== $audit_id ) { return; } $status = null; if ( isset( $item['value'] ) ) { if ( \is_array( $item['value'] ) && isset( $item['value']['value'] ) && \is_numeric( $item['value']['value'] ) ) { $status = (int) $item['value']['value']; } elseif ( \is_numeric( $item['value'] ) ) { $status = (int) $item['value']; } } if ( null === $status ) { return; } $headers .= '' . esc_html__( 'Document Request', 'w3-total-cache' ) . ''; $icon = $status ? 'dashicons-yes-alt' : 'dashicons-dismiss'; $label = $status ? esc_html__( 'Pass', 'w3-total-cache' ) : esc_html__( 'Fail', 'w3-total-cache' ); $items .= '' . $label . ''; } /** * Format bytes into readable strings. * * @since 2.9.1 * * @param int $bytes Byte value. * * @return string */ private static function format_transfer_size( $bytes ) { if ( empty( $bytes ) || ! \is_numeric( $bytes ) ) { return ''; } return \size_format( $bytes, 2 ); } /** * 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(), ), 'h4' => 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; } }