Completed
Push — master ( 02e362...3c05a4 )
by Fulvio
04:00
created

Settings::get_eval_terminal_password()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * WP PHP Console
4
 *
5
 * This source file is subject to the GNU General Public License v3.0
6
 * that is bundled with this package in the file license.txt.
7
 * It is also available through the world-wide-web at this URL:
8
 * http://www.gnu.org/licenses/gpl-3.0.html
9
 *
10
 * @author    Fulvio Notarstefano <[email protected]>
11
 * @copyright Copyright (c) 2014-2019 Fulvio Notarstefano
12
 * @license   http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
13
 */
14
15
namespace WP_PHP_Console;
16
17
defined( 'ABSPATH' ) or exit;
18
19
/**
20
 * WP PHP Console settings handler.
21
 *
22
 * @since 1.5.0
23
 */
24
class Settings {
25
26
27
	/** @var array settings options */
28
	private static $options = [];
29
30
31
	/**
32
	 * Gets the settings option key.
33
	 *
34
	 * @since 1.6.0
35
	 *
36
	 * @return string
37
	 */
38
	public static function get_settings_key() {
39
40
		return str_replace( '-', '_', Plugin::ID );
41
	}
42
43
44
	/**
45
	 * Gets the plugin settings.
46
	 *
47
	 * @since 1.6.0
48
	 *
49
	 * @return array associative array of key-values
50
	 */
51
	public static function get_settings() {
52
53
		if ( empty( self::$options ) ) {
54
55
			self::$options = wp_parse_args(
56
				(array) get_option( self::get_settings_key(), [] ),
57
				[
58
					'ip'       => '',
59
					'password' => '',
60
					'register' => false,
61
					'short'    => false,
62
					'ssl'      => false,
63
					'stack'    => false,
64
				]
65
			);
66
		}
67
68
		return self::$options;
69
	}
70
71
72
	/**
73
	 * Gets the restricted IP(s), if set.
74
	 *
75
	 * @since 1.6.0
76
	 *
77
	 * @return string[] array of individual IPs or ranges of IPs
78
	 */
79
	public static function get_allowed_ip_masks() {
80
81
		$allowed_ips = self::get_settings()['ip'];
82
83
		return ! empty( $allowed_ips['ip'] ) ? explode( ',', $allowed_ips['ip'] ) : [];
84
	}
85
86
87
	/**
88
	 * Gets the PHP Console terminal password.
89
	 *
90
	 * @since 1.6.0
91
	 *
92
	 * @return string
93
	 */
94
	public static function get_eval_terminal_password() {
95
96
		return self::get_settings()['password'];
97
	}
98
99
100
	/**
101
	 * Determines whether a password exists and is not empty.
102
	 *
103
	 * @since 1.6.0
104
	 *
105
	 * @return bool
106
	 */
107
	public static function has_eval_terminal_password() {
108
109
		$password = self::get_eval_terminal_password();
110
111
		return is_string( $password ) && '' !== trim( $password );
112
	}
113
114
115
	/**
116
	 * Determines whether the PC class should be registered and made available.
117
	 *
118
	 * @since 1.6.0
119
	 *
120
	 * @return bool
121
	 */
122
	public static function should_register_pc_class() {
123
124
		return ! empty( self::get_settings()['register'] );
125
	}
126
127
128
	/**
129
	 * Determines whether PHP Console should only accept secure connections.
130
	 *
131
	 * @since 1.6.0
132
	 *
133
	 * @return bool
134
	 */
135
	public static function should_use_ssl_only() {
136
137
		return ! empty( self::get_settings()['ssl'] );
138
	}
139
140
141
	/**
142
	 * Determines whether the full call stack should be displayed.
143
	 *
144
	 * @since 1.6.0
145
	 *
146
	 * @return bool
147
	 */
148
	public static function should_show_call_stack() {
149
150
		return ! empty( self::get_settings()['stack'] );
151
	}
152
153
154
	/**
155
	 * Determines whether the length of PHP Console error sources should be shortened.
156
	 *
157
	 * @since 1.6.0
158
	 *
159
	 * @return bool
160
	 */
161
	public static function should_use_short_path_names() {
162
163
		return ! empty( self::get_settings()['short'] );
164
	}
165
166
167
}
168