|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
class Environment |
|
|
|
|
|
|
4
|
|
|
{ |
|
5
|
|
|
/** |
|
6
|
|
|
* @var mixed |
|
7
|
|
|
*/ |
|
8
|
|
|
private $env; |
|
9
|
|
|
|
|
10
|
|
|
public function __construct() |
|
|
|
|
|
|
11
|
|
|
{ |
|
12
|
|
|
// Set host name |
|
13
|
|
|
$host = $_SERVER['SERVER_NAME']; |
|
14
|
|
|
|
|
15
|
|
|
// List of our development domains |
|
16
|
|
|
$dev_domains = [ |
|
17
|
|
|
'localhost', |
|
18
|
|
|
]; |
|
19
|
|
|
|
|
20
|
|
|
if (in_array($host, $dev_domains)) { |
|
21
|
|
|
|
|
22
|
|
|
$this->env = 'development'; |
|
23
|
|
|
|
|
24
|
|
|
// Set development ENV |
|
25
|
|
|
if (!defined('WP_ENV')) { |
|
26
|
|
|
define('WP_ENV', 'development'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
// Enable strict error reporting |
|
30
|
|
|
error_reporting(E_ALL | E_STRICT); |
|
31
|
|
|
@ini_set('display_errors', 1); |
|
32
|
|
|
|
|
33
|
|
|
if (!defined('WP_DEBUG')) { |
|
34
|
|
|
define('WP_DEBUG', true); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
} else { |
|
38
|
|
|
|
|
39
|
|
|
$this->env = 'production'; |
|
40
|
|
|
|
|
41
|
|
|
// Set production ENV |
|
42
|
|
|
if (!defined('WP_ENV')) { |
|
43
|
|
|
define('WP_ENV', 'production'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// Limit post revisions to 5. |
|
47
|
|
|
if (!defined('WP_POST_REVISIONS')) { |
|
48
|
|
|
define('WP_POST_REVISIONS', 5); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
// disallow wp files editor. |
|
52
|
|
|
if (!defined('DISALLOW_FILE_EDIT')) { |
|
53
|
|
|
define('DISALLOW_FILE_EDIT', true); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if (!defined('WP_DEBUG')) { |
|
57
|
|
|
define('WP_DEBUG', false); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if (!defined('WP_ENV')) { |
|
63
|
|
|
|
|
64
|
|
|
// Fallback if WP_ENV isn't defined |
|
65
|
|
|
// Used to check for 'development' or 'production' |
|
66
|
|
|
if (!defined('WP_ENV')) { |
|
67
|
|
|
define('WP_ENV', 'production'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return null |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getEnv() |
|
78
|
|
|
{ |
|
79
|
|
|
|
|
80
|
|
|
return $this->env; |
|
81
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
function set_environment() |
|
87
|
|
|
{ |
|
88
|
|
|
|
|
89
|
|
|
$env = new Environment(); |
|
90
|
|
|
return $env; |
|
91
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
add_action('init', 'set_environment', 2); |
|
95
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.