PluginProbe
Version Info – Server Health Monitor, PHP & MySQL Version Display, Environment Indicators / 1.3.3
Version Info – Server Health Monitor, PHP & MySQL Version Display, Environment Indicators v1.3.3
2.1.0 2.0.3 2.0.2 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.2.0 1.2.1 1.3.0 1.3.1 1.3.2 1.3.3 2.0.0 2.0.1
version-info / version-info.php

version-info.php in Version Info – Server Health Monitor, PHP & MySQL Version Display, Environment Indicators 1.3.3, at version-info.php

254 lines 9.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Version Info
4 * Plugin URI: https://wordpress.org/plugins/version-info
5 * Description: Show current WordPress, PHP, Web Server, and MySQL versions optionally in the admin footer, WP-Admin bar, or dashboard widget.
6 * Author: Gaucho Plugins
7 * Author URI: https://gauchoplugins.com
8 * Version: 1.3.3
9 * License: GPLv3
10 * License URI: https://www.gnu.org/licenses/gpl-3.0.html
11 * Text Domain: version-info
12 */
13
14 namespace GauchoPlugins\VersionInfo;
15
16 use wpdb;
17
18 class VersionInfo {
19 private $db;
20
21 /**
22 * Constructor to initialize the plugin.
23 */
24 public function __construct(wpdb $wpdb) {
25 $this->db = $wpdb;
26 add_action('plugins_loaded', [$this, 'load_text_domain']);
27 add_filter('update_footer', [$this, 'version_in_footer'], 11);
28 add_action('admin_bar_menu', [$this, 'add_version_info_to_admin_bar'], 100);
29 add_action('admin_menu', [$this, 'add_settings_page']);
30 add_action('admin_init', [$this, 'register_settings']);
31 add_action('wp_dashboard_setup', [$this, 'conditionally_add_dashboard_widget']);
32 }
33
34 /**
35 * Load the plugin's text domain for translation.
36 */
37 public function load_text_domain() {
38 load_plugin_textdomain('version-info', false, dirname(plugin_basename(__FILE__)) . '/languages');
39 }
40
41 /**
42 * Add version info to the WP-Admin bar (admin-only, controlled by settings).
43 */
44 public function add_version_info_to_admin_bar($wp_admin_bar) {
45 if (!get_option('version_info_show_admin_bar', false) || !current_user_can('administrator')) {
46 return;
47 }
48
49 $wp_admin_bar->add_node([
50 'id' => 'version_info_admin_bar',
51 'title' => $this->get_admin_bar_version_details(),
52 'parent' => 'top-secondary',
53 ]);
54 }
55
56 /**
57 * Display version info in the admin footer (admin-only, controlled by settings).
58 */
59 public function version_in_footer() {
60 if (!get_option('version_info_show_footer', true) || !current_user_can('administrator')) {
61 return '';
62 }
63 return $this->get_footer_version_details();
64 }
65
66 /**
67 * Retrieve version details for display in the WP-Admin bar (without update link).
68 */
69 private function get_admin_bar_version_details() {
70 $wp_version = get_bloginfo('version');
71 $server_software = sanitize_text_field($_SERVER['SERVER_SOFTWARE'] ?? __('Unknown', 'version-info'));
72 $mysql_version = $this->db->get_var('SELECT VERSION()');
73 if (is_wp_error($mysql_version)) {
74 $mysql_version = __('Error fetching version', 'version-info');
75 }
76
77 return sprintf(
78 __('WordPress %s | PHP %s | Web Server %s | MySQL %s', 'version-info'),
79 esc_html($wp_version),
80 esc_html(phpversion()),
81 esc_html($server_software),
82 esc_html($mysql_version)
83 );
84 }
85
86 /**
87 * Retrieve version details for display in the footer (with update link if available).
88 */
89 private function get_footer_version_details() {
90 // Get the current WordPress version.
91 $wp_version = get_bloginfo('version');
92 $update_message = '';
93
94 // Check if there are any WordPress core updates available.
95 $updates = get_core_updates();
96 if (!empty($updates) && !is_wp_error($updates)) {
97 foreach ($updates as $update) {
98 // If there's an update and it's a new version, add the update message.
99 if (version_compare($wp_version, $update->version, '<')) {
100 $update_message = sprintf(
101 ' (<a href="%s">%s %s</a>)',
102 esc_url(admin_url('update-core.php')),
103 __('Get Version', 'version-info'),
104 esc_html($update->version)
105 );
106 break; // We only need to display the first update found.
107 }
108 }
109 }
110
111 // Fetch the server and MySQL version details.
112 $server_software = sanitize_text_field($_SERVER['SERVER_SOFTWARE'] ?? __('Unknown', 'version-info'));
113 $mysql_version = $this->db->get_var('SELECT VERSION()');
114 if (is_wp_error($mysql_version)) {
115 $mysql_version = __('Error fetching version', 'version-info');
116 }
117
118 // Combine the details into the final version string.
119 return sprintf(
120 __('WordPress %s%s | PHP %s | Web Server %s | MySQL %s', 'version-info'),
121 esc_html($wp_version),
122 $update_message, // Add the update message if available.
123 esc_html(phpversion()),
124 esc_html($server_software),
125 esc_html($mysql_version)
126 );
127 }
128
129 /**
130 * Add a settings page under Settings > Version Info.
131 */
132 public function add_settings_page() {
133 add_options_page(
134 __('Version Info Settings', 'version-info'),
135 __('Version Info', 'version-info'),
136 'manage_options',
137 'version-info-settings',
138 [$this, 'render_settings_page']
139 );
140 }
141
142 /**
143 * Register plugin settings with validation.
144 */
145 public function register_settings() {
146 register_setting('version_info_settings_group', 'version_info_show_footer', [
147 'sanitize_callback' => [$this, 'validate_boolean_option'],
148 ]);
149 register_setting('version_info_settings_group', 'version_info_show_admin_bar', [
150 'sanitize_callback' => [$this, 'validate_boolean_option'],
151 ]);
152 register_setting('version_info_settings_group', 'version_info_show_dashboard_widget', [
153 'sanitize_callback' => [$this, 'validate_boolean_option'],
154 'default' => 0, // Set widget disabled by default
155 ]);
156 }
157
158 /**
159 * Validate boolean options.
160 */
161 public function validate_boolean_option($input) {
162 return filter_var($input, FILTER_VALIDATE_BOOLEAN); // Better boolean validation
163 }
164
165 /**
166 * Conditionally add the dashboard widget based on the settings.
167 */
168 public function conditionally_add_dashboard_widget() {
169 // Only register the widget if the setting is enabled
170 if (get_option('version_info_show_dashboard_widget', false) && current_user_can('administrator')) {
171 $this->add_dashboard_widget();
172 }
173 }
174
175 /**
176 * Add the dashboard widget (admin-only, controlled by settings).
177 */
178 public function add_dashboard_widget() {
179 wp_add_dashboard_widget(
180 'version_info_dashboard_widget',
181 __('Version Info', 'version-info'),
182 [$this, 'display_dashboard_widget']
183 );
184 }
185
186 /**
187 * Display content for the dashboard widget.
188 */
189 public function display_dashboard_widget() {
190 global $wpdb;
191
192 echo '<ul>';
193 echo '<li><strong>' . esc_html__('WordPress Version:', 'version-info') . '</strong> ' . esc_html(get_bloginfo('version')) . '</li>';
194 echo '<li><strong>' . esc_html__('PHP Version:', 'version-info') . '</strong> ' . esc_html(phpversion()) . '</li>';
195 echo '<li><strong>' . esc_html__('Web Server:', 'version-info') . '</strong> ' . esc_html(sanitize_text_field($_SERVER['SERVER_SOFTWARE'] ?? 'Unknown')) . '</li>';
196 echo '<li><strong>' . esc_html__('MySQL Version:', 'version-info') . '</strong> ' . esc_html($wpdb->db_version()) . '</li>';
197 echo '</ul>';
198 }
199
200 /**
201 * Render the settings page with proper nonce verification for security.
202 */
203 public function render_settings_page() {
204 // Handle POST request and verify the nonce.
205 if ($_SERVER['REQUEST_METHOD'] === 'POST') {
206 if (!isset($_POST['version_info_settings_nonce']) ||
207 !wp_verify_nonce($_POST['version_info_settings_nonce'], 'version_info_settings_action')) {
208 wp_die(__('Security check failed.', 'version-info')); // CSRF protection
209 }
210 }
211 ?>
212 <div class="wrap">
213 <h1><?php _e('Version Info Settings', 'version-info'); ?></h1>
214 <form method="post" action="options.php">
215 <?php
216 // Output nonce, action, and option group fields for the settings form.
217 settings_fields('version_info_settings_group');
218 wp_nonce_field('version_info_settings_action', 'version_info_settings_nonce');
219 do_settings_sections('version-info-settings');
220 ?>
221 <table class="form-table">
222 <tr>
223 <th><?php _e('Show Version Info in Admin Bar', 'version-info'); ?></th>
224 <td>
225 <input type="checkbox" name="version_info_show_admin_bar" value="1"
226 <?php checked(1, get_option('version_info_show_admin_bar', false)); ?> />
227 </td>
228 </tr>
229 <tr>
230 <th><?php _e('Show Version Info as Dashboard Widget', 'version-info'); ?></th>
231 <td>
232 <input type="checkbox" name="version_info_show_dashboard_widget" value="1"
233 <?php checked(1, get_option('version_info_show_dashboard_widget', false)); ?> />
234 </td>
235 </tr>
236 <tr>
237 <th><?php _e('Show Version Info in Footer', 'version-info'); ?></th>
238 <td>
239 <input type="checkbox" name="version_info_show_footer" value="1"
240 <?php checked(1, get_option('version_info_show_footer', true)); ?> />
241 </td>
242 </tr>
243 </table>
244 <?php submit_button(); ?>
245 </form>
246 </div>
247 <?php
248 }
249 }
250
251 // Initialize the plugin
252 global $wpdb;
253 new VersionInfo($wpdb);
254