PluginProbe
OPcache Manager / trunk
OPcache Manager vtrunk
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.2.0 1.3.0 1.3.1 1.3.2 2.0.0 2.1.0 2.10.0 2.11.0 2.12.0 2.13.0 2.13.1 2.14.0 2.2.0 2.3.0 2.3.1 2.3.2 2.4.0 2.5.0 2.6.0 All 36 releases
opcache-manager / includes / system / class-environment.php

class-environment.php in OPcache Manager trunk, at includes/system/class-environment.php

411 lines 10.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin environment handling.
4 *
5 * @package System
6 * @author Pierre Lannoy <https://pierre.lannoy.fr/>.
7 * @since 1.0.0
8 */
9
10 namespace OPcacheManager\System;
11
12 use Exception;
13
14 /**
15 * The class responsible to manage and detect plugin environment.
16 *
17 * @package System
18 * @author Pierre Lannoy <https://pierre.lannoy.fr/>.
19 * @since 1.0.0
20 */
21 class Environment {
22
23 /**
24 * Initializes the class and set its properties.
25 *
26 * @since 1.0.0
27 */
28 public function __construct() {
29 }
30
31 /**
32 * Defines all needed globals.
33 *
34 * @since 1.0.0
35 */
36 public static function init() {
37 if ( ! function_exists( 'get_home_path' ) ) {
38 require_once ABSPATH . 'wp-admin/includes/file.php';
39 }
40 $plugin_path = str_replace( OPCM_SLUG . '/includes/system/', OPCM_SLUG . '/', plugin_dir_path( __FILE__ ) );
41 $plugin_path = str_replace( OPCM_SLUG . '\includes\system/', OPCM_SLUG . '/', $plugin_path );
42 $plugin_url = str_replace( OPCM_SLUG . '/includes/system/', OPCM_SLUG . '/', plugin_dir_url( __FILE__ ) );
43 $plugin_relative_url = str_replace( get_site_url() . '/', '', $plugin_url );
44 define( 'OPCM_PLUGIN_DIR', $plugin_path );
45 define( 'OPCM_PLUGIN_URL', $plugin_url );
46 define( 'OPCM_PLUGIN_RELATIVE_URL', $plugin_relative_url );
47 define( 'OPCM_ADMIN_DIR', OPCM_PLUGIN_DIR . 'admin/' );
48 define( 'OPCM_ADMIN_URL', OPCM_PLUGIN_URL . 'admin/' );
49 define( 'OPCM_PUBLIC_DIR', OPCM_PLUGIN_DIR . 'public/' );
50 define( 'OPCM_PUBLIC_URL', OPCM_PLUGIN_URL . 'public/' );
51 define( 'OPCM_INCLUDES_DIR', OPCM_PLUGIN_DIR . 'includes/' );
52 define( 'OPCM_VENDOR_DIR', OPCM_PLUGIN_DIR . 'includes/libraries/' );
53 define( 'OPCM_LANGUAGES_DIR', OPCM_PLUGIN_DIR . 'languages/' );
54 define( 'OPCM_ADMIN_RELATIVE_URL', self::admin_relative_url() );
55 define( 'OPCM_AJAX_RELATIVE_URL', self::ajax_relative_url() );
56 define( 'OPCM_PLUGIN_SIGNATURE', OPCM_PRODUCT_NAME . ' v' . OPCM_VERSION );
57 define( 'OPCM_PLUGIN_AGENT', OPCM_PRODUCT_NAME . ' (' . self::wordpress_version_id() . '; ' . self::plugin_version_id() . '; +' . OPCM_PRODUCT_URL . ')' );
58 define( 'OPCM_ASSETS_ID', OPCM_PRODUCT_ABBREVIATION . '-assets' );
59 define( 'OPCM_ABSPATH', get_home_path() );
60 define( 'OPCM_CONTENT', OPCM_ABSPATH . 'wp-content/' );
61 }
62
63 /**
64 * Get the current execution mode.
65 *
66 * @return integer The current execution mode.
67 * @since 1.0.0
68 */
69 public static function exec_mode() {
70 $req_uri = filter_input( INPUT_SERVER, 'REQUEST_URI' );
71 if ( defined( 'WP_CLI' ) && WP_CLI ) {
72 $id = 1;
73 } elseif ( wp_doing_cron() ) {
74 $id = 2;
75 } elseif ( wp_doing_ajax() ) {
76 $id = 3;
77 } elseif ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) {
78 $id = 4;
79 } elseif ( defined( 'REST_REQUEST ' ) && REST_REQUEST ) {
80 $id = 5;
81 } elseif ( $req_uri ? 0 === strpos( strtolower( $req_uri ), '/wp-json/' ) : false ) {
82 $id = 5;
83 } elseif ( $req_uri ? 0 === strpos( strtolower( $req_uri ), '/feed/' ) : false ) {
84 $id = 6;
85 } elseif ( is_admin() ) {
86 $id = 7;
87 } else {
88 $id = 8;
89 }
90 return $id;
91 }
92
93 /**
94 * Get the current execution mode.
95 *
96 * @return boolean True if metrics are available.
97 * @since 1.0.0
98 */
99 public static function exec_mode_for_metrics() {
100 return in_array( self::exec_mode(), [ 1, 2, 4, 5, 6, 7 ], true );
101 }
102
103 /**
104 * Get the major version number.
105 *
106 * @param string $version Optional. The full version string.
107 * @return string The major version number.
108 * @since 1.0.0
109 */
110 public static function major_version( $version = OPCM_VERSION ) {
111 try {
112 $result = substr( $version, 0, strpos( $version, '.' ) );
113 } catch ( Exception $ex ) {
114 $result = 'x';
115 }
116 return $result;
117 }
118
119 /**
120 * Get the major version number.
121 *
122 * @param string $version Optional. The full version string.
123 * @return string The major version number.
124 * @since 1.0.0
125 */
126 public static function minor_version( $version = OPCM_VERSION ) {
127 try {
128 $result = substr( $version, strpos( $version, '.' ) + 1, 1000 );
129 $result = substr( $result, 0, strpos( $result, '.' ) );
130 } catch ( Exception $ex ) {
131 $result = 'x';
132 }
133 return $result;
134 }
135
136 /**
137 * Get the major version number.
138 *
139 * @param string $version Optional. The full version string.
140 * @return string The major version number.
141 * @since 1.0.0
142 */
143 public static function patch_version( $version = OPCM_VERSION ) {
144 try {
145 $result = substr( $version, strpos( $version, '.' ) + 1, 1000 );
146 $result = substr( $result, strpos( $result, '.' ) + 1, 1000 );
147 if ( strpos( $result, '-' ) > 0 ) {
148 $result = substr( $result, 0, strpos( $result, '-' ) );
149 }
150 } catch ( Exception $ex ) {
151 $result = 'x';
152 }
153 return $result;
154 }
155
156 /**
157 * Get the environment type.
158 *
159 * @return string The environment type.
160 * @since 1.0.0
161 */
162 public static function stage() {
163 if ( function_exists( 'wp_get_environment_type' ) ) {
164 return wp_get_environment_type();
165 }
166 return 'unknown';
167 }
168
169 /**
170 * Verification of WP MU.
171 *
172 * @return boolean True if MU, false otherwise.
173 * @since 1.0.0
174 */
175 public static function is_wordpress_multisite() {
176 return is_multisite();
177 }
178
179 /**
180 * Get the WordPress version ID.
181 *
182 * @return string The WordPress version ID.
183 * @since 1.0.0
184 */
185 public static function wordpress_version_id() {
186 global $wp_version;
187 return 'WordPress/' . $wp_version;
188 }
189
190 /**
191 * Get the WordPress version Text.
192 *
193 * @return string The WordPress version in human-readable text.
194 * @since 1.0.0
195 */
196 public static function wordpress_version_text() {
197 global $wp_version;
198 $s = '';
199 if ( is_multisite() ) {
200 $s = 'MU ';
201 }
202 return 'WordPress ' . $s . $wp_version;
203 }
204
205 /**
206 * Get the WordPress debug status.
207 *
208 * @return string The WordPress debug status in human-readable text.
209 * @since 1.0.0
210 */
211 public static function wordpress_debug_text() {
212 $debug = false;
213 $opt = [];
214 $s = '';
215 if ( defined( 'WP_DEBUG' ) ) {
216 if ( WP_DEBUG ) {
217 $debug = true;
218 if ( defined( 'WP_DEBUG_LOG' ) ) {
219 if ( WP_DEBUG_LOG ) {
220 $opt[] = esc_html__( 'log', 'opcache-manager' );
221 }
222 }
223 if ( defined( 'WP_DEBUG_DISPLAY' ) ) {
224 if ( WP_DEBUG_DISPLAY ) {
225 $opt[] = esc_html__( 'display', 'opcache-manager' );
226 }
227 }
228 $s = implode( ', ', $opt );
229 }
230 }
231 return ( $debug ? esc_html__( 'Debug enabled', 'opcache-manager' ) . ( '' !== $s ? ' (' . $s . ')' : '' ) : esc_html__( 'Debug disabled', 'opcache-manager' ) );
232 }
233
234 /**
235 * Get the plugin version ID.
236 *
237 * @return string The plugin version ID.
238 * @since 1.0.0
239 */
240 public static function plugin_version_id() {
241 return OPCM_PRODUCT_SHORTNAME . '/' . OPCM_VERSION;
242 }
243
244 /**
245 * Get the plugin version text.
246 *
247 * @return string The plugin version in human-readable text.
248 * @since 1.0.0
249 */
250 public static function plugin_version_text() {
251 $s = OPCM_PRODUCT_NAME . ' ' . OPCM_VERSION;
252 if ( defined( 'OPCM_CODENAME' ) ) {
253 if ( OPCM_CODENAME !== '"-"' ) {
254 $s .= ' ' . OPCM_CODENAME;
255 }
256 }
257 return $s;
258 }
259
260 /**
261 * Is the plugin a development preview?
262 *
263 * @return boolean True if it's a development preview, false otherwise.
264 * @since 1.0.0
265 */
266 public static function is_plugin_in_dev_mode() {
267 return ( strpos( OPCM_VERSION, 'dev' ) > 0 );
268 }
269
270 /**
271 * Is the plugin a release candidate?
272 *
273 * @return boolean True if it's a release candidate, false otherwise.
274 * @since 1.0.0
275 */
276 public static function is_plugin_in_rc_mode() {
277 return ( strpos( OPCM_VERSION, 'rc' ) > 0 );
278 }
279
280 /**
281 * Is the plugin production ready?
282 *
283 * @return boolean True if it's production ready, false otherwise.
284 * @since 1.0.0
285 */
286 public static function is_plugin_in_production_mode() {
287 return ( ! self::is_plugin_in_dev_mode() && ! self::is_plugin_in_rc_mode() );
288 }
289
290 /**
291 * Get the PHP version.
292 *
293 * @return string The PHP version.
294 * @since 1.0.0
295 */
296 public static function php_version() {
297 $s = phpversion();
298 if ( strpos( $s, '-' ) > 0 ) {
299 $s = substr( $s, 0, strpos( $s, '-' ) );
300 }
301 return $s;
302 }
303
304 /**
305 * Get the PHP full version.
306 *
307 * @return string The PHP full version.
308 * @since 1.0.0
309 */
310 public static function php_full_version() {
311 return phpversion();
312 }
313
314 /**
315 * Get the PHP version text.
316 *
317 * @return string The PHP version in human-readable text.
318 * @since 1.0.0
319 */
320 public static function php_version_text() {
321 return 'PHP ' . self::php_version();
322 }
323
324 /**
325 * Get the PHP full version text.
326 *
327 * @return string The PHP full version in human-readable text.
328 * @since 1.0.0
329 */
330 public static function php_full_version_text() {
331 return 'PHP ' . self::php_full_version();
332 }
333
334 /**
335 * Get the MYSQL version.
336 *
337 * @return string The MYSQL full version.
338 * @since 1.0.0
339 */
340 public static function mysql_version() {
341 global $wpdb;
342 return $wpdb->db_version();
343 }
344
345 /**
346 * Get the MYSQL version text.
347 *
348 * @return string The MYSQL version in human-readable text.
349 * @since 1.0.0
350 */
351 public static function mysql_version_text() {
352 return 'MySQL ' . self::mysql_version();
353 }
354
355 /**
356 * Get the database name and host.
357 *
358 * @return string The database name and host in human-readable text.
359 * @since 1.0.0
360 */
361 public static function mysql_name_text() {
362 return DB_NAME . '@' . DB_HOST;
363 }
364
365 /**
366 * Get the database charset.
367 *
368 * @return string The charset text.
369 * @since 1.0.0
370 */
371 public static function mysql_charset_text() {
372 return DB_CHARSET;
373 }
374
375 /**
376 * Get the database user.
377 *
378 * @return string The user text.
379 * @since 1.0.0
380 */
381 public static function mysql_user_text() {
382 return DB_USER;
383 }
384
385 /**
386 * The detection of this url allows the plugin to make ajax calls when
387 * the site has not a standard /wp-admin/ path.
388 *
389 * @return string The relative url for ajax calls.
390 * @since 1.0.0
391 */
392 private static function ajax_relative_url() {
393 $url = preg_replace( '/(http[s]?:\/\/.*\/)/iU', '', get_admin_url(), 1 );
394 return ( substr( $url, 0 ) === '/' ? '' : '/' ) . $url . ( substr( $url, -1 ) === '/' ? '' : '/' ) . 'admin-ajax.php';
395 }
396
397 /**
398 * The detection of this url allows the plugin to be called from
399 * WP dashboard when the site has not a standard /wp-admin/ path.
400 *
401 * @return string The relative url for WP admin.
402 * @since 1.0.0
403 */
404 private static function admin_relative_url() {
405 $url = preg_replace( '/(http[s]?:\/\/.*\/)/iU', '', get_admin_url(), 1 );
406 return ( substr( $url, 0 ) === '/' ? '' : '/' ) . $url . ( substr( $url, -1 ) === '/' ? '' : '/' ) . 'admin.php';
407 }
408 }
409
410 Environment::init();
411