| 1 |
<?php // phpcs:disable WordPress.Files.FileName -- PSR-4 class file naming; renaming would break the autoloader. |
| 2 |
|
| 3 |
|
| 4 |
namespace GauchoPlugins\VersionInfo; |
| 5 |
|
| 6 |
defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
| 7 |
|
| 8 |
/** |
| 9 |
* "Version Info" dashboard widget. |
| 10 |
*/ |
| 11 |
class DashboardWidget { |
| 12 |
|
| 13 |
/** |
| 14 |
* Hooks the dashboard-widget registration. |
| 15 |
*/ |
| 16 |
public function register() { |
| 17 |
add_action( 'wp_dashboard_setup', array( $this, 'maybe_add_widget' ) ); |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Adds the widget when enabled and the user may view version info. |
| 22 |
*/ |
| 23 |
public function maybe_add_widget() { |
| 24 |
if ( ! get_option( 'version_info_show_dashboard_widget', false ) || ! Plugin::current_user_can_view() ) { |
| 25 |
return; |
| 26 |
} |
| 27 |
|
| 28 |
wp_add_dashboard_widget( |
| 29 |
'version_info_dashboard_widget', |
| 30 |
__( 'Version Info', 'version-info' ), |
| 31 |
array( $this, 'render' ) |
| 32 |
); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Renders the widget body. |
| 37 |
*/ |
| 38 |
public function render() { |
| 39 |
global $wpdb; |
| 40 |
|
| 41 |
$items = array( |
| 42 |
'wordpress' => array( |
| 43 |
'label' => __( 'WordPress Version:', 'version-info' ), |
| 44 |
'value' => apply_filters( 'version_info_wp_version', get_bloginfo( 'version' ) ), |
| 45 |
), |
| 46 |
'php' => array( |
| 47 |
'label' => __( 'PHP Version:', 'version-info' ), |
| 48 |
'value' => apply_filters( 'version_info_php_version', phpversion() ), |
| 49 |
), |
| 50 |
'server' => array( |
| 51 |
'label' => __( 'Web Server:', 'version-info' ), |
| 52 |
'value' => apply_filters( |
| 53 |
'version_info_server_software', |
| 54 |
sanitize_text_field( isset( $_SERVER['SERVER_SOFTWARE'] ) ? $_SERVER['SERVER_SOFTWARE'] : 'Unknown' ) |
| 55 |
), |
| 56 |
), |
| 57 |
'mysql' => array( |
| 58 |
'label' => __( 'MySQL Version:', 'version-info' ), |
| 59 |
'value' => apply_filters( 'version_info_mysql_version', (string) $wpdb->db_version() ), |
| 60 |
), |
| 61 |
); |
| 62 |
|
| 63 |
if ( get_option( 'version_info_show_memory', true ) ) { |
| 64 |
$memory_text = SystemInfo::memory_text(); |
| 65 |
if ( '' !== $memory_text ) { |
| 66 |
// Key 'memory' — the PRO full HUD unsets this row in favor of |
| 67 |
// its richer memory_usage/php_memory/php_peak rows. |
| 68 |
$items['memory'] = array( |
| 69 |
'label' => __( 'PHP Memory:', 'version-info' ), |
| 70 |
'value' => $memory_text, |
| 71 |
); |
| 72 |
} |
| 73 |
$server_ip = SystemInfo::server_ip(); |
| 74 |
if ( '' !== $server_ip ) { |
| 75 |
// Key deliberately matches the PRO widget-extras key so the |
| 76 |
// full HUD overwrites this row in place (no duplicates). |
| 77 |
$items['server_ip'] = array( |
| 78 |
'label' => __( 'Server IP:', 'version-info' ), |
| 79 |
'value' => $server_ip, |
| 80 |
); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
$eol = SystemInfo::php_eol_info(); |
| 85 |
$eol_date = null === $eol['date'] ? '' : date_i18n( get_option( 'date_format' ), strtotime( $eol['date'] ) ); |
| 86 |
if ( $eol['is_eol'] ) { |
| 87 |
/* translators: %s: end-of-life date of the running PHP version */ |
| 88 |
$eol_value = sprintf( __( 'End of life since %s', 'version-info' ), $eol_date ); |
| 89 |
} elseif ( null !== $eol['days_left'] ) { |
| 90 |
/* translators: %1$s: number of days until end of life, %2$s: end-of-life date */ |
| 91 |
$eol_value = sprintf( __( 'Supported — %1$s days left (%2$s)', 'version-info' ), number_format_i18n( $eol['days_left'] ), $eol_date ); |
| 92 |
} else { |
| 93 |
$eol_value = __( 'No EOL data (current release)', 'version-info' ); |
| 94 |
} |
| 95 |
$items['php_eol'] = array( |
| 96 |
'label' => __( 'PHP Lifecycle:', 'version-info' ), |
| 97 |
'value' => $eol_value, |
| 98 |
); |
| 99 |
|
| 100 |
/** |
| 101 |
* Filtered dashboard-widget rows. |
| 102 |
* |
| 103 |
* @var array<string, array{label: string, value: string, html?: string}> $items |
| 104 |
*/ |
| 105 |
$items = apply_filters( 'version_info_dashboard_widget_items', $items ); |
| 106 |
|
| 107 |
$allowed = array( |
| 108 |
'span' => array( |
| 109 |
'id' => true, |
| 110 |
'class' => true, |
| 111 |
'style' => true, |
| 112 |
), |
| 113 |
'div' => array( |
| 114 |
'id' => true, |
| 115 |
'class' => true, |
| 116 |
'style' => true, |
| 117 |
), |
| 118 |
'strong' => array( 'style' => true ), |
| 119 |
'code' => array(), |
| 120 |
'em' => array(), |
| 121 |
'br' => array(), |
| 122 |
'svg' => array( |
| 123 |
'id' => true, |
| 124 |
'class' => true, |
| 125 |
'style' => true, |
| 126 |
'width' => true, |
| 127 |
'height' => true, |
| 128 |
'viewbox' => true, |
| 129 |
'xmlns' => true, |
| 130 |
), |
| 131 |
'polyline' => array( |
| 132 |
'id' => true, |
| 133 |
'class' => true, |
| 134 |
'style' => true, |
| 135 |
'points' => true, |
| 136 |
'stroke' => true, |
| 137 |
'stroke-width' => true, |
| 138 |
'fill' => true, |
| 139 |
), |
| 140 |
); |
| 141 |
|
| 142 |
// WP's safecss_filter_attr strips `display` by default. Allow it for |
| 143 |
// the duration of this render so our percent-bar markup survives. |
| 144 |
add_filter( 'safe_style_css', array( $this, 'allow_display_css' ) ); |
| 145 |
|
| 146 |
echo '<ul>'; |
| 147 |
foreach ( $items as $item ) { |
| 148 |
echo '<li><strong>' . esc_html( $item['label'] ) . '</strong> '; |
| 149 |
if ( isset( $item['html'] ) && '' !== $item['html'] ) { |
| 150 |
echo wp_kses( $item['html'], $allowed ); |
| 151 |
} else { |
| 152 |
echo esc_html( $item['value'] ); |
| 153 |
} |
| 154 |
echo '</li>'; |
| 155 |
} |
| 156 |
echo '</ul>'; |
| 157 |
|
| 158 |
// Rendered outside the item loop: the wp_kses whitelist above has no |
| 159 |
// <button>, deliberately. Click handler lives in AdminBar::print_copy_js(). |
| 160 |
echo '<p><button type="button" class="button" id="vi-copy-md">' . esc_html__( 'Copy as Markdown', 'version-info' ) . '</button></p>'; |
| 161 |
|
| 162 |
remove_filter( 'safe_style_css', array( $this, 'allow_display_css' ) ); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Allows the `display` CSS property through safecss_filter_attr(). |
| 167 |
* |
| 168 |
* @param string[] $props Allowed CSS property names. |
| 169 |
* @return string[] |
| 170 |
*/ |
| 171 |
public function allow_display_css( $props ) { |
| 172 |
if ( ! in_array( 'display', $props, true ) ) { |
| 173 |
$props[] = 'display'; |
| 174 |
} |
| 175 |
return $props; |
| 176 |
} |
| 177 |
} |
| 178 |
|