PluginProbe
Sessions / 2.3.1
Sessions v2.3.1
2.1.0 2.10.0 2.11.0 2.12.0 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.2.0 2.3.0 2.3.1 2.4.0 2.4.1 2.5.0 2.6.0 2.6.1 2.6.2 2.7.0 2.8.0 2.9.0 2.9.1 3.0.0 3.1.0 3.1.1 All 39 releases
sessions / includes / system / class-environment.php

class-environment.php in Sessions 2.3.1, at includes/system/class-environment.php

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