PluginProbe
DecaLog / 4.4.0
DecaLog v4.4.0
3.0.2 3.1.0 3.10.0 3.2.0 3.3.0 3.4.0 3.4.1 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.6.3 3.7.0 3.7.1 3.8.0 3.9.0 3.9.1 4.0.0 4.1.0 4.2.0 4.3.0 4.3.1 4.4.0 4.5.0 All 75 releases
decalog / includes / system / class-environment.php

class-environment.php in DecaLog 4.4.0, at includes/system/class-environment.php

497 lines 13.1 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 Decalog\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 $all_plugins_path = str_replace( DECALOG_SLUG . '/includes/system/', '', plugin_dir_path( __FILE__ ) );
38 $all_plugins_path = str_replace( DECALOG_SLUG . '\includes\system/', '', $all_plugins_path );
39 $plugin_path = str_replace( DECALOG_SLUG . '/includes/system/', DECALOG_SLUG . '/', plugin_dir_path( __FILE__ ) );
40 $plugin_path = str_replace( DECALOG_SLUG . '\includes\system/', DECALOG_SLUG . '/', $plugin_path );
41 $plugin_url = str_replace( DECALOG_SLUG . '/includes/system/', DECALOG_SLUG . '/', plugin_dir_url( __FILE__ ) );
42 $plugin_relative_url = str_replace( get_site_url() . '/', '', $plugin_url );
43 define( 'DECALOG_ALL_PLUGINS_DIR', $all_plugins_path );
44 define( 'DECALOG_PLUGIN_DIR', $plugin_path );
45 define( 'DECALOG_PLUGIN_URL', $plugin_url );
46 define( 'DECALOG_PLUGIN_RELATIVE_URL', $plugin_relative_url );
47 define( 'DECALOG_ADMIN_DIR', DECALOG_PLUGIN_DIR . 'admin/' );
48 define( 'DECALOG_ADMIN_URL', DECALOG_PLUGIN_URL . 'admin/' );
49 define( 'DECALOG_PUBLIC_DIR', DECALOG_PLUGIN_DIR . 'public/' );
50 define( 'DECALOG_PUBLIC_URL', DECALOG_PLUGIN_URL . 'public/' );
51 define( 'DECALOG_INCLUDES_DIR', DECALOG_PLUGIN_DIR . 'includes/' );
52 define( 'DECALOG_VENDOR_DIR', DECALOG_PLUGIN_DIR . 'includes/libraries/' );
53 define( 'DECALOG_LISTENERS_DIR', DECALOG_PLUGIN_DIR . 'includes/listeners/' );
54 define( 'DECALOG_LANGUAGES_DIR', DECALOG_PLUGIN_DIR . 'languages/' );
55 define( 'DECALOG_ADMIN_RELATIVE_URL', self::admin_relative_url() );
56 define( 'DECALOG_AJAX_RELATIVE_URL', self::ajax_relative_url() );
57 define( 'DECALOG_PLUGIN_SIGNATURE', DECALOG_PRODUCT_NAME . ' v' . DECALOG_VERSION );
58 define( 'DECALOG_PLUGIN_AGENT', DECALOG_PRODUCT_NAME . ' (' . self::wordpress_version_id() . '; ' . self::plugin_version_id() . '; +' . DECALOG_PRODUCT_URL . ')' );
59 define( 'DECALOG_ASSETS_ID', DECALOG_PRODUCT_ABBREVIATION . '-assets' );
60 define( 'DECALOG_LIVELOG_ID', DECALOG_PRODUCT_ABBREVIATION . '-console' );
61 define( 'DECALOG_REST_NAMESPACE', DECALOG_SLUG . '/v' . DECALOG_API_VERSION );
62 $host = gethostname();
63 if ( $host ) {
64 define( 'DECALOG_INSTANCE_NAME', $host );
65 } else {
66 define( 'DECALOG_INSTANCE_NAME', 'undefined' );
67 }
68
69 }
70
71 /**
72 * Get the current execution mode.
73 *
74 * @return integer The current execution mode.
75 * @since 1.0.0
76 */
77 public static function exec_mode() {
78 $req_uri = filter_input( INPUT_SERVER, 'REQUEST_URI' );
79 if ( defined( 'WP_CLI' ) && WP_CLI ) {
80 $id = 1;
81 } elseif ( wp_doing_cron() ) {
82 $id = 2;
83 } elseif ( wp_doing_ajax() ) {
84 $id = 3;
85 } elseif ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) {
86 $id = 4;
87 } elseif ( defined( 'REST_REQUEST ' ) && REST_REQUEST ) {
88 $id = 5;
89 } elseif ( $req_uri ? 0 === strpos( strtolower( $req_uri ), '/wp-json/' ) : false ) {
90 $id = 5;
91 } elseif ( $req_uri ? 0 === strpos( strtolower( $req_uri ), '/feed/' ) : false ) {
92 $id = 6;
93 } elseif ( is_admin() ) {
94 $id = 7;
95 } else {
96 $id = 8;
97 }
98 return $id;
99 }
100
101 /**
102 * Get the current execution mode.
103 *
104 * @return boolean True if metrics are available.
105 * @since 1.0.0
106 */
107 public static function exec_mode_for_metrics() {
108 return in_array( self::exec_mode(), [ 1, 2, 4, 5, 6, 7 ], true );
109 }
110
111 /**
112 * Get the current execution mode.
113 *
114 * @return boolean True if metrics are available.
115 * @since 3.6.3
116 */
117 public static function exec_mode_for_closing_metrics() {
118 $exec_mode = self::exec_mode();
119 if ( in_array( $exec_mode, [ 2, 4, 5, 6, 7 ], true ) ) {
120 return true;
121 }
122 if ( 1 === $exec_mode ) {
123 return defined( 'DECALOG_FORCE_MONITORING');
124 }
125 return false;
126 }
127
128 /**
129 * Verify scrapping mode..
130 *
131 * @return boolean True if we're in scrapping call for theme/plugin editor.
132 * @since 2.4.0
133 */
134 public static function is_sandboxed() {
135 if ( defined( 'WP_SANDBOX_SCRAPING' ) && WP_SANDBOX_SCRAPING ) {
136 return true;
137 }
138 if ( ! isset( $_REQUEST['wp_scrape_key'] ) || ! isset( $_REQUEST['wp_scrape_nonce'] ) ) {
139 return false;
140 }
141 $key = substr( sanitize_key( wp_unslash( $_REQUEST['wp_scrape_key'] ) ), 0, 32 );
142 $nonce = wp_unslash( $_REQUEST['wp_scrape_nonce'] );
143 if ( get_transient( 'scrape_key_' . $key ) !== $nonce ) {
144 return false;
145 }
146 return true;
147 }
148
149 /**
150 * Get the current url.
151 *
152 * @return string The current full url.
153 * @since 2.4.0
154 */
155 public static function get_current_url() {
156 global $wp;
157 if ( '' === get_option( 'permalink_structure' ) ) {
158 // phpcs:ignore
159 return home_url( add_query_arg( [$_GET], $wp->request ) );
160 }
161 // phpcs:ignore
162 return home_url( trailingslashit( add_query_arg( [$_GET], ( is_object( $wp ) ? $wp->request : [] ) ) ) );
163 }
164
165 /**
166 * Get the major version number.
167 *
168 * @param string $version Optional. The full version string.
169 * @return string The major version number.
170 * @since 1.0.0
171 */
172 public static function major_version( $version = DECALOG_VERSION ) {
173 try {
174 $result = substr( $version, 0, strpos( $version, '.' ) );
175 } catch ( Exception $ex ) {
176 $result = 'x';
177 }
178 return $result;
179 }
180
181 /**
182 * Get the major version number.
183 *
184 * @param string $version Optional. The full version string.
185 * @return string The major version number.
186 * @since 1.0.0
187 */
188 public static function minor_version( $version = DECALOG_VERSION ) {
189 try {
190 $result = substr( $version, strpos( $version, '.' ) + 1, 1000 );
191 $result = substr( $result, 0, strpos( $result, '.' ) );
192 } catch ( Exception $ex ) {
193 $result = 'x';
194 }
195 return $result;
196 }
197
198 /**
199 * Get the major version number.
200 *
201 * @param string $version Optional. The full version string.
202 * @return string The major version number.
203 * @since 1.0.0
204 */
205 public static function patch_version( $version = DECALOG_VERSION ) {
206 try {
207 $result = substr( $version, strpos( $version, '.' ) + 1, 1000 );
208 $result = substr( $result, strpos( $result, '.' ) + 1, 1000 );
209 if ( strpos( $result, '-' ) > 0 ) {
210 $result = substr( $result, 0, strpos( $result, '-' ) );
211 }
212 } catch ( Exception $ex ) {
213 $result = 'x';
214 }
215 return $result;
216 }
217
218 /**
219 * Get the environment type.
220 *
221 * @return string The environment type.
222 * @since 1.0.0
223 */
224 public static function stage() {
225 if ( function_exists( 'wp_get_environment_type' ) ) {
226 return wp_get_environment_type();
227 }
228 return 'unknown';
229 }
230
231 /**
232 * Verification of WP MU.
233 *
234 * @return boolean True if MU, false otherwise.
235 * @since 1.0.0
236 */
237 public static function is_wordpress_multisite() {
238 return is_multisite();
239 }
240
241 /**
242 * Get the WordPress version ID.
243 *
244 * @return string The WordPress version ID.
245 * @since 1.0.0
246 */
247 public static function wordpress_version_id() {
248 global $wp_version;
249 return 'WordPress/' . $wp_version;
250 }
251
252 /**
253 * Get the WordPress version Text.
254 *
255 * @param boolean Optional. Shortened version.
256 * @return string The WordPress version in human-readable text.
257 * @since 1.0.0
258 */
259 public static function wordpress_version_text( $short = false ) {
260 global $wp_version;
261 return ( $short ? 'WP' : 'WordPress ' ) . ( is_multisite() ? 'MU' : '' ) . ' ' . $wp_version;
262 }
263
264 /**
265 * Get the WordPress debug status.
266 *
267 * @return string The WordPress debug status in human-readable text.
268 * @since 1.0.0
269 */
270 public static function wordpress_debug_text() {
271 $debug = false;
272 $opt = [];
273 $s = '';
274 if ( defined( 'WP_DEBUG' ) ) {
275 if ( WP_DEBUG ) {
276 $debug = true;
277 if ( defined( 'WP_DEBUG_LOG' ) ) {
278 if ( WP_DEBUG_LOG ) {
279 $opt[] = decalog_esc_html__( 'log', 'decalog' );
280 }
281 }
282 if ( defined( 'WP_DEBUG_DISPLAY' ) ) {
283 if ( WP_DEBUG_DISPLAY ) {
284 $opt[] = decalog_esc_html__( 'display', 'decalog' );
285 }
286 }
287 $s = implode( ', ', $opt );
288 }
289 }
290 return ( $debug ? decalog_esc_html__( 'Debug enabled', 'decalog' ) . ( '' !== $s ? ' (' . $s . ')' : '' ) : decalog_esc_html__( 'Debug disabled', 'decalog' ) );
291 }
292
293 /**
294 * Get the plugin version ID.
295 *
296 * @return string The plugin version ID.
297 * @since 1.0.0
298 */
299 public static function plugin_version_id() {
300 return DECALOG_PRODUCT_SHORTNAME . '/' . DECALOG_VERSION;
301 }
302
303 /**
304 * Get the plugin version text.
305 *
306 * @return string The plugin version in human-readable text.
307 * @since 1.0.0
308 */
309 public static function plugin_version_text() {
310 $s = DECALOG_PRODUCT_NAME . ' ' . DECALOG_VERSION;
311 if ( defined( 'DECALOG_CODENAME' ) ) {
312 if ( DECALOG_CODENAME !== '"-"' ) {
313 $s .= ' ' . DECALOG_CODENAME;
314 }
315 }
316 return $s;
317 }
318
319 /**
320 * Is the plugin a development preview?
321 *
322 * @return boolean True if it's a development preview, false otherwise.
323 * @since 1.0.0
324 */
325 public static function is_plugin_in_dev_mode() {
326 return ( strpos( DECALOG_VERSION, 'dev' ) > 0 );
327 }
328
329 /**
330 * Is the plugin a release candidate?
331 *
332 * @return boolean True if it's a release candidate, false otherwise.
333 * @since 1.0.0
334 */
335 public static function is_plugin_in_rc_mode() {
336 return ( strpos( DECALOG_VERSION, 'rc' ) > 0 );
337 }
338
339 /**
340 * Is the plugin production ready?
341 *
342 * @return boolean True if it's production ready, false otherwise.
343 * @since 1.0.0
344 */
345 public static function is_plugin_in_production_mode() {
346 return ( ! self::is_plugin_in_dev_mode() && ! self::is_plugin_in_rc_mode() );
347 }
348
349 /**
350 * Get the PHP version.
351 *
352 * @return string The PHP version.
353 * @since 1.0.0
354 */
355 public static function php_version() {
356 $s = phpversion();
357 if ( strpos( $s, '-' ) > 0 ) {
358 $s = substr( $s, 0, strpos( $s, '-' ) );
359 }
360 return $s;
361 }
362
363 /**
364 * Get the PHP full version.
365 *
366 * @return string The PHP full version.
367 * @since 1.0.0
368 */
369 public static function php_full_version() {
370 return phpversion();
371 }
372
373 /**
374 * Get the PHP version text.
375 *
376 * @return string The PHP version in human-readable text.
377 * @since 1.0.0
378 */
379 public static function php_version_text() {
380 return 'PHP ' . self::php_version();
381 }
382
383 /**
384 * Get the PHP full version text.
385 *
386 * @return string The PHP full version in human-readable text.
387 * @since 1.0.0
388 */
389 public static function php_full_version_text() {
390 return 'PHP ' . self::php_full_version();
391 }
392
393 /**
394 * Get the MYSQL version.
395 *
396 * @return string The DB full version.
397 * @since 1.0.0
398 */
399 public static function mysql_version() {
400 global $wpdb;
401 $result = '-';
402 if ( method_exists( $wpdb, 'db_server_info' ) ) {
403 $info = $wpdb->db_server_info();
404 if ( preg_match( '/([0-9\.\-]+\d)\D/x', str_replace( '5.5.5-', '', $info ), $matches ) ) {
405 $result = $matches[1];
406 } elseif ( preg_match( '/([0-9\.\-]+\d)\D/x', $info, $matches ) ) {
407 $result = $matches[1];
408 }
409 }
410 return $result;
411 }
412
413 /**
414 * Get the MYSQL version.
415 *
416 * @return string The DB full model name.
417 * @since 1.0.0
418 */
419 public static function mysql_model() {
420 global $wpdb;
421 $result = 'MySQL compatible';
422 if ( method_exists( $wpdb, 'db_server_info' ) ) {
423 $info = $wpdb->db_server_info();
424 if ( preg_match( '/(mariadb|percona|mysql|postgresql)/iU', $info, $matches ) ) {
425 $result = $matches[1];
426 }
427 }
428 return $result;
429 }
430
431 /**
432 * Get the MYSQL version text.
433 *
434 * @return string The MYSQL version in human-readable text.
435 * @since 1.0.0
436 */
437 public static function mysql_version_text() {
438 return 'MySQL ' . self::mysql_version();
439 }
440
441 /**
442 * Get the database name and host.
443 *
444 * @return string The database name and host in human-readable text.
445 * @since 1.0.0
446 */
447 public static function mysql_name_text() {
448 return DB_NAME . '@' . DB_HOST;
449 }
450
451 /**
452 * Get the database charset.
453 *
454 * @return string The charset text.
455 * @since 1.0.0
456 */
457 public static function mysql_charset_text() {
458 return DB_CHARSET;
459 }
460
461 /**
462 * Get the database user.
463 *
464 * @return string The user text.
465 * @since 1.0.0
466 */
467 public static function mysql_user_text() {
468 return DB_USER;
469 }
470
471 /**
472 * The detection of this url allows the plugin to make ajax calls when
473 * the site has not a standard /wp-admin/ path.
474 *
475 * @return string The relative url for ajax calls.
476 * @since 1.0.0
477 */
478 private static function ajax_relative_url() {
479 $url = preg_replace( '/(http[s]?:\/\/.*\/)/iU', '', get_admin_url(), 1 );
480 return ( substr( $url, 0 ) === '/' ? '' : '/' ) . $url . ( substr( $url, -1 ) === '/' ? '' : '/' ) . 'admin-ajax.php';
481 }
482
483 /**
484 * The detection of this url allows the plugin to be called from
485 * WP dashboard when the site has not a standard /wp-admin/ path.
486 *
487 * @return string The relative url for WP admin.
488 * @since 1.0.0
489 */
490 private static function admin_relative_url() {
491 $url = preg_replace( '/(http[s]?:\/\/.*\/)/iU', '', get_admin_url(), 1 );
492 return ( substr( $url, 0 ) === '/' ? '' : '/' ) . $url . ( substr( $url, -1 ) === '/' ? '' : '/' ) . 'admin.php';
493 }
494 }
495
496 Environment::init();
497