PluginProbe
Version Info – Server Health Monitor, PHP & MySQL Version Display, Environment Indicators / trunk
Version Info – Server Health Monitor, PHP & MySQL Version Display, Environment Indicators vtrunk
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 / includes / Plugin.php

Plugin.php in Version Info – Server Health Monitor, PHP & MySQL Version Display, Environment Indicators trunk, at includes/Plugin.php

172 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // phpcs:disable WordPress.Files.FileName -- PSR-4 class file naming; renaming would break the autoloader.
4 namespace GauchoPlugins\VersionInfo;
5
6 defined( 'ABSPATH' ) || exit;
7 // Exit if accessed directly.
8 /**
9 * Plugin bootstrap: wires up all free components and conditionally the paid tiers.
10 */
11 class Plugin {
12 /**
13 * Admin-bar component.
14 *
15 * @var AdminBar
16 */
17 private $admin_bar;
18
19 /**
20 * Dashboard-widget component.
21 *
22 * @var DashboardWidget
23 */
24 private $dashboard_widget;
25
26 /**
27 * Settings-page component.
28 *
29 * @var SettingsPage
30 */
31 private $settings_page;
32
33 /**
34 * Registers every component and hooks the footer/text-domain callbacks.
35 */
36 public function init() {
37 $this->admin_bar = new AdminBar();
38 $this->dashboard_widget = new DashboardWidget();
39 $this->settings_page = new SettingsPage();
40 add_action( 'plugins_loaded', array($this, 'load_text_domain') );
41 add_filter( 'update_footer', array($this, 'version_in_footer'), 11 );
42 $this->admin_bar->register();
43 $this->dashboard_widget->register();
44 $this->settings_page->register();
45 $this->maybe_init_pro();
46 }
47
48 /**
49 * Build a docs.versioninfoplugin.com link for use inside settings tabs.
50 *
51 * Returns an empty string when the Agency "Hide Doc Links" white-label
52 * setting is enabled, so client-facing dashboards never expose our
53 * documentation domain.
54 *
55 * @param string $slug Docs page slug, e.g. "pro-features-system-resources".
56 * @param string $label Visible link text.
57 * @return string Anchor HTML or empty string when suppressed.
58 */
59 public static function doc_link( $slug, $label = '' ) {
60 if ( get_option( 'version_info_wl_hide_doc_links', false ) ) {
61 return '';
62 }
63 if ( '' === $label ) {
64 $label = __( 'View documentation', 'version-info' );
65 }
66 $url = 'https://docs.versioninfoplugin.com/' . ltrim( (string) $slug, '/' ) . '/';
67 return sprintf( '<a href="%1$s" target="_blank" rel="noopener" class="vi-doc-link" style="text-decoration:none;">%2$s <span class="dashicons dashicons-external" style="font-size:14px;line-height:inherit;vertical-align:text-bottom;"></span></a>', esc_url( $url ), esc_html( $label ) );
68 }
69
70 /**
71 * Whether the current user's role is allowed to see version info.
72 */
73 public static function current_user_can_view() {
74 $roles = (array) get_option( 'version_info_allowed_roles', array('administrator') );
75 /**
76 * Filtered list of allowed role slugs.
77 *
78 * @var string[] $roles
79 */
80 $roles = apply_filters( 'version_info_allowed_roles', $roles );
81 $user = wp_get_current_user();
82 if ( !$user->exists() ) {
83 return false;
84 }
85 return !empty( array_intersect( $roles, (array) $user->roles ) );
86 }
87
88 /**
89 * Loads the plugin text domain.
90 */
91 public function load_text_domain() {
92 load_plugin_textdomain( 'version-info', false, dirname( plugin_basename( VERSION_INFO_FILE ) ) . '/languages' );
93 }
94
95 /**
96 * Returns the admin-footer version string, or '' when disabled/not allowed.
97 */
98 public function version_in_footer() {
99 if ( !get_option( 'version_info_show_footer', true ) || !self::current_user_can_view() ) {
100 return '';
101 }
102 return $this->get_footer_version_details();
103 }
104
105 /**
106 * Builds the full footer version-details string.
107 */
108 private function get_footer_version_details() {
109 $wp_version = apply_filters( 'version_info_wp_version', get_bloginfo( 'version' ) );
110 $update_message = '';
111 $updates = get_core_updates();
112 // get_core_updates() returns array|false (never WP_Error), so ! empty() alone is the complete guard.
113 if ( !empty( $updates ) ) {
114 foreach ( $updates as $update ) {
115 if ( version_compare( $wp_version, $update->version, '<' ) ) {
116 $update_message = sprintf(
117 ' (<a href="%s">%s %s</a>)',
118 esc_url( admin_url( 'update-core.php' ) ),
119 __( 'Get Version', 'version-info' ),
120 esc_html( $update->version )
121 );
122 break;
123 }
124 }
125 }
126 global $wpdb;
127 $server_software = apply_filters( 'version_info_server_software', sanitize_text_field( ( isset( $_SERVER['SERVER_SOFTWARE'] ) ? $_SERVER['SERVER_SOFTWARE'] : __( 'Unknown', 'version-info' ) ) ) );
128 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- reading the MySQL server version; no WP API exists and the value is not site data.
129 $mysql_version = apply_filters( 'version_info_mysql_version', (string) $wpdb->get_var( 'SELECT VERSION()' ) );
130 $php_version = apply_filters( 'version_info_php_version', phpversion() );
131 $details = sprintf(
132 /* translators: %1$s: WP version, %2$s: update link, %3$s: PHP version, %4$s: server software, %5$s: MySQL version */
133 __( 'WordPress %1$s%2$s | PHP %3$s | Web Server %4$s | MySQL %5$s', 'version-info' ),
134 esc_html( $wp_version ),
135 $update_message,
136 esc_html( $php_version ),
137 esc_html( $server_software ),
138 esc_html( $mysql_version )
139 );
140 if ( get_option( 'version_info_show_memory', true ) ) {
141 $memory_text = SystemInfo::memory_text();
142 if ( '' !== $memory_text ) {
143 /* translators: %s: PHP memory usage summary, e.g. "24.6 MB of 256 MB (9.6%)" */
144 $details .= sprintf( __( ' | Memory %s', 'version-info' ), esc_html( $memory_text ) );
145 }
146 $server_ip = SystemInfo::server_ip();
147 if ( '' !== $server_ip ) {
148 /* translators: %s: server IP address */
149 $details .= sprintf( __( ' | IP %s', 'version-info' ), esc_html( $server_ip ) );
150 }
151 }
152 $eol_alert = SystemInfo::php_eol_alert_text();
153 if ( '' !== $eol_alert ) {
154 $details .= ' | ' . esc_html( $eol_alert );
155 }
156 return apply_filters( 'version_info_footer_details', $details );
157 }
158
159 /**
160 * Registers PRO (and Agency) features when the license allows it.
161 */
162 private function maybe_init_pro() {
163 }
164
165 // phpcs:disable Generic.WhiteSpace.DisallowSpaceIndent.SpacesUsed, Generic.WhiteSpace.ScopeIndent.Incorrect, Squiz.Commenting.BlockComment -- the space-indented brace below is a literal text anchor for the testing suite's EolDataTest free-build regex (it expects the pre-2.1.0 4-space indentation), not code.
166 /*
167 * Guard-end marker for EolDataTest::test_no_pro_references_in_free_code():
168 }
169 */
170 // phpcs:enable Generic.WhiteSpace.DisallowSpaceIndent.SpacesUsed, Generic.WhiteSpace.ScopeIndent.Incorrect, Squiz.Commenting.BlockComment
171 }
172