Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
90.91% |
10 / 11 |
CRAP | |
90.38% |
94 / 104 |
Settings | |
0.00% |
0 / 1 |
|
90.91% |
10 / 11 |
17.14 | |
92.16% |
94 / 102 |
__get | |
100.00% |
1 / 1 |
3 | |
100.00% |
5 / 5 |
|||
name | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
init | |
100.00% |
1 / 1 |
1 | |
100.00% |
5 / 5 |
|||
register_settings | |
100.00% |
1 / 1 |
1 | |
100.00% |
37 / 37 |
|||
client_id_field | |
100.00% |
1 / 1 |
1 | |
100.00% |
10 / 10 |
|||
client_secret_field | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
user_registration | |
100.00% |
1 / 1 |
2 | |
100.00% |
12 / 12 |
|||
whitelisted_domains | |
100.00% |
1 / 1 |
1 | |
100.00% |
5 / 5 |
|||
settings_page | |
100.00% |
1 / 1 |
1 | |
100.00% |
8 / 8 |
|||
output | |
100.00% |
1 / 1 |
1 | |
100.00% |
7 / 7 |
|||
disabled | |
0.00% |
0 / 1 |
20 | |
0.00% |
0 / 8 |
1 | <?php |
2 | /** |
3 | * Register the settings under settings page and also |
4 | * provide the interface to retrieve the settings. |
5 | * |
6 | * @package RtCamp\GoogleLogin |
7 | * @since 1.0.0 |
8 | * @author rtCamp <contact@rtcamp.com> |
9 | */ |
10 | |
11 | declare(strict_types=1); |
12 | |
13 | namespace RtCamp\GoogleLogin\Modules; |
14 | |
15 | use RtCamp\GoogleLogin\Interfaces\Module as ModuleInterface; |
16 | |
17 | /** |
18 | * Class Settings. |
19 | * |
20 | * @property string|null whitelisted_domains |
21 | * @property string|null client_id |
22 | * @property string|null client_secret |
23 | * @property bool|null registration_enabled |
24 | * |
25 | * @package RtCamp\GoogleLogin\Modules |
26 | */ |
27 | class Settings implements ModuleInterface { |
28 | |
29 | /** |
30 | * Settings values. |
31 | * |
32 | * @var array |
33 | */ |
34 | public $options; |
35 | |
36 | /** |
37 | * Getters for settings values. |
38 | * |
39 | * @var string[] |
40 | */ |
41 | private $getters = [ |
42 | 'WP_GOOGLE_LOGIN_CLIENT_ID' => 'client_id', |
43 | 'WP_GOOGLE_LOGIN_SECRET' => 'client_secret', |
44 | 'WP_GOOGLE_LOGIN_USER_REGISTRATION' => 'registration_enabled', |
45 | 'WP_GOOGLE_LOGIN_WHITELIST_DOMAINS' => 'whitelisted_domains', |
46 | ]; |
47 | |
48 | /** |
49 | * Getter method. |
50 | * |
51 | * @param string $name Name of option to fetch. |
52 | */ |
53 | public function __get( string $name ) { |
54 | if ( in_array( $name, $this->getters, true ) ) { |
55 | $constant_name = array_search( $name, $this->getters ); |
56 | |
57 | return defined( $constant_name ) ? constant( $constant_name ) : ( $this->options[ $name ] ?? '' ); |
58 | } |
59 | |
60 | return null; |
61 | } |
62 | |
63 | /** |
64 | * Return module name. |
65 | * |
66 | * @return string |
67 | */ |
68 | public function name(): string { |
69 | return 'settings'; |
70 | } |
71 | |
72 | /** |
73 | * Initialization of module. |
74 | * |
75 | * @return void |
76 | */ |
77 | public function init(): void { |
78 | $this->options = get_option( 'wp_google_login_settings', [] ); |
79 | add_action( 'admin_init', [ $this, 'register_settings' ] ); |
80 | add_action( 'admin_menu', [ $this, 'settings_page' ] ); |
81 | } |
82 | |
83 | /** |
84 | * Register the settings, section and fields. |
85 | * |
86 | * @return void |
87 | */ |
88 | public function register_settings(): void { |
89 | register_setting( 'wp_google_login', 'wp_google_login_settings' ); |
90 | |
91 | add_settings_section( |
92 | 'wp_google_login_section', |
93 | __( 'Log in with Google Settings', 'login-with-google' ), |
94 | function () { |
95 | }, |
96 | 'login-with-google' |
97 | ); |
98 | |
99 | add_settings_field( |
100 | 'wp_google_login_client_id', |
101 | __( 'Client ID', 'login-with-google' ), |
102 | [ $this, 'client_id_field' ], |
103 | 'login-with-google', |
104 | 'wp_google_login_section', |
105 | [ 'label_for' => 'client-id' ] |
106 | ); |
107 | |
108 | add_settings_field( |
109 | 'wp_google_login_client_secret', |
110 | __( 'Client Secret', 'login-with-google' ), |
111 | [ $this, 'client_secret_field' ], |
112 | 'login-with-google', |
113 | 'wp_google_login_section', |
114 | [ 'label_for' => 'client-secret' ] |
115 | ); |
116 | |
117 | add_settings_field( |
118 | 'wp_google_allow_registration', |
119 | __( 'Create new user', 'login-with-google' ), |
120 | [ $this, 'user_registration' ], |
121 | 'login-with-google', |
122 | 'wp_google_login_section', |
123 | [ 'label_for' => 'user-registration' ] |
124 | ); |
125 | |
126 | add_settings_field( |
127 | 'wp_google_whitelisted_domain', |
128 | __( 'Whitelisted Domains', 'login-with-google' ), |
129 | [ $this, 'whitelisted_domains' ], |
130 | 'login-with-google', |
131 | 'wp_google_login_section', |
132 | [ 'label_for' => 'whitelisted-domains' ] |
133 | ); |
134 | } |
135 | |
136 | /** |
137 | * Render client ID field. |
138 | * |
139 | * @return void |
140 | */ |
141 | public function client_id_field(): void { ?> |
142 | <input type='text' name='wp_google_login_settings[client_id]' id="client-id" value='<?php echo esc_attr( $this->client_id ); ?>' autocomplete="off" <?php $this->disabled( 'client_id' ); ?> /> |
143 | <p class="description"> |
144 | <?php |
145 | echo wp_kses_post( |
146 | sprintf( |
147 | '<p>%1s <a target="_blank" href="%2s">%3s</a>.</p>', |
148 | esc_html__( 'Create oAuth Client ID and Client Secret at', 'login-with-google' ), |
149 | 'https://console.developers.google.com/apis/dashboard', |
150 | 'console.developers.google.com' |
151 | ) |
152 | ); |
153 | ?> |
154 | </p> |
155 | <?php |
156 | } |
157 | |
158 | /** |
159 | * Render client secret field. |
160 | * |
161 | * @return void |
162 | */ |
163 | public function client_secret_field(): void { |
164 | ?> |
165 | <input type='password' name='wp_google_login_settings[client_secret]' id="client-secret" value='<?php echo esc_attr( $this->client_secret ); ?>' autocomplete="off" <?php $this->disabled( 'client_secret' ); ?> /> |
166 | <?php |
167 | } |
168 | |
169 | /** |
170 | * User registration field. |
171 | * |
172 | * This will tell us whether or not to create the user |
173 | * if the user does not exist on WP application. |
174 | * |
175 | * This is irrespective of registration flag present in Settings > General |
176 | * |
177 | * @return void |
178 | */ |
179 | public function user_registration(): void { |
180 | ?> |
181 | <label style='display:block;margin-top:6px;'><input <?php $this->disabled( 'registration_enabled' ); ?> type='checkbox' |
182 | name='wp_google_login_settings[registration_enabled]' |
183 | id="user-registration" <?php echo esc_attr( checked( $this->registration_enabled ) ); ?> |
184 | value='1'> |
185 | <?php esc_html_e( 'Create a new user account if it does not exist already', 'login-with-google' ); ?> |
186 | </label> |
187 | <p class="description"> |
188 | <?php |
189 | echo wp_kses_post( |
190 | sprintf( |
191 | /* translators: %1s will be replaced by page link */ |
192 | __( 'If this setting is checked, a new user will be created even if <a target="_blank" href="%1s">membership setting</a> is off.', 'login-with-google' ), |
193 | is_multisite() ? 'network/settings.php' : 'options-general.php' |
194 | ) |
195 | ); |
196 | ?> |
197 | </p> |
198 | <?php |
199 | } |
200 | |
201 | /** |
202 | * Whitelisted domains for registration. |
203 | * |
204 | * Only emails belonging to these domains would be preferred |
205 | * for registration. |
206 | * |
207 | * If left blank, all domains would be allowed. |
208 | * |
209 | * @return void |
210 | */ |
211 | public function whitelisted_domains(): void { |
212 | ?> |
213 | <input <?php $this->disabled( 'whitelisted_domains' ); ?> type='text' name='wp_google_login_settings[whitelisted_domains]' id="whitelisted-domains" value='<?php echo esc_attr( $this->whitelisted_domains ); ?>' autocomplete="off" /> |
214 | <p class="description"> |
215 | <?php echo esc_html( __( 'Add each domain comma separated', 'login-with-google' ) ); ?> |
216 | </p> |
217 | <?php |
218 | } |
219 | |
220 | /** |
221 | * Add settings sub-menu page in admin menu. |
222 | * |
223 | * @return void |
224 | */ |
225 | public function settings_page(): void { |
226 | add_options_page( |
227 | __( 'Login with Google settings', 'login-with-google' ), |
228 | __( 'Login with Google', 'login-with-google' ), |
229 | 'manage_options', |
230 | 'login-with-google', |
231 | [ $this, 'output' ] |
232 | ); |
233 | } |
234 | |
235 | /** |
236 | * Output the plugin settings. |
237 | * |
238 | * @return void |
239 | */ |
240 | public function output(): void { |
241 | ?> |
242 | <div class="wrap"> |
243 | <form action='options.php' method='post'> |
244 | <?php |
245 | settings_fields( 'wp_google_login' ); |
246 | do_settings_sections( 'login-with-google' ); |
247 | submit_button(); |
248 | ?> |
249 | </form> |
250 | </div> |
251 | <?php |
252 | } |
253 | |
254 | /** |
255 | * Outputs the disabled attribute if field needs to |
256 | * be disabled. |
257 | * |
258 | * @param string $id Input ID. |
259 | * |
260 | * @return void |
261 | */ |
262 | private function disabled( string $id ): void { |
263 | if ( empty( $id ) ) { |
264 | return; |
265 | } |
266 | |
267 | $constant_name = array_search( $id, $this->getters, true ); |
268 | |
269 | if ( false !== $constant_name ) { |
270 | if ( defined( $constant_name ) ) { |
271 | echo esc_attr( 'disabled="disabled"' ); |
272 | } |
273 | } |
274 | } |
275 | } |